d1_srvr.c 44 KB

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