1b077aed3SPierre Pronchery /*
2*ad991e4cSEd Maste  * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * Key Management utility functions to share functionality between the export()
12b077aed3SPierre Pronchery  * and get_params() methods.
13b077aed3SPierre Pronchery  * export() uses OSSL_PARAM_BLD, and get_params() used the OSSL_PARAM[] to
14b077aed3SPierre Pronchery  * fill in parameter data for the same key and data fields.
15b077aed3SPierre Pronchery  */
16b077aed3SPierre Pronchery 
17b077aed3SPierre Pronchery #include <openssl/core_names.h>
18b077aed3SPierre Pronchery #include "internal/param_build_set.h"
19b077aed3SPierre Pronchery 
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)20b077aed3SPierre Pronchery DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
21b077aed3SPierre Pronchery 
22b077aed3SPierre Pronchery int ossl_param_build_set_int(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
23b077aed3SPierre Pronchery                              const char *key, int num)
24b077aed3SPierre Pronchery {
25b077aed3SPierre Pronchery     if (bld != NULL)
26b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_int(bld, key, num);
27b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
28b077aed3SPierre Pronchery     if (p != NULL)
29b077aed3SPierre Pronchery         return OSSL_PARAM_set_int(p, num);
30b077aed3SPierre Pronchery     return 1;
31b077aed3SPierre Pronchery }
32b077aed3SPierre Pronchery 
ossl_param_build_set_long(OSSL_PARAM_BLD * bld,OSSL_PARAM * p,const char * key,long num)33b077aed3SPierre Pronchery int ossl_param_build_set_long(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
34b077aed3SPierre Pronchery                               const char *key, long num)
35b077aed3SPierre Pronchery {
36b077aed3SPierre Pronchery     if (bld != NULL)
37b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_long(bld, key, num);
38b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
39b077aed3SPierre Pronchery     if (p != NULL)
40b077aed3SPierre Pronchery         return OSSL_PARAM_set_long(p, num);
41b077aed3SPierre Pronchery     return 1;
42b077aed3SPierre Pronchery }
43b077aed3SPierre Pronchery 
ossl_param_build_set_utf8_string(OSSL_PARAM_BLD * bld,OSSL_PARAM * p,const char * key,const char * buf)44b077aed3SPierre Pronchery int ossl_param_build_set_utf8_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
45b077aed3SPierre Pronchery                                      const char *key, const char *buf)
46b077aed3SPierre Pronchery {
47b077aed3SPierre Pronchery     if (bld != NULL)
48b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_utf8_string(bld, key, buf, 0);
49b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
50b077aed3SPierre Pronchery     if (p != NULL)
51b077aed3SPierre Pronchery         return OSSL_PARAM_set_utf8_string(p, buf);
52b077aed3SPierre Pronchery     return 1;
53b077aed3SPierre Pronchery }
54b077aed3SPierre Pronchery 
ossl_param_build_set_octet_string(OSSL_PARAM_BLD * bld,OSSL_PARAM * p,const char * key,const unsigned char * data,size_t data_len)55b077aed3SPierre Pronchery int ossl_param_build_set_octet_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
56b077aed3SPierre Pronchery                                       const char *key,
57b077aed3SPierre Pronchery                                       const unsigned char *data,
58b077aed3SPierre Pronchery                                       size_t data_len)
59b077aed3SPierre Pronchery {
60b077aed3SPierre Pronchery     if (bld != NULL)
61b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_octet_string(bld, key, data, data_len);
62b077aed3SPierre Pronchery 
63b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
64b077aed3SPierre Pronchery     if (p != NULL)
65b077aed3SPierre Pronchery         return OSSL_PARAM_set_octet_string(p, data, data_len);
66b077aed3SPierre Pronchery     return 1;
67b077aed3SPierre Pronchery }
68b077aed3SPierre Pronchery 
ossl_param_build_set_bn_pad(OSSL_PARAM_BLD * bld,OSSL_PARAM * p,const char * key,const BIGNUM * bn,size_t sz)69b077aed3SPierre Pronchery int ossl_param_build_set_bn_pad(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
70b077aed3SPierre Pronchery                                 const char *key, const BIGNUM *bn,  size_t sz)
71b077aed3SPierre Pronchery {
72b077aed3SPierre Pronchery     if (bld != NULL)
73b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, sz);
74b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
75b077aed3SPierre Pronchery     if (p != NULL) {
76b077aed3SPierre Pronchery         if (sz > p->data_size)
77b077aed3SPierre Pronchery             return 0;
78b077aed3SPierre Pronchery         p->data_size = sz;
79b077aed3SPierre Pronchery         return OSSL_PARAM_set_BN(p, bn);
80b077aed3SPierre Pronchery     }
81b077aed3SPierre Pronchery     return 1;
82b077aed3SPierre Pronchery }
83b077aed3SPierre Pronchery 
ossl_param_build_set_bn(OSSL_PARAM_BLD * bld,OSSL_PARAM * p,const char * key,const BIGNUM * bn)84b077aed3SPierre Pronchery int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
85b077aed3SPierre Pronchery                             const char *key, const BIGNUM *bn)
86b077aed3SPierre Pronchery {
87b077aed3SPierre Pronchery     if (bld != NULL)
88b077aed3SPierre Pronchery         return OSSL_PARAM_BLD_push_BN(bld, key, bn);
89b077aed3SPierre Pronchery 
90b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(p, key);
91b077aed3SPierre Pronchery     if (p != NULL)
92b077aed3SPierre Pronchery         return OSSL_PARAM_set_BN(p, bn) > 0;
93b077aed3SPierre Pronchery     return 1;
94b077aed3SPierre Pronchery }
95b077aed3SPierre Pronchery 
ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD * bld,OSSL_PARAM * params,const char * names[],STACK_OF (BIGNUM_const)* stk)96b077aed3SPierre Pronchery int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
97b077aed3SPierre Pronchery                                       const char *names[],
98b077aed3SPierre Pronchery                                       STACK_OF(BIGNUM_const) *stk)
99b077aed3SPierre Pronchery {
100b077aed3SPierre Pronchery     int i, sz = sk_BIGNUM_const_num(stk);
101b077aed3SPierre Pronchery     OSSL_PARAM *p;
102*ad991e4cSEd Maste     const BIGNUM *bn;
103b077aed3SPierre Pronchery 
104b077aed3SPierre Pronchery     if (bld != NULL) {
105b077aed3SPierre Pronchery         for (i = 0; i < sz && names[i] != NULL; ++i) {
106*ad991e4cSEd Maste             bn = sk_BIGNUM_const_value(stk, i);
107*ad991e4cSEd Maste             if (bn != NULL && !OSSL_PARAM_BLD_push_BN(bld, names[i], bn))
108b077aed3SPierre Pronchery                 return 0;
109b077aed3SPierre Pronchery         }
110b077aed3SPierre Pronchery         return 1;
111b077aed3SPierre Pronchery     }
112b077aed3SPierre Pronchery 
113b077aed3SPierre Pronchery     for (i = 0; i < sz && names[i] != NULL; ++i) {
114*ad991e4cSEd Maste         bn = sk_BIGNUM_const_value(stk, i);
115b077aed3SPierre Pronchery         p = OSSL_PARAM_locate(params, names[i]);
116*ad991e4cSEd Maste         if (p != NULL && bn != NULL) {
117*ad991e4cSEd Maste             if (!OSSL_PARAM_set_BN(p, bn))
118b077aed3SPierre Pronchery                 return 0;
119b077aed3SPierre Pronchery         }
120b077aed3SPierre Pronchery     }
121b077aed3SPierre Pronchery     return 1;
122b077aed3SPierre Pronchery }
123