d1_clnt.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /* ssl/d1_clnt.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  60. * All rights reserved.
  61. *
  62. * This package is an SSL implementation written
  63. * by Eric Young (eay@cryptsoft.com).
  64. * The implementation was written so as to conform with Netscapes SSL.
  65. *
  66. * This library is free for commercial and non-commercial use as long as
  67. * the following conditions are aheared to. The following conditions
  68. * apply to all code found in this distribution, be it the RC4, RSA,
  69. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  70. * included with this distribution is covered by the same copyright terms
  71. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  72. *
  73. * Copyright remains Eric Young's, and as such any Copyright notices in
  74. * the code are not to be removed.
  75. * If this package is used in a product, Eric Young should be given attribution
  76. * as the author of the parts of the library used.
  77. * This can be in the form of a textual message at program startup or
  78. * in documentation (online or textual) provided with the package.
  79. *
  80. * Redistribution and use in source and binary forms, with or without
  81. * modification, are permitted provided that the following conditions
  82. * are met:
  83. * 1. Redistributions of source code must retain the copyright
  84. * notice, this list of conditions and the following disclaimer.
  85. * 2. Redistributions in binary form must reproduce the above copyright
  86. * notice, this list of conditions and the following disclaimer in the
  87. * documentation and/or other materials provided with the distribution.
  88. * 3. All advertising materials mentioning features or use of this software
  89. * must display the following acknowledgement:
  90. * "This product includes cryptographic software written by
  91. * Eric Young (eay@cryptsoft.com)"
  92. * The word 'cryptographic' can be left out if the rouines from the library
  93. * being used are not cryptographic related :-).
  94. * 4. If you include any Windows specific code (or a derivative thereof) from
  95. * the apps directory (application code) you must include an acknowledgement:
  96. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  97. *
  98. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  99. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  100. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  101. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  102. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  103. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  104. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  105. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  106. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  107. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  108. * SUCH DAMAGE.
  109. *
  110. * The licence and distribution terms for any publically available version or
  111. * derivative of this code cannot be changed. i.e. this code cannot simply be
  112. * copied and put under another distribution licence
  113. * [including the GNU Public Licence.]
  114. */
  115. #include <stdio.h>
  116. #include "ssl_locl.h"
  117. #ifndef OPENSSL_NO_KRB5
  118. #include "kssl_lcl.h"
  119. #endif
  120. #include <openssl/buffer.h>
  121. #include <openssl/rand.h>
  122. #include <openssl/objects.h>
  123. #include <openssl/evp.h>
  124. #include <openssl/md5.h>
  125. #include <openssl/bn.h>
  126. #ifndef OPENSSL_NO_DH
  127. #include <openssl/dh.h>
  128. #endif
  129. static const SSL_METHOD *dtls1_get_client_method(int ver);
  130. static int dtls1_get_hello_verify(SSL *s);
  131. static const SSL_METHOD *dtls1_get_client_method(int ver)
  132. {
  133. if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
  134. return(DTLSv1_client_method());
  135. else
  136. return(NULL);
  137. }
  138. IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
  139. ssl_undefined_function,
  140. dtls1_connect,
  141. dtls1_get_client_method)
  142. int dtls1_connect(SSL *s)
  143. {
  144. BUF_MEM *buf=NULL;
  145. unsigned long Time=(unsigned long)time(NULL);
  146. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  147. int ret= -1;
  148. int new_state,state,skip=0;;
  149. RAND_add(&Time,sizeof(Time),0);
  150. ERR_clear_error();
  151. clear_sys_error();
  152. if (s->info_callback != NULL)
  153. cb=s->info_callback;
  154. else if (s->ctx->info_callback != NULL)
  155. cb=s->ctx->info_callback;
  156. s->in_handshake++;
  157. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  158. for (;;)
  159. {
  160. state=s->state;
  161. switch(s->state)
  162. {
  163. case SSL_ST_RENEGOTIATE:
  164. s->new_session=1;
  165. s->state=SSL_ST_CONNECT;
  166. s->ctx->stats.sess_connect_renegotiate++;
  167. /* break */
  168. case SSL_ST_BEFORE:
  169. case SSL_ST_CONNECT:
  170. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  171. case SSL_ST_OK|SSL_ST_CONNECT:
  172. s->server=0;
  173. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  174. if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
  175. (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
  176. {
  177. SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
  178. ret = -1;
  179. goto end;
  180. }
  181. /* s->version=SSL3_VERSION; */
  182. s->type=SSL_ST_CONNECT;
  183. if (s->init_buf == NULL)
  184. {
  185. if ((buf=BUF_MEM_new()) == NULL)
  186. {
  187. ret= -1;
  188. goto end;
  189. }
  190. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  191. {
  192. ret= -1;
  193. goto end;
  194. }
  195. s->init_buf=buf;
  196. buf=NULL;
  197. }
  198. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  199. /* setup buffing BIO */
  200. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  201. /* don't push the buffering BIO quite yet */
  202. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  203. s->ctx->stats.sess_connect++;
  204. s->init_num=0;
  205. /* mark client_random uninitialized */
  206. memset(s->s3->client_random,0,sizeof(s->s3->client_random));
  207. s->d1->send_cookie = 0;
  208. s->hit = 0;
  209. break;
  210. case SSL3_ST_CW_CLNT_HELLO_A:
  211. case SSL3_ST_CW_CLNT_HELLO_B:
  212. s->shutdown=0;
  213. /* every DTLS ClientHello resets Finished MAC */
  214. ssl3_init_finished_mac(s);
  215. dtls1_start_timer(s);
  216. ret=dtls1_client_hello(s);
  217. if (ret <= 0) goto end;
  218. if ( s->d1->send_cookie)
  219. {
  220. s->state=SSL3_ST_CW_FLUSH;
  221. s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
  222. }
  223. else
  224. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  225. s->init_num=0;
  226. /* turn on buffering for the next lot of output */
  227. if (s->bbio != s->wbio)
  228. s->wbio=BIO_push(s->bbio,s->wbio);
  229. break;
  230. case SSL3_ST_CR_SRVR_HELLO_A:
  231. case SSL3_ST_CR_SRVR_HELLO_B:
  232. ret=ssl3_get_server_hello(s);
  233. if (ret <= 0) goto end;
  234. else
  235. {
  236. dtls1_stop_timer(s);
  237. if (s->hit)
  238. s->state=SSL3_ST_CR_FINISHED_A;
  239. else
  240. s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  241. }
  242. s->init_num=0;
  243. break;
  244. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  245. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  246. ret = dtls1_get_hello_verify(s);
  247. if ( ret <= 0)
  248. goto end;
  249. dtls1_stop_timer(s);
  250. if ( s->d1->send_cookie) /* start again, with a cookie */
  251. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  252. else
  253. s->state = SSL3_ST_CR_CERT_A;
  254. s->init_num = 0;
  255. break;
  256. case SSL3_ST_CR_CERT_A:
  257. case SSL3_ST_CR_CERT_B:
  258. #ifndef OPENSSL_NO_TLSEXT
  259. ret=ssl3_check_finished(s);
  260. if (ret <= 0) goto end;
  261. if (ret == 2)
  262. {
  263. s->hit = 1;
  264. if (s->tlsext_ticket_expected)
  265. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  266. else
  267. s->state=SSL3_ST_CR_FINISHED_A;
  268. s->init_num=0;
  269. break;
  270. }
  271. #endif
  272. /* Check if it is anon DH or PSK */
  273. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  274. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  275. {
  276. ret=ssl3_get_server_certificate(s);
  277. if (ret <= 0) goto end;
  278. #ifndef OPENSSL_NO_TLSEXT
  279. if (s->tlsext_status_expected)
  280. s->state=SSL3_ST_CR_CERT_STATUS_A;
  281. else
  282. s->state=SSL3_ST_CR_KEY_EXCH_A;
  283. }
  284. else
  285. {
  286. skip = 1;
  287. s->state=SSL3_ST_CR_KEY_EXCH_A;
  288. }
  289. #else
  290. }
  291. else
  292. skip=1;
  293. s->state=SSL3_ST_CR_KEY_EXCH_A;
  294. #endif
  295. s->init_num=0;
  296. break;
  297. case SSL3_ST_CR_KEY_EXCH_A:
  298. case SSL3_ST_CR_KEY_EXCH_B:
  299. ret=ssl3_get_key_exchange(s);
  300. if (ret <= 0) goto end;
  301. s->state=SSL3_ST_CR_CERT_REQ_A;
  302. s->init_num=0;
  303. /* at this point we check that we have the
  304. * required stuff from the server */
  305. if (!ssl3_check_cert_and_algorithm(s))
  306. {
  307. ret= -1;
  308. goto end;
  309. }
  310. break;
  311. case SSL3_ST_CR_CERT_REQ_A:
  312. case SSL3_ST_CR_CERT_REQ_B:
  313. ret=ssl3_get_certificate_request(s);
  314. if (ret <= 0) goto end;
  315. s->state=SSL3_ST_CR_SRVR_DONE_A;
  316. s->init_num=0;
  317. break;
  318. case SSL3_ST_CR_SRVR_DONE_A:
  319. case SSL3_ST_CR_SRVR_DONE_B:
  320. ret=ssl3_get_server_done(s);
  321. if (ret <= 0) goto end;
  322. if (s->s3->tmp.cert_req)
  323. s->state=SSL3_ST_CW_CERT_A;
  324. else
  325. s->state=SSL3_ST_CW_KEY_EXCH_A;
  326. s->init_num=0;
  327. break;
  328. case SSL3_ST_CW_CERT_A:
  329. case SSL3_ST_CW_CERT_B:
  330. case SSL3_ST_CW_CERT_C:
  331. case SSL3_ST_CW_CERT_D:
  332. dtls1_start_timer(s);
  333. ret=dtls1_send_client_certificate(s);
  334. if (ret <= 0) goto end;
  335. s->state=SSL3_ST_CW_KEY_EXCH_A;
  336. s->init_num=0;
  337. break;
  338. case SSL3_ST_CW_KEY_EXCH_A:
  339. case SSL3_ST_CW_KEY_EXCH_B:
  340. dtls1_start_timer(s);
  341. ret=dtls1_send_client_key_exchange(s);
  342. if (ret <= 0) goto end;
  343. /* EAY EAY EAY need to check for DH fix cert
  344. * sent back */
  345. /* For TLS, cert_req is set to 2, so a cert chain
  346. * of nothing is sent, but no verify packet is sent */
  347. if (s->s3->tmp.cert_req == 1)
  348. {
  349. s->state=SSL3_ST_CW_CERT_VRFY_A;
  350. }
  351. else
  352. {
  353. s->state=SSL3_ST_CW_CHANGE_A;
  354. s->s3->change_cipher_spec=0;
  355. }
  356. s->init_num=0;
  357. break;
  358. case SSL3_ST_CW_CERT_VRFY_A:
  359. case SSL3_ST_CW_CERT_VRFY_B:
  360. dtls1_start_timer(s);
  361. ret=dtls1_send_client_verify(s);
  362. if (ret <= 0) goto end;
  363. s->state=SSL3_ST_CW_CHANGE_A;
  364. s->init_num=0;
  365. s->s3->change_cipher_spec=0;
  366. break;
  367. case SSL3_ST_CW_CHANGE_A:
  368. case SSL3_ST_CW_CHANGE_B:
  369. dtls1_start_timer(s);
  370. ret=dtls1_send_change_cipher_spec(s,
  371. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  372. if (ret <= 0) goto end;
  373. s->state=SSL3_ST_CW_FINISHED_A;
  374. s->init_num=0;
  375. s->session->cipher=s->s3->tmp.new_cipher;
  376. #ifdef OPENSSL_NO_COMP
  377. s->session->compress_meth=0;
  378. #else
  379. if (s->s3->tmp.new_compression == NULL)
  380. s->session->compress_meth=0;
  381. else
  382. s->session->compress_meth=
  383. s->s3->tmp.new_compression->id;
  384. #endif
  385. if (!s->method->ssl3_enc->setup_key_block(s))
  386. {
  387. ret= -1;
  388. goto end;
  389. }
  390. if (!s->method->ssl3_enc->change_cipher_state(s,
  391. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  392. {
  393. ret= -1;
  394. goto end;
  395. }
  396. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  397. break;
  398. case SSL3_ST_CW_FINISHED_A:
  399. case SSL3_ST_CW_FINISHED_B:
  400. dtls1_start_timer(s);
  401. ret=dtls1_send_finished(s,
  402. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  403. s->method->ssl3_enc->client_finished_label,
  404. s->method->ssl3_enc->client_finished_label_len);
  405. if (ret <= 0) goto end;
  406. s->state=SSL3_ST_CW_FLUSH;
  407. /* clear flags */
  408. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  409. if (s->hit)
  410. {
  411. s->s3->tmp.next_state=SSL_ST_OK;
  412. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  413. {
  414. s->state=SSL_ST_OK;
  415. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  416. s->s3->delay_buf_pop_ret=0;
  417. }
  418. }
  419. else
  420. {
  421. #ifndef OPENSSL_NO_TLSEXT
  422. /* Allow NewSessionTicket if ticket expected */
  423. if (s->tlsext_ticket_expected)
  424. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  425. else
  426. #endif
  427. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  428. }
  429. s->init_num=0;
  430. break;
  431. #ifndef OPENSSL_NO_TLSEXT
  432. case SSL3_ST_CR_SESSION_TICKET_A:
  433. case SSL3_ST_CR_SESSION_TICKET_B:
  434. ret=ssl3_get_new_session_ticket(s);
  435. if (ret <= 0) goto end;
  436. s->state=SSL3_ST_CR_FINISHED_A;
  437. s->init_num=0;
  438. break;
  439. case SSL3_ST_CR_CERT_STATUS_A:
  440. case SSL3_ST_CR_CERT_STATUS_B:
  441. ret=ssl3_get_cert_status(s);
  442. if (ret <= 0) goto end;
  443. s->state=SSL3_ST_CR_KEY_EXCH_A;
  444. s->init_num=0;
  445. break;
  446. #endif
  447. case SSL3_ST_CR_FINISHED_A:
  448. case SSL3_ST_CR_FINISHED_B:
  449. s->d1->change_cipher_spec_ok = 1;
  450. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  451. SSL3_ST_CR_FINISHED_B);
  452. if (ret <= 0) goto end;
  453. dtls1_stop_timer(s);
  454. if (s->hit)
  455. s->state=SSL3_ST_CW_CHANGE_A;
  456. else
  457. s->state=SSL_ST_OK;
  458. s->init_num=0;
  459. break;
  460. case SSL3_ST_CW_FLUSH:
  461. s->rwstate=SSL_WRITING;
  462. if (BIO_flush(s->wbio) <= 0)
  463. {
  464. ret= -1;
  465. goto end;
  466. }
  467. s->rwstate=SSL_NOTHING;
  468. s->state=s->s3->tmp.next_state;
  469. break;
  470. case SSL_ST_OK:
  471. /* clean a few things up */
  472. ssl3_cleanup_key_block(s);
  473. #if 0
  474. if (s->init_buf != NULL)
  475. {
  476. BUF_MEM_free(s->init_buf);
  477. s->init_buf=NULL;
  478. }
  479. #endif
  480. /* If we are not 'joining' the last two packets,
  481. * remove the buffering now */
  482. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  483. ssl_free_wbio_buffer(s);
  484. /* else do it later in ssl3_write */
  485. s->init_num=0;
  486. s->new_session=0;
  487. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  488. if (s->hit) s->ctx->stats.sess_hit++;
  489. ret=1;
  490. /* s->server=0; */
  491. s->handshake_func=dtls1_connect;
  492. s->ctx->stats.sess_connect_good++;
  493. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  494. /* done with handshaking */
  495. s->d1->handshake_read_seq = 0;
  496. s->d1->next_handshake_write_seq = 0;
  497. goto end;
  498. /* break; */
  499. default:
  500. SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
  501. ret= -1;
  502. goto end;
  503. /* break; */
  504. }
  505. /* did we do anything */
  506. if (!s->s3->tmp.reuse_message && !skip)
  507. {
  508. if (s->debug)
  509. {
  510. if ((ret=BIO_flush(s->wbio)) <= 0)
  511. goto end;
  512. }
  513. if ((cb != NULL) && (s->state != state))
  514. {
  515. new_state=s->state;
  516. s->state=state;
  517. cb(s,SSL_CB_CONNECT_LOOP,1);
  518. s->state=new_state;
  519. }
  520. }
  521. skip=0;
  522. }
  523. end:
  524. s->in_handshake--;
  525. if (buf != NULL)
  526. BUF_MEM_free(buf);
  527. if (cb != NULL)
  528. cb(s,SSL_CB_CONNECT_EXIT,ret);
  529. return(ret);
  530. }
  531. int dtls1_client_hello(SSL *s)
  532. {
  533. unsigned char *buf;
  534. unsigned char *p,*d;
  535. unsigned int i,j;
  536. unsigned long Time,l;
  537. SSL_COMP *comp;
  538. buf=(unsigned char *)s->init_buf->data;
  539. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  540. {
  541. SSL_SESSION *sess = s->session;
  542. if ((s->session == NULL) ||
  543. (s->session->ssl_version != s->version) ||
  544. #ifdef OPENSSL_NO_TLSEXT
  545. !sess->session_id_length ||
  546. #else
  547. (!sess->session_id_length && !sess->tlsext_tick) ||
  548. #endif
  549. (s->session->not_resumable))
  550. {
  551. if (!ssl_get_new_session(s,0))
  552. goto err;
  553. }
  554. /* else use the pre-loaded session */
  555. p=s->s3->client_random;
  556. /* if client_random is initialized, reuse it, we are
  557. * required to use same upon reply to HelloVerify */
  558. for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
  559. if (i==sizeof(s->s3->client_random))
  560. {
  561. Time=(unsigned long)time(NULL); /* Time */
  562. l2n(Time,p);
  563. RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4);
  564. }
  565. /* Do the message type and length last */
  566. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  567. *(p++)=s->version>>8;
  568. *(p++)=s->version&0xff;
  569. s->client_version=s->version;
  570. /* Random stuff */
  571. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  572. p+=SSL3_RANDOM_SIZE;
  573. /* Session ID */
  574. if (s->new_session)
  575. i=0;
  576. else
  577. i=s->session->session_id_length;
  578. *(p++)=i;
  579. if (i != 0)
  580. {
  581. if (i > sizeof s->session->session_id)
  582. {
  583. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  584. goto err;
  585. }
  586. memcpy(p,s->session->session_id,i);
  587. p+=i;
  588. }
  589. /* cookie stuff */
  590. if ( s->d1->cookie_len > sizeof(s->d1->cookie))
  591. {
  592. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  593. goto err;
  594. }
  595. *(p++) = s->d1->cookie_len;
  596. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  597. p += s->d1->cookie_len;
  598. /* Ciphers supported */
  599. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  600. if (i == 0)
  601. {
  602. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  603. goto err;
  604. }
  605. s2n(i,p);
  606. p+=i;
  607. /* COMPRESSION */
  608. if (s->ctx->comp_methods == NULL)
  609. j=0;
  610. else
  611. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  612. *(p++)=1+j;
  613. for (i=0; i<j; i++)
  614. {
  615. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  616. *(p++)=comp->id;
  617. }
  618. *(p++)=0; /* Add the NULL method */
  619. #ifndef OPENSSL_NO_TLSEXT
  620. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  621. {
  622. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  623. goto err;
  624. }
  625. #endif
  626. l=(p-d);
  627. d=buf;
  628. d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
  629. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  630. /* number of bytes to write */
  631. s->init_num=p-buf;
  632. s->init_off=0;
  633. /* buffer the message to handle re-xmits */
  634. dtls1_buffer_message(s, 0);
  635. }
  636. /* SSL3_ST_CW_CLNT_HELLO_B */
  637. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  638. err:
  639. return(-1);
  640. }
  641. static int dtls1_get_hello_verify(SSL *s)
  642. {
  643. int n, al, ok = 0;
  644. unsigned char *data;
  645. unsigned int cookie_len;
  646. n=s->method->ssl_get_message(s,
  647. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
  648. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  649. -1,
  650. s->max_cert_list,
  651. &ok);
  652. if (!ok) return((int)n);
  653. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
  654. {
  655. s->d1->send_cookie = 0;
  656. s->s3->tmp.reuse_message=1;
  657. return(1);
  658. }
  659. data = (unsigned char *)s->init_msg;
  660. if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
  661. {
  662. SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
  663. s->version=(s->version&0xff00)|data[1];
  664. al = SSL_AD_PROTOCOL_VERSION;
  665. goto f_err;
  666. }
  667. data+=2;
  668. cookie_len = *(data++);
  669. if ( cookie_len > sizeof(s->d1->cookie))
  670. {
  671. al=SSL_AD_ILLEGAL_PARAMETER;
  672. goto f_err;
  673. }
  674. memcpy(s->d1->cookie, data, cookie_len);
  675. s->d1->cookie_len = cookie_len;
  676. s->d1->send_cookie = 1;
  677. return 1;
  678. f_err:
  679. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  680. return -1;
  681. }
  682. int dtls1_send_client_key_exchange(SSL *s)
  683. {
  684. unsigned char *p,*d;
  685. int n;
  686. unsigned long alg_k;
  687. #ifndef OPENSSL_NO_RSA
  688. unsigned char *q;
  689. EVP_PKEY *pkey=NULL;
  690. #endif
  691. #ifndef OPENSSL_NO_KRB5
  692. KSSL_ERR kssl_err;
  693. #endif /* OPENSSL_NO_KRB5 */
  694. #ifndef OPENSSL_NO_ECDH
  695. EC_KEY *clnt_ecdh = NULL;
  696. const EC_POINT *srvr_ecpoint = NULL;
  697. EVP_PKEY *srvr_pub_pkey = NULL;
  698. unsigned char *encodedPoint = NULL;
  699. int encoded_pt_len = 0;
  700. BN_CTX * bn_ctx = NULL;
  701. #endif
  702. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  703. {
  704. d=(unsigned char *)s->init_buf->data;
  705. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  706. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  707. /* Fool emacs indentation */
  708. if (0) {}
  709. #ifndef OPENSSL_NO_RSA
  710. else if (alg_k & SSL_kRSA)
  711. {
  712. RSA *rsa;
  713. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  714. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  715. rsa=s->session->sess_cert->peer_rsa_tmp;
  716. else
  717. {
  718. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  719. if ((pkey == NULL) ||
  720. (pkey->type != EVP_PKEY_RSA) ||
  721. (pkey->pkey.rsa == NULL))
  722. {
  723. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  724. goto err;
  725. }
  726. rsa=pkey->pkey.rsa;
  727. EVP_PKEY_free(pkey);
  728. }
  729. tmp_buf[0]=s->client_version>>8;
  730. tmp_buf[1]=s->client_version&0xff;
  731. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  732. goto err;
  733. s->session->master_key_length=sizeof tmp_buf;
  734. q=p;
  735. /* Fix buf for TLS and [incidentally] DTLS */
  736. if (s->version > SSL3_VERSION)
  737. p+=2;
  738. n=RSA_public_encrypt(sizeof tmp_buf,
  739. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  740. #ifdef PKCS1_CHECK
  741. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  742. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  743. #endif
  744. if (n <= 0)
  745. {
  746. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  747. goto err;
  748. }
  749. /* Fix buf for TLS and [incidentally] DTLS */
  750. if (s->version > SSL3_VERSION)
  751. {
  752. s2n(n,q);
  753. n+=2;
  754. }
  755. s->session->master_key_length=
  756. s->method->ssl3_enc->generate_master_secret(s,
  757. s->session->master_key,
  758. tmp_buf,sizeof tmp_buf);
  759. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  760. }
  761. #endif
  762. #ifndef OPENSSL_NO_KRB5
  763. else if (alg_k & SSL_kKRB5)
  764. {
  765. krb5_error_code krb5rc;
  766. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  767. /* krb5_data krb5_ap_req; */
  768. krb5_data *enc_ticket;
  769. krb5_data authenticator, *authp = NULL;
  770. EVP_CIPHER_CTX ciph_ctx;
  771. const EVP_CIPHER *enc = NULL;
  772. unsigned char iv[EVP_MAX_IV_LENGTH];
  773. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  774. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  775. + EVP_MAX_IV_LENGTH];
  776. int padl, outl = sizeof(epms);
  777. EVP_CIPHER_CTX_init(&ciph_ctx);
  778. #ifdef KSSL_DEBUG
  779. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  780. alg_k, SSL_kKRB5);
  781. #endif /* KSSL_DEBUG */
  782. authp = NULL;
  783. #ifdef KRB5SENDAUTH
  784. if (KRB5SENDAUTH) authp = &authenticator;
  785. #endif /* KRB5SENDAUTH */
  786. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  787. &kssl_err);
  788. enc = kssl_map_enc(kssl_ctx->enctype);
  789. if (enc == NULL)
  790. goto err;
  791. #ifdef KSSL_DEBUG
  792. {
  793. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  794. if (krb5rc && kssl_err.text)
  795. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  796. }
  797. #endif /* KSSL_DEBUG */
  798. if (krb5rc)
  799. {
  800. ssl3_send_alert(s,SSL3_AL_FATAL,
  801. SSL_AD_HANDSHAKE_FAILURE);
  802. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  803. kssl_err.reason);
  804. goto err;
  805. }
  806. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  807. ** in place of RFC 2712 KerberosWrapper, as in:
  808. **
  809. ** Send ticket (copy to *p, set n = length)
  810. ** n = krb5_ap_req.length;
  811. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  812. ** if (krb5_ap_req.data)
  813. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  814. **
  815. ** Now using real RFC 2712 KerberosWrapper
  816. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  817. ** Note: 2712 "opaque" types are here replaced
  818. ** with a 2-byte length followed by the value.
  819. ** Example:
  820. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  821. ** Where "xx xx" = length bytes. Shown here with
  822. ** optional authenticator omitted.
  823. */
  824. /* KerberosWrapper.Ticket */
  825. s2n(enc_ticket->length,p);
  826. memcpy(p, enc_ticket->data, enc_ticket->length);
  827. p+= enc_ticket->length;
  828. n = enc_ticket->length + 2;
  829. /* KerberosWrapper.Authenticator */
  830. if (authp && authp->length)
  831. {
  832. s2n(authp->length,p);
  833. memcpy(p, authp->data, authp->length);
  834. p+= authp->length;
  835. n+= authp->length + 2;
  836. free(authp->data);
  837. authp->data = NULL;
  838. authp->length = 0;
  839. }
  840. else
  841. {
  842. s2n(0,p);/* null authenticator length */
  843. n+=2;
  844. }
  845. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  846. goto err;
  847. /* 20010420 VRS. Tried it this way; failed.
  848. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  849. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  850. ** kssl_ctx->length);
  851. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  852. */
  853. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  854. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  855. kssl_ctx->key,iv);
  856. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  857. sizeof tmp_buf);
  858. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  859. outl += padl;
  860. if (outl > (int)sizeof epms)
  861. {
  862. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  863. goto err;
  864. }
  865. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  866. /* KerberosWrapper.EncryptedPreMasterSecret */
  867. s2n(outl,p);
  868. memcpy(p, epms, outl);
  869. p+=outl;
  870. n+=outl + 2;
  871. s->session->master_key_length=
  872. s->method->ssl3_enc->generate_master_secret(s,
  873. s->session->master_key,
  874. tmp_buf, sizeof tmp_buf);
  875. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  876. OPENSSL_cleanse(epms, outl);
  877. }
  878. #endif
  879. #ifndef OPENSSL_NO_DH
  880. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  881. {
  882. DH *dh_srvr,*dh_clnt;
  883. if (s->session->sess_cert->peer_dh_tmp != NULL)
  884. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  885. else
  886. {
  887. /* we get them from the cert */
  888. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  889. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  890. goto err;
  891. }
  892. /* generate a new random key */
  893. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  894. {
  895. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  896. goto err;
  897. }
  898. if (!DH_generate_key(dh_clnt))
  899. {
  900. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  901. goto err;
  902. }
  903. /* use the 'p' output buffer for the DH key, but
  904. * make sure to clear it out afterwards */
  905. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  906. if (n <= 0)
  907. {
  908. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  909. goto err;
  910. }
  911. /* generate master key from the result */
  912. s->session->master_key_length=
  913. s->method->ssl3_enc->generate_master_secret(s,
  914. s->session->master_key,p,n);
  915. /* clean up */
  916. memset(p,0,n);
  917. /* send off the data */
  918. n=BN_num_bytes(dh_clnt->pub_key);
  919. s2n(n,p);
  920. BN_bn2bin(dh_clnt->pub_key,p);
  921. n+=2;
  922. DH_free(dh_clnt);
  923. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  924. }
  925. #endif
  926. #ifndef OPENSSL_NO_ECDH
  927. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  928. {
  929. const EC_GROUP *srvr_group = NULL;
  930. EC_KEY *tkey;
  931. int ecdh_clnt_cert = 0;
  932. int field_size = 0;
  933. /* Did we send out the client's
  934. * ECDH share for use in premaster
  935. * computation as part of client certificate?
  936. * If so, set ecdh_clnt_cert to 1.
  937. */
  938. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  939. {
  940. /* XXX: For now, we do not support client
  941. * authentication using ECDH certificates.
  942. * To add such support, one needs to add
  943. * code that checks for appropriate
  944. * conditions and sets ecdh_clnt_cert to 1.
  945. * For example, the cert have an ECC
  946. * key on the same curve as the server's
  947. * and the key should be authorized for
  948. * key agreement.
  949. *
  950. * One also needs to add code in ssl3_connect
  951. * to skip sending the certificate verify
  952. * message.
  953. *
  954. * if ((s->cert->key->privatekey != NULL) &&
  955. * (s->cert->key->privatekey->type ==
  956. * EVP_PKEY_EC) && ...)
  957. * ecdh_clnt_cert = 1;
  958. */
  959. }
  960. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  961. {
  962. tkey = s->session->sess_cert->peer_ecdh_tmp;
  963. }
  964. else
  965. {
  966. /* Get the Server Public Key from Cert */
  967. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  968. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  969. if ((srvr_pub_pkey == NULL) ||
  970. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  971. (srvr_pub_pkey->pkey.ec == NULL))
  972. {
  973. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  974. ERR_R_INTERNAL_ERROR);
  975. goto err;
  976. }
  977. tkey = srvr_pub_pkey->pkey.ec;
  978. }
  979. srvr_group = EC_KEY_get0_group(tkey);
  980. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  981. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  982. {
  983. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  984. ERR_R_INTERNAL_ERROR);
  985. goto err;
  986. }
  987. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  988. {
  989. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  990. goto err;
  991. }
  992. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  993. {
  994. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  995. goto err;
  996. }
  997. if (ecdh_clnt_cert)
  998. {
  999. /* Reuse key info from our certificate
  1000. * We only need our private key to perform
  1001. * the ECDH computation.
  1002. */
  1003. const BIGNUM *priv_key;
  1004. tkey = s->cert->key->privatekey->pkey.ec;
  1005. priv_key = EC_KEY_get0_private_key(tkey);
  1006. if (priv_key == NULL)
  1007. {
  1008. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1009. goto err;
  1010. }
  1011. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  1012. {
  1013. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  1014. goto err;
  1015. }
  1016. }
  1017. else
  1018. {
  1019. /* Generate a new ECDH key pair */
  1020. if (!(EC_KEY_generate_key(clnt_ecdh)))
  1021. {
  1022. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1023. goto err;
  1024. }
  1025. }
  1026. /* use the 'p' output buffer for the ECDH key, but
  1027. * make sure to clear it out afterwards
  1028. */
  1029. field_size = EC_GROUP_get_degree(srvr_group);
  1030. if (field_size <= 0)
  1031. {
  1032. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1033. ERR_R_ECDH_LIB);
  1034. goto err;
  1035. }
  1036. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  1037. if (n <= 0)
  1038. {
  1039. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1040. ERR_R_ECDH_LIB);
  1041. goto err;
  1042. }
  1043. /* generate master key from the result */
  1044. s->session->master_key_length = s->method->ssl3_enc \
  1045. -> generate_master_secret(s,
  1046. s->session->master_key,
  1047. p, n);
  1048. memset(p, 0, n); /* clean up */
  1049. if (ecdh_clnt_cert)
  1050. {
  1051. /* Send empty client key exch message */
  1052. n = 0;
  1053. }
  1054. else
  1055. {
  1056. /* First check the size of encoding and
  1057. * allocate memory accordingly.
  1058. */
  1059. encoded_pt_len =
  1060. EC_POINT_point2oct(srvr_group,
  1061. EC_KEY_get0_public_key(clnt_ecdh),
  1062. POINT_CONVERSION_UNCOMPRESSED,
  1063. NULL, 0, NULL);
  1064. encodedPoint = (unsigned char *)
  1065. OPENSSL_malloc(encoded_pt_len *
  1066. sizeof(unsigned char));
  1067. bn_ctx = BN_CTX_new();
  1068. if ((encodedPoint == NULL) ||
  1069. (bn_ctx == NULL))
  1070. {
  1071. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1072. goto err;
  1073. }
  1074. /* Encode the public key */
  1075. n = EC_POINT_point2oct(srvr_group,
  1076. EC_KEY_get0_public_key(clnt_ecdh),
  1077. POINT_CONVERSION_UNCOMPRESSED,
  1078. encodedPoint, encoded_pt_len, bn_ctx);
  1079. *p = n; /* length of encoded point */
  1080. /* Encoded point will be copied here */
  1081. p += 1;
  1082. /* copy the point */
  1083. memcpy((unsigned char *)p, encodedPoint, n);
  1084. /* increment n to account for length field */
  1085. n += 1;
  1086. }
  1087. /* Free allocated memory */
  1088. BN_CTX_free(bn_ctx);
  1089. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1090. if (clnt_ecdh != NULL)
  1091. EC_KEY_free(clnt_ecdh);
  1092. EVP_PKEY_free(srvr_pub_pkey);
  1093. }
  1094. #endif /* !OPENSSL_NO_ECDH */
  1095. #ifndef OPENSSL_NO_PSK
  1096. else if (alg_k & SSL_kPSK)
  1097. {
  1098. char identity[PSK_MAX_IDENTITY_LEN];
  1099. unsigned char *t = NULL;
  1100. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  1101. unsigned int pre_ms_len = 0, psk_len = 0;
  1102. int psk_err = 1;
  1103. n = 0;
  1104. if (s->psk_client_callback == NULL)
  1105. {
  1106. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1107. SSL_R_PSK_NO_CLIENT_CB);
  1108. goto err;
  1109. }
  1110. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  1111. identity, PSK_MAX_IDENTITY_LEN,
  1112. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1113. if (psk_len > PSK_MAX_PSK_LEN)
  1114. {
  1115. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1116. ERR_R_INTERNAL_ERROR);
  1117. goto psk_err;
  1118. }
  1119. else if (psk_len == 0)
  1120. {
  1121. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1122. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1123. goto psk_err;
  1124. }
  1125. /* create PSK pre_master_secret */
  1126. pre_ms_len = 2+psk_len+2+psk_len;
  1127. t = psk_or_pre_ms;
  1128. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  1129. s2n(psk_len, t);
  1130. memset(t, 0, psk_len);
  1131. t+=psk_len;
  1132. s2n(psk_len, t);
  1133. if (s->session->psk_identity_hint != NULL)
  1134. OPENSSL_free(s->session->psk_identity_hint);
  1135. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  1136. if (s->ctx->psk_identity_hint != NULL &&
  1137. s->session->psk_identity_hint == NULL)
  1138. {
  1139. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1140. ERR_R_MALLOC_FAILURE);
  1141. goto psk_err;
  1142. }
  1143. if (s->session->psk_identity != NULL)
  1144. OPENSSL_free(s->session->psk_identity);
  1145. s->session->psk_identity = BUF_strdup(identity);
  1146. if (s->session->psk_identity == NULL)
  1147. {
  1148. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1149. ERR_R_MALLOC_FAILURE);
  1150. goto psk_err;
  1151. }
  1152. s->session->master_key_length =
  1153. s->method->ssl3_enc->generate_master_secret(s,
  1154. s->session->master_key,
  1155. psk_or_pre_ms, pre_ms_len);
  1156. n = strlen(identity);
  1157. s2n(n, p);
  1158. memcpy(p, identity, n);
  1159. n+=2;
  1160. psk_err = 0;
  1161. psk_err:
  1162. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  1163. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1164. if (psk_err != 0)
  1165. {
  1166. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1167. goto err;
  1168. }
  1169. }
  1170. #endif
  1171. else
  1172. {
  1173. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1174. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1175. goto err;
  1176. }
  1177. d = dtls1_set_message_header(s, d,
  1178. SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
  1179. /*
  1180. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1181. l2n3(n,d);
  1182. l2n(s->d1->handshake_write_seq,d);
  1183. s->d1->handshake_write_seq++;
  1184. */
  1185. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1186. /* number of bytes to write */
  1187. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1188. s->init_off=0;
  1189. /* buffer the message to handle re-xmits */
  1190. dtls1_buffer_message(s, 0);
  1191. }
  1192. /* SSL3_ST_CW_KEY_EXCH_B */
  1193. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1194. err:
  1195. #ifndef OPENSSL_NO_ECDH
  1196. BN_CTX_free(bn_ctx);
  1197. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1198. if (clnt_ecdh != NULL)
  1199. EC_KEY_free(clnt_ecdh);
  1200. EVP_PKEY_free(srvr_pub_pkey);
  1201. #endif
  1202. return(-1);
  1203. }
  1204. int dtls1_send_client_verify(SSL *s)
  1205. {
  1206. unsigned char *p,*d;
  1207. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1208. EVP_PKEY *pkey;
  1209. #ifndef OPENSSL_NO_RSA
  1210. unsigned u=0;
  1211. #endif
  1212. unsigned long n;
  1213. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1214. int j;
  1215. #endif
  1216. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1217. {
  1218. d=(unsigned char *)s->init_buf->data;
  1219. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  1220. pkey=s->cert->key->privatekey;
  1221. s->method->ssl3_enc->cert_verify_mac(s,
  1222. NID_sha1,
  1223. &(data[MD5_DIGEST_LENGTH]));
  1224. #ifndef OPENSSL_NO_RSA
  1225. if (pkey->type == EVP_PKEY_RSA)
  1226. {
  1227. s->method->ssl3_enc->cert_verify_mac(s,
  1228. NID_md5,
  1229. &(data[0]));
  1230. if (RSA_sign(NID_md5_sha1, data,
  1231. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1232. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1233. {
  1234. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1235. goto err;
  1236. }
  1237. s2n(u,p);
  1238. n=u+2;
  1239. }
  1240. else
  1241. #endif
  1242. #ifndef OPENSSL_NO_DSA
  1243. if (pkey->type == EVP_PKEY_DSA)
  1244. {
  1245. if (!DSA_sign(pkey->save_type,
  1246. &(data[MD5_DIGEST_LENGTH]),
  1247. SHA_DIGEST_LENGTH,&(p[2]),
  1248. (unsigned int *)&j,pkey->pkey.dsa))
  1249. {
  1250. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1251. goto err;
  1252. }
  1253. s2n(j,p);
  1254. n=j+2;
  1255. }
  1256. else
  1257. #endif
  1258. #ifndef OPENSSL_NO_ECDSA
  1259. if (pkey->type == EVP_PKEY_EC)
  1260. {
  1261. if (!ECDSA_sign(pkey->save_type,
  1262. &(data[MD5_DIGEST_LENGTH]),
  1263. SHA_DIGEST_LENGTH,&(p[2]),
  1264. (unsigned int *)&j,pkey->pkey.ec))
  1265. {
  1266. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,
  1267. ERR_R_ECDSA_LIB);
  1268. goto err;
  1269. }
  1270. s2n(j,p);
  1271. n=j+2;
  1272. }
  1273. else
  1274. #endif
  1275. {
  1276. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  1277. goto err;
  1278. }
  1279. d = dtls1_set_message_header(s, d,
  1280. SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
  1281. s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
  1282. s->init_off=0;
  1283. /* buffer the message to handle re-xmits */
  1284. dtls1_buffer_message(s, 0);
  1285. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1286. }
  1287. /* s->state = SSL3_ST_CW_CERT_VRFY_B */
  1288. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1289. err:
  1290. return(-1);
  1291. }
  1292. int dtls1_send_client_certificate(SSL *s)
  1293. {
  1294. X509 *x509=NULL;
  1295. EVP_PKEY *pkey=NULL;
  1296. int i;
  1297. unsigned long l;
  1298. if (s->state == SSL3_ST_CW_CERT_A)
  1299. {
  1300. if ((s->cert == NULL) ||
  1301. (s->cert->key->x509 == NULL) ||
  1302. (s->cert->key->privatekey == NULL))
  1303. s->state=SSL3_ST_CW_CERT_B;
  1304. else
  1305. s->state=SSL3_ST_CW_CERT_C;
  1306. }
  1307. /* We need to get a client cert */
  1308. if (s->state == SSL3_ST_CW_CERT_B)
  1309. {
  1310. /* If we get an error, we need to
  1311. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1312. * We then get retied later */
  1313. i=0;
  1314. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1315. if (i < 0)
  1316. {
  1317. s->rwstate=SSL_X509_LOOKUP;
  1318. return(-1);
  1319. }
  1320. s->rwstate=SSL_NOTHING;
  1321. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1322. {
  1323. s->state=SSL3_ST_CW_CERT_B;
  1324. if ( !SSL_use_certificate(s,x509) ||
  1325. !SSL_use_PrivateKey(s,pkey))
  1326. i=0;
  1327. }
  1328. else if (i == 1)
  1329. {
  1330. i=0;
  1331. SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1332. }
  1333. if (x509 != NULL) X509_free(x509);
  1334. if (pkey != NULL) EVP_PKEY_free(pkey);
  1335. if (i == 0)
  1336. {
  1337. if (s->version == SSL3_VERSION)
  1338. {
  1339. s->s3->tmp.cert_req=0;
  1340. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1341. return(1);
  1342. }
  1343. else
  1344. {
  1345. s->s3->tmp.cert_req=2;
  1346. }
  1347. }
  1348. /* Ok, we have a cert */
  1349. s->state=SSL3_ST_CW_CERT_C;
  1350. }
  1351. if (s->state == SSL3_ST_CW_CERT_C)
  1352. {
  1353. s->state=SSL3_ST_CW_CERT_D;
  1354. l=dtls1_output_cert_chain(s,
  1355. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1356. s->init_num=(int)l;
  1357. s->init_off=0;
  1358. /* set header called by dtls1_output_cert_chain() */
  1359. /* buffer the message to handle re-xmits */
  1360. dtls1_buffer_message(s, 0);
  1361. }
  1362. /* SSL3_ST_CW_CERT_D */
  1363. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1364. }