/src/openssl/crypto/asn1/asn_pack.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2023 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/asn1.h> |
13 | | |
14 | | #include <crypto/asn1.h> |
15 | | |
16 | | /* ASN1 packing and unpacking functions */ |
17 | | |
18 | | ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) |
19 | 0 | { |
20 | 0 | ASN1_STRING *octmp; |
21 | |
|
22 | 0 | if (oct == NULL || *oct == NULL) { |
23 | 0 | if ((octmp = ASN1_STRING_new()) == NULL) { |
24 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
25 | 0 | return NULL; |
26 | 0 | } |
27 | 0 | } else { |
28 | 0 | octmp = *oct; |
29 | 0 | } |
30 | | |
31 | 0 | ASN1_STRING_set0(octmp, NULL, 0); |
32 | |
|
33 | 0 | if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) { |
34 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR); |
35 | 0 | goto err; |
36 | 0 | } |
37 | 0 | if (octmp->data == NULL) { |
38 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
39 | 0 | goto err; |
40 | 0 | } |
41 | | |
42 | 0 | if (oct != NULL && *oct == NULL) |
43 | 0 | *oct = octmp; |
44 | |
|
45 | 0 | return octmp; |
46 | 0 | err: |
47 | 0 | if (oct == NULL || *oct == NULL) |
48 | 0 | ASN1_STRING_free(octmp); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | | /* Extract an ASN1 object from an ASN1_STRING */ |
53 | | |
54 | | void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it) |
55 | 0 | { |
56 | 0 | const unsigned char *p; |
57 | 0 | void *ret; |
58 | |
|
59 | 0 | p = oct->data; |
60 | 0 | if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL) |
61 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
62 | 0 | return ret; |
63 | 0 | } |
64 | | |
65 | | void *ASN1_item_unpack_ex(const ASN1_STRING *oct, const ASN1_ITEM *it, |
66 | | OSSL_LIB_CTX *libctx, const char *propq) |
67 | 0 | { |
68 | 0 | const unsigned char *p; |
69 | 0 | void *ret; |
70 | |
|
71 | 0 | p = oct->data; |
72 | 0 | if ((ret = ASN1_item_d2i_ex(NULL, &p, oct->length, it, |
73 | 0 | libctx, propq)) |
74 | 0 | == NULL) |
75 | | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
76 | 0 | return ret; |
77 | 0 | } |