d1_srvr.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /* ssl/d1_srvr.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * (nagendra@cs.stanford.edu) 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. * openssl-core@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  60. * All rights reserved.
  61. *
  62. * This package is an SSL implementation written
  63. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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 == DTLS_ANY_VERSION)
  132. return DTLS_server_method();
  133. else if (ver == DTLS1_VERSION)
  134. return DTLSv1_server_method();
  135. else if (ver == DTLS1_2_VERSION)
  136. return DTLSv1_2_server_method();
  137. else
  138. return NULL;
  139. }
  140. IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
  141. DTLSv1_server_method,
  142. dtls1_accept,
  143. ssl_undefined_function,
  144. dtls1_get_server_method, DTLSv1_enc_data)
  145. IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION,
  146. DTLSv1_2_server_method,
  147. dtls1_accept,
  148. ssl_undefined_function,
  149. dtls1_get_server_method, DTLSv1_2_enc_data)
  150. IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION,
  151. DTLS_server_method,
  152. dtls1_accept,
  153. ssl_undefined_function,
  154. dtls1_get_server_method, DTLSv1_2_enc_data)
  155. int dtls1_accept(SSL *s)
  156. {
  157. BUF_MEM *buf;
  158. unsigned long Time = (unsigned long)time(NULL);
  159. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  160. unsigned long alg_k;
  161. int ret = -1;
  162. int new_state, state, skip = 0;
  163. int listen;
  164. #ifndef OPENSSL_NO_SCTP
  165. unsigned char sctpauthkey[64];
  166. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  167. #endif
  168. RAND_add(&Time, sizeof(Time), 0);
  169. ERR_clear_error();
  170. clear_sys_error();
  171. if (s->info_callback != NULL)
  172. cb = s->info_callback;
  173. else if (s->ctx->info_callback != NULL)
  174. cb = s->ctx->info_callback;
  175. listen = s->d1->listen;
  176. /* init things to blank */
  177. s->in_handshake++;
  178. if (!SSL_in_init(s) || SSL_in_before(s))
  179. SSL_clear(s);
  180. s->d1->listen = listen;
  181. #ifndef OPENSSL_NO_SCTP
  182. /*
  183. * Notify SCTP BIO socket to enter handshake mode and prevent stream
  184. * identifier other than 0. Will be ignored if no SCTP is used.
  185. */
  186. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
  187. s->in_handshake, NULL);
  188. #endif
  189. if (s->cert == NULL) {
  190. SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
  191. return (-1);
  192. }
  193. #ifndef OPENSSL_NO_HEARTBEATS
  194. /*
  195. * If we're awaiting a HeartbeatResponse, pretend we already got and
  196. * don't await it anymore, because Heartbeats don't make sense during
  197. * handshakes anyway.
  198. */
  199. if (s->tlsext_hb_pending) {
  200. dtls1_stop_timer(s);
  201. s->tlsext_hb_pending = 0;
  202. s->tlsext_hb_seq++;
  203. }
  204. #endif
  205. for (;;) {
  206. state = s->state;
  207. switch (s->state) {
  208. case SSL_ST_RENEGOTIATE:
  209. s->renegotiate = 1;
  210. /* s->state=SSL_ST_ACCEPT; */
  211. case SSL_ST_BEFORE:
  212. case SSL_ST_ACCEPT:
  213. case SSL_ST_BEFORE | SSL_ST_ACCEPT:
  214. case SSL_ST_OK | SSL_ST_ACCEPT:
  215. s->server = 1;
  216. if (cb != NULL)
  217. cb(s, SSL_CB_HANDSHAKE_START, 1);
  218. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
  219. SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
  220. return -1;
  221. }
  222. s->type = SSL_ST_ACCEPT;
  223. if (s->init_buf == NULL) {
  224. if ((buf = BUF_MEM_new()) == NULL) {
  225. ret = -1;
  226. s->state = SSL_ST_ERR;
  227. goto end;
  228. }
  229. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  230. BUF_MEM_free(buf);
  231. ret = -1;
  232. s->state = SSL_ST_ERR;
  233. goto end;
  234. }
  235. s->init_buf = buf;
  236. }
  237. if (!ssl3_setup_buffers(s)) {
  238. ret = -1;
  239. s->state = SSL_ST_ERR;
  240. goto end;
  241. }
  242. s->init_num = 0;
  243. s->d1->change_cipher_spec_ok = 0;
  244. /*
  245. * Should have been reset by ssl3_get_finished, too.
  246. */
  247. s->s3->change_cipher_spec = 0;
  248. if (s->state != SSL_ST_RENEGOTIATE) {
  249. /*
  250. * Ok, we now need to push on a buffering BIO so that the
  251. * output is sent in a way that TCP likes :-) ...but not with
  252. * SCTP :-)
  253. */
  254. #ifndef OPENSSL_NO_SCTP
  255. if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
  256. #endif
  257. if (!ssl_init_wbio_buffer(s, 1)) {
  258. ret = -1;
  259. s->state = SSL_ST_ERR;
  260. goto end;
  261. }
  262. if (!ssl3_init_finished_mac(s)) {
  263. ret = -1;
  264. s->state = SSL_ST_ERR;
  265. goto end;
  266. }
  267. s->state = SSL3_ST_SR_CLNT_HELLO_A;
  268. s->ctx->stats.sess_accept++;
  269. } else if (!s->s3->send_connection_binding &&
  270. !(s->options &
  271. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  272. /*
  273. * Server attempting to renegotiate with client that doesn't
  274. * support secure renegotiation.
  275. */
  276. SSLerr(SSL_F_DTLS1_ACCEPT,
  277. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  278. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  279. ret = -1;
  280. s->state = SSL_ST_ERR;
  281. goto end;
  282. } else {
  283. /*
  284. * s->state == SSL_ST_RENEGOTIATE, we will just send a
  285. * HelloRequest
  286. */
  287. s->ctx->stats.sess_accept_renegotiate++;
  288. s->state = SSL3_ST_SW_HELLO_REQ_A;
  289. }
  290. break;
  291. case SSL3_ST_SW_HELLO_REQ_A:
  292. case SSL3_ST_SW_HELLO_REQ_B:
  293. s->shutdown = 0;
  294. dtls1_clear_sent_buffer(s);
  295. dtls1_start_timer(s);
  296. ret = ssl3_send_hello_request(s);
  297. if (ret <= 0)
  298. goto end;
  299. s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
  300. s->state = SSL3_ST_SW_FLUSH;
  301. s->init_num = 0;
  302. if (!ssl3_init_finished_mac(s)) {
  303. ret = -1;
  304. s->state = SSL_ST_ERR;
  305. goto end;
  306. }
  307. break;
  308. case SSL3_ST_SW_HELLO_REQ_C:
  309. s->state = SSL_ST_OK;
  310. break;
  311. case SSL3_ST_SR_CLNT_HELLO_A:
  312. case SSL3_ST_SR_CLNT_HELLO_B:
  313. case SSL3_ST_SR_CLNT_HELLO_C:
  314. s->shutdown = 0;
  315. ret = ssl3_get_client_hello(s);
  316. if (ret <= 0)
  317. goto end;
  318. dtls1_stop_timer(s);
  319. if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  320. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
  321. else
  322. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  323. s->init_num = 0;
  324. /* If we're just listening, stop here */
  325. if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) {
  326. ret = 2;
  327. s->d1->listen = 0;
  328. /*
  329. * Set expected sequence numbers to continue the handshake.
  330. */
  331. s->d1->handshake_read_seq = 2;
  332. s->d1->handshake_write_seq = 1;
  333. s->d1->next_handshake_write_seq = 1;
  334. goto end;
  335. }
  336. break;
  337. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
  338. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
  339. ret = dtls1_send_hello_verify_request(s);
  340. if (ret <= 0)
  341. goto end;
  342. s->state = SSL3_ST_SW_FLUSH;
  343. s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
  344. /* HelloVerifyRequest resets Finished MAC */
  345. if (s->version != DTLS1_BAD_VER)
  346. if (!ssl3_init_finished_mac(s)) {
  347. ret = -1;
  348. s->state = SSL_ST_ERR;
  349. goto end;
  350. }
  351. break;
  352. #ifndef OPENSSL_NO_SCTP
  353. case DTLS1_SCTP_ST_SR_READ_SOCK:
  354. if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  355. s->s3->in_read_app_data = 2;
  356. s->rwstate = SSL_READING;
  357. BIO_clear_retry_flags(SSL_get_rbio(s));
  358. BIO_set_retry_read(SSL_get_rbio(s));
  359. ret = -1;
  360. goto end;
  361. }
  362. s->state = SSL3_ST_SR_FINISHED_A;
  363. break;
  364. case DTLS1_SCTP_ST_SW_WRITE_SOCK:
  365. ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
  366. if (ret < 0)
  367. goto end;
  368. if (ret == 0) {
  369. if (s->d1->next_state != SSL_ST_OK) {
  370. s->s3->in_read_app_data = 2;
  371. s->rwstate = SSL_READING;
  372. BIO_clear_retry_flags(SSL_get_rbio(s));
  373. BIO_set_retry_read(SSL_get_rbio(s));
  374. ret = -1;
  375. goto end;
  376. }
  377. }
  378. s->state = s->d1->next_state;
  379. break;
  380. #endif
  381. case SSL3_ST_SW_SRVR_HELLO_A:
  382. case SSL3_ST_SW_SRVR_HELLO_B:
  383. s->renegotiate = 2;
  384. dtls1_start_timer(s);
  385. ret = ssl3_send_server_hello(s);
  386. if (ret <= 0)
  387. goto end;
  388. if (s->hit) {
  389. #ifndef OPENSSL_NO_SCTP
  390. /*
  391. * Add new shared key for SCTP-Auth, will be ignored if no
  392. * SCTP used.
  393. */
  394. snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
  395. DTLS1_SCTP_AUTH_LABEL);
  396. if (SSL_export_keying_material(s, sctpauthkey,
  397. sizeof(sctpauthkey), labelbuffer,
  398. sizeof(labelbuffer), NULL, 0, 0) <= 0) {
  399. ret = -1;
  400. s->state = SSL_ST_ERR;
  401. goto end;
  402. }
  403. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  404. sizeof(sctpauthkey), sctpauthkey);
  405. #endif
  406. #ifndef OPENSSL_NO_TLSEXT
  407. if (s->tlsext_ticket_expected)
  408. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  409. else
  410. s->state = SSL3_ST_SW_CHANGE_A;
  411. #else
  412. s->state = SSL3_ST_SW_CHANGE_A;
  413. #endif
  414. } else
  415. s->state = SSL3_ST_SW_CERT_A;
  416. s->init_num = 0;
  417. break;
  418. case SSL3_ST_SW_CERT_A:
  419. case SSL3_ST_SW_CERT_B:
  420. /* Check if it is anon DH or normal PSK */
  421. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  422. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  423. dtls1_start_timer(s);
  424. ret = ssl3_send_server_certificate(s);
  425. if (ret <= 0)
  426. goto end;
  427. #ifndef OPENSSL_NO_TLSEXT
  428. if (s->tlsext_status_expected)
  429. s->state = SSL3_ST_SW_CERT_STATUS_A;
  430. else
  431. s->state = SSL3_ST_SW_KEY_EXCH_A;
  432. } else {
  433. skip = 1;
  434. s->state = SSL3_ST_SW_KEY_EXCH_A;
  435. }
  436. #else
  437. } else
  438. skip = 1;
  439. s->state = SSL3_ST_SW_KEY_EXCH_A;
  440. #endif
  441. s->init_num = 0;
  442. break;
  443. case SSL3_ST_SW_KEY_EXCH_A:
  444. case SSL3_ST_SW_KEY_EXCH_B:
  445. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  446. /*
  447. * clear this, it may get reset by
  448. * send_server_key_exchange
  449. */
  450. s->s3->tmp.use_rsa_tmp = 0;
  451. /*
  452. * only send if a DH key exchange or RSA but we have a sign only
  453. * certificate
  454. */
  455. if (0
  456. /*
  457. * PSK: send ServerKeyExchange if PSK identity hint if
  458. * provided
  459. */
  460. #ifndef OPENSSL_NO_PSK
  461. || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
  462. #endif
  463. || (alg_k & SSL_kDHE)
  464. || (alg_k & SSL_kEECDH)
  465. || ((alg_k & SSL_kRSA)
  466. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  467. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  468. && EVP_PKEY_size(s->cert->pkeys
  469. [SSL_PKEY_RSA_ENC].privatekey) *
  470. 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  471. )
  472. )
  473. )
  474. ) {
  475. dtls1_start_timer(s);
  476. ret = ssl3_send_server_key_exchange(s);
  477. if (ret <= 0)
  478. goto end;
  479. } else
  480. skip = 1;
  481. s->state = SSL3_ST_SW_CERT_REQ_A;
  482. s->init_num = 0;
  483. break;
  484. case SSL3_ST_SW_CERT_REQ_A:
  485. case SSL3_ST_SW_CERT_REQ_B:
  486. if ( /* don't request cert unless asked for it: */
  487. !(s->verify_mode & SSL_VERIFY_PEER) ||
  488. /*
  489. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  490. * during re-negotiation:
  491. */
  492. ((s->session->peer != NULL) &&
  493. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  494. /*
  495. * never request cert in anonymous ciphersuites (see
  496. * section "Certificate request" in SSL 3 drafts and in
  497. * RFC 2246):
  498. */
  499. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  500. /*
  501. * ... except when the application insists on
  502. * verification (against the specs, but s3_clnt.c accepts
  503. * this for SSL 3)
  504. */
  505. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  506. /*
  507. * never request cert in Kerberos ciphersuites
  508. */
  509. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
  510. /*
  511. * With normal PSK Certificates and Certificate Requests
  512. * are omitted
  513. */
  514. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  515. /* no cert request */
  516. skip = 1;
  517. s->s3->tmp.cert_request = 0;
  518. s->state = SSL3_ST_SW_SRVR_DONE_A;
  519. #ifndef OPENSSL_NO_SCTP
  520. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  521. s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
  522. s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  523. }
  524. #endif
  525. } else {
  526. s->s3->tmp.cert_request = 1;
  527. dtls1_start_timer(s);
  528. ret = ssl3_send_certificate_request(s);
  529. if (ret <= 0)
  530. goto end;
  531. #ifndef NETSCAPE_HANG_BUG
  532. s->state = SSL3_ST_SW_SRVR_DONE_A;
  533. # ifndef OPENSSL_NO_SCTP
  534. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  535. s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
  536. s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  537. }
  538. # endif
  539. #else
  540. s->state = SSL3_ST_SW_FLUSH;
  541. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  542. # ifndef OPENSSL_NO_SCTP
  543. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  544. s->d1->next_state = s->s3->tmp.next_state;
  545. s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  546. }
  547. # endif
  548. #endif
  549. s->init_num = 0;
  550. }
  551. break;
  552. case SSL3_ST_SW_SRVR_DONE_A:
  553. case SSL3_ST_SW_SRVR_DONE_B:
  554. dtls1_start_timer(s);
  555. ret = ssl3_send_server_done(s);
  556. if (ret <= 0)
  557. goto end;
  558. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  559. s->state = SSL3_ST_SW_FLUSH;
  560. s->init_num = 0;
  561. break;
  562. case SSL3_ST_SW_FLUSH:
  563. s->rwstate = SSL_WRITING;
  564. if (BIO_flush(s->wbio) <= 0) {
  565. /*
  566. * If the write error was fatal, stop trying
  567. */
  568. if (!BIO_should_retry(s->wbio)) {
  569. s->rwstate = SSL_NOTHING;
  570. s->state = s->s3->tmp.next_state;
  571. }
  572. ret = -1;
  573. goto end;
  574. }
  575. s->rwstate = SSL_NOTHING;
  576. s->state = s->s3->tmp.next_state;
  577. break;
  578. case SSL3_ST_SR_CERT_A:
  579. case SSL3_ST_SR_CERT_B:
  580. if (s->s3->tmp.cert_request) {
  581. ret = ssl3_get_client_certificate(s);
  582. if (ret <= 0)
  583. goto end;
  584. }
  585. s->init_num = 0;
  586. s->state = SSL3_ST_SR_KEY_EXCH_A;
  587. break;
  588. case SSL3_ST_SR_KEY_EXCH_A:
  589. case SSL3_ST_SR_KEY_EXCH_B:
  590. ret = ssl3_get_client_key_exchange(s);
  591. if (ret <= 0)
  592. goto end;
  593. #ifndef OPENSSL_NO_SCTP
  594. /*
  595. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  596. * used.
  597. */
  598. snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
  599. DTLS1_SCTP_AUTH_LABEL);
  600. if (SSL_export_keying_material(s, sctpauthkey,
  601. sizeof(sctpauthkey), labelbuffer,
  602. sizeof(labelbuffer), NULL, 0, 0) <= 0) {
  603. ret = -1;
  604. s->state = SSL_ST_ERR;
  605. goto end;
  606. }
  607. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  608. sizeof(sctpauthkey), sctpauthkey);
  609. #endif
  610. s->state = SSL3_ST_SR_CERT_VRFY_A;
  611. s->init_num = 0;
  612. if (ret == 2) {
  613. /*
  614. * For the ECDH ciphersuites when the client sends its ECDH
  615. * pub key in a certificate, the CertificateVerify message is
  616. * not sent.
  617. */
  618. s->state = SSL3_ST_SR_FINISHED_A;
  619. s->init_num = 0;
  620. } else if (SSL_USE_SIGALGS(s)) {
  621. s->state = SSL3_ST_SR_CERT_VRFY_A;
  622. s->init_num = 0;
  623. if (!s->session->peer)
  624. break;
  625. /*
  626. * For sigalgs freeze the handshake buffer at this point and
  627. * digest cached records.
  628. */
  629. if (!s->s3->handshake_buffer) {
  630. SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
  631. s->state = SSL_ST_ERR;
  632. return -1;
  633. }
  634. s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
  635. if (!ssl3_digest_cached_records(s)) {
  636. s->state = SSL_ST_ERR;
  637. return -1;
  638. }
  639. } else {
  640. s->state = SSL3_ST_SR_CERT_VRFY_A;
  641. s->init_num = 0;
  642. /*
  643. * We need to get hashes here so if there is a client cert,
  644. * it can be verified
  645. */
  646. s->method->ssl3_enc->cert_verify_mac(s,
  647. NID_md5,
  648. &(s->s3->
  649. tmp.cert_verify_md
  650. [0]));
  651. s->method->ssl3_enc->cert_verify_mac(s, NID_sha1,
  652. &(s->s3->
  653. tmp.cert_verify_md
  654. [MD5_DIGEST_LENGTH]));
  655. }
  656. break;
  657. case SSL3_ST_SR_CERT_VRFY_A:
  658. case SSL3_ST_SR_CERT_VRFY_B:
  659. ret = ssl3_get_cert_verify(s);
  660. if (ret <= 0)
  661. goto end;
  662. #ifndef OPENSSL_NO_SCTP
  663. if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
  664. state == SSL_ST_RENEGOTIATE)
  665. s->state = DTLS1_SCTP_ST_SR_READ_SOCK;
  666. else
  667. #endif
  668. s->state = SSL3_ST_SR_FINISHED_A;
  669. s->init_num = 0;
  670. break;
  671. case SSL3_ST_SR_FINISHED_A:
  672. case SSL3_ST_SR_FINISHED_B:
  673. /*
  674. * Enable CCS. Receiving a CCS clears the flag, so make
  675. * sure not to re-enable it to ban duplicates. This *should* be the
  676. * first time we have received one - but we check anyway to be
  677. * cautious.
  678. * s->s3->change_cipher_spec is set when a CCS is
  679. * processed in d1_pkt.c, and remains set until
  680. * the client's Finished message is read.
  681. */
  682. if (!s->s3->change_cipher_spec)
  683. s->d1->change_cipher_spec_ok = 1;
  684. ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
  685. SSL3_ST_SR_FINISHED_B);
  686. if (ret <= 0)
  687. goto end;
  688. dtls1_stop_timer(s);
  689. if (s->hit)
  690. s->state = SSL_ST_OK;
  691. #ifndef OPENSSL_NO_TLSEXT
  692. else if (s->tlsext_ticket_expected)
  693. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  694. #endif
  695. else
  696. s->state = SSL3_ST_SW_CHANGE_A;
  697. s->init_num = 0;
  698. break;
  699. #ifndef OPENSSL_NO_TLSEXT
  700. case SSL3_ST_SW_SESSION_TICKET_A:
  701. case SSL3_ST_SW_SESSION_TICKET_B:
  702. ret = ssl3_send_newsession_ticket(s);
  703. if (ret <= 0)
  704. goto end;
  705. s->state = SSL3_ST_SW_CHANGE_A;
  706. s->init_num = 0;
  707. break;
  708. case SSL3_ST_SW_CERT_STATUS_A:
  709. case SSL3_ST_SW_CERT_STATUS_B:
  710. ret = ssl3_send_cert_status(s);
  711. if (ret <= 0)
  712. goto end;
  713. s->state = SSL3_ST_SW_KEY_EXCH_A;
  714. s->init_num = 0;
  715. break;
  716. #endif
  717. case SSL3_ST_SW_CHANGE_A:
  718. case SSL3_ST_SW_CHANGE_B:
  719. s->session->cipher = s->s3->tmp.new_cipher;
  720. if (!s->method->ssl3_enc->setup_key_block(s)) {
  721. ret = -1;
  722. s->state = SSL_ST_ERR;
  723. goto end;
  724. }
  725. ret = dtls1_send_change_cipher_spec(s,
  726. SSL3_ST_SW_CHANGE_A,
  727. SSL3_ST_SW_CHANGE_B);
  728. if (ret <= 0)
  729. goto end;
  730. #ifndef OPENSSL_NO_SCTP
  731. if (!s->hit) {
  732. /*
  733. * Change to new shared key of SCTP-Auth, will be ignored if
  734. * no SCTP used.
  735. */
  736. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  737. 0, NULL);
  738. }
  739. #endif
  740. s->state = SSL3_ST_SW_FINISHED_A;
  741. s->init_num = 0;
  742. if (!s->method->ssl3_enc->change_cipher_state(s,
  743. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  744. {
  745. ret = -1;
  746. s->state = SSL_ST_ERR;
  747. goto end;
  748. }
  749. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  750. break;
  751. case SSL3_ST_SW_FINISHED_A:
  752. case SSL3_ST_SW_FINISHED_B:
  753. ret = ssl3_send_finished(s,
  754. SSL3_ST_SW_FINISHED_A,
  755. SSL3_ST_SW_FINISHED_B,
  756. s->method->
  757. ssl3_enc->server_finished_label,
  758. s->method->
  759. ssl3_enc->server_finished_label_len);
  760. if (ret <= 0)
  761. goto end;
  762. s->state = SSL3_ST_SW_FLUSH;
  763. if (s->hit) {
  764. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  765. #ifndef OPENSSL_NO_SCTP
  766. /*
  767. * Change to new shared key of SCTP-Auth, will be ignored if
  768. * no SCTP used.
  769. */
  770. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  771. 0, NULL);
  772. #endif
  773. } else {
  774. s->s3->tmp.next_state = SSL_ST_OK;
  775. #ifndef OPENSSL_NO_SCTP
  776. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  777. s->d1->next_state = s->s3->tmp.next_state;
  778. s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
  779. }
  780. #endif
  781. }
  782. s->init_num = 0;
  783. break;
  784. case SSL_ST_OK:
  785. /* clean a few things up */
  786. ssl3_cleanup_key_block(s);
  787. #if 0
  788. BUF_MEM_free(s->init_buf);
  789. s->init_buf = NULL;
  790. #endif
  791. /* remove buffering on output */
  792. ssl_free_wbio_buffer(s);
  793. s->init_num = 0;
  794. if (s->renegotiate == 2) { /* skipped if we just sent a
  795. * HelloRequest */
  796. s->renegotiate = 0;
  797. s->new_session = 0;
  798. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  799. s->ctx->stats.sess_accept_good++;
  800. /* s->server=1; */
  801. s->handshake_func = dtls1_accept;
  802. if (cb != NULL)
  803. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  804. }
  805. ret = 1;
  806. /* done handshaking, next message is client hello */
  807. s->d1->handshake_read_seq = 0;
  808. /* next message is server hello */
  809. s->d1->handshake_write_seq = 0;
  810. s->d1->next_handshake_write_seq = 0;
  811. dtls1_clear_received_buffer(s);
  812. goto end;
  813. /* break; */
  814. case SSL_ST_ERR:
  815. default:
  816. SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE);
  817. ret = -1;
  818. goto end;
  819. /* break; */
  820. }
  821. if (!s->s3->tmp.reuse_message && !skip) {
  822. if (s->debug) {
  823. if ((ret = BIO_flush(s->wbio)) <= 0)
  824. goto end;
  825. }
  826. if ((cb != NULL) && (s->state != state)) {
  827. new_state = s->state;
  828. s->state = state;
  829. cb(s, SSL_CB_ACCEPT_LOOP, 1);
  830. s->state = new_state;
  831. }
  832. }
  833. skip = 0;
  834. }
  835. end:
  836. /* BIO_flush(s->wbio); */
  837. s->in_handshake--;
  838. #ifndef OPENSSL_NO_SCTP
  839. /*
  840. * Notify SCTP BIO socket to leave handshake mode and prevent stream
  841. * identifier other than 0. Will be ignored if no SCTP is used.
  842. */
  843. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
  844. s->in_handshake, NULL);
  845. #endif
  846. if (cb != NULL)
  847. cb(s, SSL_CB_ACCEPT_EXIT, ret);
  848. return (ret);
  849. }
  850. int dtls1_send_hello_verify_request(SSL *s)
  851. {
  852. unsigned int msg_len;
  853. unsigned char *msg, *buf, *p;
  854. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) {
  855. buf = (unsigned char *)s->init_buf->data;
  856. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  857. /* Always use DTLS 1.0 version: see RFC 6347 */
  858. *(p++) = DTLS1_VERSION >> 8;
  859. *(p++) = DTLS1_VERSION & 0xFF;
  860. if (s->ctx->app_gen_cookie_cb == NULL ||
  861. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  862. &(s->d1->cookie_len)) == 0) {
  863. SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,
  864. ERR_R_INTERNAL_ERROR);
  865. s->state = SSL_ST_ERR;
  866. return 0;
  867. }
  868. *(p++) = (unsigned char)s->d1->cookie_len;
  869. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  870. p += s->d1->cookie_len;
  871. msg_len = p - msg;
  872. dtls1_set_message_header(s, buf,
  873. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0,
  874. msg_len);
  875. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  876. /* number of bytes to write */
  877. s->init_num = p - buf;
  878. s->init_off = 0;
  879. }
  880. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  881. return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
  882. }