s3_clnt.c 126 KB

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