Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/encode_decode/endecoder_common.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2025 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 <openssl/core.h>
11
#include <openssl/buffer.h>
12
#include "internal/asn1.h"
13
#include "prov/bio.h"
14
#include "prov/endecoder_local.h"
15
16
OSSL_FUNC_keymgmt_new_fn *
17
ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
18
0
{
19
    /* Pilfer the keymgmt dispatch table */
20
0
    for (; fns->function_id != 0; fns++)
21
0
        if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
22
0
            return OSSL_FUNC_keymgmt_new(fns);
23
24
0
    return NULL;
25
0
}
26
27
OSSL_FUNC_keymgmt_free_fn *
28
ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
29
0
{
30
    /* Pilfer the keymgmt dispatch table */
31
0
    for (; fns->function_id != 0; fns++)
32
0
        if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
33
0
            return OSSL_FUNC_keymgmt_free(fns);
34
35
0
    return NULL;
36
0
}
37
38
OSSL_FUNC_keymgmt_import_fn *
39
ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
40
0
{
41
    /* Pilfer the keymgmt dispatch table */
42
0
    for (; fns->function_id != 0; fns++)
43
0
        if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
44
0
            return OSSL_FUNC_keymgmt_import(fns);
45
46
0
    return NULL;
47
0
}
48
49
OSSL_FUNC_keymgmt_export_fn *
50
ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
51
0
{
52
    /* Pilfer the keymgmt dispatch table */
53
0
    for (; fns->function_id != 0; fns++)
54
0
        if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
55
0
            return OSSL_FUNC_keymgmt_export(fns);
56
57
0
    return NULL;
58
0
}
59
60
void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
61
    int selection, const OSSL_PARAM params[])
62
0
{
63
0
    OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns);
64
0
    OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
65
0
    OSSL_FUNC_keymgmt_import_fn *kmgmt_import = ossl_prov_get_keymgmt_import(fns);
66
0
    void *key = NULL;
67
68
0
    if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
69
0
        if ((key = kmgmt_new(provctx)) == NULL
70
0
            || !kmgmt_import(key, selection, params)) {
71
0
            kmgmt_free(key);
72
0
            key = NULL;
73
0
        }
74
0
    }
75
0
    return key;
76
0
}
77
78
void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key)
79
0
{
80
0
    OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
81
82
0
    if (kmgmt_free != NULL)
83
0
        kmgmt_free(key);
84
0
}
85
86
int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data,
87
    long *len)
88
0
{
89
0
    BUF_MEM *mem = NULL;
90
0
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
91
0
    int ok;
92
93
0
    if (in == NULL)
94
0
        return 0;
95
0
    ok = (asn1_d2i_read_bio(in, &mem) >= 0);
96
0
    if (ok) {
97
0
        *data = (unsigned char *)mem->data;
98
0
        *len = (long)mem->length;
99
0
        OPENSSL_free(mem);
100
0
    }
101
0
    BIO_free(in);
102
0
    return ok;
103
0
}