]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/asn1_lib.c
Fix pointer size issues with argv on VMS
[thirdparty/openssl.git] / crypto / asn1 / asn1_lib.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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.
0f113f3e 7 *
d02b48c6
RE
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).
0f113f3e 14 *
d02b48c6
RE
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.
0f113f3e 21 *
d02b48c6
RE
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 :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
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.
0f113f3e 51 *
d02b48c6
RE
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#include <stdio.h>
f9082268 59#include <limits.h>
b39fc560 60#include "internal/cryptlib.h"
ec577822 61#include <openssl/asn1.h>
d02b48c6 62
0f113f3e
MC
63static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
64 int max);
d02b48c6 65static void asn1_put_length(unsigned char **pp, int length);
d02b48c6 66
875a644a 67static int _asn1_check_infinite_end(const unsigned char **p, long len)
0f113f3e
MC
68{
69 /*
70 * If there is 0 or 1 byte left, the length check should pick things up
71 */
72 if (len <= 0)
73 return (1);
74 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
75 (*p) += 2;
76 return (1);
77 }
78 return (0);
79}
d02b48c6 80
6343829a 81int ASN1_check_infinite_end(unsigned char **p, long len)
0f113f3e
MC
82{
83 return _asn1_check_infinite_end((const unsigned char **)p, len);
84}
d02b48c6 85
6343829a 86int ASN1_const_check_infinite_end(const unsigned char **p, long len)
0f113f3e
MC
87{
88 return _asn1_check_infinite_end(p, len);
89}
875a644a 90
6343829a 91int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
0f113f3e
MC
92 int *pclass, long omax)
93{
94 int i, ret;
95 long l;
96 const unsigned char *p = *pp;
97 int tag, xclass, inf;
98 long max = omax;
99
100 if (!max)
101 goto err;
102 ret = (*p & V_ASN1_CONSTRUCTED);
103 xclass = (*p & V_ASN1_PRIVATE);
104 i = *p & V_ASN1_PRIMITIVE_TAG;
105 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
106 p++;
107 if (--max == 0)
108 goto err;
109 l = 0;
110 while (*p & 0x80) {
111 l <<= 7L;
112 l |= *(p++) & 0x7f;
113 if (--max == 0)
114 goto err;
115 if (l > (INT_MAX >> 7L))
116 goto err;
117 }
118 l <<= 7L;
119 l |= *(p++) & 0x7f;
120 tag = (int)l;
121 if (--max == 0)
122 goto err;
123 } else {
124 tag = i;
125 p++;
126 if (--max == 0)
127 goto err;
128 }
129 *ptag = tag;
130 *pclass = xclass;
131 if (!asn1_get_length(&p, &inf, plength, (int)max))
132 goto err;
133
134 if (inf && !(ret & V_ASN1_CONSTRUCTED))
135 goto err;
398e99fe 136
0f113f3e
MC
137 if (*plength > (omax - (p - *pp))) {
138 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
139 /*
140 * Set this so that even if things are not long enough the values are
141 * set correctly
142 */
143 ret |= 0x80;
144 }
145 *pp = p;
146 return (ret | inf);
147 err:
148 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
149 return (0x80);
150}
151
152static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
153 int max)
154{
155 const unsigned char *p = *pp;
156 unsigned long ret = 0;
157 unsigned int i;
158
159 if (max-- < 1)
160 return (0);
161 if (*p == 0x80) {
162 *inf = 1;
163 ret = 0;
164 p++;
165 } else {
166 *inf = 0;
167 i = *p & 0x7f;
168 if (*(p++) & 0x80) {
169 if (max < (int)i)
170 return 0;
171 /* Skip leading zeroes */
172 while (i && *p == 0) {
173 p++;
174 i--;
175 }
176 if (i > sizeof(long))
177 return 0;
178 while (i-- > 0) {
179 ret <<= 8L;
180 ret |= *(p++);
181 }
182 } else
183 ret = i;
184 }
185 if (ret > LONG_MAX)
186 return 0;
187 *pp = p;
188 *rl = (long)ret;
189 return (1);
190}
191
192/*
193 * class 0 is constructed constructed == 2 for indefinite length constructed
194 */
6343829a 195void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
0f113f3e
MC
196 int xclass)
197{
198 unsigned char *p = *pp;
199 int i, ttag;
200
201 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
202 i |= (xclass & V_ASN1_PRIVATE);
203 if (tag < 31)
204 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
205 else {
206 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
207 for (i = 0, ttag = tag; ttag > 0; i++)
208 ttag >>= 7;
209 ttag = i;
210 while (i-- > 0) {
211 p[i] = tag & 0x7f;
212 if (i != (ttag - 1))
213 p[i] |= 0x80;
214 tag >>= 7;
215 }
216 p += ttag;
217 }
218 if (constructed == 2)
219 *(p++) = 0x80;
220 else
221 asn1_put_length(&p, length);
222 *pp = p;
223}
d02b48c6 224
230fd6b7 225int ASN1_put_eoc(unsigned char **pp)
0f113f3e
MC
226{
227 unsigned char *p = *pp;
228 *p++ = 0;
229 *p++ = 0;
230 *pp = p;
231 return 2;
232}
230fd6b7 233
6b691a5c 234static void asn1_put_length(unsigned char **pp, int length)
0f113f3e
MC
235{
236 unsigned char *p = *pp;
237 int i, l;
238 if (length <= 127)
239 *(p++) = (unsigned char)length;
240 else {
241 l = length;
242 for (i = 0; l > 0; i++)
243 l >>= 8;
244 *(p++) = i | 0x80;
245 l = i;
246 while (i-- > 0) {
247 p[i] = length & 0xff;
248 length >>= 8;
249 }
250 p += l;
251 }
252 *pp = p;
253}
d02b48c6 254
6343829a 255int ASN1_object_size(int constructed, int length, int tag)
0f113f3e
MC
256{
257 int ret;
258
259 ret = length;
260 ret++;
261 if (tag >= 31) {
262 while (tag > 0) {
263 tag >>= 7;
264 ret++;
265 }
266 }
267 if (constructed == 2)
268 return ret + 3;
269 ret++;
270 if (length > 127) {
271 while (length > 0) {
272 length >>= 8;
273 ret++;
274 }
275 }
276 return (ret);
277}
d02b48c6 278
18f54773 279int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
0f113f3e
MC
280{
281 if (str == NULL)
282 return 0;
283 dst->type = str->type;
284 if (!ASN1_STRING_set(dst, str->data, str->length))
285 return 0;
4002da0f
DSH
286 /* Copy flags but preserve embed value */
287 dst->flags &= ASN1_STRING_FLAG_EMBED;
288 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
0f113f3e
MC
289 return 1;
290}
18f54773 291
6384e46d 292ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
0f113f3e
MC
293{
294 ASN1_STRING *ret;
295 if (!str)
296 return NULL;
297 ret = ASN1_STRING_new();
90945fa3 298 if (ret == NULL)
0f113f3e
MC
299 return NULL;
300 if (!ASN1_STRING_copy(ret, str)) {
301 ASN1_STRING_free(ret);
302 return NULL;
303 }
304 return ret;
305}
d02b48c6 306
6343829a 307int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
0f113f3e
MC
308{
309 unsigned char *c;
310 const char *data = _data;
311
312 if (len < 0) {
313 if (data == NULL)
314 return (0);
315 else
316 len = strlen(data);
317 }
318 if ((str->length < len) || (str->data == NULL)) {
319 c = str->data;
2d29e2df 320 str->data = OPENSSL_realloc(c, len + 1);
0f113f3e
MC
321 if (str->data == NULL) {
322 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
323 str->data = c;
324 return (0);
325 }
326 }
327 str->length = len;
328 if (data != NULL) {
329 memcpy(str->data, data, len);
330 /* an allowance for strings :-) */
331 str->data[len] = '\0';
332 }
333 return (1);
334}
d02b48c6 335
6343829a 336void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
0f113f3e 337{
b548a1f1 338 OPENSSL_free(str->data);
0f113f3e
MC
339 str->data = data;
340 str->length = len;
341}
399a6f0b 342
6b691a5c 343ASN1_STRING *ASN1_STRING_new(void)
0f113f3e
MC
344{
345 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
346}
d02b48c6 347
6b691a5c 348ASN1_STRING *ASN1_STRING_type_new(int type)
0f113f3e
MC
349{
350 ASN1_STRING *ret;
351
64b25758 352 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
353 if (ret == NULL) {
354 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
355 return (NULL);
356 }
0f113f3e 357 ret->type = type;
0f113f3e
MC
358 return (ret);
359}
d02b48c6 360
6b691a5c 361void ASN1_STRING_free(ASN1_STRING *a)
0f113f3e
MC
362{
363 if (a == NULL)
364 return;
b548a1f1 365 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
0f113f3e 366 OPENSSL_free(a->data);
47c9a1b5
DSH
367 if (!(a->flags & ASN1_STRING_FLAG_EMBED))
368 OPENSSL_free(a);
0f113f3e 369}
d02b48c6 370
a8ae0891
DSH
371void ASN1_STRING_clear_free(ASN1_STRING *a)
372{
0dfb9398
RS
373 if (a == NULL)
374 return;
375 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
a8ae0891
DSH
376 OPENSSL_cleanse(a->data, a->length);
377 ASN1_STRING_free(a);
378}
379
6384e46d 380int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
0f113f3e
MC
381{
382 int i;
383
384 i = (a->length - b->length);
385 if (i == 0) {
386 i = memcmp(a->data, b->data, a->length);
387 if (i == 0)
388 return (a->type - b->type);
389 else
390 return (i);
391 } else
392 return (i);
393}
d02b48c6 394
6343829a 395int ASN1_STRING_length(const ASN1_STRING *x)
0f113f3e 396{
f422a514 397 return x->length;
0f113f3e 398}
08e9c1af 399
6343829a 400void ASN1_STRING_length_set(ASN1_STRING *x, int len)
0f113f3e 401{
f422a514 402 x->length = len;
0f113f3e 403}
08e9c1af
DSH
404
405int ASN1_STRING_type(ASN1_STRING *x)
0f113f3e 406{
f422a514 407 return x->type;
0f113f3e
MC
408}
409
410unsigned char *ASN1_STRING_data(ASN1_STRING *x)
411{
f422a514 412 return x->data;
0f113f3e 413}