2016-05-18 02:52:22 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2002-08-03 02:48:55 +08:00
|
|
|
#include <limits.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/asn1.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "asn1_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
2016-03-30 02:37:57 +08:00
|
|
|
long max);
|
1998-12-21 18:52:47 +08:00
|
|
|
static void asn1_put_length(unsigned char **pp, int length);
|
|
|
|
|
2004-03-16 07:15:26 +08:00
|
|
|
static int _asn1_check_infinite_end(const unsigned char **p, long len)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If there is 0 or 1 byte left, the length check should pick things up
|
|
|
|
*/
|
2020-03-05 15:43:06 +08:00
|
|
|
if (len <= 0) {
|
2017-07-13 06:23:22 +08:00
|
|
|
return 1;
|
2020-03-05 15:43:06 +08:00
|
|
|
} else {
|
|
|
|
if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
|
|
|
|
(*p) += 2;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-07-13 06:23:22 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
int ASN1_check_infinite_end(unsigned char **p, long len)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return _asn1_check_infinite_end((const unsigned char **)p, len);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
int ASN1_const_check_infinite_end(const unsigned char **p, long len)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return _asn1_check_infinite_end(p, len);
|
|
|
|
}
|
2004-03-16 07:15:26 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
2015-01-22 11:40:55 +08:00
|
|
|
int *pclass, long omax)
|
|
|
|
{
|
|
|
|
int i, ret;
|
2020-03-05 15:43:06 +08:00
|
|
|
long len;
|
2015-01-22 11:40:55 +08:00
|
|
|
const unsigned char *p = *pp;
|
|
|
|
int tag, xclass, inf;
|
|
|
|
long max = omax;
|
|
|
|
|
|
|
|
if (!max)
|
|
|
|
goto err;
|
|
|
|
ret = (*p & V_ASN1_CONSTRUCTED);
|
|
|
|
xclass = (*p & V_ASN1_PRIVATE);
|
|
|
|
i = *p & V_ASN1_PRIMITIVE_TAG;
|
|
|
|
if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
|
|
|
|
p++;
|
|
|
|
if (--max == 0)
|
|
|
|
goto err;
|
2020-03-05 15:43:06 +08:00
|
|
|
len = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
while (*p & 0x80) {
|
2020-03-05 15:43:06 +08:00
|
|
|
len <<= 7L;
|
|
|
|
len |= *(p++) & 0x7f;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (--max == 0)
|
|
|
|
goto err;
|
2020-03-05 15:43:06 +08:00
|
|
|
if (len > (INT_MAX >> 7L))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-03-05 15:43:06 +08:00
|
|
|
len <<= 7L;
|
|
|
|
len |= *(p++) & 0x7f;
|
|
|
|
tag = (int)len;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (--max == 0)
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
tag = i;
|
|
|
|
p++;
|
|
|
|
if (--max == 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
*ptag = tag;
|
|
|
|
*pclass = xclass;
|
2016-03-30 02:37:57 +08:00
|
|
|
if (!asn1_get_length(&p, &inf, plength, max))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (inf && !(ret & V_ASN1_CONSTRUCTED))
|
|
|
|
goto err;
|
2014-07-02 07:57:57 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (*plength > (omax - (p - *pp))) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Set this so that even if things are not long enough the values are
|
|
|
|
* set correctly
|
|
|
|
*/
|
|
|
|
ret |= 0x80;
|
|
|
|
}
|
|
|
|
*pp = p;
|
2017-07-13 06:23:22 +08:00
|
|
|
return ret | inf;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
|
2017-07-13 06:23:22 +08:00
|
|
|
return 0x80;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2017-07-12 06:54:14 +08:00
|
|
|
/*
|
|
|
|
* Decode a length field.
|
|
|
|
* The short form is a single byte defining a length 0 - 127.
|
|
|
|
* The long form is a byte 0 - 127 with the top bit set and this indicates
|
|
|
|
* the number of following octets that contain the length. These octets
|
|
|
|
* are stored most significant digit first.
|
|
|
|
*/
|
2015-01-22 11:40:55 +08:00
|
|
|
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
2016-03-30 02:37:57 +08:00
|
|
|
long max)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
const unsigned char *p = *pp;
|
|
|
|
unsigned long ret = 0;
|
2017-07-12 06:54:14 +08:00
|
|
|
int i;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (max-- < 1)
|
2016-03-30 02:37:57 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (*p == 0x80) {
|
|
|
|
*inf = 1;
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
*inf = 0;
|
|
|
|
i = *p & 0x7f;
|
2017-07-12 06:54:14 +08:00
|
|
|
if (*p++ & 0x80) {
|
|
|
|
if (max < i + 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
/* Skip leading zeroes */
|
2017-07-12 06:54:14 +08:00
|
|
|
while (i > 0 && *p == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
p++;
|
|
|
|
i--;
|
|
|
|
}
|
2017-07-12 06:54:14 +08:00
|
|
|
if (i > (int)sizeof(long))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2017-07-12 06:54:14 +08:00
|
|
|
while (i > 0) {
|
|
|
|
ret <<= 8;
|
|
|
|
ret |= *p++;
|
|
|
|
i--;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-07-12 06:54:14 +08:00
|
|
|
if (ret > LONG_MAX)
|
|
|
|
return 0;
|
2020-03-05 15:43:06 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = i;
|
2020-03-05 15:43:06 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
*pp = p;
|
|
|
|
*rl = (long)ret;
|
2016-03-30 02:37:57 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* class 0 is constructed constructed == 2 for indefinite length constructed
|
|
|
|
*/
|
2008-11-12 11:58:08 +08:00
|
|
|
void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
|
2015-01-22 11:40:55 +08:00
|
|
|
int xclass)
|
|
|
|
{
|
|
|
|
unsigned char *p = *pp;
|
|
|
|
int i, ttag;
|
|
|
|
|
|
|
|
i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
|
|
|
|
i |= (xclass & V_ASN1_PRIVATE);
|
2020-03-05 15:43:06 +08:00
|
|
|
if (tag < 31) {
|
2015-01-22 11:40:55 +08:00
|
|
|
*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
|
2020-03-05 15:43:06 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
*(p++) = i | V_ASN1_PRIMITIVE_TAG;
|
|
|
|
for (i = 0, ttag = tag; ttag > 0; i++)
|
|
|
|
ttag >>= 7;
|
|
|
|
ttag = i;
|
|
|
|
while (i-- > 0) {
|
|
|
|
p[i] = tag & 0x7f;
|
|
|
|
if (i != (ttag - 1))
|
|
|
|
p[i] |= 0x80;
|
|
|
|
tag >>= 7;
|
|
|
|
}
|
|
|
|
p += ttag;
|
|
|
|
}
|
|
|
|
if (constructed == 2)
|
|
|
|
*(p++) = 0x80;
|
|
|
|
else
|
|
|
|
asn1_put_length(&p, length);
|
|
|
|
*pp = p;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2002-10-03 20:38:52 +08:00
|
|
|
int ASN1_put_eoc(unsigned char **pp)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char *p = *pp;
|
2020-03-05 15:43:06 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
*p++ = 0;
|
|
|
|
*p++ = 0;
|
|
|
|
*pp = p;
|
|
|
|
return 2;
|
|
|
|
}
|
2002-10-03 20:38:52 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
static void asn1_put_length(unsigned char **pp, int length)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char *p = *pp;
|
2020-03-05 15:43:06 +08:00
|
|
|
int i, len;
|
|
|
|
|
|
|
|
if (length <= 127) {
|
2015-01-22 11:40:55 +08:00
|
|
|
*(p++) = (unsigned char)length;
|
2020-03-05 15:43:06 +08:00
|
|
|
} else {
|
|
|
|
len = length;
|
|
|
|
for (i = 0; len > 0; i++)
|
|
|
|
len >>= 8;
|
2015-01-22 11:40:55 +08:00
|
|
|
*(p++) = i | 0x80;
|
2020-03-05 15:43:06 +08:00
|
|
|
len = i;
|
2015-01-22 11:40:55 +08:00
|
|
|
while (i-- > 0) {
|
|
|
|
p[i] = length & 0xff;
|
|
|
|
length >>= 8;
|
|
|
|
}
|
2020-03-05 15:43:06 +08:00
|
|
|
p += len;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
*pp = p;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
int ASN1_object_size(int constructed, int length, int tag)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-08-02 07:30:47 +08:00
|
|
|
int ret = 1;
|
2020-03-05 15:43:06 +08:00
|
|
|
|
2016-08-02 07:30:47 +08:00
|
|
|
if (length < 0)
|
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (tag >= 31) {
|
|
|
|
while (tag > 0) {
|
|
|
|
tag >>= 7;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 07:30:47 +08:00
|
|
|
if (constructed == 2) {
|
|
|
|
ret += 3;
|
|
|
|
} else {
|
|
|
|
ret++;
|
|
|
|
if (length > 127) {
|
|
|
|
int tmplen = length;
|
|
|
|
while (tmplen > 0) {
|
|
|
|
tmplen >>= 8;
|
|
|
|
ret++;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 07:30:47 +08:00
|
|
|
if (ret >= INT_MAX - length)
|
|
|
|
return -1;
|
|
|
|
return ret + length;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2007-04-15 01:53:55 +08:00
|
|
|
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (str == NULL)
|
|
|
|
return 0;
|
|
|
|
dst->type = str->type;
|
|
|
|
if (!ASN1_STRING_set(dst, str->data, str->length))
|
|
|
|
return 0;
|
2015-10-12 06:25:08 +08:00
|
|
|
/* Copy flags but preserve embed value */
|
|
|
|
dst->flags &= ASN1_STRING_FLAG_EMBED;
|
|
|
|
dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2007-04-15 01:53:55 +08:00
|
|
|
|
2006-03-04 21:55:02 +08:00
|
|
|
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_STRING *ret;
|
2020-03-05 15:43:06 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
ret = ASN1_STRING_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ret == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
if (!ASN1_STRING_copy(ret, str)) {
|
|
|
|
ASN1_STRING_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-04-06 07:23:00 +08:00
|
|
|
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char *c;
|
|
|
|
const char *data = _data;
|
2020-04-06 07:23:00 +08:00
|
|
|
size_t len;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-04-06 07:23:00 +08:00
|
|
|
if (len_in < 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (data == NULL)
|
2017-07-13 06:23:22 +08:00
|
|
|
return 0;
|
2020-04-06 07:23:00 +08:00
|
|
|
len = strlen(data);
|
|
|
|
} else {
|
|
|
|
len = (size_t)len_in;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Verify that the length fits within an integer for assignment to
|
|
|
|
* str->length below. The additional 1 is subtracted to allow for the
|
|
|
|
* '\0' terminator even though this isn't strictly necessary.
|
|
|
|
*/
|
|
|
|
if (len > INT_MAX - 1) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
|
2020-04-06 07:23:00 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2020-04-06 07:23:00 +08:00
|
|
|
if ((size_t)str->length <= len || str->data == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
c = str->data;
|
2015-04-29 04:34:52 +08:00
|
|
|
str->data = OPENSSL_realloc(c, len + 1);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (str->data == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
str->data = c;
|
2017-07-13 06:23:22 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
str->length = len;
|
|
|
|
if (data != NULL) {
|
|
|
|
memcpy(str->data, data, len);
|
|
|
|
/* an allowance for strings :-) */
|
|
|
|
str->data[len] = '\0';
|
|
|
|
}
|
2017-07-13 06:23:22 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(str->data);
|
2015-01-22 11:40:55 +08:00
|
|
|
str->data = data;
|
|
|
|
str->length = len;
|
|
|
|
}
|
2006-05-08 20:44:25 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
ASN1_STRING *ASN1_STRING_new(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-07-13 06:23:22 +08:00
|
|
|
return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
ASN1_STRING *ASN1_STRING_type_new(int type)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_STRING *ret;
|
|
|
|
|
2015-09-03 21:15:26 +08:00
|
|
|
ret = OPENSSL_zalloc(sizeof(*ret));
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
|
2017-07-13 06:23:22 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
ret->type = type;
|
2017-07-13 06:23:22 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-10-02 21:13:40 +08:00
|
|
|
void asn1_string_embed_free(ASN1_STRING *a, int embed)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (a == NULL)
|
|
|
|
return;
|
2015-05-01 22:02:07 +08:00
|
|
|
if (!(a->flags & ASN1_STRING_FLAG_NDEF))
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(a->data);
|
2016-10-02 21:13:40 +08:00
|
|
|
if (embed == 0)
|
2015-10-07 20:28:46 +08:00
|
|
|
OPENSSL_free(a);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-10-02 21:13:40 +08:00
|
|
|
void ASN1_STRING_free(ASN1_STRING *a)
|
|
|
|
{
|
|
|
|
if (a == NULL)
|
|
|
|
return;
|
|
|
|
asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:20:23 +08:00
|
|
|
void ASN1_STRING_clear_free(ASN1_STRING *a)
|
|
|
|
{
|
2015-03-24 19:52:24 +08:00
|
|
|
if (a == NULL)
|
|
|
|
return;
|
|
|
|
if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
|
2015-03-03 22:20:23 +08:00
|
|
|
OPENSSL_cleanse(a->data, a->length);
|
|
|
|
ASN1_STRING_free(a);
|
|
|
|
}
|
|
|
|
|
2006-03-04 21:55:02 +08:00
|
|
|
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = (a->length - b->length);
|
|
|
|
if (i == 0) {
|
|
|
|
i = memcmp(a->data, b->data, a->length);
|
|
|
|
if (i == 0)
|
2017-07-13 06:23:22 +08:00
|
|
|
return a->type - b->type;
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
2017-07-13 06:23:22 +08:00
|
|
|
return i;
|
2020-03-05 15:43:06 +08:00
|
|
|
} else {
|
2017-07-13 06:23:22 +08:00
|
|
|
return i;
|
2020-03-05 15:43:06 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
int ASN1_STRING_length(const ASN1_STRING *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-03-14 12:16:42 +08:00
|
|
|
return x->length;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-10-20 09:50:23 +08:00
|
|
|
|
2020-09-19 03:21:43 +08:00
|
|
|
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
2008-11-12 11:58:08 +08:00
|
|
|
void ASN1_STRING_length_set(ASN1_STRING *x, int len)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-03-14 12:16:42 +08:00
|
|
|
x->length = len;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2020-09-19 03:21:43 +08:00
|
|
|
#endif
|
1999-10-20 09:50:23 +08:00
|
|
|
|
2016-07-04 04:09:02 +08:00
|
|
|
int ASN1_STRING_type(const ASN1_STRING *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-03-14 12:16:42 +08:00
|
|
|
return x->type;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:06:48 +08:00
|
|
|
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
|
|
|
|
{
|
|
|
|
return x->data;
|
|
|
|
}
|
|
|
|
|
2020-03-05 15:43:06 +08:00
|
|
|
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *ASN1_STRING_data(ASN1_STRING *x)
|
|
|
|
{
|
2015-03-14 12:16:42 +08:00
|
|
|
return x->data;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-08-16 21:06:48 +08:00
|
|
|
#endif
|
2020-03-05 15:43:06 +08:00
|
|
|
|
|
|
|
char *sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, const char *sep,
|
|
|
|
size_t max_len /* excluding NUL terminator */)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ASN1_UTF8STRING *current;
|
|
|
|
size_t length = 0, sep_len;
|
|
|
|
char *result = NULL;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!ossl_assert(sep != NULL))
|
|
|
|
return NULL;
|
|
|
|
sep_len = strlen(sep);
|
|
|
|
|
|
|
|
for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
|
|
|
|
current = sk_ASN1_UTF8STRING_value(text, i);
|
|
|
|
if (i > 0)
|
|
|
|
length += sep_len;
|
|
|
|
length += ASN1_STRING_length(current);
|
|
|
|
if (length > max_len)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((result = OPENSSL_malloc(length + 1)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) {
|
|
|
|
current = sk_ASN1_UTF8STRING_value(text, i);
|
|
|
|
length = ASN1_STRING_length(current);
|
|
|
|
if (i > 0 && sep_len > 0) {
|
2020-03-20 22:15:34 +08:00
|
|
|
strncpy(p, sep, sep_len + 1);
|
2020-03-05 15:43:06 +08:00
|
|
|
p += sep_len;
|
|
|
|
}
|
|
|
|
strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
|
|
|
|
p += length;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|