Add more first-class support for SCSVS
[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 } SSL_SESSION_ASN1;
69
70 ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
71     ASN1_SIMPLE(SSL_SESSION_ASN1, version, LONG),
72     ASN1_SIMPLE(SSL_SESSION_ASN1, ssl_version, LONG),
73     ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
74     ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
75     ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
76     ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
77     ASN1_EXP_OPT(SSL_SESSION_ASN1, time, ZLONG, 1),
78     ASN1_EXP_OPT(SSL_SESSION_ASN1, timeout, ZLONG, 2),
79     ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
80     ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
81     ASN1_EXP_OPT(SSL_SESSION_ASN1, verify_result, ZLONG, 5),
82     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
83 #ifndef OPENSSL_NO_PSK
84     ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
85     ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
86 #endif
87     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZLONG, 9),
88     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
89     ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
90 #ifndef OPENSSL_NO_SRP
91     ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
92 #endif
93     ASN1_EXP_OPT(SSL_SESSION_ASN1, flags, ZLONG, 13),
94     ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_age_add, ZLONG, 14)
95 } static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
96
97 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
98
99 /* Utility functions for i2d_SSL_SESSION */
100
101 /* Initialise OCTET STRING from buffer and length */
102
103 static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
104                               unsigned char *data, size_t len)
105 {
106     os->data = data;
107     os->length = (int)len;
108     os->flags = 0;
109     *dest = os;
110 }
111
112 /* Initialise OCTET STRING from string */
113 static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
114                               char *data)
115 {
116     if (data != NULL)
117         ssl_session_oinit(dest, os, (unsigned char *)data, strlen(data));
118     else
119         *dest = NULL;
120 }
121
122 int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
123 {
124
125     SSL_SESSION_ASN1 as;
126
127     ASN1_OCTET_STRING cipher;
128     unsigned char cipher_data[2];
129     ASN1_OCTET_STRING master_key, session_id, sid_ctx;
130
131 #ifndef OPENSSL_NO_COMP
132     ASN1_OCTET_STRING comp_id;
133     unsigned char comp_id_data;
134 #endif
135
136     ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
137
138 #ifndef OPENSSL_NO_SRP
139     ASN1_OCTET_STRING srp_username;
140 #endif
141
142 #ifndef OPENSSL_NO_PSK
143     ASN1_OCTET_STRING psk_identity, psk_identity_hint;
144 #endif
145
146     long l;
147
148     if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
149         return 0;
150
151     memset(&as, 0, sizeof(as));
152
153     as.version = SSL_SESSION_ASN1_VERSION;
154     as.ssl_version = in->ssl_version;
155
156     if (in->cipher == NULL)
157         l = in->cipher_id;
158     else
159         l = in->cipher->id;
160     cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
161     cipher_data[1] = ((unsigned char)(l)) & 0xff;
162
163     ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
164
165 #ifndef OPENSSL_NO_COMP
166     if (in->compress_meth) {
167         comp_id_data = (unsigned char)in->compress_meth;
168         ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
169     }
170 #endif
171
172     ssl_session_oinit(&as.master_key, &master_key,
173                       in->master_key, in->master_key_length);
174
175     ssl_session_oinit(&as.session_id, &session_id,
176                       in->session_id, in->session_id_length);
177
178     ssl_session_oinit(&as.session_id_context, &sid_ctx,
179                       in->sid_ctx, in->sid_ctx_length);
180
181     as.time = in->time;
182     as.timeout = in->timeout;
183     as.verify_result = in->verify_result;
184
185     as.peer = in->peer;
186
187     ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
188                       in->ext.hostname);
189     if (in->ext.tick) {
190         ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
191                           in->ext.tick, in->ext.ticklen);
192     }
193     if (in->ext.tick_lifetime_hint > 0)
194         as.tlsext_tick_lifetime_hint = in->ext.tick_lifetime_hint;
195     as.tlsext_tick_age_add = in->ext.tick_age_add;
196 #ifndef OPENSSL_NO_PSK
197     ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
198                       in->psk_identity_hint);
199     ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
200 #endif                          /* OPENSSL_NO_PSK */
201 #ifndef OPENSSL_NO_SRP
202     ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
203 #endif                          /* OPENSSL_NO_SRP */
204
205     as.flags = in->flags;
206
207     return i2d_SSL_SESSION_ASN1(&as, pp);
208
209 }
210
211 /* Utility functions for d2i_SSL_SESSION */
212
213 /* OPENSSL_strndup an OCTET STRING */
214
215 static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
216 {
217     OPENSSL_free(*pdst);
218     *pdst = NULL;
219     if (src == NULL)
220         return 1;
221     *pdst = OPENSSL_strndup((char *)src->data, src->length);
222     if (*pdst == NULL)
223         return 0;
224     return 1;
225 }
226
227 /* Copy an OCTET STRING, return error if it exceeds maximum length */
228
229 static int ssl_session_memcpy(unsigned char *dst, size_t *pdstlen,
230                               ASN1_OCTET_STRING *src, size_t maxlen)
231 {
232     if (src == NULL) {
233         *pdstlen = 0;
234         return 1;
235     }
236     if (src->length < 0 || src->length > (int)maxlen)
237         return 0;
238     memcpy(dst, src->data, src->length);
239     *pdstlen = src->length;
240     return 1;
241 }
242
243 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
244                              long length)
245 {
246     long id;
247     size_t tmpl;
248     const unsigned char *p = *pp;
249     SSL_SESSION_ASN1 *as = NULL;
250     SSL_SESSION *ret = NULL;
251
252     as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
253     /* ASN.1 code returns suitable error */
254     if (as == NULL)
255         goto err;
256
257     if (!a || !*a) {
258         ret = SSL_SESSION_new();
259         if (ret == NULL)
260             goto err;
261     } else {
262         ret = *a;
263     }
264
265     if (as->version != SSL_SESSION_ASN1_VERSION) {
266         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
267         goto err;
268     }
269
270     if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
271         && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
272         && as->ssl_version != DTLS1_BAD_VER) {
273         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
274         goto err;
275     }
276
277     ret->ssl_version = (int)as->ssl_version;
278
279     if (as->cipher->length != 2) {
280         SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
281         goto err;
282     }
283
284     id = 0x03000000L | ((unsigned long)as->cipher->data[0] << 8L)
285                      | (unsigned long)as->cipher->data[1];
286
287     ret->cipher_id = id;
288     ret->cipher = ssl3_get_cipher_by_id(id);
289     if (ret->cipher == NULL)
290         goto err;
291
292     if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
293                             as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
294         goto err;
295
296     if (!ssl_session_memcpy(ret->master_key, &tmpl,
297                             as->master_key, TLS13_MAX_RESUMPTION_MASTER_LENGTH))
298         goto err;
299
300     ret->master_key_length = tmpl;
301
302     if (as->time != 0)
303         ret->time = as->time;
304     else
305         ret->time = (unsigned long)time(NULL);
306
307     if (as->timeout != 0)
308         ret->timeout = as->timeout;
309     else
310         ret->timeout = 3;
311
312     X509_free(ret->peer);
313     ret->peer = as->peer;
314     as->peer = NULL;
315
316     if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
317                             as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
318         goto err;
319
320     /* NB: this defaults to zero which is X509_V_OK */
321     ret->verify_result = as->verify_result;
322
323     if (!ssl_session_strndup(&ret->ext.hostname, as->tlsext_hostname))
324         goto err;
325
326 #ifndef OPENSSL_NO_PSK
327     if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
328         goto err;
329     if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
330         goto err;
331 #endif
332
333     ret->ext.tick_lifetime_hint = as->tlsext_tick_lifetime_hint;
334     ret->ext.tick_age_add = as->tlsext_tick_age_add;
335     if (as->tlsext_tick) {
336         ret->ext.tick = as->tlsext_tick->data;
337         ret->ext.ticklen = as->tlsext_tick->length;
338         as->tlsext_tick->data = NULL;
339     } else {
340         ret->ext.tick = NULL;
341     }
342 #ifndef OPENSSL_NO_COMP
343     if (as->comp_id) {
344         if (as->comp_id->length != 1) {
345             SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_BAD_LENGTH);
346             goto err;
347         }
348         ret->compress_meth = as->comp_id->data[0];
349     } else {
350         ret->compress_meth = 0;
351     }
352 #endif
353
354 #ifndef OPENSSL_NO_SRP
355     if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
356         goto err;
357 #endif                          /* OPENSSL_NO_SRP */
358     /* Flags defaults to zero which is fine */
359     ret->flags = as->flags;
360
361     M_ASN1_free_of(as, SSL_SESSION_ASN1);
362
363     if ((a != NULL) && (*a == NULL))
364         *a = ret;
365     *pp = p;
366     return ret;
367
368  err:
369     M_ASN1_free_of(as, SSL_SESSION_ASN1);
370     if ((a == NULL) || (*a != ret))
371         SSL_SESSION_free(ret);
372     return NULL;
373 }