Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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