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