25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ssl_asn1.c 24 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/bytestring.h>
  86. #include <openssl/err.h>
  87. #include <openssl/mem.h>
  88. #include <openssl/x509.h>
  89. #include "internal.h"
  90. /* An SSL_SESSION is serialized as the following ASN.1 structure:
  91. *
  92. * SSLSession ::= SEQUENCE {
  93. * version INTEGER (1), -- ignored
  94. * sslVersion INTEGER, -- protocol version number
  95. * cipher OCTET STRING, -- two bytes long
  96. * sessionID OCTET STRING,
  97. * masterKey OCTET STRING,
  98. * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
  99. * timeout [2] INTEGER OPTIONAL, -- in seconds
  100. * peer [3] Certificate OPTIONAL,
  101. * sessionIDContext [4] OCTET STRING OPTIONAL,
  102. * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
  103. * hostName [6] OCTET STRING OPTIONAL,
  104. * -- from server_name extension
  105. * pskIdentity [8] OCTET STRING OPTIONAL,
  106. * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
  107. * ticket [10] OCTET STRING OPTIONAL, -- client-only
  108. * peerSHA256 [13] OCTET STRING OPTIONAL,
  109. * originalHandshakeHash [14] OCTET STRING OPTIONAL,
  110. * signedCertTimestampList [15] OCTET STRING OPTIONAL,
  111. * -- contents of SCT extension
  112. * ocspResponse [16] OCTET STRING OPTIONAL,
  113. * -- stapled OCSP response from the server
  114. * extendedMasterSecret [17] BOOLEAN OPTIONAL,
  115. * keyExchangeInfo [18] INTEGER OPTIONAL,
  116. * certChain [19] SEQUENCE OF Certificate OPTIONAL,
  117. * }
  118. *
  119. * Note: historically this serialization has included other optional
  120. * fields. Their presense is currently treated as a parse error:
  121. *
  122. * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
  123. * pskIdentityHint [7] OCTET STRING OPTIONAL,
  124. * compressionMethod [11] OCTET STRING OPTIONAL,
  125. * srpUsername [12] OCTET STRING OPTIONAL, */
  126. static const unsigned kVersion = 1;
  127. static const int kTimeTag =
  128. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
  129. static const int kTimeoutTag =
  130. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
  131. static const int kPeerTag =
  132. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
  133. static const int kSessionIDContextTag =
  134. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
  135. static const int kVerifyResultTag =
  136. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
  137. static const int kHostNameTag =
  138. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
  139. static const int kPSKIdentityTag =
  140. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
  141. static const int kTicketLifetimeHintTag =
  142. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
  143. static const int kTicketTag =
  144. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
  145. static const int kPeerSHA256Tag =
  146. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
  147. static const int kOriginalHandshakeHashTag =
  148. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
  149. static const int kSignedCertTimestampListTag =
  150. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
  151. static const int kOCSPResponseTag =
  152. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
  153. static const int kExtendedMasterSecretTag =
  154. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
  155. static const int kKeyExchangeInfoTag =
  156. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
  157. static const int kCertChainTag =
  158. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
  159. static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
  160. size_t *out_len, int for_ticket) {
  161. CBB cbb, session, child, child2;
  162. if (in == NULL || in->cipher == NULL) {
  163. return 0;
  164. }
  165. CBB_zero(&cbb);
  166. if (!CBB_init(&cbb, 0) ||
  167. !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
  168. !CBB_add_asn1_uint64(&session, kVersion) ||
  169. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  170. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  171. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  172. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  173. /* The session ID is irrelevant for a session ticket. */
  174. !CBB_add_bytes(&child, in->session_id,
  175. for_ticket ? 0 : in->session_id_length) ||
  176. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  177. !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
  178. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  179. goto err;
  180. }
  181. if (in->time != 0) {
  182. if (!CBB_add_asn1(&session, &child, kTimeTag) ||
  183. !CBB_add_asn1_uint64(&child, in->time)) {
  184. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  185. goto err;
  186. }
  187. }
  188. if (in->timeout != 0) {
  189. if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
  190. !CBB_add_asn1_uint64(&child, in->timeout)) {
  191. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  192. goto err;
  193. }
  194. }
  195. /* The peer certificate is only serialized if the SHA-256 isn't
  196. * serialized instead. */
  197. if (in->peer && !in->peer_sha256_valid) {
  198. if (!CBB_add_asn1(&session, &child, kPeerTag)) {
  199. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  200. goto err;
  201. }
  202. if (!ssl_add_cert_to_cbb(&child, in->peer)) {
  203. goto err;
  204. }
  205. }
  206. /* Although it is OPTIONAL and usually empty, OpenSSL has
  207. * historically always encoded the sid_ctx. */
  208. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  209. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  210. !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
  211. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  212. goto err;
  213. }
  214. if (in->verify_result != X509_V_OK) {
  215. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  216. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  217. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  218. goto err;
  219. }
  220. }
  221. if (in->tlsext_hostname) {
  222. if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
  223. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  224. !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
  225. strlen(in->tlsext_hostname))) {
  226. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  227. goto err;
  228. }
  229. }
  230. if (in->psk_identity) {
  231. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  232. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  233. !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
  234. strlen(in->psk_identity))) {
  235. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  236. goto err;
  237. }
  238. }
  239. if (in->tlsext_tick_lifetime_hint > 0) {
  240. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  241. !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
  242. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  243. goto err;
  244. }
  245. }
  246. if (in->tlsext_tick && !for_ticket) {
  247. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  248. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  249. !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
  250. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  251. goto err;
  252. }
  253. }
  254. if (in->peer_sha256_valid) {
  255. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  256. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  257. !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
  258. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  259. goto err;
  260. }
  261. }
  262. if (in->original_handshake_hash_len > 0) {
  263. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  264. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  265. !CBB_add_bytes(&child2, in->original_handshake_hash,
  266. in->original_handshake_hash_len)) {
  267. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  268. goto err;
  269. }
  270. }
  271. if (in->tlsext_signed_cert_timestamp_list_length > 0) {
  272. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  273. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  274. !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
  275. in->tlsext_signed_cert_timestamp_list_length)) {
  276. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  277. goto err;
  278. }
  279. }
  280. if (in->ocsp_response_length > 0) {
  281. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  282. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  283. !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
  284. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  285. goto err;
  286. }
  287. }
  288. if (in->extended_master_secret) {
  289. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  290. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  291. !CBB_add_u8(&child2, 0xff)) {
  292. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. }
  296. if (in->key_exchange_info > 0 &&
  297. (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
  298. !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
  299. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  300. goto err;
  301. }
  302. /* The certificate chain is only serialized if the leaf's SHA-256 isn't
  303. * serialized instead. */
  304. if (in->cert_chain != NULL && !in->peer_sha256_valid) {
  305. if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
  306. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  307. goto err;
  308. }
  309. size_t i;
  310. for (i = 0; i < sk_X509_num(in->cert_chain); i++) {
  311. if (!ssl_add_cert_to_cbb(&child, sk_X509_value(in->cert_chain, i))) {
  312. goto err;
  313. }
  314. }
  315. }
  316. if (!CBB_finish(&cbb, out_data, out_len)) {
  317. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  318. goto err;
  319. }
  320. return 1;
  321. err:
  322. CBB_cleanup(&cbb);
  323. return 0;
  324. }
  325. int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
  326. size_t *out_len) {
  327. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
  328. }
  329. int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
  330. size_t *out_len) {
  331. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
  332. }
  333. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  334. uint8_t *out;
  335. size_t len;
  336. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  337. return -1;
  338. }
  339. if (len > INT_MAX) {
  340. OPENSSL_free(out);
  341. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  342. return -1;
  343. }
  344. if (pp) {
  345. memcpy(*pp, out, len);
  346. *pp += len;
  347. }
  348. OPENSSL_free(out);
  349. return len;
  350. }
  351. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  352. * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
  353. * entry, if |*out| is not NULL, it frees the existing contents. If
  354. * the element was not found, it sets |*out| to NULL. It returns one
  355. * on success, whether or not the element was found, and zero on
  356. * decode error. */
  357. static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
  358. CBS value;
  359. int present;
  360. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  361. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  362. return 0;
  363. }
  364. if (present) {
  365. if (CBS_contains_zero_byte(&value)) {
  366. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  367. return 0;
  368. }
  369. if (!CBS_strdup(&value, out)) {
  370. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  371. return 0;
  372. }
  373. } else {
  374. OPENSSL_free(*out);
  375. *out = NULL;
  376. }
  377. return 1;
  378. }
  379. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  380. * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  381. * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  382. * contents. On entry, if the element was not found, it sets
  383. * |*out_ptr| to NULL. It returns one on success, whether or not the
  384. * element was found, and zero on decode error. */
  385. static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
  386. size_t *out_len, unsigned tag) {
  387. CBS value;
  388. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  389. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  390. return 0;
  391. }
  392. if (!CBS_stow(&value, out_ptr, out_len)) {
  393. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
  399. * explicitly tagged with |tag| of size at most |max_out|. */
  400. static int SSL_SESSION_parse_bounded_octet_string(
  401. CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) {
  402. CBS value;
  403. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
  404. CBS_len(&value) > max_out) {
  405. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  406. return 0;
  407. }
  408. memcpy(out, CBS_data(&value), CBS_len(&value));
  409. *out_len = (unsigned)CBS_len(&value);
  410. return 1;
  411. }
  412. static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
  413. long default_value) {
  414. uint64_t value;
  415. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  416. (uint64_t)default_value) ||
  417. value > LONG_MAX) {
  418. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  419. return 0;
  420. }
  421. *out = (long)value;
  422. return 1;
  423. }
  424. static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
  425. uint32_t default_value) {
  426. uint64_t value;
  427. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  428. (uint64_t)default_value) ||
  429. value > 0xffffffff) {
  430. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  431. return 0;
  432. }
  433. *out = (uint32_t)value;
  434. return 1;
  435. }
  436. static X509 *parse_x509(CBS *cbs) {
  437. if (CBS_len(cbs) > LONG_MAX) {
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  439. return NULL;
  440. }
  441. const uint8_t *ptr = CBS_data(cbs);
  442. X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
  443. if (ret == NULL) {
  444. return NULL;
  445. }
  446. CBS_skip(cbs, ptr - CBS_data(cbs));
  447. return ret;
  448. }
  449. static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
  450. SSL_SESSION *ret = SSL_SESSION_new();
  451. if (ret == NULL) {
  452. goto err;
  453. }
  454. CBS session;
  455. uint64_t version, ssl_version;
  456. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  457. !CBS_get_asn1_uint64(&session, &version) ||
  458. version != kVersion ||
  459. !CBS_get_asn1_uint64(&session, &ssl_version)) {
  460. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  461. goto err;
  462. }
  463. /* Only support SSLv3/TLS and DTLS. */
  464. if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
  465. (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
  466. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
  467. goto err;
  468. }
  469. ret->ssl_version = ssl_version;
  470. CBS cipher;
  471. uint16_t cipher_value;
  472. if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  473. !CBS_get_u16(&cipher, &cipher_value) ||
  474. CBS_len(&cipher) != 0) {
  475. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  476. goto err;
  477. }
  478. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  479. if (ret->cipher == NULL) {
  480. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
  481. goto err;
  482. }
  483. CBS session_id, master_key;
  484. if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  485. CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
  486. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  487. CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  488. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  489. goto err;
  490. }
  491. memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  492. ret->session_id_length = CBS_len(&session_id);
  493. memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  494. ret->master_key_length = CBS_len(&master_key);
  495. if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) ||
  496. !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) {
  497. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  498. goto err;
  499. }
  500. CBS peer;
  501. int has_peer;
  502. if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
  503. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  504. goto err;
  505. }
  506. X509_free(ret->peer);
  507. ret->peer = NULL;
  508. if (has_peer) {
  509. ret->peer = parse_x509(&peer);
  510. if (ret->peer == NULL) {
  511. goto err;
  512. }
  513. if (CBS_len(&peer) != 0) {
  514. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  515. goto err;
  516. }
  517. }
  518. if (!SSL_SESSION_parse_bounded_octet_string(
  519. &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
  520. kSessionIDContextTag) ||
  521. !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
  522. X509_V_OK) ||
  523. !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
  524. kHostNameTag) ||
  525. !SSL_SESSION_parse_string(&session, &ret->psk_identity,
  526. kPSKIdentityTag) ||
  527. !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
  528. kTicketLifetimeHintTag, 0) ||
  529. !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
  530. &ret->tlsext_ticklen, kTicketTag)) {
  531. goto err;
  532. }
  533. if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
  534. CBS child, peer_sha256;
  535. if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
  536. !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
  537. CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
  538. CBS_len(&child) != 0) {
  539. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  540. goto err;
  541. }
  542. memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
  543. ret->peer_sha256_valid = 1;
  544. } else {
  545. ret->peer_sha256_valid = 0;
  546. }
  547. if (!SSL_SESSION_parse_bounded_octet_string(
  548. &session, ret->original_handshake_hash,
  549. &ret->original_handshake_hash_len,
  550. sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
  551. !SSL_SESSION_parse_octet_string(
  552. &session, &ret->tlsext_signed_cert_timestamp_list,
  553. &ret->tlsext_signed_cert_timestamp_list_length,
  554. kSignedCertTimestampListTag) ||
  555. !SSL_SESSION_parse_octet_string(
  556. &session, &ret->ocsp_response, &ret->ocsp_response_length,
  557. kOCSPResponseTag)) {
  558. goto err;
  559. }
  560. int extended_master_secret;
  561. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  562. kExtendedMasterSecretTag,
  563. 0 /* default to false */)) {
  564. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  565. goto err;
  566. }
  567. ret->extended_master_secret = !!extended_master_secret;
  568. if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
  569. kKeyExchangeInfoTag, 0)) {
  570. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  571. goto err;
  572. }
  573. CBS cert_chain;
  574. int has_cert_chain;
  575. if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
  576. kCertChainTag)) {
  577. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  578. goto err;
  579. }
  580. sk_X509_pop_free(ret->cert_chain, X509_free);
  581. ret->cert_chain = NULL;
  582. if (has_cert_chain) {
  583. ret->cert_chain = sk_X509_new_null();
  584. if (ret->cert_chain == NULL) {
  585. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  586. goto err;
  587. }
  588. while (CBS_len(&cert_chain) > 0) {
  589. X509 *x509 = parse_x509(&cert_chain);
  590. if (x509 == NULL) {
  591. goto err;
  592. }
  593. if (!sk_X509_push(ret->cert_chain, x509)) {
  594. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  595. X509_free(x509);
  596. goto err;
  597. }
  598. }
  599. }
  600. if (CBS_len(&session) != 0) {
  601. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  602. goto err;
  603. }
  604. return ret;
  605. err:
  606. SSL_SESSION_free(ret);
  607. return NULL;
  608. }
  609. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
  610. CBS cbs;
  611. CBS_init(&cbs, in, in_len);
  612. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  613. if (ret == NULL) {
  614. return NULL;
  615. }
  616. if (CBS_len(&cbs) != 0) {
  617. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  618. SSL_SESSION_free(ret);
  619. return NULL;
  620. }
  621. return ret;
  622. }
  623. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  624. if (length < 0) {
  625. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  626. return NULL;
  627. }
  628. CBS cbs;
  629. CBS_init(&cbs, *pp, length);
  630. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  631. if (ret == NULL) {
  632. return NULL;
  633. }
  634. if (a) {
  635. SSL_SESSION_free(*a);
  636. *a = ret;
  637. }
  638. *pp = CBS_data(&cbs);
  639. return ret;
  640. }