xref: /freebsd/crypto/openssl/crypto/asn1/asn_pack.c (revision e71b70530d95c4f34d8bdbd78d1242df1ba4a945)
16f9291ceSJung-uk Kim /*
2*e71b7053SJung-uk Kim  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4*e71b7053SJung-uk Kim  * Licensed under the OpenSSL license (the "License").  You may not use
5*e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6*e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7*e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
1074664626SKris Kennaway #include <stdio.h>
11*e71b7053SJung-uk Kim #include "internal/cryptlib.h"
1274664626SKris Kennaway #include <openssl/asn1.h>
1374664626SKris Kennaway 
1474664626SKris Kennaway /* ASN1 packing and unpacking functions */
1574664626SKris Kennaway 
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 
20*e71b7053SJung-uk Kim      if (oct == NULL || *oct == NULL) {
21*e71b7053SJung-uk Kim         if ((octmp = ASN1_STRING_new()) == NULL) {
223b4e3dcbSSimon L. B. Nielsen             ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
235c87c606SMark Murray             return NULL;
245c87c606SMark Murray         }
25*e71b7053SJung-uk Kim     } else {
266f9291ceSJung-uk Kim         octmp = *oct;
27*e71b7053SJung-uk Kim     }
285c87c606SMark Murray 
295c87c606SMark Murray     OPENSSL_free(octmp->data);
305c87c606SMark Murray     octmp->data = NULL;
31*e71b7053SJung-uk Kim 
32*e71b7053SJung-uk Kim     if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) == 0) {
33*e71b7053SJung-uk Kim         ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
34*e71b7053SJung-uk Kim         goto err;
35*e71b7053SJung-uk Kim     }
36*e71b7053SJung-uk Kim     if (octmp->data == NULL) {
37*e71b7053SJung-uk Kim         ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
38*e71b7053SJung-uk Kim         goto err;
395c87c606SMark Murray     }
405c87c606SMark Murray 
41*e71b7053SJung-uk Kim     if (oct != NULL && *oct == NULL)
42*e71b7053SJung-uk Kim         *oct = octmp;
43*e71b7053SJung-uk Kim 
445c87c606SMark Murray     return octmp;
45*e71b7053SJung-uk Kim  err:
46*e71b7053SJung-uk Kim     if (oct == NULL || *oct == NULL)
47*e71b7053SJung-uk Kim         ASN1_STRING_free(octmp);
48*e71b7053SJung-uk Kim     return NULL;
495c87c606SMark Murray }
505c87c606SMark Murray 
515c87c606SMark Murray /* Extract an ASN1 object from an ASN1_STRING */
525c87c606SMark Murray 
53*e71b7053SJung-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;
59*e71b7053SJung-uk Kim     if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL)
603b4e3dcbSSimon L. B. Nielsen         ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR);
615c87c606SMark Murray     return ret;
625c87c606SMark Murray }
63