d1_srvr.c 59 KB

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