Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/param_build_set.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2026 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
/*
11
 * Key Management utility functions to share functionality between the export()
12
 * and get_params() methods.
13
 * export() uses OSSL_PARAM_BLD, and get_params() used the OSSL_PARAM[] to
14
 * fill in parameter data for the same key and data fields.
15
 */
16
17
#include <openssl/core_names.h>
18
#include "internal/param_build_set.h"
19
20
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
21
22
int ossl_param_build_set_int(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
23
    const char *key, int num)
24
3.27M
{
25
3.27M
    if (bld != NULL)
26
369k
        return OSSL_PARAM_BLD_push_int(bld, key, num);
27
2.90M
    p = OSSL_PARAM_locate(p, key);
28
2.90M
    if (p != NULL)
29
4
        return OSSL_PARAM_set_int(p, num);
30
2.90M
    return 1;
31
2.90M
}
32
33
int ossl_param_build_set_long(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
34
    const char *key, long num)
35
2.47k
{
36
2.47k
    if (bld != NULL)
37
0
        return OSSL_PARAM_BLD_push_long(bld, key, num);
38
2.47k
    p = OSSL_PARAM_locate(p, key);
39
2.47k
    if (p != NULL)
40
0
        return OSSL_PARAM_set_long(p, num);
41
2.47k
    return 1;
42
2.47k
}
43
44
int ossl_param_build_set_utf8_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
45
    const char *key, const char *buf)
46
2.80M
{
47
2.80M
    if (bld != NULL)
48
328k
        return OSSL_PARAM_BLD_push_utf8_string(bld, key, buf, 0);
49
2.48M
    p = OSSL_PARAM_locate(p, key);
50
2.48M
    if (p != NULL)
51
73.7k
        return OSSL_PARAM_set_utf8_string(p, buf);
52
2.40M
    return 1;
53
2.48M
}
54
55
int ossl_param_build_set_octet_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
56
    const char *key,
57
    const unsigned char *data,
58
    size_t data_len)
59
887k
{
60
887k
    if (bld != NULL)
61
137k
        return OSSL_PARAM_BLD_push_octet_string(bld, key, data, data_len);
62
63
749k
    p = OSSL_PARAM_locate(p, key);
64
749k
    if (p != NULL)
65
0
        return OSSL_PARAM_set_octet_string(p, data, data_len);
66
749k
    return 1;
67
749k
}
68
69
int ossl_param_build_set_bn_pad(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
70
    const char *key, const BIGNUM *bn, size_t sz)
71
104k
{
72
104k
    if (bld != NULL)
73
31.2k
        return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, sz);
74
73.0k
    p = OSSL_PARAM_locate(p, key);
75
73.0k
    if (p != NULL) {
76
        /* Size probe: NULL data means "report the required size". */
77
0
        if (p->data == NULL) {
78
0
            p->return_size = sz;
79
0
            return 1;
80
0
        }
81
0
        if (sz > p->data_size) {
82
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
83
0
            return 0;
84
0
        }
85
0
        p->data_size = sz;
86
0
        return OSSL_PARAM_set_BN(p, bn);
87
0
    }
88
73.0k
    return 1;
89
73.0k
}
90
91
int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
92
    const char *key, const BIGNUM *bn)
93
1.66M
{
94
1.66M
    if (bld != NULL)
95
379k
        return OSSL_PARAM_BLD_push_BN(bld, key, bn);
96
97
1.28M
    p = OSSL_PARAM_locate(p, key);
98
1.28M
    if (p != NULL)
99
345
        return OSSL_PARAM_set_BN(p, bn) > 0;
100
1.28M
    return 1;
101
1.28M
}
102
103
int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
104
    const char *names[],
105
    STACK_OF(BIGNUM_const) *stk)
106
251k
{
107
251k
    int i, sz = sk_BIGNUM_const_num(stk);
108
251k
    OSSL_PARAM *p;
109
251k
    const BIGNUM *bn;
110
111
251k
    if (bld != NULL) {
112
464k
        for (i = 0; i < sz && names[i] != NULL; ++i) {
113
290k
            bn = sk_BIGNUM_const_value(stk, i);
114
290k
            if (bn != NULL && !OSSL_PARAM_BLD_push_BN(bld, names[i], bn))
115
0
                return 0;
116
290k
        }
117
174k
        return 1;
118
174k
    }
119
120
229k
    for (i = 0; i < sz && names[i] != NULL; ++i) {
121
152k
        bn = sk_BIGNUM_const_value(stk, i);
122
152k
        p = OSSL_PARAM_locate(params, names[i]);
123
152k
        if (p != NULL && bn != NULL) {
124
0
            if (!OSSL_PARAM_set_BN(p, bn))
125
0
                return 0;
126
0
        }
127
152k
    }
128
77.5k
    return 1;
129
77.5k
}