2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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_local.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
28 if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
36 int ASN1_check_infinite_end(unsigned char **p, long len)
38 return _asn1_check_infinite_end((const unsigned char **)p, len);
41 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
43 return _asn1_check_infinite_end(p, len);
46 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
47 int *pclass, long omax)
51 const unsigned char *p = *pp;
56 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
59 ret = (*p & V_ASN1_CONSTRUCTED);
60 xclass = (*p & V_ASN1_PRIVATE);
61 i = *p & V_ASN1_PRIMITIVE_TAG;
62 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
72 if (len > (INT_MAX >> 7L))
88 if (!asn1_get_length(&p, &inf, plength, max))
91 if (inf && !(ret & V_ASN1_CONSTRUCTED))
94 if (*plength > (omax - (p - *pp))) {
95 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
97 * Set this so that even if things are not long enough the values are
105 ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
110 * Decode a length field.
111 * The short form is a single byte defining a length 0 - 127.
112 * The long form is a byte 0 - 127 with the top bit set and this indicates
113 * the number of following octets that contain the length. These octets
114 * are stored most significant digit first.
116 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
119 const unsigned char *p = *pp;
120 unsigned long ret = 0;
134 /* Skip leading zeroes */
135 while (i > 0 && *p == 0) {
139 if (i > (int)sizeof(long))
158 * constructed == 2 for indefinite length constructed
160 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
163 unsigned char *p = *pp;
166 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167 i |= (xclass & V_ASN1_PRIVATE);
169 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
171 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
172 for (i = 0, ttag = tag; ttag > 0; i++)
183 if (constructed == 2)
186 asn1_put_length(&p, length);
190 int ASN1_put_eoc(unsigned char **pp)
192 unsigned char *p = *pp;
200 static void asn1_put_length(unsigned char **pp, int length)
202 unsigned char *p = *pp;
206 *(p++) = (unsigned char)length;
209 for (i = 0; len > 0; i++)
214 p[i] = length & 0xff;
222 int ASN1_object_size(int constructed, int length, int tag)
234 if (constructed == 2) {
246 if (ret >= INT_MAX - length)
251 void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num)
254 str->flags |= ASN1_STRING_FLAG_BITS_LEFT | (num & 0x07);
257 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
261 dst->type = str->type;
262 if (!ASN1_STRING_set(dst, str->data, str->length))
264 /* Copy flags but preserve embed value */
265 dst->flags &= ASN1_STRING_FLAG_EMBED;
266 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
270 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
276 ret = ASN1_STRING_new();
279 if (!ASN1_STRING_copy(ret, str)) {
280 ASN1_STRING_free(ret);
286 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
289 const char *data = _data;
297 len = (size_t)len_in;
300 * Verify that the length fits within an integer for assignment to
301 * str->length below. The additional 1 is subtracted to allow for the
302 * '\0' terminator even though this isn't strictly necessary.
304 if (len > INT_MAX - 1) {
305 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
308 if ((size_t)str->length <= len || str->data == NULL) {
310 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
311 /* No NUL terminator in fuzzing builds */
312 str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
314 str->data = OPENSSL_realloc(c, len + 1);
316 if (str->data == NULL) {
323 memcpy(str->data, data, len);
324 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
325 /* Set the unused byte to something non NUL and printable. */
327 str->data[len] = '~';
330 * Add a NUL terminator. This should not be necessary - but we add it as
331 * a safety precaution
333 str->data[len] = '\0';
339 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
341 OPENSSL_free(str->data);
346 ASN1_STRING *ASN1_STRING_new(void)
348 return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
351 ASN1_STRING *ASN1_STRING_type_new(int type)
355 ret = OPENSSL_zalloc(sizeof(*ret));
362 void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
366 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
367 OPENSSL_free(a->data);
372 void ASN1_STRING_free(ASN1_STRING *a)
376 ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
379 void ASN1_STRING_clear_free(ASN1_STRING *a)
383 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
384 OPENSSL_cleanse(a->data, a->length);
388 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
392 i = (a->length - b->length);
395 i = memcmp(a->data, b->data, a->length);
397 return a->type - b->type;
405 int ASN1_STRING_length(const ASN1_STRING *x)
410 #ifndef OPENSSL_NO_DEPRECATED_3_0
411 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
417 int ASN1_STRING_type(const ASN1_STRING *x)
422 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
427 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
428 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
434 /* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */
435 char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
436 const char *sep, size_t max_len)
439 ASN1_UTF8STRING *current;
440 size_t length = 0, sep_len;
446 sep_len = strlen(sep);
448 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
449 current = sk_ASN1_UTF8STRING_value(text, i);
452 length += ASN1_STRING_length(current);
453 if (max_len != 0 && length > max_len)
456 if ((result = OPENSSL_malloc(length + 1)) == NULL)
460 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
461 current = sk_ASN1_UTF8STRING_value(text, i);
462 length = ASN1_STRING_length(current);
463 if (i > 0 && sep_len > 0) {
464 strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */
467 strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);