Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

1174 rindas
38 KiB

  1. /*
  2. * DTLS implementation written by Nagendra Modadugu
  3. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  59. * All rights reserved.
  60. *
  61. * This package is an SSL implementation written
  62. * by Eric Young (eay@cryptsoft.com).
  63. * The implementation was written so as to conform with Netscapes SSL.
  64. *
  65. * This library is free for commercial and non-commercial use as long as
  66. * the following conditions are aheared to. The following conditions
  67. * apply to all code found in this distribution, be it the RC4, RSA,
  68. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  69. * included with this distribution is covered by the same copyright terms
  70. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  71. *
  72. * Copyright remains Eric Young's, and as such any Copyright notices in
  73. * the code are not to be removed.
  74. * If this package is used in a product, Eric Young should be given attribution
  75. * as the author of the parts of the library used.
  76. * This can be in the form of a textual message at program startup or
  77. * in documentation (online or textual) provided with the package.
  78. *
  79. * Redistribution and use in source and binary forms, with or without
  80. * modification, are permitted provided that the following conditions
  81. * are met:
  82. * 1. Redistributions of source code must retain the copyright
  83. * notice, this list of conditions and the following disclaimer.
  84. * 2. Redistributions in binary form must reproduce the above copyright
  85. * notice, this list of conditions and the following disclaimer in the
  86. * documentation and/or other materials provided with the distribution.
  87. * 3. All advertising materials mentioning features or use of this software
  88. * must display the following acknowledgement:
  89. * "This product includes cryptographic software written by
  90. * Eric Young (eay@cryptsoft.com)"
  91. * The word 'cryptographic' can be left out if the rouines from the library
  92. * being used are not cryptographic related :-).
  93. * 4. If you include any Windows specific code (or a derivative thereof) from
  94. * the apps directory (application code) you must include an acknowledgement:
  95. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  96. *
  97. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  98. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  99. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  100. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  101. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  102. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  103. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  104. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  105. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  106. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  107. * SUCH DAMAGE.
  108. *
  109. * The licence and distribution terms for any publically available version or
  110. * derivative of this code cannot be changed. i.e. this code cannot simply be
  111. * copied and put under another distribution licence
  112. * [including the GNU Public Licence.] */
  113. #include <assert.h>
  114. #include <limits.h>
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/err.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/obj.h>
  122. #include <openssl/rand.h>
  123. #include <openssl/x509.h>
  124. #include "ssl_locl.h"
  125. #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
  126. #define RSMBLY_BITMASK_MARK(bitmask, start, end) \
  127. { \
  128. if ((end) - (start) <= 8) { \
  129. long ii; \
  130. for (ii = (start); ii < (end); ii++) \
  131. bitmask[((ii) >> 3)] |= (1 << ((ii)&7)); \
  132. } else { \
  133. long ii; \
  134. bitmask[((start) >> 3)] |= bitmask_start_values[((start)&7)]; \
  135. for (ii = (((start) >> 3) + 1); ii < ((((end)-1)) >> 3); ii++) \
  136. bitmask[ii] = 0xff; \
  137. bitmask[(((end)-1) >> 3)] |= bitmask_end_values[((end)&7)]; \
  138. } \
  139. }
  140. #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) \
  141. { \
  142. long ii; \
  143. assert((msg_len) > 0); \
  144. is_complete = 1; \
  145. if (bitmask[(((msg_len)-1) >> 3)] != bitmask_end_values[((msg_len)&7)]) \
  146. is_complete = 0; \
  147. if (is_complete) \
  148. for (ii = (((msg_len)-1) >> 3) - 1; ii >= 0; ii--) \
  149. if (bitmask[ii] != 0xff) { \
  150. is_complete = 0; \
  151. break; \
  152. } \
  153. }
  154. static const uint8_t bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8,
  155. 0xf0, 0xe0, 0xc0, 0x80};
  156. static const uint8_t bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07,
  157. 0x0f, 0x1f, 0x3f, 0x7f};
  158. /* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
  159. * for these values? Notably, why is kMinMTU a function of the transport
  160. * protocol's overhead rather than, say, what's needed to hold a minimally-sized
  161. * handshake fragment plus protocol overhead. */
  162. /* kMinMTU is the minimum acceptable MTU value. */
  163. static const unsigned int kMinMTU = 256 - 28;
  164. /* kDefaultMTU is the default MTU value to use if neither the user nor
  165. * the underlying BIO supplies one. */
  166. static const unsigned int kDefaultMTU = 1500 - 28;
  167. static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  168. unsigned long frag_len);
  169. static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
  170. static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok);
  171. static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
  172. int reassembly) {
  173. hm_fragment *frag = NULL;
  174. unsigned char *buf = NULL;
  175. unsigned char *bitmask = NULL;
  176. frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
  177. if (frag == NULL) {
  178. return NULL;
  179. }
  180. if (frag_len) {
  181. buf = (unsigned char *)OPENSSL_malloc(frag_len);
  182. if (buf == NULL) {
  183. OPENSSL_free(frag);
  184. return NULL;
  185. }
  186. }
  187. /* zero length fragment gets zero frag->fragment */
  188. frag->fragment = buf;
  189. /* Initialize reassembly bitmask if necessary */
  190. if (reassembly) {
  191. bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
  192. if (bitmask == NULL) {
  193. if (buf != NULL) {
  194. OPENSSL_free(buf);
  195. }
  196. OPENSSL_free(frag);
  197. return NULL;
  198. }
  199. memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
  200. }
  201. frag->reassembly = bitmask;
  202. return frag;
  203. }
  204. void dtls1_hm_fragment_free(hm_fragment *frag) {
  205. if (frag->msg_header.is_ccs) {
  206. /* TODO(davidben): Simplify aead_write_ctx ownership, probably by just
  207. * forbidding DTLS renego. */
  208. SSL_AEAD_CTX *aead_write_ctx =
  209. frag->msg_header.saved_retransmit_state.aead_write_ctx;
  210. if (aead_write_ctx) {
  211. EVP_AEAD_CTX_cleanup(&aead_write_ctx->ctx);
  212. OPENSSL_free(aead_write_ctx);
  213. }
  214. }
  215. if (frag->fragment) {
  216. OPENSSL_free(frag->fragment);
  217. }
  218. if (frag->reassembly) {
  219. OPENSSL_free(frag->reassembly);
  220. }
  221. OPENSSL_free(frag);
  222. }
  223. /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
  224. * SSL3_RT_CHANGE_CIPHER_SPEC) */
  225. int dtls1_do_write(SSL *s, int type) {
  226. int ret;
  227. int curr_mtu;
  228. unsigned int len, frag_off;
  229. size_t max_overhead = 0;
  230. /* AHA! Figure out the MTU, and stick to the right size */
  231. if (s->d1->mtu < dtls1_min_mtu() &&
  232. !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
  233. long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  234. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  235. s->d1->mtu = (unsigned)mtu;
  236. } else {
  237. s->d1->mtu = kDefaultMTU;
  238. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
  239. }
  240. }
  241. /* should have something reasonable now */
  242. assert(s->d1->mtu >= dtls1_min_mtu());
  243. if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
  244. assert(s->init_num ==
  245. (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
  246. }
  247. /* Determine the maximum overhead of the current cipher. */
  248. if (s->aead_write_ctx != NULL) {
  249. max_overhead = EVP_AEAD_max_overhead(s->aead_write_ctx->ctx.aead);
  250. if (s->aead_write_ctx->variable_nonce_included_in_record) {
  251. max_overhead += s->aead_write_ctx->variable_nonce_len;
  252. }
  253. }
  254. frag_off = 0;
  255. while (s->init_num) {
  256. /* Account for data in the buffering BIO; multiple records may be packed
  257. * into a single packet during the handshake.
  258. *
  259. * TODO(davidben): This is buggy; if the MTU is larger than the buffer size,
  260. * the large record will be split across two packets. Moreover, in that
  261. * case, the |dtls1_write_bytes| call may not return synchronously. This
  262. * will break on retry as the |s->init_off| and |s->init_num| adjustment
  263. * will run a second time. */
  264. curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
  265. DTLS1_RT_HEADER_LENGTH - max_overhead;
  266. if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
  267. /* Flush the buffer and continue with a fresh packet.
  268. *
  269. * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple
  270. * calls to |dtls1_do_write|, |frag_off| will be wrong. */
  271. ret = BIO_flush(SSL_get_wbio(s));
  272. if (ret <= 0) {
  273. return ret;
  274. }
  275. assert(BIO_wpending(SSL_get_wbio(s)) == 0);
  276. curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
  277. }
  278. /* XDTLS: this function is too long. split out the CCS part */
  279. if (type == SSL3_RT_HANDSHAKE) {
  280. /* If this isn't the first fragment, reserve space to prepend a new
  281. * fragment header. This will override the body of a previous fragment. */
  282. if (s->init_off != 0) {
  283. assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
  284. s->init_off -= DTLS1_HM_HEADER_LENGTH;
  285. s->init_num += DTLS1_HM_HEADER_LENGTH;
  286. }
  287. if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
  288. /* To make forward progress, the MTU must, at minimum, fit the handshake
  289. * header and one byte of handshake body. */
  290. OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
  291. return -1;
  292. }
  293. if (s->init_num > curr_mtu) {
  294. len = curr_mtu;
  295. } else {
  296. len = s->init_num;
  297. }
  298. assert(len >= DTLS1_HM_HEADER_LENGTH);
  299. dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
  300. dtls1_write_message_header(
  301. s, (uint8_t *)&s->init_buf->data[s->init_off]);
  302. } else {
  303. assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
  304. /* ChangeCipherSpec cannot be fragmented. */
  305. if (s->init_num > curr_mtu) {
  306. OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
  307. return -1;
  308. }
  309. len = s->init_num;
  310. }
  311. ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
  312. if (ret < 0) {
  313. return -1;
  314. }
  315. /* bad if this assert fails, only part of the handshake message got sent.
  316. * But why would this happen? */
  317. assert(len == (unsigned int)ret);
  318. if (ret == s->init_num) {
  319. if (s->msg_callback) {
  320. s->msg_callback(1, s->version, type, s->init_buf->data,
  321. (size_t)(s->init_off + s->init_num), s,
  322. s->msg_callback_arg);
  323. }
  324. s->init_off = 0; /* done writing this message */
  325. s->init_num = 0;
  326. return 1;
  327. }
  328. s->init_off += ret;
  329. s->init_num -= ret;
  330. frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
  331. }
  332. return 0;
  333. }
  334. /* Obtain handshake message of message type 'mt' (any if mt == -1), maximum
  335. * acceptable body length 'max'. Read an entire handshake message. Handshake
  336. * messages arrive in fragments. */
  337. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
  338. int hash_message, int *ok) {
  339. int i, al;
  340. struct hm_header_st *msg_hdr;
  341. uint8_t *p;
  342. unsigned long msg_len;
  343. /* s3->tmp is used to store messages that are unexpected, caused
  344. * by the absence of an optional handshake message */
  345. if (s->s3->tmp.reuse_message) {
  346. /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
  347. * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
  348. * would have to have been applied to the previous call. */
  349. assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
  350. s->s3->tmp.reuse_message = 0;
  351. if (mt >= 0 && s->s3->tmp.message_type != mt) {
  352. al = SSL_AD_UNEXPECTED_MESSAGE;
  353. OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
  354. goto f_err;
  355. }
  356. *ok = 1;
  357. s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  358. s->init_num = (int)s->s3->tmp.message_size;
  359. return s->init_num;
  360. }
  361. msg_hdr = &s->d1->r_msg_hdr;
  362. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  363. again:
  364. i = dtls1_get_message_fragment(s, stn, max, ok);
  365. if (i == DTLS1_HM_BAD_FRAGMENT ||
  366. i == DTLS1_HM_FRAGMENT_RETRY) {
  367. /* bad fragment received */
  368. goto again;
  369. } else if (i <= 0 && !*ok) {
  370. return i;
  371. }
  372. p = (uint8_t *)s->init_buf->data;
  373. msg_len = msg_hdr->msg_len;
  374. /* reconstruct message header */
  375. *(p++) = msg_hdr->type;
  376. l2n3(msg_len, p);
  377. s2n(msg_hdr->seq, p);
  378. l2n3(0, p);
  379. l2n3(msg_len, p);
  380. p -= DTLS1_HM_HEADER_LENGTH;
  381. msg_len += DTLS1_HM_HEADER_LENGTH;
  382. s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  383. if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE) {
  384. ssl3_hash_current_message(s);
  385. }
  386. if (s->msg_callback) {
  387. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s,
  388. s->msg_callback_arg);
  389. }
  390. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  391. s->d1->handshake_read_seq++;
  392. return s->init_num;
  393. f_err:
  394. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  395. *ok = 0;
  396. return -1;
  397. }
  398. static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
  399. int max) {
  400. size_t frag_off, frag_len, msg_len;
  401. msg_len = msg_hdr->msg_len;
  402. frag_off = msg_hdr->frag_off;
  403. frag_len = msg_hdr->frag_len;
  404. /* sanity checking */
  405. if ((frag_off + frag_len) > msg_len) {
  406. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
  407. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  408. return SSL_AD_ILLEGAL_PARAMETER;
  409. }
  410. if ((frag_off + frag_len) > (unsigned long)max) {
  411. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
  412. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  413. return SSL_AD_ILLEGAL_PARAMETER;
  414. }
  415. if (s->d1->r_msg_hdr.frag_off == 0) {
  416. /* first fragment */
  417. /* msg_len is limited to 2^24, but is effectively checked
  418. * against max above */
  419. if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
  420. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
  421. return SSL_AD_INTERNAL_ERROR;
  422. }
  423. s->s3->tmp.message_size = msg_len;
  424. s->d1->r_msg_hdr.msg_len = msg_len;
  425. s->s3->tmp.message_type = msg_hdr->type;
  426. s->d1->r_msg_hdr.type = msg_hdr->type;
  427. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  428. } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
  429. /* They must be playing with us! BTW, failure to enforce
  430. * upper limit would open possibility for buffer overrun. */
  431. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
  432. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  433. return SSL_AD_ILLEGAL_PARAMETER;
  434. }
  435. return 0; /* no error */
  436. }
  437. static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) {
  438. /* (0) check whether the desired fragment is available
  439. * if so:
  440. * (1) copy over the fragment to s->init_buf->data[]
  441. * (2) update s->init_num */
  442. pitem *item;
  443. hm_fragment *frag;
  444. int al;
  445. unsigned long frag_len;
  446. *ok = 0;
  447. item = pqueue_peek(s->d1->buffered_messages);
  448. if (item == NULL) {
  449. return 0;
  450. }
  451. frag = (hm_fragment *)item->data;
  452. /* Don't return if reassembly still in progress */
  453. if (frag->reassembly != NULL) {
  454. return 0;
  455. }
  456. if (s->d1->handshake_read_seq != frag->msg_header.seq) {
  457. return 0;
  458. }
  459. frag_len = frag->msg_header.frag_len;
  460. pqueue_pop(s->d1->buffered_messages);
  461. al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
  462. if (al == 0) {
  463. /* no alert */
  464. uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  465. memcpy(&p[frag->msg_header.frag_off], frag->fragment,
  466. frag->msg_header.frag_len);
  467. }
  468. dtls1_hm_fragment_free(frag);
  469. pitem_free(item);
  470. if (al == 0) {
  471. *ok = 1;
  472. return frag_len;
  473. }
  474. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  475. s->init_num = 0;
  476. *ok = 0;
  477. return -1;
  478. }
  479. /* dtls1_max_handshake_message_len returns the maximum number of bytes
  480. * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
  481. * be greater if the maximum certificate list size requires it. */
  482. static unsigned long dtls1_max_handshake_message_len(const SSL *s) {
  483. unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  484. if (max_len < (unsigned long)s->max_cert_list) {
  485. return s->max_cert_list;
  486. }
  487. return max_len;
  488. }
  489. static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr,
  490. int *ok) {
  491. hm_fragment *frag = NULL;
  492. pitem *item = NULL;
  493. int i = -1, is_complete;
  494. uint8_t seq64be[8];
  495. unsigned long frag_len = msg_hdr->frag_len;
  496. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
  497. msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) {
  498. goto err;
  499. }
  500. if (frag_len == 0) {
  501. return DTLS1_HM_FRAGMENT_RETRY;
  502. }
  503. /* Try to find item in queue */
  504. memset(seq64be, 0, sizeof(seq64be));
  505. seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
  506. seq64be[7] = (uint8_t)msg_hdr->seq;
  507. item = pqueue_find(s->d1->buffered_messages, seq64be);
  508. if (item == NULL) {
  509. frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
  510. if (frag == NULL) {
  511. goto err;
  512. }
  513. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  514. frag->msg_header.frag_len = frag->msg_header.msg_len;
  515. frag->msg_header.frag_off = 0;
  516. } else {
  517. frag = (hm_fragment *)item->data;
  518. if (frag->msg_header.msg_len != msg_hdr->msg_len) {
  519. item = NULL;
  520. frag = NULL;
  521. goto err;
  522. }
  523. }
  524. /* If message is already reassembled, this must be a
  525. * retransmit and can be dropped. In this case item != NULL and so frag
  526. * does not need to be freed. */
  527. if (frag->reassembly == NULL) {
  528. uint8_t devnull[256];
  529. assert(item != NULL);
  530. while (frag_len) {
  531. i = s->method->ssl_read_bytes(
  532. s, SSL3_RT_HANDSHAKE, devnull,
  533. frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
  534. if (i <= 0) {
  535. goto err;
  536. }
  537. frag_len -= i;
  538. }
  539. return DTLS1_HM_FRAGMENT_RETRY;
  540. }
  541. /* read the body of the fragment (header has already been read */
  542. i = s->method->ssl_read_bytes(
  543. s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0);
  544. if ((unsigned long)i != frag_len) {
  545. i = -1;
  546. }
  547. if (i <= 0) {
  548. goto err;
  549. }
  550. RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
  551. (long)(msg_hdr->frag_off + frag_len));
  552. RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
  553. is_complete);
  554. if (is_complete) {
  555. OPENSSL_free(frag->reassembly);
  556. frag->reassembly = NULL;
  557. }
  558. if (item == NULL) {
  559. item = pitem_new(seq64be, frag);
  560. if (item == NULL) {
  561. i = -1;
  562. goto err;
  563. }
  564. item = pqueue_insert(s->d1->buffered_messages, item);
  565. /* pqueue_insert fails iff a duplicate item is inserted.
  566. * However, |item| cannot be a duplicate. If it were,
  567. * |pqueue_find|, above, would have returned it and control
  568. * would never have reached this branch. */
  569. assert(item != NULL);
  570. }
  571. return DTLS1_HM_FRAGMENT_RETRY;
  572. err:
  573. if (frag != NULL && item == NULL) {
  574. dtls1_hm_fragment_free(frag);
  575. }
  576. *ok = 0;
  577. return i;
  578. }
  579. static int dtls1_process_out_of_seq_message(SSL *s,
  580. const struct hm_header_st *msg_hdr,
  581. int *ok) {
  582. int i = -1;
  583. hm_fragment *frag = NULL;
  584. pitem *item = NULL;
  585. uint8_t seq64be[8];
  586. unsigned long frag_len = msg_hdr->frag_len;
  587. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) {
  588. goto err;
  589. }
  590. /* Try to find item in queue, to prevent duplicate entries */
  591. memset(seq64be, 0, sizeof(seq64be));
  592. seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
  593. seq64be[7] = (uint8_t)msg_hdr->seq;
  594. item = pqueue_find(s->d1->buffered_messages, seq64be);
  595. /* If we already have an entry and this one is a fragment,
  596. * don't discard it and rather try to reassemble it. */
  597. if (item != NULL && frag_len != msg_hdr->msg_len) {
  598. item = NULL;
  599. }
  600. /* Discard the message if sequence number was already there, is
  601. * too far in the future, already in the queue or if we received
  602. * a FINISHED before the SERVER_HELLO, which then must be a stale
  603. * retransmit. */
  604. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  605. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  606. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
  607. uint8_t devnull[256];
  608. while (frag_len) {
  609. i = s->method->ssl_read_bytes(
  610. s, SSL3_RT_HANDSHAKE, devnull,
  611. frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
  612. if (i <= 0) {
  613. goto err;
  614. }
  615. frag_len -= i;
  616. }
  617. } else {
  618. if (frag_len != msg_hdr->msg_len) {
  619. return dtls1_reassemble_fragment(s, msg_hdr, ok);
  620. }
  621. if (frag_len > dtls1_max_handshake_message_len(s)) {
  622. goto err;
  623. }
  624. frag = dtls1_hm_fragment_new(frag_len, 0);
  625. if (frag == NULL) {
  626. goto err;
  627. }
  628. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  629. if (frag_len) {
  630. /* read the body of the fragment (header has already been read */
  631. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment,
  632. frag_len, 0);
  633. if ((unsigned long)i != frag_len) {
  634. i = -1;
  635. }
  636. if (i <= 0) {
  637. goto err;
  638. }
  639. }
  640. item = pitem_new(seq64be, frag);
  641. if (item == NULL) {
  642. goto err;
  643. }
  644. item = pqueue_insert(s->d1->buffered_messages, item);
  645. /* pqueue_insert fails iff a duplicate item is inserted.
  646. * However, |item| cannot be a duplicate. If it were,
  647. * |pqueue_find|, above, would have returned it. Then, either
  648. * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
  649. * to NULL and it will have been processed with
  650. * |dtls1_reassemble_fragment|, above, or the record will have
  651. * been discarded. */
  652. assert(item != NULL);
  653. }
  654. return DTLS1_HM_FRAGMENT_RETRY;
  655. err:
  656. if (frag != NULL && item == NULL) {
  657. dtls1_hm_fragment_free(frag);
  658. }
  659. *ok = 0;
  660. return i;
  661. }
  662. static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) {
  663. uint8_t wire[DTLS1_HM_HEADER_LENGTH];
  664. unsigned long len, frag_off, frag_len;
  665. int i, al;
  666. struct hm_header_st msg_hdr;
  667. redo:
  668. /* see if we have the required fragment already */
  669. if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
  670. if (*ok) {
  671. s->init_num = frag_len;
  672. }
  673. return frag_len;
  674. }
  675. /* read handshake message header */
  676. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
  677. DTLS1_HM_HEADER_LENGTH, 0);
  678. if (i <= 0) {
  679. /* nbio, or an error */
  680. s->rwstate = SSL_READING;
  681. *ok = 0;
  682. return i;
  683. }
  684. /* Handshake fails if message header is incomplete */
  685. if (i != DTLS1_HM_HEADER_LENGTH) {
  686. al = SSL_AD_UNEXPECTED_MESSAGE;
  687. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
  688. SSL_R_UNEXPECTED_MESSAGE);
  689. goto f_err;
  690. }
  691. /* parse the message fragment header */
  692. dtls1_get_message_header(wire, &msg_hdr);
  693. /* if this is a future (or stale) message it gets buffered
  694. * (or dropped)--no further processing at this time. */
  695. if (msg_hdr.seq != s->d1->handshake_read_seq) {
  696. return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
  697. }
  698. len = msg_hdr.msg_len;
  699. frag_off = msg_hdr.frag_off;
  700. frag_len = msg_hdr.frag_len;
  701. if (frag_len && frag_len < len) {
  702. return dtls1_reassemble_fragment(s, &msg_hdr, ok);
  703. }
  704. if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
  705. wire[0] == SSL3_MT_HELLO_REQUEST) {
  706. /* The server may always send 'Hello Request' messages --
  707. * we are doing a handshake anyway now, so ignore them
  708. * if their format is correct. Does not count for
  709. * 'Finished' MAC. */
  710. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
  711. if (s->msg_callback) {
  712. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire,
  713. DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg);
  714. }
  715. s->init_num = 0;
  716. goto redo;
  717. } else {
  718. /* Incorrectly formated Hello request */
  719. al = SSL_AD_UNEXPECTED_MESSAGE;
  720. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
  721. SSL_R_UNEXPECTED_MESSAGE);
  722. goto f_err;
  723. }
  724. }
  725. if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) {
  726. goto f_err;
  727. }
  728. /* XDTLS: ressurect this when restart is in place */
  729. s->state = stn;
  730. if (frag_len > 0) {
  731. uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  732. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len,
  733. 0);
  734. /* XDTLS: fix this--message fragments cannot span multiple packets */
  735. if (i <= 0) {
  736. s->rwstate = SSL_READING;
  737. *ok = 0;
  738. return i;
  739. }
  740. } else {
  741. i = 0;
  742. }
  743. /* XDTLS: an incorrectly formatted fragment should cause the
  744. * handshake to fail */
  745. if (i != (int)frag_len) {
  746. al = SSL3_AD_ILLEGAL_PARAMETER;
  747. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
  748. SSL3_AD_ILLEGAL_PARAMETER);
  749. goto f_err;
  750. }
  751. *ok = 1;
  752. /* Note that s->init_num is *not* used as current offset in
  753. * s->init_buf->data, but as a counter summing up fragments'
  754. * lengths: as soon as they sum up to handshake packet
  755. * length, we assume we have got all the fragments. */
  756. s->init_num = frag_len;
  757. return frag_len;
  758. f_err:
  759. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  760. s->init_num = 0;
  761. *ok = 0;
  762. return -1;
  763. }
  764. /* for these 2 messages, we need to
  765. * ssl->enc_read_ctx re-init
  766. * ssl->s3->read_sequence zero
  767. * ssl->s3->read_mac_secret re-init
  768. * ssl->session->read_sym_enc assign
  769. * ssl->session->read_compression assign
  770. * ssl->session->read_hash assign */
  771. int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
  772. uint8_t *p;
  773. if (s->state == a) {
  774. p = (uint8_t *)s->init_buf->data;
  775. *p++ = SSL3_MT_CCS;
  776. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  777. s->init_num = DTLS1_CCS_HEADER_LENGTH;
  778. s->init_off = 0;
  779. dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
  780. 0);
  781. /* buffer the message to handle re-xmits */
  782. dtls1_buffer_message(s, 1);
  783. s->state = b;
  784. }
  785. /* SSL3_ST_CW_CHANGE_B */
  786. return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
  787. }
  788. int dtls1_read_failed(SSL *s, int code) {
  789. if (code > 0) {
  790. fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
  791. return 1;
  792. }
  793. if (!dtls1_is_timer_expired(s)) {
  794. /* not a timeout, none of our business, let higher layers handle this. In
  795. * fact, it's probably an error */
  796. return code;
  797. }
  798. if (!SSL_in_init(s)) {
  799. /* done, no need to send a retransmit */
  800. BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
  801. return code;
  802. }
  803. return dtls1_handle_timeout(s);
  804. }
  805. int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
  806. /* The index of the retransmission queue actually is the message sequence
  807. * number, since the queue only contains messages of a single handshake.
  808. * However, the ChangeCipherSpec has no message sequence number and so using
  809. * only the sequence will result in the CCS and Finished having the same
  810. * index. To prevent this, the sequence number is multiplied by 2. In case of
  811. * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
  812. * maintains the order of the index (important for priority queues) and fits
  813. * in the unsigned short variable. */
  814. return seq * 2 - is_ccs;
  815. }
  816. int dtls1_retransmit_buffered_messages(SSL *s) {
  817. pqueue sent = s->d1->sent_messages;
  818. piterator iter;
  819. pitem *item;
  820. hm_fragment *frag;
  821. int found = 0;
  822. iter = pqueue_iterator(sent);
  823. for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
  824. frag = (hm_fragment *)item->data;
  825. if (dtls1_retransmit_message(
  826. s, (unsigned short)dtls1_get_queue_priority(
  827. frag->msg_header.seq, frag->msg_header.is_ccs),
  828. 0, &found) <= 0 &&
  829. found) {
  830. fprintf(stderr, "dtls1_retransmit_message() failed\n");
  831. return -1;
  832. }
  833. }
  834. return 1;
  835. }
  836. int dtls1_buffer_message(SSL *s, int is_ccs) {
  837. pitem *item;
  838. hm_fragment *frag;
  839. uint8_t seq64be[8];
  840. /* this function is called immediately after a message has
  841. * been serialized */
  842. assert(s->init_off == 0);
  843. frag = dtls1_hm_fragment_new(s->init_num, 0);
  844. if (!frag) {
  845. return 0;
  846. }
  847. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  848. if (is_ccs) {
  849. assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
  850. (unsigned int)s->init_num);
  851. } else {
  852. assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
  853. (unsigned int)s->init_num);
  854. }
  855. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  856. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  857. frag->msg_header.type = s->d1->w_msg_hdr.type;
  858. frag->msg_header.frag_off = 0;
  859. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  860. frag->msg_header.is_ccs = is_ccs;
  861. /* save current state*/
  862. frag->msg_header.saved_retransmit_state.aead_write_ctx = s->aead_write_ctx;
  863. frag->msg_header.saved_retransmit_state.session = s->session;
  864. frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
  865. memset(seq64be, 0, sizeof(seq64be));
  866. seq64be[6] = (uint8_t)(
  867. dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
  868. 8);
  869. seq64be[7] = (uint8_t)(
  870. dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
  871. item = pitem_new(seq64be, frag);
  872. if (item == NULL) {
  873. dtls1_hm_fragment_free(frag);
  874. return 0;
  875. }
  876. pqueue_insert(s->d1->sent_messages, item);
  877. return 1;
  878. }
  879. int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
  880. int *found) {
  881. int ret;
  882. /* XDTLS: for now assuming that read/writes are blocking */
  883. pitem *item;
  884. hm_fragment *frag;
  885. unsigned long header_length;
  886. uint8_t seq64be[8];
  887. struct dtls1_retransmit_state saved_state;
  888. uint8_t save_write_sequence[8];
  889. /* assert(s->init_num == 0);
  890. assert(s->init_off == 0); */
  891. /* XDTLS: the requested message ought to be found, otherwise error */
  892. memset(seq64be, 0, sizeof(seq64be));
  893. seq64be[6] = (uint8_t)(seq >> 8);
  894. seq64be[7] = (uint8_t)seq;
  895. item = pqueue_find(s->d1->sent_messages, seq64be);
  896. if (item == NULL) {
  897. fprintf(stderr, "retransmit: message %d non-existant\n", seq);
  898. *found = 0;
  899. return 0;
  900. }
  901. *found = 1;
  902. frag = (hm_fragment *)item->data;
  903. if (frag->msg_header.is_ccs) {
  904. header_length = DTLS1_CCS_HEADER_LENGTH;
  905. } else {
  906. header_length = DTLS1_HM_HEADER_LENGTH;
  907. }
  908. memcpy(s->init_buf->data, frag->fragment,
  909. frag->msg_header.msg_len + header_length);
  910. s->init_num = frag->msg_header.msg_len + header_length;
  911. dtls1_set_message_header(s, frag->msg_header.type,
  912. frag->msg_header.msg_len, frag->msg_header.seq,
  913. 0, frag->msg_header.frag_len);
  914. /* save current state */
  915. saved_state.aead_write_ctx = s->aead_write_ctx;
  916. saved_state.session = s->session;
  917. saved_state.epoch = s->d1->w_epoch;
  918. /* restore state in which the message was originally sent */
  919. s->aead_write_ctx = frag->msg_header.saved_retransmit_state.aead_write_ctx;
  920. s->session = frag->msg_header.saved_retransmit_state.session;
  921. s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
  922. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
  923. memcpy(save_write_sequence, s->s3->write_sequence,
  924. sizeof(s->s3->write_sequence));
  925. memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
  926. sizeof(s->s3->write_sequence));
  927. }
  928. ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
  929. : SSL3_RT_HANDSHAKE);
  930. /* restore current state */
  931. s->aead_write_ctx = saved_state.aead_write_ctx;
  932. s->session = saved_state.session;
  933. s->d1->w_epoch = saved_state.epoch;
  934. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
  935. memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
  936. sizeof(s->s3->write_sequence));
  937. memcpy(s->s3->write_sequence, save_write_sequence,
  938. sizeof(s->s3->write_sequence));
  939. }
  940. (void)BIO_flush(SSL_get_wbio(s));
  941. return ret;
  942. }
  943. /* call this function when the buffered messages are no longer needed */
  944. void dtls1_clear_record_buffer(SSL *s) {
  945. pitem *item;
  946. for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
  947. item = pqueue_pop(s->d1->sent_messages)) {
  948. dtls1_hm_fragment_free((hm_fragment *)item->data);
  949. pitem_free(item);
  950. }
  951. }
  952. /* don't actually do the writing, wait till the MTU has been retrieved */
  953. void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
  954. unsigned short seq_num, unsigned long frag_off,
  955. unsigned long frag_len) {
  956. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  957. msg_hdr->type = mt;
  958. msg_hdr->msg_len = len;
  959. msg_hdr->seq = seq_num;
  960. msg_hdr->frag_off = frag_off;
  961. msg_hdr->frag_len = frag_len;
  962. }
  963. static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  964. unsigned long frag_len) {
  965. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  966. msg_hdr->frag_off = frag_off;
  967. msg_hdr->frag_len = frag_len;
  968. }
  969. static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
  970. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  971. *p++ = msg_hdr->type;
  972. l2n3(msg_hdr->msg_len, p);
  973. s2n(msg_hdr->seq, p);
  974. l2n3(msg_hdr->frag_off, p);
  975. l2n3(msg_hdr->frag_len, p);
  976. return p;
  977. }
  978. unsigned int dtls1_min_mtu(void) {
  979. return kMinMTU;
  980. }
  981. void dtls1_get_message_header(uint8_t *data,
  982. struct hm_header_st *msg_hdr) {
  983. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  984. msg_hdr->type = *(data++);
  985. n2l3(data, msg_hdr->msg_len);
  986. n2s(data, msg_hdr->seq);
  987. n2l3(data, msg_hdr->frag_off);
  988. n2l3(data, msg_hdr->frag_len);
  989. }
  990. void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
  991. memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
  992. ccs_hdr->type = *(data++);
  993. }
  994. int dtls1_shutdown(SSL *s) {
  995. int ret;
  996. ret = ssl3_shutdown(s);
  997. return ret;
  998. }