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

799 рядки
28 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 2005 Nokia. All rights reserved.
  59. *
  60. * The portions of the attached software ("Contribution") is developed by
  61. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  62. * license.
  63. *
  64. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  65. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  66. * support (see RFC 4279) to OpenSSL.
  67. *
  68. * No patent licenses or other rights except those expressly stated in
  69. * the OpenSSL open source license shall be deemed granted or received
  70. * expressly, by implication, estoppel, or otherwise.
  71. *
  72. * No assurances are provided by Nokia that the Contribution does not
  73. * infringe the patent or other intellectual property rights of any third
  74. * party or that the license provides you with all the necessary rights
  75. * to make use of the Contribution.
  76. *
  77. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  78. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  79. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  80. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  81. * OTHERWISE. */
  82. #include <openssl/ssl.h>
  83. #include <limits.h>
  84. #include <string.h>
  85. #include <openssl/buf.h>
  86. #include <openssl/bytestring.h>
  87. #include <openssl/err.h>
  88. #include <openssl/mem.h>
  89. #include <openssl/x509.h>
  90. #include "internal.h"
  91. /* An SSL_SESSION is serialized as the following ASN.1 structure:
  92. *
  93. * SSLSession ::= SEQUENCE {
  94. * version INTEGER (1), -- session structure version
  95. * sslVersion INTEGER, -- protocol version number
  96. * cipher OCTET STRING, -- two bytes long
  97. * sessionID OCTET STRING,
  98. * masterKey OCTET STRING,
  99. * time [1] INTEGER, -- seconds since UNIX epoch
  100. * timeout [2] INTEGER, -- in seconds
  101. * peer [3] Certificate OPTIONAL,
  102. * sessionIDContext [4] OCTET STRING OPTIONAL,
  103. * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
  104. * hostName [6] OCTET STRING OPTIONAL,
  105. * -- from server_name extension
  106. * pskIdentity [8] OCTET STRING OPTIONAL,
  107. * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
  108. * ticket [10] OCTET STRING OPTIONAL, -- client-only
  109. * peerSHA256 [13] OCTET STRING OPTIONAL,
  110. * originalHandshakeHash [14] OCTET STRING OPTIONAL,
  111. * signedCertTimestampList [15] OCTET STRING OPTIONAL,
  112. * -- contents of SCT extension
  113. * ocspResponse [16] OCTET STRING OPTIONAL,
  114. * -- stapled OCSP response from the server
  115. * extendedMasterSecret [17] BOOLEAN OPTIONAL,
  116. * groupID [18] INTEGER OPTIONAL,
  117. * -- For historical reasons, for legacy DHE or
  118. * -- static RSA ciphers, this field contains
  119. * -- another value to be discarded.
  120. * certChain [19] SEQUENCE OF Certificate OPTIONAL,
  121. * ticketAgeAdd [21] OCTET STRING OPTIONAL,
  122. * isServer [22] BOOLEAN DEFAULT TRUE,
  123. * }
  124. *
  125. * Note: historically this serialization has included other optional
  126. * fields. Their presense is currently treated as a parse error:
  127. *
  128. * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
  129. * pskIdentityHint [7] OCTET STRING OPTIONAL,
  130. * compressionMethod [11] OCTET STRING OPTIONAL,
  131. * srpUsername [12] OCTET STRING OPTIONAL,
  132. * ticketFlags [20] INTEGER OPTIONAL,
  133. */
  134. static const unsigned kVersion = 1;
  135. static const int kTimeTag =
  136. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
  137. static const int kTimeoutTag =
  138. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
  139. static const int kPeerTag =
  140. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
  141. static const int kSessionIDContextTag =
  142. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
  143. static const int kVerifyResultTag =
  144. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
  145. static const int kHostNameTag =
  146. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
  147. static const int kPSKIdentityTag =
  148. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
  149. static const int kTicketLifetimeHintTag =
  150. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
  151. static const int kTicketTag =
  152. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
  153. static const int kPeerSHA256Tag =
  154. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
  155. static const int kOriginalHandshakeHashTag =
  156. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
  157. static const int kSignedCertTimestampListTag =
  158. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
  159. static const int kOCSPResponseTag =
  160. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
  161. static const int kExtendedMasterSecretTag =
  162. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
  163. static const int kGroupIDTag =
  164. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
  165. static const int kCertChainTag =
  166. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
  167. static const int kTicketAgeAddTag =
  168. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
  169. static const int kIsServerTag =
  170. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
  171. static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
  172. size_t *out_len, int for_ticket) {
  173. CBB cbb, session, child, child2;
  174. if (in == NULL || in->cipher == NULL) {
  175. return 0;
  176. }
  177. CBB_zero(&cbb);
  178. if (!CBB_init(&cbb, 0) ||
  179. !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
  180. !CBB_add_asn1_uint64(&session, kVersion) ||
  181. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  182. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  183. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  184. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  185. /* The session ID is irrelevant for a session ticket. */
  186. !CBB_add_bytes(&child, in->session_id,
  187. for_ticket ? 0 : in->session_id_length) ||
  188. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  189. !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
  190. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  191. goto err;
  192. }
  193. if (in->time < 0) {
  194. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  195. goto err;
  196. }
  197. if (!CBB_add_asn1(&session, &child, kTimeTag) ||
  198. !CBB_add_asn1_uint64(&child, in->time)) {
  199. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  200. goto err;
  201. }
  202. if (in->timeout < 0) {
  203. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  204. goto err;
  205. }
  206. if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
  207. !CBB_add_asn1_uint64(&child, in->timeout)) {
  208. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  209. goto err;
  210. }
  211. /* The peer certificate is only serialized if the SHA-256 isn't
  212. * serialized instead. */
  213. if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
  214. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
  215. if (!CBB_add_asn1(&session, &child, kPeerTag) ||
  216. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  217. CRYPTO_BUFFER_len(buffer))) {
  218. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  219. goto err;
  220. }
  221. }
  222. /* Although it is OPTIONAL and usually empty, OpenSSL has
  223. * historically always encoded the sid_ctx. */
  224. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  225. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  226. !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
  227. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  228. goto err;
  229. }
  230. if (in->verify_result != X509_V_OK) {
  231. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  232. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  233. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  234. goto err;
  235. }
  236. }
  237. if (in->tlsext_hostname) {
  238. if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
  239. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  240. !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
  241. strlen(in->tlsext_hostname))) {
  242. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  243. goto err;
  244. }
  245. }
  246. if (in->psk_identity) {
  247. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  248. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  249. !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
  250. strlen(in->psk_identity))) {
  251. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  252. goto err;
  253. }
  254. }
  255. if (in->tlsext_tick_lifetime_hint > 0) {
  256. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  257. !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
  258. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  259. goto err;
  260. }
  261. }
  262. if (in->tlsext_tick && !for_ticket) {
  263. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  264. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  265. !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  267. goto err;
  268. }
  269. }
  270. if (in->peer_sha256_valid) {
  271. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  272. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  273. !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
  274. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  275. goto err;
  276. }
  277. }
  278. if (in->original_handshake_hash_len > 0) {
  279. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  280. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  281. !CBB_add_bytes(&child2, in->original_handshake_hash,
  282. in->original_handshake_hash_len)) {
  283. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  284. goto err;
  285. }
  286. }
  287. if (in->tlsext_signed_cert_timestamp_list_length > 0) {
  288. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  289. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  290. !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
  291. in->tlsext_signed_cert_timestamp_list_length)) {
  292. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. }
  296. if (in->ocsp_response_length > 0) {
  297. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  298. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  299. !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
  300. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  301. goto err;
  302. }
  303. }
  304. if (in->extended_master_secret) {
  305. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  306. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  307. !CBB_add_u8(&child2, 0xff)) {
  308. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  309. goto err;
  310. }
  311. }
  312. if (in->group_id > 0 &&
  313. (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
  314. !CBB_add_asn1_uint64(&child, in->group_id))) {
  315. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  316. goto err;
  317. }
  318. /* The certificate chain is only serialized if the leaf's SHA-256 isn't
  319. * serialized instead. */
  320. if (in->certs != NULL &&
  321. !in->peer_sha256_valid &&
  322. sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
  323. if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
  324. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  325. goto err;
  326. }
  327. for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
  328. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
  329. if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  330. CRYPTO_BUFFER_len(buffer))) {
  331. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  332. goto err;
  333. }
  334. }
  335. }
  336. if (in->ticket_age_add_valid) {
  337. if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
  338. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  339. !CBB_add_u32(&child2, in->ticket_age_add)) {
  340. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  341. goto err;
  342. }
  343. }
  344. if (!in->is_server) {
  345. if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
  346. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  347. !CBB_add_u8(&child2, 0x00)) {
  348. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  349. goto err;
  350. }
  351. }
  352. if (!CBB_finish(&cbb, out_data, out_len)) {
  353. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  354. goto err;
  355. }
  356. return 1;
  357. err:
  358. CBB_cleanup(&cbb);
  359. return 0;
  360. }
  361. int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
  362. size_t *out_len) {
  363. if (in->not_resumable) {
  364. /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
  365. * called on a TLS 1.3 or False Started connection, serialize with a
  366. * placeholder value so it is not accidentally deserialized into a resumable
  367. * one. */
  368. static const char kNotResumableSession[] = "NOT RESUMABLE";
  369. *out_len = strlen(kNotResumableSession);
  370. *out_data = BUF_memdup(kNotResumableSession, *out_len);
  371. if (*out_data == NULL) {
  372. return 0;
  373. }
  374. return 1;
  375. }
  376. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
  377. }
  378. int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
  379. size_t *out_len) {
  380. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
  381. }
  382. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  383. uint8_t *out;
  384. size_t len;
  385. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  386. return -1;
  387. }
  388. if (len > INT_MAX) {
  389. OPENSSL_free(out);
  390. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  391. return -1;
  392. }
  393. if (pp) {
  394. memcpy(*pp, out, len);
  395. *pp += len;
  396. }
  397. OPENSSL_free(out);
  398. return len;
  399. }
  400. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  401. * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
  402. * entry, if |*out| is not NULL, it frees the existing contents. If
  403. * the element was not found, it sets |*out| to NULL. It returns one
  404. * on success, whether or not the element was found, and zero on
  405. * decode error. */
  406. static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
  407. CBS value;
  408. int present;
  409. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  410. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  411. return 0;
  412. }
  413. if (present) {
  414. if (CBS_contains_zero_byte(&value)) {
  415. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  416. return 0;
  417. }
  418. if (!CBS_strdup(&value, out)) {
  419. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  420. return 0;
  421. }
  422. } else {
  423. OPENSSL_free(*out);
  424. *out = NULL;
  425. }
  426. return 1;
  427. }
  428. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  429. * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  430. * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  431. * contents. On entry, if the element was not found, it sets
  432. * |*out_ptr| to NULL. It returns one on success, whether or not the
  433. * element was found, and zero on decode error. */
  434. static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
  435. size_t *out_len, unsigned tag) {
  436. CBS value;
  437. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  439. return 0;
  440. }
  441. if (!CBS_stow(&value, out_ptr, out_len)) {
  442. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  443. return 0;
  444. }
  445. return 1;
  446. }
  447. /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
  448. * explicitly tagged with |tag| of size at most |max_out|. */
  449. static int SSL_SESSION_parse_bounded_octet_string(
  450. CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
  451. CBS value;
  452. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
  453. CBS_len(&value) > max_out) {
  454. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  455. return 0;
  456. }
  457. memcpy(out, CBS_data(&value), CBS_len(&value));
  458. *out_len = (uint8_t)CBS_len(&value);
  459. return 1;
  460. }
  461. static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
  462. long default_value) {
  463. uint64_t value;
  464. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  465. (uint64_t)default_value) ||
  466. value > LONG_MAX) {
  467. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  468. return 0;
  469. }
  470. *out = (long)value;
  471. return 1;
  472. }
  473. static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
  474. uint32_t default_value) {
  475. uint64_t value;
  476. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  477. (uint64_t)default_value) ||
  478. value > 0xffffffff) {
  479. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  480. return 0;
  481. }
  482. *out = (uint32_t)value;
  483. return 1;
  484. }
  485. static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
  486. SSL_SESSION *ret = SSL_SESSION_new();
  487. if (ret == NULL) {
  488. goto err;
  489. }
  490. CBS session;
  491. uint64_t version, ssl_version;
  492. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  493. !CBS_get_asn1_uint64(&session, &version) ||
  494. version != kVersion ||
  495. !CBS_get_asn1_uint64(&session, &ssl_version)) {
  496. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  497. goto err;
  498. }
  499. ret->ssl_version = ssl_version;
  500. CBS cipher;
  501. uint16_t cipher_value;
  502. if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  503. !CBS_get_u16(&cipher, &cipher_value) ||
  504. CBS_len(&cipher) != 0) {
  505. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  506. goto err;
  507. }
  508. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  509. if (ret->cipher == NULL) {
  510. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
  511. goto err;
  512. }
  513. CBS session_id, master_key;
  514. if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  515. CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
  516. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  517. CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  518. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  519. goto err;
  520. }
  521. memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  522. ret->session_id_length = CBS_len(&session_id);
  523. memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  524. ret->master_key_length = CBS_len(&master_key);
  525. CBS child;
  526. uint64_t time, timeout;
  527. if (!CBS_get_asn1(&session, &child, kTimeTag) ||
  528. !CBS_get_asn1_uint64(&child, &time) ||
  529. time > LONG_MAX ||
  530. !CBS_get_asn1(&session, &child, kTimeoutTag) ||
  531. !CBS_get_asn1_uint64(&child, &timeout) ||
  532. timeout > LONG_MAX) {
  533. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  534. goto err;
  535. }
  536. ret->time = (long)time;
  537. ret->timeout = (long)timeout;
  538. CBS peer;
  539. int has_peer;
  540. if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  541. (has_peer && CBS_len(&peer) == 0)) {
  542. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  543. goto err;
  544. }
  545. /* |peer| is processed with the certificate chain. */
  546. if (!SSL_SESSION_parse_bounded_octet_string(
  547. &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
  548. kSessionIDContextTag) ||
  549. !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
  550. X509_V_OK) ||
  551. !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
  552. kHostNameTag) ||
  553. !SSL_SESSION_parse_string(&session, &ret->psk_identity,
  554. kPSKIdentityTag) ||
  555. !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
  556. kTicketLifetimeHintTag, 0) ||
  557. !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
  558. &ret->tlsext_ticklen, kTicketTag)) {
  559. goto err;
  560. }
  561. if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
  562. CBS peer_sha256;
  563. if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
  564. !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
  565. CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
  566. CBS_len(&child) != 0) {
  567. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  568. goto err;
  569. }
  570. memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
  571. ret->peer_sha256_valid = 1;
  572. } else {
  573. ret->peer_sha256_valid = 0;
  574. }
  575. if (!SSL_SESSION_parse_bounded_octet_string(
  576. &session, ret->original_handshake_hash,
  577. &ret->original_handshake_hash_len,
  578. sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
  579. !SSL_SESSION_parse_octet_string(
  580. &session, &ret->tlsext_signed_cert_timestamp_list,
  581. &ret->tlsext_signed_cert_timestamp_list_length,
  582. kSignedCertTimestampListTag) ||
  583. !SSL_SESSION_parse_octet_string(
  584. &session, &ret->ocsp_response, &ret->ocsp_response_length,
  585. kOCSPResponseTag)) {
  586. goto err;
  587. }
  588. int extended_master_secret;
  589. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  590. kExtendedMasterSecretTag,
  591. 0 /* default to false */)) {
  592. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  593. goto err;
  594. }
  595. ret->extended_master_secret = !!extended_master_secret;
  596. uint32_t value;
  597. if (!SSL_SESSION_parse_u32(&session, &value, kGroupIDTag, 0)) {
  598. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  599. goto err;
  600. }
  601. /* Historically, the group_id field was used for key-exchange-specific
  602. * information. Discard all but the group ID. */
  603. if (ret->cipher->algorithm_mkey & (SSL_kRSA | SSL_kDHE)) {
  604. value = 0;
  605. }
  606. if (value > 0xffff) {
  607. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  608. goto err;
  609. }
  610. ret->group_id = (uint16_t)value;
  611. CBS cert_chain;
  612. CBS_init(&cert_chain, NULL, 0);
  613. int has_cert_chain;
  614. if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
  615. kCertChainTag) ||
  616. (has_cert_chain && CBS_len(&cert_chain) == 0)) {
  617. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  618. goto err;
  619. }
  620. if (has_cert_chain && !has_peer) {
  621. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  622. goto err;
  623. }
  624. if (has_peer || has_cert_chain) {
  625. ret->certs = sk_CRYPTO_BUFFER_new_null();
  626. if (ret->certs == NULL) {
  627. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  628. goto err;
  629. }
  630. if (has_peer) {
  631. /* TODO(agl): this should use the |SSL_CTX|'s pool. */
  632. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL);
  633. if (buffer == NULL ||
  634. !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
  635. CRYPTO_BUFFER_free(buffer);
  636. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  637. goto err;
  638. }
  639. }
  640. while (CBS_len(&cert_chain) > 0) {
  641. CBS cert;
  642. if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
  643. CBS_len(&cert) == 0) {
  644. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  645. goto err;
  646. }
  647. /* TODO(agl): this should use the |SSL_CTX|'s pool. */
  648. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL);
  649. if (buffer == NULL ||
  650. !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
  651. CRYPTO_BUFFER_free(buffer);
  652. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  653. goto err;
  654. }
  655. }
  656. }
  657. if (!ssl_session_x509_cache_objects(ret)) {
  658. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  659. goto err;
  660. }
  661. CBS age_add;
  662. int age_add_present;
  663. if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
  664. kTicketAgeAddTag) ||
  665. (age_add_present &&
  666. !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
  667. CBS_len(&age_add) != 0) {
  668. goto err;
  669. }
  670. ret->ticket_age_add_valid = age_add_present;
  671. int is_server;
  672. if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
  673. 1 /* default to true */)) {
  674. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  675. goto err;
  676. }
  677. /* TODO: in time we can include |is_server| for servers too, then we can
  678. enforce that client and server sessions are never mixed up. */
  679. ret->is_server = is_server;
  680. if (CBS_len(&session) != 0) {
  681. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  682. goto err;
  683. }
  684. return ret;
  685. err:
  686. SSL_SESSION_free(ret);
  687. return NULL;
  688. }
  689. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
  690. CBS cbs;
  691. CBS_init(&cbs, in, in_len);
  692. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  693. if (ret == NULL) {
  694. return NULL;
  695. }
  696. if (CBS_len(&cbs) != 0) {
  697. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  698. SSL_SESSION_free(ret);
  699. return NULL;
  700. }
  701. return ret;
  702. }
  703. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  704. if (length < 0) {
  705. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  706. return NULL;
  707. }
  708. CBS cbs;
  709. CBS_init(&cbs, *pp, length);
  710. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  711. if (ret == NULL) {
  712. return NULL;
  713. }
  714. if (a) {
  715. SSL_SESSION_free(*a);
  716. *a = ret;
  717. }
  718. *pp = CBS_data(&cbs);
  719. return ret;
  720. }