s3_clnt.c 116 KB

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