s3_clnt.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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-2006 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. static const SSL_METHOD *ssl3_get_client_method(int ver);
  163. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  164. #ifndef OPENSSL_NO_ECDH
  165. int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs);
  166. #endif
  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),l;
  182. long num1;
  183. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  184. int ret= -1;
  185. int new_state,state,skip=0;;
  186. RAND_add(&Time,sizeof(Time),0);
  187. ERR_clear_error();
  188. clear_sys_error();
  189. if (s->info_callback != NULL)
  190. cb=s->info_callback;
  191. else if (s->ctx->info_callback != NULL)
  192. cb=s->ctx->info_callback;
  193. s->in_handshake++;
  194. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  195. for (;;)
  196. {
  197. state=s->state;
  198. switch(s->state)
  199. {
  200. case SSL_ST_RENEGOTIATE:
  201. s->new_session=1;
  202. s->state=SSL_ST_CONNECT;
  203. s->ctx->stats.sess_connect_renegotiate++;
  204. /* break */
  205. case SSL_ST_BEFORE:
  206. case SSL_ST_CONNECT:
  207. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  208. case SSL_ST_OK|SSL_ST_CONNECT:
  209. s->server=0;
  210. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  211. if ((s->version & 0xff00 ) != 0x0300)
  212. {
  213. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  214. ret = -1;
  215. goto end;
  216. }
  217. /* s->version=SSL3_VERSION; */
  218. s->type=SSL_ST_CONNECT;
  219. if (s->init_buf == NULL)
  220. {
  221. if ((buf=BUF_MEM_new()) == NULL)
  222. {
  223. ret= -1;
  224. goto end;
  225. }
  226. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  227. {
  228. ret= -1;
  229. goto end;
  230. }
  231. s->init_buf=buf;
  232. buf=NULL;
  233. }
  234. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  235. /* setup buffing BIO */
  236. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  237. /* don't push the buffering BIO quite yet */
  238. ssl3_init_finished_mac(s);
  239. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  240. s->ctx->stats.sess_connect++;
  241. s->init_num=0;
  242. break;
  243. case SSL3_ST_CW_CLNT_HELLO_A:
  244. case SSL3_ST_CW_CLNT_HELLO_B:
  245. s->shutdown=0;
  246. ret=ssl3_client_hello(s);
  247. if (ret <= 0) goto end;
  248. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  249. s->init_num=0;
  250. /* turn on buffering for the next lot of output */
  251. if (s->bbio != s->wbio)
  252. s->wbio=BIO_push(s->bbio,s->wbio);
  253. break;
  254. case SSL3_ST_CR_SRVR_HELLO_A:
  255. case SSL3_ST_CR_SRVR_HELLO_B:
  256. ret=ssl3_get_server_hello(s);
  257. if (ret <= 0) goto end;
  258. if (s->hit)
  259. s->state=SSL3_ST_CR_FINISHED_A;
  260. else
  261. s->state=SSL3_ST_CR_CERT_A;
  262. s->init_num=0;
  263. break;
  264. case SSL3_ST_CR_CERT_A:
  265. case SSL3_ST_CR_CERT_B:
  266. /* Check if it is anon DH/ECDH */
  267. /* or PSK */
  268. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
  269. && !(s->s3->tmp.new_cipher->algorithms & SSL_kPSK))
  270. {
  271. ret=ssl3_get_server_certificate(s);
  272. if (ret <= 0) goto end;
  273. }
  274. else
  275. skip=1;
  276. s->state=SSL3_ST_CR_KEY_EXCH_A;
  277. s->init_num=0;
  278. break;
  279. case SSL3_ST_CR_KEY_EXCH_A:
  280. case SSL3_ST_CR_KEY_EXCH_B:
  281. ret=ssl3_get_key_exchange(s);
  282. if (ret <= 0) goto end;
  283. s->state=SSL3_ST_CR_CERT_REQ_A;
  284. s->init_num=0;
  285. /* at this point we check that we have the
  286. * required stuff from the server */
  287. if (!ssl3_check_cert_and_algorithm(s))
  288. {
  289. ret= -1;
  290. goto end;
  291. }
  292. break;
  293. case SSL3_ST_CR_CERT_REQ_A:
  294. case SSL3_ST_CR_CERT_REQ_B:
  295. ret=ssl3_get_certificate_request(s);
  296. if (ret <= 0) goto end;
  297. s->state=SSL3_ST_CR_SRVR_DONE_A;
  298. s->init_num=0;
  299. break;
  300. case SSL3_ST_CR_SRVR_DONE_A:
  301. case SSL3_ST_CR_SRVR_DONE_B:
  302. ret=ssl3_get_server_done(s);
  303. if (ret <= 0) goto end;
  304. if (s->s3->tmp.cert_req)
  305. s->state=SSL3_ST_CW_CERT_A;
  306. else
  307. s->state=SSL3_ST_CW_KEY_EXCH_A;
  308. s->init_num=0;
  309. break;
  310. case SSL3_ST_CW_CERT_A:
  311. case SSL3_ST_CW_CERT_B:
  312. case SSL3_ST_CW_CERT_C:
  313. case SSL3_ST_CW_CERT_D:
  314. ret=ssl3_send_client_certificate(s);
  315. if (ret <= 0) goto end;
  316. s->state=SSL3_ST_CW_KEY_EXCH_A;
  317. s->init_num=0;
  318. break;
  319. case SSL3_ST_CW_KEY_EXCH_A:
  320. case SSL3_ST_CW_KEY_EXCH_B:
  321. ret=ssl3_send_client_key_exchange(s);
  322. if (ret <= 0) goto end;
  323. l=s->s3->tmp.new_cipher->algorithms;
  324. /* EAY EAY EAY need to check for DH fix cert
  325. * sent back */
  326. /* For TLS, cert_req is set to 2, so a cert chain
  327. * of nothing is sent, but no verify packet is sent */
  328. /* XXX: For now, we do not support client
  329. * authentication in ECDH cipher suites with
  330. * ECDH (rather than ECDSA) certificates.
  331. * We need to skip the certificate verify
  332. * message when client's ECDH public key is sent
  333. * inside the client certificate.
  334. */
  335. if (s->s3->tmp.cert_req == 1)
  336. {
  337. s->state=SSL3_ST_CW_CERT_VRFY_A;
  338. }
  339. else
  340. {
  341. s->state=SSL3_ST_CW_CHANGE_A;
  342. s->s3->change_cipher_spec=0;
  343. }
  344. s->init_num=0;
  345. break;
  346. case SSL3_ST_CW_CERT_VRFY_A:
  347. case SSL3_ST_CW_CERT_VRFY_B:
  348. ret=ssl3_send_client_verify(s);
  349. if (ret <= 0) goto end;
  350. s->state=SSL3_ST_CW_CHANGE_A;
  351. s->init_num=0;
  352. s->s3->change_cipher_spec=0;
  353. break;
  354. case SSL3_ST_CW_CHANGE_A:
  355. case SSL3_ST_CW_CHANGE_B:
  356. ret=ssl3_send_change_cipher_spec(s,
  357. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  358. if (ret <= 0) goto end;
  359. s->state=SSL3_ST_CW_FINISHED_A;
  360. s->init_num=0;
  361. s->session->cipher=s->s3->tmp.new_cipher;
  362. #ifdef OPENSSL_NO_COMP
  363. s->session->compress_meth=0;
  364. #else
  365. if (s->s3->tmp.new_compression == NULL)
  366. s->session->compress_meth=0;
  367. else
  368. s->session->compress_meth=
  369. s->s3->tmp.new_compression->id;
  370. #endif
  371. if (!s->method->ssl3_enc->setup_key_block(s))
  372. {
  373. ret= -1;
  374. goto end;
  375. }
  376. if (!s->method->ssl3_enc->change_cipher_state(s,
  377. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  378. {
  379. ret= -1;
  380. goto end;
  381. }
  382. break;
  383. case SSL3_ST_CW_FINISHED_A:
  384. case SSL3_ST_CW_FINISHED_B:
  385. ret=ssl3_send_finished(s,
  386. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  387. s->method->ssl3_enc->client_finished_label,
  388. s->method->ssl3_enc->client_finished_label_len);
  389. if (ret <= 0) goto end;
  390. s->state=SSL3_ST_CW_FLUSH;
  391. /* clear flags */
  392. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  393. if (s->hit)
  394. {
  395. s->s3->tmp.next_state=SSL_ST_OK;
  396. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  397. {
  398. s->state=SSL_ST_OK;
  399. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  400. s->s3->delay_buf_pop_ret=0;
  401. }
  402. }
  403. else
  404. {
  405. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  406. }
  407. s->init_num=0;
  408. break;
  409. case SSL3_ST_CR_FINISHED_A:
  410. case SSL3_ST_CR_FINISHED_B:
  411. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  412. SSL3_ST_CR_FINISHED_B);
  413. if (ret <= 0) goto end;
  414. if (s->hit)
  415. s->state=SSL3_ST_CW_CHANGE_A;
  416. else
  417. s->state=SSL_ST_OK;
  418. s->init_num=0;
  419. break;
  420. case SSL3_ST_CW_FLUSH:
  421. /* number of bytes to be flushed */
  422. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  423. if (num1 > 0)
  424. {
  425. s->rwstate=SSL_WRITING;
  426. num1=BIO_flush(s->wbio);
  427. if (num1 <= 0) { ret= -1; goto end; }
  428. s->rwstate=SSL_NOTHING;
  429. }
  430. s->state=s->s3->tmp.next_state;
  431. break;
  432. case SSL_ST_OK:
  433. /* clean a few things up */
  434. ssl3_cleanup_key_block(s);
  435. if (s->init_buf != NULL)
  436. {
  437. BUF_MEM_free(s->init_buf);
  438. s->init_buf=NULL;
  439. }
  440. /* If we are not 'joining' the last two packets,
  441. * remove the buffering now */
  442. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  443. ssl_free_wbio_buffer(s);
  444. /* else do it later in ssl3_write */
  445. s->init_num=0;
  446. s->new_session=0;
  447. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  448. if (s->hit) s->ctx->stats.sess_hit++;
  449. ret=1;
  450. /* s->server=0; */
  451. s->handshake_func=ssl3_connect;
  452. s->ctx->stats.sess_connect_good++;
  453. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  454. goto end;
  455. /* break; */
  456. default:
  457. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  458. ret= -1;
  459. goto end;
  460. /* break; */
  461. }
  462. /* did we do anything */
  463. if (!s->s3->tmp.reuse_message && !skip)
  464. {
  465. if (s->debug)
  466. {
  467. if ((ret=BIO_flush(s->wbio)) <= 0)
  468. goto end;
  469. }
  470. if ((cb != NULL) && (s->state != state))
  471. {
  472. new_state=s->state;
  473. s->state=state;
  474. cb(s,SSL_CB_CONNECT_LOOP,1);
  475. s->state=new_state;
  476. }
  477. }
  478. skip=0;
  479. }
  480. end:
  481. s->in_handshake--;
  482. if (buf != NULL)
  483. BUF_MEM_free(buf);
  484. if (cb != NULL)
  485. cb(s,SSL_CB_CONNECT_EXIT,ret);
  486. return(ret);
  487. }
  488. int ssl3_client_hello(SSL *s)
  489. {
  490. unsigned char *buf;
  491. unsigned char *p,*d;
  492. int i;
  493. unsigned long Time,l;
  494. #ifndef OPENSSL_NO_COMP
  495. int j;
  496. SSL_COMP *comp;
  497. #endif
  498. buf=(unsigned char *)s->init_buf->data;
  499. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  500. {
  501. if ((s->session == NULL) ||
  502. (s->session->ssl_version != s->version) ||
  503. (s->session->not_resumable))
  504. {
  505. if (!ssl_get_new_session(s,0))
  506. goto err;
  507. }
  508. /* else use the pre-loaded session */
  509. p=s->s3->client_random;
  510. Time=(unsigned long)time(NULL); /* Time */
  511. l2n(Time,p);
  512. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  513. goto err;
  514. /* Do the message type and length last */
  515. d=p= &(buf[4]);
  516. *(p++)=s->version>>8;
  517. *(p++)=s->version&0xff;
  518. s->client_version=s->version;
  519. /* Random stuff */
  520. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  521. p+=SSL3_RANDOM_SIZE;
  522. /* Session ID */
  523. if (s->new_session)
  524. i=0;
  525. else
  526. i=s->session->session_id_length;
  527. *(p++)=i;
  528. if (i != 0)
  529. {
  530. if (i > (int)sizeof(s->session->session_id))
  531. {
  532. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  533. goto err;
  534. }
  535. memcpy(p,s->session->session_id,i);
  536. p+=i;
  537. }
  538. /* Ciphers supported */
  539. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  540. if (i == 0)
  541. {
  542. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  543. goto err;
  544. }
  545. s2n(i,p);
  546. p+=i;
  547. /* COMPRESSION */
  548. #ifdef OPENSSL_NO_COMP
  549. *(p++)=1;
  550. #else
  551. if ((s->options & SSL_OP_NO_COMPRESSION)
  552. || !s->ctx->comp_methods)
  553. j=0;
  554. else
  555. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  556. *(p++)=1+j;
  557. for (i=0; i<j; i++)
  558. {
  559. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  560. *(p++)=comp->id;
  561. }
  562. #endif
  563. *(p++)=0; /* Add the NULL method */
  564. #ifndef OPENSSL_NO_TLSEXT
  565. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  566. {
  567. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  568. goto err;
  569. }
  570. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  571. {
  572. SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  573. goto err;
  574. }
  575. #endif
  576. l=(p-d);
  577. d=buf;
  578. *(d++)=SSL3_MT_CLIENT_HELLO;
  579. l2n3(l,d);
  580. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  581. /* number of bytes to write */
  582. s->init_num=p-buf;
  583. s->init_off=0;
  584. }
  585. /* SSL3_ST_CW_CLNT_HELLO_B */
  586. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  587. err:
  588. return(-1);
  589. }
  590. int ssl3_get_server_hello(SSL *s)
  591. {
  592. STACK_OF(SSL_CIPHER) *sk;
  593. SSL_CIPHER *c;
  594. unsigned char *p,*d;
  595. int i,al,ok;
  596. unsigned int j;
  597. long n;
  598. #ifndef OPENSSL_NO_COMP
  599. SSL_COMP *comp;
  600. #endif
  601. n=s->method->ssl_get_message(s,
  602. SSL3_ST_CR_SRVR_HELLO_A,
  603. SSL3_ST_CR_SRVR_HELLO_B,
  604. -1,
  605. 300, /* ?? */
  606. &ok);
  607. if (!ok) return((int)n);
  608. if ( SSL_version(s) == DTLS1_VERSION)
  609. {
  610. if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
  611. {
  612. if ( s->d1->send_cookie == 0)
  613. {
  614. s->s3->tmp.reuse_message = 1;
  615. return 1;
  616. }
  617. else /* already sent a cookie */
  618. {
  619. al=SSL_AD_UNEXPECTED_MESSAGE;
  620. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  621. goto f_err;
  622. }
  623. }
  624. }
  625. if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
  626. {
  627. al=SSL_AD_UNEXPECTED_MESSAGE;
  628. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  629. goto f_err;
  630. }
  631. d=p=(unsigned char *)s->init_msg;
  632. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  633. {
  634. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  635. s->version=(s->version&0xff00)|p[1];
  636. al=SSL_AD_PROTOCOL_VERSION;
  637. goto f_err;
  638. }
  639. p+=2;
  640. /* load the server hello data */
  641. /* load the server random */
  642. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  643. p+=SSL3_RANDOM_SIZE;
  644. /* get the session-id */
  645. j= *(p++);
  646. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  647. {
  648. al=SSL_AD_ILLEGAL_PARAMETER;
  649. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  650. goto f_err;
  651. }
  652. if (j != 0 && j == s->session->session_id_length
  653. && memcmp(p,s->session->session_id,j) == 0)
  654. {
  655. if(s->sid_ctx_length != s->session->sid_ctx_length
  656. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  657. {
  658. /* actually a client application bug */
  659. al=SSL_AD_ILLEGAL_PARAMETER;
  660. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  661. goto f_err;
  662. }
  663. s->hit=1;
  664. }
  665. else /* a miss or crap from the other end */
  666. {
  667. /* If we were trying for session-id reuse, make a new
  668. * SSL_SESSION so we don't stuff up other people */
  669. s->hit=0;
  670. if (s->session->session_id_length > 0)
  671. {
  672. if (!ssl_get_new_session(s,0))
  673. {
  674. al=SSL_AD_INTERNAL_ERROR;
  675. goto f_err;
  676. }
  677. }
  678. s->session->session_id_length=j;
  679. memcpy(s->session->session_id,p,j); /* j could be 0 */
  680. }
  681. p+=j;
  682. c=ssl_get_cipher_by_char(s,p);
  683. if (c == NULL)
  684. {
  685. /* unknown cipher */
  686. al=SSL_AD_ILLEGAL_PARAMETER;
  687. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  688. goto f_err;
  689. }
  690. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  691. sk=ssl_get_ciphers_by_id(s);
  692. i=sk_SSL_CIPHER_find(sk,c);
  693. if (i < 0)
  694. {
  695. /* we did not say we would use this cipher */
  696. al=SSL_AD_ILLEGAL_PARAMETER;
  697. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  698. goto f_err;
  699. }
  700. /* Depending on the session caching (internal/external), the cipher
  701. and/or cipher_id values may not be set. Make sure that
  702. cipher_id is set and use it for comparison. */
  703. if (s->session->cipher)
  704. s->session->cipher_id = s->session->cipher->id;
  705. if (s->hit && (s->session->cipher_id != c->id))
  706. {
  707. if (!(s->options &
  708. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  709. {
  710. al=SSL_AD_ILLEGAL_PARAMETER;
  711. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  712. goto f_err;
  713. }
  714. }
  715. s->s3->tmp.new_cipher=c;
  716. /* lets get the compression algorithm */
  717. /* COMPRESSION */
  718. #ifdef OPENSSL_NO_COMP
  719. if (*(p++) != 0)
  720. {
  721. al=SSL_AD_ILLEGAL_PARAMETER;
  722. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  723. goto f_err;
  724. }
  725. #else
  726. j= *(p++);
  727. if ((j == 0) || (s->options & SSL_OP_NO_COMPRESSION))
  728. comp=NULL;
  729. else
  730. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  731. if ((j != 0) && (comp == NULL))
  732. {
  733. al=SSL_AD_ILLEGAL_PARAMETER;
  734. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  735. goto f_err;
  736. }
  737. else
  738. {
  739. s->s3->tmp.new_compression=comp;
  740. }
  741. #endif
  742. #ifndef OPENSSL_NO_TLSEXT
  743. /* TLS extensions*/
  744. if (s->version > SSL3_VERSION)
  745. {
  746. if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
  747. {
  748. /* 'al' set by ssl_parse_serverhello_tlsext */
  749. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
  750. goto f_err;
  751. }
  752. if (ssl_check_serverhello_tlsext(s) <= 0)
  753. {
  754. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_SERVERHELLO_TLSEXT);
  755. goto err;
  756. }
  757. }
  758. #endif
  759. if (p != (d+n))
  760. {
  761. /* wrong packet length */
  762. al=SSL_AD_DECODE_ERROR;
  763. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  764. goto err;
  765. }
  766. return(1);
  767. f_err:
  768. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  769. err:
  770. return(-1);
  771. }
  772. int ssl3_get_server_certificate(SSL *s)
  773. {
  774. int al,i,ok,ret= -1;
  775. unsigned long n,nc,llen,l;
  776. X509 *x=NULL;
  777. const unsigned char *q,*p;
  778. unsigned char *d;
  779. STACK_OF(X509) *sk=NULL;
  780. SESS_CERT *sc;
  781. EVP_PKEY *pkey=NULL;
  782. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  783. n=s->method->ssl_get_message(s,
  784. SSL3_ST_CR_CERT_A,
  785. SSL3_ST_CR_CERT_B,
  786. -1,
  787. s->max_cert_list,
  788. &ok);
  789. if (!ok) return((int)n);
  790. if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
  791. {
  792. s->s3->tmp.reuse_message=1;
  793. return(1);
  794. }
  795. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  796. {
  797. al=SSL_AD_UNEXPECTED_MESSAGE;
  798. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  799. goto f_err;
  800. }
  801. p=d=(unsigned char *)s->init_msg;
  802. if ((sk=sk_X509_new_null()) == NULL)
  803. {
  804. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  805. goto err;
  806. }
  807. n2l3(p,llen);
  808. if (llen+3 != n)
  809. {
  810. al=SSL_AD_DECODE_ERROR;
  811. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  812. goto f_err;
  813. }
  814. for (nc=0; nc<llen; )
  815. {
  816. n2l3(p,l);
  817. if ((l+nc+3) > llen)
  818. {
  819. al=SSL_AD_DECODE_ERROR;
  820. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  821. goto f_err;
  822. }
  823. q=p;
  824. x=d2i_X509(NULL,&q,l);
  825. if (x == NULL)
  826. {
  827. al=SSL_AD_BAD_CERTIFICATE;
  828. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  829. goto f_err;
  830. }
  831. if (q != (p+l))
  832. {
  833. al=SSL_AD_DECODE_ERROR;
  834. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  835. goto f_err;
  836. }
  837. if (!sk_X509_push(sk,x))
  838. {
  839. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  840. goto err;
  841. }
  842. x=NULL;
  843. nc+=l+3;
  844. p=q;
  845. }
  846. i=ssl_verify_cert_chain(s,sk);
  847. if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
  848. #ifndef OPENSSL_NO_KRB5
  849. && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  850. != (SSL_aKRB5|SSL_kKRB5)
  851. #endif /* OPENSSL_NO_KRB5 */
  852. )
  853. {
  854. al=ssl_verify_alarm_type(s->verify_result);
  855. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  856. goto f_err;
  857. }
  858. ERR_clear_error(); /* but we keep s->verify_result */
  859. sc=ssl_sess_cert_new();
  860. if (sc == NULL) goto err;
  861. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  862. s->session->sess_cert=sc;
  863. sc->cert_chain=sk;
  864. /* Inconsistency alert: cert_chain does include the peer's
  865. * certificate, which we don't include in s3_srvr.c */
  866. x=sk_X509_value(sk,0);
  867. sk=NULL;
  868. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  869. pkey=X509_get_pubkey(x);
  870. /* VRS: allow null cert if auth == KRB5 */
  871. need_cert = ((s->s3->tmp.new_cipher->algorithms
  872. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  873. == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
  874. #ifdef KSSL_DEBUG
  875. printf("pkey,x = %p, %p\n", pkey,x);
  876. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  877. printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
  878. s->s3->tmp.new_cipher->algorithms, need_cert);
  879. #endif /* KSSL_DEBUG */
  880. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  881. {
  882. x=NULL;
  883. al=SSL3_AL_FATAL;
  884. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  885. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  886. goto f_err;
  887. }
  888. i=ssl_cert_type(x,pkey);
  889. if (need_cert && i < 0)
  890. {
  891. x=NULL;
  892. al=SSL3_AL_FATAL;
  893. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  894. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  895. goto f_err;
  896. }
  897. if (need_cert)
  898. {
  899. sc->peer_cert_type=i;
  900. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  901. /* Why would the following ever happen?
  902. * We just created sc a couple of lines ago. */
  903. if (sc->peer_pkeys[i].x509 != NULL)
  904. X509_free(sc->peer_pkeys[i].x509);
  905. sc->peer_pkeys[i].x509=x;
  906. sc->peer_key= &(sc->peer_pkeys[i]);
  907. if (s->session->peer != NULL)
  908. X509_free(s->session->peer);
  909. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  910. s->session->peer=x;
  911. }
  912. else
  913. {
  914. sc->peer_cert_type=i;
  915. sc->peer_key= NULL;
  916. if (s->session->peer != NULL)
  917. X509_free(s->session->peer);
  918. s->session->peer=NULL;
  919. }
  920. s->session->verify_result = s->verify_result;
  921. x=NULL;
  922. ret=1;
  923. if (0)
  924. {
  925. f_err:
  926. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  927. }
  928. err:
  929. EVP_PKEY_free(pkey);
  930. X509_free(x);
  931. sk_X509_pop_free(sk,X509_free);
  932. return(ret);
  933. }
  934. int ssl3_get_key_exchange(SSL *s)
  935. {
  936. #ifndef OPENSSL_NO_RSA
  937. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  938. #endif
  939. EVP_MD_CTX md_ctx;
  940. unsigned char *param,*p;
  941. int al,i,j,param_len,ok;
  942. long n,alg;
  943. EVP_PKEY *pkey=NULL;
  944. #ifndef OPENSSL_NO_RSA
  945. RSA *rsa=NULL;
  946. #endif
  947. #ifndef OPENSSL_NO_DH
  948. DH *dh=NULL;
  949. #endif
  950. #ifndef OPENSSL_NO_ECDH
  951. EC_KEY *ecdh = NULL;
  952. BN_CTX *bn_ctx = NULL;
  953. EC_POINT *srvr_ecpoint = NULL;
  954. int curve_nid = 0;
  955. int encoded_pt_len = 0;
  956. #endif
  957. /* use same message size as in ssl3_get_certificate_request()
  958. * as ServerKeyExchange message may be skipped */
  959. n=s->method->ssl_get_message(s,
  960. SSL3_ST_CR_KEY_EXCH_A,
  961. SSL3_ST_CR_KEY_EXCH_B,
  962. -1,
  963. s->max_cert_list,
  964. &ok);
  965. if (!ok) return((int)n);
  966. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  967. {
  968. #ifndef OPENSSL_NO_PSK
  969. /* In plain PSK ciphersuite, ServerKeyExchange can be
  970. omitted if no identity hint is sent. Set
  971. session->sess_cert anyway to avoid problems
  972. later.*/
  973. if (s->s3->tmp.new_cipher->algorithms & SSL_kPSK)
  974. {
  975. s->session->sess_cert=ssl_sess_cert_new();
  976. if (s->ctx->psk_identity_hint)
  977. OPENSSL_free(s->ctx->psk_identity_hint);
  978. s->ctx->psk_identity_hint = NULL;
  979. }
  980. #endif
  981. s->s3->tmp.reuse_message=1;
  982. return(1);
  983. }
  984. param=p=(unsigned char *)s->init_msg;
  985. if (s->session->sess_cert != NULL)
  986. {
  987. #ifndef OPENSSL_NO_RSA
  988. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  989. {
  990. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  991. s->session->sess_cert->peer_rsa_tmp=NULL;
  992. }
  993. #endif
  994. #ifndef OPENSSL_NO_DH
  995. if (s->session->sess_cert->peer_dh_tmp)
  996. {
  997. DH_free(s->session->sess_cert->peer_dh_tmp);
  998. s->session->sess_cert->peer_dh_tmp=NULL;
  999. }
  1000. #endif
  1001. #ifndef OPENSSL_NO_ECDH
  1002. if (s->session->sess_cert->peer_ecdh_tmp)
  1003. {
  1004. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1005. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1006. }
  1007. #endif
  1008. }
  1009. else
  1010. {
  1011. s->session->sess_cert=ssl_sess_cert_new();
  1012. }
  1013. param_len=0;
  1014. alg=s->s3->tmp.new_cipher->algorithms;
  1015. EVP_MD_CTX_init(&md_ctx);
  1016. #ifndef OPENSSL_NO_PSK
  1017. if (alg & SSL_kPSK)
  1018. {
  1019. char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
  1020. al=SSL_AD_HANDSHAKE_FAILURE;
  1021. n2s(p,i);
  1022. param_len=i+2;
  1023. /* Store PSK identity hint for later use, hint is used
  1024. * in ssl3_send_client_key_exchange. Assume that the
  1025. * maximum length of a PSK identity hint can be as
  1026. * long as the maximum length of a PSK identity. */
  1027. if (i > PSK_MAX_IDENTITY_LEN)
  1028. {
  1029. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1030. SSL_R_DATA_LENGTH_TOO_LONG);
  1031. goto f_err;
  1032. }
  1033. if (param_len > n)
  1034. {
  1035. al=SSL_AD_DECODE_ERROR;
  1036. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1037. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1038. goto f_err;
  1039. }
  1040. /* If received PSK identity hint contains NULL
  1041. * characters, the hint is truncated from the first
  1042. * NULL. p may not be ending with NULL, so create a
  1043. * NULL-terminated string. */
  1044. memcpy(tmp_id_hint, p, i);
  1045. memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1046. if (s->ctx->psk_identity_hint != NULL)
  1047. OPENSSL_free(s->ctx->psk_identity_hint);
  1048. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1049. if (s->ctx->psk_identity_hint == NULL)
  1050. {
  1051. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1052. goto f_err;
  1053. }
  1054. p+=i;
  1055. n-=param_len;
  1056. }
  1057. else
  1058. #endif /* !OPENSSL_NO_PSK */
  1059. #ifndef OPENSSL_NO_RSA
  1060. if (alg & SSL_kRSA)
  1061. {
  1062. if ((rsa=RSA_new()) == NULL)
  1063. {
  1064. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1065. goto err;
  1066. }
  1067. n2s(p,i);
  1068. param_len=i+2;
  1069. if (param_len > n)
  1070. {
  1071. al=SSL_AD_DECODE_ERROR;
  1072. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  1073. goto f_err;
  1074. }
  1075. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  1076. {
  1077. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1078. goto err;
  1079. }
  1080. p+=i;
  1081. n2s(p,i);
  1082. param_len+=i+2;
  1083. if (param_len > n)
  1084. {
  1085. al=SSL_AD_DECODE_ERROR;
  1086. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  1087. goto f_err;
  1088. }
  1089. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  1090. {
  1091. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1092. goto err;
  1093. }
  1094. p+=i;
  1095. n-=param_len;
  1096. /* this should be because we are using an export cipher */
  1097. if (alg & SSL_aRSA)
  1098. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1099. else
  1100. {
  1101. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1102. goto err;
  1103. }
  1104. s->session->sess_cert->peer_rsa_tmp=rsa;
  1105. rsa=NULL;
  1106. }
  1107. #else /* OPENSSL_NO_RSA */
  1108. if (0)
  1109. ;
  1110. #endif
  1111. #ifndef OPENSSL_NO_DH
  1112. else if (alg & SSL_kEDH)
  1113. {
  1114. if ((dh=DH_new()) == NULL)
  1115. {
  1116. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  1117. goto err;
  1118. }
  1119. n2s(p,i);
  1120. param_len=i+2;
  1121. if (param_len > n)
  1122. {
  1123. al=SSL_AD_DECODE_ERROR;
  1124. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  1125. goto f_err;
  1126. }
  1127. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  1128. {
  1129. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1130. goto err;
  1131. }
  1132. p+=i;
  1133. n2s(p,i);
  1134. param_len+=i+2;
  1135. if (param_len > n)
  1136. {
  1137. al=SSL_AD_DECODE_ERROR;
  1138. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  1139. goto f_err;
  1140. }
  1141. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1142. {
  1143. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1144. goto err;
  1145. }
  1146. p+=i;
  1147. n2s(p,i);
  1148. param_len+=i+2;
  1149. if (param_len > n)
  1150. {
  1151. al=SSL_AD_DECODE_ERROR;
  1152. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1153. goto f_err;
  1154. }
  1155. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1156. {
  1157. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1158. goto err;
  1159. }
  1160. p+=i;
  1161. n-=param_len;
  1162. #ifndef OPENSSL_NO_RSA
  1163. if (alg & SSL_aRSA)
  1164. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1165. #else
  1166. if (0)
  1167. ;
  1168. #endif
  1169. #ifndef OPENSSL_NO_DSA
  1170. else if (alg & SSL_aDSS)
  1171. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1172. #endif
  1173. /* else anonymous DH, so no certificate or pkey. */
  1174. s->session->sess_cert->peer_dh_tmp=dh;
  1175. dh=NULL;
  1176. }
  1177. else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
  1178. {
  1179. al=SSL_AD_ILLEGAL_PARAMETER;
  1180. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1181. goto f_err;
  1182. }
  1183. #endif /* !OPENSSL_NO_DH */
  1184. #ifndef OPENSSL_NO_ECDH
  1185. else if (alg & SSL_kECDHE)
  1186. {
  1187. EC_GROUP *ngroup;
  1188. const EC_GROUP *group;
  1189. if ((ecdh=EC_KEY_new()) == NULL)
  1190. {
  1191. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1192. goto err;
  1193. }
  1194. /* Extract elliptic curve parameters and the
  1195. * server's ephemeral ECDH public key.
  1196. * Keep accumulating lengths of various components in
  1197. * param_len and make sure it never exceeds n.
  1198. */
  1199. /* XXX: For now we only support named (not generic) curves
  1200. * and the ECParameters in this case is just three bytes.
  1201. */
  1202. param_len=3;
  1203. if ((param_len > n) ||
  1204. (*p != NAMED_CURVE_TYPE) ||
  1205. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
  1206. {
  1207. al=SSL_AD_INTERNAL_ERROR;
  1208. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1209. goto f_err;
  1210. }
  1211. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1212. if (ngroup == NULL)
  1213. {
  1214. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1215. goto err;
  1216. }
  1217. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1218. {
  1219. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1220. goto err;
  1221. }
  1222. EC_GROUP_free(ngroup);
  1223. group = EC_KEY_get0_group(ecdh);
  1224. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1225. (EC_GROUP_get_degree(group) > 163))
  1226. {
  1227. al=SSL_AD_EXPORT_RESTRICTION;
  1228. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1229. goto f_err;
  1230. }
  1231. p+=3;
  1232. /* Next, get the encoded ECPoint */
  1233. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1234. ((bn_ctx = BN_CTX_new()) == NULL))
  1235. {
  1236. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1237. goto err;
  1238. }
  1239. encoded_pt_len = *p; /* length of encoded point */
  1240. p+=1;
  1241. param_len += (1 + encoded_pt_len);
  1242. if ((param_len > n) ||
  1243. (EC_POINT_oct2point(group, srvr_ecpoint,
  1244. p, encoded_pt_len, bn_ctx) == 0))
  1245. {
  1246. al=SSL_AD_DECODE_ERROR;
  1247. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1248. goto f_err;
  1249. }
  1250. n-=param_len;
  1251. p+=encoded_pt_len;
  1252. /* The ECC/TLS specification does not mention
  1253. * the use of DSA to sign ECParameters in the server
  1254. * key exchange message. We do support RSA and ECDSA.
  1255. */
  1256. if (0) ;
  1257. #ifndef OPENSSL_NO_RSA
  1258. else if (alg & SSL_aRSA)
  1259. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1260. #endif
  1261. #ifndef OPENSSL_NO_ECDSA
  1262. else if (alg & SSL_aECDSA)
  1263. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1264. #endif
  1265. /* else anonymous ECDH, so no certificate or pkey. */
  1266. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1267. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1268. ecdh=NULL;
  1269. BN_CTX_free(bn_ctx);
  1270. EC_POINT_free(srvr_ecpoint);
  1271. srvr_ecpoint = NULL;
  1272. }
  1273. else if (alg & SSL_kECDH)
  1274. {
  1275. al=SSL_AD_UNEXPECTED_MESSAGE;
  1276. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1277. goto f_err;
  1278. }
  1279. #endif /* !OPENSSL_NO_ECDH */
  1280. if (alg & SSL_aFZA)
  1281. {
  1282. al=SSL_AD_HANDSHAKE_FAILURE;
  1283. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1284. goto f_err;
  1285. }
  1286. /* p points to the next byte, there are 'n' bytes left */
  1287. /* if it was signed, check the signature */
  1288. if (pkey != NULL)
  1289. {
  1290. n2s(p,i);
  1291. n-=2;
  1292. j=EVP_PKEY_size(pkey);
  1293. if ((i != n) || (n > j) || (n <= 0))
  1294. {
  1295. /* wrong packet length */
  1296. al=SSL_AD_DECODE_ERROR;
  1297. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1298. goto f_err;
  1299. }
  1300. #ifndef OPENSSL_NO_RSA
  1301. if (pkey->type == EVP_PKEY_RSA)
  1302. {
  1303. int num;
  1304. j=0;
  1305. q=md_buf;
  1306. for (num=2; num > 0; num--)
  1307. {
  1308. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1309. ?s->ctx->md5:s->ctx->sha1, NULL);
  1310. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1311. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1312. EVP_DigestUpdate(&md_ctx,param,param_len);
  1313. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1314. q+=i;
  1315. j+=i;
  1316. }
  1317. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1318. pkey->pkey.rsa);
  1319. if (i < 0)
  1320. {
  1321. al=SSL_AD_DECRYPT_ERROR;
  1322. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1323. goto f_err;
  1324. }
  1325. if (i == 0)
  1326. {
  1327. /* bad signature */
  1328. al=SSL_AD_DECRYPT_ERROR;
  1329. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1330. goto f_err;
  1331. }
  1332. }
  1333. else
  1334. #endif
  1335. #ifndef OPENSSL_NO_DSA
  1336. if (pkey->type == EVP_PKEY_DSA)
  1337. {
  1338. /* lets do DSS */
  1339. EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
  1340. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1341. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1342. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1343. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1344. {
  1345. /* bad signature */
  1346. al=SSL_AD_DECRYPT_ERROR;
  1347. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1348. goto f_err;
  1349. }
  1350. }
  1351. else
  1352. #endif
  1353. #ifndef OPENSSL_NO_ECDSA
  1354. if (pkey->type == EVP_PKEY_EC)
  1355. {
  1356. /* let's do ECDSA */
  1357. EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1358. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1359. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1360. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1361. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1362. {
  1363. /* bad signature */
  1364. al=SSL_AD_DECRYPT_ERROR;
  1365. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1366. goto f_err;
  1367. }
  1368. }
  1369. else
  1370. #endif
  1371. {
  1372. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1373. goto err;
  1374. }
  1375. }
  1376. else
  1377. {
  1378. if (!(alg & SSL_aNULL) && !(alg & SSL_kPSK))
  1379. /* aNULL or kPSK do not need public keys */
  1380. {
  1381. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1382. goto err;
  1383. }
  1384. /* still data left over */
  1385. if (n != 0)
  1386. {
  1387. al=SSL_AD_DECODE_ERROR;
  1388. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1389. goto f_err;
  1390. }
  1391. }
  1392. EVP_PKEY_free(pkey);
  1393. EVP_MD_CTX_cleanup(&md_ctx);
  1394. return(1);
  1395. f_err:
  1396. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1397. err:
  1398. EVP_PKEY_free(pkey);
  1399. #ifndef OPENSSL_NO_RSA
  1400. if (rsa != NULL)
  1401. RSA_free(rsa);
  1402. #endif
  1403. #ifndef OPENSSL_NO_DH
  1404. if (dh != NULL)
  1405. DH_free(dh);
  1406. #endif
  1407. #ifndef OPENSSL_NO_ECDH
  1408. BN_CTX_free(bn_ctx);
  1409. EC_POINT_free(srvr_ecpoint);
  1410. if (ecdh != NULL)
  1411. EC_KEY_free(ecdh);
  1412. #endif
  1413. EVP_MD_CTX_cleanup(&md_ctx);
  1414. return(-1);
  1415. }
  1416. int ssl3_get_certificate_request(SSL *s)
  1417. {
  1418. int ok,ret=0;
  1419. unsigned long n,nc,l;
  1420. unsigned int llen,ctype_num,i;
  1421. X509_NAME *xn=NULL;
  1422. const unsigned char *p,*q;
  1423. unsigned char *d;
  1424. STACK_OF(X509_NAME) *ca_sk=NULL;
  1425. n=s->method->ssl_get_message(s,
  1426. SSL3_ST_CR_CERT_REQ_A,
  1427. SSL3_ST_CR_CERT_REQ_B,
  1428. -1,
  1429. s->max_cert_list,
  1430. &ok);
  1431. if (!ok) return((int)n);
  1432. s->s3->tmp.cert_req=0;
  1433. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1434. {
  1435. s->s3->tmp.reuse_message=1;
  1436. return(1);
  1437. }
  1438. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1439. {
  1440. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1441. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1442. goto err;
  1443. }
  1444. /* TLS does not like anon-DH with client cert */
  1445. if (s->version > SSL3_VERSION)
  1446. {
  1447. l=s->s3->tmp.new_cipher->algorithms;
  1448. if (l & SSL_aNULL)
  1449. {
  1450. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1451. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1452. goto err;
  1453. }
  1454. }
  1455. p=d=(unsigned char *)s->init_msg;
  1456. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1457. {
  1458. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1459. goto err;
  1460. }
  1461. /* get the certificate types */
  1462. ctype_num= *(p++);
  1463. if (ctype_num > SSL3_CT_NUMBER)
  1464. ctype_num=SSL3_CT_NUMBER;
  1465. for (i=0; i<ctype_num; i++)
  1466. s->s3->tmp.ctype[i]= p[i];
  1467. p+=ctype_num;
  1468. /* get the CA RDNs */
  1469. n2s(p,llen);
  1470. #if 0
  1471. {
  1472. FILE *out;
  1473. out=fopen("/tmp/vsign.der","w");
  1474. fwrite(p,1,llen,out);
  1475. fclose(out);
  1476. }
  1477. #endif
  1478. if ((llen+ctype_num+2+1) != n)
  1479. {
  1480. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1481. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1482. goto err;
  1483. }
  1484. for (nc=0; nc<llen; )
  1485. {
  1486. n2s(p,l);
  1487. if ((l+nc+2) > llen)
  1488. {
  1489. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1490. goto cont; /* netscape bugs */
  1491. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1492. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1493. goto err;
  1494. }
  1495. q=p;
  1496. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1497. {
  1498. /* If netscape tolerance is on, ignore errors */
  1499. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1500. goto cont;
  1501. else
  1502. {
  1503. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1504. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1505. goto err;
  1506. }
  1507. }
  1508. if (q != (p+l))
  1509. {
  1510. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1511. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1512. goto err;
  1513. }
  1514. if (!sk_X509_NAME_push(ca_sk,xn))
  1515. {
  1516. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1517. goto err;
  1518. }
  1519. p+=l;
  1520. nc+=l+2;
  1521. }
  1522. if (0)
  1523. {
  1524. cont:
  1525. ERR_clear_error();
  1526. }
  1527. /* we should setup a certificate to return.... */
  1528. s->s3->tmp.cert_req=1;
  1529. s->s3->tmp.ctype_num=ctype_num;
  1530. if (s->s3->tmp.ca_names != NULL)
  1531. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1532. s->s3->tmp.ca_names=ca_sk;
  1533. ca_sk=NULL;
  1534. ret=1;
  1535. err:
  1536. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1537. return(ret);
  1538. }
  1539. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1540. {
  1541. return(X509_NAME_cmp(*a,*b));
  1542. }
  1543. int ssl3_get_server_done(SSL *s)
  1544. {
  1545. int ok,ret=0;
  1546. long n;
  1547. n=s->method->ssl_get_message(s,
  1548. SSL3_ST_CR_SRVR_DONE_A,
  1549. SSL3_ST_CR_SRVR_DONE_B,
  1550. SSL3_MT_SERVER_DONE,
  1551. 30, /* should be very small, like 0 :-) */
  1552. &ok);
  1553. if (!ok) return((int)n);
  1554. if (n > 0)
  1555. {
  1556. /* should contain no data */
  1557. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1558. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1559. return -1;
  1560. }
  1561. ret=1;
  1562. return(ret);
  1563. }
  1564. int ssl3_send_client_key_exchange(SSL *s)
  1565. {
  1566. unsigned char *p,*d;
  1567. int n;
  1568. unsigned long l;
  1569. #ifndef OPENSSL_NO_RSA
  1570. unsigned char *q;
  1571. EVP_PKEY *pkey=NULL;
  1572. #endif
  1573. #ifndef OPENSSL_NO_KRB5
  1574. KSSL_ERR kssl_err;
  1575. #endif /* OPENSSL_NO_KRB5 */
  1576. #ifndef OPENSSL_NO_ECDH
  1577. EC_KEY *clnt_ecdh = NULL;
  1578. const EC_POINT *srvr_ecpoint = NULL;
  1579. EVP_PKEY *srvr_pub_pkey = NULL;
  1580. unsigned char *encodedPoint = NULL;
  1581. int encoded_pt_len = 0;
  1582. BN_CTX * bn_ctx = NULL;
  1583. #endif
  1584. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1585. {
  1586. d=(unsigned char *)s->init_buf->data;
  1587. p= &(d[4]);
  1588. l=s->s3->tmp.new_cipher->algorithms;
  1589. /* Fool emacs indentation */
  1590. if (0) {}
  1591. #ifndef OPENSSL_NO_RSA
  1592. else if (l & SSL_kRSA)
  1593. {
  1594. RSA *rsa;
  1595. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1596. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1597. rsa=s->session->sess_cert->peer_rsa_tmp;
  1598. else
  1599. {
  1600. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1601. if ((pkey == NULL) ||
  1602. (pkey->type != EVP_PKEY_RSA) ||
  1603. (pkey->pkey.rsa == NULL))
  1604. {
  1605. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1606. goto err;
  1607. }
  1608. rsa=pkey->pkey.rsa;
  1609. EVP_PKEY_free(pkey);
  1610. }
  1611. tmp_buf[0]=s->client_version>>8;
  1612. tmp_buf[1]=s->client_version&0xff;
  1613. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1614. goto err;
  1615. s->session->master_key_length=sizeof tmp_buf;
  1616. q=p;
  1617. /* Fix buf for TLS and beyond */
  1618. if (s->version > SSL3_VERSION)
  1619. p+=2;
  1620. n=RSA_public_encrypt(sizeof tmp_buf,
  1621. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1622. #ifdef PKCS1_CHECK
  1623. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1624. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1625. #endif
  1626. if (n <= 0)
  1627. {
  1628. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1629. goto err;
  1630. }
  1631. /* Fix buf for TLS and beyond */
  1632. if (s->version > SSL3_VERSION)
  1633. {
  1634. s2n(n,q);
  1635. n+=2;
  1636. }
  1637. s->session->master_key_length=
  1638. s->method->ssl3_enc->generate_master_secret(s,
  1639. s->session->master_key,
  1640. tmp_buf,sizeof tmp_buf);
  1641. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1642. }
  1643. #endif
  1644. #ifndef OPENSSL_NO_KRB5
  1645. else if (l & SSL_kKRB5)
  1646. {
  1647. krb5_error_code krb5rc;
  1648. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1649. /* krb5_data krb5_ap_req; */
  1650. krb5_data *enc_ticket;
  1651. krb5_data authenticator, *authp = NULL;
  1652. EVP_CIPHER_CTX ciph_ctx;
  1653. EVP_CIPHER *enc = NULL;
  1654. unsigned char iv[EVP_MAX_IV_LENGTH];
  1655. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1656. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  1657. + EVP_MAX_IV_LENGTH];
  1658. int padl, outl = sizeof(epms);
  1659. EVP_CIPHER_CTX_init(&ciph_ctx);
  1660. #ifdef KSSL_DEBUG
  1661. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1662. l, SSL_kKRB5);
  1663. #endif /* KSSL_DEBUG */
  1664. authp = NULL;
  1665. #ifdef KRB5SENDAUTH
  1666. if (KRB5SENDAUTH) authp = &authenticator;
  1667. #endif /* KRB5SENDAUTH */
  1668. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  1669. &kssl_err);
  1670. enc = kssl_map_enc(kssl_ctx->enctype);
  1671. if (enc == NULL)
  1672. goto err;
  1673. #ifdef KSSL_DEBUG
  1674. {
  1675. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1676. if (krb5rc && kssl_err.text)
  1677. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1678. }
  1679. #endif /* KSSL_DEBUG */
  1680. if (krb5rc)
  1681. {
  1682. ssl3_send_alert(s,SSL3_AL_FATAL,
  1683. SSL_AD_HANDSHAKE_FAILURE);
  1684. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1685. kssl_err.reason);
  1686. goto err;
  1687. }
  1688. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  1689. ** in place of RFC 2712 KerberosWrapper, as in:
  1690. **
  1691. ** Send ticket (copy to *p, set n = length)
  1692. ** n = krb5_ap_req.length;
  1693. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1694. ** if (krb5_ap_req.data)
  1695. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1696. **
  1697. ** Now using real RFC 2712 KerberosWrapper
  1698. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  1699. ** Note: 2712 "opaque" types are here replaced
  1700. ** with a 2-byte length followed by the value.
  1701. ** Example:
  1702. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  1703. ** Where "xx xx" = length bytes. Shown here with
  1704. ** optional authenticator omitted.
  1705. */
  1706. /* KerberosWrapper.Ticket */
  1707. s2n(enc_ticket->length,p);
  1708. memcpy(p, enc_ticket->data, enc_ticket->length);
  1709. p+= enc_ticket->length;
  1710. n = enc_ticket->length + 2;
  1711. /* KerberosWrapper.Authenticator */
  1712. if (authp && authp->length)
  1713. {
  1714. s2n(authp->length,p);
  1715. memcpy(p, authp->data, authp->length);
  1716. p+= authp->length;
  1717. n+= authp->length + 2;
  1718. free(authp->data);
  1719. authp->data = NULL;
  1720. authp->length = 0;
  1721. }
  1722. else
  1723. {
  1724. s2n(0,p);/* null authenticator length */
  1725. n+=2;
  1726. }
  1727. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  1728. goto err;
  1729. /* 20010420 VRS. Tried it this way; failed.
  1730. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1731. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1732. ** kssl_ctx->length);
  1733. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1734. */
  1735. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1736. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  1737. kssl_ctx->key,iv);
  1738. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  1739. sizeof tmp_buf);
  1740. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  1741. outl += padl;
  1742. if (outl > sizeof epms)
  1743. {
  1744. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1745. goto err;
  1746. }
  1747. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1748. /* KerberosWrapper.EncryptedPreMasterSecret */
  1749. s2n(outl,p);
  1750. memcpy(p, epms, outl);
  1751. p+=outl;
  1752. n+=outl + 2;
  1753. s->session->master_key_length=
  1754. s->method->ssl3_enc->generate_master_secret(s,
  1755. s->session->master_key,
  1756. tmp_buf, sizeof tmp_buf);
  1757. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  1758. OPENSSL_cleanse(epms, outl);
  1759. }
  1760. #endif
  1761. #ifndef OPENSSL_NO_DH
  1762. else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1763. {
  1764. DH *dh_srvr,*dh_clnt;
  1765. if (s->session->sess_cert->peer_dh_tmp != NULL)
  1766. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  1767. else
  1768. {
  1769. /* we get them from the cert */
  1770. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1771. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  1772. goto err;
  1773. }
  1774. /* generate a new random key */
  1775. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  1776. {
  1777. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1778. goto err;
  1779. }
  1780. if (!DH_generate_key(dh_clnt))
  1781. {
  1782. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1783. goto err;
  1784. }
  1785. /* use the 'p' output buffer for the DH key, but
  1786. * make sure to clear it out afterwards */
  1787. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  1788. if (n <= 0)
  1789. {
  1790. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1791. goto err;
  1792. }
  1793. /* generate master key from the result */
  1794. s->session->master_key_length=
  1795. s->method->ssl3_enc->generate_master_secret(s,
  1796. s->session->master_key,p,n);
  1797. /* clean up */
  1798. memset(p,0,n);
  1799. /* send off the data */
  1800. n=BN_num_bytes(dh_clnt->pub_key);
  1801. s2n(n,p);
  1802. BN_bn2bin(dh_clnt->pub_key,p);
  1803. n+=2;
  1804. DH_free(dh_clnt);
  1805. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  1806. }
  1807. #endif
  1808. #ifndef OPENSSL_NO_ECDH
  1809. else if ((l & SSL_kECDH) || (l & SSL_kECDHE))
  1810. {
  1811. const EC_GROUP *srvr_group = NULL;
  1812. EC_KEY *tkey;
  1813. int ecdh_clnt_cert = 0;
  1814. int field_size = 0;
  1815. /* Did we send out the client's
  1816. * ECDH share for use in premaster
  1817. * computation as part of client certificate?
  1818. * If so, set ecdh_clnt_cert to 1.
  1819. */
  1820. if ((l & SSL_kECDH) && (s->cert != NULL))
  1821. {
  1822. /* XXX: For now, we do not support client
  1823. * authentication using ECDH certificates.
  1824. * To add such support, one needs to add
  1825. * code that checks for appropriate
  1826. * conditions and sets ecdh_clnt_cert to 1.
  1827. * For example, the cert have an ECC
  1828. * key on the same curve as the server's
  1829. * and the key should be authorized for
  1830. * key agreement.
  1831. *
  1832. * One also needs to add code in ssl3_connect
  1833. * to skip sending the certificate verify
  1834. * message.
  1835. *
  1836. * if ((s->cert->key->privatekey != NULL) &&
  1837. * (s->cert->key->privatekey->type ==
  1838. * EVP_PKEY_EC) && ...)
  1839. * ecdh_clnt_cert = 1;
  1840. */
  1841. }
  1842. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  1843. {
  1844. tkey = s->session->sess_cert->peer_ecdh_tmp;
  1845. }
  1846. else
  1847. {
  1848. /* Get the Server Public Key from Cert */
  1849. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  1850. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1851. if ((srvr_pub_pkey == NULL) ||
  1852. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  1853. (srvr_pub_pkey->pkey.ec == NULL))
  1854. {
  1855. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1856. ERR_R_INTERNAL_ERROR);
  1857. goto err;
  1858. }
  1859. tkey = srvr_pub_pkey->pkey.ec;
  1860. }
  1861. srvr_group = EC_KEY_get0_group(tkey);
  1862. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  1863. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  1864. {
  1865. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1866. ERR_R_INTERNAL_ERROR);
  1867. goto err;
  1868. }
  1869. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  1870. {
  1871. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1872. goto err;
  1873. }
  1874. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  1875. {
  1876. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  1877. goto err;
  1878. }
  1879. if (ecdh_clnt_cert)
  1880. {
  1881. /* Reuse key info from our certificate
  1882. * We only need our private key to perform
  1883. * the ECDH computation.
  1884. */
  1885. const BIGNUM *priv_key;
  1886. tkey = s->cert->key->privatekey->pkey.ec;
  1887. priv_key = EC_KEY_get0_private_key(tkey);
  1888. if (priv_key == NULL)
  1889. {
  1890. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1891. goto err;
  1892. }
  1893. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  1894. {
  1895. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  1896. goto err;
  1897. }
  1898. }
  1899. else
  1900. {
  1901. /* Generate a new ECDH key pair */
  1902. if (!(EC_KEY_generate_key(clnt_ecdh)))
  1903. {
  1904. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1905. goto err;
  1906. }
  1907. }
  1908. /* use the 'p' output buffer for the ECDH key, but
  1909. * make sure to clear it out afterwards
  1910. */
  1911. field_size = EC_GROUP_get_degree(srvr_group);
  1912. if (field_size <= 0)
  1913. {
  1914. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1915. ERR_R_ECDH_LIB);
  1916. goto err;
  1917. }
  1918. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  1919. if (n <= 0)
  1920. {
  1921. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1922. ERR_R_ECDH_LIB);
  1923. goto err;
  1924. }
  1925. /* generate master key from the result */
  1926. s->session->master_key_length = s->method->ssl3_enc \
  1927. -> generate_master_secret(s,
  1928. s->session->master_key,
  1929. p, n);
  1930. memset(p, 0, n); /* clean up */
  1931. if (ecdh_clnt_cert)
  1932. {
  1933. /* Send empty client key exch message */
  1934. n = 0;
  1935. }
  1936. else
  1937. {
  1938. /* First check the size of encoding and
  1939. * allocate memory accordingly.
  1940. */
  1941. encoded_pt_len =
  1942. EC_POINT_point2oct(srvr_group,
  1943. EC_KEY_get0_public_key(clnt_ecdh),
  1944. POINT_CONVERSION_UNCOMPRESSED,
  1945. NULL, 0, NULL);
  1946. encodedPoint = (unsigned char *)
  1947. OPENSSL_malloc(encoded_pt_len *
  1948. sizeof(unsigned char));
  1949. bn_ctx = BN_CTX_new();
  1950. if ((encodedPoint == NULL) ||
  1951. (bn_ctx == NULL))
  1952. {
  1953. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1954. goto err;
  1955. }
  1956. /* Encode the public key */
  1957. n = EC_POINT_point2oct(srvr_group,
  1958. EC_KEY_get0_public_key(clnt_ecdh),
  1959. POINT_CONVERSION_UNCOMPRESSED,
  1960. encodedPoint, encoded_pt_len, bn_ctx);
  1961. *p = n; /* length of encoded point */
  1962. /* Encoded point will be copied here */
  1963. p += 1;
  1964. /* copy the point */
  1965. memcpy((unsigned char *)p, encodedPoint, n);
  1966. /* increment n to account for length field */
  1967. n += 1;
  1968. }
  1969. /* Free allocated memory */
  1970. BN_CTX_free(bn_ctx);
  1971. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1972. if (clnt_ecdh != NULL)
  1973. EC_KEY_free(clnt_ecdh);
  1974. EVP_PKEY_free(srvr_pub_pkey);
  1975. }
  1976. #endif /* !OPENSSL_NO_ECDH */
  1977. #ifndef OPENSSL_NO_PSK
  1978. else if (l & SSL_kPSK)
  1979. {
  1980. char identity[PSK_MAX_IDENTITY_LEN];
  1981. unsigned char *t = NULL;
  1982. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  1983. unsigned int pre_ms_len = 0, psk_len = 0;
  1984. int psk_err = 1;
  1985. n = 0;
  1986. if (s->psk_client_callback == NULL)
  1987. {
  1988. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1989. SSL_R_PSK_NO_CLIENT_CB);
  1990. goto err;
  1991. }
  1992. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  1993. identity, PSK_MAX_IDENTITY_LEN,
  1994. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1995. if (psk_len > PSK_MAX_PSK_LEN)
  1996. {
  1997. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1998. ERR_R_INTERNAL_ERROR);
  1999. goto psk_err;
  2000. }
  2001. else if (psk_len == 0)
  2002. {
  2003. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2004. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2005. goto psk_err;
  2006. }
  2007. /* create PSK pre_master_secret */
  2008. pre_ms_len = 2+psk_len+2+psk_len;
  2009. t = psk_or_pre_ms;
  2010. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2011. s2n(psk_len, t);
  2012. memset(t, 0, psk_len);
  2013. t+=psk_len;
  2014. s2n(psk_len, t);
  2015. if (s->session->psk_identity_hint != NULL)
  2016. OPENSSL_free(s->session->psk_identity_hint);
  2017. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2018. if (s->ctx->psk_identity_hint != NULL &&
  2019. s->session->psk_identity_hint == NULL)
  2020. {
  2021. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2022. ERR_R_MALLOC_FAILURE);
  2023. goto psk_err;
  2024. }
  2025. if (s->session->psk_identity != NULL)
  2026. OPENSSL_free(s->session->psk_identity);
  2027. s->session->psk_identity = BUF_strdup(identity);
  2028. if (s->session->psk_identity == NULL)
  2029. {
  2030. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2031. ERR_R_MALLOC_FAILURE);
  2032. goto psk_err;
  2033. }
  2034. s->session->master_key_length =
  2035. s->method->ssl3_enc->generate_master_secret(s,
  2036. s->session->master_key,
  2037. psk_or_pre_ms, pre_ms_len);
  2038. n = strlen(identity);
  2039. s2n(n, p);
  2040. memcpy(p, identity, n);
  2041. n+=2;
  2042. psk_err = 0;
  2043. psk_err:
  2044. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  2045. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2046. if (psk_err != 0)
  2047. {
  2048. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2049. goto err;
  2050. }
  2051. }
  2052. #endif
  2053. else
  2054. {
  2055. ssl3_send_alert(s, SSL3_AL_FATAL,
  2056. SSL_AD_HANDSHAKE_FAILURE);
  2057. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2058. ERR_R_INTERNAL_ERROR);
  2059. goto err;
  2060. }
  2061. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  2062. l2n3(n,d);
  2063. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2064. /* number of bytes to write */
  2065. s->init_num=n+4;
  2066. s->init_off=0;
  2067. }
  2068. /* SSL3_ST_CW_KEY_EXCH_B */
  2069. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2070. err:
  2071. #ifndef OPENSSL_NO_ECDH
  2072. BN_CTX_free(bn_ctx);
  2073. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2074. if (clnt_ecdh != NULL)
  2075. EC_KEY_free(clnt_ecdh);
  2076. EVP_PKEY_free(srvr_pub_pkey);
  2077. #endif
  2078. return(-1);
  2079. }
  2080. int ssl3_send_client_verify(SSL *s)
  2081. {
  2082. unsigned char *p,*d;
  2083. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  2084. EVP_PKEY *pkey;
  2085. #ifndef OPENSSL_NO_RSA
  2086. unsigned u=0;
  2087. #endif
  2088. unsigned long n;
  2089. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  2090. int j;
  2091. #endif
  2092. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2093. {
  2094. d=(unsigned char *)s->init_buf->data;
  2095. p= &(d[4]);
  2096. pkey=s->cert->key->privatekey;
  2097. s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
  2098. &(data[MD5_DIGEST_LENGTH]));
  2099. #ifndef OPENSSL_NO_RSA
  2100. if (pkey->type == EVP_PKEY_RSA)
  2101. {
  2102. s->method->ssl3_enc->cert_verify_mac(s,
  2103. &(s->s3->finish_dgst1),&(data[0]));
  2104. if (RSA_sign(NID_md5_sha1, data,
  2105. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  2106. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  2107. {
  2108. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  2109. goto err;
  2110. }
  2111. s2n(u,p);
  2112. n=u+2;
  2113. }
  2114. else
  2115. #endif
  2116. #ifndef OPENSSL_NO_DSA
  2117. if (pkey->type == EVP_PKEY_DSA)
  2118. {
  2119. if (!DSA_sign(pkey->save_type,
  2120. &(data[MD5_DIGEST_LENGTH]),
  2121. SHA_DIGEST_LENGTH,&(p[2]),
  2122. (unsigned int *)&j,pkey->pkey.dsa))
  2123. {
  2124. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  2125. goto err;
  2126. }
  2127. s2n(j,p);
  2128. n=j+2;
  2129. }
  2130. else
  2131. #endif
  2132. #ifndef OPENSSL_NO_ECDSA
  2133. if (pkey->type == EVP_PKEY_EC)
  2134. {
  2135. if (!ECDSA_sign(pkey->save_type,
  2136. &(data[MD5_DIGEST_LENGTH]),
  2137. SHA_DIGEST_LENGTH,&(p[2]),
  2138. (unsigned int *)&j,pkey->pkey.ec))
  2139. {
  2140. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2141. ERR_R_ECDSA_LIB);
  2142. goto err;
  2143. }
  2144. s2n(j,p);
  2145. n=j+2;
  2146. }
  2147. else
  2148. #endif
  2149. {
  2150. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  2151. goto err;
  2152. }
  2153. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  2154. l2n3(n,d);
  2155. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2156. s->init_num=(int)n+4;
  2157. s->init_off=0;
  2158. }
  2159. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2160. err:
  2161. return(-1);
  2162. }
  2163. int ssl3_send_client_certificate(SSL *s)
  2164. {
  2165. X509 *x509=NULL;
  2166. EVP_PKEY *pkey=NULL;
  2167. int i;
  2168. unsigned long l;
  2169. if (s->state == SSL3_ST_CW_CERT_A)
  2170. {
  2171. if ((s->cert == NULL) ||
  2172. (s->cert->key->x509 == NULL) ||
  2173. (s->cert->key->privatekey == NULL))
  2174. s->state=SSL3_ST_CW_CERT_B;
  2175. else
  2176. s->state=SSL3_ST_CW_CERT_C;
  2177. }
  2178. /* We need to get a client cert */
  2179. if (s->state == SSL3_ST_CW_CERT_B)
  2180. {
  2181. /* If we get an error, we need to
  2182. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2183. * We then get retied later */
  2184. i=0;
  2185. if (s->ctx->client_cert_cb != NULL)
  2186. i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
  2187. if (i < 0)
  2188. {
  2189. s->rwstate=SSL_X509_LOOKUP;
  2190. return(-1);
  2191. }
  2192. s->rwstate=SSL_NOTHING;
  2193. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2194. {
  2195. s->state=SSL3_ST_CW_CERT_B;
  2196. if ( !SSL_use_certificate(s,x509) ||
  2197. !SSL_use_PrivateKey(s,pkey))
  2198. i=0;
  2199. }
  2200. else if (i == 1)
  2201. {
  2202. i=0;
  2203. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2204. }
  2205. if (x509 != NULL) X509_free(x509);
  2206. if (pkey != NULL) EVP_PKEY_free(pkey);
  2207. if (i == 0)
  2208. {
  2209. if (s->version == SSL3_VERSION)
  2210. {
  2211. s->s3->tmp.cert_req=0;
  2212. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2213. return(1);
  2214. }
  2215. else
  2216. {
  2217. s->s3->tmp.cert_req=2;
  2218. }
  2219. }
  2220. /* Ok, we have a cert */
  2221. s->state=SSL3_ST_CW_CERT_C;
  2222. }
  2223. if (s->state == SSL3_ST_CW_CERT_C)
  2224. {
  2225. s->state=SSL3_ST_CW_CERT_D;
  2226. l=ssl3_output_cert_chain(s,
  2227. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  2228. s->init_num=(int)l;
  2229. s->init_off=0;
  2230. }
  2231. /* SSL3_ST_CW_CERT_D */
  2232. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2233. }
  2234. #define has_bits(i,m) (((i)&(m)) == (m))
  2235. int ssl3_check_cert_and_algorithm(SSL *s)
  2236. {
  2237. int i,idx;
  2238. long algs;
  2239. EVP_PKEY *pkey=NULL;
  2240. SESS_CERT *sc;
  2241. #ifndef OPENSSL_NO_RSA
  2242. RSA *rsa;
  2243. #endif
  2244. #ifndef OPENSSL_NO_DH
  2245. DH *dh;
  2246. #endif
  2247. sc=s->session->sess_cert;
  2248. if (sc == NULL)
  2249. {
  2250. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2251. goto err;
  2252. }
  2253. algs=s->s3->tmp.new_cipher->algorithms;
  2254. /* we don't have a certificate */
  2255. if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5|SSL_kPSK))
  2256. return(1);
  2257. #ifndef OPENSSL_NO_RSA
  2258. rsa=s->session->sess_cert->peer_rsa_tmp;
  2259. #endif
  2260. #ifndef OPENSSL_NO_DH
  2261. dh=s->session->sess_cert->peer_dh_tmp;
  2262. #endif
  2263. /* This is the passed certificate */
  2264. idx=sc->peer_cert_type;
  2265. #ifndef OPENSSL_NO_ECDH
  2266. if (idx == SSL_PKEY_ECC)
  2267. {
  2268. if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2269. s->s3->tmp.new_cipher) == 0)
  2270. { /* check failed */
  2271. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2272. goto f_err;
  2273. }
  2274. else
  2275. {
  2276. return 1;
  2277. }
  2278. }
  2279. #endif
  2280. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2281. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2282. EVP_PKEY_free(pkey);
  2283. /* Check that we have a certificate if we require one */
  2284. if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2285. {
  2286. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2287. goto f_err;
  2288. }
  2289. #ifndef OPENSSL_NO_DSA
  2290. else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2291. {
  2292. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2293. goto f_err;
  2294. }
  2295. #endif
  2296. #ifndef OPENSSL_NO_RSA
  2297. if ((algs & SSL_kRSA) &&
  2298. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2299. {
  2300. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2301. goto f_err;
  2302. }
  2303. #endif
  2304. #ifndef OPENSSL_NO_DH
  2305. if ((algs & SSL_kEDH) &&
  2306. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2307. {
  2308. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2309. goto f_err;
  2310. }
  2311. else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2312. {
  2313. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2314. goto f_err;
  2315. }
  2316. #ifndef OPENSSL_NO_DSA
  2317. else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2318. {
  2319. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2320. goto f_err;
  2321. }
  2322. #endif
  2323. #endif
  2324. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2325. {
  2326. #ifndef OPENSSL_NO_RSA
  2327. if (algs & SSL_kRSA)
  2328. {
  2329. if (rsa == NULL
  2330. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2331. {
  2332. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2333. goto f_err;
  2334. }
  2335. }
  2336. else
  2337. #endif
  2338. #ifndef OPENSSL_NO_DH
  2339. if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2340. {
  2341. if (dh == NULL
  2342. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2343. {
  2344. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  2345. goto f_err;
  2346. }
  2347. }
  2348. else
  2349. #endif
  2350. {
  2351. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2352. goto f_err;
  2353. }
  2354. }
  2355. return(1);
  2356. f_err:
  2357. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2358. err:
  2359. return(0);
  2360. }