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>
14 #include "asn1_locl.h"
16 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
18 static void asn1_put_length(unsigned char **pp, int length);
20 static int _asn1_check_infinite_end(const unsigned char **p, long len)
23 * If there is 0 or 1 byte left, the length check should pick things up
27 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
34 int ASN1_check_infinite_end(unsigned char **p, long len)
36 return _asn1_check_infinite_end((const unsigned char **)p, len);
39 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
41 return _asn1_check_infinite_end(p, len);
44 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
45 int *pclass, long omax)
49 const unsigned char *p = *pp;
55 ret = (*p & V_ASN1_CONSTRUCTED);
56 xclass = (*p & V_ASN1_PRIVATE);
57 i = *p & V_ASN1_PRIMITIVE_TAG;
58 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
68 if (l > (INT_MAX >> 7L))
84 if (!asn1_get_length(&p, &inf, plength, max))
87 if (inf && !(ret & V_ASN1_CONSTRUCTED))
90 if (*plength > (omax - (p - *pp))) {
91 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
93 * Set this so that even if things are not long enough the values are
101 ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
106 * Decode a length field.
107 * The short form is a single byte defining a length 0 - 127.
108 * The long form is a byte 0 - 127 with the top bit set and this indicates
109 * the number of following octets that contain the length. These octets
110 * are stored most significant digit first.
112 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
115 const unsigned char *p = *pp;
116 unsigned long ret = 0;
130 /* Skip leading zeroes */
131 while (i > 0 && *p == 0) {
135 if (i > (int)sizeof(long))
153 * class 0 is constructed constructed == 2 for indefinite length constructed
155 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
158 unsigned char *p = *pp;
161 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
162 i |= (xclass & V_ASN1_PRIVATE);
164 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
166 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
167 for (i = 0, ttag = tag; ttag > 0; i++)
178 if (constructed == 2)
181 asn1_put_length(&p, length);
185 int ASN1_put_eoc(unsigned char **pp)
187 unsigned char *p = *pp;
194 static void asn1_put_length(unsigned char **pp, int length)
196 unsigned char *p = *pp;
199 *(p++) = (unsigned char)length;
202 for (i = 0; l > 0; i++)
207 p[i] = length & 0xff;
215 int ASN1_object_size(int constructed, int length, int tag)
226 if (constructed == 2) {
238 if (ret >= INT_MAX - length)
243 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
247 dst->type = str->type;
248 if (!ASN1_STRING_set(dst, str->data, str->length))
250 /* Copy flags but preserve embed value */
251 dst->flags &= ASN1_STRING_FLAG_EMBED;
252 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
256 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
261 ret = ASN1_STRING_new();
264 if (!ASN1_STRING_copy(ret, str)) {
265 ASN1_STRING_free(ret);
271 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
274 const char *data = _data;
282 if ((str->length <= len) || (str->data == NULL)) {
284 str->data = OPENSSL_realloc(c, len + 1);
285 if (str->data == NULL) {
286 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
293 memcpy(str->data, data, len);
294 /* an allowance for strings :-) */
295 str->data[len] = '\0';
300 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
302 OPENSSL_free(str->data);
307 ASN1_STRING *ASN1_STRING_new(void)
309 return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
312 ASN1_STRING *ASN1_STRING_type_new(int type)
316 ret = OPENSSL_zalloc(sizeof(*ret));
318 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
325 void asn1_string_embed_free(ASN1_STRING *a, int embed)
329 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
330 OPENSSL_free(a->data);
335 void ASN1_STRING_free(ASN1_STRING *a)
339 asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
342 void ASN1_STRING_clear_free(ASN1_STRING *a)
346 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
347 OPENSSL_cleanse(a->data, a->length);
351 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
355 i = (a->length - b->length);
357 i = memcmp(a->data, b->data, a->length);
359 return a->type - b->type;
366 int ASN1_STRING_length(const ASN1_STRING *x)
371 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
376 int ASN1_STRING_type(const ASN1_STRING *x)
381 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
386 # if OPENSSL_API_COMPAT < 0x10100000L
387 unsigned char *ASN1_STRING_data(ASN1_STRING *x)