d1_clnt.c 43 KB

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