1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 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/asn1t.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#ifndef NO_OLD_ASN1
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_civoid *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, const void *x)
17e1051a39Sopenharmony_ci{
18e1051a39Sopenharmony_ci    unsigned char *b, *p;
19e1051a39Sopenharmony_ci    const unsigned char *p2;
20e1051a39Sopenharmony_ci    int i;
21e1051a39Sopenharmony_ci    char *ret;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci    if (x == NULL)
24e1051a39Sopenharmony_ci        return NULL;
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci    i = i2d(x, NULL);
27e1051a39Sopenharmony_ci    if (i <= 0)
28e1051a39Sopenharmony_ci        return NULL;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci    b = OPENSSL_malloc(i + 10);
31e1051a39Sopenharmony_ci    if (b == NULL) {
32e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
33e1051a39Sopenharmony_ci        return NULL;
34e1051a39Sopenharmony_ci    }
35e1051a39Sopenharmony_ci    p = b;
36e1051a39Sopenharmony_ci    i = i2d(x, &p);
37e1051a39Sopenharmony_ci    p2 = b;
38e1051a39Sopenharmony_ci    ret = d2i(NULL, &p2, i);
39e1051a39Sopenharmony_ci    OPENSSL_free(b);
40e1051a39Sopenharmony_ci    return ret;
41e1051a39Sopenharmony_ci}
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci#endif
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci/*
46e1051a39Sopenharmony_ci * ASN1_ITEM version of dup: this follows the model above except we don't
47e1051a39Sopenharmony_ci * need to allocate the buffer. At some point this could be rewritten to
48e1051a39Sopenharmony_ci * directly dup the underlying structure instead of doing and encode and
49e1051a39Sopenharmony_ci * decode.
50e1051a39Sopenharmony_ci */
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_civoid *ASN1_item_dup(const ASN1_ITEM *it, const void *x)
53e1051a39Sopenharmony_ci{
54e1051a39Sopenharmony_ci    ASN1_aux_cb *asn1_cb = NULL;
55e1051a39Sopenharmony_ci    unsigned char *b = NULL;
56e1051a39Sopenharmony_ci    const unsigned char *p;
57e1051a39Sopenharmony_ci    long i;
58e1051a39Sopenharmony_ci    ASN1_VALUE *ret;
59e1051a39Sopenharmony_ci    OSSL_LIB_CTX *libctx = NULL;
60e1051a39Sopenharmony_ci    const char *propq = NULL;
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci    if (x == NULL)
63e1051a39Sopenharmony_ci        return NULL;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    if (it->itype == ASN1_ITYPE_SEQUENCE || it->itype == ASN1_ITYPE_CHOICE
66e1051a39Sopenharmony_ci        || it->itype == ASN1_ITYPE_NDEF_SEQUENCE) {
67e1051a39Sopenharmony_ci        const ASN1_AUX *aux = it->funcs;
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci        asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
70e1051a39Sopenharmony_ci    }
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ci    if (asn1_cb != NULL) {
73e1051a39Sopenharmony_ci        if (!asn1_cb(ASN1_OP_DUP_PRE, (ASN1_VALUE **)&x, it, NULL)
74e1051a39Sopenharmony_ci                || !asn1_cb(ASN1_OP_GET0_LIBCTX, (ASN1_VALUE **)&x, it, &libctx)
75e1051a39Sopenharmony_ci                || !asn1_cb(ASN1_OP_GET0_PROPQ, (ASN1_VALUE **)&x, it, &propq))
76e1051a39Sopenharmony_ci            goto auxerr;
77e1051a39Sopenharmony_ci    }
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    i = ASN1_item_i2d(x, &b, it);
80e1051a39Sopenharmony_ci    if (b == NULL) {
81e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
82e1051a39Sopenharmony_ci        return NULL;
83e1051a39Sopenharmony_ci    }
84e1051a39Sopenharmony_ci    p = b;
85e1051a39Sopenharmony_ci    ret = ASN1_item_d2i_ex(NULL, &p, i, it, libctx, propq);
86e1051a39Sopenharmony_ci    OPENSSL_free(b);
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    if (asn1_cb != NULL
89e1051a39Sopenharmony_ci        && !asn1_cb(ASN1_OP_DUP_POST, &ret, it, (void *)x))
90e1051a39Sopenharmony_ci        goto auxerr;
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    return ret;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci auxerr:
95e1051a39Sopenharmony_ci    ERR_raise_data(ERR_LIB_ASN1, ASN1_R_AUX_ERROR, "Type=%s", it->sname);
96e1051a39Sopenharmony_ci    return NULL;
97e1051a39Sopenharmony_ci}
98