s3_srvr.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. /* ssl/s3_srvr.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-2002 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. #define REUSE_CIPHER_BUG
  112. #define NETSCAPE_HANG_BUG
  113. #include <stdio.h>
  114. #include <openssl/buffer.h>
  115. #include <openssl/rand.h>
  116. #include <openssl/objects.h>
  117. #include <openssl/md5.h>
  118. #include <openssl/sha.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/x509.h>
  121. #include "ssl_locl.h"
  122. #include "cryptlib.h"
  123. static SSL_METHOD *ssl3_get_server_method(int ver);
  124. static int ssl3_get_client_hello(SSL *s);
  125. static int ssl3_check_client_hello(SSL *s);
  126. static int ssl3_send_server_hello(SSL *s);
  127. static int ssl3_send_server_key_exchange(SSL *s);
  128. static int ssl3_send_certificate_request(SSL *s);
  129. static int ssl3_send_server_done(SSL *s);
  130. static int ssl3_get_client_key_exchange(SSL *s);
  131. static int ssl3_get_client_certificate(SSL *s);
  132. static int ssl3_get_cert_verify(SSL *s);
  133. static int ssl3_send_hello_request(SSL *s);
  134. static SSL_METHOD *ssl3_get_server_method(int ver)
  135. {
  136. if (ver == SSL3_VERSION)
  137. return(SSLv3_server_method());
  138. else
  139. return(NULL);
  140. }
  141. SSL_METHOD *SSLv3_server_method(void)
  142. {
  143. static int init=1;
  144. static SSL_METHOD SSLv3_server_data;
  145. if (init)
  146. {
  147. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  148. if (init)
  149. {
  150. memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
  151. sizeof(SSL_METHOD));
  152. SSLv3_server_data.ssl_accept=ssl3_accept;
  153. SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
  154. init=0;
  155. }
  156. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  157. }
  158. return(&SSLv3_server_data);
  159. }
  160. int ssl3_accept(SSL *s)
  161. {
  162. BUF_MEM *buf;
  163. unsigned long l,Time=time(NULL);
  164. void (*cb)()=NULL;
  165. long num1;
  166. int ret= -1;
  167. int new_state,state,skip=0;
  168. RAND_add(&Time,sizeof(Time),0);
  169. ERR_clear_error();
  170. clear_sys_error();
  171. if (s->info_callback != NULL)
  172. cb=s->info_callback;
  173. else if (s->ctx->info_callback != NULL)
  174. cb=s->ctx->info_callback;
  175. /* init things to blank */
  176. s->in_handshake++;
  177. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  178. if (s->cert == NULL)
  179. {
  180. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  181. return(-1);
  182. }
  183. for (;;)
  184. {
  185. state=s->state;
  186. switch (s->state)
  187. {
  188. case SSL_ST_RENEGOTIATE:
  189. s->new_session=1;
  190. /* s->state=SSL_ST_ACCEPT; */
  191. case SSL_ST_BEFORE:
  192. case SSL_ST_ACCEPT:
  193. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  194. case SSL_ST_OK|SSL_ST_ACCEPT:
  195. s->server=1;
  196. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  197. if ((s->version>>8) != 3)
  198. {
  199. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR);
  200. return -1;
  201. }
  202. s->type=SSL_ST_ACCEPT;
  203. if (s->init_buf == NULL)
  204. {
  205. if ((buf=BUF_MEM_new()) == NULL)
  206. {
  207. ret= -1;
  208. goto end;
  209. }
  210. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  211. {
  212. ret= -1;
  213. goto end;
  214. }
  215. s->init_buf=buf;
  216. }
  217. if (!ssl3_setup_buffers(s))
  218. {
  219. ret= -1;
  220. goto end;
  221. }
  222. s->init_num=0;
  223. if (s->state != SSL_ST_RENEGOTIATE)
  224. {
  225. /* Ok, we now need to push on a buffering BIO so that
  226. * the output is sent in a way that TCP likes :-)
  227. */
  228. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  229. ssl3_init_finished_mac(s);
  230. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  231. s->ctx->stats.sess_accept++;
  232. }
  233. else
  234. {
  235. /* s->state == SSL_ST_RENEGOTIATE,
  236. * we will just send a HelloRequest */
  237. s->ctx->stats.sess_accept_renegotiate++;
  238. s->state=SSL3_ST_SW_HELLO_REQ_A;
  239. }
  240. break;
  241. case SSL3_ST_SW_HELLO_REQ_A:
  242. case SSL3_ST_SW_HELLO_REQ_B:
  243. s->shutdown=0;
  244. ret=ssl3_send_hello_request(s);
  245. if (ret <= 0) goto end;
  246. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  247. s->state=SSL3_ST_SW_FLUSH;
  248. s->init_num=0;
  249. ssl3_init_finished_mac(s);
  250. break;
  251. case SSL3_ST_SW_HELLO_REQ_C:
  252. s->state=SSL_ST_OK;
  253. break;
  254. case SSL3_ST_SR_CLNT_HELLO_A:
  255. case SSL3_ST_SR_CLNT_HELLO_B:
  256. case SSL3_ST_SR_CLNT_HELLO_C:
  257. s->shutdown=0;
  258. ret=ssl3_get_client_hello(s);
  259. if (ret <= 0) goto end;
  260. s->new_session = 2;
  261. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  262. s->init_num=0;
  263. break;
  264. case SSL3_ST_SW_SRVR_HELLO_A:
  265. case SSL3_ST_SW_SRVR_HELLO_B:
  266. ret=ssl3_send_server_hello(s);
  267. if (ret <= 0) goto end;
  268. if (s->hit)
  269. s->state=SSL3_ST_SW_CHANGE_A;
  270. else
  271. s->state=SSL3_ST_SW_CERT_A;
  272. s->init_num=0;
  273. break;
  274. case SSL3_ST_SW_CERT_A:
  275. case SSL3_ST_SW_CERT_B:
  276. /* Check if it is anon DH */
  277. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  278. {
  279. ret=ssl3_send_server_certificate(s);
  280. if (ret <= 0) goto end;
  281. }
  282. else
  283. skip=1;
  284. s->state=SSL3_ST_SW_KEY_EXCH_A;
  285. s->init_num=0;
  286. break;
  287. case SSL3_ST_SW_KEY_EXCH_A:
  288. case SSL3_ST_SW_KEY_EXCH_B:
  289. l=s->s3->tmp.new_cipher->algorithms;
  290. /* clear this, it may get reset by
  291. * send_server_key_exchange */
  292. if (s->options & SSL_OP_EPHEMERAL_RSA)
  293. s->s3->tmp.use_rsa_tmp=1;
  294. else
  295. s->s3->tmp.use_rsa_tmp=0;
  296. /* only send if a DH key exchange, fortezza or
  297. * RSA but we have a sign only certificate */
  298. if (s->s3->tmp.use_rsa_tmp
  299. || (l & (SSL_DH|SSL_kFZA))
  300. || ((l & SSL_kRSA)
  301. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  302. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  303. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  304. )
  305. )
  306. )
  307. )
  308. {
  309. ret=ssl3_send_server_key_exchange(s);
  310. if (ret <= 0) goto end;
  311. }
  312. else
  313. skip=1;
  314. s->state=SSL3_ST_SW_CERT_REQ_A;
  315. s->init_num=0;
  316. break;
  317. case SSL3_ST_SW_CERT_REQ_A:
  318. case SSL3_ST_SW_CERT_REQ_B:
  319. if (/* don't request cert unless asked for it: */
  320. !(s->verify_mode & SSL_VERIFY_PEER) ||
  321. /* if SSL_VERIFY_CLIENT_ONCE is set,
  322. * don't request cert during re-negotiation: */
  323. ((s->session->peer != NULL) &&
  324. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  325. /* never request cert in anonymous ciphersuites
  326. * (see section "Certificate request" in SSL 3 drafts
  327. * and in RFC 2246): */
  328. ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
  329. /* ... except when the application insists on verification
  330. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  331. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
  332. {
  333. /* no cert request */
  334. skip=1;
  335. s->s3->tmp.cert_request=0;
  336. s->state=SSL3_ST_SW_SRVR_DONE_A;
  337. }
  338. else
  339. {
  340. s->s3->tmp.cert_request=1;
  341. ret=ssl3_send_certificate_request(s);
  342. if (ret <= 0) goto end;
  343. #ifndef NETSCAPE_HANG_BUG
  344. s->state=SSL3_ST_SW_SRVR_DONE_A;
  345. #else
  346. s->state=SSL3_ST_SW_FLUSH;
  347. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  348. #endif
  349. s->init_num=0;
  350. }
  351. break;
  352. case SSL3_ST_SW_SRVR_DONE_A:
  353. case SSL3_ST_SW_SRVR_DONE_B:
  354. ret=ssl3_send_server_done(s);
  355. if (ret <= 0) goto end;
  356. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  357. s->state=SSL3_ST_SW_FLUSH;
  358. s->init_num=0;
  359. break;
  360. case SSL3_ST_SW_FLUSH:
  361. /* number of bytes to be flushed */
  362. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  363. if (num1 > 0)
  364. {
  365. s->rwstate=SSL_WRITING;
  366. num1=BIO_flush(s->wbio);
  367. if (num1 <= 0) { ret= -1; goto end; }
  368. s->rwstate=SSL_NOTHING;
  369. }
  370. s->state=s->s3->tmp.next_state;
  371. break;
  372. case SSL3_ST_SR_CERT_A:
  373. case SSL3_ST_SR_CERT_B:
  374. /* Check for second client hello (MS SGC) */
  375. ret = ssl3_check_client_hello(s);
  376. if (ret <= 0)
  377. goto end;
  378. if (ret == 2)
  379. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  380. else {
  381. if (s->s3->tmp.cert_request)
  382. {
  383. ret=ssl3_get_client_certificate(s);
  384. if (ret <= 0) goto end;
  385. }
  386. s->init_num=0;
  387. s->state=SSL3_ST_SR_KEY_EXCH_A;
  388. }
  389. break;
  390. case SSL3_ST_SR_KEY_EXCH_A:
  391. case SSL3_ST_SR_KEY_EXCH_B:
  392. ret=ssl3_get_client_key_exchange(s);
  393. if (ret <= 0) goto end;
  394. s->state=SSL3_ST_SR_CERT_VRFY_A;
  395. s->init_num=0;
  396. /* We need to get hashes here so if there is
  397. * a client cert, it can be verified */
  398. s->method->ssl3_enc->cert_verify_mac(s,
  399. &(s->s3->finish_dgst1),
  400. &(s->s3->tmp.cert_verify_md[0]));
  401. s->method->ssl3_enc->cert_verify_mac(s,
  402. &(s->s3->finish_dgst2),
  403. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  404. break;
  405. case SSL3_ST_SR_CERT_VRFY_A:
  406. case SSL3_ST_SR_CERT_VRFY_B:
  407. /* we should decide if we expected this one */
  408. ret=ssl3_get_cert_verify(s);
  409. if (ret <= 0) goto end;
  410. s->state=SSL3_ST_SR_FINISHED_A;
  411. s->init_num=0;
  412. break;
  413. case SSL3_ST_SR_FINISHED_A:
  414. case SSL3_ST_SR_FINISHED_B:
  415. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  416. SSL3_ST_SR_FINISHED_B);
  417. if (ret <= 0) goto end;
  418. if (s->hit)
  419. s->state=SSL_ST_OK;
  420. else
  421. s->state=SSL3_ST_SW_CHANGE_A;
  422. s->init_num=0;
  423. break;
  424. case SSL3_ST_SW_CHANGE_A:
  425. case SSL3_ST_SW_CHANGE_B:
  426. s->session->cipher=s->s3->tmp.new_cipher;
  427. if (!s->method->ssl3_enc->setup_key_block(s))
  428. { ret= -1; goto end; }
  429. ret=ssl3_send_change_cipher_spec(s,
  430. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  431. if (ret <= 0) goto end;
  432. s->state=SSL3_ST_SW_FINISHED_A;
  433. s->init_num=0;
  434. if (!s->method->ssl3_enc->change_cipher_state(s,
  435. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  436. {
  437. ret= -1;
  438. goto end;
  439. }
  440. break;
  441. case SSL3_ST_SW_FINISHED_A:
  442. case SSL3_ST_SW_FINISHED_B:
  443. ret=ssl3_send_finished(s,
  444. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  445. s->method->ssl3_enc->server_finished_label,
  446. s->method->ssl3_enc->server_finished_label_len);
  447. if (ret <= 0) goto end;
  448. s->state=SSL3_ST_SW_FLUSH;
  449. if (s->hit)
  450. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  451. else
  452. s->s3->tmp.next_state=SSL_ST_OK;
  453. s->init_num=0;
  454. break;
  455. case SSL_ST_OK:
  456. /* clean a few things up */
  457. ssl3_cleanup_key_block(s);
  458. BUF_MEM_free(s->init_buf);
  459. s->init_buf=NULL;
  460. /* remove buffering on output */
  461. ssl_free_wbio_buffer(s);
  462. s->init_num=0;
  463. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  464. {
  465. /* actually not necessarily a 'new' session */
  466. s->new_session=0;
  467. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  468. s->ctx->stats.sess_accept_good++;
  469. /* s->server=1; */
  470. s->handshake_func=ssl3_accept;
  471. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  472. }
  473. ret = 1;
  474. goto end;
  475. /* break; */
  476. default:
  477. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
  478. ret= -1;
  479. goto end;
  480. /* break; */
  481. }
  482. if (!s->s3->tmp.reuse_message && !skip)
  483. {
  484. if (s->debug)
  485. {
  486. if ((ret=BIO_flush(s->wbio)) <= 0)
  487. goto end;
  488. }
  489. if ((cb != NULL) && (s->state != state))
  490. {
  491. new_state=s->state;
  492. s->state=state;
  493. cb(s,SSL_CB_ACCEPT_LOOP,1);
  494. s->state=new_state;
  495. }
  496. }
  497. skip=0;
  498. }
  499. end:
  500. /* BIO_flush(s->wbio); */
  501. s->in_handshake--;
  502. if (cb != NULL)
  503. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  504. return(ret);
  505. }
  506. static int ssl3_send_hello_request(SSL *s)
  507. {
  508. unsigned char *p;
  509. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  510. {
  511. p=(unsigned char *)s->init_buf->data;
  512. *(p++)=SSL3_MT_HELLO_REQUEST;
  513. *(p++)=0;
  514. *(p++)=0;
  515. *(p++)=0;
  516. s->state=SSL3_ST_SW_HELLO_REQ_B;
  517. /* number of bytes to write */
  518. s->init_num=4;
  519. s->init_off=0;
  520. }
  521. /* SSL3_ST_SW_HELLO_REQ_B */
  522. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  523. }
  524. static int ssl3_check_client_hello(SSL *s)
  525. {
  526. int ok;
  527. long n;
  528. /* this function is called when we really expect a Certificate message,
  529. * so permit appropriate message length */
  530. n=ssl3_get_message(s,
  531. SSL3_ST_SR_CERT_A,
  532. SSL3_ST_SR_CERT_B,
  533. -1,
  534. #if defined(MSDOS) && !defined(WIN32)
  535. 1024*30, /* 30k max cert list :-) */
  536. #else
  537. 1024*100, /* 100k max cert list :-) */
  538. #endif
  539. &ok);
  540. if (!ok) return((int)n);
  541. s->s3->tmp.reuse_message = 1;
  542. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  543. {
  544. /* Throw away what we have done so far in the current handshake,
  545. * which will now be aborted. (A full SSL_clear would be too much.)
  546. * I hope that tmp.dh is the only thing that may need to be cleared
  547. * when a handshake is not completed ... */
  548. #ifndef NO_DH
  549. if (s->s3->tmp.dh != NULL)
  550. {
  551. DH_free(s->s3->tmp.dh);
  552. s->s3->tmp.dh = NULL;
  553. }
  554. #endif
  555. return 2;
  556. }
  557. return 1;
  558. }
  559. static int ssl3_get_client_hello(SSL *s)
  560. {
  561. int i,j,ok,al,ret= -1;
  562. long n;
  563. unsigned long id;
  564. unsigned char *p,*d,*q;
  565. SSL_CIPHER *c;
  566. SSL_COMP *comp=NULL;
  567. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  568. /* We do this so that we will respond with our native type.
  569. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  570. * This down switching should be handled by a different method.
  571. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  572. * TLSv1.
  573. */
  574. if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
  575. {
  576. s->first_packet=1;
  577. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  578. }
  579. n=ssl3_get_message(s,
  580. SSL3_ST_SR_CLNT_HELLO_B,
  581. SSL3_ST_SR_CLNT_HELLO_C,
  582. SSL3_MT_CLIENT_HELLO,
  583. SSL3_RT_MAX_PLAIN_LENGTH,
  584. &ok);
  585. if (!ok) return((int)n);
  586. d=p=(unsigned char *)s->init_buf->data;
  587. /* use version from inside client hello, not from record header
  588. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  589. s->client_version=(((int)p[0])<<8)|(int)p[1];
  590. p+=2;
  591. if (s->client_version < s->version)
  592. {
  593. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
  594. if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
  595. {
  596. /* similar to ssl3_get_record, send alert using remote version number */
  597. s->version = s->client_version;
  598. }
  599. al = SSL_AD_PROTOCOL_VERSION;
  600. goto f_err;
  601. }
  602. /* load the client random */
  603. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  604. p+=SSL3_RANDOM_SIZE;
  605. /* get the session-id */
  606. j= *(p++);
  607. s->hit=0;
  608. if (j == 0)
  609. {
  610. if (!ssl_get_new_session(s,1))
  611. goto err;
  612. }
  613. else
  614. {
  615. i=ssl_get_prev_session(s,p,j);
  616. if (i == 1)
  617. { /* previous session */
  618. s->hit=1;
  619. }
  620. else if (i == -1)
  621. goto err;
  622. else /* i == 0 */
  623. {
  624. if (!ssl_get_new_session(s,1))
  625. goto err;
  626. }
  627. }
  628. p+=j;
  629. n2s(p,i);
  630. if ((i == 0) && (j != 0))
  631. {
  632. /* we need a cipher if we are not resuming a session */
  633. al=SSL_AD_ILLEGAL_PARAMETER;
  634. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
  635. goto f_err;
  636. }
  637. if ((p+i) >= (d+n))
  638. {
  639. /* not enough data */
  640. al=SSL_AD_DECODE_ERROR;
  641. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  642. goto f_err;
  643. }
  644. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  645. == NULL))
  646. {
  647. goto err;
  648. }
  649. p+=i;
  650. /* If it is a hit, check that the cipher is in the list */
  651. if ((s->hit) && (i > 0))
  652. {
  653. j=0;
  654. id=s->session->cipher->id;
  655. #ifdef CIPHER_DEBUG
  656. printf("client sent %d ciphers\n",sk_num(ciphers));
  657. #endif
  658. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  659. {
  660. c=sk_SSL_CIPHER_value(ciphers,i);
  661. #ifdef CIPHER_DEBUG
  662. printf("client [%2d of %2d]:%s\n",
  663. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  664. #endif
  665. if (c->id == id)
  666. {
  667. j=1;
  668. break;
  669. }
  670. }
  671. if (j == 0)
  672. {
  673. if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  674. {
  675. /* Very bad for multi-threading.... */
  676. s->session->cipher=sk_SSL_CIPHER_value(ciphers,
  677. 0);
  678. }
  679. else
  680. {
  681. /* we need to have the cipher in the cipher
  682. * list if we are asked to reuse it */
  683. al=SSL_AD_ILLEGAL_PARAMETER;
  684. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
  685. goto f_err;
  686. }
  687. }
  688. }
  689. /* compression */
  690. i= *(p++);
  691. if ((p+i) > (d+n))
  692. {
  693. /* not enough data */
  694. al=SSL_AD_DECODE_ERROR;
  695. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  696. goto f_err;
  697. }
  698. q=p;
  699. for (j=0; j<i; j++)
  700. {
  701. if (p[j] == 0) break;
  702. }
  703. p+=i;
  704. if (j >= i)
  705. {
  706. /* no compress */
  707. al=SSL_AD_DECODE_ERROR;
  708. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
  709. goto f_err;
  710. }
  711. /* Worst case, we will use the NULL compression, but if we have other
  712. * options, we will now look for them. We have i-1 compression
  713. * algorithms from the client, starting at q. */
  714. s->s3->tmp.new_compression=NULL;
  715. if (s->ctx->comp_methods != NULL)
  716. { /* See if we have a match */
  717. int m,nn,o,v,done=0;
  718. nn=sk_SSL_COMP_num(s->ctx->comp_methods);
  719. for (m=0; m<nn; m++)
  720. {
  721. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  722. v=comp->id;
  723. for (o=0; o<i; o++)
  724. {
  725. if (v == q[o])
  726. {
  727. done=1;
  728. break;
  729. }
  730. }
  731. if (done) break;
  732. }
  733. if (done)
  734. s->s3->tmp.new_compression=comp;
  735. else
  736. comp=NULL;
  737. }
  738. /* TLS does not mind if there is extra stuff */
  739. #if 0 /* SSL 3.0 does not mind either, so we should disable this test
  740. * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
  741. * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
  742. if (s->version == SSL3_VERSION)
  743. {
  744. if (p < (d+n))
  745. {
  746. /* wrong number of bytes,
  747. * there could be more to follow */
  748. al=SSL_AD_DECODE_ERROR;
  749. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  750. goto f_err;
  751. }
  752. }
  753. #endif
  754. /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
  755. * pick a cipher */
  756. if (!s->hit)
  757. {
  758. s->session->compress_meth=(comp == NULL)?0:comp->id;
  759. if (s->session->ciphers != NULL)
  760. sk_SSL_CIPHER_free(s->session->ciphers);
  761. s->session->ciphers=ciphers;
  762. if (ciphers == NULL)
  763. {
  764. al=SSL_AD_ILLEGAL_PARAMETER;
  765. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
  766. goto f_err;
  767. }
  768. ciphers=NULL;
  769. c=ssl3_choose_cipher(s,s->session->ciphers,
  770. ssl_get_ciphers_by_id(s));
  771. if (c == NULL)
  772. {
  773. al=SSL_AD_HANDSHAKE_FAILURE;
  774. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  775. goto f_err;
  776. }
  777. s->s3->tmp.new_cipher=c;
  778. }
  779. else
  780. {
  781. /* Session-id reuse */
  782. #ifdef REUSE_CIPHER_BUG
  783. STACK_OF(SSL_CIPHER) *sk;
  784. SSL_CIPHER *nc=NULL;
  785. SSL_CIPHER *ec=NULL;
  786. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  787. {
  788. sk=s->session->ciphers;
  789. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  790. {
  791. c=sk_SSL_CIPHER_value(sk,i);
  792. if (c->algorithms & SSL_eNULL)
  793. nc=c;
  794. if (SSL_C_IS_EXPORT(c))
  795. ec=c;
  796. }
  797. if (nc != NULL)
  798. s->s3->tmp.new_cipher=nc;
  799. else if (ec != NULL)
  800. s->s3->tmp.new_cipher=ec;
  801. else
  802. s->s3->tmp.new_cipher=s->session->cipher;
  803. }
  804. else
  805. #endif
  806. s->s3->tmp.new_cipher=s->session->cipher;
  807. }
  808. /* we now have the following setup.
  809. * client_random
  810. * cipher_list - our prefered list of ciphers
  811. * ciphers - the clients prefered list of ciphers
  812. * compression - basically ignored right now
  813. * ssl version is set - sslv3
  814. * s->session - The ssl session has been setup.
  815. * s->hit - session reuse flag
  816. * s->tmp.new_cipher - the new cipher to use.
  817. */
  818. ret=1;
  819. if (0)
  820. {
  821. f_err:
  822. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  823. }
  824. err:
  825. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  826. return(ret);
  827. }
  828. static int ssl3_send_server_hello(SSL *s)
  829. {
  830. unsigned char *buf;
  831. unsigned char *p,*d;
  832. int i,sl;
  833. unsigned long l,Time;
  834. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  835. {
  836. buf=(unsigned char *)s->init_buf->data;
  837. p=s->s3->server_random;
  838. Time=time(NULL); /* Time */
  839. l2n(Time,p);
  840. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  841. /* Do the message type and length last */
  842. d=p= &(buf[4]);
  843. *(p++)=s->version>>8;
  844. *(p++)=s->version&0xff;
  845. /* Random stuff */
  846. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  847. p+=SSL3_RANDOM_SIZE;
  848. /* now in theory we have 3 options to sending back the
  849. * session id. If it is a re-use, we send back the
  850. * old session-id, if it is a new session, we send
  851. * back the new session-id or we send back a 0 length
  852. * session-id if we want it to be single use.
  853. * Currently I will not implement the '0' length session-id
  854. * 12-Jan-98 - I'll now support the '0' length stuff.
  855. */
  856. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  857. s->session->session_id_length=0;
  858. sl=s->session->session_id_length;
  859. if (sl > sizeof s->session->session_id)
  860. {
  861. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_INTERNAL_ERROR);
  862. return -1;
  863. }
  864. *(p++)=sl;
  865. memcpy(p,s->session->session_id,sl);
  866. p+=sl;
  867. /* put the cipher */
  868. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  869. p+=i;
  870. /* put the compression method */
  871. if (s->s3->tmp.new_compression == NULL)
  872. *(p++)=0;
  873. else
  874. *(p++)=s->s3->tmp.new_compression->id;
  875. /* do the header */
  876. l=(p-d);
  877. d=buf;
  878. *(d++)=SSL3_MT_SERVER_HELLO;
  879. l2n3(l,d);
  880. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  881. /* number of bytes to write */
  882. s->init_num=p-buf;
  883. s->init_off=0;
  884. }
  885. /* SSL3_ST_CW_CLNT_HELLO_B */
  886. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  887. }
  888. static int ssl3_send_server_done(SSL *s)
  889. {
  890. unsigned char *p;
  891. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  892. {
  893. p=(unsigned char *)s->init_buf->data;
  894. /* do the header */
  895. *(p++)=SSL3_MT_SERVER_DONE;
  896. *(p++)=0;
  897. *(p++)=0;
  898. *(p++)=0;
  899. s->state=SSL3_ST_SW_SRVR_DONE_B;
  900. /* number of bytes to write */
  901. s->init_num=4;
  902. s->init_off=0;
  903. }
  904. /* SSL3_ST_CW_CLNT_HELLO_B */
  905. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  906. }
  907. static int ssl3_send_server_key_exchange(SSL *s)
  908. {
  909. #ifndef NO_RSA
  910. unsigned char *q;
  911. int j,num;
  912. RSA *rsa;
  913. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  914. unsigned int u;
  915. #endif
  916. #ifndef NO_DH
  917. DH *dh=NULL,*dhp;
  918. #endif
  919. EVP_PKEY *pkey;
  920. unsigned char *p,*d;
  921. int al,i;
  922. unsigned long type;
  923. int n;
  924. CERT *cert;
  925. BIGNUM *r[4];
  926. int nr[4],kn;
  927. BUF_MEM *buf;
  928. EVP_MD_CTX md_ctx;
  929. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  930. {
  931. type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  932. cert=s->cert;
  933. buf=s->init_buf;
  934. r[0]=r[1]=r[2]=r[3]=NULL;
  935. n=0;
  936. #ifndef NO_RSA
  937. if (type & SSL_kRSA)
  938. {
  939. rsa=cert->rsa_tmp;
  940. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  941. {
  942. rsa=s->cert->rsa_tmp_cb(s,
  943. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  944. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  945. if(rsa == NULL)
  946. {
  947. al=SSL_AD_HANDSHAKE_FAILURE;
  948. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  949. goto f_err;
  950. }
  951. CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
  952. cert->rsa_tmp=rsa;
  953. }
  954. if (rsa == NULL)
  955. {
  956. al=SSL_AD_HANDSHAKE_FAILURE;
  957. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  958. goto f_err;
  959. }
  960. r[0]=rsa->n;
  961. r[1]=rsa->e;
  962. s->s3->tmp.use_rsa_tmp=1;
  963. }
  964. else
  965. #endif
  966. #ifndef NO_DH
  967. if (type & SSL_kEDH)
  968. {
  969. dhp=cert->dh_tmp;
  970. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  971. dhp=s->cert->dh_tmp_cb(s,
  972. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  973. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  974. if (dhp == NULL)
  975. {
  976. al=SSL_AD_HANDSHAKE_FAILURE;
  977. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  978. goto f_err;
  979. }
  980. if (s->s3->tmp.dh != NULL)
  981. {
  982. DH_free(dh);
  983. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
  984. goto err;
  985. }
  986. if ((dh=DHparams_dup(dhp)) == NULL)
  987. {
  988. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  989. goto err;
  990. }
  991. s->s3->tmp.dh=dh;
  992. if ((dhp->pub_key == NULL ||
  993. dhp->priv_key == NULL ||
  994. (s->options & SSL_OP_SINGLE_DH_USE)))
  995. {
  996. if(!DH_generate_key(dh))
  997. {
  998. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  999. ERR_R_DH_LIB);
  1000. goto err;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. dh->pub_key=BN_dup(dhp->pub_key);
  1006. dh->priv_key=BN_dup(dhp->priv_key);
  1007. if ((dh->pub_key == NULL) ||
  1008. (dh->priv_key == NULL))
  1009. {
  1010. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1011. goto err;
  1012. }
  1013. }
  1014. r[0]=dh->p;
  1015. r[1]=dh->g;
  1016. r[2]=dh->pub_key;
  1017. }
  1018. else
  1019. #endif
  1020. {
  1021. al=SSL_AD_HANDSHAKE_FAILURE;
  1022. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1023. goto f_err;
  1024. }
  1025. for (i=0; r[i] != NULL; i++)
  1026. {
  1027. nr[i]=BN_num_bytes(r[i]);
  1028. n+=2+nr[i];
  1029. }
  1030. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  1031. {
  1032. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1033. == NULL)
  1034. {
  1035. al=SSL_AD_DECODE_ERROR;
  1036. goto f_err;
  1037. }
  1038. kn=EVP_PKEY_size(pkey);
  1039. }
  1040. else
  1041. {
  1042. pkey=NULL;
  1043. kn=0;
  1044. }
  1045. if (!BUF_MEM_grow(buf,n+4+kn))
  1046. {
  1047. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1048. goto err;
  1049. }
  1050. d=(unsigned char *)s->init_buf->data;
  1051. p= &(d[4]);
  1052. for (i=0; r[i] != NULL; i++)
  1053. {
  1054. s2n(nr[i],p);
  1055. BN_bn2bin(r[i],p);
  1056. p+=nr[i];
  1057. }
  1058. /* not anonymous */
  1059. if (pkey != NULL)
  1060. {
  1061. /* n is the length of the params, they start at &(d[4])
  1062. * and p points to the space at the end. */
  1063. #ifndef NO_RSA
  1064. if (pkey->type == EVP_PKEY_RSA)
  1065. {
  1066. q=md_buf;
  1067. j=0;
  1068. for (num=2; num > 0; num--)
  1069. {
  1070. EVP_DigestInit(&md_ctx,(num == 2)
  1071. ?s->ctx->md5:s->ctx->sha1);
  1072. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1073. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1074. EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  1075. EVP_DigestFinal(&md_ctx,q,
  1076. (unsigned int *)&i);
  1077. q+=i;
  1078. j+=i;
  1079. }
  1080. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1081. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1082. {
  1083. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1084. goto err;
  1085. }
  1086. s2n(u,p);
  1087. n+=u+2;
  1088. }
  1089. else
  1090. #endif
  1091. #if !defined(NO_DSA)
  1092. if (pkey->type == EVP_PKEY_DSA)
  1093. {
  1094. /* lets do DSS */
  1095. EVP_SignInit(&md_ctx,EVP_dss1());
  1096. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1097. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1098. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1099. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1100. (unsigned int *)&i,pkey))
  1101. {
  1102. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1103. goto err;
  1104. }
  1105. s2n(i,p);
  1106. n+=i+2;
  1107. }
  1108. else
  1109. #endif
  1110. {
  1111. /* Is this error check actually needed? */
  1112. al=SSL_AD_HANDSHAKE_FAILURE;
  1113. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1114. goto f_err;
  1115. }
  1116. }
  1117. *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1118. l2n3(n,d);
  1119. /* we should now have things packed up, so lets send
  1120. * it off */
  1121. s->init_num=n+4;
  1122. s->init_off=0;
  1123. }
  1124. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1125. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1126. f_err:
  1127. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1128. err:
  1129. return(-1);
  1130. }
  1131. static int ssl3_send_certificate_request(SSL *s)
  1132. {
  1133. unsigned char *p,*d;
  1134. int i,j,nl,off,n;
  1135. STACK_OF(X509_NAME) *sk=NULL;
  1136. X509_NAME *name;
  1137. BUF_MEM *buf;
  1138. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1139. {
  1140. buf=s->init_buf;
  1141. d=p=(unsigned char *)&(buf->data[4]);
  1142. /* get the list of acceptable cert types */
  1143. p++;
  1144. n=ssl3_get_req_cert_type(s,p);
  1145. d[0]=n;
  1146. p+=n;
  1147. n++;
  1148. off=n;
  1149. p+=2;
  1150. n+=2;
  1151. sk=SSL_get_client_CA_list(s);
  1152. nl=0;
  1153. if (sk != NULL)
  1154. {
  1155. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1156. {
  1157. name=sk_X509_NAME_value(sk,i);
  1158. j=i2d_X509_NAME(name,NULL);
  1159. if (!BUF_MEM_grow(buf,4+n+j+2))
  1160. {
  1161. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1162. goto err;
  1163. }
  1164. p=(unsigned char *)&(buf->data[4+n]);
  1165. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1166. {
  1167. s2n(j,p);
  1168. i2d_X509_NAME(name,&p);
  1169. n+=2+j;
  1170. nl+=2+j;
  1171. }
  1172. else
  1173. {
  1174. d=p;
  1175. i2d_X509_NAME(name,&p);
  1176. j-=2; s2n(j,d); j+=2;
  1177. n+=j;
  1178. nl+=j;
  1179. }
  1180. }
  1181. }
  1182. /* else no CA names */
  1183. p=(unsigned char *)&(buf->data[4+off]);
  1184. s2n(nl,p);
  1185. d=(unsigned char *)buf->data;
  1186. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1187. l2n3(n,d);
  1188. /* we should now have things packed up, so lets send
  1189. * it off */
  1190. s->init_num=n+4;
  1191. s->init_off=0;
  1192. #ifdef NETSCAPE_HANG_BUG
  1193. p=(unsigned char *)s->init_buf->data + s->init_num;
  1194. /* do the header */
  1195. *(p++)=SSL3_MT_SERVER_DONE;
  1196. *(p++)=0;
  1197. *(p++)=0;
  1198. *(p++)=0;
  1199. s->init_num += 4;
  1200. #endif
  1201. s->state = SSL3_ST_SW_CERT_REQ_B;
  1202. }
  1203. /* SSL3_ST_SW_CERT_REQ_B */
  1204. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1205. err:
  1206. return(-1);
  1207. }
  1208. static int ssl3_get_client_key_exchange(SSL *s)
  1209. {
  1210. int i,al,ok;
  1211. long n;
  1212. unsigned long l;
  1213. unsigned char *p;
  1214. #ifndef NO_RSA
  1215. RSA *rsa=NULL;
  1216. EVP_PKEY *pkey=NULL;
  1217. #endif
  1218. #ifndef NO_DH
  1219. BIGNUM *pub=NULL;
  1220. DH *dh_srvr;
  1221. #endif
  1222. n=ssl3_get_message(s,
  1223. SSL3_ST_SR_KEY_EXCH_A,
  1224. SSL3_ST_SR_KEY_EXCH_B,
  1225. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1226. 2048, /* ???? */
  1227. &ok);
  1228. if (!ok) return((int)n);
  1229. p=(unsigned char *)s->init_buf->data;
  1230. l=s->s3->tmp.new_cipher->algorithms;
  1231. #ifndef NO_RSA
  1232. if (l & SSL_kRSA)
  1233. {
  1234. /* FIX THIS UP EAY EAY EAY EAY */
  1235. if (s->s3->tmp.use_rsa_tmp)
  1236. {
  1237. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1238. rsa=s->cert->rsa_tmp;
  1239. /* Don't do a callback because rsa_tmp should
  1240. * be sent already */
  1241. if (rsa == NULL)
  1242. {
  1243. al=SSL_AD_HANDSHAKE_FAILURE;
  1244. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1245. goto f_err;
  1246. }
  1247. }
  1248. else
  1249. {
  1250. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1251. if ( (pkey == NULL) ||
  1252. (pkey->type != EVP_PKEY_RSA) ||
  1253. (pkey->pkey.rsa == NULL))
  1254. {
  1255. al=SSL_AD_HANDSHAKE_FAILURE;
  1256. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1257. goto f_err;
  1258. }
  1259. rsa=pkey->pkey.rsa;
  1260. }
  1261. /* TLS */
  1262. if (s->version > SSL3_VERSION)
  1263. {
  1264. n2s(p,i);
  1265. if (n != i+2)
  1266. {
  1267. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1268. {
  1269. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1270. goto err;
  1271. }
  1272. else
  1273. p-=2;
  1274. }
  1275. else
  1276. n=i;
  1277. }
  1278. i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1279. al = -1;
  1280. if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1281. {
  1282. al=SSL_AD_DECODE_ERROR;
  1283. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
  1284. }
  1285. if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1286. {
  1287. /* The premaster secret must contain the same version number as the
  1288. * ClientHello to detect version rollback attacks (strangely, the
  1289. * protocol does not offer such protection for DH ciphersuites).
  1290. * However, buggy clients exist that send the negotiated protocol
  1291. * version instead if the server does not support the requested
  1292. * protocol version.
  1293. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1294. if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1295. (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1296. {
  1297. al=SSL_AD_DECODE_ERROR;
  1298. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
  1299. /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  1300. * (http://eprint.iacr.org/2003/052/) exploits the version
  1301. * number check as a "bad version oracle" -- an alert would
  1302. * reveal that the plaintext corresponding to some ciphertext
  1303. * made up by the adversary is properly formatted except
  1304. * that the version number is wrong. To avoid such attacks,
  1305. * we should treat this just like any other decryption error. */
  1306. }
  1307. }
  1308. if (al != -1)
  1309. {
  1310. /* Some decryption failure -- use random value instead as countermeasure
  1311. * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
  1312. * (see RFC 2246, section 7.4.7.1). */
  1313. ERR_clear_error();
  1314. i = SSL_MAX_MASTER_KEY_LENGTH;
  1315. p[0] = s->client_version >> 8;
  1316. p[1] = s->client_version & 0xff;
  1317. RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
  1318. }
  1319. s->session->master_key_length=
  1320. s->method->ssl3_enc->generate_master_secret(s,
  1321. s->session->master_key,
  1322. p,i);
  1323. OPENSSL_cleanse(p,i);
  1324. }
  1325. else
  1326. #endif
  1327. #ifndef NO_DH
  1328. if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1329. {
  1330. n2s(p,i);
  1331. if (n != i+2)
  1332. {
  1333. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1334. {
  1335. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1336. goto err;
  1337. }
  1338. else
  1339. {
  1340. p-=2;
  1341. i=(int)n;
  1342. }
  1343. }
  1344. if (n == 0L) /* the parameters are in the cert */
  1345. {
  1346. al=SSL_AD_HANDSHAKE_FAILURE;
  1347. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1348. goto f_err;
  1349. }
  1350. else
  1351. {
  1352. if (s->s3->tmp.dh == NULL)
  1353. {
  1354. al=SSL_AD_HANDSHAKE_FAILURE;
  1355. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1356. goto f_err;
  1357. }
  1358. else
  1359. dh_srvr=s->s3->tmp.dh;
  1360. }
  1361. pub=BN_bin2bn(p,i,NULL);
  1362. if (pub == NULL)
  1363. {
  1364. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1365. goto err;
  1366. }
  1367. i=DH_compute_key(p,pub,dh_srvr);
  1368. if (i <= 0)
  1369. {
  1370. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1371. goto err;
  1372. }
  1373. DH_free(s->s3->tmp.dh);
  1374. s->s3->tmp.dh=NULL;
  1375. BN_clear_free(pub);
  1376. pub=NULL;
  1377. s->session->master_key_length=
  1378. s->method->ssl3_enc->generate_master_secret(s,
  1379. s->session->master_key,p,i);
  1380. OPENSSL_cleanse(p,i);
  1381. }
  1382. else
  1383. #endif
  1384. {
  1385. al=SSL_AD_HANDSHAKE_FAILURE;
  1386. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
  1387. goto f_err;
  1388. }
  1389. return(1);
  1390. f_err:
  1391. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1392. #if !defined(NO_DH) || !defined(NO_RSA)
  1393. err:
  1394. #endif
  1395. return(-1);
  1396. }
  1397. static int ssl3_get_cert_verify(SSL *s)
  1398. {
  1399. EVP_PKEY *pkey=NULL;
  1400. unsigned char *p;
  1401. int al,ok,ret=0;
  1402. long n;
  1403. int type=0,i,j;
  1404. X509 *peer;
  1405. n=ssl3_get_message(s,
  1406. SSL3_ST_SR_CERT_VRFY_A,
  1407. SSL3_ST_SR_CERT_VRFY_B,
  1408. -1,
  1409. 514, /* 514? */
  1410. &ok);
  1411. if (!ok) return((int)n);
  1412. if (s->session->peer != NULL)
  1413. {
  1414. peer=s->session->peer;
  1415. pkey=X509_get_pubkey(peer);
  1416. type=X509_certificate_type(peer,pkey);
  1417. }
  1418. else
  1419. {
  1420. peer=NULL;
  1421. pkey=NULL;
  1422. }
  1423. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  1424. {
  1425. s->s3->tmp.reuse_message=1;
  1426. if ((peer != NULL) && (type | EVP_PKT_SIGN))
  1427. {
  1428. al=SSL_AD_UNEXPECTED_MESSAGE;
  1429. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  1430. goto f_err;
  1431. }
  1432. ret=1;
  1433. goto end;
  1434. }
  1435. if (peer == NULL)
  1436. {
  1437. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  1438. al=SSL_AD_UNEXPECTED_MESSAGE;
  1439. goto f_err;
  1440. }
  1441. if (!(type & EVP_PKT_SIGN))
  1442. {
  1443. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  1444. al=SSL_AD_ILLEGAL_PARAMETER;
  1445. goto f_err;
  1446. }
  1447. if (s->s3->change_cipher_spec)
  1448. {
  1449. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  1450. al=SSL_AD_UNEXPECTED_MESSAGE;
  1451. goto f_err;
  1452. }
  1453. /* we now have a signature that we need to verify */
  1454. p=(unsigned char *)s->init_buf->data;
  1455. n2s(p,i);
  1456. n-=2;
  1457. if (i > n)
  1458. {
  1459. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  1460. al=SSL_AD_DECODE_ERROR;
  1461. goto f_err;
  1462. }
  1463. j=EVP_PKEY_size(pkey);
  1464. if ((i > j) || (n > j) || (n <= 0))
  1465. {
  1466. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  1467. al=SSL_AD_DECODE_ERROR;
  1468. goto f_err;
  1469. }
  1470. #ifndef NO_RSA
  1471. if (pkey->type == EVP_PKEY_RSA)
  1472. {
  1473. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  1474. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  1475. pkey->pkey.rsa);
  1476. if (i < 0)
  1477. {
  1478. al=SSL_AD_DECRYPT_ERROR;
  1479. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  1480. goto f_err;
  1481. }
  1482. if (i == 0)
  1483. {
  1484. al=SSL_AD_DECRYPT_ERROR;
  1485. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  1486. goto f_err;
  1487. }
  1488. }
  1489. else
  1490. #endif
  1491. #ifndef NO_DSA
  1492. if (pkey->type == EVP_PKEY_DSA)
  1493. {
  1494. j=DSA_verify(pkey->save_type,
  1495. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  1496. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  1497. if (j <= 0)
  1498. {
  1499. /* bad signature */
  1500. al=SSL_AD_DECRYPT_ERROR;
  1501. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  1502. goto f_err;
  1503. }
  1504. }
  1505. else
  1506. #endif
  1507. {
  1508. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
  1509. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  1510. goto f_err;
  1511. }
  1512. ret=1;
  1513. if (0)
  1514. {
  1515. f_err:
  1516. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1517. }
  1518. end:
  1519. EVP_PKEY_free(pkey);
  1520. return(ret);
  1521. }
  1522. static int ssl3_get_client_certificate(SSL *s)
  1523. {
  1524. int i,ok,al,ret= -1;
  1525. X509 *x=NULL;
  1526. unsigned long l,nc,llen,n;
  1527. unsigned char *p,*d,*q;
  1528. STACK_OF(X509) *sk=NULL;
  1529. n=ssl3_get_message(s,
  1530. SSL3_ST_SR_CERT_A,
  1531. SSL3_ST_SR_CERT_B,
  1532. -1,
  1533. #if defined(MSDOS) && !defined(WIN32)
  1534. 1024*30, /* 30k max cert list :-) */
  1535. #else
  1536. 1024*100, /* 100k max cert list :-) */
  1537. #endif
  1538. &ok);
  1539. if (!ok) return((int)n);
  1540. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  1541. {
  1542. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  1543. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1544. {
  1545. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1546. al=SSL_AD_HANDSHAKE_FAILURE;
  1547. goto f_err;
  1548. }
  1549. /* If tls asked for a client cert, the client must return a 0 list */
  1550. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  1551. {
  1552. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  1553. al=SSL_AD_UNEXPECTED_MESSAGE;
  1554. goto f_err;
  1555. }
  1556. s->s3->tmp.reuse_message=1;
  1557. return(1);
  1558. }
  1559. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  1560. {
  1561. al=SSL_AD_UNEXPECTED_MESSAGE;
  1562. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  1563. goto f_err;
  1564. }
  1565. d=p=(unsigned char *)s->init_buf->data;
  1566. if ((sk=sk_X509_new_null()) == NULL)
  1567. {
  1568. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1569. goto err;
  1570. }
  1571. n2l3(p,llen);
  1572. if (llen+3 != n)
  1573. {
  1574. al=SSL_AD_DECODE_ERROR;
  1575. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  1576. goto f_err;
  1577. }
  1578. for (nc=0; nc<llen; )
  1579. {
  1580. n2l3(p,l);
  1581. if ((l+nc+3) > llen)
  1582. {
  1583. al=SSL_AD_DECODE_ERROR;
  1584. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1585. goto f_err;
  1586. }
  1587. q=p;
  1588. x=d2i_X509(NULL,&p,l);
  1589. if (x == NULL)
  1590. {
  1591. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  1592. goto err;
  1593. }
  1594. if (p != (q+l))
  1595. {
  1596. al=SSL_AD_DECODE_ERROR;
  1597. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1598. goto f_err;
  1599. }
  1600. if (!sk_X509_push(sk,x))
  1601. {
  1602. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1603. goto err;
  1604. }
  1605. x=NULL;
  1606. nc+=l+3;
  1607. }
  1608. if (sk_X509_num(sk) <= 0)
  1609. {
  1610. /* TLS does not mind 0 certs returned */
  1611. if (s->version == SSL3_VERSION)
  1612. {
  1613. al=SSL_AD_HANDSHAKE_FAILURE;
  1614. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  1615. goto f_err;
  1616. }
  1617. /* Fail for TLS only if we required a certificate */
  1618. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  1619. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1620. {
  1621. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1622. al=SSL_AD_HANDSHAKE_FAILURE;
  1623. goto f_err;
  1624. }
  1625. }
  1626. else
  1627. {
  1628. i=ssl_verify_cert_chain(s,sk);
  1629. if (!i)
  1630. {
  1631. al=ssl_verify_alarm_type(s->verify_result);
  1632. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  1633. goto f_err;
  1634. }
  1635. }
  1636. if (s->session->peer != NULL) /* This should not be needed */
  1637. X509_free(s->session->peer);
  1638. s->session->peer=sk_X509_shift(sk);
  1639. s->session->verify_result = s->verify_result;
  1640. /* With the current implementation, sess_cert will always be NULL
  1641. * when we arrive here. */
  1642. if (s->session->sess_cert == NULL)
  1643. {
  1644. s->session->sess_cert = ssl_sess_cert_new();
  1645. if (s->session->sess_cert == NULL)
  1646. {
  1647. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1648. goto err;
  1649. }
  1650. }
  1651. if (s->session->sess_cert->cert_chain != NULL)
  1652. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  1653. s->session->sess_cert->cert_chain=sk;
  1654. /* Inconsistency alert: cert_chain does *not* include the
  1655. * peer's own certificate, while we do include it in s3_clnt.c */
  1656. sk=NULL;
  1657. ret=1;
  1658. if (0)
  1659. {
  1660. f_err:
  1661. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1662. }
  1663. err:
  1664. if (x != NULL) X509_free(x);
  1665. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  1666. return(ret);
  1667. }
  1668. int ssl3_send_server_certificate(SSL *s)
  1669. {
  1670. unsigned long l;
  1671. X509 *x;
  1672. if (s->state == SSL3_ST_SW_CERT_A)
  1673. {
  1674. x=ssl_get_server_send_cert(s);
  1675. if (x == NULL)
  1676. {
  1677. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
  1678. return(0);
  1679. }
  1680. l=ssl3_output_cert_chain(s,x);
  1681. s->state=SSL3_ST_SW_CERT_B;
  1682. s->init_num=(int)l;
  1683. s->init_off=0;
  1684. }
  1685. /* SSL3_ST_SW_CERT_B */
  1686. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1687. }