d1_srvr.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /* ssl/d1_srvr.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. #include <openssl/buffer.h>
  118. #include <openssl/rand.h>
  119. #include <openssl/objects.h>
  120. #include <openssl/evp.h>
  121. #include <openssl/x509.h>
  122. #include <openssl/md5.h>
  123. #include <openssl/bn.h>
  124. #ifndef OPENSSL_NO_DH
  125. #include <openssl/dh.h>
  126. #endif
  127. static const SSL_METHOD *dtls1_get_server_method(int ver);
  128. static int dtls1_send_hello_verify_request(SSL *s);
  129. static const SSL_METHOD *dtls1_get_server_method(int ver)
  130. {
  131. if (ver == DTLS1_VERSION)
  132. return(DTLSv1_server_method());
  133. else
  134. return(NULL);
  135. }
  136. IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
  137. dtls1_accept,
  138. ssl_undefined_function,
  139. dtls1_get_server_method)
  140. int dtls1_accept(SSL *s)
  141. {
  142. BUF_MEM *buf;
  143. unsigned long Time=(unsigned long)time(NULL);
  144. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  145. unsigned long alg_k;
  146. int ret= -1;
  147. int new_state,state,skip=0;
  148. int listen;
  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. listen = s->d1->listen;
  161. /* init things to blank */
  162. s->in_handshake++;
  163. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  164. s->d1->listen = listen;
  165. #ifndef OPENSSL_NO_SCTP
  166. /* Notify SCTP BIO socket to enter handshake
  167. * mode and prevent stream identifier other
  168. * than 0. Will be ignored if no SCTP is used.
  169. */
  170. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
  171. #endif
  172. if (s->cert == NULL)
  173. {
  174. SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  175. return(-1);
  176. }
  177. #ifndef OPENSSL_NO_HEARTBEATS
  178. /* If we're awaiting a HeartbeatResponse, pretend we
  179. * already got and don't await it anymore, because
  180. * Heartbeats don't make sense during handshakes anyway.
  181. */
  182. if (s->tlsext_hb_pending)
  183. {
  184. dtls1_stop_timer(s);
  185. s->tlsext_hb_pending = 0;
  186. s->tlsext_hb_seq++;
  187. }
  188. #endif
  189. for (;;)
  190. {
  191. state=s->state;
  192. switch (s->state)
  193. {
  194. case SSL_ST_RENEGOTIATE:
  195. s->renegotiate=1;
  196. /* s->state=SSL_ST_ACCEPT; */
  197. case SSL_ST_BEFORE:
  198. case SSL_ST_ACCEPT:
  199. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  200. case SSL_ST_OK|SSL_ST_ACCEPT:
  201. s->server=1;
  202. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  203. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
  204. {
  205. SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
  206. return -1;
  207. }
  208. s->type=SSL_ST_ACCEPT;
  209. if (s->init_buf == NULL)
  210. {
  211. if ((buf=BUF_MEM_new()) == NULL)
  212. {
  213. ret= -1;
  214. goto end;
  215. }
  216. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  217. {
  218. ret= -1;
  219. goto end;
  220. }
  221. s->init_buf=buf;
  222. }
  223. if (!ssl3_setup_buffers(s))
  224. {
  225. ret= -1;
  226. goto end;
  227. }
  228. s->init_num=0;
  229. if (s->state != SSL_ST_RENEGOTIATE)
  230. {
  231. /* Ok, we now need to push on a buffering BIO so that
  232. * the output is sent in a way that TCP likes :-)
  233. * ...but not with SCTP :-)
  234. */
  235. #ifndef OPENSSL_NO_SCTP
  236. if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
  237. #endif
  238. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  239. ssl3_init_finished_mac(s);
  240. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  241. s->ctx->stats.sess_accept++;
  242. }
  243. else
  244. {
  245. /* s->state == SSL_ST_RENEGOTIATE,
  246. * we will just send a HelloRequest */
  247. s->ctx->stats.sess_accept_renegotiate++;
  248. s->state=SSL3_ST_SW_HELLO_REQ_A;
  249. }
  250. break;
  251. case SSL3_ST_SW_HELLO_REQ_A:
  252. case SSL3_ST_SW_HELLO_REQ_B:
  253. s->shutdown=0;
  254. dtls1_start_timer(s);
  255. ret=dtls1_send_hello_request(s);
  256. if (ret <= 0) goto end;
  257. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  258. s->state=SSL3_ST_SW_FLUSH;
  259. s->init_num=0;
  260. ssl3_init_finished_mac(s);
  261. break;
  262. case SSL3_ST_SW_HELLO_REQ_C:
  263. s->state=SSL_ST_OK;
  264. break;
  265. case SSL3_ST_SR_CLNT_HELLO_A:
  266. case SSL3_ST_SR_CLNT_HELLO_B:
  267. case SSL3_ST_SR_CLNT_HELLO_C:
  268. s->shutdown=0;
  269. ret=ssl3_get_client_hello(s);
  270. if (ret <= 0) goto end;
  271. dtls1_stop_timer(s);
  272. if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  273. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
  274. else
  275. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  276. s->init_num=0;
  277. /* Reflect ClientHello sequence to remain stateless while listening */
  278. if (listen)
  279. {
  280. memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence));
  281. }
  282. /* If we're just listening, stop here */
  283. if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A)
  284. {
  285. ret = 2;
  286. s->d1->listen = 0;
  287. /* Set expected sequence numbers
  288. * to continue the handshake.
  289. */
  290. s->d1->handshake_read_seq = 2;
  291. s->d1->handshake_write_seq = 1;
  292. s->d1->next_handshake_write_seq = 1;
  293. goto end;
  294. }
  295. break;
  296. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
  297. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
  298. ret = dtls1_send_hello_verify_request(s);
  299. if ( ret <= 0) goto end;
  300. s->state=SSL3_ST_SW_FLUSH;
  301. s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
  302. /* HelloVerifyRequest resets Finished MAC */
  303. if (s->version != DTLS1_BAD_VER)
  304. ssl3_init_finished_mac(s);
  305. break;
  306. #ifndef OPENSSL_NO_SCTP
  307. case DTLS1_SCTP_ST_SR_READ_SOCK:
  308. if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))
  309. {
  310. s->s3->in_read_app_data=2;
  311. s->rwstate=SSL_READING;
  312. BIO_clear_retry_flags(SSL_get_rbio(s));
  313. BIO_set_retry_read(SSL_get_rbio(s));
  314. ret = -1;
  315. goto end;
  316. }
  317. s->state=SSL3_ST_SR_FINISHED_A;
  318. break;
  319. case DTLS1_SCTP_ST_SW_WRITE_SOCK:
  320. ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
  321. if (ret < 0) goto end;
  322. if (ret == 0)
  323. {
  324. if (s->d1->next_state != SSL_ST_OK)
  325. {
  326. s->s3->in_read_app_data=2;
  327. s->rwstate=SSL_READING;
  328. BIO_clear_retry_flags(SSL_get_rbio(s));
  329. BIO_set_retry_read(SSL_get_rbio(s));
  330. ret = -1;
  331. goto end;
  332. }
  333. }
  334. s->state=s->d1->next_state;
  335. break;
  336. #endif
  337. case SSL3_ST_SW_SRVR_HELLO_A:
  338. case SSL3_ST_SW_SRVR_HELLO_B:
  339. s->renegotiate = 2;
  340. dtls1_start_timer(s);
  341. ret=dtls1_send_server_hello(s);
  342. if (ret <= 0) goto end;
  343. if (s->hit)
  344. {
  345. #ifndef OPENSSL_NO_SCTP
  346. /* Add new shared key for SCTP-Auth,
  347. * will be ignored if no SCTP used.
  348. */
  349. snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
  350. DTLS1_SCTP_AUTH_LABEL);
  351. SSL_export_keying_material(s, sctpauthkey,
  352. sizeof(sctpauthkey), labelbuffer,
  353. sizeof(labelbuffer), NULL, 0, 0);
  354. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  355. sizeof(sctpauthkey), sctpauthkey);
  356. #endif
  357. #ifndef OPENSSL_NO_TLSEXT
  358. if (s->tlsext_ticket_expected)
  359. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  360. else
  361. s->state=SSL3_ST_SW_CHANGE_A;
  362. #else
  363. s->state=SSL3_ST_SW_CHANGE_A;
  364. #endif
  365. }
  366. else
  367. s->state=SSL3_ST_SW_CERT_A;
  368. s->init_num=0;
  369. break;
  370. case SSL3_ST_SW_CERT_A:
  371. case SSL3_ST_SW_CERT_B:
  372. /* Check if it is anon DH or normal PSK */
  373. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  374. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  375. {
  376. dtls1_start_timer(s);
  377. ret=dtls1_send_server_certificate(s);
  378. if (ret <= 0) goto end;
  379. #ifndef OPENSSL_NO_TLSEXT
  380. if (s->tlsext_status_expected)
  381. s->state=SSL3_ST_SW_CERT_STATUS_A;
  382. else
  383. s->state=SSL3_ST_SW_KEY_EXCH_A;
  384. }
  385. else
  386. {
  387. skip = 1;
  388. s->state=SSL3_ST_SW_KEY_EXCH_A;
  389. }
  390. #else
  391. }
  392. else
  393. skip=1;
  394. s->state=SSL3_ST_SW_KEY_EXCH_A;
  395. #endif
  396. s->init_num=0;
  397. break;
  398. case SSL3_ST_SW_KEY_EXCH_A:
  399. case SSL3_ST_SW_KEY_EXCH_B:
  400. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  401. /* clear this, it may get reset by
  402. * send_server_key_exchange */
  403. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  404. #ifndef OPENSSL_NO_KRB5
  405. && !(alg_k & SSL_kKRB5)
  406. #endif /* OPENSSL_NO_KRB5 */
  407. )
  408. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  409. * even when forbidden by protocol specs
  410. * (handshake may fail as clients are not required to
  411. * be able to handle this) */
  412. s->s3->tmp.use_rsa_tmp=1;
  413. else
  414. s->s3->tmp.use_rsa_tmp=0;
  415. /* only send if a DH key exchange or
  416. * RSA but we have a sign only certificate */
  417. if (s->s3->tmp.use_rsa_tmp
  418. /* PSK: send ServerKeyExchange if PSK identity
  419. * hint if provided */
  420. #ifndef OPENSSL_NO_PSK
  421. || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
  422. #endif
  423. || (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  424. || (alg_k & SSL_kEECDH)
  425. || ((alg_k & SSL_kRSA)
  426. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  427. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  428. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  429. )
  430. )
  431. )
  432. )
  433. {
  434. dtls1_start_timer(s);
  435. ret=dtls1_send_server_key_exchange(s);
  436. if (ret <= 0) goto end;
  437. }
  438. else
  439. skip=1;
  440. s->state=SSL3_ST_SW_CERT_REQ_A;
  441. s->init_num=0;
  442. break;
  443. case SSL3_ST_SW_CERT_REQ_A:
  444. case SSL3_ST_SW_CERT_REQ_B:
  445. if (/* don't request cert unless asked for it: */
  446. !(s->verify_mode & SSL_VERIFY_PEER) ||
  447. /* if SSL_VERIFY_CLIENT_ONCE is set,
  448. * don't request cert during re-negotiation: */
  449. ((s->session->peer != NULL) &&
  450. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  451. /* never request cert in anonymous ciphersuites
  452. * (see section "Certificate request" in SSL 3 drafts
  453. * and in RFC 2246): */
  454. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  455. /* ... except when the application insists on verification
  456. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  457. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  458. /* never request cert in Kerberos ciphersuites */
  459. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
  460. /* With normal PSK Certificates and
  461. * Certificate Requests are omitted */
  462. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  463. {
  464. /* no cert request */
  465. skip=1;
  466. s->s3->tmp.cert_request=0;
  467. s->state=SSL3_ST_SW_SRVR_DONE_A;
  468. #ifndef OPENSSL_NO_SCTP
  469. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  470. {
  471. s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
  472. s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  473. }
  474. #endif
  475. }
  476. else
  477. {
  478. s->s3->tmp.cert_request=1;
  479. dtls1_start_timer(s);
  480. ret=dtls1_send_certificate_request(s);
  481. if (ret <= 0) goto end;
  482. #ifndef NETSCAPE_HANG_BUG
  483. s->state=SSL3_ST_SW_SRVR_DONE_A;
  484. #ifndef OPENSSL_NO_SCTP
  485. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  486. {
  487. s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
  488. s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  489. }
  490. #endif
  491. #else
  492. s->state=SSL3_ST_SW_FLUSH;
  493. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  494. #ifndef OPENSSL_NO_SCTP
  495. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  496. {
  497. s->d1->next_state = s->s3->tmp.next_state;
  498. s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK;
  499. }
  500. #endif
  501. #endif
  502. s->init_num=0;
  503. }
  504. break;
  505. case SSL3_ST_SW_SRVR_DONE_A:
  506. case SSL3_ST_SW_SRVR_DONE_B:
  507. dtls1_start_timer(s);
  508. ret=dtls1_send_server_done(s);
  509. if (ret <= 0) goto end;
  510. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  511. s->state=SSL3_ST_SW_FLUSH;
  512. s->init_num=0;
  513. break;
  514. case SSL3_ST_SW_FLUSH:
  515. s->rwstate=SSL_WRITING;
  516. if (BIO_flush(s->wbio) <= 0)
  517. {
  518. /* If the write error was fatal, stop trying */
  519. if (!BIO_should_retry(s->wbio))
  520. {
  521. s->rwstate=SSL_NOTHING;
  522. s->state=s->s3->tmp.next_state;
  523. }
  524. ret= -1;
  525. goto end;
  526. }
  527. s->rwstate=SSL_NOTHING;
  528. s->state=s->s3->tmp.next_state;
  529. break;
  530. case SSL3_ST_SR_CERT_A:
  531. case SSL3_ST_SR_CERT_B:
  532. /* Check for second client hello (MS SGC) */
  533. ret = ssl3_check_client_hello(s);
  534. if (ret <= 0)
  535. goto end;
  536. if (ret == 2)
  537. {
  538. dtls1_stop_timer(s);
  539. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  540. }
  541. else {
  542. /* could be sent for a DH cert, even if we
  543. * have not asked for it :-) */
  544. ret=ssl3_get_client_certificate(s);
  545. if (ret <= 0) goto end;
  546. s->init_num=0;
  547. s->state=SSL3_ST_SR_KEY_EXCH_A;
  548. }
  549. break;
  550. case SSL3_ST_SR_KEY_EXCH_A:
  551. case SSL3_ST_SR_KEY_EXCH_B:
  552. ret=ssl3_get_client_key_exchange(s);
  553. if (ret <= 0) goto end;
  554. #ifndef OPENSSL_NO_SCTP
  555. /* Add new shared key for SCTP-Auth,
  556. * will be ignored if no SCTP used.
  557. */
  558. snprintf((char *) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
  559. DTLS1_SCTP_AUTH_LABEL);
  560. SSL_export_keying_material(s, sctpauthkey,
  561. sizeof(sctpauthkey), labelbuffer,
  562. sizeof(labelbuffer), NULL, 0, 0);
  563. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  564. sizeof(sctpauthkey), sctpauthkey);
  565. #endif
  566. s->state=SSL3_ST_SR_CERT_VRFY_A;
  567. s->init_num=0;
  568. if (ret == 2)
  569. {
  570. /* For the ECDH ciphersuites when
  571. * the client sends its ECDH pub key in
  572. * a certificate, the CertificateVerify
  573. * message is not sent.
  574. */
  575. s->state=SSL3_ST_SR_FINISHED_A;
  576. s->init_num = 0;
  577. }
  578. else
  579. {
  580. s->state=SSL3_ST_SR_CERT_VRFY_A;
  581. s->init_num=0;
  582. /* We need to get hashes here so if there is
  583. * a client cert, it can be verified */
  584. s->method->ssl3_enc->cert_verify_mac(s,
  585. NID_md5,
  586. &(s->s3->tmp.cert_verify_md[0]));
  587. s->method->ssl3_enc->cert_verify_mac(s,
  588. NID_sha1,
  589. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  590. }
  591. break;
  592. case SSL3_ST_SR_CERT_VRFY_A:
  593. case SSL3_ST_SR_CERT_VRFY_B:
  594. s->d1->change_cipher_spec_ok = 1;
  595. /* we should decide if we expected this one */
  596. ret=ssl3_get_cert_verify(s);
  597. if (ret <= 0) goto end;
  598. #ifndef OPENSSL_NO_SCTP
  599. if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
  600. state == SSL_ST_RENEGOTIATE)
  601. s->state=DTLS1_SCTP_ST_SR_READ_SOCK;
  602. else
  603. #endif
  604. s->state=SSL3_ST_SR_FINISHED_A;
  605. s->init_num=0;
  606. break;
  607. case SSL3_ST_SR_FINISHED_A:
  608. case SSL3_ST_SR_FINISHED_B:
  609. s->d1->change_cipher_spec_ok = 1;
  610. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  611. SSL3_ST_SR_FINISHED_B);
  612. if (ret <= 0) goto end;
  613. dtls1_stop_timer(s);
  614. if (s->hit)
  615. s->state=SSL_ST_OK;
  616. #ifndef OPENSSL_NO_TLSEXT
  617. else if (s->tlsext_ticket_expected)
  618. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  619. #endif
  620. else
  621. s->state=SSL3_ST_SW_CHANGE_A;
  622. s->init_num=0;
  623. break;
  624. #ifndef OPENSSL_NO_TLSEXT
  625. case SSL3_ST_SW_SESSION_TICKET_A:
  626. case SSL3_ST_SW_SESSION_TICKET_B:
  627. ret=dtls1_send_newsession_ticket(s);
  628. if (ret <= 0) goto end;
  629. s->state=SSL3_ST_SW_CHANGE_A;
  630. s->init_num=0;
  631. break;
  632. case SSL3_ST_SW_CERT_STATUS_A:
  633. case SSL3_ST_SW_CERT_STATUS_B:
  634. ret=ssl3_send_cert_status(s);
  635. if (ret <= 0) goto end;
  636. s->state=SSL3_ST_SW_KEY_EXCH_A;
  637. s->init_num=0;
  638. break;
  639. #endif
  640. case SSL3_ST_SW_CHANGE_A:
  641. case SSL3_ST_SW_CHANGE_B:
  642. s->session->cipher=s->s3->tmp.new_cipher;
  643. if (!s->method->ssl3_enc->setup_key_block(s))
  644. { ret= -1; goto end; }
  645. ret=dtls1_send_change_cipher_spec(s,
  646. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  647. if (ret <= 0) goto end;
  648. #ifndef OPENSSL_NO_SCTP
  649. /* Change to new shared key of SCTP-Auth,
  650. * will be ignored if no SCTP used.
  651. */
  652. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
  653. #endif
  654. s->state=SSL3_ST_SW_FINISHED_A;
  655. s->init_num=0;
  656. if (!s->method->ssl3_enc->change_cipher_state(s,
  657. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  658. {
  659. ret= -1;
  660. goto end;
  661. }
  662. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  663. break;
  664. case SSL3_ST_SW_FINISHED_A:
  665. case SSL3_ST_SW_FINISHED_B:
  666. ret=dtls1_send_finished(s,
  667. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  668. s->method->ssl3_enc->server_finished_label,
  669. s->method->ssl3_enc->server_finished_label_len);
  670. if (ret <= 0) goto end;
  671. s->state=SSL3_ST_SW_FLUSH;
  672. if (s->hit)
  673. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  674. else
  675. {
  676. s->s3->tmp.next_state=SSL_ST_OK;
  677. #ifndef OPENSSL_NO_SCTP
  678. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  679. {
  680. s->d1->next_state = s->s3->tmp.next_state;
  681. s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK;
  682. }
  683. #endif
  684. }
  685. s->init_num=0;
  686. break;
  687. case SSL_ST_OK:
  688. /* clean a few things up */
  689. ssl3_cleanup_key_block(s);
  690. #if 0
  691. BUF_MEM_free(s->init_buf);
  692. s->init_buf=NULL;
  693. #endif
  694. /* remove buffering on output */
  695. ssl_free_wbio_buffer(s);
  696. s->init_num=0;
  697. if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
  698. {
  699. s->renegotiate=0;
  700. s->new_session=0;
  701. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  702. s->ctx->stats.sess_accept_good++;
  703. /* s->server=1; */
  704. s->handshake_func=dtls1_accept;
  705. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  706. }
  707. ret = 1;
  708. /* done handshaking, next message is client hello */
  709. s->d1->handshake_read_seq = 0;
  710. /* next message is server hello */
  711. s->d1->handshake_write_seq = 0;
  712. s->d1->next_handshake_write_seq = 0;
  713. goto end;
  714. /* break; */
  715. default:
  716. SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
  717. ret= -1;
  718. goto end;
  719. /* break; */
  720. }
  721. if (!s->s3->tmp.reuse_message && !skip)
  722. {
  723. if (s->debug)
  724. {
  725. if ((ret=BIO_flush(s->wbio)) <= 0)
  726. goto end;
  727. }
  728. if ((cb != NULL) && (s->state != state))
  729. {
  730. new_state=s->state;
  731. s->state=state;
  732. cb(s,SSL_CB_ACCEPT_LOOP,1);
  733. s->state=new_state;
  734. }
  735. }
  736. skip=0;
  737. }
  738. end:
  739. /* BIO_flush(s->wbio); */
  740. s->in_handshake--;
  741. #ifndef OPENSSL_NO_SCTP
  742. /* Notify SCTP BIO socket to leave handshake
  743. * mode and prevent stream identifier other
  744. * than 0. Will be ignored if no SCTP is used.
  745. */
  746. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
  747. #endif
  748. if (cb != NULL)
  749. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  750. return(ret);
  751. }
  752. int dtls1_send_hello_request(SSL *s)
  753. {
  754. unsigned char *p;
  755. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  756. {
  757. p=(unsigned char *)s->init_buf->data;
  758. p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
  759. s->state=SSL3_ST_SW_HELLO_REQ_B;
  760. /* number of bytes to write */
  761. s->init_num=DTLS1_HM_HEADER_LENGTH;
  762. s->init_off=0;
  763. /* no need to buffer this message, since there are no retransmit
  764. * requests for it */
  765. }
  766. /* SSL3_ST_SW_HELLO_REQ_B */
  767. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  768. }
  769. int dtls1_send_hello_verify_request(SSL *s)
  770. {
  771. unsigned int msg_len;
  772. unsigned char *msg, *buf, *p;
  773. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
  774. {
  775. buf = (unsigned char *)s->init_buf->data;
  776. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  777. *(p++) = s->version >> 8;
  778. *(p++) = s->version & 0xFF;
  779. if (s->ctx->app_gen_cookie_cb == NULL ||
  780. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  781. &(s->d1->cookie_len)) == 0)
  782. {
  783. SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
  784. return 0;
  785. }
  786. *(p++) = (unsigned char) s->d1->cookie_len;
  787. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  788. p += s->d1->cookie_len;
  789. msg_len = p - msg;
  790. dtls1_set_message_header(s, buf,
  791. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
  792. s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  793. /* number of bytes to write */
  794. s->init_num=p-buf;
  795. s->init_off=0;
  796. }
  797. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  798. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  799. }
  800. int dtls1_send_server_hello(SSL *s)
  801. {
  802. unsigned char *buf;
  803. unsigned char *p,*d;
  804. int i;
  805. unsigned int sl;
  806. unsigned long l,Time;
  807. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  808. {
  809. buf=(unsigned char *)s->init_buf->data;
  810. p=s->s3->server_random;
  811. Time=(unsigned long)time(NULL); /* Time */
  812. l2n(Time,p);
  813. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4);
  814. /* Do the message type and length last */
  815. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  816. *(p++)=s->version>>8;
  817. *(p++)=s->version&0xff;
  818. /* Random stuff */
  819. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  820. p+=SSL3_RANDOM_SIZE;
  821. /* now in theory we have 3 options to sending back the
  822. * session id. If it is a re-use, we send back the
  823. * old session-id, if it is a new session, we send
  824. * back the new session-id or we send back a 0 length
  825. * session-id if we want it to be single use.
  826. * Currently I will not implement the '0' length session-id
  827. * 12-Jan-98 - I'll now support the '0' length stuff.
  828. */
  829. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  830. s->session->session_id_length=0;
  831. sl=s->session->session_id_length;
  832. if (sl > sizeof s->session->session_id)
  833. {
  834. SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  835. return -1;
  836. }
  837. *(p++)=sl;
  838. memcpy(p,s->session->session_id,sl);
  839. p+=sl;
  840. /* put the cipher */
  841. if (s->s3->tmp.new_cipher == NULL)
  842. return -1;
  843. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  844. p+=i;
  845. /* put the compression method */
  846. #ifdef OPENSSL_NO_COMP
  847. *(p++)=0;
  848. #else
  849. if (s->s3->tmp.new_compression == NULL)
  850. *(p++)=0;
  851. else
  852. *(p++)=s->s3->tmp.new_compression->id;
  853. #endif
  854. #ifndef OPENSSL_NO_TLSEXT
  855. if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  856. {
  857. SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
  858. return -1;
  859. }
  860. #endif
  861. /* do the header */
  862. l=(p-d);
  863. d=buf;
  864. d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
  865. s->state=SSL3_ST_SW_SRVR_HELLO_B;
  866. /* number of bytes to write */
  867. s->init_num=p-buf;
  868. s->init_off=0;
  869. /* buffer the message to handle re-xmits */
  870. dtls1_buffer_message(s, 0);
  871. }
  872. /* SSL3_ST_SW_SRVR_HELLO_B */
  873. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  874. }
  875. int dtls1_send_server_done(SSL *s)
  876. {
  877. unsigned char *p;
  878. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  879. {
  880. p=(unsigned char *)s->init_buf->data;
  881. /* do the header */
  882. p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
  883. s->state=SSL3_ST_SW_SRVR_DONE_B;
  884. /* number of bytes to write */
  885. s->init_num=DTLS1_HM_HEADER_LENGTH;
  886. s->init_off=0;
  887. /* buffer the message to handle re-xmits */
  888. dtls1_buffer_message(s, 0);
  889. }
  890. /* SSL3_ST_SW_SRVR_DONE_B */
  891. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  892. }
  893. int dtls1_send_server_key_exchange(SSL *s)
  894. {
  895. #ifndef OPENSSL_NO_RSA
  896. unsigned char *q;
  897. int j,num;
  898. RSA *rsa;
  899. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  900. unsigned int u;
  901. #endif
  902. #ifndef OPENSSL_NO_DH
  903. DH *dh=NULL,*dhp;
  904. #endif
  905. #ifndef OPENSSL_NO_ECDH
  906. EC_KEY *ecdh=NULL, *ecdhp;
  907. unsigned char *encodedPoint = NULL;
  908. int encodedlen = 0;
  909. int curve_id = 0;
  910. BN_CTX *bn_ctx = NULL;
  911. #endif
  912. EVP_PKEY *pkey;
  913. unsigned char *p,*d;
  914. int al,i;
  915. unsigned long type;
  916. int n;
  917. CERT *cert;
  918. BIGNUM *r[4];
  919. int nr[4],kn;
  920. BUF_MEM *buf;
  921. EVP_MD_CTX md_ctx;
  922. EVP_MD_CTX_init(&md_ctx);
  923. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  924. {
  925. type=s->s3->tmp.new_cipher->algorithm_mkey;
  926. cert=s->cert;
  927. buf=s->init_buf;
  928. r[0]=r[1]=r[2]=r[3]=NULL;
  929. n=0;
  930. #ifndef OPENSSL_NO_RSA
  931. if (type & SSL_kRSA)
  932. {
  933. rsa=cert->rsa_tmp;
  934. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  935. {
  936. rsa=s->cert->rsa_tmp_cb(s,
  937. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  938. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  939. if(rsa == NULL)
  940. {
  941. al=SSL_AD_HANDSHAKE_FAILURE;
  942. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  943. goto f_err;
  944. }
  945. RSA_up_ref(rsa);
  946. cert->rsa_tmp=rsa;
  947. }
  948. if (rsa == NULL)
  949. {
  950. al=SSL_AD_HANDSHAKE_FAILURE;
  951. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  952. goto f_err;
  953. }
  954. r[0]=rsa->n;
  955. r[1]=rsa->e;
  956. s->s3->tmp.use_rsa_tmp=1;
  957. }
  958. else
  959. #endif
  960. #ifndef OPENSSL_NO_DH
  961. if (type & SSL_kEDH)
  962. {
  963. dhp=cert->dh_tmp;
  964. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  965. dhp=s->cert->dh_tmp_cb(s,
  966. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  967. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  968. if (dhp == NULL)
  969. {
  970. al=SSL_AD_HANDSHAKE_FAILURE;
  971. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  972. goto f_err;
  973. }
  974. if (s->s3->tmp.dh != NULL)
  975. {
  976. DH_free(dh);
  977. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  978. goto err;
  979. }
  980. if ((dh=DHparams_dup(dhp)) == NULL)
  981. {
  982. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  983. goto err;
  984. }
  985. s->s3->tmp.dh=dh;
  986. if ((dhp->pub_key == NULL ||
  987. dhp->priv_key == NULL ||
  988. (s->options & SSL_OP_SINGLE_DH_USE)))
  989. {
  990. if(!DH_generate_key(dh))
  991. {
  992. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
  993. ERR_R_DH_LIB);
  994. goto err;
  995. }
  996. }
  997. else
  998. {
  999. dh->pub_key=BN_dup(dhp->pub_key);
  1000. dh->priv_key=BN_dup(dhp->priv_key);
  1001. if ((dh->pub_key == NULL) ||
  1002. (dh->priv_key == NULL))
  1003. {
  1004. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1005. goto err;
  1006. }
  1007. }
  1008. r[0]=dh->p;
  1009. r[1]=dh->g;
  1010. r[2]=dh->pub_key;
  1011. }
  1012. else
  1013. #endif
  1014. #ifndef OPENSSL_NO_ECDH
  1015. if (type & SSL_kEECDH)
  1016. {
  1017. const EC_GROUP *group;
  1018. ecdhp=cert->ecdh_tmp;
  1019. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  1020. {
  1021. ecdhp=s->cert->ecdh_tmp_cb(s,
  1022. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1023. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1024. }
  1025. if (ecdhp == NULL)
  1026. {
  1027. al=SSL_AD_HANDSHAKE_FAILURE;
  1028. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1029. goto f_err;
  1030. }
  1031. if (s->s3->tmp.ecdh != NULL)
  1032. {
  1033. EC_KEY_free(s->s3->tmp.ecdh);
  1034. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1035. goto err;
  1036. }
  1037. /* Duplicate the ECDH structure. */
  1038. if (ecdhp == NULL)
  1039. {
  1040. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1041. goto err;
  1042. }
  1043. if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
  1044. {
  1045. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1046. goto err;
  1047. }
  1048. s->s3->tmp.ecdh=ecdh;
  1049. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1050. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1051. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1052. {
  1053. if(!EC_KEY_generate_key(ecdh))
  1054. {
  1055. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1056. goto err;
  1057. }
  1058. }
  1059. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1060. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1061. (EC_KEY_get0_private_key(ecdh) == NULL))
  1062. {
  1063. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1064. goto err;
  1065. }
  1066. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1067. (EC_GROUP_get_degree(group) > 163))
  1068. {
  1069. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1070. goto err;
  1071. }
  1072. /* XXX: For now, we only support ephemeral ECDH
  1073. * keys over named (not generic) curves. For
  1074. * supported named curves, curve_id is non-zero.
  1075. */
  1076. if ((curve_id =
  1077. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  1078. == 0)
  1079. {
  1080. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1081. goto err;
  1082. }
  1083. /* Encode the public key.
  1084. * First check the size of encoding and
  1085. * allocate memory accordingly.
  1086. */
  1087. encodedlen = EC_POINT_point2oct(group,
  1088. EC_KEY_get0_public_key(ecdh),
  1089. POINT_CONVERSION_UNCOMPRESSED,
  1090. NULL, 0, NULL);
  1091. encodedPoint = (unsigned char *)
  1092. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1093. bn_ctx = BN_CTX_new();
  1094. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1095. {
  1096. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1097. goto err;
  1098. }
  1099. encodedlen = EC_POINT_point2oct(group,
  1100. EC_KEY_get0_public_key(ecdh),
  1101. POINT_CONVERSION_UNCOMPRESSED,
  1102. encodedPoint, encodedlen, bn_ctx);
  1103. if (encodedlen == 0)
  1104. {
  1105. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1106. goto err;
  1107. }
  1108. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1109. /* XXX: For now, we only support named (not
  1110. * generic) curves in ECDH ephemeral key exchanges.
  1111. * In this situation, we need four additional bytes
  1112. * to encode the entire ServerECDHParams
  1113. * structure.
  1114. */
  1115. n = 4 + encodedlen;
  1116. /* We'll generate the serverKeyExchange message
  1117. * explicitly so we can set these to NULLs
  1118. */
  1119. r[0]=NULL;
  1120. r[1]=NULL;
  1121. r[2]=NULL;
  1122. r[3]=NULL;
  1123. }
  1124. else
  1125. #endif /* !OPENSSL_NO_ECDH */
  1126. #ifndef OPENSSL_NO_PSK
  1127. if (type & SSL_kPSK)
  1128. {
  1129. /* reserve size for record length and PSK identity hint*/
  1130. n+=2+strlen(s->ctx->psk_identity_hint);
  1131. }
  1132. else
  1133. #endif /* !OPENSSL_NO_PSK */
  1134. {
  1135. al=SSL_AD_HANDSHAKE_FAILURE;
  1136. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1137. goto f_err;
  1138. }
  1139. for (i=0; r[i] != NULL; i++)
  1140. {
  1141. nr[i]=BN_num_bytes(r[i]);
  1142. n+=2+nr[i];
  1143. }
  1144. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1145. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  1146. {
  1147. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher, NULL))
  1148. == NULL)
  1149. {
  1150. al=SSL_AD_DECODE_ERROR;
  1151. goto f_err;
  1152. }
  1153. kn=EVP_PKEY_size(pkey);
  1154. }
  1155. else
  1156. {
  1157. pkey=NULL;
  1158. kn=0;
  1159. }
  1160. if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
  1161. {
  1162. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1163. goto err;
  1164. }
  1165. d=(unsigned char *)s->init_buf->data;
  1166. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  1167. for (i=0; r[i] != NULL; i++)
  1168. {
  1169. s2n(nr[i],p);
  1170. BN_bn2bin(r[i],p);
  1171. p+=nr[i];
  1172. }
  1173. #ifndef OPENSSL_NO_ECDH
  1174. if (type & SSL_kEECDH)
  1175. {
  1176. /* XXX: For now, we only support named (not generic) curves.
  1177. * In this situation, the serverKeyExchange message has:
  1178. * [1 byte CurveType], [2 byte CurveName]
  1179. * [1 byte length of encoded point], followed by
  1180. * the actual encoded point itself
  1181. */
  1182. *p = NAMED_CURVE_TYPE;
  1183. p += 1;
  1184. *p = 0;
  1185. p += 1;
  1186. *p = curve_id;
  1187. p += 1;
  1188. *p = encodedlen;
  1189. p += 1;
  1190. memcpy((unsigned char*)p,
  1191. (unsigned char *)encodedPoint,
  1192. encodedlen);
  1193. OPENSSL_free(encodedPoint);
  1194. p += encodedlen;
  1195. }
  1196. #endif
  1197. #ifndef OPENSSL_NO_PSK
  1198. if (type & SSL_kPSK)
  1199. {
  1200. /* copy PSK identity hint */
  1201. s2n(strlen(s->ctx->psk_identity_hint), p);
  1202. strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
  1203. p+=strlen(s->ctx->psk_identity_hint);
  1204. }
  1205. #endif
  1206. /* not anonymous */
  1207. if (pkey != NULL)
  1208. {
  1209. /* n is the length of the params, they start at
  1210. * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space
  1211. * at the end. */
  1212. #ifndef OPENSSL_NO_RSA
  1213. if (pkey->type == EVP_PKEY_RSA)
  1214. {
  1215. q=md_buf;
  1216. j=0;
  1217. for (num=2; num > 0; num--)
  1218. {
  1219. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1220. ?s->ctx->md5:s->ctx->sha1, NULL);
  1221. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1222. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1223. EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  1224. EVP_DigestFinal_ex(&md_ctx,q,
  1225. (unsigned int *)&i);
  1226. q+=i;
  1227. j+=i;
  1228. }
  1229. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1230. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1231. {
  1232. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1233. goto err;
  1234. }
  1235. s2n(u,p);
  1236. n+=u+2;
  1237. }
  1238. else
  1239. #endif
  1240. #if !defined(OPENSSL_NO_DSA)
  1241. if (pkey->type == EVP_PKEY_DSA)
  1242. {
  1243. /* lets do DSS */
  1244. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1245. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1246. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1247. EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  1248. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1249. (unsigned int *)&i,pkey))
  1250. {
  1251. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1252. goto err;
  1253. }
  1254. s2n(i,p);
  1255. n+=i+2;
  1256. }
  1257. else
  1258. #endif
  1259. #if !defined(OPENSSL_NO_ECDSA)
  1260. if (pkey->type == EVP_PKEY_EC)
  1261. {
  1262. /* let's do ECDSA */
  1263. EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1264. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1265. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1266. EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  1267. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1268. (unsigned int *)&i,pkey))
  1269. {
  1270. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1271. goto err;
  1272. }
  1273. s2n(i,p);
  1274. n+=i+2;
  1275. }
  1276. else
  1277. #endif
  1278. {
  1279. /* Is this error check actually needed? */
  1280. al=SSL_AD_HANDSHAKE_FAILURE;
  1281. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1282. goto f_err;
  1283. }
  1284. }
  1285. d = dtls1_set_message_header(s, d,
  1286. SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
  1287. /* we should now have things packed up, so lets send
  1288. * it off */
  1289. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1290. s->init_off=0;
  1291. /* buffer the message to handle re-xmits */
  1292. dtls1_buffer_message(s, 0);
  1293. }
  1294. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1295. EVP_MD_CTX_cleanup(&md_ctx);
  1296. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1297. f_err:
  1298. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1299. err:
  1300. #ifndef OPENSSL_NO_ECDH
  1301. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1302. BN_CTX_free(bn_ctx);
  1303. #endif
  1304. EVP_MD_CTX_cleanup(&md_ctx);
  1305. return(-1);
  1306. }
  1307. int dtls1_send_certificate_request(SSL *s)
  1308. {
  1309. unsigned char *p,*d;
  1310. int i,j,nl,off,n;
  1311. STACK_OF(X509_NAME) *sk=NULL;
  1312. X509_NAME *name;
  1313. BUF_MEM *buf;
  1314. unsigned int msg_len;
  1315. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1316. {
  1317. buf=s->init_buf;
  1318. d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
  1319. /* get the list of acceptable cert types */
  1320. p++;
  1321. n=ssl3_get_req_cert_type(s,p);
  1322. d[0]=n;
  1323. p+=n;
  1324. n++;
  1325. off=n;
  1326. p+=2;
  1327. n+=2;
  1328. sk=SSL_get_client_CA_list(s);
  1329. nl=0;
  1330. if (sk != NULL)
  1331. {
  1332. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1333. {
  1334. name=sk_X509_NAME_value(sk,i);
  1335. j=i2d_X509_NAME(name,NULL);
  1336. if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2))
  1337. {
  1338. SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1339. goto err;
  1340. }
  1341. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]);
  1342. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1343. {
  1344. s2n(j,p);
  1345. i2d_X509_NAME(name,&p);
  1346. n+=2+j;
  1347. nl+=2+j;
  1348. }
  1349. else
  1350. {
  1351. d=p;
  1352. i2d_X509_NAME(name,&p);
  1353. j-=2; s2n(j,d); j+=2;
  1354. n+=j;
  1355. nl+=j;
  1356. }
  1357. }
  1358. }
  1359. /* else no CA names */
  1360. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]);
  1361. s2n(nl,p);
  1362. d=(unsigned char *)buf->data;
  1363. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1364. l2n3(n,d);
  1365. s2n(s->d1->handshake_write_seq,d);
  1366. s->d1->handshake_write_seq++;
  1367. /* we should now have things packed up, so lets send
  1368. * it off */
  1369. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1370. s->init_off=0;
  1371. #ifdef NETSCAPE_HANG_BUG
  1372. /* XXX: what to do about this? */
  1373. p=(unsigned char *)s->init_buf->data + s->init_num;
  1374. /* do the header */
  1375. *(p++)=SSL3_MT_SERVER_DONE;
  1376. *(p++)=0;
  1377. *(p++)=0;
  1378. *(p++)=0;
  1379. s->init_num += 4;
  1380. #endif
  1381. /* XDTLS: set message header ? */
  1382. msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
  1383. dtls1_set_message_header(s, (void *)s->init_buf->data,
  1384. SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len);
  1385. /* buffer the message to handle re-xmits */
  1386. dtls1_buffer_message(s, 0);
  1387. s->state = SSL3_ST_SW_CERT_REQ_B;
  1388. }
  1389. /* SSL3_ST_SW_CERT_REQ_B */
  1390. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1391. err:
  1392. return(-1);
  1393. }
  1394. int dtls1_send_server_certificate(SSL *s)
  1395. {
  1396. unsigned long l;
  1397. X509 *x;
  1398. if (s->state == SSL3_ST_SW_CERT_A)
  1399. {
  1400. x=ssl_get_server_send_cert(s);
  1401. if (x == NULL)
  1402. {
  1403. /* VRS: allow null cert if auth == KRB5 */
  1404. if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
  1405. (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5))
  1406. {
  1407. SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  1408. return(0);
  1409. }
  1410. }
  1411. l=dtls1_output_cert_chain(s,x);
  1412. s->state=SSL3_ST_SW_CERT_B;
  1413. s->init_num=(int)l;
  1414. s->init_off=0;
  1415. /* buffer the message to handle re-xmits */
  1416. dtls1_buffer_message(s, 0);
  1417. }
  1418. /* SSL3_ST_SW_CERT_B */
  1419. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1420. }
  1421. #ifndef OPENSSL_NO_TLSEXT
  1422. int dtls1_send_newsession_ticket(SSL *s)
  1423. {
  1424. if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
  1425. {
  1426. unsigned char *p, *senc, *macstart;
  1427. int len, slen;
  1428. unsigned int hlen, msg_len;
  1429. EVP_CIPHER_CTX ctx;
  1430. HMAC_CTX hctx;
  1431. SSL_CTX *tctx = s->initial_ctx;
  1432. unsigned char iv[EVP_MAX_IV_LENGTH];
  1433. unsigned char key_name[16];
  1434. /* get session encoding length */
  1435. slen = i2d_SSL_SESSION(s->session, NULL);
  1436. /* Some length values are 16 bits, so forget it if session is
  1437. * too long
  1438. */
  1439. if (slen > 0xFF00)
  1440. return -1;
  1441. /* Grow buffer if need be: the length calculation is as
  1442. * follows 12 (DTLS handshake message header) +
  1443. * 4 (ticket lifetime hint) + 2 (ticket length) +
  1444. * 16 (key name) + max_iv_len (iv length) +
  1445. * session_length + max_enc_block_size (max encrypted session
  1446. * length) + max_md_size (HMAC).
  1447. */
  1448. if (!BUF_MEM_grow(s->init_buf,
  1449. DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
  1450. EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
  1451. return -1;
  1452. senc = OPENSSL_malloc(slen);
  1453. if (!senc)
  1454. return -1;
  1455. p = senc;
  1456. i2d_SSL_SESSION(s->session, &p);
  1457. p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
  1458. EVP_CIPHER_CTX_init(&ctx);
  1459. HMAC_CTX_init(&hctx);
  1460. /* Initialize HMAC and cipher contexts. If callback present
  1461. * it does all the work otherwise use generated values
  1462. * from parent ctx.
  1463. */
  1464. if (tctx->tlsext_ticket_key_cb)
  1465. {
  1466. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  1467. &hctx, 1) < 0)
  1468. {
  1469. OPENSSL_free(senc);
  1470. return -1;
  1471. }
  1472. }
  1473. else
  1474. {
  1475. RAND_pseudo_bytes(iv, 16);
  1476. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1477. tctx->tlsext_tick_aes_key, iv);
  1478. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  1479. tlsext_tick_md(), NULL);
  1480. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  1481. }
  1482. l2n(s->session->tlsext_tick_lifetime_hint, p);
  1483. /* Skip ticket length for now */
  1484. p += 2;
  1485. /* Output key name */
  1486. macstart = p;
  1487. memcpy(p, key_name, 16);
  1488. p += 16;
  1489. /* output IV */
  1490. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  1491. p += EVP_CIPHER_CTX_iv_length(&ctx);
  1492. /* Encrypt session data */
  1493. EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
  1494. p += len;
  1495. EVP_EncryptFinal(&ctx, p, &len);
  1496. p += len;
  1497. EVP_CIPHER_CTX_cleanup(&ctx);
  1498. HMAC_Update(&hctx, macstart, p - macstart);
  1499. HMAC_Final(&hctx, p, &hlen);
  1500. HMAC_CTX_cleanup(&hctx);
  1501. p += hlen;
  1502. /* Now write out lengths: p points to end of data written */
  1503. /* Total length */
  1504. len = p - (unsigned char *)(s->init_buf->data);
  1505. /* Ticket length */
  1506. p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
  1507. s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
  1508. /* number of bytes to write */
  1509. s->init_num= len;
  1510. s->state=SSL3_ST_SW_SESSION_TICKET_B;
  1511. s->init_off=0;
  1512. OPENSSL_free(senc);
  1513. /* XDTLS: set message header ? */
  1514. msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
  1515. dtls1_set_message_header(s, (void *)s->init_buf->data,
  1516. SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len);
  1517. /* buffer the message to handle re-xmits */
  1518. dtls1_buffer_message(s, 0);
  1519. }
  1520. /* SSL3_ST_SW_SESSION_TICKET_B */
  1521. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1522. }
  1523. #endif