]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/asn1_lib.c
Add ASN1_STRING_get0_data(), deprecate ASN1_STRING_data().
[thirdparty/openssl.git] / crypto / asn1 / asn1_lib.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
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
RE
8 */
9
10#include <stdio.h>
f9082268 11#include <limits.h>
b39fc560 12#include "internal/cryptlib.h"
ec577822 13#include <openssl/asn1.h>
d02b48c6 14
0f113f3e 15static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
79c7f74d 16 long max);
d02b48c6 17static void asn1_put_length(unsigned char **pp, int length);
d02b48c6 18
875a644a 19static int _asn1_check_infinite_end(const unsigned char **p, long len)
0f113f3e
MC
20{
21 /*
22 * If there is 0 or 1 byte left, the length check should pick things up
23 */
24 if (len <= 0)
25 return (1);
26 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
27 (*p) += 2;
28 return (1);
29 }
30 return (0);
31}
d02b48c6 32
6343829a 33int ASN1_check_infinite_end(unsigned char **p, long len)
0f113f3e
MC
34{
35 return _asn1_check_infinite_end((const unsigned char **)p, len);
36}
d02b48c6 37
6343829a 38int ASN1_const_check_infinite_end(const unsigned char **p, long len)
0f113f3e
MC
39{
40 return _asn1_check_infinite_end(p, len);
41}
875a644a 42
6343829a 43int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
0f113f3e
MC
44 int *pclass, long omax)
45{
46 int i, ret;
47 long l;
48 const unsigned char *p = *pp;
49 int tag, xclass, inf;
50 long max = omax;
51
52 if (!max)
53 goto err;
54 ret = (*p & V_ASN1_CONSTRUCTED);
55 xclass = (*p & V_ASN1_PRIVATE);
56 i = *p & V_ASN1_PRIMITIVE_TAG;
57 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
58 p++;
59 if (--max == 0)
60 goto err;
61 l = 0;
62 while (*p & 0x80) {
63 l <<= 7L;
64 l |= *(p++) & 0x7f;
65 if (--max == 0)
66 goto err;
67 if (l > (INT_MAX >> 7L))
68 goto err;
69 }
70 l <<= 7L;
71 l |= *(p++) & 0x7f;
72 tag = (int)l;
73 if (--max == 0)
74 goto err;
75 } else {
76 tag = i;
77 p++;
78 if (--max == 0)
79 goto err;
80 }
81 *ptag = tag;
82 *pclass = xclass;
79c7f74d 83 if (!asn1_get_length(&p, &inf, plength, max))
0f113f3e
MC
84 goto err;
85
86 if (inf && !(ret & V_ASN1_CONSTRUCTED))
87 goto err;
398e99fe 88
0f113f3e
MC
89 if (*plength > (omax - (p - *pp))) {
90 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
91 /*
92 * Set this so that even if things are not long enough the values are
93 * set correctly
94 */
95 ret |= 0x80;
96 }
97 *pp = p;
98 return (ret | inf);
99 err:
100 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
101 return (0x80);
102}
103
104static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
79c7f74d 105 long max)
0f113f3e
MC
106{
107 const unsigned char *p = *pp;
108 unsigned long ret = 0;
79c7f74d 109 unsigned long i;
0f113f3e
MC
110
111 if (max-- < 1)
79c7f74d 112 return 0;
0f113f3e
MC
113 if (*p == 0x80) {
114 *inf = 1;
115 ret = 0;
116 p++;
117 } else {
118 *inf = 0;
119 i = *p & 0x7f;
120 if (*(p++) & 0x80) {
79c7f74d 121 if (max < (long)i + 1)
0f113f3e
MC
122 return 0;
123 /* Skip leading zeroes */
124 while (i && *p == 0) {
125 p++;
126 i--;
127 }
128 if (i > sizeof(long))
129 return 0;
130 while (i-- > 0) {
131 ret <<= 8L;
132 ret |= *(p++);
133 }
134 } else
135 ret = i;
136 }
137 if (ret > LONG_MAX)
138 return 0;
139 *pp = p;
140 *rl = (long)ret;
79c7f74d 141 return 1;
0f113f3e
MC
142}
143
144/*
145 * class 0 is constructed constructed == 2 for indefinite length constructed
146 */
6343829a 147void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
0f113f3e
MC
148 int xclass)
149{
150 unsigned char *p = *pp;
151 int i, ttag;
152
153 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
154 i |= (xclass & V_ASN1_PRIVATE);
155 if (tag < 31)
156 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
157 else {
158 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
159 for (i = 0, ttag = tag; ttag > 0; i++)
160 ttag >>= 7;
161 ttag = i;
162 while (i-- > 0) {
163 p[i] = tag & 0x7f;
164 if (i != (ttag - 1))
165 p[i] |= 0x80;
166 tag >>= 7;
167 }
168 p += ttag;
169 }
170 if (constructed == 2)
171 *(p++) = 0x80;
172 else
173 asn1_put_length(&p, length);
174 *pp = p;
175}
d02b48c6 176
230fd6b7 177int ASN1_put_eoc(unsigned char **pp)
0f113f3e
MC
178{
179 unsigned char *p = *pp;
180 *p++ = 0;
181 *p++ = 0;
182 *pp = p;
183 return 2;
184}
230fd6b7 185
6b691a5c 186static void asn1_put_length(unsigned char **pp, int length)
0f113f3e
MC
187{
188 unsigned char *p = *pp;
189 int i, l;
190 if (length <= 127)
191 *(p++) = (unsigned char)length;
192 else {
193 l = length;
194 for (i = 0; l > 0; i++)
195 l >>= 8;
196 *(p++) = i | 0x80;
197 l = i;
198 while (i-- > 0) {
199 p[i] = length & 0xff;
200 length >>= 8;
201 }
202 p += l;
203 }
204 *pp = p;
205}
d02b48c6 206
6343829a 207int ASN1_object_size(int constructed, int length, int tag)
0f113f3e 208{
e9f17097
DSH
209 int ret = 1;
210 if (length < 0)
211 return -1;
0f113f3e
MC
212 if (tag >= 31) {
213 while (tag > 0) {
214 tag >>= 7;
215 ret++;
216 }
217 }
e9f17097
DSH
218 if (constructed == 2) {
219 ret += 3;
220 } else {
221 ret++;
222 if (length > 127) {
223 int tmplen = length;
224 while (tmplen > 0) {
225 tmplen >>= 8;
226 ret++;
227 }
0f113f3e
MC
228 }
229 }
e9f17097
DSH
230 if (ret >= INT_MAX - length)
231 return -1;
232 return ret + length;
0f113f3e 233}
d02b48c6 234
18f54773 235int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
0f113f3e
MC
236{
237 if (str == NULL)
238 return 0;
239 dst->type = str->type;
240 if (!ASN1_STRING_set(dst, str->data, str->length))
241 return 0;
4002da0f
DSH
242 /* Copy flags but preserve embed value */
243 dst->flags &= ASN1_STRING_FLAG_EMBED;
244 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
0f113f3e
MC
245 return 1;
246}
18f54773 247
6384e46d 248ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
0f113f3e
MC
249{
250 ASN1_STRING *ret;
251 if (!str)
252 return NULL;
253 ret = ASN1_STRING_new();
90945fa3 254 if (ret == NULL)
0f113f3e
MC
255 return NULL;
256 if (!ASN1_STRING_copy(ret, str)) {
257 ASN1_STRING_free(ret);
258 return NULL;
259 }
260 return ret;
261}
d02b48c6 262
6343829a 263int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
0f113f3e
MC
264{
265 unsigned char *c;
266 const char *data = _data;
267
268 if (len < 0) {
269 if (data == NULL)
270 return (0);
271 else
272 len = strlen(data);
273 }
274 if ((str->length < len) || (str->data == NULL)) {
275 c = str->data;
2d29e2df 276 str->data = OPENSSL_realloc(c, len + 1);
0f113f3e
MC
277 if (str->data == NULL) {
278 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
279 str->data = c;
280 return (0);
281 }
282 }
283 str->length = len;
284 if (data != NULL) {
285 memcpy(str->data, data, len);
286 /* an allowance for strings :-) */
287 str->data[len] = '\0';
288 }
289 return (1);
290}
d02b48c6 291
6343829a 292void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
0f113f3e 293{
b548a1f1 294 OPENSSL_free(str->data);
0f113f3e
MC
295 str->data = data;
296 str->length = len;
297}
399a6f0b 298
6b691a5c 299ASN1_STRING *ASN1_STRING_new(void)
0f113f3e
MC
300{
301 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
302}
d02b48c6 303
6b691a5c 304ASN1_STRING *ASN1_STRING_type_new(int type)
0f113f3e
MC
305{
306 ASN1_STRING *ret;
307
64b25758 308 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
309 if (ret == NULL) {
310 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
311 return (NULL);
312 }
0f113f3e 313 ret->type = type;
0f113f3e
MC
314 return (ret);
315}
d02b48c6 316
6b691a5c 317void ASN1_STRING_free(ASN1_STRING *a)
0f113f3e
MC
318{
319 if (a == NULL)
320 return;
b548a1f1 321 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
0f113f3e 322 OPENSSL_free(a->data);
47c9a1b5
DSH
323 if (!(a->flags & ASN1_STRING_FLAG_EMBED))
324 OPENSSL_free(a);
0f113f3e 325}
d02b48c6 326
a8ae0891
DSH
327void ASN1_STRING_clear_free(ASN1_STRING *a)
328{
0dfb9398
RS
329 if (a == NULL)
330 return;
331 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
a8ae0891
DSH
332 OPENSSL_cleanse(a->data, a->length);
333 ASN1_STRING_free(a);
334}
335
6384e46d 336int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
0f113f3e
MC
337{
338 int i;
339
340 i = (a->length - b->length);
341 if (i == 0) {
342 i = memcmp(a->data, b->data, a->length);
343 if (i == 0)
344 return (a->type - b->type);
345 else
346 return (i);
347 } else
348 return (i);
349}
d02b48c6 350
6343829a 351int ASN1_STRING_length(const ASN1_STRING *x)
0f113f3e 352{
f422a514 353 return x->length;
0f113f3e 354}
08e9c1af 355
6343829a 356void ASN1_STRING_length_set(ASN1_STRING *x, int len)
0f113f3e 357{
f422a514 358 x->length = len;
0f113f3e 359}
08e9c1af 360
08275a29 361int ASN1_STRING_type(const ASN1_STRING *x)
0f113f3e 362{
f422a514 363 return x->type;
0f113f3e
MC
364}
365
17ebf85a
DSH
366const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
367{
368 return x->data;
369}
370
371# if OPENSSL_API_COMPAT < 0x10100000L
0f113f3e
MC
372unsigned char *ASN1_STRING_data(ASN1_STRING *x)
373{
f422a514 374 return x->data;
0f113f3e 375}
17ebf85a 376#endif