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);
105 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
108 const unsigned char *p = *pp;
109 unsigned long ret = 0;
122 if (max < (long)i + 1)
124 /* Skip leading zeroes */
125 while (i && *p == 0) {
129 if (i > sizeof(long))
146 * class 0 is constructed constructed == 2 for indefinite length constructed
148 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
151 unsigned char *p = *pp;
154 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
155 i |= (xclass & V_ASN1_PRIVATE);
157 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
159 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
160 for (i = 0, ttag = tag; ttag > 0; i++)
171 if (constructed == 2)
174 asn1_put_length(&p, length);
178 int ASN1_put_eoc(unsigned char **pp)
180 unsigned char *p = *pp;
187 static void asn1_put_length(unsigned char **pp, int length)
189 unsigned char *p = *pp;
192 *(p++) = (unsigned char)length;
195 for (i = 0; l > 0; i++)
200 p[i] = length & 0xff;
208 int ASN1_object_size(int constructed, int length, int tag)
219 if (constructed == 2) {
231 if (ret >= INT_MAX - length)
236 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
240 dst->type = str->type;
241 if (!ASN1_STRING_set(dst, str->data, str->length))
243 /* Copy flags but preserve embed value */
244 dst->flags &= ASN1_STRING_FLAG_EMBED;
245 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
249 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
254 ret = ASN1_STRING_new();
257 if (!ASN1_STRING_copy(ret, str)) {
258 ASN1_STRING_free(ret);
264 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
267 const char *data = _data;
275 if ((str->length <= len) || (str->data == NULL)) {
277 str->data = OPENSSL_realloc(c, len + 1);
278 if (str->data == NULL) {
279 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
286 memcpy(str->data, data, len);
287 /* an allowance for strings :-) */
288 str->data[len] = '\0';
293 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
295 OPENSSL_free(str->data);
300 ASN1_STRING *ASN1_STRING_new(void)
302 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
305 ASN1_STRING *ASN1_STRING_type_new(int type)
309 ret = OPENSSL_zalloc(sizeof(*ret));
311 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
318 void asn1_string_embed_free(ASN1_STRING *a, int embed)
322 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
323 OPENSSL_free(a->data);
328 void ASN1_STRING_free(ASN1_STRING *a)
332 asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
335 void ASN1_STRING_clear_free(ASN1_STRING *a)
339 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
340 OPENSSL_cleanse(a->data, a->length);
344 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
348 i = (a->length - b->length);
350 i = memcmp(a->data, b->data, a->length);
352 return (a->type - b->type);
359 int ASN1_STRING_length(const ASN1_STRING *x)
364 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
369 int ASN1_STRING_type(const ASN1_STRING *x)
374 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
379 # if OPENSSL_API_COMPAT < 0x10100000L
380 unsigned char *ASN1_STRING_data(ASN1_STRING *x)