d1_clnt.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /* ssl/d1_clnt.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  60. * All rights reserved.
  61. *
  62. * This package is an SSL implementation written
  63. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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, dtls1_get_client_method)
  141. int dtls1_connect(SSL *s)
  142. {
  143. BUF_MEM *buf = NULL;
  144. unsigned long Time = (unsigned long)time(NULL);
  145. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  146. int ret = -1;
  147. int new_state, state, skip = 0;
  148. #ifndef OPENSSL_NO_SCTP
  149. unsigned char sctpauthkey[64];
  150. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  151. #endif
  152. RAND_add(&Time, sizeof(Time), 0);
  153. ERR_clear_error();
  154. clear_sys_error();
  155. if (s->info_callback != NULL)
  156. cb = s->info_callback;
  157. else if (s->ctx->info_callback != NULL)
  158. cb = s->ctx->info_callback;
  159. s->in_handshake++;
  160. if (!SSL_in_init(s) || SSL_in_before(s))
  161. SSL_clear(s);
  162. #ifndef OPENSSL_NO_SCTP
  163. /*
  164. * Notify SCTP BIO socket to enter handshake mode and prevent stream
  165. * identifier other than 0. Will be ignored if no SCTP is used.
  166. */
  167. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
  168. s->in_handshake, NULL);
  169. #endif
  170. #ifndef OPENSSL_NO_HEARTBEATS
  171. /*
  172. * If we're awaiting a HeartbeatResponse, pretend we already got and
  173. * don't await it anymore, because Heartbeats don't make sense during
  174. * handshakes anyway.
  175. */
  176. if (s->tlsext_hb_pending) {
  177. dtls1_stop_timer(s);
  178. s->tlsext_hb_pending = 0;
  179. s->tlsext_hb_seq++;
  180. }
  181. #endif
  182. for (;;) {
  183. state = s->state;
  184. switch (s->state) {
  185. case SSL_ST_RENEGOTIATE:
  186. s->renegotiate = 1;
  187. s->state = SSL_ST_CONNECT;
  188. s->ctx->stats.sess_connect_renegotiate++;
  189. /* break */
  190. case SSL_ST_BEFORE:
  191. case SSL_ST_CONNECT:
  192. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  193. case SSL_ST_OK | SSL_ST_CONNECT:
  194. s->server = 0;
  195. if (cb != NULL)
  196. cb(s, SSL_CB_HANDSHAKE_START, 1);
  197. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
  198. (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) {
  199. SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
  200. ret = -1;
  201. s->state = SSL_ST_ERR;
  202. goto end;
  203. }
  204. /* s->version=SSL3_VERSION; */
  205. s->type = SSL_ST_CONNECT;
  206. if (s->init_buf == NULL) {
  207. if ((buf = BUF_MEM_new()) == NULL) {
  208. ret = -1;
  209. s->state = SSL_ST_ERR;
  210. goto end;
  211. }
  212. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  213. ret = -1;
  214. s->state = SSL_ST_ERR;
  215. goto end;
  216. }
  217. s->init_buf = buf;
  218. buf = NULL;
  219. }
  220. if (!ssl3_setup_buffers(s)) {
  221. ret = -1;
  222. s->state = SSL_ST_ERR;
  223. goto end;
  224. }
  225. /* setup buffing BIO */
  226. if (!ssl_init_wbio_buffer(s, 0)) {
  227. ret = -1;
  228. s->state = SSL_ST_ERR;
  229. goto end;
  230. }
  231. /* don't push the buffering BIO quite yet */
  232. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  233. s->ctx->stats.sess_connect++;
  234. s->init_num = 0;
  235. /* mark client_random uninitialized */
  236. memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
  237. s->d1->send_cookie = 0;
  238. s->hit = 0;
  239. s->d1->change_cipher_spec_ok = 0;
  240. /*
  241. * Should have been reset by ssl3_get_finished, too.
  242. */
  243. s->s3->change_cipher_spec = 0;
  244. break;
  245. #ifndef OPENSSL_NO_SCTP
  246. case DTLS1_SCTP_ST_CR_READ_SOCK:
  247. if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  248. s->s3->in_read_app_data = 2;
  249. s->rwstate = SSL_READING;
  250. BIO_clear_retry_flags(SSL_get_rbio(s));
  251. BIO_set_retry_read(SSL_get_rbio(s));
  252. ret = -1;
  253. goto end;
  254. }
  255. s->state = s->s3->tmp.next_state;
  256. break;
  257. case DTLS1_SCTP_ST_CW_WRITE_SOCK:
  258. /* read app data until dry event */
  259. ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
  260. if (ret < 0)
  261. goto end;
  262. if (ret == 0) {
  263. s->s3->in_read_app_data = 2;
  264. s->rwstate = SSL_READING;
  265. BIO_clear_retry_flags(SSL_get_rbio(s));
  266. BIO_set_retry_read(SSL_get_rbio(s));
  267. ret = -1;
  268. goto end;
  269. }
  270. s->state = s->d1->next_state;
  271. break;
  272. #endif
  273. case SSL3_ST_CW_CLNT_HELLO_A:
  274. case SSL3_ST_CW_CLNT_HELLO_B:
  275. s->shutdown = 0;
  276. /* every DTLS ClientHello resets Finished MAC */
  277. ssl3_init_finished_mac(s);
  278. dtls1_start_timer(s);
  279. ret = dtls1_client_hello(s);
  280. if (ret <= 0)
  281. goto end;
  282. if (s->d1->send_cookie) {
  283. s->state = SSL3_ST_CW_FLUSH;
  284. s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
  285. } else
  286. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  287. s->init_num = 0;
  288. #ifndef OPENSSL_NO_SCTP
  289. /* Disable buffering for SCTP */
  290. if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  291. #endif
  292. /*
  293. * turn on buffering for the next lot of output
  294. */
  295. if (s->bbio != s->wbio)
  296. s->wbio = BIO_push(s->bbio, s->wbio);
  297. #ifndef OPENSSL_NO_SCTP
  298. }
  299. #endif
  300. break;
  301. case SSL3_ST_CR_SRVR_HELLO_A:
  302. case SSL3_ST_CR_SRVR_HELLO_B:
  303. ret = ssl3_get_server_hello(s);
  304. if (ret <= 0)
  305. goto end;
  306. else {
  307. if (s->hit) {
  308. #ifndef OPENSSL_NO_SCTP
  309. /*
  310. * Add new shared key for SCTP-Auth, will be ignored if
  311. * no SCTP used.
  312. */
  313. snprintf((char *)labelbuffer,
  314. sizeof(DTLS1_SCTP_AUTH_LABEL),
  315. DTLS1_SCTP_AUTH_LABEL);
  316. SSL_export_keying_material(s, sctpauthkey,
  317. sizeof(sctpauthkey),
  318. labelbuffer,
  319. sizeof(labelbuffer), NULL, 0,
  320. 0);
  321. BIO_ctrl(SSL_get_wbio(s),
  322. BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  323. sizeof(sctpauthkey), sctpauthkey);
  324. #endif
  325. s->state = SSL3_ST_CR_FINISHED_A;
  326. } else
  327. s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  328. }
  329. s->init_num = 0;
  330. break;
  331. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  332. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  333. ret = dtls1_get_hello_verify(s);
  334. if (ret <= 0)
  335. goto end;
  336. dtls1_stop_timer(s);
  337. if (s->d1->send_cookie) /* start again, with a cookie */
  338. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  339. else
  340. s->state = SSL3_ST_CR_CERT_A;
  341. s->init_num = 0;
  342. break;
  343. case SSL3_ST_CR_CERT_A:
  344. case SSL3_ST_CR_CERT_B:
  345. /* Check if it is anon DH or PSK */
  346. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  347. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  348. ret = ssl3_get_server_certificate(s);
  349. if (ret <= 0)
  350. goto end;
  351. #ifndef OPENSSL_NO_TLSEXT
  352. if (s->tlsext_status_expected)
  353. s->state = SSL3_ST_CR_CERT_STATUS_A;
  354. else
  355. s->state = SSL3_ST_CR_KEY_EXCH_A;
  356. } else {
  357. skip = 1;
  358. s->state = SSL3_ST_CR_KEY_EXCH_A;
  359. }
  360. #else
  361. } else
  362. skip = 1;
  363. s->state = SSL3_ST_CR_KEY_EXCH_A;
  364. #endif
  365. s->init_num = 0;
  366. break;
  367. case SSL3_ST_CR_KEY_EXCH_A:
  368. case SSL3_ST_CR_KEY_EXCH_B:
  369. ret = ssl3_get_key_exchange(s);
  370. if (ret <= 0)
  371. goto end;
  372. s->state = SSL3_ST_CR_CERT_REQ_A;
  373. s->init_num = 0;
  374. /*
  375. * at this point we check that we have the required stuff from
  376. * the server
  377. */
  378. if (!ssl3_check_cert_and_algorithm(s)) {
  379. ret = -1;
  380. s->state = SSL_ST_ERR;
  381. goto end;
  382. }
  383. break;
  384. case SSL3_ST_CR_CERT_REQ_A:
  385. case SSL3_ST_CR_CERT_REQ_B:
  386. ret = ssl3_get_certificate_request(s);
  387. if (ret <= 0)
  388. goto end;
  389. s->state = SSL3_ST_CR_SRVR_DONE_A;
  390. s->init_num = 0;
  391. break;
  392. case SSL3_ST_CR_SRVR_DONE_A:
  393. case SSL3_ST_CR_SRVR_DONE_B:
  394. ret = ssl3_get_server_done(s);
  395. if (ret <= 0)
  396. goto end;
  397. dtls1_stop_timer(s);
  398. if (s->s3->tmp.cert_req)
  399. s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
  400. else
  401. s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
  402. s->init_num = 0;
  403. #ifndef OPENSSL_NO_SCTP
  404. if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
  405. state == SSL_ST_RENEGOTIATE)
  406. s->state = DTLS1_SCTP_ST_CR_READ_SOCK;
  407. else
  408. #endif
  409. s->state = s->s3->tmp.next_state;
  410. break;
  411. case SSL3_ST_CW_CERT_A:
  412. case SSL3_ST_CW_CERT_B:
  413. case SSL3_ST_CW_CERT_C:
  414. case SSL3_ST_CW_CERT_D:
  415. dtls1_start_timer(s);
  416. ret = dtls1_send_client_certificate(s);
  417. if (ret <= 0)
  418. goto end;
  419. s->state = SSL3_ST_CW_KEY_EXCH_A;
  420. s->init_num = 0;
  421. break;
  422. case SSL3_ST_CW_KEY_EXCH_A:
  423. case SSL3_ST_CW_KEY_EXCH_B:
  424. dtls1_start_timer(s);
  425. ret = dtls1_send_client_key_exchange(s);
  426. if (ret <= 0)
  427. goto end;
  428. #ifndef OPENSSL_NO_SCTP
  429. /*
  430. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  431. * used.
  432. */
  433. snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
  434. DTLS1_SCTP_AUTH_LABEL);
  435. SSL_export_keying_material(s, sctpauthkey,
  436. sizeof(sctpauthkey), labelbuffer,
  437. sizeof(labelbuffer), NULL, 0, 0);
  438. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  439. sizeof(sctpauthkey), sctpauthkey);
  440. #endif
  441. /*
  442. * EAY EAY EAY need to check for DH fix cert sent back
  443. */
  444. /*
  445. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  446. * sent, but no verify packet is sent
  447. */
  448. if (s->s3->tmp.cert_req == 1) {
  449. s->state = SSL3_ST_CW_CERT_VRFY_A;
  450. } else {
  451. #ifndef OPENSSL_NO_SCTP
  452. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  453. s->d1->next_state = SSL3_ST_CW_CHANGE_A;
  454. s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
  455. } else
  456. #endif
  457. s->state = SSL3_ST_CW_CHANGE_A;
  458. }
  459. s->init_num = 0;
  460. break;
  461. case SSL3_ST_CW_CERT_VRFY_A:
  462. case SSL3_ST_CW_CERT_VRFY_B:
  463. dtls1_start_timer(s);
  464. ret = dtls1_send_client_verify(s);
  465. if (ret <= 0)
  466. goto end;
  467. #ifndef OPENSSL_NO_SCTP
  468. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  469. s->d1->next_state = SSL3_ST_CW_CHANGE_A;
  470. s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
  471. } else
  472. #endif
  473. s->state = SSL3_ST_CW_CHANGE_A;
  474. s->init_num = 0;
  475. break;
  476. case SSL3_ST_CW_CHANGE_A:
  477. case SSL3_ST_CW_CHANGE_B:
  478. if (!s->hit)
  479. dtls1_start_timer(s);
  480. ret = dtls1_send_change_cipher_spec(s,
  481. SSL3_ST_CW_CHANGE_A,
  482. SSL3_ST_CW_CHANGE_B);
  483. if (ret <= 0)
  484. goto end;
  485. s->state = SSL3_ST_CW_FINISHED_A;
  486. s->init_num = 0;
  487. s->session->cipher = s->s3->tmp.new_cipher;
  488. #ifdef OPENSSL_NO_COMP
  489. s->session->compress_meth = 0;
  490. #else
  491. if (s->s3->tmp.new_compression == NULL)
  492. s->session->compress_meth = 0;
  493. else
  494. s->session->compress_meth = s->s3->tmp.new_compression->id;
  495. #endif
  496. if (!s->method->ssl3_enc->setup_key_block(s)) {
  497. ret = -1;
  498. s->state = SSL_ST_ERR;
  499. goto end;
  500. }
  501. if (!s->method->ssl3_enc->change_cipher_state(s,
  502. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  503. {
  504. ret = -1;
  505. s->state = SSL_ST_ERR;
  506. goto end;
  507. }
  508. #ifndef OPENSSL_NO_SCTP
  509. if (s->hit) {
  510. /*
  511. * Change to new shared key of SCTP-Auth, will be ignored if
  512. * no SCTP used.
  513. */
  514. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  515. 0, NULL);
  516. }
  517. #endif
  518. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  519. break;
  520. case SSL3_ST_CW_FINISHED_A:
  521. case SSL3_ST_CW_FINISHED_B:
  522. if (!s->hit)
  523. dtls1_start_timer(s);
  524. ret = dtls1_send_finished(s,
  525. SSL3_ST_CW_FINISHED_A,
  526. SSL3_ST_CW_FINISHED_B,
  527. s->method->
  528. ssl3_enc->client_finished_label,
  529. s->method->
  530. ssl3_enc->client_finished_label_len);
  531. if (ret <= 0)
  532. goto end;
  533. s->state = SSL3_ST_CW_FLUSH;
  534. /* clear flags */
  535. s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
  536. if (s->hit) {
  537. s->s3->tmp.next_state = SSL_ST_OK;
  538. #ifndef OPENSSL_NO_SCTP
  539. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  540. s->d1->next_state = s->s3->tmp.next_state;
  541. s->s3->tmp.next_state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
  542. }
  543. #endif
  544. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
  545. s->state = SSL_ST_OK;
  546. #ifndef OPENSSL_NO_SCTP
  547. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  548. s->d1->next_state = SSL_ST_OK;
  549. s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
  550. }
  551. #endif
  552. s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
  553. s->s3->delay_buf_pop_ret = 0;
  554. }
  555. } else {
  556. #ifndef OPENSSL_NO_SCTP
  557. /*
  558. * Change to new shared key of SCTP-Auth, will be ignored if
  559. * no SCTP used.
  560. */
  561. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  562. 0, NULL);
  563. #endif
  564. #ifndef OPENSSL_NO_TLSEXT
  565. /*
  566. * Allow NewSessionTicket if ticket expected
  567. */
  568. if (s->tlsext_ticket_expected)
  569. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  570. else
  571. #endif
  572. s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
  573. }
  574. s->init_num = 0;
  575. break;
  576. #ifndef OPENSSL_NO_TLSEXT
  577. case SSL3_ST_CR_SESSION_TICKET_A:
  578. case SSL3_ST_CR_SESSION_TICKET_B:
  579. ret = ssl3_get_new_session_ticket(s);
  580. if (ret <= 0)
  581. goto end;
  582. s->state = SSL3_ST_CR_FINISHED_A;
  583. s->init_num = 0;
  584. break;
  585. case SSL3_ST_CR_CERT_STATUS_A:
  586. case SSL3_ST_CR_CERT_STATUS_B:
  587. ret = ssl3_get_cert_status(s);
  588. if (ret <= 0)
  589. goto end;
  590. s->state = SSL3_ST_CR_KEY_EXCH_A;
  591. s->init_num = 0;
  592. break;
  593. #endif
  594. case SSL3_ST_CR_FINISHED_A:
  595. case SSL3_ST_CR_FINISHED_B:
  596. s->d1->change_cipher_spec_ok = 1;
  597. ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
  598. SSL3_ST_CR_FINISHED_B);
  599. if (ret <= 0)
  600. goto end;
  601. dtls1_stop_timer(s);
  602. if (s->hit)
  603. s->state = SSL3_ST_CW_CHANGE_A;
  604. else
  605. s->state = SSL_ST_OK;
  606. #ifndef OPENSSL_NO_SCTP
  607. if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
  608. state == SSL_ST_RENEGOTIATE) {
  609. s->d1->next_state = s->state;
  610. s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
  611. }
  612. #endif
  613. s->init_num = 0;
  614. break;
  615. case SSL3_ST_CW_FLUSH:
  616. s->rwstate = SSL_WRITING;
  617. if (BIO_flush(s->wbio) <= 0) {
  618. /*
  619. * If the write error was fatal, stop trying
  620. */
  621. if (!BIO_should_retry(s->wbio)) {
  622. s->rwstate = SSL_NOTHING;
  623. s->state = s->s3->tmp.next_state;
  624. }
  625. ret = -1;
  626. goto end;
  627. }
  628. s->rwstate = SSL_NOTHING;
  629. s->state = s->s3->tmp.next_state;
  630. break;
  631. case SSL_ST_OK:
  632. /* clean a few things up */
  633. ssl3_cleanup_key_block(s);
  634. #if 0
  635. if (s->init_buf != NULL) {
  636. BUF_MEM_free(s->init_buf);
  637. s->init_buf = NULL;
  638. }
  639. #endif
  640. /*
  641. * If we are not 'joining' the last two packets, remove the
  642. * buffering now
  643. */
  644. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  645. ssl_free_wbio_buffer(s);
  646. /* else do it later in ssl3_write */
  647. s->init_num = 0;
  648. s->renegotiate = 0;
  649. s->new_session = 0;
  650. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  651. if (s->hit)
  652. s->ctx->stats.sess_hit++;
  653. ret = 1;
  654. /* s->server=0; */
  655. s->handshake_func = dtls1_connect;
  656. s->ctx->stats.sess_connect_good++;
  657. if (cb != NULL)
  658. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  659. /* done with handshaking */
  660. s->d1->handshake_read_seq = 0;
  661. s->d1->next_handshake_write_seq = 0;
  662. goto end;
  663. /* break; */
  664. case SSL_ST_ERR:
  665. default:
  666. SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);
  667. ret = -1;
  668. goto end;
  669. /* break; */
  670. }
  671. /* did we do anything */
  672. if (!s->s3->tmp.reuse_message && !skip) {
  673. if (s->debug) {
  674. if ((ret = BIO_flush(s->wbio)) <= 0)
  675. goto end;
  676. }
  677. if ((cb != NULL) && (s->state != state)) {
  678. new_state = s->state;
  679. s->state = state;
  680. cb(s, SSL_CB_CONNECT_LOOP, 1);
  681. s->state = new_state;
  682. }
  683. }
  684. skip = 0;
  685. }
  686. end:
  687. s->in_handshake--;
  688. #ifndef OPENSSL_NO_SCTP
  689. /*
  690. * Notify SCTP BIO socket to leave handshake mode and allow stream
  691. * identifier other than 0. Will be ignored if no SCTP is used.
  692. */
  693. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
  694. s->in_handshake, NULL);
  695. #endif
  696. if (buf != NULL)
  697. BUF_MEM_free(buf);
  698. if (cb != NULL)
  699. cb(s, SSL_CB_CONNECT_EXIT, ret);
  700. return (ret);
  701. }
  702. int dtls1_client_hello(SSL *s)
  703. {
  704. unsigned char *buf;
  705. unsigned char *p, *d;
  706. unsigned int i, j;
  707. unsigned long l;
  708. SSL_COMP *comp;
  709. buf = (unsigned char *)s->init_buf->data;
  710. if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
  711. SSL_SESSION *sess = s->session;
  712. if ((s->session == NULL) || (s->session->ssl_version != s->version) ||
  713. #ifdef OPENSSL_NO_TLSEXT
  714. !sess->session_id_length ||
  715. #else
  716. (!sess->session_id_length && !sess->tlsext_tick) ||
  717. #endif
  718. (s->session->not_resumable)) {
  719. if (!ssl_get_new_session(s, 0))
  720. goto err;
  721. }
  722. /* else use the pre-loaded session */
  723. p = s->s3->client_random;
  724. /*
  725. * if client_random is initialized, reuse it, we are required to use
  726. * same upon reply to HelloVerify
  727. */
  728. for (i = 0; p[i] == '\0' && i < sizeof(s->s3->client_random); i++) ;
  729. if (i == sizeof(s->s3->client_random))
  730. ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
  731. /* Do the message type and length last */
  732. d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  733. *(p++) = s->version >> 8;
  734. *(p++) = s->version & 0xff;
  735. s->client_version = s->version;
  736. /* Random stuff */
  737. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  738. p += SSL3_RANDOM_SIZE;
  739. /* Session ID */
  740. if (s->new_session)
  741. i = 0;
  742. else
  743. i = s->session->session_id_length;
  744. *(p++) = i;
  745. if (i != 0) {
  746. if (i > sizeof s->session->session_id) {
  747. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  748. goto err;
  749. }
  750. memcpy(p, s->session->session_id, i);
  751. p += i;
  752. }
  753. /* cookie stuff */
  754. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  755. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  756. goto err;
  757. }
  758. *(p++) = s->d1->cookie_len;
  759. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  760. p += s->d1->cookie_len;
  761. /* Ciphers supported */
  762. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
  763. if (i == 0) {
  764. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  765. goto err;
  766. }
  767. s2n(i, p);
  768. p += i;
  769. /* COMPRESSION */
  770. if (s->ctx->comp_methods == NULL)
  771. j = 0;
  772. else
  773. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  774. *(p++) = 1 + j;
  775. for (i = 0; i < j; i++) {
  776. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  777. *(p++) = comp->id;
  778. }
  779. *(p++) = 0; /* Add the NULL method */
  780. #ifndef OPENSSL_NO_TLSEXT
  781. /* TLS extensions */
  782. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  783. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  784. goto err;
  785. }
  786. if ((p =
  787. ssl_add_clienthello_tlsext(s, p,
  788. buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
  789. NULL) {
  790. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  791. goto err;
  792. }
  793. #endif
  794. l = (p - d);
  795. d = buf;
  796. d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
  797. s->state = SSL3_ST_CW_CLNT_HELLO_B;
  798. /* number of bytes to write */
  799. s->init_num = p - buf;
  800. s->init_off = 0;
  801. /* buffer the message to handle re-xmits */
  802. dtls1_buffer_message(s, 0);
  803. }
  804. /* SSL3_ST_CW_CLNT_HELLO_B */
  805. return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
  806. err:
  807. return (-1);
  808. }
  809. static int dtls1_get_hello_verify(SSL *s)
  810. {
  811. int n, al, ok = 0;
  812. unsigned char *data;
  813. unsigned int cookie_len;
  814. n = s->method->ssl_get_message(s,
  815. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
  816. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  817. -1, s->max_cert_list, &ok);
  818. if (!ok)
  819. return ((int)n);
  820. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  821. s->d1->send_cookie = 0;
  822. s->s3->tmp.reuse_message = 1;
  823. return (1);
  824. }
  825. data = (unsigned char *)s->init_msg;
  826. if ((data[0] != (s->version >> 8)) || (data[1] != (s->version & 0xff))) {
  827. SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY, SSL_R_WRONG_SSL_VERSION);
  828. s->version = (s->version & 0xff00) | data[1];
  829. al = SSL_AD_PROTOCOL_VERSION;
  830. goto f_err;
  831. }
  832. data += 2;
  833. cookie_len = *(data++);
  834. if (cookie_len > sizeof(s->d1->cookie)) {
  835. al = SSL_AD_ILLEGAL_PARAMETER;
  836. goto f_err;
  837. }
  838. memcpy(s->d1->cookie, data, cookie_len);
  839. s->d1->cookie_len = cookie_len;
  840. s->d1->send_cookie = 1;
  841. return 1;
  842. f_err:
  843. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  844. s->state = SSL_ST_ERR;
  845. return -1;
  846. }
  847. int dtls1_send_client_key_exchange(SSL *s)
  848. {
  849. unsigned char *p, *d;
  850. int n;
  851. unsigned long alg_k;
  852. #ifndef OPENSSL_NO_RSA
  853. unsigned char *q;
  854. EVP_PKEY *pkey = NULL;
  855. #endif
  856. #ifndef OPENSSL_NO_KRB5
  857. KSSL_ERR kssl_err;
  858. #endif /* OPENSSL_NO_KRB5 */
  859. #ifndef OPENSSL_NO_ECDH
  860. EC_KEY *clnt_ecdh = NULL;
  861. const EC_POINT *srvr_ecpoint = NULL;
  862. EVP_PKEY *srvr_pub_pkey = NULL;
  863. unsigned char *encodedPoint = NULL;
  864. int encoded_pt_len = 0;
  865. BN_CTX *bn_ctx = NULL;
  866. #endif
  867. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  868. d = (unsigned char *)s->init_buf->data;
  869. p = &(d[DTLS1_HM_HEADER_LENGTH]);
  870. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  871. /* Fool emacs indentation */
  872. if (0) {
  873. }
  874. #ifndef OPENSSL_NO_RSA
  875. else if (alg_k & SSL_kRSA) {
  876. RSA *rsa;
  877. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  878. if (s->session->sess_cert == NULL) {
  879. /*
  880. * We should always have a server certificate with SSL_kRSA.
  881. */
  882. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  883. ERR_R_INTERNAL_ERROR);
  884. goto err;
  885. }
  886. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  887. rsa = s->session->sess_cert->peer_rsa_tmp;
  888. else {
  889. pkey =
  890. X509_get_pubkey(s->session->
  891. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
  892. x509);
  893. if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
  894. || (pkey->pkey.rsa == NULL)) {
  895. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  896. ERR_R_INTERNAL_ERROR);
  897. goto err;
  898. }
  899. rsa = pkey->pkey.rsa;
  900. EVP_PKEY_free(pkey);
  901. }
  902. tmp_buf[0] = s->client_version >> 8;
  903. tmp_buf[1] = s->client_version & 0xff;
  904. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  905. goto err;
  906. s->session->master_key_length = sizeof tmp_buf;
  907. q = p;
  908. /* Fix buf for TLS and [incidentally] DTLS */
  909. if (s->version > SSL3_VERSION)
  910. p += 2;
  911. n = RSA_public_encrypt(sizeof tmp_buf,
  912. tmp_buf, p, rsa, RSA_PKCS1_PADDING);
  913. # ifdef PKCS1_CHECK
  914. if (s->options & SSL_OP_PKCS1_CHECK_1)
  915. p[1]++;
  916. if (s->options & SSL_OP_PKCS1_CHECK_2)
  917. tmp_buf[0] = 0x70;
  918. # endif
  919. if (n <= 0) {
  920. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  921. SSL_R_BAD_RSA_ENCRYPT);
  922. goto err;
  923. }
  924. /* Fix buf for TLS and [incidentally] DTLS */
  925. if (s->version > SSL3_VERSION) {
  926. s2n(n, q);
  927. n += 2;
  928. }
  929. s->session->master_key_length =
  930. s->method->ssl3_enc->generate_master_secret(s,
  931. s->
  932. session->master_key,
  933. tmp_buf,
  934. sizeof tmp_buf);
  935. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  936. }
  937. #endif
  938. #ifndef OPENSSL_NO_KRB5
  939. else if (alg_k & SSL_kKRB5) {
  940. krb5_error_code krb5rc;
  941. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  942. /* krb5_data krb5_ap_req; */
  943. krb5_data *enc_ticket;
  944. krb5_data authenticator, *authp = NULL;
  945. EVP_CIPHER_CTX ciph_ctx;
  946. const EVP_CIPHER *enc = NULL;
  947. unsigned char iv[EVP_MAX_IV_LENGTH];
  948. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  949. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
  950. int padl, outl = sizeof(epms);
  951. EVP_CIPHER_CTX_init(&ciph_ctx);
  952. # ifdef KSSL_DEBUG
  953. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  954. alg_k, SSL_kKRB5);
  955. # endif /* KSSL_DEBUG */
  956. authp = NULL;
  957. # ifdef KRB5SENDAUTH
  958. if (KRB5SENDAUTH)
  959. authp = &authenticator;
  960. # endif /* KRB5SENDAUTH */
  961. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
  962. enc = kssl_map_enc(kssl_ctx->enctype);
  963. if (enc == NULL)
  964. goto err;
  965. # ifdef KSSL_DEBUG
  966. {
  967. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  968. if (krb5rc && kssl_err.text)
  969. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  970. }
  971. # endif /* KSSL_DEBUG */
  972. if (krb5rc) {
  973. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  974. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  975. goto err;
  976. }
  977. /*-
  978. * 20010406 VRS - Earlier versions used KRB5 AP_REQ
  979. ** in place of RFC 2712 KerberosWrapper, as in:
  980. **
  981. ** Send ticket (copy to *p, set n = length)
  982. ** n = krb5_ap_req.length;
  983. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  984. ** if (krb5_ap_req.data)
  985. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  986. **
  987. ** Now using real RFC 2712 KerberosWrapper
  988. ** (Thanks to Simon Wilkinson <[email protected]>)
  989. ** Note: 2712 "opaque" types are here replaced
  990. ** with a 2-byte length followed by the value.
  991. ** Example:
  992. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  993. ** Where "xx xx" = length bytes. Shown here with
  994. ** optional authenticator omitted.
  995. */
  996. /* KerberosWrapper.Ticket */
  997. s2n(enc_ticket->length, p);
  998. memcpy(p, enc_ticket->data, enc_ticket->length);
  999. p += enc_ticket->length;
  1000. n = enc_ticket->length + 2;
  1001. /* KerberosWrapper.Authenticator */
  1002. if (authp && authp->length) {
  1003. s2n(authp->length, p);
  1004. memcpy(p, authp->data, authp->length);
  1005. p += authp->length;
  1006. n += authp->length + 2;
  1007. free(authp->data);
  1008. authp->data = NULL;
  1009. authp->length = 0;
  1010. } else {
  1011. s2n(0, p); /* null authenticator length */
  1012. n += 2;
  1013. }
  1014. if (RAND_bytes(tmp_buf, sizeof tmp_buf) <= 0)
  1015. goto err;
  1016. /*-
  1017. * 20010420 VRS. Tried it this way; failed.
  1018. * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1019. * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1020. * kssl_ctx->length);
  1021. * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1022. */
  1023. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1024. EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
  1025. EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
  1026. sizeof tmp_buf);
  1027. EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
  1028. outl += padl;
  1029. if (outl > (int)sizeof epms) {
  1030. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1031. ERR_R_INTERNAL_ERROR);
  1032. goto err;
  1033. }
  1034. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1035. /* KerberosWrapper.EncryptedPreMasterSecret */
  1036. s2n(outl, p);
  1037. memcpy(p, epms, outl);
  1038. p += outl;
  1039. n += outl + 2;
  1040. s->session->master_key_length =
  1041. s->method->ssl3_enc->generate_master_secret(s,
  1042. s->
  1043. session->master_key,
  1044. tmp_buf,
  1045. sizeof tmp_buf);
  1046. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  1047. OPENSSL_cleanse(epms, outl);
  1048. }
  1049. #endif
  1050. #ifndef OPENSSL_NO_DH
  1051. else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
  1052. DH *dh_srvr, *dh_clnt;
  1053. if (s->session->sess_cert == NULL) {
  1054. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1055. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1056. SSL_R_UNEXPECTED_MESSAGE);
  1057. goto err;
  1058. }
  1059. if (s->session->sess_cert->peer_dh_tmp != NULL)
  1060. dh_srvr = s->session->sess_cert->peer_dh_tmp;
  1061. else {
  1062. /* we get them from the cert */
  1063. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1064. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1065. SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  1066. goto err;
  1067. }
  1068. /* generate a new random key */
  1069. if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
  1070. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  1071. goto err;
  1072. }
  1073. if (!DH_generate_key(dh_clnt)) {
  1074. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  1075. goto err;
  1076. }
  1077. /*
  1078. * use the 'p' output buffer for the DH key, but make sure to
  1079. * clear it out afterwards
  1080. */
  1081. n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
  1082. if (n <= 0) {
  1083. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  1084. goto err;
  1085. }
  1086. /* generate master key from the result */
  1087. s->session->master_key_length =
  1088. s->method->ssl3_enc->generate_master_secret(s,
  1089. s->
  1090. session->master_key,
  1091. p, n);
  1092. /* clean up */
  1093. memset(p, 0, n);
  1094. /* send off the data */
  1095. n = BN_num_bytes(dh_clnt->pub_key);
  1096. s2n(n, p);
  1097. BN_bn2bin(dh_clnt->pub_key, p);
  1098. n += 2;
  1099. DH_free(dh_clnt);
  1100. /* perhaps clean things up a bit EAY EAY EAY EAY */
  1101. }
  1102. #endif
  1103. #ifndef OPENSSL_NO_ECDH
  1104. else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
  1105. const EC_GROUP *srvr_group = NULL;
  1106. EC_KEY *tkey;
  1107. int ecdh_clnt_cert = 0;
  1108. int field_size = 0;
  1109. if (s->session->sess_cert == NULL) {
  1110. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1111. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1112. SSL_R_UNEXPECTED_MESSAGE);
  1113. goto err;
  1114. }
  1115. /*
  1116. * Did we send out the client's ECDH share for use in premaster
  1117. * computation as part of client certificate? If so, set
  1118. * ecdh_clnt_cert to 1.
  1119. */
  1120. if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
  1121. /*
  1122. * XXX: For now, we do not support client authentication
  1123. * using ECDH certificates. To add such support, one needs to
  1124. * add code that checks for appropriate conditions and sets
  1125. * ecdh_clnt_cert to 1. For example, the cert have an ECC key
  1126. * on the same curve as the server's and the key should be
  1127. * authorized for key agreement. One also needs to add code
  1128. * in ssl3_connect to skip sending the certificate verify
  1129. * message. if ((s->cert->key->privatekey != NULL) &&
  1130. * (s->cert->key->privatekey->type == EVP_PKEY_EC) && ...)
  1131. * ecdh_clnt_cert = 1;
  1132. */
  1133. }
  1134. if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
  1135. tkey = s->session->sess_cert->peer_ecdh_tmp;
  1136. } else {
  1137. /* Get the Server Public Key from Cert */
  1138. srvr_pub_pkey =
  1139. X509_get_pubkey(s->session->
  1140. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1141. if ((srvr_pub_pkey == NULL)
  1142. || (srvr_pub_pkey->type != EVP_PKEY_EC)
  1143. || (srvr_pub_pkey->pkey.ec == NULL)) {
  1144. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1145. ERR_R_INTERNAL_ERROR);
  1146. goto err;
  1147. }
  1148. tkey = srvr_pub_pkey->pkey.ec;
  1149. }
  1150. srvr_group = EC_KEY_get0_group(tkey);
  1151. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  1152. if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
  1153. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1154. ERR_R_INTERNAL_ERROR);
  1155. goto err;
  1156. }
  1157. if ((clnt_ecdh = EC_KEY_new()) == NULL) {
  1158. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1159. ERR_R_MALLOC_FAILURE);
  1160. goto err;
  1161. }
  1162. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  1163. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  1164. goto err;
  1165. }
  1166. if (ecdh_clnt_cert) {
  1167. /*
  1168. * Reuse key info from our certificate We only need our
  1169. * private key to perform the ECDH computation.
  1170. */
  1171. const BIGNUM *priv_key;
  1172. tkey = s->cert->key->privatekey->pkey.ec;
  1173. priv_key = EC_KEY_get0_private_key(tkey);
  1174. if (priv_key == NULL) {
  1175. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1176. ERR_R_MALLOC_FAILURE);
  1177. goto err;
  1178. }
  1179. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
  1180. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1181. ERR_R_EC_LIB);
  1182. goto err;
  1183. }
  1184. } else {
  1185. /* Generate a new ECDH key pair */
  1186. if (!(EC_KEY_generate_key(clnt_ecdh))) {
  1187. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1188. ERR_R_ECDH_LIB);
  1189. goto err;
  1190. }
  1191. }
  1192. /*
  1193. * use the 'p' output buffer for the ECDH key, but make sure to
  1194. * clear it out afterwards
  1195. */
  1196. field_size = EC_GROUP_get_degree(srvr_group);
  1197. if (field_size <= 0) {
  1198. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1199. goto err;
  1200. }
  1201. n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
  1202. clnt_ecdh, NULL);
  1203. if (n <= 0) {
  1204. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1205. goto err;
  1206. }
  1207. /* generate master key from the result */
  1208. s->session->master_key_length =
  1209. s->method->ssl3_enc->generate_master_secret(s,
  1210. s->
  1211. session->master_key,
  1212. p, n);
  1213. memset(p, 0, n); /* clean up */
  1214. if (ecdh_clnt_cert) {
  1215. /* Send empty client key exch message */
  1216. n = 0;
  1217. } else {
  1218. /*
  1219. * First check the size of encoding and allocate memory
  1220. * accordingly.
  1221. */
  1222. encoded_pt_len =
  1223. EC_POINT_point2oct(srvr_group,
  1224. EC_KEY_get0_public_key(clnt_ecdh),
  1225. POINT_CONVERSION_UNCOMPRESSED,
  1226. NULL, 0, NULL);
  1227. encodedPoint = (unsigned char *)
  1228. OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
  1229. bn_ctx = BN_CTX_new();
  1230. if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
  1231. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1232. ERR_R_MALLOC_FAILURE);
  1233. goto err;
  1234. }
  1235. /* Encode the public key */
  1236. n = EC_POINT_point2oct(srvr_group,
  1237. EC_KEY_get0_public_key(clnt_ecdh),
  1238. POINT_CONVERSION_UNCOMPRESSED,
  1239. encodedPoint, encoded_pt_len, bn_ctx);
  1240. *p = n; /* length of encoded point */
  1241. /* Encoded point will be copied here */
  1242. p += 1;
  1243. /* copy the point */
  1244. memcpy((unsigned char *)p, encodedPoint, n);
  1245. /* increment n to account for length field */
  1246. n += 1;
  1247. }
  1248. /* Free allocated memory */
  1249. BN_CTX_free(bn_ctx);
  1250. if (encodedPoint != NULL)
  1251. OPENSSL_free(encodedPoint);
  1252. if (clnt_ecdh != NULL)
  1253. EC_KEY_free(clnt_ecdh);
  1254. EVP_PKEY_free(srvr_pub_pkey);
  1255. }
  1256. #endif /* !OPENSSL_NO_ECDH */
  1257. #ifndef OPENSSL_NO_PSK
  1258. else if (alg_k & SSL_kPSK) {
  1259. char identity[PSK_MAX_IDENTITY_LEN];
  1260. unsigned char *t = NULL;
  1261. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
  1262. unsigned int pre_ms_len = 0, psk_len = 0;
  1263. int psk_err = 1;
  1264. n = 0;
  1265. if (s->psk_client_callback == NULL) {
  1266. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1267. SSL_R_PSK_NO_CLIENT_CB);
  1268. goto err;
  1269. }
  1270. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  1271. identity, PSK_MAX_IDENTITY_LEN,
  1272. psk_or_pre_ms,
  1273. sizeof(psk_or_pre_ms));
  1274. if (psk_len > PSK_MAX_PSK_LEN) {
  1275. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1276. ERR_R_INTERNAL_ERROR);
  1277. goto psk_err;
  1278. } else if (psk_len == 0) {
  1279. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1280. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1281. goto psk_err;
  1282. }
  1283. /* create PSK pre_master_secret */
  1284. pre_ms_len = 2 + psk_len + 2 + psk_len;
  1285. t = psk_or_pre_ms;
  1286. memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
  1287. s2n(psk_len, t);
  1288. memset(t, 0, psk_len);
  1289. t += psk_len;
  1290. s2n(psk_len, t);
  1291. if (s->session->psk_identity_hint != NULL)
  1292. OPENSSL_free(s->session->psk_identity_hint);
  1293. s->session->psk_identity_hint =
  1294. BUF_strdup(s->ctx->psk_identity_hint);
  1295. if (s->ctx->psk_identity_hint != NULL
  1296. && s->session->psk_identity_hint == NULL) {
  1297. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1298. ERR_R_MALLOC_FAILURE);
  1299. goto psk_err;
  1300. }
  1301. if (s->session->psk_identity != NULL)
  1302. OPENSSL_free(s->session->psk_identity);
  1303. s->session->psk_identity = BUF_strdup(identity);
  1304. if (s->session->psk_identity == NULL) {
  1305. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1306. ERR_R_MALLOC_FAILURE);
  1307. goto psk_err;
  1308. }
  1309. s->session->master_key_length =
  1310. s->method->ssl3_enc->generate_master_secret(s,
  1311. s->
  1312. session->master_key,
  1313. psk_or_pre_ms,
  1314. pre_ms_len);
  1315. n = strlen(identity);
  1316. s2n(n, p);
  1317. memcpy(p, identity, n);
  1318. n += 2;
  1319. psk_err = 0;
  1320. psk_err:
  1321. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  1322. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1323. if (psk_err != 0) {
  1324. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1325. goto err;
  1326. }
  1327. }
  1328. #endif
  1329. else {
  1330. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1331. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1332. ERR_R_INTERNAL_ERROR);
  1333. goto err;
  1334. }
  1335. d = dtls1_set_message_header(s, d,
  1336. SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
  1337. /*-
  1338. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1339. l2n3(n,d);
  1340. l2n(s->d1->handshake_write_seq,d);
  1341. s->d1->handshake_write_seq++;
  1342. */
  1343. s->state = SSL3_ST_CW_KEY_EXCH_B;
  1344. /* number of bytes to write */
  1345. s->init_num = n + DTLS1_HM_HEADER_LENGTH;
  1346. s->init_off = 0;
  1347. /* buffer the message to handle re-xmits */
  1348. dtls1_buffer_message(s, 0);
  1349. }
  1350. /* SSL3_ST_CW_KEY_EXCH_B */
  1351. return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
  1352. err:
  1353. #ifndef OPENSSL_NO_ECDH
  1354. BN_CTX_free(bn_ctx);
  1355. if (encodedPoint != NULL)
  1356. OPENSSL_free(encodedPoint);
  1357. if (clnt_ecdh != NULL)
  1358. EC_KEY_free(clnt_ecdh);
  1359. EVP_PKEY_free(srvr_pub_pkey);
  1360. #endif
  1361. return (-1);
  1362. }
  1363. int dtls1_send_client_verify(SSL *s)
  1364. {
  1365. unsigned char *p, *d;
  1366. unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
  1367. EVP_PKEY *pkey;
  1368. #ifndef OPENSSL_NO_RSA
  1369. unsigned u = 0;
  1370. #endif
  1371. unsigned long n;
  1372. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1373. int j;
  1374. #endif
  1375. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  1376. d = (unsigned char *)s->init_buf->data;
  1377. p = &(d[DTLS1_HM_HEADER_LENGTH]);
  1378. pkey = s->cert->key->privatekey;
  1379. s->method->ssl3_enc->cert_verify_mac(s,
  1380. NID_sha1,
  1381. &(data[MD5_DIGEST_LENGTH]));
  1382. #ifndef OPENSSL_NO_RSA
  1383. if (pkey->type == EVP_PKEY_RSA) {
  1384. s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
  1385. if (RSA_sign(NID_md5_sha1, data,
  1386. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
  1387. &(p[2]), &u, pkey->pkey.rsa) <= 0) {
  1388. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
  1389. goto err;
  1390. }
  1391. s2n(u, p);
  1392. n = u + 2;
  1393. } else
  1394. #endif
  1395. #ifndef OPENSSL_NO_DSA
  1396. if (pkey->type == EVP_PKEY_DSA) {
  1397. if (!DSA_sign(pkey->save_type,
  1398. &(data[MD5_DIGEST_LENGTH]),
  1399. SHA_DIGEST_LENGTH, &(p[2]),
  1400. (unsigned int *)&j, pkey->pkey.dsa)) {
  1401. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
  1402. goto err;
  1403. }
  1404. s2n(j, p);
  1405. n = j + 2;
  1406. } else
  1407. #endif
  1408. #ifndef OPENSSL_NO_ECDSA
  1409. if (pkey->type == EVP_PKEY_EC) {
  1410. if (!ECDSA_sign(pkey->save_type,
  1411. &(data[MD5_DIGEST_LENGTH]),
  1412. SHA_DIGEST_LENGTH, &(p[2]),
  1413. (unsigned int *)&j, pkey->pkey.ec)) {
  1414. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
  1415. goto err;
  1416. }
  1417. s2n(j, p);
  1418. n = j + 2;
  1419. } else
  1420. #endif
  1421. {
  1422. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  1423. goto err;
  1424. }
  1425. d = dtls1_set_message_header(s, d,
  1426. SSL3_MT_CERTIFICATE_VERIFY, n, 0, n);
  1427. s->init_num = (int)n + DTLS1_HM_HEADER_LENGTH;
  1428. s->init_off = 0;
  1429. /* buffer the message to handle re-xmits */
  1430. dtls1_buffer_message(s, 0);
  1431. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1432. }
  1433. /* s->state = SSL3_ST_CW_CERT_VRFY_B */
  1434. return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
  1435. err:
  1436. return (-1);
  1437. }
  1438. int dtls1_send_client_certificate(SSL *s)
  1439. {
  1440. X509 *x509 = NULL;
  1441. EVP_PKEY *pkey = NULL;
  1442. int i;
  1443. unsigned long l;
  1444. if (s->state == SSL3_ST_CW_CERT_A) {
  1445. if ((s->cert == NULL) ||
  1446. (s->cert->key->x509 == NULL) ||
  1447. (s->cert->key->privatekey == NULL))
  1448. s->state = SSL3_ST_CW_CERT_B;
  1449. else
  1450. s->state = SSL3_ST_CW_CERT_C;
  1451. }
  1452. /* We need to get a client cert */
  1453. if (s->state == SSL3_ST_CW_CERT_B) {
  1454. /*
  1455. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  1456. * return(-1); We then get retied later
  1457. */
  1458. i = 0;
  1459. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1460. if (i < 0) {
  1461. s->rwstate = SSL_X509_LOOKUP;
  1462. return (-1);
  1463. }
  1464. s->rwstate = SSL_NOTHING;
  1465. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  1466. s->state = SSL3_ST_CW_CERT_B;
  1467. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  1468. i = 0;
  1469. } else if (i == 1) {
  1470. i = 0;
  1471. SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
  1472. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1473. }
  1474. if (x509 != NULL)
  1475. X509_free(x509);
  1476. if (pkey != NULL)
  1477. EVP_PKEY_free(pkey);
  1478. if (i == 0) {
  1479. if (s->version == SSL3_VERSION) {
  1480. s->s3->tmp.cert_req = 0;
  1481. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1482. return (1);
  1483. } else {
  1484. s->s3->tmp.cert_req = 2;
  1485. }
  1486. }
  1487. /* Ok, we have a cert */
  1488. s->state = SSL3_ST_CW_CERT_C;
  1489. }
  1490. if (s->state == SSL3_ST_CW_CERT_C) {
  1491. s->state = SSL3_ST_CW_CERT_D;
  1492. l = dtls1_output_cert_chain(s,
  1493. (s->s3->tmp.cert_req ==
  1494. 2) ? NULL : s->cert->key->x509);
  1495. if (!l) {
  1496. SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  1497. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1498. return 0;
  1499. }
  1500. s->init_num = (int)l;
  1501. s->init_off = 0;
  1502. /* set header called by dtls1_output_cert_chain() */
  1503. /* buffer the message to handle re-xmits */
  1504. dtls1_buffer_message(s, 0);
  1505. }
  1506. /* SSL3_ST_CW_CERT_D */
  1507. return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
  1508. }