You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

519 lines
17 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 <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.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 "../crypto/internal.h"
  118. #include "internal.h"
  119. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
  120. /* ssl3_get_record reads a new input record. On success, it places it in
  121. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  122. * more data is needed. */
  123. static int ssl3_get_record(SSL *ssl) {
  124. again:
  125. switch (ssl->s3->recv_shutdown) {
  126. case ssl_shutdown_none:
  127. break;
  128. case ssl_shutdown_fatal_alert:
  129. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  130. return -1;
  131. case ssl_shutdown_close_notify:
  132. return 0;
  133. }
  134. CBS body;
  135. uint8_t type, alert = SSL_AD_DECODE_ERROR;
  136. size_t consumed;
  137. enum ssl_open_record_t open_ret =
  138. tls_open_record(ssl, &type, &body, &consumed, &alert,
  139. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
  140. if (open_ret != ssl_open_record_partial) {
  141. ssl_read_buffer_consume(ssl, consumed);
  142. }
  143. switch (open_ret) {
  144. case ssl_open_record_partial: {
  145. int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
  146. if (read_ret <= 0) {
  147. return read_ret;
  148. }
  149. goto again;
  150. }
  151. case ssl_open_record_success:
  152. if (CBS_len(&body) > 0xffff) {
  153. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  154. return -1;
  155. }
  156. SSL3_RECORD *rr = &ssl->s3->rrec;
  157. rr->type = type;
  158. rr->length = (uint16_t)CBS_len(&body);
  159. rr->data = (uint8_t *)CBS_data(&body);
  160. return 1;
  161. case ssl_open_record_discard:
  162. goto again;
  163. case ssl_open_record_close_notify:
  164. return 0;
  165. case ssl_open_record_fatal_alert:
  166. return -1;
  167. case ssl_open_record_error:
  168. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  169. return -1;
  170. }
  171. assert(0);
  172. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  173. return -1;
  174. }
  175. int ssl3_write_app_data(SSL *ssl, const uint8_t *buf, int len) {
  176. assert(ssl_can_write(ssl));
  177. assert(ssl->s3->aead_write_ctx != NULL);
  178. unsigned tot, n, nw;
  179. assert(ssl->s3->wnum <= INT_MAX);
  180. tot = ssl->s3->wnum;
  181. ssl->s3->wnum = 0;
  182. /* Ensure that if we end up with a smaller value of data to write out than
  183. * the the original len from a write which didn't complete for non-blocking
  184. * I/O and also somehow ended up avoiding the check for this in
  185. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  186. * end up with (len-tot) as a large number that will then promptly send
  187. * beyond the end of the users buffer ... so we trap and report the error in
  188. * a way the user will notice. */
  189. if (len < 0 || (size_t)len < tot) {
  190. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  191. return -1;
  192. }
  193. n = len - tot;
  194. for (;;) {
  195. /* max contains the maximum number of bytes that we can put into a
  196. * record. */
  197. unsigned max = ssl->max_send_fragment;
  198. if (n > max) {
  199. nw = max;
  200. } else {
  201. nw = n;
  202. }
  203. int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &buf[tot], nw);
  204. if (ret <= 0) {
  205. ssl->s3->wnum = tot;
  206. return ret;
  207. }
  208. if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
  209. return tot + ret;
  210. }
  211. n -= ret;
  212. tot += ret;
  213. }
  214. }
  215. static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
  216. unsigned int len) {
  217. if (ssl->s3->wpend_tot > (int)len ||
  218. (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
  219. ssl->s3->wpend_buf != buf) ||
  220. ssl->s3->wpend_type != type) {
  221. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
  222. return -1;
  223. }
  224. int ret = ssl_write_buffer_flush(ssl);
  225. if (ret <= 0) {
  226. return ret;
  227. }
  228. return ssl->s3->wpend_ret;
  229. }
  230. /* do_ssl3_write writes an SSL record of the given type. */
  231. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
  232. /* If there is still data from the previous record, flush it. */
  233. if (ssl_write_buffer_is_pending(ssl)) {
  234. return ssl3_write_pending(ssl, type, buf, len);
  235. }
  236. /* The handshake flight buffer is mutually exclusive with application data.
  237. *
  238. * TODO(davidben): This will not be true when closure alerts use this. */
  239. if (ssl->s3->pending_flight != NULL) {
  240. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  241. return -1;
  242. }
  243. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  244. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  245. return -1;
  246. }
  247. if (len == 0) {
  248. return 0;
  249. }
  250. size_t max_out = len + SSL_max_seal_overhead(ssl);
  251. if (max_out < len) {
  252. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  253. return -1;
  254. }
  255. uint8_t *out;
  256. size_t ciphertext_len;
  257. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  258. !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
  259. return -1;
  260. }
  261. ssl_write_buffer_set_len(ssl, ciphertext_len);
  262. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  263. * later */
  264. ssl->s3->wpend_tot = len;
  265. ssl->s3->wpend_buf = buf;
  266. ssl->s3->wpend_type = type;
  267. ssl->s3->wpend_ret = len;
  268. /* we now just need to write the buffer */
  269. return ssl3_write_pending(ssl, type, buf, len);
  270. }
  271. static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) {
  272. SSL3_RECORD *rr = &ssl->s3->rrec;
  273. if (len <= 0) {
  274. return len;
  275. }
  276. if (len > (int)rr->length) {
  277. len = (int)rr->length;
  278. }
  279. OPENSSL_memcpy(out, rr->data, len);
  280. if (!peek) {
  281. rr->length -= len;
  282. rr->data += len;
  283. if (rr->length == 0) {
  284. /* The record has been consumed, so we may now clear the buffer. */
  285. ssl_read_buffer_discard(ssl);
  286. }
  287. }
  288. return len;
  289. }
  290. int ssl3_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
  291. int peek) {
  292. assert(ssl_can_read(ssl));
  293. assert(ssl->s3->aead_read_ctx != NULL);
  294. *out_got_handshake = 0;
  295. ssl->method->release_current_message(ssl, 0 /* don't free buffer */);
  296. SSL3_RECORD *rr = &ssl->s3->rrec;
  297. for (;;) {
  298. /* A previous iteration may have read a partial handshake message. Do not
  299. * allow more app data in that case. */
  300. int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0;
  301. /* Get new packet if necessary. */
  302. if (rr->length == 0 && !has_hs_data) {
  303. int ret = ssl3_get_record(ssl);
  304. if (ret <= 0) {
  305. return ret;
  306. }
  307. }
  308. if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) {
  309. /* If reading 0-RTT data, reject handshake data. 0-RTT data is terminated
  310. * by an alert. */
  311. if (SSL_in_init(ssl)) {
  312. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  313. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  314. return -1;
  315. }
  316. /* Post-handshake data prior to TLS 1.3 is always renegotiation, which we
  317. * never accept as a server. Otherwise |ssl3_get_message| will send
  318. * |SSL_R_EXCESSIVE_MESSAGE_SIZE|. */
  319. if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  320. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  321. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  322. return -1;
  323. }
  324. /* Parse post-handshake handshake messages. */
  325. int ret = ssl3_get_message(ssl);
  326. if (ret <= 0) {
  327. return ret;
  328. }
  329. *out_got_handshake = 1;
  330. return -1;
  331. }
  332. /* Handle the end_of_early_data alert. */
  333. if (rr->type == SSL3_RT_ALERT &&
  334. rr->length == 2 &&
  335. rr->data[0] == SSL3_AL_WARNING &&
  336. rr->data[1] == TLS1_AD_END_OF_EARLY_DATA &&
  337. ssl->server &&
  338. ssl->s3->hs != NULL &&
  339. ssl->s3->hs->can_early_read &&
  340. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  341. /* Consume the record. */
  342. rr->length = 0;
  343. ssl_read_buffer_discard(ssl);
  344. /* Stop accepting early data. */
  345. ssl->s3->hs->can_early_read = 0;
  346. *out_got_handshake = 1;
  347. return -1;
  348. }
  349. if (rr->type != SSL3_RT_APPLICATION_DATA) {
  350. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  351. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  352. return -1;
  353. }
  354. if (rr->length != 0) {
  355. return consume_record(ssl, buf, len, peek);
  356. }
  357. /* Discard empty records and loop again. */
  358. }
  359. }
  360. int ssl3_read_change_cipher_spec(SSL *ssl) {
  361. SSL3_RECORD *rr = &ssl->s3->rrec;
  362. if (rr->length == 0) {
  363. int ret = ssl3_get_record(ssl);
  364. if (ret <= 0) {
  365. return ret;
  366. }
  367. }
  368. if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
  369. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  370. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  371. return -1;
  372. }
  373. if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
  374. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  375. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  376. return -1;
  377. }
  378. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data,
  379. rr->length);
  380. rr->length = 0;
  381. ssl_read_buffer_discard(ssl);
  382. return 1;
  383. }
  384. void ssl3_read_close_notify(SSL *ssl) {
  385. /* Read records until an error or close_notify. */
  386. while (ssl3_get_record(ssl) > 0) {
  387. ;
  388. }
  389. }
  390. int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) {
  391. SSL3_RECORD *rr = &ssl->s3->rrec;
  392. for (;;) {
  393. /* Get new packet if necessary. */
  394. if (rr->length == 0) {
  395. int ret = ssl3_get_record(ssl);
  396. if (ret <= 0) {
  397. return ret;
  398. }
  399. }
  400. if (rr->type != SSL3_RT_HANDSHAKE) {
  401. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  402. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  403. return -1;
  404. }
  405. if (rr->length != 0) {
  406. return consume_record(ssl, buf, len, 0 /* consume data */);
  407. }
  408. /* Discard empty records and loop again. */
  409. }
  410. }
  411. int ssl3_send_alert(SSL *ssl, int level, int desc) {
  412. /* It is illegal to send an alert when we've already sent a closing one. */
  413. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  414. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  415. return -1;
  416. }
  417. if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
  418. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  419. } else {
  420. assert(level == SSL3_AL_FATAL);
  421. ssl->s3->send_shutdown = ssl_shutdown_fatal_alert;
  422. }
  423. ssl->s3->alert_dispatch = 1;
  424. ssl->s3->send_alert[0] = level;
  425. ssl->s3->send_alert[1] = desc;
  426. if (!ssl_write_buffer_is_pending(ssl)) {
  427. /* Nothing is being written out, so the alert may be dispatched
  428. * immediately. */
  429. return ssl->method->dispatch_alert(ssl);
  430. }
  431. /* The alert will be dispatched later. */
  432. return -1;
  433. }
  434. int ssl3_dispatch_alert(SSL *ssl) {
  435. int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
  436. if (ret <= 0) {
  437. return ret;
  438. }
  439. ssl->s3->alert_dispatch = 0;
  440. /* If the alert is fatal, flush the BIO now. */
  441. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  442. BIO_flush(ssl->wbio);
  443. }
  444. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert,
  445. 2);
  446. int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  447. ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
  448. return 1;
  449. }