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