16f9291ceSJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
1074664626SKris Kennaway #include <stdio.h>
11e71b7053SJung-uk Kim #include "internal/cryptlib.h"
1274664626SKris Kennaway #include <openssl/asn1.h>
1374664626SKris Kennaway
1474664626SKris Kennaway /* ASN1 packing and unpacking functions */
1574664626SKris Kennaway
ASN1_item_pack(void * obj,const ASN1_ITEM * it,ASN1_STRING ** oct)165c87c606SMark Murray ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
175c87c606SMark Murray {
185c87c606SMark Murray ASN1_STRING *octmp;
195c87c606SMark Murray
20e71b7053SJung-uk Kim if (oct == NULL || *oct == NULL) {
21e71b7053SJung-uk Kim if ((octmp = ASN1_STRING_new()) == NULL) {
22*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
235c87c606SMark Murray return NULL;
245c87c606SMark Murray }
25e71b7053SJung-uk Kim } else {
266f9291ceSJung-uk Kim octmp = *oct;
27e71b7053SJung-uk Kim }
285c87c606SMark Murray
295c87c606SMark Murray OPENSSL_free(octmp->data);
305c87c606SMark Murray octmp->data = NULL;
31e71b7053SJung-uk Kim
32*b077aed3SPierre Pronchery if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) {
33*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);
34e71b7053SJung-uk Kim goto err;
35e71b7053SJung-uk Kim }
36e71b7053SJung-uk Kim if (octmp->data == NULL) {
37*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
38e71b7053SJung-uk Kim goto err;
395c87c606SMark Murray }
405c87c606SMark Murray
41e71b7053SJung-uk Kim if (oct != NULL && *oct == NULL)
42e71b7053SJung-uk Kim *oct = octmp;
43e71b7053SJung-uk Kim
445c87c606SMark Murray return octmp;
45e71b7053SJung-uk Kim err:
46e71b7053SJung-uk Kim if (oct == NULL || *oct == NULL)
47e71b7053SJung-uk Kim ASN1_STRING_free(octmp);
48e71b7053SJung-uk Kim return NULL;
495c87c606SMark Murray }
505c87c606SMark Murray
515c87c606SMark Murray /* Extract an ASN1 object from an ASN1_STRING */
525c87c606SMark Murray
ASN1_item_unpack(const ASN1_STRING * oct,const ASN1_ITEM * it)53e71b7053SJung-uk Kim void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
545c87c606SMark Murray {
553b4e3dcbSSimon L. B. Nielsen const unsigned char *p;
565c87c606SMark Murray void *ret;
575c87c606SMark Murray
585c87c606SMark Murray p = oct->data;
59e71b7053SJung-uk Kim if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL)
60*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
615c87c606SMark Murray return ret;
625c87c606SMark Murray }
63