705db16470481e2af55c5ab98c2d9455c3a1f975
[openssl.git] / ssl / ssl_asn1.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2005 Nokia. All rights reserved.
12  *
13  * The portions of the attached software ("Contribution") is developed by
14  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
15  * license.
16  *
17  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
18  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
19  * support (see RFC 4279) to OpenSSL.
20  *
21  * No patent licenses or other rights except those expressly stated in
22  * the OpenSSL open source license shall be deemed granted or received
23  * expressly, by implication, estoppel, or otherwise.
24  *
25  * No assurances are provided by Nokia that the Contribution does not
26  * infringe the patent or other intellectual property rights of any third
27  * party or that the license provides you with all the necessary rights
28  * to make use of the Contribution.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
31  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
32  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
33  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
34  * OTHERWISE.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "ssl_locl.h"
40 #include <openssl/asn1t.h>
41 #include <openssl/x509.h>
42
43 typedef struct {
44     long version;
45     long ssl_version;
46     ASN1_OCTET_STRING *cipher;
47     ASN1_OCTET_STRING *comp_id;
48     ASN1_OCTET_STRING *master_key;
49     ASN1_OCTET_STRING *session_id;
50     ASN1_OCTET_STRING *key_arg;
51     long time;
52     long timeout;
53     X509 *peer;
54     ASN1_OCTET_STRING *session_id_context;
55     long verify_result;
56     ASN1_OCTET_STRING *tlsext_hostname;
57     long tlsext_tick_lifetime_hint;
58     long tlsext_tick_age_add;
59     ASN1_OCTET_STRING *tlsext_tick;
60 #ifndef OPENSSL_NO_PSK
61     ASN1_OCTET_STRING *psk_identity_hint;
62     ASN1_OCTET_STRING *psk_identity;
63 #endif
64 #ifndef OPENSSL_NO_SRP
65     ASN1_OCTET_STRING *srp_username;
66 #endif
67     long flags;
68     uint32_t max_early_data;
69 } SSL_SESSION_ASN1;
70
71 ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
72     ASN1_SIMPLE(SSL_SESSION_ASN1, version, LONG),
73     ASN1_SIMPLE(SSL_SESSION_ASN1, ssl_version, LONG),
74     ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
75     ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
76     ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
77     ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
78     ASN1_EXP_OPT(SSL_SESSION_ASN1, time, ZLONG, 1),
79     ASN1_EXP_OPT(SSL_SESSION_ASN1, timeout, ZLONG, 2),
80     ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
81     ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
82     ASN1_EXP_OPT(SSL_SESSION_ASN1, verify_result, ZLONG, 5),
83     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
84 #ifndef OPENSSL_NO_PSK
85     ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
86     ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
87 #endif
88     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZLONG, 9),
89     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
90     ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
91 #ifndef OPENSSL_NO_SRP
92     ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
93 #endif
94     ASN1_EXP_OPT(SSL_SESSION_ASN1, flags, ZLONG, 13),
95     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_age_add, ZLONG, 14),
96     ASN1_EXP_OPT(SSL_SESSION_ASN1, max_early_data, ZLONG, 15)
97 } static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
98
99 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
100
101 /* Utility functions for i2d_SSL_SESSION */
102
103 /* Initialise OCTET STRING from buffer and length */
104
105 static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
106                               unsigned char *data, size_t len)
107 {
108     os->data = data;
109     os->length = (int)len;
110     os->flags = 0;
111     *dest = os;
112 }
113
114 /* Initialise OCTET STRING from string */
115 static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
116                               char *data)
117 {
118     if (data != NULL)
119         ssl_session_oinit(dest, os, (unsigned char *)data, strlen(data));
120     else
121         *dest = NULL;
122 }
123
124 int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
125 {
126
127     SSL_SESSION_ASN1 as;
128
129     ASN1_OCTET_STRING cipher;
130     unsigned char cipher_data[2];
131     ASN1_OCTET_STRING master_key, session_id, sid_ctx;
132
133 #ifndef OPENSSL_NO_COMP
134     ASN1_OCTET_STRING comp_id;
135     unsigned char comp_id_data;
136 #endif
137
138     ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
139
140 #ifndef OPENSSL_NO_SRP
141     ASN1_OCTET_STRING srp_username;
142 #endif
143
144 #ifndef OPENSSL_NO_PSK
145     ASN1_OCTET_STRING psk_identity, psk_identity_hint;
146 #endif
147
148     long l;
149
150     if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
151         return 0;
152
153     memset(&as, 0, sizeof(as));
154
155     as.version = SSL_SESSION_ASN1_VERSION;
156     as.ssl_version = in->ssl_version;
157
158     if (in->cipher == NULL)
159         l = in->cipher_id;
160     else
161         l = in->cipher->id;
162     cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
163     cipher_data[1] = ((unsigned char)(l)) & 0xff;
164
165     ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
166
167 #ifndef OPENSSL_NO_COMP
168     if (in->compress_meth) {
169         comp_id_data = (unsigned char)in->compress_meth;
170         ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
171     }
172 #endif
173
174     ssl_session_oinit(&as.master_key, &master_key,
175                       in->master_key, in->master_key_length);
176
177     ssl_session_oinit(&as.session_id, &session_id,
178                       in->session_id, in->session_id_length);
179
180     ssl_session_oinit(&as.session_id_context, &sid_ctx,
181                       in->sid_ctx, in->sid_ctx_length);
182
183     as.time = in->time;
184     as.timeout = in->timeout;
185     as.verify_result = in->verify_result;
186
187     as.peer = in->peer;
188
189     ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
190                       in->ext.hostname);
191     if (in->ext.tick) {
192         ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
193                           in->ext.tick, in->ext.ticklen);
194     }
195     if (in->ext.tick_lifetime_hint > 0)
196         as.tlsext_tick_lifetime_hint = in->ext.tick_lifetime_hint;
197     as.tlsext_tick_age_add = in->ext.tick_age_add;
198 #ifndef OPENSSL_NO_PSK
199     ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
200                       in->psk_identity_hint);
201     ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
202 #endif                          /* OPENSSL_NO_PSK */
203 #ifndef OPENSSL_NO_SRP
204     ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
205 #endif                          /* OPENSSL_NO_SRP */
206
207     as.flags = in->flags;
208     as.max_early_data = in->ext.max_early_data;
209
210     return i2d_SSL_SESSION_ASN1(&as, pp);
211
212 }
213
214 /* Utility functions for d2i_SSL_SESSION */
215
216 /* OPENSSL_strndup an OCTET STRING */
217
218 static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
219 {
220     OPENSSL_free(*pdst);
221     *pdst = NULL;
222     if (src == NULL)
223         return 1;
224     *pdst = OPENSSL_strndup((char *)src->data, src->length);
225     if (*pdst == NULL)
226         return 0;
227     return 1;
228 }
229
230 /* Copy an OCTET STRING, return error if it exceeds maximum length */
231
232 static int ssl_session_memcpy(unsigned char *dst, size_t *pdstlen,
233                               ASN1_OCTET_STRING *src, size_t maxlen)
234 {
235     if (src == NULL) {
236         *pdstlen = 0;
237         return 1;
238     }
239     if (src->length < 0 || src->length > (int)maxlen)
240         return 0;
241     memcpy(dst, src->data, src->length);
242     *pdstlen = src->length;
243     return 1;
244 }
245
246 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
247                              long length)
248 {
249     long id;
250     size_t tmpl;
251     const unsigned char *p = *pp;
252     SSL_SESSION_ASN1 *as = NULL;
253     SSL_SESSION *ret = NULL;
254
255     as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
256     /* ASN.1 code returns suitable error */
257     if (as == NULL)
258         goto err;
259
260     if (!a || !*a) {
261         ret = SSL_SESSION_new();
262         if (ret == NULL)
263             goto err;
264     } else {
265         ret = *a;
266     }
267
268     if (as->version != SSL_SESSION_ASN1_VERSION) {
269         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
270         goto err;
271     }
272
273     if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
274         && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
275         && as->ssl_version != DTLS1_BAD_VER) {
276         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
277         goto err;
278     }
279
280     ret->ssl_version = (int)as->ssl_version;
281
282     if (as->cipher->length != 2) {
283         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
284         goto err;
285     }
286
287     id = 0x03000000L | ((unsigned long)as->cipher->data[0] << 8L)
288                      | (unsigned long)as->cipher->data[1];
289
290     ret->cipher_id = id;
291     ret->cipher = ssl3_get_cipher_by_id(id);
292     if (ret->cipher == NULL)
293         goto err;
294
295     if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
296                             as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
297         goto err;
298
299     if (!ssl_session_memcpy(ret->master_key, &tmpl,
300                             as->master_key, TLS13_MAX_RESUMPTION_MASTER_LENGTH))
301         goto err;
302
303     ret->master_key_length = tmpl;
304
305     if (as->time != 0)
306         ret->time = as->time;
307     else
308         ret->time = (unsigned long)time(NULL);
309
310     if (as->timeout != 0)
311         ret->timeout = as->timeout;
312     else
313         ret->timeout = 3;
314
315     X509_free(ret->peer);
316     ret->peer = as->peer;
317     as->peer = NULL;
318
319     if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
320                             as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
321         goto err;
322
323     /* NB: this defaults to zero which is X509_V_OK */
324     ret->verify_result = as->verify_result;
325
326     if (!ssl_session_strndup(&ret->ext.hostname, as->tlsext_hostname))
327         goto err;
328
329 #ifndef OPENSSL_NO_PSK
330     if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
331         goto err;
332     if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
333         goto err;
334 #endif
335
336     ret->ext.tick_lifetime_hint = as->tlsext_tick_lifetime_hint;
337     ret->ext.tick_age_add = as->tlsext_tick_age_add;
338     if (as->tlsext_tick) {
339         ret->ext.tick = as->tlsext_tick->data;
340         ret->ext.ticklen = as->tlsext_tick->length;
341         as->tlsext_tick->data = NULL;
342     } else {
343         ret->ext.tick = NULL;
344     }
345 #ifndef OPENSSL_NO_COMP
346     if (as->comp_id) {
347         if (as->comp_id->length != 1) {
348             SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_BAD_LENGTH);
349             goto err;
350         }
351         ret->compress_meth = as->comp_id->data[0];
352     } else {
353         ret->compress_meth = 0;
354     }
355 #endif
356
357 #ifndef OPENSSL_NO_SRP
358     if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
359         goto err;
360 #endif                          /* OPENSSL_NO_SRP */
361     /* Flags defaults to zero which is fine */
362     ret->flags = as->flags;
363     ret->ext.max_early_data = as->max_early_data;
364
365     M_ASN1_free_of(as, SSL_SESSION_ASN1);
366
367     if ((a != NULL) && (*a == NULL))
368         *a = ret;
369     *pp = p;
370     return ret;
371
372  err:
373     M_ASN1_free_of(as, SSL_SESSION_ASN1);
374     if ((a == NULL) || (*a != ret))
375         SSL_SESSION_free(ret);
376     return NULL;
377 }