Coverage Report

Created: 2024-01-12 07:13

/src/openssl/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
0
{
18
0
    ASN1_STRING *octmp;
19
20
0
    if (oct == NULL || *oct == NULL) {
21
0
        if ((octmp = ASN1_STRING_new()) == NULL) {
22
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
23
0
            return NULL;
24
0
        }
25
0
    } else {
26
0
        octmp = *oct;
27
0
    }
28
29
0
    ASN1_STRING_set0(octmp, NULL, 0);
30
31
0
    if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) <= 0) {
32
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);
33
0
        goto err;
34
0
    }
35
0
    if (octmp->data == NULL) {
36
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
37
0
        goto err;
38
0
    }
39
40
0
    if (oct != NULL && *oct == NULL)
41
0
        *oct = octmp;
42
43
0
    return octmp;
44
0
 err:
45
0
    if (oct == NULL || *oct == NULL)
46
0
        ASN1_STRING_free(octmp);
47
0
    return NULL;
48
0
}
49
50
/* Extract an ASN1 object from an ASN1_STRING */
51
52
void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
53
739
{
54
739
    const unsigned char *p;
55
739
    void *ret;
56
57
739
    p = oct->data;
58
739
    if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL)
59
739
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
60
739
    return ret;
61
739
}
62
63
void *ASN1_item_unpack_ex(const ASN1_STRING *oct, const ASN1_ITEM *it,
64
                          OSSL_LIB_CTX *libctx, const char *propq)
65
0
{
66
0
    const unsigned char *p;
67
0
    void *ret;
68
69
0
    p = oct->data;
70
0
    if ((ret = ASN1_item_d2i_ex(NULL, &p, oct->length, it,\
71
0
                                libctx, propq)) == NULL)
72
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
73
0
    return ret;
74
0
}