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