2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
15 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
17 static void asn1_put_length(unsigned char **pp, int length);
19 static int _asn1_check_infinite_end(const unsigned char **p, long len)
22 * If there is 0 or 1 byte left, the length check should pick things up
26 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
33 int ASN1_check_infinite_end(unsigned char **p, long len)
35 return _asn1_check_infinite_end((const unsigned char **)p, len);
38 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
40 return _asn1_check_infinite_end(p, len);
43 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
44 int *pclass, long omax)
48 const unsigned char *p = *pp;
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 */
67 if (l > (INT_MAX >> 7L))
83 if (!asn1_get_length(&p, &inf, plength, max))
86 if (inf && !(ret & V_ASN1_CONSTRUCTED))
89 if (*plength > (omax - (p - *pp))) {
90 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
92 * Set this so that even if things are not long enough the values are
100 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
104 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
107 const unsigned char *p = *pp;
108 unsigned long ret = 0;
121 if (max < (long)i + 1)
123 /* Skip leading zeroes */
124 while (i && *p == 0) {
128 if (i > sizeof(long))
145 * class 0 is constructed constructed == 2 for indefinite length constructed
147 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
150 unsigned char *p = *pp;
153 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
154 i |= (xclass & V_ASN1_PRIVATE);
156 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
158 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
159 for (i = 0, ttag = tag; ttag > 0; i++)
170 if (constructed == 2)
173 asn1_put_length(&p, length);
177 int ASN1_put_eoc(unsigned char **pp)
179 unsigned char *p = *pp;
186 static void asn1_put_length(unsigned char **pp, int length)
188 unsigned char *p = *pp;
191 *(p++) = (unsigned char)length;
194 for (i = 0; l > 0; i++)
199 p[i] = length & 0xff;
207 int ASN1_object_size(int constructed, int length, int tag)
219 if (constructed == 2)
231 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
235 dst->type = str->type;
236 if (!ASN1_STRING_set(dst, str->data, str->length))
238 /* Copy flags but preserve embed value */
239 dst->flags &= ASN1_STRING_FLAG_EMBED;
240 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
244 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
249 ret = ASN1_STRING_new();
252 if (!ASN1_STRING_copy(ret, str)) {
253 ASN1_STRING_free(ret);
259 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
262 const char *data = _data;
270 if ((str->length < len) || (str->data == NULL)) {
272 str->data = OPENSSL_realloc(c, len + 1);
273 if (str->data == NULL) {
274 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
281 memcpy(str->data, data, len);
282 /* an allowance for strings :-) */
283 str->data[len] = '\0';
288 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
290 OPENSSL_free(str->data);
295 ASN1_STRING *ASN1_STRING_new(void)
297 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
300 ASN1_STRING *ASN1_STRING_type_new(int type)
304 ret = OPENSSL_zalloc(sizeof(*ret));
306 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
313 void ASN1_STRING_free(ASN1_STRING *a)
317 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
318 OPENSSL_free(a->data);
319 if (!(a->flags & ASN1_STRING_FLAG_EMBED))
323 void ASN1_STRING_clear_free(ASN1_STRING *a)
327 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
328 OPENSSL_cleanse(a->data, a->length);
332 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
336 i = (a->length - b->length);
338 i = memcmp(a->data, b->data, a->length);
340 return (a->type - b->type);
347 int ASN1_STRING_length(const ASN1_STRING *x)
352 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
357 int ASN1_STRING_type(ASN1_STRING *x)
362 unsigned char *ASN1_STRING_data(ASN1_STRING *x)