2 * Copyright 1995-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/asn1t.h>
16 void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, const void *x)
19 const unsigned char *p2;
30 b = OPENSSL_malloc(i + 10);
32 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
38 ret = d2i(NULL, &p2, i);
46 * ASN1_ITEM version of dup: this follows the model above except we don't
47 * need to allocate the buffer. At some point this could be rewritten to
48 * directly dup the underlying structure instead of doing and encode and
52 void *ASN1_item_dup(const ASN1_ITEM *it, const void *x)
54 ASN1_aux_cb *asn1_cb = NULL;
55 unsigned char *b = NULL;
56 const unsigned char *p;
63 if (it->itype == ASN1_ITYPE_SEQUENCE || it->itype == ASN1_ITYPE_CHOICE
64 || it->itype == ASN1_ITYPE_NDEF_SEQUENCE) {
65 const ASN1_AUX *aux = it->funcs;
67 asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
71 && !asn1_cb(ASN1_OP_DUP_PRE, (ASN1_VALUE **)&x, it, NULL))
74 i = ASN1_item_i2d(x, &b, it);
76 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
80 ret = ASN1_item_d2i(NULL, &p, i, it);
84 && !asn1_cb(ASN1_OP_DUP_POST, &ret, it, (void *)x))
90 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_AUX_ERROR, "Type=%s", it->sname);