s3_clnt.c 76 KB

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