Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_dup.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <openssl/err.h>
7 #include <openssl/mem.h>
8
9 void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *input) {
10 if (i2d == NULL || d2i == NULL || input == NULL) {
11 OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
12 return NULL;
13 }
14
15 // Size and allocate |buf|.
16 unsigned char *buf = NULL;
17 int buf_len = i2d(input, &buf);
18 if (buf == NULL || buf_len < 0) {
19 return NULL;
20 }
21
22 // |buf| needs to be converted to |const| to be passed in.
23 const unsigned char *temp_input = buf;
24 char *ret = d2i(NULL, &temp_input, buf_len);
25 OPENSSL_free(buf);
26 return ret;
27 }
28
29 // ASN1_ITEM version of dup: this follows the model above except we don't
30 // need to allocate the buffer. At some point this could be rewritten to
31 // directly dup the underlying structure instead of doing and encode and
32 // decode.
33 void *ASN1_item_dup(const ASN1_ITEM *it, void *x) {
34 unsigned char *b = NULL;
35 const unsigned char *p;
36 long i;
37 void *ret;
38
39 if (x == NULL) {
40 return NULL;
41 }
42
43 i = ASN1_item_i2d(x, &b, it);
44 if (b == NULL) {
45 return NULL;
46 }
47 p = b;
48 ret = ASN1_item_d2i(NULL, &p, i, it);
49 OPENSSL_free(b);
50 return ret;
51 }