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