2 * Copyright 1999-2020 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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
14 /* ASN1 packing and unpacking functions */
16 ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
20 if (oct == NULL || *oct == NULL) {
21 if ((octmp = ASN1_STRING_new()) == NULL) {
22 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
29 ASN1_STRING_set0(octmp, NULL, 0);
31 if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) {
32 ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);
35 if (octmp->data == NULL) {
36 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
40 if (oct != NULL && *oct == NULL)
45 if (oct == NULL || *oct == NULL)
46 ASN1_STRING_free(octmp);
50 /* Extract an ASN1 object from an ASN1_STRING */
52 void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
54 const unsigned char *p;
58 if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL)
59 ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
63 void *ASN1_item_unpack_ex(const ASN1_STRING *oct, const ASN1_ITEM *it,
64 OSSL_LIB_CTX *libctx, const char *propq)
66 const unsigned char *p;
70 if ((ret = ASN1_item_d2i_ex(NULL, &p, oct->length, it,\
71 libctx, propq)) == NULL)
72 ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);