| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
|
| 6 |
#include <openssl/err.h> |
| 7 |
#include <openssl/mem.h> |
| 8 |
|
| 9 |
|
| 10 |
ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out) { |
| 11 |
uint8_t *new_data = NULL; |
| 12 |
int len = ASN1_item_i2d(obj, &new_data, it); |
| 13 |
if (len <= 0) { |
| 14 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ENCODE_ERROR); |
| 15 |
return NULL; |
| 16 |
} |
| 17 |
|
| 18 |
ASN1_STRING *ret = NULL; |
| 19 |
if (out == NULL || *out == NULL) { |
| 20 |
ret = ASN1_STRING_new(); |
| 21 |
if (ret == NULL) { |
| 22 |
OPENSSL_free(new_data); |
| 23 |
return NULL; |
| 24 |
} |
| 25 |
} else { |
| 26 |
ret = *out; |
| 27 |
} |
| 28 |
|
| 29 |
ASN1_STRING_set0(ret, new_data, len); |
| 30 |
if (out != NULL) { |
| 31 |
*out = ret; |
| 32 |
} |
| 33 |
return ret; |
| 34 |
} |
| 35 |
|
| 36 |
void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it) { |
| 37 |
const unsigned char *p = oct->data; |
| 38 |
void *ret = ASN1_item_d2i(NULL, &p, oct->length, it); |
| 39 |
if (ret == NULL || p != oct->data + oct->length) { |
| 40 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
| 41 |
ASN1_item_free(ret, it); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
return ret; |
| 45 |
} |