2 * Copyright 2019 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/err.h>
13 #include <openssl/params.h>
16 * When processing text to params, we're trying to be smart with numbers.
17 * Instead of handling each specific separate integer type, we use a bignum
18 * and ensure that it isn't larger than the expected size, and we then make
19 * sure it is the expected size... if there is one given.
20 * (if the size can be arbitrary, then we give whatever we have)
23 static int prepare_from_text(const OSSL_PARAM
*paramdefs
, const char *key
,
24 const char *value
, size_t value_n
,
25 /* Output parameters */
26 const OSSL_PARAM
**paramdef
, int *ishex
,
27 size_t *buf_n
, BIGNUM
**tmpbn
)
32 * ishex is used to translate legacy style string controls in hex format
33 * to octet string parameters.
35 *ishex
= strncmp(key
, "hex", 3) == 0;
40 p
= *paramdef
= OSSL_PARAM_locate_const(paramdefs
, key
);
44 switch (p
->data_type
) {
45 case OSSL_PARAM_INTEGER
:
46 case OSSL_PARAM_UNSIGNED_INTEGER
:
48 BN_hex2bn(tmpbn
, value
);
50 BN_dec2bn(tmpbn
, value
);
56 * 2s complement negate, part 1
58 * BN_bn2nativepad puts the absolute value of the number in the
59 * buffer, i.e. if it's negative, we need to deal with it. We do
60 * it by subtracting 1 here and inverting the bytes in
61 * construct_from_text() below.
63 if (p
->data_type
== OSSL_PARAM_INTEGER
&& BN_is_negative(*tmpbn
)
64 && !BN_sub_word(*tmpbn
, 1)) {
68 *buf_n
= BN_num_bytes(*tmpbn
);
71 * TODO(v3.0) is this the right way to do this? This code expects
72 * a zero data size to simply mean "arbitrary size".
74 if (p
->data_size
> 0) {
75 if (*buf_n
>= p
->data_size
) {
76 CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER
);
77 /* Since this is a different error, we don't break */
80 /* Change actual size to become the desired size. */
81 *buf_n
= p
->data_size
;
84 case OSSL_PARAM_UTF8_STRING
:
86 CRYPTOerr(0, ERR_R_PASSED_INVALID_ARGUMENT
);
89 *buf_n
= strlen(value
) + 1;
91 case OSSL_PARAM_OCTET_STRING
:
93 *buf_n
= strlen(value
) >> 1;
103 static int construct_from_text(OSSL_PARAM
*to
, const OSSL_PARAM
*paramdef
,
104 const char *value
, size_t value_n
, int ishex
,
105 void *buf
, size_t buf_n
, BIGNUM
*tmpbn
)
110 switch (paramdef
->data_type
) {
111 case OSSL_PARAM_INTEGER
:
112 case OSSL_PARAM_UNSIGNED_INTEGER
:
115 if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
121 BN_bn2nativepad(tmpbn
, buf
, buf_n
);
124 * 2s complement negate, part two.
126 * Because we did the first part on the BIGNUM itself, we can just
127 * invert all the bytes here and be done with it.
129 if (paramdef
->data_type
== OSSL_PARAM_INTEGER
130 && BN_is_negative(tmpbn
)) {
134 for (cp
= buf
; i
-- > 0; cp
++)
138 case OSSL_PARAM_UTF8_STRING
:
139 strncpy(buf
, value
, buf_n
);
141 case OSSL_PARAM_OCTET_STRING
:
145 if (!OPENSSL_hexstr2buf_ex(buf
, buf_n
, &l
, value
))
148 memcpy(buf
, value
, buf_n
);
156 to
->data_size
= buf_n
;
162 int OSSL_PARAM_construct_from_text(OSSL_PARAM
*to
,
163 const OSSL_PARAM
*paramdefs
,
164 const char *key
, const char *value
,
166 void *buf
, size_t *buf_n
)
168 const OSSL_PARAM
*paramdef
= NULL
;
170 BIGNUM
*tmpbn
= NULL
;
173 if (to
== NULL
|| paramdefs
== NULL
)
176 if (!prepare_from_text(paramdefs
, key
, value
, value_n
,
177 ¶mdef
, &ishex
, buf_n
, &tmpbn
))
181 * The user gets the expected buffer size back even if the buffer isn't
187 ok
= construct_from_text(to
, paramdef
, value
, value_n
, ishex
,
193 int OSSL_PARAM_allocate_from_text(OSSL_PARAM
*to
,
194 const OSSL_PARAM
*paramdefs
,
195 const char *key
, const char *value
,
198 const OSSL_PARAM
*paramdef
= NULL
;
202 BIGNUM
*tmpbn
= NULL
;
205 if (to
== NULL
|| paramdefs
== NULL
)
208 if (!prepare_from_text(paramdefs
, key
, value
, value_n
,
209 ¶mdef
, &ishex
, &buf_n
, &tmpbn
))
212 if ((buf
= OPENSSL_malloc(buf_n
)) == NULL
) {
213 CRYPTOerr(0, ERR_R_MALLOC_FAILURE
);
217 ok
= construct_from_text(to
, paramdef
, value
, value_n
, ishex
,