s3_clnt.c 88 KB

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