xref: /third_party/openssl/crypto/asn1/asn_pack.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/asn1.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci/* ASN1 packing and unpacking functions */
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ciASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
17e1051a39Sopenharmony_ci{
18e1051a39Sopenharmony_ci    ASN1_STRING *octmp;
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci     if (oct == NULL || *oct == NULL) {
21e1051a39Sopenharmony_ci        if ((octmp = ASN1_STRING_new()) == NULL) {
22e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
23e1051a39Sopenharmony_ci            return NULL;
24e1051a39Sopenharmony_ci        }
25e1051a39Sopenharmony_ci    } else {
26e1051a39Sopenharmony_ci        octmp = *oct;
27e1051a39Sopenharmony_ci    }
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    OPENSSL_free(octmp->data);
30e1051a39Sopenharmony_ci    octmp->data = NULL;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) {
33e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);
34e1051a39Sopenharmony_ci        goto err;
35e1051a39Sopenharmony_ci    }
36e1051a39Sopenharmony_ci    if (octmp->data == NULL) {
37e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
38e1051a39Sopenharmony_ci        goto err;
39e1051a39Sopenharmony_ci    }
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    if (oct != NULL && *oct == NULL)
42e1051a39Sopenharmony_ci        *oct = octmp;
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    return octmp;
45e1051a39Sopenharmony_ci err:
46e1051a39Sopenharmony_ci    if (oct == NULL || *oct == NULL)
47e1051a39Sopenharmony_ci        ASN1_STRING_free(octmp);
48e1051a39Sopenharmony_ci    return NULL;
49e1051a39Sopenharmony_ci}
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci/* Extract an ASN1 object from an ASN1_STRING */
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_civoid *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
54e1051a39Sopenharmony_ci{
55e1051a39Sopenharmony_ci    const unsigned char *p;
56e1051a39Sopenharmony_ci    void *ret;
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    p = oct->data;
59e1051a39Sopenharmony_ci    if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL)
60e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
61e1051a39Sopenharmony_ci    return ret;
62e1051a39Sopenharmony_ci}
63