ssl_asn1.c 30 KB

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