2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <openssl/ebcdic.h>
13 #include <openssl/err.h>
14 #include <openssl/params.h>
17 * When processing text to params, we're trying to be smart with numbers.
18 * Instead of handling each specific separate integer type, we use a bignum
19 * and ensure that it isn't larger than the expected size, and we then make
20 * sure it is the expected size... if there is one given.
21 * (if the size can be arbitrary, then we give whatever we have)
24 static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
25 const char *value, size_t value_n,
26 /* Output parameters */
27 const OSSL_PARAM **paramdef, int *ishex,
28 size_t *buf_n, BIGNUM **tmpbn, int *found)
34 * ishex is used to translate legacy style string controls in hex format
35 * to octet string parameters.
37 *ishex = strncmp(key, "hex", 3) == 0;
42 p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
48 switch (p->data_type) {
49 case OSSL_PARAM_INTEGER:
50 case OSSL_PARAM_UNSIGNED_INTEGER:
52 BN_hex2bn(tmpbn, value);
54 BN_asc2bn(tmpbn, value);
60 * 2s complement negate, part 1
62 * BN_bn2nativepad puts the absolute value of the number in the
63 * buffer, i.e. if it's negative, we need to deal with it. We do
64 * it by subtracting 1 here and inverting the bytes in
65 * construct_from_text() below.
66 * To subtract 1 from an absolute value of a negative number we
67 * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4.
69 if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
70 && !BN_add_word(*tmpbn, 1)) {
74 buf_bits = (size_t)BN_num_bits(*tmpbn);
75 *buf_n = (buf_bits + 7) / 8;
78 * TODO(v3.0) is this the right way to do this? This code expects
79 * a zero data size to simply mean "arbitrary size".
81 if (p->data_size > 0) {
82 if (buf_bits > p->data_size * 8
83 || (p->data_type == OSSL_PARAM_INTEGER
84 && buf_bits == p->data_size * 8)) {
85 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
86 /* Since this is a different error, we don't break */
89 /* Change actual size to become the desired size. */
90 *buf_n = p->data_size;
93 case OSSL_PARAM_UTF8_STRING:
95 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
98 *buf_n = strlen(value) + 1;
100 case OSSL_PARAM_OCTET_STRING:
102 *buf_n = strlen(value) >> 1;
112 static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
113 const char *value, size_t value_n, int ishex,
114 void *buf, size_t buf_n, BIGNUM *tmpbn)
120 switch (paramdef->data_type) {
121 case OSSL_PARAM_INTEGER:
122 case OSSL_PARAM_UNSIGNED_INTEGER:
125 if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
131 BN_bn2nativepad(tmpbn, buf, buf_n);
134 * 2s complement negate, part two.
136 * Because we did the first part on the BIGNUM itself, we can just
137 * invert all the bytes here and be done with it.
139 if (paramdef->data_type == OSSL_PARAM_INTEGER
140 && BN_is_negative(tmpbn)) {
144 for (cp = buf; i-- > 0; cp++)
148 case OSSL_PARAM_UTF8_STRING:
149 #ifdef CHARSET_EBCDIC
150 ebcdic2ascii(buf, value, buf_n);
152 strncpy(buf, value, buf_n);
155 case OSSL_PARAM_OCTET_STRING:
159 if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
162 memcpy(buf, value, buf_n);
170 to->data_size = buf_n;
171 to->return_size = OSSL_PARAM_UNMODIFIED;
176 int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
177 const OSSL_PARAM *paramdefs,
178 const char *key, const char *value,
179 size_t value_n, int *found)
181 const OSSL_PARAM *paramdef = NULL;
185 BIGNUM *tmpbn = NULL;
188 if (to == NULL || paramdefs == NULL)
191 if (!prepare_from_text(paramdefs, key, value, value_n,
192 ¶mdef, &ishex, &buf_n, &tmpbn, found))
195 if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
196 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
200 ok = construct_from_text(to, paramdef, value, value_n, ishex,