s3_clnt.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761
  1. /* ssl/s3_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <stdio.h>
  151. #include "ssl_locl.h"
  152. #include "kssl_lcl.h"
  153. #include <openssl/buffer.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/objects.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/md5.h>
  158. #ifdef OPENSSL_FIPS
  159. # include <openssl/fips.h>
  160. #endif
  161. #ifndef OPENSSL_NO_DH
  162. # include <openssl/dh.h>
  163. #endif
  164. #include <openssl/bn.h>
  165. #ifndef OPENSSL_NO_ENGINE
  166. # include <openssl/engine.h>
  167. #endif
  168. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
  169. #ifndef OPENSSL_NO_TLSEXT
  170. static int ssl3_check_finished(SSL *s);
  171. #endif
  172. #ifndef OPENSSL_NO_SSL3_METHOD
  173. static const SSL_METHOD *ssl3_get_client_method(int ver)
  174. {
  175. if (ver == SSL3_VERSION)
  176. return (SSLv3_client_method());
  177. else
  178. return (NULL);
  179. }
  180. IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
  181. ssl_undefined_function,
  182. ssl3_connect, ssl3_get_client_method)
  183. #endif
  184. int ssl3_connect(SSL *s)
  185. {
  186. BUF_MEM *buf = NULL;
  187. unsigned long Time = (unsigned long)time(NULL);
  188. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  189. int ret = -1;
  190. int new_state, state, skip = 0;
  191. RAND_add(&Time, sizeof(Time), 0);
  192. ERR_clear_error();
  193. clear_sys_error();
  194. if (s->info_callback != NULL)
  195. cb = s->info_callback;
  196. else if (s->ctx->info_callback != NULL)
  197. cb = s->ctx->info_callback;
  198. s->in_handshake++;
  199. if (!SSL_in_init(s) || SSL_in_before(s))
  200. SSL_clear(s);
  201. #ifndef OPENSSL_NO_HEARTBEATS
  202. /*
  203. * If we're awaiting a HeartbeatResponse, pretend we already got and
  204. * don't await it anymore, because Heartbeats don't make sense during
  205. * handshakes anyway.
  206. */
  207. if (s->tlsext_hb_pending) {
  208. s->tlsext_hb_pending = 0;
  209. s->tlsext_hb_seq++;
  210. }
  211. #endif
  212. for (;;) {
  213. state = s->state;
  214. switch (s->state) {
  215. case SSL_ST_RENEGOTIATE:
  216. s->renegotiate = 1;
  217. s->state = SSL_ST_CONNECT;
  218. s->ctx->stats.sess_connect_renegotiate++;
  219. /* break */
  220. case SSL_ST_BEFORE:
  221. case SSL_ST_CONNECT:
  222. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  223. case SSL_ST_OK | SSL_ST_CONNECT:
  224. s->server = 0;
  225. if (cb != NULL)
  226. cb(s, SSL_CB_HANDSHAKE_START, 1);
  227. if ((s->version & 0xff00) != 0x0300) {
  228. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  229. s->state = SSL_ST_ERR;
  230. ret = -1;
  231. goto end;
  232. }
  233. /* s->version=SSL3_VERSION; */
  234. s->type = SSL_ST_CONNECT;
  235. if (s->init_buf == NULL) {
  236. if ((buf = BUF_MEM_new()) == NULL) {
  237. ret = -1;
  238. s->state = SSL_ST_ERR;
  239. goto end;
  240. }
  241. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  242. ret = -1;
  243. s->state = SSL_ST_ERR;
  244. goto end;
  245. }
  246. s->init_buf = buf;
  247. buf = NULL;
  248. }
  249. if (!ssl3_setup_buffers(s)) {
  250. ret = -1;
  251. goto end;
  252. }
  253. /* setup buffing BIO */
  254. if (!ssl_init_wbio_buffer(s, 0)) {
  255. ret = -1;
  256. s->state = SSL_ST_ERR;
  257. goto end;
  258. }
  259. /* don't push the buffering BIO quite yet */
  260. ssl3_init_finished_mac(s);
  261. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  262. s->ctx->stats.sess_connect++;
  263. s->init_num = 0;
  264. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  265. /*
  266. * Should have been reset by ssl3_get_finished, too.
  267. */
  268. s->s3->change_cipher_spec = 0;
  269. break;
  270. case SSL3_ST_CW_CLNT_HELLO_A:
  271. case SSL3_ST_CW_CLNT_HELLO_B:
  272. s->shutdown = 0;
  273. ret = ssl3_client_hello(s);
  274. if (ret <= 0)
  275. goto end;
  276. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  277. s->init_num = 0;
  278. /* turn on buffering for the next lot of output */
  279. if (s->bbio != s->wbio)
  280. s->wbio = BIO_push(s->bbio, s->wbio);
  281. break;
  282. case SSL3_ST_CR_SRVR_HELLO_A:
  283. case SSL3_ST_CR_SRVR_HELLO_B:
  284. ret = ssl3_get_server_hello(s);
  285. if (ret <= 0)
  286. goto end;
  287. if (s->hit) {
  288. s->state = SSL3_ST_CR_FINISHED_A;
  289. #ifndef OPENSSL_NO_TLSEXT
  290. if (s->tlsext_ticket_expected) {
  291. /* receive renewed session ticket */
  292. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  293. }
  294. #endif
  295. } else {
  296. s->state = SSL3_ST_CR_CERT_A;
  297. }
  298. s->init_num = 0;
  299. break;
  300. case SSL3_ST_CR_CERT_A:
  301. case SSL3_ST_CR_CERT_B:
  302. #ifndef OPENSSL_NO_TLSEXT
  303. /* Noop (ret = 0) for everything but EAP-FAST. */
  304. ret = ssl3_check_finished(s);
  305. if (ret < 0)
  306. goto end;
  307. if (ret == 1) {
  308. s->hit = 1;
  309. s->state = SSL3_ST_CR_FINISHED_A;
  310. s->init_num = 0;
  311. break;
  312. }
  313. #endif
  314. /* Check if it is anon DH/ECDH, SRP auth */
  315. /* or PSK */
  316. if (!
  317. (s->s3->tmp.
  318. new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  319. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  320. ret = ssl3_get_server_certificate(s);
  321. if (ret <= 0)
  322. goto end;
  323. #ifndef OPENSSL_NO_TLSEXT
  324. if (s->tlsext_status_expected)
  325. s->state = SSL3_ST_CR_CERT_STATUS_A;
  326. else
  327. s->state = SSL3_ST_CR_KEY_EXCH_A;
  328. } else {
  329. skip = 1;
  330. s->state = SSL3_ST_CR_KEY_EXCH_A;
  331. }
  332. #else
  333. } else
  334. skip = 1;
  335. s->state = SSL3_ST_CR_KEY_EXCH_A;
  336. #endif
  337. s->init_num = 0;
  338. break;
  339. case SSL3_ST_CR_KEY_EXCH_A:
  340. case SSL3_ST_CR_KEY_EXCH_B:
  341. ret = ssl3_get_key_exchange(s);
  342. if (ret <= 0)
  343. goto end;
  344. s->state = SSL3_ST_CR_CERT_REQ_A;
  345. s->init_num = 0;
  346. /*
  347. * at this point we check that we have the required stuff from
  348. * the server
  349. */
  350. if (!ssl3_check_cert_and_algorithm(s)) {
  351. ret = -1;
  352. s->state = SSL_ST_ERR;
  353. goto end;
  354. }
  355. break;
  356. case SSL3_ST_CR_CERT_REQ_A:
  357. case SSL3_ST_CR_CERT_REQ_B:
  358. ret = ssl3_get_certificate_request(s);
  359. if (ret <= 0)
  360. goto end;
  361. s->state = SSL3_ST_CR_SRVR_DONE_A;
  362. s->init_num = 0;
  363. break;
  364. case SSL3_ST_CR_SRVR_DONE_A:
  365. case SSL3_ST_CR_SRVR_DONE_B:
  366. ret = ssl3_get_server_done(s);
  367. if (ret <= 0)
  368. goto end;
  369. #ifndef OPENSSL_NO_SRP
  370. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  371. if ((ret = SRP_Calc_A_param(s)) <= 0) {
  372. SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
  373. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  374. s->state = SSL_ST_ERR;
  375. goto end;
  376. }
  377. }
  378. #endif
  379. if (s->s3->tmp.cert_req)
  380. s->state = SSL3_ST_CW_CERT_A;
  381. else
  382. s->state = SSL3_ST_CW_KEY_EXCH_A;
  383. s->init_num = 0;
  384. break;
  385. case SSL3_ST_CW_CERT_A:
  386. case SSL3_ST_CW_CERT_B:
  387. case SSL3_ST_CW_CERT_C:
  388. case SSL3_ST_CW_CERT_D:
  389. ret = ssl3_send_client_certificate(s);
  390. if (ret <= 0)
  391. goto end;
  392. s->state = SSL3_ST_CW_KEY_EXCH_A;
  393. s->init_num = 0;
  394. break;
  395. case SSL3_ST_CW_KEY_EXCH_A:
  396. case SSL3_ST_CW_KEY_EXCH_B:
  397. ret = ssl3_send_client_key_exchange(s);
  398. if (ret <= 0)
  399. goto end;
  400. /*
  401. * EAY EAY EAY need to check for DH fix cert sent back
  402. */
  403. /*
  404. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  405. * sent, but no verify packet is sent
  406. */
  407. /*
  408. * XXX: For now, we do not support client authentication in ECDH
  409. * cipher suites with ECDH (rather than ECDSA) certificates. We
  410. * need to skip the certificate verify message when client's
  411. * ECDH public key is sent inside the client certificate.
  412. */
  413. if (s->s3->tmp.cert_req == 1) {
  414. s->state = SSL3_ST_CW_CERT_VRFY_A;
  415. } else {
  416. s->state = SSL3_ST_CW_CHANGE_A;
  417. }
  418. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  419. s->state = SSL3_ST_CW_CHANGE_A;
  420. }
  421. s->init_num = 0;
  422. break;
  423. case SSL3_ST_CW_CERT_VRFY_A:
  424. case SSL3_ST_CW_CERT_VRFY_B:
  425. ret = ssl3_send_client_verify(s);
  426. if (ret <= 0)
  427. goto end;
  428. s->state = SSL3_ST_CW_CHANGE_A;
  429. s->init_num = 0;
  430. break;
  431. case SSL3_ST_CW_CHANGE_A:
  432. case SSL3_ST_CW_CHANGE_B:
  433. ret = ssl3_send_change_cipher_spec(s,
  434. SSL3_ST_CW_CHANGE_A,
  435. SSL3_ST_CW_CHANGE_B);
  436. if (ret <= 0)
  437. goto end;
  438. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  439. s->state = SSL3_ST_CW_FINISHED_A;
  440. #else
  441. if (s->s3->next_proto_neg_seen)
  442. s->state = SSL3_ST_CW_NEXT_PROTO_A;
  443. else
  444. s->state = SSL3_ST_CW_FINISHED_A;
  445. #endif
  446. s->init_num = 0;
  447. s->session->cipher = s->s3->tmp.new_cipher;
  448. #ifdef OPENSSL_NO_COMP
  449. s->session->compress_meth = 0;
  450. #else
  451. if (s->s3->tmp.new_compression == NULL)
  452. s->session->compress_meth = 0;
  453. else
  454. s->session->compress_meth = s->s3->tmp.new_compression->id;
  455. #endif
  456. if (!s->method->ssl3_enc->setup_key_block(s)) {
  457. ret = -1;
  458. s->state = SSL_ST_ERR;
  459. goto end;
  460. }
  461. if (!s->method->ssl3_enc->change_cipher_state(s,
  462. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  463. {
  464. ret = -1;
  465. s->state = SSL_ST_ERR;
  466. goto end;
  467. }
  468. break;
  469. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  470. case SSL3_ST_CW_NEXT_PROTO_A:
  471. case SSL3_ST_CW_NEXT_PROTO_B:
  472. ret = ssl3_send_next_proto(s);
  473. if (ret <= 0)
  474. goto end;
  475. s->state = SSL3_ST_CW_FINISHED_A;
  476. break;
  477. #endif
  478. case SSL3_ST_CW_FINISHED_A:
  479. case SSL3_ST_CW_FINISHED_B:
  480. ret = ssl3_send_finished(s,
  481. SSL3_ST_CW_FINISHED_A,
  482. SSL3_ST_CW_FINISHED_B,
  483. s->method->
  484. ssl3_enc->client_finished_label,
  485. s->method->
  486. ssl3_enc->client_finished_label_len);
  487. if (ret <= 0)
  488. goto end;
  489. s->state = SSL3_ST_CW_FLUSH;
  490. /* clear flags */
  491. s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
  492. if (s->hit) {
  493. s->s3->tmp.next_state = SSL_ST_OK;
  494. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
  495. s->state = SSL_ST_OK;
  496. s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
  497. s->s3->delay_buf_pop_ret = 0;
  498. }
  499. } else {
  500. #ifndef OPENSSL_NO_TLSEXT
  501. /*
  502. * Allow NewSessionTicket if ticket expected
  503. */
  504. if (s->tlsext_ticket_expected)
  505. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  506. else
  507. #endif
  508. s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
  509. }
  510. s->init_num = 0;
  511. break;
  512. #ifndef OPENSSL_NO_TLSEXT
  513. case SSL3_ST_CR_SESSION_TICKET_A:
  514. case SSL3_ST_CR_SESSION_TICKET_B:
  515. ret = ssl3_get_new_session_ticket(s);
  516. if (ret <= 0)
  517. goto end;
  518. s->state = SSL3_ST_CR_FINISHED_A;
  519. s->init_num = 0;
  520. break;
  521. case SSL3_ST_CR_CERT_STATUS_A:
  522. case SSL3_ST_CR_CERT_STATUS_B:
  523. ret = ssl3_get_cert_status(s);
  524. if (ret <= 0)
  525. goto end;
  526. s->state = SSL3_ST_CR_KEY_EXCH_A;
  527. s->init_num = 0;
  528. break;
  529. #endif
  530. case SSL3_ST_CR_FINISHED_A:
  531. case SSL3_ST_CR_FINISHED_B:
  532. if (!s->s3->change_cipher_spec)
  533. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  534. ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
  535. SSL3_ST_CR_FINISHED_B);
  536. if (ret <= 0)
  537. goto end;
  538. if (s->hit)
  539. s->state = SSL3_ST_CW_CHANGE_A;
  540. else
  541. s->state = SSL_ST_OK;
  542. s->init_num = 0;
  543. break;
  544. case SSL3_ST_CW_FLUSH:
  545. s->rwstate = SSL_WRITING;
  546. if (BIO_flush(s->wbio) <= 0) {
  547. ret = -1;
  548. goto end;
  549. }
  550. s->rwstate = SSL_NOTHING;
  551. s->state = s->s3->tmp.next_state;
  552. break;
  553. case SSL_ST_OK:
  554. /* clean a few things up */
  555. ssl3_cleanup_key_block(s);
  556. if (s->init_buf != NULL) {
  557. BUF_MEM_free(s->init_buf);
  558. s->init_buf = NULL;
  559. }
  560. /*
  561. * If we are not 'joining' the last two packets, remove the
  562. * buffering now
  563. */
  564. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  565. ssl_free_wbio_buffer(s);
  566. /* else do it later in ssl3_write */
  567. s->init_num = 0;
  568. s->renegotiate = 0;
  569. s->new_session = 0;
  570. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  571. if (s->hit)
  572. s->ctx->stats.sess_hit++;
  573. ret = 1;
  574. /* s->server=0; */
  575. s->handshake_func = ssl3_connect;
  576. s->ctx->stats.sess_connect_good++;
  577. if (cb != NULL)
  578. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  579. goto end;
  580. /* break; */
  581. case SSL_ST_ERR:
  582. default:
  583. SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
  584. ret = -1;
  585. goto end;
  586. /* break; */
  587. }
  588. /* did we do anything */
  589. if (!s->s3->tmp.reuse_message && !skip) {
  590. if (s->debug) {
  591. if ((ret = BIO_flush(s->wbio)) <= 0)
  592. goto end;
  593. }
  594. if ((cb != NULL) && (s->state != state)) {
  595. new_state = s->state;
  596. s->state = state;
  597. cb(s, SSL_CB_CONNECT_LOOP, 1);
  598. s->state = new_state;
  599. }
  600. }
  601. skip = 0;
  602. }
  603. end:
  604. s->in_handshake--;
  605. if (buf != NULL)
  606. BUF_MEM_free(buf);
  607. if (cb != NULL)
  608. cb(s, SSL_CB_CONNECT_EXIT, ret);
  609. return (ret);
  610. }
  611. int ssl3_client_hello(SSL *s)
  612. {
  613. unsigned char *buf;
  614. unsigned char *p, *d;
  615. int i;
  616. unsigned long l;
  617. int al = 0;
  618. #ifndef OPENSSL_NO_COMP
  619. int j;
  620. SSL_COMP *comp;
  621. #endif
  622. buf = (unsigned char *)s->init_buf->data;
  623. if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
  624. SSL_SESSION *sess = s->session;
  625. if ((sess == NULL) || (sess->ssl_version != s->version) ||
  626. #ifdef OPENSSL_NO_TLSEXT
  627. !sess->session_id_length ||
  628. #else
  629. /*
  630. * In the case of EAP-FAST, we can have a pre-shared
  631. * "ticket" without a session ID.
  632. */
  633. (!sess->session_id_length && !sess->tlsext_tick) ||
  634. #endif
  635. (sess->not_resumable)) {
  636. if (!ssl_get_new_session(s, 0))
  637. goto err;
  638. }
  639. if (s->method->version == DTLS_ANY_VERSION) {
  640. /* Determine which DTLS version to use */
  641. int options = s->options;
  642. /* If DTLS 1.2 disabled correct the version number */
  643. if (options & SSL_OP_NO_DTLSv1_2) {
  644. if (tls1_suiteb(s)) {
  645. SSLerr(SSL_F_SSL3_CLIENT_HELLO,
  646. SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  647. goto err;
  648. }
  649. /*
  650. * Disabling all versions is silly: return an error.
  651. */
  652. if (options & SSL_OP_NO_DTLSv1) {
  653. SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
  654. goto err;
  655. }
  656. /*
  657. * Update method so we don't use any DTLS 1.2 features.
  658. */
  659. s->method = DTLSv1_client_method();
  660. s->version = DTLS1_VERSION;
  661. } else {
  662. /*
  663. * We only support one version: update method
  664. */
  665. if (options & SSL_OP_NO_DTLSv1)
  666. s->method = DTLSv1_2_client_method();
  667. s->version = DTLS1_2_VERSION;
  668. }
  669. s->client_version = s->version;
  670. }
  671. /* else use the pre-loaded session */
  672. p = s->s3->client_random;
  673. /*
  674. * for DTLS if client_random is initialized, reuse it, we are
  675. * required to use same upon reply to HelloVerify
  676. */
  677. if (SSL_IS_DTLS(s)) {
  678. size_t idx;
  679. i = 1;
  680. for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
  681. if (p[idx]) {
  682. i = 0;
  683. break;
  684. }
  685. }
  686. } else
  687. i = 1;
  688. if (i && ssl_fill_hello_random(s, 0, p,
  689. sizeof(s->s3->client_random)) <= 0)
  690. goto err;
  691. /* Do the message type and length last */
  692. d = p = ssl_handshake_start(s);
  693. /*-
  694. * version indicates the negotiated version: for example from
  695. * an SSLv2/v3 compatible client hello). The client_version
  696. * field is the maximum version we permit and it is also
  697. * used in RSA encrypted premaster secrets. Some servers can
  698. * choke if we initially report a higher version then
  699. * renegotiate to a lower one in the premaster secret. This
  700. * didn't happen with TLS 1.0 as most servers supported it
  701. * but it can with TLS 1.1 or later if the server only supports
  702. * 1.0.
  703. *
  704. * Possible scenario with previous logic:
  705. * 1. Client hello indicates TLS 1.2
  706. * 2. Server hello says TLS 1.0
  707. * 3. RSA encrypted premaster secret uses 1.2.
  708. * 4. Handhaked proceeds using TLS 1.0.
  709. * 5. Server sends hello request to renegotiate.
  710. * 6. Client hello indicates TLS v1.0 as we now
  711. * know that is maximum server supports.
  712. * 7. Server chokes on RSA encrypted premaster secret
  713. * containing version 1.0.
  714. *
  715. * For interoperability it should be OK to always use the
  716. * maximum version we support in client hello and then rely
  717. * on the checking of version to ensure the servers isn't
  718. * being inconsistent: for example initially negotiating with
  719. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  720. * client_version in client hello and not resetting it to
  721. * the negotiated version.
  722. */
  723. #if 0
  724. *(p++) = s->version >> 8;
  725. *(p++) = s->version & 0xff;
  726. s->client_version = s->version;
  727. #else
  728. *(p++) = s->client_version >> 8;
  729. *(p++) = s->client_version & 0xff;
  730. #endif
  731. /* Random stuff */
  732. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  733. p += SSL3_RANDOM_SIZE;
  734. /* Session ID */
  735. if (s->new_session)
  736. i = 0;
  737. else
  738. i = s->session->session_id_length;
  739. *(p++) = i;
  740. if (i != 0) {
  741. if (i > (int)sizeof(s->session->session_id)) {
  742. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  743. goto err;
  744. }
  745. memcpy(p, s->session->session_id, i);
  746. p += i;
  747. }
  748. /* cookie stuff for DTLS */
  749. if (SSL_IS_DTLS(s)) {
  750. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  751. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  752. goto err;
  753. }
  754. *(p++) = s->d1->cookie_len;
  755. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  756. p += s->d1->cookie_len;
  757. }
  758. /* Ciphers supported */
  759. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
  760. if (i == 0) {
  761. SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  762. goto err;
  763. }
  764. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  765. /*
  766. * Some servers hang if client hello > 256 bytes as hack workaround
  767. * chop number of supported ciphers to keep it well below this if we
  768. * use TLS v1.2
  769. */
  770. if (TLS1_get_version(s) >= TLS1_2_VERSION
  771. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  772. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  773. #endif
  774. s2n(i, p);
  775. p += i;
  776. /* COMPRESSION */
  777. #ifdef OPENSSL_NO_COMP
  778. *(p++) = 1;
  779. #else
  780. if ((s->options & SSL_OP_NO_COMPRESSION)
  781. || !s->ctx->comp_methods)
  782. j = 0;
  783. else
  784. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  785. *(p++) = 1 + j;
  786. for (i = 0; i < j; i++) {
  787. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  788. *(p++) = comp->id;
  789. }
  790. #endif
  791. *(p++) = 0; /* Add the NULL method */
  792. #ifndef OPENSSL_NO_TLSEXT
  793. /* TLS extensions */
  794. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  795. SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  796. goto err;
  797. }
  798. if ((p =
  799. ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  800. &al)) == NULL) {
  801. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  802. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  803. goto err;
  804. }
  805. #endif
  806. l = p - d;
  807. ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
  808. s->state = SSL3_ST_CW_CLNT_HELLO_B;
  809. }
  810. /* SSL3_ST_CW_CLNT_HELLO_B */
  811. return ssl_do_write(s);
  812. err:
  813. s->state = SSL_ST_ERR;
  814. return (-1);
  815. }
  816. int ssl3_get_server_hello(SSL *s)
  817. {
  818. STACK_OF(SSL_CIPHER) *sk;
  819. const SSL_CIPHER *c;
  820. CERT *ct = s->cert;
  821. unsigned char *p, *d;
  822. int i, al = SSL_AD_INTERNAL_ERROR, ok;
  823. unsigned int j;
  824. long n;
  825. #ifndef OPENSSL_NO_COMP
  826. SSL_COMP *comp;
  827. #endif
  828. /*
  829. * Hello verify request and/or server hello version may not match so set
  830. * first packet if we're negotiating version.
  831. */
  832. if (SSL_IS_DTLS(s))
  833. s->first_packet = 1;
  834. n = s->method->ssl_get_message(s,
  835. SSL3_ST_CR_SRVR_HELLO_A,
  836. SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
  837. if (!ok)
  838. return ((int)n);
  839. if (SSL_IS_DTLS(s)) {
  840. s->first_packet = 0;
  841. if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  842. if (s->d1->send_cookie == 0) {
  843. s->s3->tmp.reuse_message = 1;
  844. return 1;
  845. } else { /* already sent a cookie */
  846. al = SSL_AD_UNEXPECTED_MESSAGE;
  847. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
  848. goto f_err;
  849. }
  850. }
  851. }
  852. if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
  853. al = SSL_AD_UNEXPECTED_MESSAGE;
  854. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
  855. goto f_err;
  856. }
  857. d = p = (unsigned char *)s->init_msg;
  858. if (s->method->version == DTLS_ANY_VERSION) {
  859. /* Work out correct protocol version to use */
  860. int hversion = (p[0] << 8) | p[1];
  861. int options = s->options;
  862. if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
  863. s->method = DTLSv1_2_client_method();
  864. else if (tls1_suiteb(s)) {
  865. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  866. SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  867. s->version = hversion;
  868. al = SSL_AD_PROTOCOL_VERSION;
  869. goto f_err;
  870. } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
  871. s->method = DTLSv1_client_method();
  872. else {
  873. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
  874. s->version = hversion;
  875. al = SSL_AD_PROTOCOL_VERSION;
  876. goto f_err;
  877. }
  878. s->session->ssl_version = s->version = s->method->version;
  879. }
  880. if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
  881. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
  882. s->version = (s->version & 0xff00) | p[1];
  883. al = SSL_AD_PROTOCOL_VERSION;
  884. goto f_err;
  885. }
  886. p += 2;
  887. /* load the server hello data */
  888. /* load the server random */
  889. memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
  890. p += SSL3_RANDOM_SIZE;
  891. s->hit = 0;
  892. /* get the session-id */
  893. j = *(p++);
  894. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
  895. al = SSL_AD_ILLEGAL_PARAMETER;
  896. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
  897. goto f_err;
  898. }
  899. #ifndef OPENSSL_NO_TLSEXT
  900. /*
  901. * Check if we can resume the session based on external pre-shared secret.
  902. * EAP-FAST (RFC 4851) supports two types of session resumption.
  903. * Resumption based on server-side state works with session IDs.
  904. * Resumption based on pre-shared Protected Access Credentials (PACs)
  905. * works by overriding the SessionTicket extension at the application
  906. * layer, and does not send a session ID. (We do not know whether EAP-FAST
  907. * servers would honour the session ID.) Therefore, the session ID alone
  908. * is not a reliable indicator of session resumption, so we first check if
  909. * we can resume, and later peek at the next handshake message to see if the
  910. * server wants to resume.
  911. */
  912. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
  913. s->session->tlsext_tick) {
  914. SSL_CIPHER *pref_cipher = NULL;
  915. s->session->master_key_length = sizeof(s->session->master_key);
  916. if (s->tls_session_secret_cb(s, s->session->master_key,
  917. &s->session->master_key_length,
  918. NULL, &pref_cipher,
  919. s->tls_session_secret_cb_arg)) {
  920. s->session->cipher = pref_cipher ?
  921. pref_cipher : ssl_get_cipher_by_char(s, p + j);
  922. } else {
  923. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  924. al = SSL_AD_INTERNAL_ERROR;
  925. goto f_err;
  926. }
  927. }
  928. #endif /* OPENSSL_NO_TLSEXT */
  929. if (j != 0 && j == s->session->session_id_length
  930. && memcmp(p, s->session->session_id, j) == 0) {
  931. if (s->sid_ctx_length != s->session->sid_ctx_length
  932. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  933. /* actually a client application bug */
  934. al = SSL_AD_ILLEGAL_PARAMETER;
  935. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  936. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  937. goto f_err;
  938. }
  939. s->hit = 1;
  940. } else {
  941. /*
  942. * If we were trying for session-id reuse but the server
  943. * didn't echo the ID, make a new SSL_SESSION.
  944. * In the case of EAP-FAST and PAC, we do not send a session ID,
  945. * so the PAC-based session secret is always preserved. It'll be
  946. * overwritten if the server refuses resumption.
  947. */
  948. if (s->session->session_id_length > 0) {
  949. if (!ssl_get_new_session(s, 0)) {
  950. goto f_err;
  951. }
  952. }
  953. s->session->session_id_length = j;
  954. memcpy(s->session->session_id, p, j); /* j could be 0 */
  955. }
  956. p += j;
  957. c = ssl_get_cipher_by_char(s, p);
  958. if (c == NULL) {
  959. /* unknown cipher */
  960. al = SSL_AD_ILLEGAL_PARAMETER;
  961. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
  962. goto f_err;
  963. }
  964. /* Set version disabled mask now we know version */
  965. if (!SSL_USE_TLS1_2_CIPHERS(s))
  966. ct->mask_ssl = SSL_TLSV1_2;
  967. else
  968. ct->mask_ssl = 0;
  969. /*
  970. * If it is a disabled cipher we didn't send it in client hello, so
  971. * return an error.
  972. */
  973. if (c->algorithm_ssl & ct->mask_ssl ||
  974. c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) {
  975. al = SSL_AD_ILLEGAL_PARAMETER;
  976. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  977. goto f_err;
  978. }
  979. p += ssl_put_cipher_by_char(s, NULL, NULL);
  980. sk = ssl_get_ciphers_by_id(s);
  981. i = sk_SSL_CIPHER_find(sk, c);
  982. if (i < 0) {
  983. /* we did not say we would use this cipher */
  984. al = SSL_AD_ILLEGAL_PARAMETER;
  985. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  986. goto f_err;
  987. }
  988. /*
  989. * Depending on the session caching (internal/external), the cipher
  990. * and/or cipher_id values may not be set. Make sure that cipher_id is
  991. * set and use it for comparison.
  992. */
  993. if (s->session->cipher)
  994. s->session->cipher_id = s->session->cipher->id;
  995. if (s->hit && (s->session->cipher_id != c->id)) {
  996. /* Workaround is now obsolete */
  997. #if 0
  998. if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  999. #endif
  1000. {
  1001. al = SSL_AD_ILLEGAL_PARAMETER;
  1002. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  1003. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  1004. goto f_err;
  1005. }
  1006. }
  1007. s->s3->tmp.new_cipher = c;
  1008. /*
  1009. * Don't digest cached records if no sigalgs: we may need them for client
  1010. * authentication.
  1011. */
  1012. if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
  1013. goto f_err;
  1014. /* lets get the compression algorithm */
  1015. /* COMPRESSION */
  1016. #ifdef OPENSSL_NO_COMP
  1017. if (*(p++) != 0) {
  1018. al = SSL_AD_ILLEGAL_PARAMETER;
  1019. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  1020. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1021. goto f_err;
  1022. }
  1023. /*
  1024. * If compression is disabled we'd better not try to resume a session
  1025. * using compression.
  1026. */
  1027. if (s->session->compress_meth != 0) {
  1028. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  1029. goto f_err;
  1030. }
  1031. #else
  1032. j = *(p++);
  1033. if (s->hit && j != s->session->compress_meth) {
  1034. al = SSL_AD_ILLEGAL_PARAMETER;
  1035. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  1036. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  1037. goto f_err;
  1038. }
  1039. if (j == 0)
  1040. comp = NULL;
  1041. else if (s->options & SSL_OP_NO_COMPRESSION) {
  1042. al = SSL_AD_ILLEGAL_PARAMETER;
  1043. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
  1044. goto f_err;
  1045. } else
  1046. comp = ssl3_comp_find(s->ctx->comp_methods, j);
  1047. if ((j != 0) && (comp == NULL)) {
  1048. al = SSL_AD_ILLEGAL_PARAMETER;
  1049. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  1050. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1051. goto f_err;
  1052. } else {
  1053. s->s3->tmp.new_compression = comp;
  1054. }
  1055. #endif
  1056. #ifndef OPENSSL_NO_TLSEXT
  1057. /* TLS extensions */
  1058. if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
  1059. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
  1060. goto err;
  1061. }
  1062. #endif
  1063. if (p != (d + n)) {
  1064. /* wrong packet length */
  1065. al = SSL_AD_DECODE_ERROR;
  1066. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
  1067. goto f_err;
  1068. }
  1069. return (1);
  1070. f_err:
  1071. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1072. err:
  1073. s->state = SSL_ST_ERR;
  1074. return (-1);
  1075. }
  1076. int ssl3_get_server_certificate(SSL *s)
  1077. {
  1078. int al, i, ok, ret = -1;
  1079. unsigned long n, nc, llen, l;
  1080. X509 *x = NULL;
  1081. const unsigned char *q, *p;
  1082. unsigned char *d;
  1083. STACK_OF(X509) *sk = NULL;
  1084. SESS_CERT *sc;
  1085. EVP_PKEY *pkey = NULL;
  1086. int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
  1087. * KRB5 */
  1088. n = s->method->ssl_get_message(s,
  1089. SSL3_ST_CR_CERT_A,
  1090. SSL3_ST_CR_CERT_B,
  1091. -1, s->max_cert_list, &ok);
  1092. if (!ok)
  1093. return ((int)n);
  1094. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  1095. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  1096. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
  1097. s->s3->tmp.reuse_message = 1;
  1098. return (1);
  1099. }
  1100. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
  1101. al = SSL_AD_UNEXPECTED_MESSAGE;
  1102. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
  1103. goto f_err;
  1104. }
  1105. p = d = (unsigned char *)s->init_msg;
  1106. if ((sk = sk_X509_new_null()) == NULL) {
  1107. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1108. goto err;
  1109. }
  1110. n2l3(p, llen);
  1111. if (llen + 3 != n) {
  1112. al = SSL_AD_DECODE_ERROR;
  1113. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  1114. goto f_err;
  1115. }
  1116. for (nc = 0; nc < llen;) {
  1117. n2l3(p, l);
  1118. if ((l + nc + 3) > llen) {
  1119. al = SSL_AD_DECODE_ERROR;
  1120. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1121. SSL_R_CERT_LENGTH_MISMATCH);
  1122. goto f_err;
  1123. }
  1124. q = p;
  1125. x = d2i_X509(NULL, &q, l);
  1126. if (x == NULL) {
  1127. al = SSL_AD_BAD_CERTIFICATE;
  1128. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
  1129. goto f_err;
  1130. }
  1131. if (q != (p + l)) {
  1132. al = SSL_AD_DECODE_ERROR;
  1133. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1134. SSL_R_CERT_LENGTH_MISMATCH);
  1135. goto f_err;
  1136. }
  1137. if (!sk_X509_push(sk, x)) {
  1138. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1139. goto err;
  1140. }
  1141. x = NULL;
  1142. nc += l + 3;
  1143. p = q;
  1144. }
  1145. i = ssl_verify_cert_chain(s, sk);
  1146. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  1147. #ifndef OPENSSL_NO_KRB5
  1148. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1149. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1150. #endif /* OPENSSL_NO_KRB5 */
  1151. ) {
  1152. al = ssl_verify_alarm_type(s->verify_result);
  1153. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1154. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1155. goto f_err;
  1156. }
  1157. ERR_clear_error(); /* but we keep s->verify_result */
  1158. sc = ssl_sess_cert_new();
  1159. if (sc == NULL)
  1160. goto err;
  1161. if (s->session->sess_cert)
  1162. ssl_sess_cert_free(s->session->sess_cert);
  1163. s->session->sess_cert = sc;
  1164. sc->cert_chain = sk;
  1165. /*
  1166. * Inconsistency alert: cert_chain does include the peer's certificate,
  1167. * which we don't include in s3_srvr.c
  1168. */
  1169. x = sk_X509_value(sk, 0);
  1170. sk = NULL;
  1171. /*
  1172. * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
  1173. */
  1174. pkey = X509_get_pubkey(x);
  1175. /* VRS: allow null cert if auth == KRB5 */
  1176. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1177. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1178. ? 0 : 1;
  1179. #ifdef KSSL_DEBUG
  1180. fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
  1181. fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
  1182. fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
  1183. s->s3->tmp.new_cipher->name,
  1184. s->s3->tmp.new_cipher->algorithm_mkey,
  1185. s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  1186. #endif /* KSSL_DEBUG */
  1187. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
  1188. x = NULL;
  1189. al = SSL3_AL_FATAL;
  1190. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1191. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1192. goto f_err;
  1193. }
  1194. i = ssl_cert_type(x, pkey);
  1195. if (need_cert && i < 0) {
  1196. x = NULL;
  1197. al = SSL3_AL_FATAL;
  1198. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1199. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1200. goto f_err;
  1201. }
  1202. if (need_cert) {
  1203. int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1204. if (exp_idx >= 0 && i != exp_idx) {
  1205. x = NULL;
  1206. al = SSL_AD_ILLEGAL_PARAMETER;
  1207. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1208. SSL_R_WRONG_CERTIFICATE_TYPE);
  1209. goto f_err;
  1210. }
  1211. sc->peer_cert_type = i;
  1212. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1213. /*
  1214. * Why would the following ever happen? We just created sc a couple
  1215. * of lines ago.
  1216. */
  1217. if (sc->peer_pkeys[i].x509 != NULL)
  1218. X509_free(sc->peer_pkeys[i].x509);
  1219. sc->peer_pkeys[i].x509 = x;
  1220. sc->peer_key = &(sc->peer_pkeys[i]);
  1221. if (s->session->peer != NULL)
  1222. X509_free(s->session->peer);
  1223. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1224. s->session->peer = x;
  1225. } else {
  1226. sc->peer_cert_type = i;
  1227. sc->peer_key = NULL;
  1228. if (s->session->peer != NULL)
  1229. X509_free(s->session->peer);
  1230. s->session->peer = NULL;
  1231. }
  1232. s->session->verify_result = s->verify_result;
  1233. x = NULL;
  1234. ret = 1;
  1235. if (0) {
  1236. f_err:
  1237. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1238. err:
  1239. s->state = SSL_ST_ERR;
  1240. }
  1241. EVP_PKEY_free(pkey);
  1242. X509_free(x);
  1243. sk_X509_pop_free(sk, X509_free);
  1244. return (ret);
  1245. }
  1246. int ssl3_get_key_exchange(SSL *s)
  1247. {
  1248. #ifndef OPENSSL_NO_RSA
  1249. unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
  1250. #endif
  1251. EVP_MD_CTX md_ctx;
  1252. unsigned char *param, *p;
  1253. int al, j, ok;
  1254. long i, param_len, n, alg_k, alg_a;
  1255. EVP_PKEY *pkey = NULL;
  1256. const EVP_MD *md = NULL;
  1257. #ifndef OPENSSL_NO_RSA
  1258. RSA *rsa = NULL;
  1259. #endif
  1260. #ifndef OPENSSL_NO_DH
  1261. DH *dh = NULL;
  1262. #endif
  1263. #ifndef OPENSSL_NO_ECDH
  1264. EC_KEY *ecdh = NULL;
  1265. BN_CTX *bn_ctx = NULL;
  1266. EC_POINT *srvr_ecpoint = NULL;
  1267. int curve_nid = 0;
  1268. int encoded_pt_len = 0;
  1269. #endif
  1270. EVP_MD_CTX_init(&md_ctx);
  1271. /*
  1272. * use same message size as in ssl3_get_certificate_request() as
  1273. * ServerKeyExchange message may be skipped
  1274. */
  1275. n = s->method->ssl_get_message(s,
  1276. SSL3_ST_CR_KEY_EXCH_A,
  1277. SSL3_ST_CR_KEY_EXCH_B,
  1278. -1, s->max_cert_list, &ok);
  1279. if (!ok)
  1280. return ((int)n);
  1281. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1282. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  1283. /*
  1284. * Can't skip server key exchange if this is an ephemeral
  1285. * ciphersuite.
  1286. */
  1287. if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
  1288. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1289. al = SSL_AD_UNEXPECTED_MESSAGE;
  1290. goto f_err;
  1291. }
  1292. #ifndef OPENSSL_NO_PSK
  1293. /*
  1294. * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
  1295. * identity hint is sent. Set session->sess_cert anyway to avoid
  1296. * problems later.
  1297. */
  1298. if (alg_k & SSL_kPSK) {
  1299. s->session->sess_cert = ssl_sess_cert_new();
  1300. if (s->ctx->psk_identity_hint)
  1301. OPENSSL_free(s->ctx->psk_identity_hint);
  1302. s->ctx->psk_identity_hint = NULL;
  1303. }
  1304. #endif
  1305. s->s3->tmp.reuse_message = 1;
  1306. return (1);
  1307. }
  1308. param = p = (unsigned char *)s->init_msg;
  1309. if (s->session->sess_cert != NULL) {
  1310. #ifndef OPENSSL_NO_RSA
  1311. if (s->session->sess_cert->peer_rsa_tmp != NULL) {
  1312. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1313. s->session->sess_cert->peer_rsa_tmp = NULL;
  1314. }
  1315. #endif
  1316. #ifndef OPENSSL_NO_DH
  1317. if (s->session->sess_cert->peer_dh_tmp) {
  1318. DH_free(s->session->sess_cert->peer_dh_tmp);
  1319. s->session->sess_cert->peer_dh_tmp = NULL;
  1320. }
  1321. #endif
  1322. #ifndef OPENSSL_NO_ECDH
  1323. if (s->session->sess_cert->peer_ecdh_tmp) {
  1324. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1325. s->session->sess_cert->peer_ecdh_tmp = NULL;
  1326. }
  1327. #endif
  1328. } else {
  1329. s->session->sess_cert = ssl_sess_cert_new();
  1330. }
  1331. /* Total length of the parameters including the length prefix */
  1332. param_len = 0;
  1333. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1334. al = SSL_AD_DECODE_ERROR;
  1335. #ifndef OPENSSL_NO_PSK
  1336. if (alg_k & SSL_kPSK) {
  1337. param_len = 2;
  1338. if (param_len > n) {
  1339. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1340. goto f_err;
  1341. }
  1342. n2s(p, i);
  1343. /*
  1344. * Store PSK identity hint for later use, hint is used in
  1345. * ssl3_send_client_key_exchange. Assume that the maximum length of
  1346. * a PSK identity hint can be as long as the maximum length of a PSK
  1347. * identity.
  1348. */
  1349. if (i > PSK_MAX_IDENTITY_LEN) {
  1350. al = SSL_AD_HANDSHAKE_FAILURE;
  1351. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
  1352. goto f_err;
  1353. }
  1354. if (i > n - param_len) {
  1355. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1356. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1357. goto f_err;
  1358. }
  1359. param_len += i;
  1360. s->session->psk_identity_hint = BUF_strndup((char *)p, i);
  1361. if (s->session->psk_identity_hint == NULL) {
  1362. al = SSL_AD_HANDSHAKE_FAILURE;
  1363. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1364. goto f_err;
  1365. }
  1366. p += i;
  1367. n -= param_len;
  1368. } else
  1369. #endif /* !OPENSSL_NO_PSK */
  1370. #ifndef OPENSSL_NO_SRP
  1371. if (alg_k & SSL_kSRP) {
  1372. param_len = 2;
  1373. if (param_len > n) {
  1374. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1375. goto f_err;
  1376. }
  1377. n2s(p, i);
  1378. if (i > n - param_len) {
  1379. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
  1380. goto f_err;
  1381. }
  1382. param_len += i;
  1383. if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
  1384. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1385. goto err;
  1386. }
  1387. p += i;
  1388. if (2 > n - param_len) {
  1389. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1390. goto f_err;
  1391. }
  1392. param_len += 2;
  1393. n2s(p, i);
  1394. if (i > n - param_len) {
  1395. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
  1396. goto f_err;
  1397. }
  1398. param_len += i;
  1399. if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
  1400. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1401. goto err;
  1402. }
  1403. p += i;
  1404. if (1 > n - param_len) {
  1405. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1406. goto f_err;
  1407. }
  1408. param_len += 1;
  1409. i = (unsigned int)(p[0]);
  1410. p++;
  1411. if (i > n - param_len) {
  1412. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
  1413. goto f_err;
  1414. }
  1415. param_len += i;
  1416. if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
  1417. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1418. goto err;
  1419. }
  1420. p += i;
  1421. if (2 > n - param_len) {
  1422. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1423. goto f_err;
  1424. }
  1425. param_len += 2;
  1426. n2s(p, i);
  1427. if (i > n - param_len) {
  1428. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
  1429. goto f_err;
  1430. }
  1431. param_len += i;
  1432. if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
  1433. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1434. goto err;
  1435. }
  1436. p += i;
  1437. n -= param_len;
  1438. if (!srp_verify_server_param(s, &al)) {
  1439. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
  1440. goto f_err;
  1441. }
  1442. /* We must check if there is a certificate */
  1443. # ifndef OPENSSL_NO_RSA
  1444. if (alg_a & SSL_aRSA)
  1445. pkey =
  1446. X509_get_pubkey(s->session->
  1447. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1448. # else
  1449. if (0) ;
  1450. # endif
  1451. # ifndef OPENSSL_NO_DSA
  1452. else if (alg_a & SSL_aDSS)
  1453. pkey =
  1454. X509_get_pubkey(s->session->
  1455. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1456. x509);
  1457. # endif
  1458. } else
  1459. #endif /* !OPENSSL_NO_SRP */
  1460. #ifndef OPENSSL_NO_RSA
  1461. if (alg_k & SSL_kRSA) {
  1462. /* Temporary RSA keys only allowed in export ciphersuites */
  1463. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  1464. al = SSL_AD_UNEXPECTED_MESSAGE;
  1465. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1466. goto f_err;
  1467. }
  1468. if ((rsa = RSA_new()) == NULL) {
  1469. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1470. goto err;
  1471. }
  1472. param_len = 2;
  1473. if (param_len > n) {
  1474. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1475. goto f_err;
  1476. }
  1477. n2s(p, i);
  1478. if (i > n - param_len) {
  1479. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
  1480. goto f_err;
  1481. }
  1482. param_len += i;
  1483. if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
  1484. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1485. goto err;
  1486. }
  1487. p += i;
  1488. if (2 > n - param_len) {
  1489. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1490. goto f_err;
  1491. }
  1492. param_len += 2;
  1493. n2s(p, i);
  1494. if (i > n - param_len) {
  1495. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
  1496. goto f_err;
  1497. }
  1498. param_len += i;
  1499. if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
  1500. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1501. goto err;
  1502. }
  1503. p += i;
  1504. n -= param_len;
  1505. /* this should be because we are using an export cipher */
  1506. if (alg_a & SSL_aRSA)
  1507. pkey =
  1508. X509_get_pubkey(s->session->
  1509. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1510. else {
  1511. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1512. goto err;
  1513. }
  1514. if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  1515. al = SSL_AD_UNEXPECTED_MESSAGE;
  1516. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1517. goto f_err;
  1518. }
  1519. s->session->sess_cert->peer_rsa_tmp = rsa;
  1520. rsa = NULL;
  1521. }
  1522. #else /* OPENSSL_NO_RSA */
  1523. if (0) ;
  1524. #endif
  1525. #ifndef OPENSSL_NO_DH
  1526. else if (alg_k & SSL_kEDH) {
  1527. if ((dh = DH_new()) == NULL) {
  1528. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
  1529. goto err;
  1530. }
  1531. param_len = 2;
  1532. if (param_len > n) {
  1533. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1534. goto f_err;
  1535. }
  1536. n2s(p, i);
  1537. if (i > n - param_len) {
  1538. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
  1539. goto f_err;
  1540. }
  1541. param_len += i;
  1542. if (!(dh->p = BN_bin2bn(p, i, NULL))) {
  1543. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1544. goto err;
  1545. }
  1546. p += i;
  1547. if (BN_is_zero(dh->p)) {
  1548. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
  1549. goto f_err;
  1550. }
  1551. if (2 > n - param_len) {
  1552. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1553. goto f_err;
  1554. }
  1555. param_len += 2;
  1556. n2s(p, i);
  1557. if (i > n - param_len) {
  1558. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
  1559. goto f_err;
  1560. }
  1561. param_len += i;
  1562. if (!(dh->g = BN_bin2bn(p, i, NULL))) {
  1563. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1564. goto err;
  1565. }
  1566. p += i;
  1567. if (BN_is_zero(dh->g)) {
  1568. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
  1569. goto f_err;
  1570. }
  1571. if (2 > n - param_len) {
  1572. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1573. goto f_err;
  1574. }
  1575. param_len += 2;
  1576. n2s(p, i);
  1577. if (i > n - param_len) {
  1578. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1579. goto f_err;
  1580. }
  1581. param_len += i;
  1582. if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
  1583. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1584. goto err;
  1585. }
  1586. p += i;
  1587. n -= param_len;
  1588. if (BN_is_zero(dh->pub_key)) {
  1589. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
  1590. goto f_err;
  1591. }
  1592. # ifndef OPENSSL_NO_RSA
  1593. if (alg_a & SSL_aRSA)
  1594. pkey =
  1595. X509_get_pubkey(s->session->
  1596. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1597. # else
  1598. if (0) ;
  1599. # endif
  1600. # ifndef OPENSSL_NO_DSA
  1601. else if (alg_a & SSL_aDSS)
  1602. pkey =
  1603. X509_get_pubkey(s->session->
  1604. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1605. x509);
  1606. # endif
  1607. /* else anonymous DH, so no certificate or pkey. */
  1608. s->session->sess_cert->peer_dh_tmp = dh;
  1609. dh = NULL;
  1610. } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
  1611. al = SSL_AD_ILLEGAL_PARAMETER;
  1612. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1613. SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1614. goto f_err;
  1615. }
  1616. #endif /* !OPENSSL_NO_DH */
  1617. #ifndef OPENSSL_NO_ECDH
  1618. else if (alg_k & SSL_kEECDH) {
  1619. EC_GROUP *ngroup;
  1620. const EC_GROUP *group;
  1621. if ((ecdh = EC_KEY_new()) == NULL) {
  1622. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1623. goto err;
  1624. }
  1625. /*
  1626. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1627. * public key. Keep accumulating lengths of various components in
  1628. * param_len and make sure it never exceeds n.
  1629. */
  1630. /*
  1631. * XXX: For now we only support named (not generic) curves and the
  1632. * ECParameters in this case is just three bytes. We also need one
  1633. * byte for the length of the encoded point
  1634. */
  1635. param_len = 4;
  1636. if (param_len > n) {
  1637. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1638. goto f_err;
  1639. }
  1640. /*
  1641. * Check curve is one of our preferences, if not server has sent an
  1642. * invalid curve. ECParameters is 3 bytes.
  1643. */
  1644. if (!tls1_check_curve(s, p, 3)) {
  1645. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
  1646. goto f_err;
  1647. }
  1648. if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
  1649. al = SSL_AD_INTERNAL_ERROR;
  1650. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1651. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1652. goto f_err;
  1653. }
  1654. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1655. if (ngroup == NULL) {
  1656. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1657. goto err;
  1658. }
  1659. if (EC_KEY_set_group(ecdh, ngroup) == 0) {
  1660. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1661. goto err;
  1662. }
  1663. EC_GROUP_free(ngroup);
  1664. group = EC_KEY_get0_group(ecdh);
  1665. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1666. (EC_GROUP_get_degree(group) > 163)) {
  1667. al = SSL_AD_EXPORT_RESTRICTION;
  1668. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1669. SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1670. goto f_err;
  1671. }
  1672. p += 3;
  1673. /* Next, get the encoded ECPoint */
  1674. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1675. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1676. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1677. goto err;
  1678. }
  1679. encoded_pt_len = *p; /* length of encoded point */
  1680. p += 1;
  1681. if ((encoded_pt_len > n - param_len) ||
  1682. (EC_POINT_oct2point(group, srvr_ecpoint,
  1683. p, encoded_pt_len, bn_ctx) == 0)) {
  1684. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
  1685. goto f_err;
  1686. }
  1687. param_len += encoded_pt_len;
  1688. n -= param_len;
  1689. p += encoded_pt_len;
  1690. /*
  1691. * The ECC/TLS specification does not mention the use of DSA to sign
  1692. * ECParameters in the server key exchange message. We do support RSA
  1693. * and ECDSA.
  1694. */
  1695. if (0) ;
  1696. # ifndef OPENSSL_NO_RSA
  1697. else if (alg_a & SSL_aRSA)
  1698. pkey =
  1699. X509_get_pubkey(s->session->
  1700. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1701. # endif
  1702. # ifndef OPENSSL_NO_ECDSA
  1703. else if (alg_a & SSL_aECDSA)
  1704. pkey =
  1705. X509_get_pubkey(s->session->
  1706. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1707. # endif
  1708. /* else anonymous ECDH, so no certificate or pkey. */
  1709. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1710. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1711. ecdh = NULL;
  1712. BN_CTX_free(bn_ctx);
  1713. bn_ctx = NULL;
  1714. EC_POINT_free(srvr_ecpoint);
  1715. srvr_ecpoint = NULL;
  1716. } else if (alg_k) {
  1717. al = SSL_AD_UNEXPECTED_MESSAGE;
  1718. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1719. goto f_err;
  1720. }
  1721. #endif /* !OPENSSL_NO_ECDH */
  1722. /* p points to the next byte, there are 'n' bytes left */
  1723. /* if it was signed, check the signature */
  1724. if (pkey != NULL) {
  1725. if (SSL_USE_SIGALGS(s)) {
  1726. int rv;
  1727. if (2 > n) {
  1728. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1729. goto f_err;
  1730. }
  1731. rv = tls12_check_peer_sigalg(&md, s, p, pkey);
  1732. if (rv == -1)
  1733. goto err;
  1734. else if (rv == 0) {
  1735. goto f_err;
  1736. }
  1737. #ifdef SSL_DEBUG
  1738. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1739. #endif
  1740. p += 2;
  1741. n -= 2;
  1742. } else
  1743. md = EVP_sha1();
  1744. if (2 > n) {
  1745. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1746. goto f_err;
  1747. }
  1748. n2s(p, i);
  1749. n -= 2;
  1750. j = EVP_PKEY_size(pkey);
  1751. /*
  1752. * Check signature length. If n is 0 then signature is empty
  1753. */
  1754. if ((i != n) || (n > j) || (n <= 0)) {
  1755. /* wrong packet length */
  1756. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
  1757. goto f_err;
  1758. }
  1759. #ifndef OPENSSL_NO_RSA
  1760. if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
  1761. int num;
  1762. unsigned int size;
  1763. j = 0;
  1764. q = md_buf;
  1765. for (num = 2; num > 0; num--) {
  1766. EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1767. if (EVP_DigestInit_ex(&md_ctx,
  1768. (num == 2) ? s->ctx->md5 : s->ctx->sha1,
  1769. NULL) <= 0
  1770. || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
  1771. SSL3_RANDOM_SIZE) <= 0
  1772. || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
  1773. SSL3_RANDOM_SIZE) <= 0
  1774. || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0
  1775. || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
  1776. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1777. ERR_R_INTERNAL_ERROR);
  1778. al = SSL_AD_INTERNAL_ERROR;
  1779. goto f_err;
  1780. }
  1781. q += size;
  1782. j += size;
  1783. }
  1784. i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
  1785. if (i < 0) {
  1786. al = SSL_AD_DECRYPT_ERROR;
  1787. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
  1788. goto f_err;
  1789. }
  1790. if (i == 0) {
  1791. /* bad signature */
  1792. al = SSL_AD_DECRYPT_ERROR;
  1793. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1794. goto f_err;
  1795. }
  1796. } else
  1797. #endif
  1798. {
  1799. if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
  1800. || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
  1801. SSL3_RANDOM_SIZE) <= 0
  1802. || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
  1803. SSL3_RANDOM_SIZE) <= 0
  1804. || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) {
  1805. al = SSL_AD_INTERNAL_ERROR;
  1806. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1807. goto f_err;
  1808. }
  1809. if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
  1810. /* bad signature */
  1811. al = SSL_AD_DECRYPT_ERROR;
  1812. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1813. goto f_err;
  1814. }
  1815. }
  1816. } else {
  1817. /* aNULL, aSRP or kPSK do not need public keys */
  1818. if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
  1819. /* Might be wrong key type, check it */
  1820. if (ssl3_check_cert_and_algorithm(s))
  1821. /* Otherwise this shouldn't happen */
  1822. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1823. goto err;
  1824. }
  1825. /* still data left over */
  1826. if (n != 0) {
  1827. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1828. goto f_err;
  1829. }
  1830. }
  1831. EVP_PKEY_free(pkey);
  1832. EVP_MD_CTX_cleanup(&md_ctx);
  1833. return (1);
  1834. f_err:
  1835. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1836. err:
  1837. EVP_PKEY_free(pkey);
  1838. #ifndef OPENSSL_NO_RSA
  1839. if (rsa != NULL)
  1840. RSA_free(rsa);
  1841. #endif
  1842. #ifndef OPENSSL_NO_DH
  1843. if (dh != NULL)
  1844. DH_free(dh);
  1845. #endif
  1846. #ifndef OPENSSL_NO_ECDH
  1847. BN_CTX_free(bn_ctx);
  1848. EC_POINT_free(srvr_ecpoint);
  1849. if (ecdh != NULL)
  1850. EC_KEY_free(ecdh);
  1851. #endif
  1852. EVP_MD_CTX_cleanup(&md_ctx);
  1853. s->state = SSL_ST_ERR;
  1854. return (-1);
  1855. }
  1856. int ssl3_get_certificate_request(SSL *s)
  1857. {
  1858. int ok, ret = 0;
  1859. unsigned long n, nc, l;
  1860. unsigned int llen, ctype_num, i;
  1861. X509_NAME *xn = NULL;
  1862. const unsigned char *p, *q;
  1863. unsigned char *d;
  1864. STACK_OF(X509_NAME) *ca_sk = NULL;
  1865. n = s->method->ssl_get_message(s,
  1866. SSL3_ST_CR_CERT_REQ_A,
  1867. SSL3_ST_CR_CERT_REQ_B,
  1868. -1, s->max_cert_list, &ok);
  1869. if (!ok)
  1870. return ((int)n);
  1871. s->s3->tmp.cert_req = 0;
  1872. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1873. s->s3->tmp.reuse_message = 1;
  1874. /*
  1875. * If we get here we don't need any cached handshake records as we
  1876. * wont be doing client auth.
  1877. */
  1878. if (s->s3->handshake_buffer) {
  1879. if (!ssl3_digest_cached_records(s))
  1880. goto err;
  1881. }
  1882. return (1);
  1883. }
  1884. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1885. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1886. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
  1887. goto err;
  1888. }
  1889. /* TLS does not like anon-DH with client cert */
  1890. if (s->version > SSL3_VERSION) {
  1891. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
  1892. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1893. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1894. SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1895. goto err;
  1896. }
  1897. }
  1898. p = d = (unsigned char *)s->init_msg;
  1899. if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
  1900. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1901. goto err;
  1902. }
  1903. /* get the certificate types */
  1904. ctype_num = *(p++);
  1905. if (s->cert->ctypes) {
  1906. OPENSSL_free(s->cert->ctypes);
  1907. s->cert->ctypes = NULL;
  1908. }
  1909. if (ctype_num > SSL3_CT_NUMBER) {
  1910. /* If we exceed static buffer copy all to cert structure */
  1911. s->cert->ctypes = OPENSSL_malloc(ctype_num);
  1912. memcpy(s->cert->ctypes, p, ctype_num);
  1913. s->cert->ctype_num = (size_t)ctype_num;
  1914. ctype_num = SSL3_CT_NUMBER;
  1915. }
  1916. for (i = 0; i < ctype_num; i++)
  1917. s->s3->tmp.ctype[i] = p[i];
  1918. p += p[-1];
  1919. if (SSL_USE_SIGALGS(s)) {
  1920. n2s(p, llen);
  1921. /*
  1922. * Check we have enough room for signature algorithms and following
  1923. * length value.
  1924. */
  1925. if ((unsigned long)(p - d + llen + 2) > n) {
  1926. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1927. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1928. SSL_R_DATA_LENGTH_TOO_LONG);
  1929. goto err;
  1930. }
  1931. /* Clear certificate digests and validity flags */
  1932. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1933. s->cert->pkeys[i].digest = NULL;
  1934. s->cert->pkeys[i].valid_flags = 0;
  1935. }
  1936. if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
  1937. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1938. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1939. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1940. goto err;
  1941. }
  1942. if (!tls1_process_sigalgs(s)) {
  1943. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1944. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1945. goto err;
  1946. }
  1947. p += llen;
  1948. }
  1949. /* get the CA RDNs */
  1950. n2s(p, llen);
  1951. #if 0
  1952. {
  1953. FILE *out;
  1954. out = fopen("/tmp/vsign.der", "w");
  1955. fwrite(p, 1, llen, out);
  1956. fclose(out);
  1957. }
  1958. #endif
  1959. if ((unsigned long)(p - d + llen) != n) {
  1960. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1961. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1962. goto err;
  1963. }
  1964. for (nc = 0; nc < llen;) {
  1965. n2s(p, l);
  1966. if ((l + nc + 2) > llen) {
  1967. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1968. goto cont; /* netscape bugs */
  1969. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1970. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
  1971. goto err;
  1972. }
  1973. q = p;
  1974. if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
  1975. /* If netscape tolerance is on, ignore errors */
  1976. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1977. goto cont;
  1978. else {
  1979. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1980. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
  1981. goto err;
  1982. }
  1983. }
  1984. if (q != (p + l)) {
  1985. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1986. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1987. SSL_R_CA_DN_LENGTH_MISMATCH);
  1988. goto err;
  1989. }
  1990. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1991. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1992. goto err;
  1993. }
  1994. p += l;
  1995. nc += l + 2;
  1996. }
  1997. if (0) {
  1998. cont:
  1999. ERR_clear_error();
  2000. }
  2001. /* we should setup a certificate to return.... */
  2002. s->s3->tmp.cert_req = 1;
  2003. s->s3->tmp.ctype_num = ctype_num;
  2004. if (s->s3->tmp.ca_names != NULL)
  2005. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  2006. s->s3->tmp.ca_names = ca_sk;
  2007. ca_sk = NULL;
  2008. ret = 1;
  2009. goto done;
  2010. err:
  2011. s->state = SSL_ST_ERR;
  2012. done:
  2013. if (ca_sk != NULL)
  2014. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  2015. return (ret);
  2016. }
  2017. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  2018. {
  2019. return (X509_NAME_cmp(*a, *b));
  2020. }
  2021. #ifndef OPENSSL_NO_TLSEXT
  2022. int ssl3_get_new_session_ticket(SSL *s)
  2023. {
  2024. int ok, al, ret = 0, ticklen;
  2025. long n;
  2026. const unsigned char *p;
  2027. unsigned char *d;
  2028. unsigned long ticket_lifetime_hint;
  2029. n = s->method->ssl_get_message(s,
  2030. SSL3_ST_CR_SESSION_TICKET_A,
  2031. SSL3_ST_CR_SESSION_TICKET_B,
  2032. SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
  2033. if (!ok)
  2034. return ((int)n);
  2035. if (n < 6) {
  2036. /* need at least ticket_lifetime_hint + ticket length */
  2037. al = SSL_AD_DECODE_ERROR;
  2038. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  2039. goto f_err;
  2040. }
  2041. p = d = (unsigned char *)s->init_msg;
  2042. n2l(p, ticket_lifetime_hint);
  2043. n2s(p, ticklen);
  2044. /* ticket_lifetime_hint + ticket_length + ticket */
  2045. if (ticklen + 6 != n) {
  2046. al = SSL_AD_DECODE_ERROR;
  2047. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  2048. goto f_err;
  2049. }
  2050. /* Server is allowed to change its mind and send an empty ticket. */
  2051. if (ticklen == 0)
  2052. return 1;
  2053. if (s->session->session_id_length > 0) {
  2054. int i = s->session_ctx->session_cache_mode;
  2055. SSL_SESSION *new_sess;
  2056. /*
  2057. * We reused an existing session, so we need to replace it with a new
  2058. * one
  2059. */
  2060. if (i & SSL_SESS_CACHE_CLIENT) {
  2061. /*
  2062. * Remove the old session from the cache
  2063. */
  2064. if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
  2065. if (s->session_ctx->remove_session_cb != NULL)
  2066. s->session_ctx->remove_session_cb(s->session_ctx,
  2067. s->session);
  2068. } else {
  2069. /* We carry on if this fails */
  2070. SSL_CTX_remove_session(s->session_ctx, s->session);
  2071. }
  2072. }
  2073. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  2074. al = SSL_AD_INTERNAL_ERROR;
  2075. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  2076. goto f_err;
  2077. }
  2078. SSL_SESSION_free(s->session);
  2079. s->session = new_sess;
  2080. }
  2081. if (s->session->tlsext_tick) {
  2082. OPENSSL_free(s->session->tlsext_tick);
  2083. s->session->tlsext_ticklen = 0;
  2084. }
  2085. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  2086. if (!s->session->tlsext_tick) {
  2087. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  2088. goto err;
  2089. }
  2090. memcpy(s->session->tlsext_tick, p, ticklen);
  2091. s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  2092. s->session->tlsext_ticklen = ticklen;
  2093. /*
  2094. * There are two ways to detect a resumed ticket session. One is to set
  2095. * an appropriate session ID and then the server must return a match in
  2096. * ServerHello. This allows the normal client session ID matching to work
  2097. * and we know much earlier that the ticket has been accepted. The
  2098. * other way is to set zero length session ID when the ticket is
  2099. * presented and rely on the handshake to determine session resumption.
  2100. * We choose the former approach because this fits in with assumptions
  2101. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  2102. * SHA256 is disabled) hash of the ticket.
  2103. */
  2104. EVP_Digest(p, ticklen,
  2105. s->session->session_id, &s->session->session_id_length,
  2106. # ifndef OPENSSL_NO_SHA256
  2107. EVP_sha256(), NULL);
  2108. # else
  2109. EVP_sha1(), NULL);
  2110. # endif
  2111. ret = 1;
  2112. return (ret);
  2113. f_err:
  2114. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2115. err:
  2116. s->state = SSL_ST_ERR;
  2117. return (-1);
  2118. }
  2119. int ssl3_get_cert_status(SSL *s)
  2120. {
  2121. int ok, al;
  2122. unsigned long resplen, n;
  2123. const unsigned char *p;
  2124. n = s->method->ssl_get_message(s,
  2125. SSL3_ST_CR_CERT_STATUS_A,
  2126. SSL3_ST_CR_CERT_STATUS_B,
  2127. -1, 16384, &ok);
  2128. if (!ok)
  2129. return ((int)n);
  2130. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  2131. /*
  2132. * The CertificateStatus message is optional even if
  2133. * tlsext_status_expected is set
  2134. */
  2135. s->s3->tmp.reuse_message = 1;
  2136. } else {
  2137. if (n < 4) {
  2138. /* need at least status type + length */
  2139. al = SSL_AD_DECODE_ERROR;
  2140. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2141. goto f_err;
  2142. }
  2143. p = (unsigned char *)s->init_msg;
  2144. if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
  2145. al = SSL_AD_DECODE_ERROR;
  2146. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
  2147. goto f_err;
  2148. }
  2149. n2l3(p, resplen);
  2150. if (resplen + 4 != n) {
  2151. al = SSL_AD_DECODE_ERROR;
  2152. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2153. goto f_err;
  2154. }
  2155. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  2156. if (s->tlsext_ocsp_resp == NULL) {
  2157. al = SSL_AD_INTERNAL_ERROR;
  2158. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2159. goto f_err;
  2160. }
  2161. s->tlsext_ocsp_resplen = resplen;
  2162. }
  2163. if (s->ctx->tlsext_status_cb) {
  2164. int ret;
  2165. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  2166. if (ret == 0) {
  2167. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  2168. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
  2169. goto f_err;
  2170. }
  2171. if (ret < 0) {
  2172. al = SSL_AD_INTERNAL_ERROR;
  2173. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2174. goto f_err;
  2175. }
  2176. }
  2177. return 1;
  2178. f_err:
  2179. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2180. s->state = SSL_ST_ERR;
  2181. return (-1);
  2182. }
  2183. #endif
  2184. int ssl3_get_server_done(SSL *s)
  2185. {
  2186. int ok, ret = 0;
  2187. long n;
  2188. /* Second to last param should be very small, like 0 :-) */
  2189. n = s->method->ssl_get_message(s,
  2190. SSL3_ST_CR_SRVR_DONE_A,
  2191. SSL3_ST_CR_SRVR_DONE_B,
  2192. SSL3_MT_SERVER_DONE, 30, &ok);
  2193. if (!ok)
  2194. return ((int)n);
  2195. if (n > 0) {
  2196. /* should contain no data */
  2197. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2198. SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
  2199. s->state = SSL_ST_ERR;
  2200. return -1;
  2201. }
  2202. ret = 1;
  2203. return (ret);
  2204. }
  2205. #ifndef OPENSSL_NO_DH
  2206. static DH *get_server_static_dh_key(SESS_CERT *scert)
  2207. {
  2208. DH *dh_srvr = NULL;
  2209. EVP_PKEY *spkey = NULL;
  2210. int idx = scert->peer_cert_type;
  2211. if (idx >= 0)
  2212. spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
  2213. if (spkey) {
  2214. dh_srvr = EVP_PKEY_get1_DH(spkey);
  2215. EVP_PKEY_free(spkey);
  2216. }
  2217. if (dh_srvr == NULL)
  2218. SSLerr(SSL_F_GET_SERVER_STATIC_DH_KEY, ERR_R_INTERNAL_ERROR);
  2219. return dh_srvr;
  2220. }
  2221. #endif
  2222. int ssl3_send_client_key_exchange(SSL *s)
  2223. {
  2224. unsigned char *p;
  2225. int n;
  2226. unsigned long alg_k;
  2227. #ifndef OPENSSL_NO_RSA
  2228. unsigned char *q;
  2229. EVP_PKEY *pkey = NULL;
  2230. #endif
  2231. #ifndef OPENSSL_NO_KRB5
  2232. KSSL_ERR kssl_err;
  2233. #endif /* OPENSSL_NO_KRB5 */
  2234. #ifndef OPENSSL_NO_ECDH
  2235. EC_KEY *clnt_ecdh = NULL;
  2236. const EC_POINT *srvr_ecpoint = NULL;
  2237. EVP_PKEY *srvr_pub_pkey = NULL;
  2238. unsigned char *encodedPoint = NULL;
  2239. int encoded_pt_len = 0;
  2240. BN_CTX *bn_ctx = NULL;
  2241. #endif
  2242. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  2243. p = ssl_handshake_start(s);
  2244. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2245. /* Fool emacs indentation */
  2246. if (0) {
  2247. }
  2248. #ifndef OPENSSL_NO_RSA
  2249. else if (alg_k & SSL_kRSA) {
  2250. RSA *rsa;
  2251. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2252. if (s->session->sess_cert == NULL) {
  2253. /*
  2254. * We should always have a server certificate with SSL_kRSA.
  2255. */
  2256. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2257. ERR_R_INTERNAL_ERROR);
  2258. goto err;
  2259. }
  2260. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  2261. rsa = s->session->sess_cert->peer_rsa_tmp;
  2262. else {
  2263. pkey =
  2264. X509_get_pubkey(s->session->
  2265. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
  2266. x509);
  2267. if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
  2268. || (pkey->pkey.rsa == NULL)) {
  2269. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2270. ERR_R_INTERNAL_ERROR);
  2271. EVP_PKEY_free(pkey);
  2272. goto err;
  2273. }
  2274. rsa = pkey->pkey.rsa;
  2275. EVP_PKEY_free(pkey);
  2276. }
  2277. tmp_buf[0] = s->client_version >> 8;
  2278. tmp_buf[1] = s->client_version & 0xff;
  2279. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2280. goto err;
  2281. s->session->master_key_length = sizeof tmp_buf;
  2282. q = p;
  2283. /* Fix buf for TLS and beyond */
  2284. if (s->version > SSL3_VERSION)
  2285. p += 2;
  2286. n = RSA_public_encrypt(sizeof tmp_buf,
  2287. tmp_buf, p, rsa, RSA_PKCS1_PADDING);
  2288. # ifdef PKCS1_CHECK
  2289. if (s->options & SSL_OP_PKCS1_CHECK_1)
  2290. p[1]++;
  2291. if (s->options & SSL_OP_PKCS1_CHECK_2)
  2292. tmp_buf[0] = 0x70;
  2293. # endif
  2294. if (n <= 0) {
  2295. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2296. SSL_R_BAD_RSA_ENCRYPT);
  2297. goto err;
  2298. }
  2299. /* Fix buf for TLS and beyond */
  2300. if (s->version > SSL3_VERSION) {
  2301. s2n(n, q);
  2302. n += 2;
  2303. }
  2304. s->session->master_key_length =
  2305. s->method->ssl3_enc->generate_master_secret(s,
  2306. s->
  2307. session->master_key,
  2308. tmp_buf,
  2309. sizeof tmp_buf);
  2310. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2311. }
  2312. #endif
  2313. #ifndef OPENSSL_NO_KRB5
  2314. else if (alg_k & SSL_kKRB5) {
  2315. krb5_error_code krb5rc;
  2316. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2317. /* krb5_data krb5_ap_req; */
  2318. krb5_data *enc_ticket;
  2319. krb5_data authenticator, *authp = NULL;
  2320. EVP_CIPHER_CTX ciph_ctx;
  2321. const EVP_CIPHER *enc = NULL;
  2322. unsigned char iv[EVP_MAX_IV_LENGTH];
  2323. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2324. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
  2325. int padl, outl = sizeof(epms);
  2326. EVP_CIPHER_CTX_init(&ciph_ctx);
  2327. # ifdef KSSL_DEBUG
  2328. fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
  2329. alg_k, SSL_kKRB5);
  2330. # endif /* KSSL_DEBUG */
  2331. authp = NULL;
  2332. # ifdef KRB5SENDAUTH
  2333. if (KRB5SENDAUTH)
  2334. authp = &authenticator;
  2335. # endif /* KRB5SENDAUTH */
  2336. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
  2337. enc = kssl_map_enc(kssl_ctx->enctype);
  2338. if (enc == NULL)
  2339. goto err;
  2340. # ifdef KSSL_DEBUG
  2341. {
  2342. fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
  2343. if (krb5rc && kssl_err.text)
  2344. fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
  2345. kssl_err.text);
  2346. }
  2347. # endif /* KSSL_DEBUG */
  2348. if (krb5rc) {
  2349. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2350. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  2351. goto err;
  2352. }
  2353. /*-
  2354. * 20010406 VRS - Earlier versions used KRB5 AP_REQ
  2355. * in place of RFC 2712 KerberosWrapper, as in:
  2356. *
  2357. * Send ticket (copy to *p, set n = length)
  2358. * n = krb5_ap_req.length;
  2359. * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  2360. * if (krb5_ap_req.data)
  2361. * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  2362. *
  2363. * Now using real RFC 2712 KerberosWrapper
  2364. * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  2365. * Note: 2712 "opaque" types are here replaced
  2366. * with a 2-byte length followed by the value.
  2367. * Example:
  2368. * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  2369. * Where "xx xx" = length bytes. Shown here with
  2370. * optional authenticator omitted.
  2371. */
  2372. /* KerberosWrapper.Ticket */
  2373. s2n(enc_ticket->length, p);
  2374. memcpy(p, enc_ticket->data, enc_ticket->length);
  2375. p += enc_ticket->length;
  2376. n = enc_ticket->length + 2;
  2377. /* KerberosWrapper.Authenticator */
  2378. if (authp && authp->length) {
  2379. s2n(authp->length, p);
  2380. memcpy(p, authp->data, authp->length);
  2381. p += authp->length;
  2382. n += authp->length + 2;
  2383. free(authp->data);
  2384. authp->data = NULL;
  2385. authp->length = 0;
  2386. } else {
  2387. s2n(0, p); /* null authenticator length */
  2388. n += 2;
  2389. }
  2390. tmp_buf[0] = s->client_version >> 8;
  2391. tmp_buf[1] = s->client_version & 0xff;
  2392. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2393. goto err;
  2394. /*-
  2395. * 20010420 VRS. Tried it this way; failed.
  2396. * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  2397. * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  2398. * kssl_ctx->length);
  2399. * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  2400. */
  2401. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2402. EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
  2403. EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
  2404. sizeof tmp_buf);
  2405. EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
  2406. outl += padl;
  2407. if (outl > (int)sizeof epms) {
  2408. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2409. ERR_R_INTERNAL_ERROR);
  2410. goto err;
  2411. }
  2412. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2413. /* KerberosWrapper.EncryptedPreMasterSecret */
  2414. s2n(outl, p);
  2415. memcpy(p, epms, outl);
  2416. p += outl;
  2417. n += outl + 2;
  2418. s->session->master_key_length =
  2419. s->method->ssl3_enc->generate_master_secret(s,
  2420. s->
  2421. session->master_key,
  2422. tmp_buf,
  2423. sizeof tmp_buf);
  2424. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2425. OPENSSL_cleanse(epms, outl);
  2426. }
  2427. #endif
  2428. #ifndef OPENSSL_NO_DH
  2429. else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
  2430. DH *dh_srvr, *dh_clnt;
  2431. SESS_CERT *scert = s->session->sess_cert;
  2432. if (scert == NULL) {
  2433. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2434. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2435. SSL_R_UNEXPECTED_MESSAGE);
  2436. goto err;
  2437. }
  2438. if (scert->peer_dh_tmp != NULL) {
  2439. dh_srvr = scert->peer_dh_tmp;
  2440. } else {
  2441. dh_srvr = get_server_static_dh_key(scert);
  2442. if (dh_srvr == NULL)
  2443. goto err;
  2444. }
  2445. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  2446. /* Use client certificate key */
  2447. EVP_PKEY *clkey = s->cert->key->privatekey;
  2448. dh_clnt = NULL;
  2449. if (clkey)
  2450. dh_clnt = EVP_PKEY_get1_DH(clkey);
  2451. if (dh_clnt == NULL) {
  2452. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2453. ERR_R_INTERNAL_ERROR);
  2454. goto err;
  2455. }
  2456. } else {
  2457. /* generate a new random key */
  2458. if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
  2459. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2460. goto err;
  2461. }
  2462. if (!DH_generate_key(dh_clnt)) {
  2463. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2464. DH_free(dh_clnt);
  2465. goto err;
  2466. }
  2467. }
  2468. /*
  2469. * use the 'p' output buffer for the DH key, but make sure to
  2470. * clear it out afterwards
  2471. */
  2472. n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
  2473. if (scert->peer_dh_tmp == NULL)
  2474. DH_free(dh_srvr);
  2475. if (n <= 0) {
  2476. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2477. DH_free(dh_clnt);
  2478. goto err;
  2479. }
  2480. /* generate master key from the result */
  2481. s->session->master_key_length =
  2482. s->method->ssl3_enc->generate_master_secret(s,
  2483. s->
  2484. session->master_key,
  2485. p, n);
  2486. /* clean up */
  2487. memset(p, 0, n);
  2488. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  2489. n = 0;
  2490. else {
  2491. /* send off the data */
  2492. n = BN_num_bytes(dh_clnt->pub_key);
  2493. s2n(n, p);
  2494. BN_bn2bin(dh_clnt->pub_key, p);
  2495. n += 2;
  2496. }
  2497. DH_free(dh_clnt);
  2498. }
  2499. #endif
  2500. #ifndef OPENSSL_NO_ECDH
  2501. else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
  2502. const EC_GROUP *srvr_group = NULL;
  2503. EC_KEY *tkey;
  2504. int ecdh_clnt_cert = 0;
  2505. int field_size = 0;
  2506. if (s->session->sess_cert == NULL) {
  2507. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2508. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2509. SSL_R_UNEXPECTED_MESSAGE);
  2510. goto err;
  2511. }
  2512. /*
  2513. * Did we send out the client's ECDH share for use in premaster
  2514. * computation as part of client certificate? If so, set
  2515. * ecdh_clnt_cert to 1.
  2516. */
  2517. if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
  2518. /*-
  2519. * XXX: For now, we do not support client
  2520. * authentication using ECDH certificates.
  2521. * To add such support, one needs to add
  2522. * code that checks for appropriate
  2523. * conditions and sets ecdh_clnt_cert to 1.
  2524. * For example, the cert have an ECC
  2525. * key on the same curve as the server's
  2526. * and the key should be authorized for
  2527. * key agreement.
  2528. *
  2529. * One also needs to add code in ssl3_connect
  2530. * to skip sending the certificate verify
  2531. * message.
  2532. *
  2533. * if ((s->cert->key->privatekey != NULL) &&
  2534. * (s->cert->key->privatekey->type ==
  2535. * EVP_PKEY_EC) && ...)
  2536. * ecdh_clnt_cert = 1;
  2537. */
  2538. }
  2539. if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
  2540. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2541. } else {
  2542. /* Get the Server Public Key from Cert */
  2543. srvr_pub_pkey =
  2544. X509_get_pubkey(s->session->
  2545. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2546. if ((srvr_pub_pkey == NULL)
  2547. || (srvr_pub_pkey->type != EVP_PKEY_EC)
  2548. || (srvr_pub_pkey->pkey.ec == NULL)) {
  2549. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2550. ERR_R_INTERNAL_ERROR);
  2551. goto err;
  2552. }
  2553. tkey = srvr_pub_pkey->pkey.ec;
  2554. }
  2555. srvr_group = EC_KEY_get0_group(tkey);
  2556. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2557. if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
  2558. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2559. ERR_R_INTERNAL_ERROR);
  2560. goto err;
  2561. }
  2562. if ((clnt_ecdh = EC_KEY_new()) == NULL) {
  2563. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2564. ERR_R_MALLOC_FAILURE);
  2565. goto err;
  2566. }
  2567. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  2568. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2569. goto err;
  2570. }
  2571. if (ecdh_clnt_cert) {
  2572. /*
  2573. * Reuse key info from our certificate We only need our
  2574. * private key to perform the ECDH computation.
  2575. */
  2576. const BIGNUM *priv_key;
  2577. tkey = s->cert->key->privatekey->pkey.ec;
  2578. priv_key = EC_KEY_get0_private_key(tkey);
  2579. if (priv_key == NULL) {
  2580. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2581. ERR_R_MALLOC_FAILURE);
  2582. goto err;
  2583. }
  2584. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
  2585. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2586. goto err;
  2587. }
  2588. } else {
  2589. /* Generate a new ECDH key pair */
  2590. if (!(EC_KEY_generate_key(clnt_ecdh))) {
  2591. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2592. ERR_R_ECDH_LIB);
  2593. goto err;
  2594. }
  2595. }
  2596. /*
  2597. * use the 'p' output buffer for the ECDH key, but make sure to
  2598. * clear it out afterwards
  2599. */
  2600. field_size = EC_GROUP_get_degree(srvr_group);
  2601. if (field_size <= 0) {
  2602. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2603. goto err;
  2604. }
  2605. n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
  2606. clnt_ecdh, NULL);
  2607. if (n <= 0) {
  2608. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2609. goto err;
  2610. }
  2611. /* generate master key from the result */
  2612. s->session->master_key_length =
  2613. s->method->ssl3_enc->generate_master_secret(s,
  2614. s->
  2615. session->master_key,
  2616. p, n);
  2617. memset(p, 0, n); /* clean up */
  2618. if (ecdh_clnt_cert) {
  2619. /* Send empty client key exch message */
  2620. n = 0;
  2621. } else {
  2622. /*
  2623. * First check the size of encoding and allocate memory
  2624. * accordingly.
  2625. */
  2626. encoded_pt_len =
  2627. EC_POINT_point2oct(srvr_group,
  2628. EC_KEY_get0_public_key(clnt_ecdh),
  2629. POINT_CONVERSION_UNCOMPRESSED,
  2630. NULL, 0, NULL);
  2631. encodedPoint = (unsigned char *)
  2632. OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
  2633. bn_ctx = BN_CTX_new();
  2634. if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
  2635. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2636. ERR_R_MALLOC_FAILURE);
  2637. goto err;
  2638. }
  2639. /* Encode the public key */
  2640. n = EC_POINT_point2oct(srvr_group,
  2641. EC_KEY_get0_public_key(clnt_ecdh),
  2642. POINT_CONVERSION_UNCOMPRESSED,
  2643. encodedPoint, encoded_pt_len, bn_ctx);
  2644. *p = n; /* length of encoded point */
  2645. /* Encoded point will be copied here */
  2646. p += 1;
  2647. /* copy the point */
  2648. memcpy((unsigned char *)p, encodedPoint, n);
  2649. /* increment n to account for length field */
  2650. n += 1;
  2651. }
  2652. /* Free allocated memory */
  2653. BN_CTX_free(bn_ctx);
  2654. if (encodedPoint != NULL)
  2655. OPENSSL_free(encodedPoint);
  2656. if (clnt_ecdh != NULL)
  2657. EC_KEY_free(clnt_ecdh);
  2658. EVP_PKEY_free(srvr_pub_pkey);
  2659. }
  2660. #endif /* !OPENSSL_NO_ECDH */
  2661. else if (alg_k & SSL_kGOST) {
  2662. /* GOST key exchange message creation */
  2663. EVP_PKEY_CTX *pkey_ctx;
  2664. X509 *peer_cert;
  2665. size_t msglen;
  2666. unsigned int md_len;
  2667. int keytype;
  2668. unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
  2669. EVP_MD_CTX *ukm_hash;
  2670. EVP_PKEY *pub_key;
  2671. /*
  2672. * Get server sertificate PKEY and create ctx from it
  2673. */
  2674. peer_cert =
  2675. s->session->
  2676. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
  2677. if (!peer_cert)
  2678. peer_cert =
  2679. s->session->
  2680. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
  2681. if (!peer_cert) {
  2682. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2683. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2684. goto err;
  2685. }
  2686. pkey_ctx = EVP_PKEY_CTX_new(pub_key =
  2687. X509_get_pubkey(peer_cert), NULL);
  2688. if (pkey_ctx == NULL) {
  2689. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2690. ERR_R_MALLOC_FAILURE);
  2691. goto err;
  2692. }
  2693. /*
  2694. * If we have send a certificate, and certificate key
  2695. *
  2696. * * parameters match those of server certificate, use
  2697. * certificate key for key exchange
  2698. */
  2699. /* Otherwise, generate ephemeral key pair */
  2700. if (pkey_ctx == NULL
  2701. || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2702. /* Generate session key */
  2703. || RAND_bytes(premaster_secret, 32) <= 0) {
  2704. EVP_PKEY_CTX_free(pkey_ctx);
  2705. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2706. ERR_R_INTERNAL_ERROR);
  2707. goto err;
  2708. }
  2709. /*
  2710. * If we have client certificate, use its secret as peer key
  2711. */
  2712. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2713. if (EVP_PKEY_derive_set_peer
  2714. (pkey_ctx, s->cert->key->privatekey) <= 0) {
  2715. /*
  2716. * If there was an error - just ignore it. Ephemeral key
  2717. * * would be used
  2718. */
  2719. ERR_clear_error();
  2720. }
  2721. }
  2722. /*
  2723. * Compute shared IV and store it in algorithm-specific context
  2724. * data
  2725. */
  2726. ukm_hash = EVP_MD_CTX_create();
  2727. if (EVP_DigestInit(ukm_hash,
  2728. EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
  2729. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2730. SSL3_RANDOM_SIZE) <= 0
  2731. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2732. SSL3_RANDOM_SIZE) <= 0
  2733. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2734. EVP_MD_CTX_destroy(ukm_hash);
  2735. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2736. ERR_R_INTERNAL_ERROR);
  2737. goto err;
  2738. }
  2739. EVP_MD_CTX_destroy(ukm_hash);
  2740. if (EVP_PKEY_CTX_ctrl
  2741. (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
  2742. shared_ukm) < 0) {
  2743. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2744. SSL_R_LIBRARY_BUG);
  2745. goto err;
  2746. }
  2747. /* Make GOST keytransport blob message */
  2748. /*
  2749. * Encapsulate it into sequence
  2750. */
  2751. *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2752. msglen = 255;
  2753. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
  2754. <= 0) {
  2755. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2756. SSL_R_LIBRARY_BUG);
  2757. goto err;
  2758. }
  2759. if (msglen >= 0x80) {
  2760. *(p++) = 0x81;
  2761. *(p++) = msglen & 0xff;
  2762. n = msglen + 3;
  2763. } else {
  2764. *(p++) = msglen & 0xff;
  2765. n = msglen + 2;
  2766. }
  2767. memcpy(p, tmp, msglen);
  2768. /* Check if pubkey from client certificate was used */
  2769. if (EVP_PKEY_CTX_ctrl
  2770. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
  2771. /* Set flag "skip certificate verify" */
  2772. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2773. }
  2774. EVP_PKEY_CTX_free(pkey_ctx);
  2775. s->session->master_key_length =
  2776. s->method->ssl3_enc->generate_master_secret(s,
  2777. s->
  2778. session->master_key,
  2779. premaster_secret,
  2780. 32);
  2781. EVP_PKEY_free(pub_key);
  2782. }
  2783. #ifndef OPENSSL_NO_SRP
  2784. else if (alg_k & SSL_kSRP) {
  2785. if (s->srp_ctx.A != NULL) {
  2786. /* send off the data */
  2787. n = BN_num_bytes(s->srp_ctx.A);
  2788. s2n(n, p);
  2789. BN_bn2bin(s->srp_ctx.A, p);
  2790. n += 2;
  2791. } else {
  2792. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2793. ERR_R_INTERNAL_ERROR);
  2794. goto err;
  2795. }
  2796. if (s->session->srp_username != NULL)
  2797. OPENSSL_free(s->session->srp_username);
  2798. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2799. if (s->session->srp_username == NULL) {
  2800. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2801. ERR_R_MALLOC_FAILURE);
  2802. goto err;
  2803. }
  2804. if ((s->session->master_key_length =
  2805. SRP_generate_client_master_secret(s,
  2806. s->session->master_key)) <
  2807. 0) {
  2808. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2809. ERR_R_INTERNAL_ERROR);
  2810. goto err;
  2811. }
  2812. }
  2813. #endif
  2814. #ifndef OPENSSL_NO_PSK
  2815. else if (alg_k & SSL_kPSK) {
  2816. /*
  2817. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2818. * \0-terminated identity. The last byte is for us for simulating
  2819. * strnlen.
  2820. */
  2821. char identity[PSK_MAX_IDENTITY_LEN + 2];
  2822. size_t identity_len;
  2823. unsigned char *t = NULL;
  2824. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
  2825. unsigned int pre_ms_len = 0, psk_len = 0;
  2826. int psk_err = 1;
  2827. n = 0;
  2828. if (s->psk_client_callback == NULL) {
  2829. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2830. SSL_R_PSK_NO_CLIENT_CB);
  2831. goto err;
  2832. }
  2833. memset(identity, 0, sizeof(identity));
  2834. psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
  2835. identity, sizeof(identity) - 1,
  2836. psk_or_pre_ms,
  2837. sizeof(psk_or_pre_ms));
  2838. if (psk_len > PSK_MAX_PSK_LEN) {
  2839. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2840. ERR_R_INTERNAL_ERROR);
  2841. goto psk_err;
  2842. } else if (psk_len == 0) {
  2843. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2844. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2845. goto psk_err;
  2846. }
  2847. identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
  2848. identity_len = strlen(identity);
  2849. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  2850. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2851. ERR_R_INTERNAL_ERROR);
  2852. goto psk_err;
  2853. }
  2854. /* create PSK pre_master_secret */
  2855. pre_ms_len = 2 + psk_len + 2 + psk_len;
  2856. t = psk_or_pre_ms;
  2857. memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
  2858. s2n(psk_len, t);
  2859. memset(t, 0, psk_len);
  2860. t += psk_len;
  2861. s2n(psk_len, t);
  2862. if (s->session->psk_identity_hint != NULL)
  2863. OPENSSL_free(s->session->psk_identity_hint);
  2864. s->session->psk_identity_hint =
  2865. BUF_strdup(s->ctx->psk_identity_hint);
  2866. if (s->ctx->psk_identity_hint != NULL
  2867. && s->session->psk_identity_hint == NULL) {
  2868. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2869. ERR_R_MALLOC_FAILURE);
  2870. goto psk_err;
  2871. }
  2872. if (s->session->psk_identity != NULL)
  2873. OPENSSL_free(s->session->psk_identity);
  2874. s->session->psk_identity = BUF_strdup(identity);
  2875. if (s->session->psk_identity == NULL) {
  2876. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2877. ERR_R_MALLOC_FAILURE);
  2878. goto psk_err;
  2879. }
  2880. s->session->master_key_length =
  2881. s->method->ssl3_enc->generate_master_secret(s,
  2882. s->
  2883. session->master_key,
  2884. psk_or_pre_ms,
  2885. pre_ms_len);
  2886. s2n(identity_len, p);
  2887. memcpy(p, identity, identity_len);
  2888. n = 2 + identity_len;
  2889. psk_err = 0;
  2890. psk_err:
  2891. OPENSSL_cleanse(identity, sizeof(identity));
  2892. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2893. if (psk_err != 0) {
  2894. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2895. goto err;
  2896. }
  2897. }
  2898. #endif
  2899. else {
  2900. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2901. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2902. goto err;
  2903. }
  2904. ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
  2905. s->state = SSL3_ST_CW_KEY_EXCH_B;
  2906. }
  2907. /* SSL3_ST_CW_KEY_EXCH_B */
  2908. return ssl_do_write(s);
  2909. err:
  2910. #ifndef OPENSSL_NO_ECDH
  2911. BN_CTX_free(bn_ctx);
  2912. if (encodedPoint != NULL)
  2913. OPENSSL_free(encodedPoint);
  2914. if (clnt_ecdh != NULL)
  2915. EC_KEY_free(clnt_ecdh);
  2916. EVP_PKEY_free(srvr_pub_pkey);
  2917. #endif
  2918. s->state = SSL_ST_ERR;
  2919. return (-1);
  2920. }
  2921. int ssl3_send_client_verify(SSL *s)
  2922. {
  2923. unsigned char *p;
  2924. unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
  2925. EVP_PKEY *pkey;
  2926. EVP_PKEY_CTX *pctx = NULL;
  2927. EVP_MD_CTX mctx;
  2928. unsigned u = 0;
  2929. unsigned long n;
  2930. int j;
  2931. EVP_MD_CTX_init(&mctx);
  2932. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  2933. p = ssl_handshake_start(s);
  2934. pkey = s->cert->key->privatekey;
  2935. /* Create context from key and test if sha1 is allowed as digest */
  2936. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2937. if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) {
  2938. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2939. goto err;
  2940. }
  2941. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
  2942. if (!SSL_USE_SIGALGS(s))
  2943. s->method->ssl3_enc->cert_verify_mac(s,
  2944. NID_sha1,
  2945. &(data
  2946. [MD5_DIGEST_LENGTH]));
  2947. } else {
  2948. ERR_clear_error();
  2949. }
  2950. /*
  2951. * For TLS v1.2 send signature algorithm and signature using agreed
  2952. * digest and cached handshake records.
  2953. */
  2954. if (SSL_USE_SIGALGS(s)) {
  2955. long hdatalen = 0;
  2956. void *hdata;
  2957. const EVP_MD *md = s->cert->key->digest;
  2958. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2959. if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
  2960. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2961. goto err;
  2962. }
  2963. p += 2;
  2964. #ifdef SSL_DEBUG
  2965. fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
  2966. EVP_MD_name(md));
  2967. #endif
  2968. if (!EVP_SignInit_ex(&mctx, md, NULL)
  2969. || !EVP_SignUpdate(&mctx, hdata, hdatalen)
  2970. || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
  2971. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2972. goto err;
  2973. }
  2974. s2n(u, p);
  2975. n = u + 4;
  2976. if (!ssl3_digest_cached_records(s))
  2977. goto err;
  2978. } else
  2979. #ifndef OPENSSL_NO_RSA
  2980. if (pkey->type == EVP_PKEY_RSA) {
  2981. s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
  2982. if (RSA_sign(NID_md5_sha1, data,
  2983. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
  2984. &(p[2]), &u, pkey->pkey.rsa) <= 0) {
  2985. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
  2986. goto err;
  2987. }
  2988. s2n(u, p);
  2989. n = u + 2;
  2990. } else
  2991. #endif
  2992. #ifndef OPENSSL_NO_DSA
  2993. if (pkey->type == EVP_PKEY_DSA) {
  2994. if (!DSA_sign(pkey->save_type,
  2995. &(data[MD5_DIGEST_LENGTH]),
  2996. SHA_DIGEST_LENGTH, &(p[2]),
  2997. (unsigned int *)&j, pkey->pkey.dsa)) {
  2998. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
  2999. goto err;
  3000. }
  3001. s2n(j, p);
  3002. n = j + 2;
  3003. } else
  3004. #endif
  3005. #ifndef OPENSSL_NO_ECDSA
  3006. if (pkey->type == EVP_PKEY_EC) {
  3007. if (!ECDSA_sign(pkey->save_type,
  3008. &(data[MD5_DIGEST_LENGTH]),
  3009. SHA_DIGEST_LENGTH, &(p[2]),
  3010. (unsigned int *)&j, pkey->pkey.ec)) {
  3011. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
  3012. goto err;
  3013. }
  3014. s2n(j, p);
  3015. n = j + 2;
  3016. } else
  3017. #endif
  3018. if (pkey->type == NID_id_GostR3410_94
  3019. || pkey->type == NID_id_GostR3410_2001) {
  3020. unsigned char signbuf[64];
  3021. int i;
  3022. size_t sigsize = 64;
  3023. s->method->ssl3_enc->cert_verify_mac(s,
  3024. NID_id_GostR3411_94, data);
  3025. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  3026. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  3027. goto err;
  3028. }
  3029. for (i = 63, j = 0; i >= 0; j++, i--) {
  3030. p[2 + j] = signbuf[i];
  3031. }
  3032. s2n(j, p);
  3033. n = j + 2;
  3034. } else {
  3035. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  3036. goto err;
  3037. }
  3038. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
  3039. s->state = SSL3_ST_CW_CERT_VRFY_B;
  3040. }
  3041. EVP_MD_CTX_cleanup(&mctx);
  3042. EVP_PKEY_CTX_free(pctx);
  3043. return ssl_do_write(s);
  3044. err:
  3045. EVP_MD_CTX_cleanup(&mctx);
  3046. EVP_PKEY_CTX_free(pctx);
  3047. s->state = SSL_ST_ERR;
  3048. return (-1);
  3049. }
  3050. /*
  3051. * Check a certificate can be used for client authentication. Currently check
  3052. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  3053. * certificates can be used and optionally checks suitability for Suite B.
  3054. */
  3055. static int ssl3_check_client_certificate(SSL *s)
  3056. {
  3057. unsigned long alg_k;
  3058. if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
  3059. return 0;
  3060. /* If no suitable signature algorithm can't use certificate */
  3061. if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
  3062. return 0;
  3063. /*
  3064. * If strict mode check suitability of chain before using it. This also
  3065. * adjusts suite B digest if necessary.
  3066. */
  3067. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  3068. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  3069. return 0;
  3070. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3071. /* See if we can use client certificate for fixed DH */
  3072. if (alg_k & (SSL_kDHr | SSL_kDHd)) {
  3073. SESS_CERT *scert = s->session->sess_cert;
  3074. int i = scert->peer_cert_type;
  3075. EVP_PKEY *clkey = NULL, *spkey = NULL;
  3076. clkey = s->cert->key->privatekey;
  3077. /* If client key not DH assume it can be used */
  3078. if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
  3079. return 1;
  3080. if (i >= 0)
  3081. spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
  3082. if (spkey) {
  3083. /* Compare server and client parameters */
  3084. i = EVP_PKEY_cmp_parameters(clkey, spkey);
  3085. EVP_PKEY_free(spkey);
  3086. if (i != 1)
  3087. return 0;
  3088. }
  3089. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  3090. }
  3091. return 1;
  3092. }
  3093. int ssl3_send_client_certificate(SSL *s)
  3094. {
  3095. X509 *x509 = NULL;
  3096. EVP_PKEY *pkey = NULL;
  3097. int i;
  3098. if (s->state == SSL3_ST_CW_CERT_A) {
  3099. /* Let cert callback update client certificates if required */
  3100. if (s->cert->cert_cb) {
  3101. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  3102. if (i < 0) {
  3103. s->rwstate = SSL_X509_LOOKUP;
  3104. return -1;
  3105. }
  3106. if (i == 0) {
  3107. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  3108. s->state = SSL_ST_ERR;
  3109. return 0;
  3110. }
  3111. s->rwstate = SSL_NOTHING;
  3112. }
  3113. if (ssl3_check_client_certificate(s))
  3114. s->state = SSL3_ST_CW_CERT_C;
  3115. else
  3116. s->state = SSL3_ST_CW_CERT_B;
  3117. }
  3118. /* We need to get a client cert */
  3119. if (s->state == SSL3_ST_CW_CERT_B) {
  3120. /*
  3121. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  3122. * return(-1); We then get retied later
  3123. */
  3124. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  3125. if (i < 0) {
  3126. s->rwstate = SSL_X509_LOOKUP;
  3127. return (-1);
  3128. }
  3129. s->rwstate = SSL_NOTHING;
  3130. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  3131. s->state = SSL3_ST_CW_CERT_B;
  3132. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  3133. i = 0;
  3134. } else if (i == 1) {
  3135. i = 0;
  3136. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
  3137. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  3138. }
  3139. if (x509 != NULL)
  3140. X509_free(x509);
  3141. if (pkey != NULL)
  3142. EVP_PKEY_free(pkey);
  3143. if (i && !ssl3_check_client_certificate(s))
  3144. i = 0;
  3145. if (i == 0) {
  3146. if (s->version == SSL3_VERSION) {
  3147. s->s3->tmp.cert_req = 0;
  3148. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  3149. return (1);
  3150. } else {
  3151. s->s3->tmp.cert_req = 2;
  3152. }
  3153. }
  3154. /* Ok, we have a cert */
  3155. s->state = SSL3_ST_CW_CERT_C;
  3156. }
  3157. if (s->state == SSL3_ST_CW_CERT_C) {
  3158. s->state = SSL3_ST_CW_CERT_D;
  3159. if (!ssl3_output_cert_chain(s,
  3160. (s->s3->tmp.cert_req ==
  3161. 2) ? NULL : s->cert->key)) {
  3162. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3163. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  3164. s->state = SSL_ST_ERR;
  3165. return 0;
  3166. }
  3167. }
  3168. /* SSL3_ST_CW_CERT_D */
  3169. return ssl_do_write(s);
  3170. }
  3171. #define has_bits(i,m) (((i)&(m)) == (m))
  3172. int ssl3_check_cert_and_algorithm(SSL *s)
  3173. {
  3174. int i, idx;
  3175. long alg_k, alg_a;
  3176. EVP_PKEY *pkey = NULL;
  3177. int pkey_bits;
  3178. SESS_CERT *sc;
  3179. #ifndef OPENSSL_NO_RSA
  3180. RSA *rsa;
  3181. #endif
  3182. #ifndef OPENSSL_NO_DH
  3183. DH *dh;
  3184. #endif
  3185. int al = SSL_AD_HANDSHAKE_FAILURE;
  3186. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3187. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  3188. /* we don't have a certificate */
  3189. if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
  3190. return (1);
  3191. sc = s->session->sess_cert;
  3192. if (sc == NULL) {
  3193. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3194. goto err;
  3195. }
  3196. #ifndef OPENSSL_NO_RSA
  3197. rsa = s->session->sess_cert->peer_rsa_tmp;
  3198. #endif
  3199. #ifndef OPENSSL_NO_DH
  3200. dh = s->session->sess_cert->peer_dh_tmp;
  3201. #endif
  3202. /* This is the passed certificate */
  3203. idx = sc->peer_cert_type;
  3204. #ifndef OPENSSL_NO_ECDH
  3205. if (idx == SSL_PKEY_ECC) {
  3206. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
  3207. /* check failed */
  3208. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  3209. goto f_err;
  3210. } else {
  3211. return 1;
  3212. }
  3213. } else if (alg_a & SSL_aECDSA) {
  3214. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3215. SSL_R_MISSING_ECDSA_SIGNING_CERT);
  3216. goto f_err;
  3217. } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
  3218. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
  3219. goto f_err;
  3220. }
  3221. #endif
  3222. pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
  3223. pkey_bits = EVP_PKEY_bits(pkey);
  3224. i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
  3225. EVP_PKEY_free(pkey);
  3226. /* Check that we have a certificate if we require one */
  3227. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  3228. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3229. SSL_R_MISSING_RSA_SIGNING_CERT);
  3230. goto f_err;
  3231. }
  3232. #ifndef OPENSSL_NO_DSA
  3233. else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
  3234. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3235. SSL_R_MISSING_DSA_SIGNING_CERT);
  3236. goto f_err;
  3237. }
  3238. #endif
  3239. #ifndef OPENSSL_NO_RSA
  3240. if (alg_k & SSL_kRSA) {
  3241. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3242. !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3243. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3244. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3245. goto f_err;
  3246. } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  3247. if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3248. if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3249. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3250. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3251. goto f_err;
  3252. }
  3253. if (rsa != NULL) {
  3254. /* server key exchange is not allowed. */
  3255. al = SSL_AD_INTERNAL_ERROR;
  3256. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3257. goto f_err;
  3258. }
  3259. }
  3260. }
  3261. }
  3262. #endif
  3263. #ifndef OPENSSL_NO_DH
  3264. if ((alg_k & SSL_kEDH) && dh == NULL) {
  3265. al = SSL_AD_INTERNAL_ERROR;
  3266. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3267. goto f_err;
  3268. }
  3269. if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
  3270. !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
  3271. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3272. SSL_R_MISSING_DH_RSA_CERT);
  3273. goto f_err;
  3274. }
  3275. # ifndef OPENSSL_NO_DSA
  3276. if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
  3277. !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
  3278. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3279. SSL_R_MISSING_DH_DSA_CERT);
  3280. goto f_err;
  3281. }
  3282. # endif
  3283. if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
  3284. int dh_size;
  3285. if (alg_k & SSL_kDHE) {
  3286. dh_size = BN_num_bits(dh->p);
  3287. } else {
  3288. DH *dh_srvr = get_server_static_dh_key(sc);
  3289. if (dh_srvr == NULL)
  3290. goto f_err;
  3291. dh_size = BN_num_bits(dh_srvr->p);
  3292. DH_free(dh_srvr);
  3293. }
  3294. if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
  3295. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
  3296. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
  3297. goto f_err;
  3298. }
  3299. }
  3300. #endif /* !OPENSSL_NO_DH */
  3301. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3302. pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3303. #ifndef OPENSSL_NO_RSA
  3304. if (alg_k & SSL_kRSA) {
  3305. if (rsa == NULL) {
  3306. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3307. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3308. goto f_err;
  3309. } else if (BN_num_bits(rsa->n) >
  3310. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3311. /* We have a temporary RSA key but it's too large. */
  3312. al = SSL_AD_EXPORT_RESTRICTION;
  3313. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3314. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3315. goto f_err;
  3316. }
  3317. } else
  3318. #endif
  3319. #ifndef OPENSSL_NO_DH
  3320. if (alg_k & SSL_kDHE) {
  3321. if (BN_num_bits(dh->p) >
  3322. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3323. /* We have a temporary DH key but it's too large. */
  3324. al = SSL_AD_EXPORT_RESTRICTION;
  3325. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3326. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3327. goto f_err;
  3328. }
  3329. } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
  3330. /* The cert should have had an export DH key. */
  3331. al = SSL_AD_EXPORT_RESTRICTION;
  3332. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3333. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3334. goto f_err;
  3335. } else
  3336. #endif
  3337. {
  3338. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3339. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  3340. goto f_err;
  3341. }
  3342. }
  3343. return (1);
  3344. f_err:
  3345. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3346. err:
  3347. return (0);
  3348. }
  3349. #ifndef OPENSSL_NO_TLSEXT
  3350. /*
  3351. * Normally, we can tell if the server is resuming the session from
  3352. * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
  3353. * message after the ServerHello to determine if the server is resuming.
  3354. * Therefore, we allow EAP-FAST to peek ahead.
  3355. * ssl3_check_finished returns 1 if we are resuming from an external
  3356. * pre-shared secret, we have a "ticket" and the next server handshake message
  3357. * is Finished; and 0 otherwise. It returns -1 upon an error.
  3358. */
  3359. static int ssl3_check_finished(SSL *s)
  3360. {
  3361. int ok = 0;
  3362. if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
  3363. !s->session->tlsext_tick)
  3364. return 0;
  3365. /* Need to permit this temporarily, in case the next message is Finished. */
  3366. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  3367. /*
  3368. * This function is called when we might get a Certificate message instead,
  3369. * so permit appropriate message length.
  3370. * We ignore the return value as we're only interested in the message type
  3371. * and not its length.
  3372. */
  3373. s->method->ssl_get_message(s,
  3374. SSL3_ST_CR_CERT_A,
  3375. SSL3_ST_CR_CERT_B,
  3376. -1, s->max_cert_list, &ok);
  3377. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  3378. if (!ok)
  3379. return -1;
  3380. s->s3->tmp.reuse_message = 1;
  3381. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  3382. return 1;
  3383. /* If we're not done, then the CCS arrived early and we should bail. */
  3384. if (s->s3->change_cipher_spec) {
  3385. SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
  3386. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  3387. return -1;
  3388. }
  3389. return 0;
  3390. }
  3391. # ifndef OPENSSL_NO_NEXTPROTONEG
  3392. int ssl3_send_next_proto(SSL *s)
  3393. {
  3394. unsigned int len, padding_len;
  3395. unsigned char *d;
  3396. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  3397. len = s->next_proto_negotiated_len;
  3398. padding_len = 32 - ((len + 2) % 32);
  3399. d = (unsigned char *)s->init_buf->data;
  3400. d[4] = len;
  3401. memcpy(d + 5, s->next_proto_negotiated, len);
  3402. d[5 + len] = padding_len;
  3403. memset(d + 6 + len, 0, padding_len);
  3404. *(d++) = SSL3_MT_NEXT_PROTO;
  3405. l2n3(2 + len + padding_len, d);
  3406. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  3407. s->init_num = 4 + 2 + len + padding_len;
  3408. s->init_off = 0;
  3409. }
  3410. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3411. }
  3412. #endif /* !OPENSSL_NO_NEXTPROTONEG */
  3413. #endif /* !OPENSSL_NO_TLSEXT */
  3414. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3415. {
  3416. int i = 0;
  3417. #ifndef OPENSSL_NO_ENGINE
  3418. if (s->ctx->client_cert_engine) {
  3419. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3420. SSL_get_client_CA_list(s),
  3421. px509, ppkey, NULL, NULL, NULL);
  3422. if (i != 0)
  3423. return i;
  3424. }
  3425. #endif
  3426. if (s->ctx->client_cert_cb)
  3427. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3428. return i;
  3429. }