Coverage Report

Created: 2024-07-27 06:39

/src/openssl30/crypto/param_build_set.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-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
/*
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
1.76M
{
25
1.76M
    if (bld != NULL)
26
227k
        return OSSL_PARAM_BLD_push_int(bld, key, num);
27
1.54M
    p = OSSL_PARAM_locate(p, key);
28
1.54M
    if (p != NULL)
29
2
        return OSSL_PARAM_set_int(p, num);
30
1.54M
    return 1;
31
1.54M
}
32
33
int ossl_param_build_set_long(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
34
                              const char *key, long num)
35
3.50k
{
36
3.50k
    if (bld != NULL)
37
0
        return OSSL_PARAM_BLD_push_long(bld, key, num);
38
3.50k
    p = OSSL_PARAM_locate(p, key);
39
3.50k
    if (p != NULL)
40
0
        return OSSL_PARAM_set_long(p, num);
41
3.50k
    return 1;
42
3.50k
}
43
44
int ossl_param_build_set_utf8_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
45
                                     const char *key, const char *buf)
46
1.59M
{
47
1.59M
    if (bld != NULL)
48
201k
        return OSSL_PARAM_BLD_push_utf8_string(bld, key, buf, 0);
49
1.38M
    p = OSSL_PARAM_locate(p, key);
50
1.38M
    if (p != NULL)
51
41.9k
        return OSSL_PARAM_set_utf8_string(p, buf);
52
1.34M
    return 1;
53
1.38M
}
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
381k
{
60
381k
    if (bld != NULL)
61
25.2k
        return OSSL_PARAM_BLD_push_octet_string(bld, key, data, data_len);
62
63
356k
    p = OSSL_PARAM_locate(p, key);
64
356k
    if (p != NULL)
65
0
        return OSSL_PARAM_set_octet_string(p, data, data_len);
66
356k
    return 1;
67
356k
}
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
67.2k
{
72
67.2k
    if (bld != NULL)
73
25.2k
        return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, sz);
74
41.9k
    p = OSSL_PARAM_locate(p, key);
75
41.9k
    if (p != NULL) {
76
0
        if (sz > p->data_size)
77
0
            return 0;
78
0
        p->data_size = sz;
79
0
        return OSSL_PARAM_set_BN(p, bn);
80
0
    }
81
41.9k
    return 1;
82
41.9k
}
83
84
int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
85
                            const char *key, const BIGNUM *bn)
86
878k
{
87
878k
    if (bld != NULL)
88
215k
        return OSSL_PARAM_BLD_push_BN(bld, key, bn);
89
90
663k
    p = OSSL_PARAM_locate(p, key);
91
663k
    if (p != NULL)
92
0
        return OSSL_PARAM_set_BN(p, bn) > 0;
93
663k
    return 1;
94
663k
}
95
96
int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
97
                                      const char *names[],
98
                                      STACK_OF(BIGNUM_const) *stk)
99
112k
{
100
112k
    int i, sz = sk_BIGNUM_const_num(stk);
101
112k
    OSSL_PARAM *p;
102
112k
    const BIGNUM *bn;
103
104
112k
    if (bld != NULL) {
105
238k
        for (i = 0; i < sz && names[i] != NULL; ++i) {
106
149k
            bn = sk_BIGNUM_const_value(stk, i);
107
149k
            if (bn != NULL && !OSSL_PARAM_BLD_push_BN(bld, names[i], bn))
108
0
                return 0;
109
149k
        }
110
89.4k
        return 1;
111
89.4k
    }
112
113
85.5k
    for (i = 0; i < sz && names[i] != NULL; ++i) {
114
62.4k
        bn = sk_BIGNUM_const_value(stk, i);
115
62.4k
        p = OSSL_PARAM_locate(params, names[i]);
116
62.4k
        if (p != NULL && bn != NULL) {
117
0
            if (!OSSL_PARAM_set_BN(p, bn))
118
0
                return 0;
119
0
        }
120
62.4k
    }
121
23.1k
    return 1;
122
23.1k
}