s3_clnt.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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-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. #include <stdio.h>
  112. #include "ssl_locl.h"
  113. #include "kssl_lcl.h"
  114. #include <openssl/buffer.h>
  115. #include <openssl/rand.h>
  116. #include <openssl/objects.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/md5.h>
  119. #include <openssl/fips.h>
  120. static SSL_METHOD *ssl3_get_client_method(int ver);
  121. static int ssl3_client_hello(SSL *s);
  122. static int ssl3_get_server_hello(SSL *s);
  123. static int ssl3_get_certificate_request(SSL *s);
  124. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  125. static int ssl3_get_server_done(SSL *s);
  126. static int ssl3_send_client_verify(SSL *s);
  127. static int ssl3_send_client_certificate(SSL *s);
  128. static int ssl3_send_client_key_exchange(SSL *s);
  129. static int ssl3_get_key_exchange(SSL *s);
  130. static int ssl3_get_server_certificate(SSL *s);
  131. static int ssl3_check_cert_and_algorithm(SSL *s);
  132. static SSL_METHOD *ssl3_get_client_method(int ver)
  133. {
  134. if (ver == SSL3_VERSION)
  135. return(SSLv3_client_method());
  136. else
  137. return(NULL);
  138. }
  139. SSL_METHOD *SSLv3_client_method(void)
  140. {
  141. static int init=1;
  142. static SSL_METHOD SSLv3_client_data;
  143. if (init)
  144. {
  145. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  146. if (init)
  147. {
  148. memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
  149. sizeof(SSL_METHOD));
  150. SSLv3_client_data.ssl_connect=ssl3_connect;
  151. SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
  152. init=0;
  153. }
  154. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  155. }
  156. return(&SSLv3_client_data);
  157. }
  158. int ssl3_connect(SSL *s)
  159. {
  160. BUF_MEM *buf=NULL;
  161. unsigned long Time=time(NULL),l;
  162. long num1;
  163. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  164. int ret= -1;
  165. int new_state,state,skip=0;;
  166. RAND_add(&Time,sizeof(Time),0);
  167. ERR_clear_error();
  168. clear_sys_error();
  169. if (s->info_callback != NULL)
  170. cb=s->info_callback;
  171. else if (s->ctx->info_callback != NULL)
  172. cb=s->ctx->info_callback;
  173. s->in_handshake++;
  174. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  175. for (;;)
  176. {
  177. state=s->state;
  178. switch(s->state)
  179. {
  180. case SSL_ST_RENEGOTIATE:
  181. s->new_session=1;
  182. s->state=SSL_ST_CONNECT;
  183. s->ctx->stats.sess_connect_renegotiate++;
  184. /* break */
  185. case SSL_ST_BEFORE:
  186. case SSL_ST_CONNECT:
  187. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  188. case SSL_ST_OK|SSL_ST_CONNECT:
  189. s->server=0;
  190. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  191. if ((s->version & 0xff00 ) != 0x0300)
  192. {
  193. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  194. ret = -1;
  195. goto end;
  196. }
  197. /* s->version=SSL3_VERSION; */
  198. s->type=SSL_ST_CONNECT;
  199. if (s->init_buf == NULL)
  200. {
  201. if ((buf=BUF_MEM_new()) == NULL)
  202. {
  203. ret= -1;
  204. goto end;
  205. }
  206. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  207. {
  208. ret= -1;
  209. goto end;
  210. }
  211. s->init_buf=buf;
  212. buf=NULL;
  213. }
  214. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  215. /* setup buffing BIO */
  216. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  217. /* don't push the buffering BIO quite yet */
  218. ssl3_init_finished_mac(s);
  219. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  220. s->ctx->stats.sess_connect++;
  221. s->init_num=0;
  222. break;
  223. case SSL3_ST_CW_CLNT_HELLO_A:
  224. case SSL3_ST_CW_CLNT_HELLO_B:
  225. s->shutdown=0;
  226. ret=ssl3_client_hello(s);
  227. if (ret <= 0) goto end;
  228. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  229. s->init_num=0;
  230. /* turn on buffering for the next lot of output */
  231. if (s->bbio != s->wbio)
  232. s->wbio=BIO_push(s->bbio,s->wbio);
  233. break;
  234. case SSL3_ST_CR_SRVR_HELLO_A:
  235. case SSL3_ST_CR_SRVR_HELLO_B:
  236. ret=ssl3_get_server_hello(s);
  237. if (ret <= 0) goto end;
  238. if (s->hit)
  239. s->state=SSL3_ST_CR_FINISHED_A;
  240. else
  241. s->state=SSL3_ST_CR_CERT_A;
  242. s->init_num=0;
  243. break;
  244. case SSL3_ST_CR_CERT_A:
  245. case SSL3_ST_CR_CERT_B:
  246. /* Check if it is anon DH */
  247. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  248. {
  249. ret=ssl3_get_server_certificate(s);
  250. if (ret <= 0) goto end;
  251. }
  252. else
  253. skip=1;
  254. s->state=SSL3_ST_CR_KEY_EXCH_A;
  255. s->init_num=0;
  256. break;
  257. case SSL3_ST_CR_KEY_EXCH_A:
  258. case SSL3_ST_CR_KEY_EXCH_B:
  259. ret=ssl3_get_key_exchange(s);
  260. if (ret <= 0) goto end;
  261. s->state=SSL3_ST_CR_CERT_REQ_A;
  262. s->init_num=0;
  263. /* at this point we check that we have the
  264. * required stuff from the server */
  265. if (!ssl3_check_cert_and_algorithm(s))
  266. {
  267. ret= -1;
  268. goto end;
  269. }
  270. break;
  271. case SSL3_ST_CR_CERT_REQ_A:
  272. case SSL3_ST_CR_CERT_REQ_B:
  273. ret=ssl3_get_certificate_request(s);
  274. if (ret <= 0) goto end;
  275. s->state=SSL3_ST_CR_SRVR_DONE_A;
  276. s->init_num=0;
  277. break;
  278. case SSL3_ST_CR_SRVR_DONE_A:
  279. case SSL3_ST_CR_SRVR_DONE_B:
  280. ret=ssl3_get_server_done(s);
  281. if (ret <= 0) goto end;
  282. if (s->s3->tmp.cert_req)
  283. s->state=SSL3_ST_CW_CERT_A;
  284. else
  285. s->state=SSL3_ST_CW_KEY_EXCH_A;
  286. s->init_num=0;
  287. break;
  288. case SSL3_ST_CW_CERT_A:
  289. case SSL3_ST_CW_CERT_B:
  290. case SSL3_ST_CW_CERT_C:
  291. case SSL3_ST_CW_CERT_D:
  292. ret=ssl3_send_client_certificate(s);
  293. if (ret <= 0) goto end;
  294. s->state=SSL3_ST_CW_KEY_EXCH_A;
  295. s->init_num=0;
  296. break;
  297. case SSL3_ST_CW_KEY_EXCH_A:
  298. case SSL3_ST_CW_KEY_EXCH_B:
  299. ret=ssl3_send_client_key_exchange(s);
  300. if (ret <= 0) goto end;
  301. l=s->s3->tmp.new_cipher->algorithms;
  302. /* EAY EAY EAY need to check for DH fix cert
  303. * sent back */
  304. /* For TLS, cert_req is set to 2, so a cert chain
  305. * of nothing is sent, but no verify packet is sent */
  306. if (s->s3->tmp.cert_req == 1)
  307. {
  308. s->state=SSL3_ST_CW_CERT_VRFY_A;
  309. }
  310. else
  311. {
  312. s->state=SSL3_ST_CW_CHANGE_A;
  313. s->s3->change_cipher_spec=0;
  314. }
  315. s->init_num=0;
  316. break;
  317. case SSL3_ST_CW_CERT_VRFY_A:
  318. case SSL3_ST_CW_CERT_VRFY_B:
  319. ret=ssl3_send_client_verify(s);
  320. if (ret <= 0) goto end;
  321. s->state=SSL3_ST_CW_CHANGE_A;
  322. s->init_num=0;
  323. s->s3->change_cipher_spec=0;
  324. break;
  325. case SSL3_ST_CW_CHANGE_A:
  326. case SSL3_ST_CW_CHANGE_B:
  327. ret=ssl3_send_change_cipher_spec(s,
  328. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  329. if (ret <= 0) goto end;
  330. s->state=SSL3_ST_CW_FINISHED_A;
  331. s->init_num=0;
  332. s->session->cipher=s->s3->tmp.new_cipher;
  333. if (s->s3->tmp.new_compression == NULL)
  334. s->session->compress_meth=0;
  335. else
  336. s->session->compress_meth=
  337. s->s3->tmp.new_compression->id;
  338. if (!s->method->ssl3_enc->setup_key_block(s))
  339. {
  340. ret= -1;
  341. goto end;
  342. }
  343. if (!s->method->ssl3_enc->change_cipher_state(s,
  344. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  345. {
  346. ret= -1;
  347. goto end;
  348. }
  349. break;
  350. case SSL3_ST_CW_FINISHED_A:
  351. case SSL3_ST_CW_FINISHED_B:
  352. ret=ssl3_send_finished(s,
  353. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  354. s->method->ssl3_enc->client_finished_label,
  355. s->method->ssl3_enc->client_finished_label_len);
  356. if (ret <= 0) goto end;
  357. s->state=SSL3_ST_CW_FLUSH;
  358. /* clear flags */
  359. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  360. if (s->hit)
  361. {
  362. s->s3->tmp.next_state=SSL_ST_OK;
  363. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  364. {
  365. s->state=SSL_ST_OK;
  366. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  367. s->s3->delay_buf_pop_ret=0;
  368. }
  369. }
  370. else
  371. {
  372. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  373. }
  374. s->init_num=0;
  375. break;
  376. case SSL3_ST_CR_FINISHED_A:
  377. case SSL3_ST_CR_FINISHED_B:
  378. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  379. SSL3_ST_CR_FINISHED_B);
  380. if (ret <= 0) goto end;
  381. if (s->hit)
  382. s->state=SSL3_ST_CW_CHANGE_A;
  383. else
  384. s->state=SSL_ST_OK;
  385. s->init_num=0;
  386. break;
  387. case SSL3_ST_CW_FLUSH:
  388. /* number of bytes to be flushed */
  389. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  390. if (num1 > 0)
  391. {
  392. s->rwstate=SSL_WRITING;
  393. num1=BIO_flush(s->wbio);
  394. if (num1 <= 0) { ret= -1; goto end; }
  395. s->rwstate=SSL_NOTHING;
  396. }
  397. s->state=s->s3->tmp.next_state;
  398. break;
  399. case SSL_ST_OK:
  400. /* clean a few things up */
  401. ssl3_cleanup_key_block(s);
  402. if (s->init_buf != NULL)
  403. {
  404. BUF_MEM_free(s->init_buf);
  405. s->init_buf=NULL;
  406. }
  407. /* If we are not 'joining' the last two packets,
  408. * remove the buffering now */
  409. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  410. ssl_free_wbio_buffer(s);
  411. /* else do it later in ssl3_write */
  412. s->init_num=0;
  413. s->new_session=0;
  414. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  415. if (s->hit) s->ctx->stats.sess_hit++;
  416. ret=1;
  417. /* s->server=0; */
  418. s->handshake_func=ssl3_connect;
  419. s->ctx->stats.sess_connect_good++;
  420. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  421. goto end;
  422. /* break; */
  423. default:
  424. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  425. ret= -1;
  426. goto end;
  427. /* break; */
  428. }
  429. /* did we do anything */
  430. if (!s->s3->tmp.reuse_message && !skip)
  431. {
  432. if (s->debug)
  433. {
  434. if ((ret=BIO_flush(s->wbio)) <= 0)
  435. goto end;
  436. }
  437. if ((cb != NULL) && (s->state != state))
  438. {
  439. new_state=s->state;
  440. s->state=state;
  441. cb(s,SSL_CB_CONNECT_LOOP,1);
  442. s->state=new_state;
  443. }
  444. }
  445. skip=0;
  446. }
  447. end:
  448. s->in_handshake--;
  449. if (buf != NULL)
  450. BUF_MEM_free(buf);
  451. if (cb != NULL)
  452. cb(s,SSL_CB_CONNECT_EXIT,ret);
  453. return(ret);
  454. }
  455. static int ssl3_client_hello(SSL *s)
  456. {
  457. unsigned char *buf;
  458. unsigned char *p,*d;
  459. int i,j;
  460. unsigned long Time,l;
  461. SSL_COMP *comp;
  462. buf=(unsigned char *)s->init_buf->data;
  463. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  464. {
  465. if ((s->session == NULL) ||
  466. (s->session->ssl_version != s->version) ||
  467. (s->session->not_resumable))
  468. {
  469. if (!ssl_get_new_session(s,0))
  470. goto err;
  471. }
  472. /* else use the pre-loaded session */
  473. p=s->s3->client_random;
  474. Time=time(NULL); /* Time */
  475. l2n(Time,p);
  476. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  477. /* Do the message type and length last */
  478. d=p= &(buf[4]);
  479. *(p++)=s->version>>8;
  480. *(p++)=s->version&0xff;
  481. s->client_version=s->version;
  482. /* Random stuff */
  483. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  484. p+=SSL3_RANDOM_SIZE;
  485. /* Session ID */
  486. if (s->new_session)
  487. i=0;
  488. else
  489. i=s->session->session_id_length;
  490. *(p++)=i;
  491. if (i != 0)
  492. {
  493. if (i > sizeof s->session->session_id)
  494. {
  495. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  496. goto err;
  497. }
  498. memcpy(p,s->session->session_id,i);
  499. p+=i;
  500. }
  501. /* Ciphers supported */
  502. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
  503. if (i == 0)
  504. {
  505. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  506. goto err;
  507. }
  508. s2n(i,p);
  509. p+=i;
  510. /* COMPRESSION */
  511. if (s->ctx->comp_methods == NULL)
  512. j=0;
  513. else
  514. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  515. *(p++)=1+j;
  516. for (i=0; i<j; i++)
  517. {
  518. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  519. *(p++)=comp->id;
  520. }
  521. *(p++)=0; /* Add the NULL method */
  522. l=(p-d);
  523. d=buf;
  524. *(d++)=SSL3_MT_CLIENT_HELLO;
  525. l2n3(l,d);
  526. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  527. /* number of bytes to write */
  528. s->init_num=p-buf;
  529. s->init_off=0;
  530. }
  531. /* SSL3_ST_CW_CLNT_HELLO_B */
  532. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  533. err:
  534. return(-1);
  535. }
  536. static int ssl3_get_server_hello(SSL *s)
  537. {
  538. STACK_OF(SSL_CIPHER) *sk;
  539. SSL_CIPHER *c;
  540. unsigned char *p,*d;
  541. int i,al,ok;
  542. unsigned int j;
  543. long n;
  544. SSL_COMP *comp;
  545. n=ssl3_get_message(s,
  546. SSL3_ST_CR_SRVR_HELLO_A,
  547. SSL3_ST_CR_SRVR_HELLO_B,
  548. SSL3_MT_SERVER_HELLO,
  549. 300, /* ?? */
  550. &ok);
  551. if (!ok) return((int)n);
  552. d=p=(unsigned char *)s->init_msg;
  553. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  554. {
  555. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  556. s->version=(s->version&0xff00)|p[1];
  557. al=SSL_AD_PROTOCOL_VERSION;
  558. goto f_err;
  559. }
  560. p+=2;
  561. /* load the server hello data */
  562. /* load the server random */
  563. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  564. p+=SSL3_RANDOM_SIZE;
  565. /* get the session-id */
  566. j= *(p++);
  567. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  568. {
  569. al=SSL_AD_ILLEGAL_PARAMETER;
  570. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  571. goto f_err;
  572. }
  573. if (j != 0 && j == s->session->session_id_length
  574. && memcmp(p,s->session->session_id,j) == 0)
  575. {
  576. if(s->sid_ctx_length != s->session->sid_ctx_length
  577. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  578. {
  579. /* actually a client application bug */
  580. al=SSL_AD_ILLEGAL_PARAMETER;
  581. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  582. goto f_err;
  583. }
  584. s->hit=1;
  585. }
  586. else /* a miss or crap from the other end */
  587. {
  588. /* If we were trying for session-id reuse, make a new
  589. * SSL_SESSION so we don't stuff up other people */
  590. s->hit=0;
  591. if (s->session->session_id_length > 0)
  592. {
  593. if (!ssl_get_new_session(s,0))
  594. {
  595. al=SSL_AD_INTERNAL_ERROR;
  596. goto f_err;
  597. }
  598. }
  599. s->session->session_id_length=j;
  600. memcpy(s->session->session_id,p,j); /* j could be 0 */
  601. }
  602. p+=j;
  603. c=ssl_get_cipher_by_char(s,p);
  604. if (c == NULL)
  605. {
  606. /* unknown cipher */
  607. al=SSL_AD_ILLEGAL_PARAMETER;
  608. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  609. goto f_err;
  610. }
  611. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  612. sk=ssl_get_ciphers_by_id(s);
  613. i=sk_SSL_CIPHER_find(sk,c);
  614. if (i < 0)
  615. {
  616. /* we did not say we would use this cipher */
  617. al=SSL_AD_ILLEGAL_PARAMETER;
  618. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  619. goto f_err;
  620. }
  621. /* Depending on the session caching (internal/external), the cipher
  622. and/or cipher_id values may not be set. Make sure that
  623. cipher_id is set and use it for comparison. */
  624. if (s->session->cipher)
  625. s->session->cipher_id = s->session->cipher->id;
  626. if (s->hit && (s->session->cipher_id != c->id))
  627. {
  628. if (!(s->options &
  629. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  630. {
  631. al=SSL_AD_ILLEGAL_PARAMETER;
  632. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  633. goto f_err;
  634. }
  635. }
  636. s->s3->tmp.new_cipher=c;
  637. /* lets get the compression algorithm */
  638. /* COMPRESSION */
  639. j= *(p++);
  640. if (j == 0)
  641. comp=NULL;
  642. else
  643. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  644. if ((j != 0) && (comp == NULL))
  645. {
  646. al=SSL_AD_ILLEGAL_PARAMETER;
  647. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  648. goto f_err;
  649. }
  650. else
  651. {
  652. s->s3->tmp.new_compression=comp;
  653. }
  654. if (p != (d+n))
  655. {
  656. /* wrong packet length */
  657. al=SSL_AD_DECODE_ERROR;
  658. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  659. goto err;
  660. }
  661. return(1);
  662. f_err:
  663. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  664. err:
  665. return(-1);
  666. }
  667. static int ssl3_get_server_certificate(SSL *s)
  668. {
  669. int al,i,ok,ret= -1;
  670. unsigned long n,nc,llen,l;
  671. X509 *x=NULL;
  672. unsigned char *p,*d,*q;
  673. STACK_OF(X509) *sk=NULL;
  674. SESS_CERT *sc;
  675. EVP_PKEY *pkey=NULL;
  676. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  677. n=ssl3_get_message(s,
  678. SSL3_ST_CR_CERT_A,
  679. SSL3_ST_CR_CERT_B,
  680. -1,
  681. s->max_cert_list,
  682. &ok);
  683. if (!ok) return((int)n);
  684. if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
  685. {
  686. s->s3->tmp.reuse_message=1;
  687. return(1);
  688. }
  689. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  690. {
  691. al=SSL_AD_UNEXPECTED_MESSAGE;
  692. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  693. goto f_err;
  694. }
  695. d=p=(unsigned char *)s->init_msg;
  696. if ((sk=sk_X509_new_null()) == NULL)
  697. {
  698. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  699. goto err;
  700. }
  701. n2l3(p,llen);
  702. if (llen+3 != n)
  703. {
  704. al=SSL_AD_DECODE_ERROR;
  705. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  706. goto f_err;
  707. }
  708. for (nc=0; nc<llen; )
  709. {
  710. n2l3(p,l);
  711. if ((l+nc+3) > llen)
  712. {
  713. al=SSL_AD_DECODE_ERROR;
  714. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  715. goto f_err;
  716. }
  717. q=p;
  718. x=d2i_X509(NULL,&q,l);
  719. if (x == NULL)
  720. {
  721. al=SSL_AD_BAD_CERTIFICATE;
  722. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  723. goto f_err;
  724. }
  725. if (q != (p+l))
  726. {
  727. al=SSL_AD_DECODE_ERROR;
  728. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  729. goto f_err;
  730. }
  731. if (!sk_X509_push(sk,x))
  732. {
  733. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  734. goto err;
  735. }
  736. x=NULL;
  737. nc+=l+3;
  738. p=q;
  739. }
  740. i=ssl_verify_cert_chain(s,sk);
  741. if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
  742. #ifndef OPENSSL_NO_KRB5
  743. && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  744. != (SSL_aKRB5|SSL_kKRB5)
  745. #endif /* OPENSSL_NO_KRB5 */
  746. )
  747. {
  748. al=ssl_verify_alarm_type(s->verify_result);
  749. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  750. goto f_err;
  751. }
  752. ERR_clear_error(); /* but we keep s->verify_result */
  753. sc=ssl_sess_cert_new();
  754. if (sc == NULL) goto err;
  755. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  756. s->session->sess_cert=sc;
  757. sc->cert_chain=sk;
  758. /* Inconsistency alert: cert_chain does include the peer's
  759. * certificate, which we don't include in s3_srvr.c */
  760. x=sk_X509_value(sk,0);
  761. sk=NULL;
  762. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  763. pkey=X509_get_pubkey(x);
  764. /* VRS: allow null cert if auth == KRB5 */
  765. need_cert = ((s->s3->tmp.new_cipher->algorithms
  766. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  767. == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
  768. #ifdef KSSL_DEBUG
  769. printf("pkey,x = %p, %p\n", pkey,x);
  770. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  771. printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
  772. s->s3->tmp.new_cipher->algorithms, need_cert);
  773. #endif /* KSSL_DEBUG */
  774. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  775. {
  776. x=NULL;
  777. al=SSL3_AL_FATAL;
  778. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  779. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  780. goto f_err;
  781. }
  782. i=ssl_cert_type(x,pkey);
  783. if (need_cert && i < 0)
  784. {
  785. x=NULL;
  786. al=SSL3_AL_FATAL;
  787. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  788. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  789. goto f_err;
  790. }
  791. if (need_cert)
  792. {
  793. sc->peer_cert_type=i;
  794. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  795. /* Why would the following ever happen?
  796. * We just created sc a couple of lines ago. */
  797. if (sc->peer_pkeys[i].x509 != NULL)
  798. X509_free(sc->peer_pkeys[i].x509);
  799. sc->peer_pkeys[i].x509=x;
  800. sc->peer_key= &(sc->peer_pkeys[i]);
  801. if (s->session->peer != NULL)
  802. X509_free(s->session->peer);
  803. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  804. s->session->peer=x;
  805. }
  806. else
  807. {
  808. sc->peer_cert_type=i;
  809. sc->peer_key= NULL;
  810. if (s->session->peer != NULL)
  811. X509_free(s->session->peer);
  812. s->session->peer=NULL;
  813. }
  814. s->session->verify_result = s->verify_result;
  815. x=NULL;
  816. ret=1;
  817. if (0)
  818. {
  819. f_err:
  820. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  821. }
  822. err:
  823. EVP_PKEY_free(pkey);
  824. X509_free(x);
  825. sk_X509_pop_free(sk,X509_free);
  826. return(ret);
  827. }
  828. static int ssl3_get_key_exchange(SSL *s)
  829. {
  830. #ifndef OPENSSL_NO_RSA
  831. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  832. #endif
  833. EVP_MD_CTX md_ctx;
  834. unsigned char *param,*p;
  835. int al,i,j,param_len,ok;
  836. long n,alg;
  837. EVP_PKEY *pkey=NULL;
  838. #ifndef OPENSSL_NO_RSA
  839. RSA *rsa=NULL;
  840. #endif
  841. #ifndef OPENSSL_NO_DH
  842. DH *dh=NULL;
  843. #endif
  844. /* use same message size as in ssl3_get_certificate_request()
  845. * as ServerKeyExchange message may be skipped */
  846. n=ssl3_get_message(s,
  847. SSL3_ST_CR_KEY_EXCH_A,
  848. SSL3_ST_CR_KEY_EXCH_B,
  849. -1,
  850. s->max_cert_list,
  851. &ok);
  852. if (!ok) return((int)n);
  853. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  854. {
  855. s->s3->tmp.reuse_message=1;
  856. return(1);
  857. }
  858. param=p=(unsigned char *)s->init_msg;
  859. if (s->session->sess_cert != NULL)
  860. {
  861. #ifndef OPENSSL_NO_RSA
  862. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  863. {
  864. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  865. s->session->sess_cert->peer_rsa_tmp=NULL;
  866. }
  867. #endif
  868. #ifndef OPENSSL_NO_DH
  869. if (s->session->sess_cert->peer_dh_tmp)
  870. {
  871. DH_free(s->session->sess_cert->peer_dh_tmp);
  872. s->session->sess_cert->peer_dh_tmp=NULL;
  873. }
  874. #endif
  875. }
  876. else
  877. {
  878. s->session->sess_cert=ssl_sess_cert_new();
  879. }
  880. param_len=0;
  881. alg=s->s3->tmp.new_cipher->algorithms;
  882. EVP_MD_CTX_init(&md_ctx);
  883. #ifndef OPENSSL_NO_RSA
  884. if (alg & SSL_kRSA)
  885. {
  886. if ((rsa=RSA_new()) == NULL)
  887. {
  888. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  889. goto err;
  890. }
  891. n2s(p,i);
  892. param_len=i+2;
  893. if (param_len > n)
  894. {
  895. al=SSL_AD_DECODE_ERROR;
  896. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  897. goto f_err;
  898. }
  899. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  900. {
  901. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  902. goto err;
  903. }
  904. p+=i;
  905. n2s(p,i);
  906. param_len+=i+2;
  907. if (param_len > n)
  908. {
  909. al=SSL_AD_DECODE_ERROR;
  910. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  911. goto f_err;
  912. }
  913. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  914. {
  915. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  916. goto err;
  917. }
  918. p+=i;
  919. n-=param_len;
  920. /* this should be because we are using an export cipher */
  921. if (alg & SSL_aRSA)
  922. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  923. else
  924. {
  925. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  926. goto err;
  927. }
  928. s->session->sess_cert->peer_rsa_tmp=rsa;
  929. rsa=NULL;
  930. }
  931. #else /* OPENSSL_NO_RSA */
  932. if (0)
  933. ;
  934. #endif
  935. #ifndef OPENSSL_NO_DH
  936. else if (alg & SSL_kEDH)
  937. {
  938. if ((dh=DH_new()) == NULL)
  939. {
  940. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  941. goto err;
  942. }
  943. n2s(p,i);
  944. param_len=i+2;
  945. if (param_len > n)
  946. {
  947. al=SSL_AD_DECODE_ERROR;
  948. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  949. goto f_err;
  950. }
  951. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  952. {
  953. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  954. goto err;
  955. }
  956. p+=i;
  957. n2s(p,i);
  958. param_len+=i+2;
  959. if (param_len > n)
  960. {
  961. al=SSL_AD_DECODE_ERROR;
  962. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  963. goto f_err;
  964. }
  965. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  966. {
  967. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  968. goto err;
  969. }
  970. p+=i;
  971. n2s(p,i);
  972. param_len+=i+2;
  973. if (param_len > n)
  974. {
  975. al=SSL_AD_DECODE_ERROR;
  976. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  977. goto f_err;
  978. }
  979. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  980. {
  981. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  982. goto err;
  983. }
  984. p+=i;
  985. n-=param_len;
  986. #ifndef OPENSSL_NO_RSA
  987. if (alg & SSL_aRSA)
  988. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  989. #else
  990. if (0)
  991. ;
  992. #endif
  993. #ifndef OPENSSL_NO_DSA
  994. else if (alg & SSL_aDSS)
  995. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  996. #endif
  997. /* else anonymous DH, so no certificate or pkey. */
  998. s->session->sess_cert->peer_dh_tmp=dh;
  999. dh=NULL;
  1000. }
  1001. else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
  1002. {
  1003. al=SSL_AD_ILLEGAL_PARAMETER;
  1004. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1005. goto f_err;
  1006. }
  1007. #endif /* !OPENSSL_NO_DH */
  1008. if (alg & SSL_aFZA)
  1009. {
  1010. al=SSL_AD_HANDSHAKE_FAILURE;
  1011. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1012. goto f_err;
  1013. }
  1014. /* p points to the next byte, there are 'n' bytes left */
  1015. /* if it was signed, check the signature */
  1016. if (pkey != NULL)
  1017. {
  1018. n2s(p,i);
  1019. n-=2;
  1020. j=EVP_PKEY_size(pkey);
  1021. if ((i != n) || (n > j) || (n <= 0))
  1022. {
  1023. /* wrong packet length */
  1024. al=SSL_AD_DECODE_ERROR;
  1025. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1026. goto f_err;
  1027. }
  1028. #ifndef OPENSSL_NO_RSA
  1029. if (pkey->type == EVP_PKEY_RSA)
  1030. {
  1031. int num;
  1032. j=0;
  1033. q=md_buf;
  1034. for (num=2; num > 0; num--)
  1035. {
  1036. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1037. ?s->ctx->md5:s->ctx->sha1, NULL);
  1038. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1039. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1040. EVP_DigestUpdate(&md_ctx,param,param_len);
  1041. #ifdef OPENSSL_FIPS
  1042. if(s->version == TLS1_VERSION && num == 2)
  1043. FIPS_allow_md5(1);
  1044. #endif
  1045. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1046. #ifdef OPENSSL_FIPS
  1047. if(s->version == TLS1_VERSION && num == 2)
  1048. FIPS_allow_md5(1);
  1049. #endif
  1050. q+=i;
  1051. j+=i;
  1052. }
  1053. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1054. pkey->pkey.rsa);
  1055. if (i < 0)
  1056. {
  1057. al=SSL_AD_DECRYPT_ERROR;
  1058. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1059. goto f_err;
  1060. }
  1061. if (i == 0)
  1062. {
  1063. /* bad signature */
  1064. al=SSL_AD_DECRYPT_ERROR;
  1065. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1066. goto f_err;
  1067. }
  1068. }
  1069. else
  1070. #endif
  1071. #ifndef OPENSSL_NO_DSA
  1072. if (pkey->type == EVP_PKEY_DSA)
  1073. {
  1074. /* lets do DSS */
  1075. EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
  1076. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1077. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1078. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1079. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1080. {
  1081. /* bad signature */
  1082. al=SSL_AD_DECRYPT_ERROR;
  1083. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1084. goto f_err;
  1085. }
  1086. }
  1087. else
  1088. #endif
  1089. {
  1090. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1091. goto err;
  1092. }
  1093. }
  1094. else
  1095. {
  1096. /* still data left over */
  1097. if (!(alg & SSL_aNULL))
  1098. {
  1099. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1100. goto err;
  1101. }
  1102. if (n != 0)
  1103. {
  1104. al=SSL_AD_DECODE_ERROR;
  1105. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1106. goto f_err;
  1107. }
  1108. }
  1109. EVP_PKEY_free(pkey);
  1110. EVP_MD_CTX_cleanup(&md_ctx);
  1111. return(1);
  1112. f_err:
  1113. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1114. err:
  1115. EVP_PKEY_free(pkey);
  1116. #ifndef OPENSSL_NO_RSA
  1117. if (rsa != NULL)
  1118. RSA_free(rsa);
  1119. #endif
  1120. #ifndef OPENSSL_NO_DH
  1121. if (dh != NULL)
  1122. DH_free(dh);
  1123. #endif
  1124. EVP_MD_CTX_cleanup(&md_ctx);
  1125. return(-1);
  1126. }
  1127. static int ssl3_get_certificate_request(SSL *s)
  1128. {
  1129. int ok,ret=0;
  1130. unsigned long n,nc,l;
  1131. unsigned int llen,ctype_num,i;
  1132. X509_NAME *xn=NULL;
  1133. unsigned char *p,*d,*q;
  1134. STACK_OF(X509_NAME) *ca_sk=NULL;
  1135. n=ssl3_get_message(s,
  1136. SSL3_ST_CR_CERT_REQ_A,
  1137. SSL3_ST_CR_CERT_REQ_B,
  1138. -1,
  1139. s->max_cert_list,
  1140. &ok);
  1141. if (!ok) return((int)n);
  1142. s->s3->tmp.cert_req=0;
  1143. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1144. {
  1145. s->s3->tmp.reuse_message=1;
  1146. return(1);
  1147. }
  1148. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1149. {
  1150. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1151. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1152. goto err;
  1153. }
  1154. /* TLS does not like anon-DH with client cert */
  1155. if (s->version > SSL3_VERSION)
  1156. {
  1157. l=s->s3->tmp.new_cipher->algorithms;
  1158. if (l & SSL_aNULL)
  1159. {
  1160. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1161. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1162. goto err;
  1163. }
  1164. }
  1165. d=p=(unsigned char *)s->init_msg;
  1166. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1167. {
  1168. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1169. goto err;
  1170. }
  1171. /* get the certificate types */
  1172. ctype_num= *(p++);
  1173. if (ctype_num > SSL3_CT_NUMBER)
  1174. ctype_num=SSL3_CT_NUMBER;
  1175. for (i=0; i<ctype_num; i++)
  1176. s->s3->tmp.ctype[i]= p[i];
  1177. p+=ctype_num;
  1178. /* get the CA RDNs */
  1179. n2s(p,llen);
  1180. #if 0
  1181. {
  1182. FILE *out;
  1183. out=fopen("/tmp/vsign.der","w");
  1184. fwrite(p,1,llen,out);
  1185. fclose(out);
  1186. }
  1187. #endif
  1188. if ((llen+ctype_num+2+1) != n)
  1189. {
  1190. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1191. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1192. goto err;
  1193. }
  1194. for (nc=0; nc<llen; )
  1195. {
  1196. n2s(p,l);
  1197. if ((l+nc+2) > llen)
  1198. {
  1199. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1200. goto cont; /* netscape bugs */
  1201. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1202. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1203. goto err;
  1204. }
  1205. q=p;
  1206. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1207. {
  1208. /* If netscape tolerance is on, ignore errors */
  1209. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1210. goto cont;
  1211. else
  1212. {
  1213. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1214. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1215. goto err;
  1216. }
  1217. }
  1218. if (q != (p+l))
  1219. {
  1220. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1221. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1222. goto err;
  1223. }
  1224. if (!sk_X509_NAME_push(ca_sk,xn))
  1225. {
  1226. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1227. goto err;
  1228. }
  1229. p+=l;
  1230. nc+=l+2;
  1231. }
  1232. if (0)
  1233. {
  1234. cont:
  1235. ERR_clear_error();
  1236. }
  1237. /* we should setup a certificate to return.... */
  1238. s->s3->tmp.cert_req=1;
  1239. s->s3->tmp.ctype_num=ctype_num;
  1240. if (s->s3->tmp.ca_names != NULL)
  1241. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1242. s->s3->tmp.ca_names=ca_sk;
  1243. ca_sk=NULL;
  1244. ret=1;
  1245. err:
  1246. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1247. return(ret);
  1248. }
  1249. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1250. {
  1251. return(X509_NAME_cmp(*a,*b));
  1252. }
  1253. static int ssl3_get_server_done(SSL *s)
  1254. {
  1255. int ok,ret=0;
  1256. long n;
  1257. n=ssl3_get_message(s,
  1258. SSL3_ST_CR_SRVR_DONE_A,
  1259. SSL3_ST_CR_SRVR_DONE_B,
  1260. SSL3_MT_SERVER_DONE,
  1261. 30, /* should be very small, like 0 :-) */
  1262. &ok);
  1263. if (!ok) return((int)n);
  1264. if (n > 0)
  1265. {
  1266. /* should contain no data */
  1267. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1268. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1269. return -1;
  1270. }
  1271. ret=1;
  1272. return(ret);
  1273. }
  1274. static int ssl3_send_client_key_exchange(SSL *s)
  1275. {
  1276. unsigned char *p,*d;
  1277. int n;
  1278. unsigned long l;
  1279. #ifndef OPENSSL_NO_RSA
  1280. unsigned char *q;
  1281. EVP_PKEY *pkey=NULL;
  1282. #endif
  1283. #ifndef OPENSSL_NO_KRB5
  1284. KSSL_ERR kssl_err;
  1285. #endif /* OPENSSL_NO_KRB5 */
  1286. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1287. {
  1288. d=(unsigned char *)s->init_buf->data;
  1289. p= &(d[4]);
  1290. l=s->s3->tmp.new_cipher->algorithms;
  1291. /* Fool emacs indentation */
  1292. if (0) {}
  1293. #ifndef OPENSSL_NO_RSA
  1294. else if (l & SSL_kRSA)
  1295. {
  1296. RSA *rsa;
  1297. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1298. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1299. rsa=s->session->sess_cert->peer_rsa_tmp;
  1300. else
  1301. {
  1302. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1303. if ((pkey == NULL) ||
  1304. (pkey->type != EVP_PKEY_RSA) ||
  1305. (pkey->pkey.rsa == NULL))
  1306. {
  1307. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1308. goto err;
  1309. }
  1310. rsa=pkey->pkey.rsa;
  1311. EVP_PKEY_free(pkey);
  1312. }
  1313. tmp_buf[0]=s->client_version>>8;
  1314. tmp_buf[1]=s->client_version&0xff;
  1315. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1316. goto err;
  1317. s->session->master_key_length=sizeof tmp_buf;
  1318. q=p;
  1319. /* Fix buf for TLS and beyond */
  1320. if (s->version > SSL3_VERSION)
  1321. p+=2;
  1322. n=RSA_public_encrypt(sizeof tmp_buf,
  1323. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1324. #ifdef PKCS1_CHECK
  1325. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1326. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1327. #endif
  1328. if (n <= 0)
  1329. {
  1330. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1331. goto err;
  1332. }
  1333. /* Fix buf for TLS and beyond */
  1334. if (s->version > SSL3_VERSION)
  1335. {
  1336. s2n(n,q);
  1337. n+=2;
  1338. }
  1339. s->session->master_key_length=
  1340. s->method->ssl3_enc->generate_master_secret(s,
  1341. s->session->master_key,
  1342. tmp_buf,sizeof tmp_buf);
  1343. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1344. }
  1345. #endif
  1346. #ifndef OPENSSL_NO_KRB5
  1347. else if (l & SSL_kKRB5)
  1348. {
  1349. krb5_error_code krb5rc;
  1350. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1351. /* krb5_data krb5_ap_req; */
  1352. krb5_data *enc_ticket;
  1353. krb5_data authenticator, *authp = NULL;
  1354. EVP_CIPHER_CTX ciph_ctx;
  1355. EVP_CIPHER *enc = NULL;
  1356. unsigned char iv[EVP_MAX_IV_LENGTH];
  1357. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1358. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  1359. + EVP_MAX_IV_LENGTH];
  1360. int padl, outl = sizeof(epms);
  1361. EVP_CIPHER_CTX_init(&ciph_ctx);
  1362. #ifdef KSSL_DEBUG
  1363. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1364. l, SSL_kKRB5);
  1365. #endif /* KSSL_DEBUG */
  1366. authp = NULL;
  1367. #ifdef KRB5SENDAUTH
  1368. if (KRB5SENDAUTH) authp = &authenticator;
  1369. #endif /* KRB5SENDAUTH */
  1370. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  1371. &kssl_err);
  1372. enc = kssl_map_enc(kssl_ctx->enctype);
  1373. if (enc == NULL)
  1374. goto err;
  1375. #ifdef KSSL_DEBUG
  1376. {
  1377. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1378. if (krb5rc && kssl_err.text)
  1379. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1380. }
  1381. #endif /* KSSL_DEBUG */
  1382. if (krb5rc)
  1383. {
  1384. ssl3_send_alert(s,SSL3_AL_FATAL,
  1385. SSL_AD_HANDSHAKE_FAILURE);
  1386. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1387. kssl_err.reason);
  1388. goto err;
  1389. }
  1390. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  1391. ** in place of RFC 2712 KerberosWrapper, as in:
  1392. **
  1393. ** Send ticket (copy to *p, set n = length)
  1394. ** n = krb5_ap_req.length;
  1395. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1396. ** if (krb5_ap_req.data)
  1397. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1398. **
  1399. ** Now using real RFC 2712 KerberosWrapper
  1400. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  1401. ** Note: 2712 "opaque" types are here replaced
  1402. ** with a 2-byte length followed by the value.
  1403. ** Example:
  1404. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  1405. ** Where "xx xx" = length bytes. Shown here with
  1406. ** optional authenticator omitted.
  1407. */
  1408. /* KerberosWrapper.Ticket */
  1409. s2n(enc_ticket->length,p);
  1410. memcpy(p, enc_ticket->data, enc_ticket->length);
  1411. p+= enc_ticket->length;
  1412. n = enc_ticket->length + 2;
  1413. /* KerberosWrapper.Authenticator */
  1414. if (authp && authp->length)
  1415. {
  1416. s2n(authp->length,p);
  1417. memcpy(p, authp->data, authp->length);
  1418. p+= authp->length;
  1419. n+= authp->length + 2;
  1420. free(authp->data);
  1421. authp->data = NULL;
  1422. authp->length = 0;
  1423. }
  1424. else
  1425. {
  1426. s2n(0,p);/* null authenticator length */
  1427. n+=2;
  1428. }
  1429. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  1430. goto err;
  1431. /* 20010420 VRS. Tried it this way; failed.
  1432. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1433. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1434. ** kssl_ctx->length);
  1435. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1436. */
  1437. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1438. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  1439. kssl_ctx->key,iv);
  1440. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  1441. sizeof tmp_buf);
  1442. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  1443. outl += padl;
  1444. if (outl > sizeof epms)
  1445. {
  1446. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1447. goto err;
  1448. }
  1449. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1450. /* KerberosWrapper.EncryptedPreMasterSecret */
  1451. s2n(outl,p);
  1452. memcpy(p, epms, outl);
  1453. p+=outl;
  1454. n+=outl + 2;
  1455. s->session->master_key_length=
  1456. s->method->ssl3_enc->generate_master_secret(s,
  1457. s->session->master_key,
  1458. tmp_buf, sizeof tmp_buf);
  1459. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  1460. OPENSSL_cleanse(epms, outl);
  1461. }
  1462. #endif
  1463. #ifndef OPENSSL_NO_DH
  1464. else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1465. {
  1466. DH *dh_srvr,*dh_clnt;
  1467. if (s->session->sess_cert->peer_dh_tmp != NULL)
  1468. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  1469. else
  1470. {
  1471. /* we get them from the cert */
  1472. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1473. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  1474. goto err;
  1475. }
  1476. /* generate a new random key */
  1477. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  1478. {
  1479. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1480. goto err;
  1481. }
  1482. if (!DH_generate_key(dh_clnt))
  1483. {
  1484. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1485. goto err;
  1486. }
  1487. /* use the 'p' output buffer for the DH key, but
  1488. * make sure to clear it out afterwards */
  1489. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  1490. if (n <= 0)
  1491. {
  1492. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1493. goto err;
  1494. }
  1495. /* generate master key from the result */
  1496. s->session->master_key_length=
  1497. s->method->ssl3_enc->generate_master_secret(s,
  1498. s->session->master_key,p,n);
  1499. /* clean up */
  1500. memset(p,0,n);
  1501. /* send off the data */
  1502. n=BN_num_bytes(dh_clnt->pub_key);
  1503. s2n(n,p);
  1504. BN_bn2bin(dh_clnt->pub_key,p);
  1505. n+=2;
  1506. DH_free(dh_clnt);
  1507. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  1508. }
  1509. #endif
  1510. else
  1511. {
  1512. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1513. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1514. goto err;
  1515. }
  1516. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1517. l2n3(n,d);
  1518. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1519. /* number of bytes to write */
  1520. s->init_num=n+4;
  1521. s->init_off=0;
  1522. }
  1523. /* SSL3_ST_CW_KEY_EXCH_B */
  1524. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1525. err:
  1526. return(-1);
  1527. }
  1528. static int ssl3_send_client_verify(SSL *s)
  1529. {
  1530. unsigned char *p,*d;
  1531. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1532. EVP_PKEY *pkey;
  1533. #ifndef OPENSSL_NO_RSA
  1534. unsigned u=0;
  1535. #endif
  1536. unsigned long n;
  1537. #ifndef OPENSSL_NO_DSA
  1538. int j;
  1539. #endif
  1540. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1541. {
  1542. d=(unsigned char *)s->init_buf->data;
  1543. p= &(d[4]);
  1544. pkey=s->cert->key->privatekey;
  1545. s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
  1546. &(data[MD5_DIGEST_LENGTH]));
  1547. #ifndef OPENSSL_NO_RSA
  1548. if (pkey->type == EVP_PKEY_RSA)
  1549. {
  1550. s->method->ssl3_enc->cert_verify_mac(s,
  1551. &(s->s3->finish_dgst1),&(data[0]));
  1552. if (RSA_sign(NID_md5_sha1, data,
  1553. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1554. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1555. {
  1556. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1557. goto err;
  1558. }
  1559. s2n(u,p);
  1560. n=u+2;
  1561. }
  1562. else
  1563. #endif
  1564. #ifndef OPENSSL_NO_DSA
  1565. if (pkey->type == EVP_PKEY_DSA)
  1566. {
  1567. if (!DSA_sign(pkey->save_type,
  1568. &(data[MD5_DIGEST_LENGTH]),
  1569. SHA_DIGEST_LENGTH,&(p[2]),
  1570. (unsigned int *)&j,pkey->pkey.dsa))
  1571. {
  1572. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1573. goto err;
  1574. }
  1575. s2n(j,p);
  1576. n=j+2;
  1577. }
  1578. else
  1579. #endif
  1580. {
  1581. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  1582. goto err;
  1583. }
  1584. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  1585. l2n3(n,d);
  1586. s->state=SSL3_ST_CW_CERT_VRFY_B;
  1587. s->init_num=(int)n+4;
  1588. s->init_off=0;
  1589. }
  1590. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1591. err:
  1592. return(-1);
  1593. }
  1594. static int ssl3_send_client_certificate(SSL *s)
  1595. {
  1596. X509 *x509=NULL;
  1597. EVP_PKEY *pkey=NULL;
  1598. int i;
  1599. unsigned long l;
  1600. if (s->state == SSL3_ST_CW_CERT_A)
  1601. {
  1602. if ((s->cert == NULL) ||
  1603. (s->cert->key->x509 == NULL) ||
  1604. (s->cert->key->privatekey == NULL))
  1605. s->state=SSL3_ST_CW_CERT_B;
  1606. else
  1607. s->state=SSL3_ST_CW_CERT_C;
  1608. }
  1609. /* We need to get a client cert */
  1610. if (s->state == SSL3_ST_CW_CERT_B)
  1611. {
  1612. /* If we get an error, we need to
  1613. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1614. * We then get retied later */
  1615. i=0;
  1616. if (s->ctx->client_cert_cb != NULL)
  1617. i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
  1618. if (i < 0)
  1619. {
  1620. s->rwstate=SSL_X509_LOOKUP;
  1621. return(-1);
  1622. }
  1623. s->rwstate=SSL_NOTHING;
  1624. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1625. {
  1626. s->state=SSL3_ST_CW_CERT_B;
  1627. if ( !SSL_use_certificate(s,x509) ||
  1628. !SSL_use_PrivateKey(s,pkey))
  1629. i=0;
  1630. }
  1631. else if (i == 1)
  1632. {
  1633. i=0;
  1634. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1635. }
  1636. if (x509 != NULL) X509_free(x509);
  1637. if (pkey != NULL) EVP_PKEY_free(pkey);
  1638. if (i == 0)
  1639. {
  1640. if (s->version == SSL3_VERSION)
  1641. {
  1642. s->s3->tmp.cert_req=0;
  1643. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1644. return(1);
  1645. }
  1646. else
  1647. {
  1648. s->s3->tmp.cert_req=2;
  1649. }
  1650. }
  1651. /* Ok, we have a cert */
  1652. s->state=SSL3_ST_CW_CERT_C;
  1653. }
  1654. if (s->state == SSL3_ST_CW_CERT_C)
  1655. {
  1656. s->state=SSL3_ST_CW_CERT_D;
  1657. l=ssl3_output_cert_chain(s,
  1658. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1659. s->init_num=(int)l;
  1660. s->init_off=0;
  1661. }
  1662. /* SSL3_ST_CW_CERT_D */
  1663. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1664. }
  1665. #define has_bits(i,m) (((i)&(m)) == (m))
  1666. static int ssl3_check_cert_and_algorithm(SSL *s)
  1667. {
  1668. int i,idx;
  1669. long algs;
  1670. EVP_PKEY *pkey=NULL;
  1671. SESS_CERT *sc;
  1672. #ifndef OPENSSL_NO_RSA
  1673. RSA *rsa;
  1674. #endif
  1675. #ifndef OPENSSL_NO_DH
  1676. DH *dh;
  1677. #endif
  1678. sc=s->session->sess_cert;
  1679. if (sc == NULL)
  1680. {
  1681. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  1682. goto err;
  1683. }
  1684. algs=s->s3->tmp.new_cipher->algorithms;
  1685. /* we don't have a certificate */
  1686. if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
  1687. return(1);
  1688. #ifndef OPENSSL_NO_RSA
  1689. rsa=s->session->sess_cert->peer_rsa_tmp;
  1690. #endif
  1691. #ifndef OPENSSL_NO_DH
  1692. dh=s->session->sess_cert->peer_dh_tmp;
  1693. #endif
  1694. /* This is the passed certificate */
  1695. idx=sc->peer_cert_type;
  1696. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  1697. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  1698. EVP_PKEY_free(pkey);
  1699. /* Check that we have a certificate if we require one */
  1700. if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  1701. {
  1702. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  1703. goto f_err;
  1704. }
  1705. #ifndef OPENSSL_NO_DSA
  1706. else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  1707. {
  1708. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  1709. goto f_err;
  1710. }
  1711. #endif
  1712. #ifndef OPENSSL_NO_RSA
  1713. if ((algs & SSL_kRSA) &&
  1714. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  1715. {
  1716. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  1717. goto f_err;
  1718. }
  1719. #endif
  1720. #ifndef OPENSSL_NO_DH
  1721. if ((algs & SSL_kEDH) &&
  1722. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  1723. {
  1724. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  1725. goto f_err;
  1726. }
  1727. else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  1728. {
  1729. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  1730. goto f_err;
  1731. }
  1732. #ifndef OPENSSL_NO_DSA
  1733. else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  1734. {
  1735. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  1736. goto f_err;
  1737. }
  1738. #endif
  1739. #endif
  1740. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  1741. {
  1742. #ifndef OPENSSL_NO_RSA
  1743. if (algs & SSL_kRSA)
  1744. {
  1745. if (rsa == NULL
  1746. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  1747. {
  1748. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  1749. goto f_err;
  1750. }
  1751. }
  1752. else
  1753. #endif
  1754. #ifndef OPENSSL_NO_DH
  1755. if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1756. {
  1757. if (dh == NULL
  1758. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  1759. {
  1760. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  1761. goto f_err;
  1762. }
  1763. }
  1764. else
  1765. #endif
  1766. {
  1767. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1768. goto f_err;
  1769. }
  1770. }
  1771. return(1);
  1772. f_err:
  1773. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1774. err:
  1775. return(0);
  1776. }