1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 #include "internal/cryptlib.h"
61 #include <openssl/asn1.h>
63 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
65 static void asn1_put_length(unsigned char **pp, int length);
67 static int _asn1_check_infinite_end(const unsigned char **p, long len)
70 * If there is 0 or 1 byte left, the length check should pick things up
74 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
81 int ASN1_check_infinite_end(unsigned char **p, long len)
83 return _asn1_check_infinite_end((const unsigned char **)p, len);
86 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
88 return _asn1_check_infinite_end(p, len);
91 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
92 int *pclass, long omax)
96 const unsigned char *p = *pp;
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 */
115 if (l > (INT_MAX >> 7L))
131 if (!asn1_get_length(&p, &inf, plength, (int)max))
134 if (inf && !(ret & V_ASN1_CONSTRUCTED))
137 if (*plength > (omax - (p - *pp))) {
138 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
140 * Set this so that even if things are not long enough the values are
148 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
152 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
155 const unsigned char *p = *pp;
156 unsigned long ret = 0;
171 /* Skip leading zeroes */
172 while (i && *p == 0) {
176 if (i > sizeof(long))
193 * class 0 is constructed constructed == 2 for indefinite length constructed
195 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
198 unsigned char *p = *pp;
201 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
202 i |= (xclass & V_ASN1_PRIVATE);
204 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
206 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
207 for (i = 0, ttag = tag; ttag > 0; i++)
218 if (constructed == 2)
221 asn1_put_length(&p, length);
225 int ASN1_put_eoc(unsigned char **pp)
227 unsigned char *p = *pp;
234 static void asn1_put_length(unsigned char **pp, int length)
236 unsigned char *p = *pp;
239 *(p++) = (unsigned char)length;
242 for (i = 0; l > 0; i++)
247 p[i] = length & 0xff;
255 int ASN1_object_size(int constructed, int length, int tag)
267 if (constructed == 2)
279 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
283 dst->type = str->type;
284 if (!ASN1_STRING_set(dst, str->data, str->length))
286 /* Copy flags but preserve embed value */
287 dst->flags &= ASN1_STRING_FLAG_EMBED;
288 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
292 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
297 ret = ASN1_STRING_new();
300 if (!ASN1_STRING_copy(ret, str)) {
301 ASN1_STRING_free(ret);
307 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
310 const char *data = _data;
318 if ((str->length < len) || (str->data == NULL)) {
320 str->data = OPENSSL_realloc(c, len + 1);
321 if (str->data == NULL) {
322 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
329 memcpy(str->data, data, len);
330 /* an allowance for strings :-) */
331 str->data[len] = '\0';
336 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
338 OPENSSL_free(str->data);
343 ASN1_STRING *ASN1_STRING_new(void)
345 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
348 ASN1_STRING *ASN1_STRING_type_new(int type)
352 ret = OPENSSL_zalloc(sizeof(*ret));
354 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
361 void ASN1_STRING_free(ASN1_STRING *a)
365 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
366 OPENSSL_free(a->data);
367 if (!(a->flags & ASN1_STRING_FLAG_EMBED))
371 void ASN1_STRING_clear_free(ASN1_STRING *a)
375 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
376 OPENSSL_cleanse(a->data, a->length);
380 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
384 i = (a->length - b->length);
386 i = memcmp(a->data, b->data, a->length);
388 return (a->type - b->type);
395 int ASN1_STRING_length(const ASN1_STRING *x)
400 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
405 int ASN1_STRING_type(ASN1_STRING *x)
410 unsigned char *ASN1_STRING_data(ASN1_STRING *x)