選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1139 行
38 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <assert.h>
  109. #include <limits.h>
  110. #include <stdio.h>
  111. #include <string.h>
  112. #include <openssl/buf.h>
  113. #include <openssl/err.h>
  114. #include <openssl/evp.h>
  115. #include <openssl/mem.h>
  116. #include <openssl/rand.h>
  117. #include "internal.h"
  118. static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
  119. char fragment);
  120. static int ssl3_get_record(SSL *s);
  121. int ssl3_read_n(SSL *s, int n, int extend) {
  122. /* If |extend| is 0, obtain new n-byte packet;
  123. * if |extend| is 1, increase packet by another n bytes.
  124. *
  125. * The packet will be in the sub-array of |s->s3->rbuf.buf| specified by
  126. * |s->packet| and |s->packet_length|. (If DTLS and |extend| is 0, additional
  127. * bytes will be read into |rbuf|, up to the size of the buffer.)
  128. *
  129. * TODO(davidben): |dtls1_get_record| and |ssl3_get_record| have very
  130. * different needs. Separate the two record layers. In DTLS, |BIO_read| is
  131. * called at most once, and only when |extend| is 0. In TLS, the buffer never
  132. * contains more than one record. */
  133. int i, len, left;
  134. uintptr_t align = 0;
  135. uint8_t *pkt;
  136. SSL3_BUFFER *rb;
  137. if (n <= 0) {
  138. return n;
  139. }
  140. rb = &s->s3->rbuf;
  141. if (rb->buf == NULL && !ssl3_setup_read_buffer(s)) {
  142. return -1;
  143. }
  144. left = rb->left;
  145. align = (uintptr_t)rb->buf + SSL3_RT_HEADER_LENGTH;
  146. align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
  147. if (!extend) {
  148. /* start with empty packet ... */
  149. if (left == 0) {
  150. rb->offset = align;
  151. } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  152. /* check if next packet length is large enough to justify payload
  153. * alignment... */
  154. pkt = rb->buf + rb->offset;
  155. if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) {
  156. /* Note that even if packet is corrupted and its length field is
  157. * insane, we can only be led to wrong decision about whether memmove
  158. * will occur or not. Header values has no effect on memmove arguments
  159. * and therefore no buffer overrun can be triggered. */
  160. memmove(rb->buf + align, pkt, left);
  161. rb->offset = align;
  162. }
  163. }
  164. s->packet = rb->buf + rb->offset;
  165. s->packet_length = 0;
  166. /* ... now we can act as if 'extend' was set */
  167. }
  168. /* In DTLS, if there is leftover data from the previous packet or |extend| is
  169. * true, clamp to the previous read. DTLS records may not span packet
  170. * boundaries. */
  171. if (SSL_IS_DTLS(s) && n > left && (left > 0 || extend)) {
  172. n = left;
  173. }
  174. /* if there is enough in the buffer from a previous read, take some */
  175. if (left >= n) {
  176. s->packet_length += n;
  177. rb->left = left - n;
  178. rb->offset += n;
  179. return n;
  180. }
  181. /* else we need to read more data */
  182. len = s->packet_length;
  183. pkt = rb->buf + align;
  184. /* Move any available bytes to front of buffer: |len| bytes already pointed
  185. * to by |packet|, |left| extra ones at the end. */
  186. if (s->packet != pkt) {
  187. /* len > 0 */
  188. memmove(pkt, s->packet, len + left);
  189. s->packet = pkt;
  190. rb->offset = len + align;
  191. }
  192. if (n > (int)(rb->len - rb->offset)) {
  193. OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR);
  194. return -1;
  195. }
  196. int max = n;
  197. if (SSL_IS_DTLS(s) && !extend) {
  198. max = rb->len - rb->offset;
  199. }
  200. while (left < n) {
  201. /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
  202. * read in more until we have len+n (up to len+max if possible). */
  203. ERR_clear_system_error();
  204. if (s->rbio != NULL) {
  205. s->rwstate = SSL_READING;
  206. i = BIO_read(s->rbio, pkt + len + left, max - left);
  207. } else {
  208. OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
  209. i = -1;
  210. }
  211. if (i <= 0) {
  212. rb->left = left;
  213. if (len + left == 0) {
  214. ssl3_release_read_buffer(s);
  215. }
  216. return i;
  217. }
  218. left += i;
  219. /* reads should *never* span multiple packets for DTLS because the
  220. * underlying transport protocol is message oriented as opposed to byte
  221. * oriented as in the TLS case. */
  222. if (SSL_IS_DTLS(s) && n > left) {
  223. n = left; /* makes the while condition false */
  224. }
  225. }
  226. /* done reading, now the book-keeping */
  227. rb->offset += n;
  228. rb->left = left - n;
  229. s->packet_length += n;
  230. s->rwstate = SSL_NOTHING;
  231. return n;
  232. }
  233. /* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
  234. * be processed per call to ssl3_get_record. Without this limit an attacker
  235. * could send empty records at a faster rate than we can process and cause
  236. * ssl3_get_record to loop forever. */
  237. #define MAX_EMPTY_RECORDS 32
  238. /* Call this to get a new input record. It will return <= 0 if more data is
  239. * needed, normally due to an error or non-blocking IO. When it finishes, one
  240. * packet has been decoded and can be found in
  241. * ssl->s3->rrec.type - is the type of record
  242. * ssl->s3->rrec.data - data
  243. * ssl->s3->rrec.length - number of bytes */
  244. /* used only by ssl3_read_bytes */
  245. static int ssl3_get_record(SSL *s) {
  246. uint8_t ssl_major, ssl_minor;
  247. int al, n, i, ret = -1;
  248. SSL3_RECORD *rr = &s->s3->rrec;
  249. uint8_t *p;
  250. uint16_t version;
  251. size_t extra;
  252. unsigned empty_record_count = 0;
  253. again:
  254. /* check if we have the header */
  255. if (s->rstate != SSL_ST_READ_BODY ||
  256. s->packet_length < SSL3_RT_HEADER_LENGTH) {
  257. n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0);
  258. if (n <= 0) {
  259. return n; /* error or non-blocking */
  260. }
  261. s->rstate = SSL_ST_READ_BODY;
  262. /* Some bytes were read, so the read buffer must be existant and
  263. * |s->s3->init_extra| is defined. */
  264. assert(s->s3->rbuf.buf != NULL);
  265. extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
  266. p = s->packet;
  267. if (s->msg_callback) {
  268. s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
  269. }
  270. /* Pull apart the header into the SSL3_RECORD */
  271. rr->type = *(p++);
  272. ssl_major = *(p++);
  273. ssl_minor = *(p++);
  274. version = (((uint16_t)ssl_major) << 8) | ssl_minor;
  275. n2s(p, rr->length);
  276. if (s->s3->have_version && version != s->version) {
  277. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
  278. al = SSL_AD_PROTOCOL_VERSION;
  279. goto f_err;
  280. }
  281. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  282. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
  283. goto err;
  284. }
  285. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
  286. al = SSL_AD_RECORD_OVERFLOW;
  287. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  288. goto f_err;
  289. }
  290. /* now s->rstate == SSL_ST_READ_BODY */
  291. } else {
  292. /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The
  293. * read buffer must be existant and |s->s3->init_extra| is defined. */
  294. assert(s->s3->rbuf.buf != NULL);
  295. extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
  296. }
  297. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  298. if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
  299. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  300. i = rr->length;
  301. n = ssl3_read_n(s, i, 1);
  302. if (n <= 0) {
  303. /* Error or non-blocking IO. Now |n| == |rr->length|, and
  304. * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
  305. return n;
  306. }
  307. }
  308. s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
  309. /* |rr->data| points to |rr->length| bytes of ciphertext in |s->packet|. */
  310. rr->data = &s->packet[SSL3_RT_HEADER_LENGTH];
  311. /* Decrypt the packet in-place.
  312. *
  313. * TODO(davidben): This assumes |s->version| is the same as the record-layer
  314. * version which isn't always true, but it only differs with the NULL cipher
  315. * which ignores the parameter. */
  316. size_t plaintext_len;
  317. if (!SSL_AEAD_CTX_open(s->aead_read_ctx, rr->data, &plaintext_len, rr->length,
  318. rr->type, s->version, s->s3->read_sequence, rr->data,
  319. rr->length)) {
  320. al = SSL_AD_BAD_RECORD_MAC;
  321. OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
  322. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  323. goto f_err;
  324. }
  325. if (!ssl3_record_sequence_update(s->s3->read_sequence, 8)) {
  326. goto err;
  327. }
  328. if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
  329. al = SSL_AD_RECORD_OVERFLOW;
  330. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
  331. goto f_err;
  332. }
  333. assert(plaintext_len <= (1u << 16));
  334. rr->length = plaintext_len;
  335. rr->off = 0;
  336. /* So at this point the following is true:
  337. * ssl->s3->rrec.type is the type of record;
  338. * ssl->s3->rrec.length is the number of bytes in the record;
  339. * ssl->s3->rrec.off is the offset to first valid byte;
  340. * ssl->s3->rrec.data the first byte of the record body. */
  341. /* we have pulled in a full packet so zero things */
  342. s->packet_length = 0;
  343. /* just read a 0 length packet */
  344. if (rr->length == 0) {
  345. empty_record_count++;
  346. if (empty_record_count > MAX_EMPTY_RECORDS) {
  347. al = SSL_AD_UNEXPECTED_MESSAGE;
  348. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
  349. goto f_err;
  350. }
  351. goto again;
  352. }
  353. return 1;
  354. f_err:
  355. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  356. err:
  357. return ret;
  358. }
  359. int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
  360. return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
  361. }
  362. /* Call this to write data in records of type |type|. It will return <= 0 if
  363. * not all data has been sent or non-blocking IO. */
  364. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
  365. const uint8_t *buf = buf_;
  366. unsigned int tot, n, nw;
  367. int i;
  368. s->rwstate = SSL_NOTHING;
  369. assert(s->s3->wnum <= INT_MAX);
  370. tot = s->s3->wnum;
  371. s->s3->wnum = 0;
  372. if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
  373. i = s->handshake_func(s);
  374. if (i < 0) {
  375. return i;
  376. }
  377. if (i == 0) {
  378. OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  379. return -1;
  380. }
  381. }
  382. /* Ensure that if we end up with a smaller value of data to write out than
  383. * the the original len from a write which didn't complete for non-blocking
  384. * I/O and also somehow ended up avoiding the check for this in
  385. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  386. * end up with (len-tot) as a large number that will then promptly send
  387. * beyond the end of the users buffer ... so we trap and report the error in
  388. * a way the user will notice. */
  389. if (len < 0 || (size_t)len < tot) {
  390. OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
  391. return -1;
  392. }
  393. int record_split_done = 0;
  394. n = (len - tot);
  395. for (;;) {
  396. /* max contains the maximum number of bytes that we can put into a
  397. * record. */
  398. unsigned max = s->max_send_fragment;
  399. /* fragment is true if do_ssl3_write should send the first byte in its own
  400. * record in order to randomise a CBC IV. */
  401. int fragment = 0;
  402. if (!record_split_done && s->s3->need_record_splitting &&
  403. type == SSL3_RT_APPLICATION_DATA) {
  404. /* Only the the first record per write call needs to be split. The
  405. * remaining plaintext was determined before the IV was randomized. */
  406. fragment = 1;
  407. record_split_done = 1;
  408. }
  409. if (n > max) {
  410. nw = max;
  411. } else {
  412. nw = n;
  413. }
  414. i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
  415. if (i <= 0) {
  416. s->s3->wnum = tot;
  417. return i;
  418. }
  419. if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
  420. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  421. return tot + i;
  422. }
  423. n -= i;
  424. tot += i;
  425. }
  426. }
  427. /* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
  428. * writes it to |out|. At most |max_out| bytes will be written. It returns one
  429. * on success and zero on error. On success, it updates the write sequence
  430. * number. */
  431. static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
  432. size_t max_out, uint8_t type, const uint8_t *in,
  433. size_t in_len) {
  434. if (max_out < SSL3_RT_HEADER_LENGTH) {
  435. OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
  436. return 0;
  437. }
  438. out[0] = type;
  439. /* Some servers hang if initial ClientHello is larger than 256 bytes and
  440. * record version number > TLS 1.0. */
  441. uint16_t wire_version = s->version;
  442. if (!s->s3->have_version && s->version > SSL3_VERSION) {
  443. wire_version = TLS1_VERSION;
  444. }
  445. out[1] = wire_version >> 8;
  446. out[2] = wire_version & 0xff;
  447. size_t ciphertext_len;
  448. if (!SSL_AEAD_CTX_seal(s->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
  449. &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
  450. type, wire_version, s->s3->write_sequence, in,
  451. in_len) ||
  452. !ssl3_record_sequence_update(s->s3->write_sequence, 8)) {
  453. return 0;
  454. }
  455. if (ciphertext_len >= 1 << 16) {
  456. OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
  457. return 0;
  458. }
  459. out[3] = ciphertext_len >> 8;
  460. out[4] = ciphertext_len & 0xff;
  461. *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
  462. if (s->msg_callback) {
  463. s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
  464. s, s->msg_callback_arg);
  465. }
  466. return 1;
  467. }
  468. /* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
  469. * then it splits the record into a one byte record and a record with the rest
  470. * of the data in order to randomise a CBC IV. */
  471. static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
  472. char fragment) {
  473. SSL3_BUFFER *wb = &s->s3->wbuf;
  474. /* first check if there is a SSL3_BUFFER still being written out. This will
  475. * happen with non blocking IO */
  476. if (wb->left != 0) {
  477. return ssl3_write_pending(s, type, buf, len);
  478. }
  479. /* If we have an alert to send, lets send it */
  480. if (s->s3->alert_dispatch) {
  481. int ret = s->method->ssl_dispatch_alert(s);
  482. if (ret <= 0) {
  483. return ret;
  484. }
  485. /* if it went, fall through and send more stuff */
  486. }
  487. if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
  488. return -1;
  489. }
  490. if (len == 0) {
  491. return 0;
  492. }
  493. if (len == 1) {
  494. /* No sense in fragmenting a one-byte record. */
  495. fragment = 0;
  496. }
  497. /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
  498. uintptr_t align;
  499. if (fragment) {
  500. /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
  501. * multiple of the block size which we may assume is aligned. Thus we only
  502. * need to account for a second copy of the record header. */
  503. align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
  504. } else {
  505. align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
  506. }
  507. align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
  508. uint8_t *out = wb->buf + align;
  509. wb->offset = align;
  510. size_t max_out = wb->len - wb->offset;
  511. const uint8_t *orig_buf = buf;
  512. unsigned int orig_len = len;
  513. size_t fragment_len = 0;
  514. if (fragment) {
  515. /* Write the first byte in its own record as a countermeasure against
  516. * known-IV weaknesses in CBC ciphersuites. (See
  517. * http://www.openssl.org/~bodo/tls-cbc.txt.) */
  518. if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
  519. return -1;
  520. }
  521. out += fragment_len;
  522. max_out -= fragment_len;
  523. buf++;
  524. len--;
  525. }
  526. assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
  527. == 0);
  528. size_t ciphertext_len;
  529. if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
  530. return -1;
  531. }
  532. ciphertext_len += fragment_len;
  533. /* now let's set up wb */
  534. wb->left = ciphertext_len;
  535. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  536. * later */
  537. s->s3->wpend_tot = orig_len;
  538. s->s3->wpend_buf = orig_buf;
  539. s->s3->wpend_type = type;
  540. s->s3->wpend_ret = orig_len;
  541. /* we now just need to write the buffer */
  542. return ssl3_write_pending(s, type, orig_buf, orig_len);
  543. }
  544. /* if s->s3->wbuf.left != 0, we need to call this */
  545. int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
  546. int i;
  547. SSL3_BUFFER *wb = &(s->s3->wbuf);
  548. if (s->s3->wpend_tot > (int)len ||
  549. (s->s3->wpend_buf != buf &&
  550. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
  551. s->s3->wpend_type != type) {
  552. OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
  553. return -1;
  554. }
  555. for (;;) {
  556. ERR_clear_system_error();
  557. if (s->wbio != NULL) {
  558. s->rwstate = SSL_WRITING;
  559. i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
  560. (unsigned int)wb->left);
  561. } else {
  562. OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
  563. i = -1;
  564. }
  565. if (i == wb->left) {
  566. wb->left = 0;
  567. wb->offset += i;
  568. ssl3_release_write_buffer(s);
  569. s->rwstate = SSL_NOTHING;
  570. return s->s3->wpend_ret;
  571. } else if (i <= 0) {
  572. if (SSL_IS_DTLS(s)) {
  573. /* For DTLS, just drop it. That's kind of the whole point in
  574. * using a datagram service */
  575. wb->left = 0;
  576. }
  577. return i;
  578. }
  579. /* TODO(davidben): This codepath is used in DTLS, but the write
  580. * payload may not split across packets. */
  581. wb->offset += i;
  582. wb->left -= i;
  583. }
  584. }
  585. /* ssl3_expect_change_cipher_spec informs the record layer that a
  586. * ChangeCipherSpec record is required at this point. If a Handshake record is
  587. * received before ChangeCipherSpec, the connection will fail. Moreover, if
  588. * there are unprocessed handshake bytes, the handshake will also fail and the
  589. * function returns zero. Otherwise, the function returns one. */
  590. int ssl3_expect_change_cipher_spec(SSL *s) {
  591. if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
  592. OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
  593. SSL_R_UNPROCESSED_HANDSHAKE_DATA);
  594. return 0;
  595. }
  596. s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
  597. return 1;
  598. }
  599. int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  600. return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  601. }
  602. void ssl3_read_close_notify(SSL *ssl) {
  603. ssl3_read_bytes(ssl, 0, NULL, 0, 0);
  604. }
  605. /* Return up to 'len' payload bytes received in 'type' records.
  606. * 'type' is one of the following:
  607. *
  608. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  609. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  610. * - 0 (during a shutdown, no data has to be returned)
  611. *
  612. * If we don't have stored data to work from, read a SSL/TLS record first
  613. * (possibly multiple records if we still don't have anything to return).
  614. *
  615. * This function must handle any surprises the peer may have for us, such as
  616. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  617. * a surprise, but handled as if it were), or renegotiation requests.
  618. * Also if record payloads contain fragments too small to process, we store
  619. * them until there is enough for the respective protocol (the record protocol
  620. * may use arbitrary fragmentation and even interleaving):
  621. * Change cipher spec protocol
  622. * just 1 byte needed, no need for keeping anything stored
  623. * Alert protocol
  624. * 2 bytes needed (AlertLevel, AlertDescription)
  625. * Handshake protocol
  626. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  627. * to detect unexpected Client Hello and Hello Request messages
  628. * here, anything else is handled by higher layers
  629. * Application data protocol
  630. * none of our business
  631. */
  632. int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
  633. int al, i, ret;
  634. unsigned int n;
  635. SSL3_RECORD *rr;
  636. void (*cb)(const SSL *ssl, int type2, int val) = NULL;
  637. if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
  638. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  639. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
  640. return -1;
  641. }
  642. if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
  643. /* (partially) satisfy request from storage */
  644. uint8_t *src = s->s3->handshake_fragment;
  645. uint8_t *dst = buf;
  646. unsigned int k;
  647. /* peek == 0 */
  648. n = 0;
  649. while (len > 0 && s->s3->handshake_fragment_len > 0) {
  650. *dst++ = *src++;
  651. len--;
  652. s->s3->handshake_fragment_len--;
  653. n++;
  654. }
  655. /* move any remaining fragment bytes: */
  656. for (k = 0; k < s->s3->handshake_fragment_len; k++) {
  657. s->s3->handshake_fragment[k] = *src++;
  658. }
  659. return n;
  660. }
  661. /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
  662. /* This may require multiple iterations. False Start will cause
  663. * |s->handshake_func| to signal success one step early, but the handshake
  664. * must be completely finished before other modes are accepted.
  665. *
  666. * TODO(davidben): Move this check up to a higher level. */
  667. while (!s->in_handshake && SSL_in_init(s)) {
  668. assert(type == SSL3_RT_APPLICATION_DATA);
  669. i = s->handshake_func(s);
  670. if (i < 0) {
  671. return i;
  672. }
  673. if (i == 0) {
  674. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  675. return -1;
  676. }
  677. }
  678. start:
  679. s->rwstate = SSL_NOTHING;
  680. /* s->s3->rrec.type - is the type of record
  681. * s->s3->rrec.data - data
  682. * s->s3->rrec.off - offset into 'data' for next read
  683. * s->s3->rrec.length - number of bytes. */
  684. rr = &s->s3->rrec;
  685. /* get new packet if necessary */
  686. if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
  687. ret = ssl3_get_record(s);
  688. if (ret <= 0) {
  689. return ret;
  690. }
  691. }
  692. /* we now have a packet which can be read and processed */
  693. /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
  694. * ssl3_get_finished. */
  695. if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
  696. rr->type != SSL3_RT_ALERT) {
  697. al = SSL_AD_UNEXPECTED_MESSAGE;
  698. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
  699. SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  700. goto f_err;
  701. }
  702. /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
  703. * Handshake record. */
  704. if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
  705. al = SSL_AD_UNEXPECTED_MESSAGE;
  706. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
  707. goto f_err;
  708. }
  709. /* If the other end has shut down, throw anything we read away (even in
  710. * 'peek' mode) */
  711. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  712. rr->length = 0;
  713. s->rwstate = SSL_NOTHING;
  714. return 0;
  715. }
  716. if (type == rr->type) {
  717. /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
  718. /* make sure that we are not getting application data when we are doing a
  719. * handshake for the first time */
  720. if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
  721. s->aead_read_ctx == NULL) {
  722. /* TODO(davidben): Is this check redundant with the handshake_func
  723. * check? */
  724. al = SSL_AD_UNEXPECTED_MESSAGE;
  725. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
  726. goto f_err;
  727. }
  728. if (len <= 0) {
  729. return len;
  730. }
  731. if ((unsigned int)len > rr->length) {
  732. n = rr->length;
  733. } else {
  734. n = (unsigned int)len;
  735. }
  736. memcpy(buf, &(rr->data[rr->off]), n);
  737. if (!peek) {
  738. rr->length -= n;
  739. rr->off += n;
  740. if (rr->length == 0) {
  741. s->rstate = SSL_ST_READ_HEADER;
  742. rr->off = 0;
  743. if (s->s3->rbuf.left == 0) {
  744. ssl3_release_read_buffer(s);
  745. }
  746. }
  747. }
  748. return n;
  749. }
  750. /* Process unexpected records. */
  751. if (rr->type == SSL3_RT_HANDSHAKE) {
  752. /* If peer renegotiations are disabled, all out-of-order handshake records
  753. * are fatal. Renegotiations as a server are never supported. */
  754. if (!s->accept_peer_renegotiations || s->server) {
  755. al = SSL_AD_NO_RENEGOTIATION;
  756. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
  757. goto f_err;
  758. }
  759. /* HelloRequests may be fragmented across multiple records. */
  760. const size_t size = sizeof(s->s3->handshake_fragment);
  761. const size_t avail = size - s->s3->handshake_fragment_len;
  762. const size_t todo = (rr->length < avail) ? rr->length : avail;
  763. memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
  764. &rr->data[rr->off], todo);
  765. rr->off += todo;
  766. rr->length -= todo;
  767. s->s3->handshake_fragment_len += todo;
  768. if (s->s3->handshake_fragment_len < size) {
  769. goto start; /* fragment was too small */
  770. }
  771. /* Parse out and consume a HelloRequest. */
  772. if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST ||
  773. s->s3->handshake_fragment[1] != 0 ||
  774. s->s3->handshake_fragment[2] != 0 ||
  775. s->s3->handshake_fragment[3] != 0) {
  776. al = SSL_AD_DECODE_ERROR;
  777. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
  778. goto f_err;
  779. }
  780. s->s3->handshake_fragment_len = 0;
  781. if (s->msg_callback) {
  782. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  783. s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
  784. }
  785. if (!SSL_is_init_finished(s) || !s->s3->initial_handshake_complete) {
  786. /* This cannot happen. If a handshake is in progress, |type| must be
  787. * |SSL3_RT_HANDSHAKE|. */
  788. assert(0);
  789. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
  790. goto err;
  791. }
  792. /* Renegotiation is only supported at quiescent points in the application
  793. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  794. * the record-layer be idle and avoid complexities of sending a handshake
  795. * record while an application_data record is being written. */
  796. if (s->s3->wbuf.left != 0 || s->s3->rbuf.left != 0) {
  797. al = SSL_AD_NO_RENEGOTIATION;
  798. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
  799. goto f_err;
  800. }
  801. /* Begin a new handshake. */
  802. s->state = SSL_ST_CONNECT;
  803. i = s->handshake_func(s);
  804. if (i < 0) {
  805. return i;
  806. }
  807. if (i == 0) {
  808. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  809. return -1;
  810. }
  811. /* The handshake completed synchronously. Continue reading records. */
  812. goto start;
  813. }
  814. /* If an alert record, process one alert out of the record. Note that we allow
  815. * a single record to contain multiple alerts. */
  816. if (rr->type == SSL3_RT_ALERT) {
  817. /* Alerts may not be fragmented. */
  818. if (rr->length < 2) {
  819. al = SSL_AD_DECODE_ERROR;
  820. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
  821. goto f_err;
  822. }
  823. if (s->msg_callback) {
  824. s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
  825. s->msg_callback_arg);
  826. }
  827. const uint8_t alert_level = rr->data[rr->off++];
  828. const uint8_t alert_descr = rr->data[rr->off++];
  829. rr->length -= 2;
  830. if (s->info_callback != NULL) {
  831. cb = s->info_callback;
  832. } else if (s->ctx->info_callback != NULL) {
  833. cb = s->ctx->info_callback;
  834. }
  835. if (cb != NULL) {
  836. uint16_t alert = (alert_level << 8) | alert_descr;
  837. cb(s, SSL_CB_READ_ALERT, alert);
  838. }
  839. if (alert_level == SSL3_AL_WARNING) {
  840. s->s3->warn_alert = alert_descr;
  841. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  842. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  843. return 0;
  844. }
  845. /* This is a warning but we receive it if we requested renegotiation and
  846. * the peer denied it. Terminate with a fatal alert because if
  847. * application tried to renegotiatie it presumably had a good reason and
  848. * expects it to succeed.
  849. *
  850. * In future we might have a renegotiation where we don't care if the
  851. * peer refused it where we carry on. */
  852. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  853. al = SSL_AD_HANDSHAKE_FAILURE;
  854. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
  855. goto f_err;
  856. }
  857. } else if (alert_level == SSL3_AL_FATAL) {
  858. char tmp[16];
  859. s->rwstate = SSL_NOTHING;
  860. s->s3->fatal_alert = alert_descr;
  861. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
  862. SSL_AD_REASON_OFFSET + alert_descr);
  863. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  864. ERR_add_error_data(2, "SSL alert number ", tmp);
  865. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  866. SSL_CTX_remove_session(s->ctx, s->session);
  867. return 0;
  868. } else {
  869. al = SSL_AD_ILLEGAL_PARAMETER;
  870. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
  871. goto f_err;
  872. }
  873. goto start;
  874. }
  875. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  876. /* but we have not received a shutdown */
  877. s->rwstate = SSL_NOTHING;
  878. rr->length = 0;
  879. return 0;
  880. }
  881. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  882. /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
  883. * record payload has to look like */
  884. if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
  885. al = SSL_AD_ILLEGAL_PARAMETER;
  886. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  887. goto f_err;
  888. }
  889. /* Check we have a cipher to change to */
  890. if (s->s3->tmp.new_cipher == NULL) {
  891. al = SSL_AD_UNEXPECTED_MESSAGE;
  892. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
  893. goto f_err;
  894. }
  895. if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
  896. al = SSL_AD_UNEXPECTED_MESSAGE;
  897. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
  898. goto f_err;
  899. }
  900. s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
  901. rr->length = 0;
  902. if (s->msg_callback) {
  903. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
  904. s->msg_callback_arg);
  905. }
  906. s->s3->change_cipher_spec = 1;
  907. if (!ssl3_do_change_cipher_spec(s)) {
  908. goto err;
  909. } else {
  910. goto start;
  911. }
  912. }
  913. /* We already handled these. */
  914. assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
  915. rr->type != SSL3_RT_HANDSHAKE);
  916. al = SSL_AD_UNEXPECTED_MESSAGE;
  917. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
  918. f_err:
  919. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  920. err:
  921. return -1;
  922. }
  923. int ssl3_do_change_cipher_spec(SSL *s) {
  924. int i;
  925. if (s->state & SSL_ST_ACCEPT) {
  926. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  927. } else {
  928. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  929. }
  930. if (s->s3->tmp.key_block == NULL) {
  931. if (s->session == NULL || s->session->master_key_length == 0) {
  932. /* might happen if dtls1_read_bytes() calls this */
  933. OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
  934. SSL_R_CCS_RECEIVED_EARLY);
  935. return 0;
  936. }
  937. s->session->cipher = s->s3->tmp.new_cipher;
  938. if (!s->enc_method->setup_key_block(s)) {
  939. return 0;
  940. }
  941. }
  942. if (!s->enc_method->change_cipher_state(s, i)) {
  943. return 0;
  944. }
  945. return 1;
  946. }
  947. int ssl3_send_alert(SSL *s, int level, int desc) {
  948. /* Map tls/ssl alert value to correct one */
  949. desc = s->enc_method->alert_value(desc);
  950. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
  951. /* SSL 3.0 does not have protocol_version alerts */
  952. desc = SSL_AD_HANDSHAKE_FAILURE;
  953. }
  954. if (desc < 0) {
  955. return -1;
  956. }
  957. /* If a fatal one, remove from cache */
  958. if (level == 2 && s->session != NULL) {
  959. SSL_CTX_remove_session(s->ctx, s->session);
  960. }
  961. s->s3->alert_dispatch = 1;
  962. s->s3->send_alert[0] = level;
  963. s->s3->send_alert[1] = desc;
  964. if (s->s3->wbuf.left == 0) {
  965. /* data is still being written out. */
  966. return s->method->ssl_dispatch_alert(s);
  967. }
  968. /* else data is still being written out, we will get written some time in the
  969. * future */
  970. return -1;
  971. }
  972. int ssl3_dispatch_alert(SSL *s) {
  973. int i, j;
  974. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  975. s->s3->alert_dispatch = 0;
  976. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
  977. if (i <= 0) {
  978. s->s3->alert_dispatch = 1;
  979. } else {
  980. /* Alert sent to BIO. If it is important, flush it now. If the message
  981. * does not get sent due to non-blocking IO, we will not worry too much. */
  982. if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
  983. BIO_flush(s->wbio);
  984. }
  985. if (s->msg_callback) {
  986. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
  987. s->msg_callback_arg);
  988. }
  989. if (s->info_callback != NULL) {
  990. cb = s->info_callback;
  991. } else if (s->ctx->info_callback != NULL) {
  992. cb = s->ctx->info_callback;
  993. }
  994. if (cb != NULL) {
  995. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  996. cb(s, SSL_CB_WRITE_ALERT, j);
  997. }
  998. }
  999. return i;
  1000. }