Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

615 рядки
20 KiB

  1. /* DTLS implementation written by Nagendra Modadugu
  2. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. */
  3. /* ====================================================================
  4. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * openssl-core@openssl.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com).
  54. *
  55. */
  56. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  57. * All rights reserved.
  58. *
  59. * This package is an SSL implementation written
  60. * by Eric Young (eay@cryptsoft.com).
  61. * The implementation was written so as to conform with Netscapes SSL.
  62. *
  63. * This library is free for commercial and non-commercial use as long as
  64. * the following conditions are aheared to. The following conditions
  65. * apply to all code found in this distribution, be it the RC4, RSA,
  66. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  67. * included with this distribution is covered by the same copyright terms
  68. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  69. *
  70. * Copyright remains Eric Young's, and as such any Copyright notices in
  71. * the code are not to be removed.
  72. * If this package is used in a product, Eric Young should be given attribution
  73. * as the author of the parts of the library used.
  74. * This can be in the form of a textual message at program startup or
  75. * in documentation (online or textual) provided with the package.
  76. *
  77. * Redistribution and use in source and binary forms, with or without
  78. * modification, are permitted provided that the following conditions
  79. * are met:
  80. * 1. Redistributions of source code must retain the copyright
  81. * notice, this list of conditions and the following disclaimer.
  82. * 2. Redistributions in binary form must reproduce the above copyright
  83. * notice, this list of conditions and the following disclaimer in the
  84. * documentation and/or other materials provided with the distribution.
  85. * 3. All advertising materials mentioning features or use of this software
  86. * must display the following acknowledgement:
  87. * "This product includes cryptographic software written by
  88. * Eric Young (eay@cryptsoft.com)"
  89. * The word 'cryptographic' can be left out if the rouines from the library
  90. * being used are not cryptographic related :-).
  91. * 4. If you include any Windows specific code (or a derivative thereof) from
  92. * the apps directory (application code) you must include an acknowledgement:
  93. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  94. *
  95. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  96. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  97. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  98. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  99. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  100. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  101. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  102. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  103. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  104. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  105. * SUCH DAMAGE.
  106. *
  107. * The licence and distribution terms for any publically available version or
  108. * derivative of this code cannot be changed. i.e. this code cannot simply be
  109. * copied and put under another distribution licence
  110. * [including the GNU Public Licence.] */
  111. #include <openssl/ssl.h>
  112. #include <assert.h>
  113. #include <stdio.h>
  114. #include <string.h>
  115. #include <openssl/buf.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/err.h>
  119. #include <openssl/rand.h>
  120. #include "internal.h"
  121. static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
  122. unsigned int len, enum dtls1_use_epoch_t use_epoch);
  123. /* dtls1_get_record reads a new input record. On success, it places it in
  124. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  125. * more data is needed. */
  126. static int dtls1_get_record(SSL *ssl) {
  127. again:
  128. /* Read a new packet if there is no unconsumed one. */
  129. if (ssl_read_buffer_len(ssl) == 0) {
  130. int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
  131. if (ret <= 0) {
  132. return ret;
  133. }
  134. }
  135. assert(ssl_read_buffer_len(ssl) > 0);
  136. /* Ensure the packet is large enough to decrypt in-place. */
  137. if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
  138. ssl_read_buffer_clear(ssl);
  139. goto again;
  140. }
  141. uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
  142. size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
  143. uint8_t type, alert;
  144. size_t len, consumed;
  145. switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
  146. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
  147. case ssl_open_record_success:
  148. ssl_read_buffer_consume(ssl, consumed);
  149. if (len > 0xffff) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  151. return -1;
  152. }
  153. SSL3_RECORD *rr = &ssl->s3->rrec;
  154. rr->type = type;
  155. rr->length = (uint16_t)len;
  156. rr->off = 0;
  157. rr->data = out;
  158. return 1;
  159. case ssl_open_record_discard:
  160. ssl_read_buffer_consume(ssl, consumed);
  161. goto again;
  162. case ssl_open_record_error:
  163. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  164. return -1;
  165. case ssl_open_record_partial:
  166. /* Impossible in DTLS. */
  167. break;
  168. }
  169. assert(0);
  170. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  171. return -1;
  172. }
  173. int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  174. return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  175. }
  176. void dtls1_read_close_notify(SSL *ssl) {
  177. /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
  178. * alerts also aren't delivered reliably, so we may even time out because the
  179. * peer never received our close_notify. Report to the caller that the channel
  180. * has fully shut down. */
  181. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  182. }
  183. /* Return up to 'len' payload bytes received in 'type' records.
  184. * 'type' is one of the following:
  185. *
  186. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  187. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  188. *
  189. * If we don't have stored data to work from, read a SSL/TLS record first
  190. * (possibly multiple records if we still don't have anything to return).
  191. *
  192. * This function must handle any surprises the peer may have for us, such as
  193. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  194. * a surprise, but handled as if it were), or renegotiation requests.
  195. * Also if record payloads contain fragments too small to process, we store
  196. * them until there is enough for the respective protocol (the record protocol
  197. * may use arbitrary fragmentation and even interleaving):
  198. * Change cipher spec protocol
  199. * just 1 byte needed, no need for keeping anything stored
  200. * Alert protocol
  201. * 2 bytes needed (AlertLevel, AlertDescription)
  202. * Handshake protocol
  203. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  204. * to detect unexpected Client Hello and Hello Request messages
  205. * here, anything else is handled by higher layers
  206. * Application data protocol
  207. * none of our business
  208. */
  209. int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) {
  210. int al, i, ret;
  211. unsigned int n;
  212. SSL3_RECORD *rr;
  213. void (*cb)(const SSL *ssl, int type2, int val) = NULL;
  214. if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
  215. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  216. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  217. return -1;
  218. }
  219. if (!s->in_handshake && SSL_in_init(s)) {
  220. /* type == SSL3_RT_APPLICATION_DATA */
  221. i = s->handshake_func(s);
  222. if (i < 0) {
  223. return i;
  224. }
  225. if (i == 0) {
  226. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  227. return -1;
  228. }
  229. }
  230. start:
  231. s->rwstate = SSL_NOTHING;
  232. /* s->s3->rrec.type - is the type of record
  233. * s->s3->rrec.data - data
  234. * s->s3->rrec.off - offset into 'data' for next read
  235. * s->s3->rrec.length - number of bytes. */
  236. rr = &s->s3->rrec;
  237. /* Check for timeout */
  238. if (DTLSv1_handle_timeout(s) > 0) {
  239. goto start;
  240. }
  241. /* get new packet if necessary */
  242. if (rr->length == 0) {
  243. ret = dtls1_get_record(s);
  244. if (ret <= 0) {
  245. ret = dtls1_read_failed(s, ret);
  246. /* anything other than a timeout is an error */
  247. if (ret <= 0) {
  248. return ret;
  249. } else {
  250. goto start;
  251. }
  252. }
  253. }
  254. /* we now have a packet which can be read and processed */
  255. /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
  256. * ssl3_get_finished. */
  257. if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
  258. rr->type != SSL3_RT_ALERT) {
  259. /* We now have an unexpected record between CCS and Finished. Most likely
  260. * the packets were reordered on their way. DTLS is unreliable, so drop the
  261. * packet and expect the peer to retransmit. */
  262. rr->length = 0;
  263. goto start;
  264. }
  265. /* If the other end has shut down, throw anything we read away (even in
  266. * 'peek' mode) */
  267. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  268. rr->length = 0;
  269. s->rwstate = SSL_NOTHING;
  270. return 0;
  271. }
  272. if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
  273. /* make sure that we are not getting application data when we
  274. * are doing a handshake for the first time */
  275. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  276. (s->aead_read_ctx == NULL)) {
  277. /* TODO(davidben): Is this check redundant with the handshake_func
  278. * check? */
  279. al = SSL_AD_UNEXPECTED_MESSAGE;
  280. OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
  281. goto f_err;
  282. }
  283. /* Discard empty records. */
  284. if (rr->length == 0) {
  285. goto start;
  286. }
  287. if (len <= 0) {
  288. return len;
  289. }
  290. if ((unsigned int)len > rr->length) {
  291. n = rr->length;
  292. } else {
  293. n = (unsigned int)len;
  294. }
  295. memcpy(buf, &(rr->data[rr->off]), n);
  296. if (!peek) {
  297. rr->length -= n;
  298. rr->off += n;
  299. if (rr->length == 0) {
  300. rr->off = 0;
  301. /* The record has been consumed, so we may now clear the buffer. */
  302. ssl_read_buffer_discard(s);
  303. }
  304. }
  305. return n;
  306. }
  307. /* If we get here, then type != rr->type. */
  308. /* If an alert record, process one alert out of the record. Note that we allow
  309. * a single record to contain multiple alerts. */
  310. if (rr->type == SSL3_RT_ALERT) {
  311. /* Alerts may not be fragmented. */
  312. if (rr->length < 2) {
  313. al = SSL_AD_DECODE_ERROR;
  314. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  315. goto f_err;
  316. }
  317. if (s->msg_callback) {
  318. s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
  319. s->msg_callback_arg);
  320. }
  321. const uint8_t alert_level = rr->data[rr->off++];
  322. const uint8_t alert_descr = rr->data[rr->off++];
  323. rr->length -= 2;
  324. if (s->info_callback != NULL) {
  325. cb = s->info_callback;
  326. } else if (s->ctx->info_callback != NULL) {
  327. cb = s->ctx->info_callback;
  328. }
  329. if (cb != NULL) {
  330. uint16_t alert = (alert_level << 8) | alert_descr;
  331. cb(s, SSL_CB_READ_ALERT, alert);
  332. }
  333. if (alert_level == SSL3_AL_WARNING) {
  334. s->s3->warn_alert = alert_descr;
  335. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  336. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  337. return 0;
  338. }
  339. } else if (alert_level == SSL3_AL_FATAL) {
  340. char tmp[16];
  341. s->rwstate = SSL_NOTHING;
  342. s->s3->fatal_alert = alert_descr;
  343. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  344. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  345. ERR_add_error_data(2, "SSL alert number ", tmp);
  346. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  347. SSL_CTX_remove_session(s->ctx, s->session);
  348. return 0;
  349. } else {
  350. al = SSL_AD_ILLEGAL_PARAMETER;
  351. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  352. goto f_err;
  353. }
  354. goto start;
  355. }
  356. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  357. /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
  358. * record payload has to look like */
  359. if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
  360. al = SSL_AD_ILLEGAL_PARAMETER;
  361. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  362. goto f_err;
  363. }
  364. rr->length = 0;
  365. if (s->msg_callback) {
  366. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
  367. s->msg_callback_arg);
  368. }
  369. /* We can't process a CCS now, because previous handshake
  370. * messages are still missing, so just drop it.
  371. */
  372. if (!s->d1->change_cipher_spec_ok) {
  373. goto start;
  374. }
  375. s->d1->change_cipher_spec_ok = 0;
  376. s->s3->change_cipher_spec = 1;
  377. if (!ssl3_do_change_cipher_spec(s)) {
  378. goto err;
  379. }
  380. /* do this whenever CCS is processed */
  381. dtls1_reset_seq_numbers(s, SSL3_CC_READ);
  382. goto start;
  383. }
  384. /* Unexpected handshake message. It may be a retransmitted Finished (the only
  385. * post-CCS message). Otherwise, it's a pre-CCS handshake message from an
  386. * unsupported renegotiation attempt. */
  387. if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) {
  388. if (rr->length < DTLS1_HM_HEADER_LENGTH) {
  389. al = SSL_AD_DECODE_ERROR;
  390. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
  391. goto f_err;
  392. }
  393. struct hm_header_st msg_hdr;
  394. dtls1_get_message_header(&rr->data[rr->off], &msg_hdr);
  395. /* Ignore a stray Finished from the previous handshake. */
  396. if (msg_hdr.type == SSL3_MT_FINISHED) {
  397. if (msg_hdr.frag_off == 0) {
  398. /* Retransmit our last flight of messages. If the peer sends the second
  399. * Finished, they may not have received ours. Only do this for the
  400. * first fragment, in case the Finished was fragmented. */
  401. if (dtls1_check_timeout_num(s) < 0) {
  402. return -1;
  403. }
  404. dtls1_retransmit_buffered_messages(s);
  405. }
  406. rr->length = 0;
  407. goto start;
  408. }
  409. }
  410. /* We already handled these. */
  411. assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT);
  412. al = SSL_AD_UNEXPECTED_MESSAGE;
  413. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  414. f_err:
  415. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  416. err:
  417. return -1;
  418. }
  419. int dtls1_write_app_data(SSL *s, const void *buf_, int len) {
  420. int i;
  421. if (SSL_in_init(s) && !s->in_handshake) {
  422. i = s->handshake_func(s);
  423. if (i < 0) {
  424. return i;
  425. }
  426. if (i == 0) {
  427. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  428. return -1;
  429. }
  430. }
  431. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  432. OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
  433. return -1;
  434. }
  435. i = dtls1_write_bytes(s, SSL3_RT_APPLICATION_DATA, buf_, len,
  436. dtls1_use_current_epoch);
  437. return i;
  438. }
  439. /* Call this to write data in records of type 'type' It will return <= 0 if not
  440. * all data has been sent or non-blocking IO. */
  441. int dtls1_write_bytes(SSL *s, int type, const void *buf, int len,
  442. enum dtls1_use_epoch_t use_epoch) {
  443. int i;
  444. assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
  445. s->rwstate = SSL_NOTHING;
  446. i = do_dtls1_write(s, type, buf, len, use_epoch);
  447. return i;
  448. }
  449. static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
  450. unsigned int len, enum dtls1_use_epoch_t use_epoch) {
  451. /* ssl3_write_pending drops the write if |BIO_write| fails in DTLS, so there
  452. * is never pending data. */
  453. assert(!ssl_write_buffer_is_pending(s));
  454. /* If we have an alert to send, lets send it */
  455. if (s->s3->alert_dispatch) {
  456. int ret = s->method->ssl_dispatch_alert(s);
  457. if (ret <= 0) {
  458. return ret;
  459. }
  460. /* if it went, fall through and send more stuff */
  461. }
  462. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  463. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  464. return -1;
  465. }
  466. if (len == 0) {
  467. return 0;
  468. }
  469. size_t max_out = len + ssl_max_seal_overhead(s);
  470. uint8_t *out;
  471. size_t ciphertext_len;
  472. if (!ssl_write_buffer_init(s, &out, max_out) ||
  473. !dtls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len,
  474. use_epoch)) {
  475. return -1;
  476. }
  477. ssl_write_buffer_set_len(s, ciphertext_len);
  478. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  479. * later */
  480. s->s3->wpend_tot = len;
  481. s->s3->wpend_buf = buf;
  482. s->s3->wpend_type = type;
  483. s->s3->wpend_ret = len;
  484. /* we now just need to write the buffer */
  485. return ssl3_write_pending(s, type, buf, len);
  486. }
  487. int dtls1_dispatch_alert(SSL *s) {
  488. int i, j;
  489. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  490. uint8_t buf[DTLS1_AL_HEADER_LENGTH];
  491. uint8_t *ptr = &buf[0];
  492. s->s3->alert_dispatch = 0;
  493. memset(buf, 0x00, sizeof(buf));
  494. *ptr++ = s->s3->send_alert[0];
  495. *ptr++ = s->s3->send_alert[1];
  496. i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf),
  497. dtls1_use_current_epoch);
  498. if (i <= 0) {
  499. s->s3->alert_dispatch = 1;
  500. } else {
  501. if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
  502. (void)BIO_flush(s->wbio);
  503. }
  504. if (s->msg_callback) {
  505. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
  506. s->msg_callback_arg);
  507. }
  508. if (s->info_callback != NULL) {
  509. cb = s->info_callback;
  510. } else if (s->ctx->info_callback != NULL) {
  511. cb = s->ctx->info_callback;
  512. }
  513. if (cb != NULL) {
  514. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  515. cb(s, SSL_CB_WRITE_ALERT, j);
  516. }
  517. }
  518. return i;
  519. }
  520. void dtls1_reset_seq_numbers(SSL *s, int rw) {
  521. uint8_t *seq;
  522. unsigned int seq_bytes = sizeof(s->s3->read_sequence);
  523. if (rw & SSL3_CC_READ) {
  524. seq = s->s3->read_sequence;
  525. s->d1->r_epoch++;
  526. memset(&s->d1->bitmap, 0, sizeof(DTLS1_BITMAP));
  527. } else {
  528. seq = s->s3->write_sequence;
  529. memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence));
  530. s->d1->w_epoch++;
  531. }
  532. memset(seq, 0x00, seq_bytes);
  533. }