ssl_asn1.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. namespace bssl {
  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, uint8_t **out_data,
  189. size_t *out_len, int for_ticket) {
  190. if (in == NULL || in->cipher == NULL) {
  191. return 0;
  192. }
  193. ScopedCBB cbb;
  194. CBB session, child, child2;
  195. if (!CBB_init(cbb.get(), 0) ||
  196. !CBB_add_asn1(cbb.get(), &session, CBS_ASN1_SEQUENCE) ||
  197. !CBB_add_asn1_uint64(&session, kVersion) ||
  198. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  199. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  200. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  201. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  202. // The session ID is irrelevant for a session ticket.
  203. !CBB_add_bytes(&child, in->session_id,
  204. for_ticket ? 0 : in->session_id_length) ||
  205. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  206. !CBB_add_bytes(&child, in->master_key, in->master_key_length) ||
  207. !CBB_add_asn1(&session, &child, kTimeTag) ||
  208. !CBB_add_asn1_uint64(&child, in->time) ||
  209. !CBB_add_asn1(&session, &child, kTimeoutTag) ||
  210. !CBB_add_asn1_uint64(&child, in->timeout)) {
  211. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  212. return 0;
  213. }
  214. // The peer certificate is only serialized if the SHA-256 isn't
  215. // serialized instead.
  216. if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
  217. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
  218. if (!CBB_add_asn1(&session, &child, kPeerTag) ||
  219. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  220. CRYPTO_BUFFER_len(buffer))) {
  221. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  222. return 0;
  223. }
  224. }
  225. // Although it is OPTIONAL and usually empty, OpenSSL has
  226. // historically always encoded the sid_ctx.
  227. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  228. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  229. !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
  230. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  231. return 0;
  232. }
  233. if (in->verify_result != X509_V_OK) {
  234. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  235. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  236. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  237. return 0;
  238. }
  239. }
  240. if (in->psk_identity) {
  241. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  242. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  243. !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
  244. strlen(in->psk_identity))) {
  245. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  246. return 0;
  247. }
  248. }
  249. if (in->tlsext_tick_lifetime_hint > 0) {
  250. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  251. !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
  252. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  253. return 0;
  254. }
  255. }
  256. if (in->tlsext_tick && !for_ticket) {
  257. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  258. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  259. !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
  260. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  261. return 0;
  262. }
  263. }
  264. if (in->peer_sha256_valid) {
  265. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  266. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  267. !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
  268. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  269. return 0;
  270. }
  271. }
  272. if (in->original_handshake_hash_len > 0) {
  273. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  274. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  275. !CBB_add_bytes(&child2, in->original_handshake_hash,
  276. in->original_handshake_hash_len)) {
  277. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  278. return 0;
  279. }
  280. }
  281. if (in->signed_cert_timestamp_list != nullptr) {
  282. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  283. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  284. !CBB_add_bytes(&child2,
  285. CRYPTO_BUFFER_data(in->signed_cert_timestamp_list),
  286. CRYPTO_BUFFER_len(in->signed_cert_timestamp_list))) {
  287. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  288. return 0;
  289. }
  290. }
  291. if (in->ocsp_response != nullptr) {
  292. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  293. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  294. !CBB_add_bytes(&child2, CRYPTO_BUFFER_data(in->ocsp_response),
  295. CRYPTO_BUFFER_len(in->ocsp_response))) {
  296. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  297. return 0;
  298. }
  299. }
  300. if (in->extended_master_secret) {
  301. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  302. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  303. !CBB_add_u8(&child2, 0xff)) {
  304. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  305. return 0;
  306. }
  307. }
  308. if (in->group_id > 0 &&
  309. (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
  310. !CBB_add_asn1_uint64(&child, in->group_id))) {
  311. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  312. return 0;
  313. }
  314. // The certificate chain is only serialized if the leaf's SHA-256 isn't
  315. // serialized instead.
  316. if (in->certs != NULL &&
  317. !in->peer_sha256_valid &&
  318. sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
  319. if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
  320. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  321. return 0;
  322. }
  323. for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
  324. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
  325. if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  326. CRYPTO_BUFFER_len(buffer))) {
  327. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  328. return 0;
  329. }
  330. }
  331. }
  332. if (in->ticket_age_add_valid) {
  333. if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
  334. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  335. !CBB_add_u32(&child2, in->ticket_age_add)) {
  336. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  337. return 0;
  338. }
  339. }
  340. if (!in->is_server) {
  341. if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
  342. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  343. !CBB_add_u8(&child2, 0x00)) {
  344. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  345. return 0;
  346. }
  347. }
  348. if (in->peer_signature_algorithm != 0 &&
  349. (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
  350. !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
  351. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  352. return 0;
  353. }
  354. if (in->ticket_max_early_data != 0 &&
  355. (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
  356. !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
  357. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  358. return 0;
  359. }
  360. if (in->timeout != in->auth_timeout &&
  361. (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
  362. !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
  363. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  364. return 0;
  365. }
  366. if (in->early_alpn) {
  367. if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
  368. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  369. !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
  370. in->early_alpn_len)) {
  371. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  372. return 0;
  373. }
  374. }
  375. if (!CBB_finish(cbb.get(), out_data, out_len)) {
  376. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  377. return 0;
  378. }
  379. return 1;
  380. }
  381. // SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  382. // explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
  383. // entry, if |*out| is not NULL, it frees the existing contents. If
  384. // the element was not found, it sets |*out| to NULL. It returns one
  385. // on success, whether or not the element was found, and zero on
  386. // decode error.
  387. static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
  388. CBS value;
  389. int present;
  390. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  391. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  392. return 0;
  393. }
  394. if (present) {
  395. if (CBS_contains_zero_byte(&value)) {
  396. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  397. return 0;
  398. }
  399. if (!CBS_strdup(&value, out)) {
  400. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  401. return 0;
  402. }
  403. } else {
  404. OPENSSL_free(*out);
  405. *out = NULL;
  406. }
  407. return 1;
  408. }
  409. // SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  410. // explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  411. // and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  412. // contents. On entry, if the element was not found, it sets
  413. // |*out_ptr| to NULL. It returns one on success, whether or not the
  414. // element was found, and zero on decode error.
  415. static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
  416. size_t *out_len, unsigned tag) {
  417. CBS value;
  418. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  419. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  420. return 0;
  421. }
  422. if (!CBS_stow(&value, out_ptr, out_len)) {
  423. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  424. return 0;
  425. }
  426. return 1;
  427. }
  428. static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, CRYPTO_BUFFER **out,
  429. unsigned tag,
  430. CRYPTO_BUFFER_POOL *pool) {
  431. if (!CBS_peek_asn1_tag(cbs, tag)) {
  432. return 1;
  433. }
  434. CBS child, value;
  435. if (!CBS_get_asn1(cbs, &child, tag) ||
  436. !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
  437. CBS_len(&child) != 0) {
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  439. return 0;
  440. }
  441. CRYPTO_BUFFER_free(*out);
  442. *out = CRYPTO_BUFFER_new_from_CBS(&value, pool);
  443. if (*out == nullptr) {
  444. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  445. return 0;
  446. }
  447. return 1;
  448. }
  449. // SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
  450. // explicitly tagged with |tag| of size at most |max_out|.
  451. static int SSL_SESSION_parse_bounded_octet_string(
  452. CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
  453. CBS value;
  454. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
  455. CBS_len(&value) > max_out) {
  456. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  457. return 0;
  458. }
  459. OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
  460. *out_len = (uint8_t)CBS_len(&value);
  461. return 1;
  462. }
  463. static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
  464. long default_value) {
  465. uint64_t value;
  466. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  467. (uint64_t)default_value) ||
  468. value > LONG_MAX) {
  469. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  470. return 0;
  471. }
  472. *out = (long)value;
  473. return 1;
  474. }
  475. static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
  476. uint32_t default_value) {
  477. uint64_t value;
  478. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  479. (uint64_t)default_value) ||
  480. value > 0xffffffff) {
  481. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  482. return 0;
  483. }
  484. *out = (uint32_t)value;
  485. return 1;
  486. }
  487. static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
  488. uint16_t default_value) {
  489. uint64_t value;
  490. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  491. (uint64_t)default_value) ||
  492. value > 0xffff) {
  493. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  494. return 0;
  495. }
  496. *out = (uint16_t)value;
  497. return 1;
  498. }
  499. UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
  500. const SSL_X509_METHOD *x509_method,
  501. CRYPTO_BUFFER_POOL *pool) {
  502. UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
  503. if (!ret) {
  504. return nullptr;
  505. }
  506. CBS session;
  507. uint64_t version, ssl_version;
  508. uint16_t unused;
  509. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  510. !CBS_get_asn1_uint64(&session, &version) ||
  511. version != kVersion ||
  512. !CBS_get_asn1_uint64(&session, &ssl_version) ||
  513. // Require sessions have versions valid in either TLS or DTLS. The session
  514. // will not be used by the handshake if not applicable, but, for
  515. // simplicity, never parse a session that does not pass
  516. // |ssl_protocol_version_from_wire|.
  517. ssl_version > UINT16_MAX ||
  518. !ssl_protocol_version_from_wire(&unused, ssl_version)) {
  519. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  520. return nullptr;
  521. }
  522. ret->ssl_version = ssl_version;
  523. CBS cipher;
  524. uint16_t cipher_value;
  525. if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  526. !CBS_get_u16(&cipher, &cipher_value) ||
  527. CBS_len(&cipher) != 0) {
  528. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  529. return nullptr;
  530. }
  531. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  532. if (ret->cipher == NULL) {
  533. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
  534. return nullptr;
  535. }
  536. CBS session_id, master_key;
  537. if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  538. CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
  539. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  540. CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  541. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  542. return nullptr;
  543. }
  544. OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  545. ret->session_id_length = CBS_len(&session_id);
  546. OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  547. ret->master_key_length = CBS_len(&master_key);
  548. CBS child;
  549. uint64_t timeout;
  550. if (!CBS_get_asn1(&session, &child, kTimeTag) ||
  551. !CBS_get_asn1_uint64(&child, &ret->time) ||
  552. !CBS_get_asn1(&session, &child, kTimeoutTag) ||
  553. !CBS_get_asn1_uint64(&child, &timeout) ||
  554. timeout > UINT32_MAX) {
  555. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  556. return nullptr;
  557. }
  558. ret->timeout = (uint32_t)timeout;
  559. CBS peer;
  560. int has_peer;
  561. if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  562. (has_peer && CBS_len(&peer) == 0)) {
  563. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  564. return nullptr;
  565. }
  566. // |peer| is processed with the certificate chain.
  567. if (!SSL_SESSION_parse_bounded_octet_string(
  568. &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
  569. kSessionIDContextTag) ||
  570. !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
  571. X509_V_OK)) {
  572. return nullptr;
  573. }
  574. // Skip the historical hostName field.
  575. CBS unused_hostname;
  576. if (!CBS_get_optional_asn1(&session, &unused_hostname, nullptr,
  577. kHostNameTag)) {
  578. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  579. return nullptr;
  580. }
  581. if (!SSL_SESSION_parse_string(&session, &ret->psk_identity,
  582. kPSKIdentityTag) ||
  583. !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
  584. kTicketLifetimeHintTag, 0) ||
  585. !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
  586. &ret->tlsext_ticklen, kTicketTag)) {
  587. return nullptr;
  588. }
  589. if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
  590. CBS peer_sha256;
  591. if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
  592. !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
  593. CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
  594. CBS_len(&child) != 0) {
  595. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  596. return nullptr;
  597. }
  598. OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
  599. sizeof(ret->peer_sha256));
  600. ret->peer_sha256_valid = 1;
  601. } else {
  602. ret->peer_sha256_valid = 0;
  603. }
  604. if (!SSL_SESSION_parse_bounded_octet_string(
  605. &session, ret->original_handshake_hash,
  606. &ret->original_handshake_hash_len,
  607. sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
  608. !SSL_SESSION_parse_crypto_buffer(&session,
  609. &ret->signed_cert_timestamp_list,
  610. kSignedCertTimestampListTag, pool) ||
  611. !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
  612. kOCSPResponseTag, pool)) {
  613. return nullptr;
  614. }
  615. int extended_master_secret;
  616. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  617. kExtendedMasterSecretTag,
  618. 0 /* default to false */)) {
  619. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  620. return nullptr;
  621. }
  622. ret->extended_master_secret = !!extended_master_secret;
  623. if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
  624. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  625. return nullptr;
  626. }
  627. CBS cert_chain;
  628. CBS_init(&cert_chain, NULL, 0);
  629. int has_cert_chain;
  630. if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
  631. kCertChainTag) ||
  632. (has_cert_chain && CBS_len(&cert_chain) == 0)) {
  633. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  634. return nullptr;
  635. }
  636. if (has_cert_chain && !has_peer) {
  637. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  638. return nullptr;
  639. }
  640. if (has_peer || has_cert_chain) {
  641. ret->certs = sk_CRYPTO_BUFFER_new_null();
  642. if (ret->certs == NULL) {
  643. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  644. return nullptr;
  645. }
  646. if (has_peer) {
  647. UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
  648. if (!buffer ||
  649. !PushToStack(ret->certs, std::move(buffer))) {
  650. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  651. return nullptr;
  652. }
  653. }
  654. while (CBS_len(&cert_chain) > 0) {
  655. CBS cert;
  656. if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
  657. CBS_len(&cert) == 0) {
  658. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  659. return nullptr;
  660. }
  661. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
  662. if (buffer == NULL ||
  663. !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
  664. CRYPTO_BUFFER_free(buffer);
  665. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  666. return nullptr;
  667. }
  668. }
  669. }
  670. if (!x509_method->session_cache_objects(ret.get())) {
  671. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  672. return nullptr;
  673. }
  674. CBS age_add;
  675. int age_add_present;
  676. if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
  677. kTicketAgeAddTag) ||
  678. (age_add_present &&
  679. !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
  680. CBS_len(&age_add) != 0) {
  681. return nullptr;
  682. }
  683. ret->ticket_age_add_valid = age_add_present;
  684. int is_server;
  685. if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
  686. 1 /* default to true */)) {
  687. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  688. return nullptr;
  689. }
  690. /* TODO: in time we can include |is_server| for servers too, then we can
  691. enforce that client and server sessions are never mixed up. */
  692. ret->is_server = is_server;
  693. if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
  694. kPeerSignatureAlgorithmTag, 0) ||
  695. !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
  696. kTicketMaxEarlyDataTag, 0) ||
  697. !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
  698. ret->timeout) ||
  699. !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
  700. &ret->early_alpn_len, kEarlyALPNTag) ||
  701. CBS_len(&session) != 0) {
  702. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  703. return nullptr;
  704. }
  705. return ret;
  706. }
  707. } // namespace bssl
  708. using namespace bssl;
  709. int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
  710. size_t *out_len) {
  711. if (in->not_resumable) {
  712. // If the caller has an unresumable session, e.g. if |SSL_get_session| were
  713. // called on a TLS 1.3 or False Started connection, serialize with a
  714. // placeholder value so it is not accidentally deserialized into a resumable
  715. // one.
  716. static const char kNotResumableSession[] = "NOT RESUMABLE";
  717. *out_len = strlen(kNotResumableSession);
  718. *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
  719. if (*out_data == NULL) {
  720. return 0;
  721. }
  722. return 1;
  723. }
  724. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
  725. }
  726. int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
  727. size_t *out_len) {
  728. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
  729. }
  730. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  731. uint8_t *out;
  732. size_t len;
  733. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  734. return -1;
  735. }
  736. if (len > INT_MAX) {
  737. OPENSSL_free(out);
  738. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  739. return -1;
  740. }
  741. if (pp) {
  742. OPENSSL_memcpy(*pp, out, len);
  743. *pp += len;
  744. }
  745. OPENSSL_free(out);
  746. return len;
  747. }
  748. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
  749. const SSL_CTX *ctx) {
  750. CBS cbs;
  751. CBS_init(&cbs, in, in_len);
  752. UniquePtr<SSL_SESSION> ret =
  753. SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
  754. if (!ret) {
  755. return NULL;
  756. }
  757. if (CBS_len(&cbs) != 0) {
  758. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  759. return NULL;
  760. }
  761. return ret.release();
  762. }