/src/openssl31/crypto/asn1/asn_pack.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* ASN1 packing and unpacking functions */ |
15 | | |
16 | | ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) |
17 | 2 | { |
18 | 2 | ASN1_STRING *octmp; |
19 | | |
20 | 2 | if (oct == NULL || *oct == NULL) { |
21 | 2 | if ((octmp = ASN1_STRING_new()) == NULL) { |
22 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
23 | 0 | return NULL; |
24 | 0 | } |
25 | 2 | } else { |
26 | 0 | octmp = *oct; |
27 | 0 | } |
28 | | |
29 | 2 | OPENSSL_free(octmp->data); |
30 | 2 | octmp->data = NULL; |
31 | | |
32 | 2 | if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) { |
33 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR); |
34 | 0 | goto err; |
35 | 0 | } |
36 | 2 | if (octmp->data == NULL) { |
37 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
38 | 0 | goto err; |
39 | 0 | } |
40 | | |
41 | 2 | if (oct != NULL && *oct == NULL) |
42 | 2 | *oct = octmp; |
43 | | |
44 | 2 | return octmp; |
45 | 0 | err: |
46 | 0 | if (oct == NULL || *oct == NULL) |
47 | 0 | ASN1_STRING_free(octmp); |
48 | 0 | return NULL; |
49 | 2 | } |
50 | | |
51 | | /* Extract an ASN1 object from an ASN1_STRING */ |
52 | | |
53 | | void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it) |
54 | 30.6k | { |
55 | 30.6k | const unsigned char *p; |
56 | 30.6k | void *ret; |
57 | | |
58 | 30.6k | p = oct->data; |
59 | 30.6k | if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL) |
60 | 30.6k | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
61 | 30.6k | return ret; |
62 | 30.6k | } |