]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_asn1.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / ssl / ssl_asn1.c
CommitLineData
d02b48c6 1/* ssl/ssl_asn1.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 8 *
d02b48c6
RE
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 15 *
d02b48c6
RE
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
0f113f3e 22 *
d02b48c6
RE
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
0f113f3e 52 *
d02b48c6
RE
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
ddac1974
NL
58/* ====================================================================
59 * Copyright 2005 Nokia. All rights reserved.
60 *
61 * The portions of the attached software ("Contribution") is developed by
62 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
63 * license.
64 *
65 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
66 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
67 * support (see RFC 4279) to OpenSSL.
68 *
69 * No patent licenses or other rights except those expressly stated in
70 * the OpenSSL open source license shall be deemed granted or received
71 * expressly, by implication, estoppel, or otherwise.
72 *
73 * No assurances are provided by Nokia that the Contribution does not
74 * infringe the patent or other intellectual property rights of any third
75 * party or that the license provides you with all the necessary rights
76 * to make use of the Contribution.
77 *
78 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
79 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
80 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
81 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
82 * OTHERWISE.
83 */
d02b48c6
RE
84
85#include <stdio.h>
86#include <stdlib.h>
7b63c0fa 87#include "ssl_locl.h"
cc5b6a03 88#include <openssl/asn1t.h>
b1fe6ca1 89#include <openssl/x509.h>
d02b48c6 90
cc5b6a03
DSH
91typedef struct {
92 long version;
93 long ssl_version;
94 ASN1_OCTET_STRING *cipher;
95 ASN1_OCTET_STRING *comp_id;
96 ASN1_OCTET_STRING *master_key;
97 ASN1_OCTET_STRING *session_id;
cc5b6a03
DSH
98 ASN1_OCTET_STRING *key_arg;
99 long time;
100 long timeout;
101 X509 *peer;
102 ASN1_OCTET_STRING *session_id_context;
103 long verify_result;
cc5b6a03
DSH
104 ASN1_OCTET_STRING *tlsext_hostname;
105 long tlsext_tick_lifetime_hint;
106 ASN1_OCTET_STRING *tlsext_tick;
e2010b20 107#ifndef OPENSSL_NO_PSK
cc5b6a03
DSH
108 ASN1_OCTET_STRING *psk_identity_hint;
109 ASN1_OCTET_STRING *psk_identity;
110#endif
edc032b5 111#ifndef OPENSSL_NO_SRP
cc5b6a03
DSH
112 ASN1_OCTET_STRING *srp_username;
113#endif
114 long flags;
0f113f3e 115} SSL_SESSION_ASN1;
d02b48c6 116
cc5b6a03
DSH
117ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
118 ASN1_SIMPLE(SSL_SESSION_ASN1, version, LONG),
119 ASN1_SIMPLE(SSL_SESSION_ASN1, ssl_version, LONG),
120 ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
121 ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
122 ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
cc5b6a03
DSH
123 ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
124 ASN1_EXP_OPT(SSL_SESSION_ASN1, time, ZLONG, 1),
125 ASN1_EXP_OPT(SSL_SESSION_ASN1, timeout, ZLONG, 2),
126 ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
127 ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
128 ASN1_EXP_OPT(SSL_SESSION_ASN1, verify_result, ZLONG, 5),
cc5b6a03 129 ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
cc5b6a03
DSH
130#ifndef OPENSSL_NO_PSK
131 ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
132 ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
133#endif
cc5b6a03
DSH
134 ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZLONG, 9),
135 ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
cc5b6a03
DSH
136 ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
137#ifndef OPENSSL_NO_SRP
138 ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
139#endif
140 ASN1_EXP_OPT(SSL_SESSION_ASN1, flags, ZLONG, 13)
df2ee0e2 141} static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
cc5b6a03
DSH
142
143IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
144
145/* Utility functions for i2d_SSL_SESSION */
146
147/* Initialise OCTET STRING from buffer and length */
148
149static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
150 unsigned char *data, size_t len)
151{
152 os->data = data;
153 os->length = len;
154 os->flags = 0;
155 *dest = os;
156}
157
158/* Initialise OCTET STRING from string */
159static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
160 char *data)
161{
162 if (data != NULL)
163 ssl_session_oinit(dest, os, (unsigned char *)data, strlen(data));
164 else
165 *dest = NULL;
166}
167
6b691a5c 168int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
0f113f3e 169{
cc5b6a03
DSH
170
171 SSL_SESSION_ASN1 as;
172
173 ASN1_OCTET_STRING cipher;
174 unsigned char cipher_data[2];
175 ASN1_OCTET_STRING master_key, session_id, sid_ctx;
176
177#ifndef OPENSSL_NO_COMP
178 ASN1_OCTET_STRING comp_id;
179 unsigned char comp_id_data;
180#endif
181
cc5b6a03 182 ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
cc5b6a03 183
edc032b5 184#ifndef OPENSSL_NO_SRP
cc5b6a03
DSH
185 ASN1_OCTET_STRING srp_username;
186#endif
187
188#ifndef OPENSSL_NO_PSK
189 ASN1_OCTET_STRING psk_identity, psk_identity_hint;
367eb1f1 190#endif
cc5b6a03 191
0f113f3e 192 long l;
0f113f3e
MC
193
194 if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
cc5b6a03
DSH
195 return 0;
196
197 memset(&as, 0, sizeof(as));
198
199 as.version = SSL_SESSION_ASN1_VERSION;
200 as.ssl_version = in->ssl_version;
0f113f3e
MC
201
202 if (in->cipher == NULL)
203 l = in->cipher_id;
204 else
205 l = in->cipher->id;
cc5b6a03
DSH
206 cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
207 cipher_data[1] = ((unsigned char)(l)) & 0xff;
208
209 ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
d02b48c6 210
9de014a7 211#ifndef OPENSSL_NO_COMP
0f113f3e 212 if (in->compress_meth) {
cc5b6a03
DSH
213 comp_id_data = (unsigned char)in->compress_meth;
214 ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
0f113f3e 215 }
9de014a7
DSH
216#endif
217
cc5b6a03
DSH
218 ssl_session_oinit(&as.master_key, &master_key,
219 in->master_key, in->master_key_length);
d02b48c6 220
cc5b6a03
DSH
221 ssl_session_oinit(&as.session_id, &session_id,
222 in->session_id, in->session_id_length);
b4cadc6e 223
cc5b6a03
DSH
224 ssl_session_oinit(&as.session_id_context, &sid_ctx,
225 in->sid_ctx, in->sid_ctx_length);
0f113f3e 226
cc5b6a03
DSH
227 as.time = in->time;
228 as.timeout = in->timeout;
229 as.verify_result = in->verify_result;
0f113f3e 230
cc5b6a03 231 as.peer = in->peer;
0f113f3e 232
cc5b6a03
DSH
233 ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
234 in->tlsext_hostname);
0f113f3e 235 if (in->tlsext_tick) {
cc5b6a03
DSH
236 ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
237 in->tlsext_tick, in->tlsext_ticklen);
0f113f3e 238 }
cc5b6a03
DSH
239 if (in->tlsext_tick_lifetime_hint > 0)
240 as.tlsext_tick_lifetime_hint = in->tlsext_tick_lifetime_hint;
ddac1974 241#ifndef OPENSSL_NO_PSK
cc5b6a03
DSH
242 ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
243 in->psk_identity_hint);
244 ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
0f113f3e 245#endif /* OPENSSL_NO_PSK */
edc032b5 246#ifndef OPENSSL_NO_SRP
cc5b6a03 247 ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
0f113f3e
MC
248#endif /* OPENSSL_NO_SRP */
249
cc5b6a03 250 as.flags = in->flags;
6f152a15 251
cc5b6a03 252 return i2d_SSL_SESSION_ASN1(&as, pp);
d02b48c6 253
cc5b6a03 254}
0f113f3e 255
cc5b6a03 256/* Utility functions for d2i_SSL_SESSION */
0f113f3e 257
7644a9ae 258/* OPENSSL_strndup an OCTET STRING */
cc5b6a03
DSH
259
260static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
261{
b548a1f1
RS
262 OPENSSL_free(*pdst);
263 *pdst = NULL;
cc5b6a03
DSH
264 if (src == NULL)
265 return 1;
7644a9ae 266 *pdst = OPENSSL_strndup((char *)src->data, src->length);
cc5b6a03
DSH
267 if (*pdst == NULL)
268 return 0;
269 return 1;
270}
271
272/* Copy an OCTET STRING, return error if it exceeds maximum length */
273
274static int ssl_session_memcpy(unsigned char *dst, unsigned int *pdstlen,
275 ASN1_OCTET_STRING *src, int maxlen)
276{
277 if (src == NULL) {
278 *pdstlen = 0;
279 return 1;
280 }
281 if (src->length > maxlen)
282 return 0;
283 memcpy(dst, src->data, src->length);
284 *pdstlen = src->length;
285 return 1;
0f113f3e 286}
d02b48c6 287
41a15c4f 288SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
0f113f3e
MC
289 long length)
290{
0f113f3e 291 long id;
cc5b6a03
DSH
292 unsigned int tmpl;
293 const unsigned char *p = *pp;
294 SSL_SESSION_ASN1 *as = NULL;
295 SSL_SESSION *ret = NULL;
296
297 as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
298 /* ASN.1 code returns suitable error */
299 if (as == NULL)
300 goto err;
0f113f3e 301
cc5b6a03
DSH
302 if (!a || !*a) {
303 ret = SSL_SESSION_new();
304 if (ret == NULL)
0f113f3e 305 goto err;
0f113f3e 306 } else {
cc5b6a03
DSH
307 ret = *a;
308 }
309
310 if (as->version != SSL_SESSION_ASN1_VERSION) {
311 SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
0f113f3e
MC
312 goto err;
313 }
314
cc5b6a03
DSH
315 if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
316 && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
317 && as->ssl_version != DTLS1_BAD_VER) {
318 SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
319 goto err;
320 }
0f113f3e 321
cc5b6a03 322 ret->ssl_version = (int)as->ssl_version;
0f113f3e 323
cc5b6a03
DSH
324 if (as->cipher->length != 2) {
325 SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
326 goto err;
327 }
0f113f3e 328
cc5b6a03
DSH
329 p = as->cipher->data;
330 id = 0x03000000L | ((unsigned long)p[0] << 8L) | (unsigned long)p[1];
0f113f3e 331
cc5b6a03
DSH
332 ret->cipher = NULL;
333 ret->cipher_id = id;
0f113f3e 334
cc5b6a03
DSH
335 if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
336 as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
337 goto err;
338
339 if (!ssl_session_memcpy(ret->master_key, &tmpl,
340 as->master_key, SSL_MAX_MASTER_KEY_LENGTH))
341 goto err;
342
343 ret->master_key_length = tmpl;
98fa4fe8 344
cc5b6a03
DSH
345 if (as->time != 0)
346 ret->time = as->time;
347 else
0f113f3e
MC
348 ret->time = (unsigned long)time(NULL);
349
cc5b6a03
DSH
350 if (as->timeout != 0)
351 ret->timeout = as->timeout;
352 else
0f113f3e
MC
353 ret->timeout = 3;
354
cc5b6a03
DSH
355 X509_free(ret->peer);
356 ret->peer = as->peer;
357 as->peer = NULL;
0f113f3e 358
cc5b6a03
DSH
359 if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
360 as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
361 goto err;
0f113f3e 362
cc5b6a03
DSH
363 /* NB: this defaults to zero which is X509_V_OK */
364 ret->verify_result = as->verify_result;
b1fe6ca1 365
cc5b6a03
DSH
366 if (!ssl_session_strndup(&ret->tlsext_hostname, as->tlsext_hostname))
367 goto err;
ed3883d2 368
ddac1974 369#ifndef OPENSSL_NO_PSK
cc5b6a03
DSH
370 if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
371 goto err;
372 if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
373 goto err;
374#endif
ddac1974 375
cc5b6a03
DSH
376 ret->tlsext_tick_lifetime_hint = as->tlsext_tick_lifetime_hint;
377 if (as->tlsext_tick) {
378 ret->tlsext_tick = as->tlsext_tick->data;
379 ret->tlsext_ticklen = as->tlsext_tick->length;
380 as->tlsext_tick->data = NULL;
381 } else {
0f113f3e 382 ret->tlsext_tick = NULL;
cc5b6a03 383 }
9de014a7 384#ifndef OPENSSL_NO_COMP
cc5b6a03
DSH
385 if (as->comp_id) {
386 if (as->comp_id->length != 1) {
387 SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_BAD_LENGTH);
388 goto err;
389 }
390 ret->compress_meth = as->comp_id->data[0];
391 } else {
392 ret->compress_meth = 0;
0f113f3e 393 }
9de014a7 394#endif
ddac1974 395
edc032b5 396#ifndef OPENSSL_NO_SRP
cc5b6a03
DSH
397 if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
398 goto err;
0f113f3e 399#endif /* OPENSSL_NO_SRP */
cc5b6a03
DSH
400 /* Flags defaults to zero which is fine */
401 ret->flags = as->flags;
402
403 M_ASN1_free_of(as, SSL_SESSION_ASN1);
404
405 if ((a != NULL) && (*a == NULL))
406 *a = ret;
407 *pp = p;
408 return ret;
409
410 err:
411 M_ASN1_free_of(as, SSL_SESSION_ASN1);
412 if ((a == NULL) || (*a != ret))
413 SSL_SESSION_free(ret);
414 return NULL;
0f113f3e 415}