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