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

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