Shane Lontis | 96ebe52 | 2020-04-01 15:51:18 +1000 | [diff] [blame] | 1 | /* |
Matt Caswell | 8020d79 | 2021-03-11 13:27:36 +0000 | [diff] [blame] | 2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. |
Shane Lontis | 96ebe52 | 2020-04-01 15:51:18 +1000 | [diff] [blame] | 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 | { |
| 25 | if (bld != NULL) |
| 26 | return OSSL_PARAM_BLD_push_int(bld, key, num); |
| 27 | p = OSSL_PARAM_locate(p, key); |
| 28 | if (p != NULL) |
| 29 | return OSSL_PARAM_set_int(p, num); |
| 30 | return 1; |
| 31 | } |
| 32 | |
Richard Levitte | 0ba71d6 | 2020-10-15 07:10:29 +0200 | [diff] [blame] | 33 | int ossl_param_build_set_long(OSSL_PARAM_BLD *bld, OSSL_PARAM *p, |
| 34 | const char *key, long num) |
| 35 | { |
| 36 | if (bld != NULL) |
| 37 | return OSSL_PARAM_BLD_push_long(bld, key, num); |
| 38 | p = OSSL_PARAM_locate(p, key); |
| 39 | if (p != NULL) |
| 40 | return OSSL_PARAM_set_long(p, num); |
| 41 | return 1; |
| 42 | } |
| 43 | |
Shane Lontis | 96ebe52 | 2020-04-01 15:51:18 +1000 | [diff] [blame] | 44 | int ossl_param_build_set_utf8_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p, |
| 45 | const char *key, const char *buf) |
| 46 | { |
| 47 | if (bld != NULL) |
| 48 | return OSSL_PARAM_BLD_push_utf8_string(bld, key, buf, 0); |
| 49 | p = OSSL_PARAM_locate(p, key); |
| 50 | if (p != NULL) |
| 51 | return OSSL_PARAM_set_utf8_string(p, buf); |
| 52 | return 1; |
| 53 | } |
| 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 | { |
| 60 | if (bld != NULL) |
| 61 | return OSSL_PARAM_BLD_push_octet_string(bld, key, data, data_len); |
| 62 | |
| 63 | p = OSSL_PARAM_locate(p, key); |
| 64 | if (p != NULL) |
| 65 | return OSSL_PARAM_set_octet_string(p, data, data_len); |
| 66 | return 1; |
| 67 | } |
| 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 | { |
| 72 | if (bld != NULL) |
| 73 | return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, sz); |
| 74 | p = OSSL_PARAM_locate(p, key); |
| 75 | if (p != NULL) { |
Pauli | 3ee3a2b | 2022-01-10 11:33:06 +1100 | [diff] [blame] | 76 | if (sz > p->data_size) { |
| 77 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
Shane Lontis | 96ebe52 | 2020-04-01 15:51:18 +1000 | [diff] [blame] | 78 | return 0; |
Pauli | 3ee3a2b | 2022-01-10 11:33:06 +1100 | [diff] [blame] | 79 | } |
Shane Lontis | 96ebe52 | 2020-04-01 15:51:18 +1000 | [diff] [blame] | 80 | p->data_size = sz; |
| 81 | return OSSL_PARAM_set_BN(p, bn); |
| 82 | } |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p, |
| 87 | const char *key, const BIGNUM *bn) |
| 88 | { |
| 89 | if (bld != NULL) |
| 90 | return OSSL_PARAM_BLD_push_BN(bld, key, bn); |
| 91 | |
| 92 | p = OSSL_PARAM_locate(p, key); |
| 93 | if (p != NULL) |
| 94 | return OSSL_PARAM_set_BN(p, bn) > 0; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params, |
| 99 | const char *names[], |
| 100 | STACK_OF(BIGNUM_const) *stk) |
| 101 | { |
| 102 | int i, sz = sk_BIGNUM_const_num(stk); |
| 103 | OSSL_PARAM *p; |
| 104 | |
| 105 | |
| 106 | if (bld != NULL) { |
| 107 | for (i = 0; i < sz && names[i] != NULL; ++i) { |
| 108 | if (!OSSL_PARAM_BLD_push_BN(bld, names[i], |
| 109 | sk_BIGNUM_const_value(stk, i))) |
| 110 | return 0; |
| 111 | } |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | for (i = 0; i < sz && names[i] != NULL; ++i) { |
| 116 | p = OSSL_PARAM_locate(params, names[i]); |
| 117 | if (p != NULL) { |
| 118 | if (!OSSL_PARAM_set_BN(p, sk_BIGNUM_const_value(stk, i))) |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | return 1; |
| 123 | } |