/src/openssl32/crypto/params_from_text.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
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 |
9 | | */ |
10 | | |
11 | | #include "internal/common.h" /* for HAS_PREFIX */ |
12 | | #include <openssl/ebcdic.h> |
13 | | #include <openssl/err.h> |
14 | | #include <openssl/params.h> |
15 | | |
16 | | /* |
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) |
22 | | */ |
23 | | |
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) |
29 | 0 | { |
30 | 0 | const OSSL_PARAM *p; |
31 | 0 | size_t buf_bits; |
32 | 0 | int r; |
33 | | |
34 | | /* |
35 | | * ishex is used to translate legacy style string controls in hex format |
36 | | * to octet string parameters. |
37 | | */ |
38 | 0 | *ishex = CHECK_AND_SKIP_PREFIX(key, "hex"); |
39 | |
|
40 | 0 | p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key); |
41 | 0 | if (found != NULL) |
42 | 0 | *found = p != NULL; |
43 | 0 | if (p == NULL) |
44 | 0 | return 0; |
45 | | |
46 | 0 | switch (p->data_type) { |
47 | 0 | case OSSL_PARAM_INTEGER: |
48 | 0 | case OSSL_PARAM_UNSIGNED_INTEGER: |
49 | 0 | if (*ishex) |
50 | 0 | r = BN_hex2bn(tmpbn, value); |
51 | 0 | else |
52 | 0 | r = BN_asc2bn(tmpbn, value); |
53 | |
|
54 | 0 | if (r == 0 || *tmpbn == NULL) |
55 | 0 | return 0; |
56 | | |
57 | 0 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER |
58 | 0 | && BN_is_negative(*tmpbn)) { |
59 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INVALID_NEGATIVE_VALUE); |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | * 2's complement negate, part 1 |
65 | | * |
66 | | * BN_bn2nativepad puts the absolute value of the number in the |
67 | | * buffer, i.e. if it's negative, we need to deal with it. We do |
68 | | * it by subtracting 1 here and inverting the bytes in |
69 | | * construct_from_text() below. |
70 | | * To subtract 1 from an absolute value of a negative number we |
71 | | * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4. |
72 | | */ |
73 | 0 | if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn) |
74 | 0 | && !BN_add_word(*tmpbn, 1)) { |
75 | 0 | return 0; |
76 | 0 | } |
77 | | |
78 | 0 | buf_bits = (size_t)BN_num_bits(*tmpbn); |
79 | | |
80 | | /* |
81 | | * Compensate for cases where the most significant bit in |
82 | | * the resulting OSSL_PARAM buffer will be set after the |
83 | | * BN_bn2nativepad() call, as the implied sign may not be |
84 | | * correct after the second part of the 2's complement |
85 | | * negation has been performed. |
86 | | * We fix these cases by extending the buffer by one byte |
87 | | * (8 bits), which will give some padding. The second part |
88 | | * of the 2's complement negation will do the rest. |
89 | | */ |
90 | 0 | if (p->data_type == OSSL_PARAM_INTEGER && buf_bits % 8 == 0) |
91 | 0 | buf_bits += 8; |
92 | |
|
93 | 0 | *buf_n = (buf_bits + 7) / 8; |
94 | | |
95 | | /* |
96 | | * A zero data size means "arbitrary size", so only do the |
97 | | * range checking if a size is specified. |
98 | | */ |
99 | 0 | if (p->data_size > 0) { |
100 | 0 | if (buf_bits > p->data_size * 8) { |
101 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
102 | | /* Since this is a different error, we don't break */ |
103 | 0 | return 0; |
104 | 0 | } |
105 | | /* Change actual size to become the desired size. */ |
106 | 0 | *buf_n = p->data_size; |
107 | 0 | } |
108 | 0 | break; |
109 | 0 | case OSSL_PARAM_UTF8_STRING: |
110 | 0 | if (*ishex) { |
111 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
112 | 0 | return 0; |
113 | 0 | } |
114 | 0 | *buf_n = strlen(value) + 1; |
115 | 0 | break; |
116 | 0 | case OSSL_PARAM_OCTET_STRING: |
117 | 0 | if (*ishex) { |
118 | 0 | size_t hexdigits = strlen(value); |
119 | 0 | if ((hexdigits % 2) != 0) { |
120 | | /* We don't accept an odd number of hex digits */ |
121 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS); |
122 | 0 | return 0; |
123 | 0 | } |
124 | 0 | *buf_n = hexdigits >> 1; |
125 | 0 | } else { |
126 | 0 | *buf_n = value_n; |
127 | 0 | } |
128 | 0 | break; |
129 | 0 | } |
130 | | |
131 | 0 | return 1; |
132 | 0 | } |
133 | | |
134 | | static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef, |
135 | | const char *value, size_t value_n, int ishex, |
136 | | void *buf, size_t buf_n, BIGNUM *tmpbn) |
137 | 0 | { |
138 | 0 | if (buf == NULL) |
139 | 0 | return 0; |
140 | | |
141 | 0 | if (buf_n > 0) { |
142 | 0 | switch (paramdef->data_type) { |
143 | 0 | case OSSL_PARAM_INTEGER: |
144 | 0 | case OSSL_PARAM_UNSIGNED_INTEGER: |
145 | | /* |
146 | | { |
147 | | if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) { |
148 | | BN_free(a); |
149 | | break; |
150 | | } |
151 | | */ |
152 | |
|
153 | 0 | BN_bn2nativepad(tmpbn, buf, buf_n); |
154 | | |
155 | | /* |
156 | | * 2's complement negation, part two. |
157 | | * |
158 | | * Because we did the first part on the BIGNUM itself, we can just |
159 | | * invert all the bytes here and be done with it. |
160 | | */ |
161 | 0 | if (paramdef->data_type == OSSL_PARAM_INTEGER |
162 | 0 | && BN_is_negative(tmpbn)) { |
163 | 0 | unsigned char *cp; |
164 | 0 | size_t i = buf_n; |
165 | |
|
166 | 0 | for (cp = buf; i-- > 0; cp++) |
167 | 0 | *cp ^= 0xFF; |
168 | 0 | } |
169 | 0 | break; |
170 | 0 | case OSSL_PARAM_UTF8_STRING: |
171 | | #ifdef CHARSET_EBCDIC |
172 | | ebcdic2ascii(buf, value, buf_n); |
173 | | #else |
174 | 0 | strncpy(buf, value, buf_n); |
175 | 0 | #endif |
176 | | /* Don't count the terminating NUL byte as data */ |
177 | 0 | buf_n--; |
178 | 0 | break; |
179 | 0 | case OSSL_PARAM_OCTET_STRING: |
180 | 0 | if (ishex) { |
181 | 0 | size_t l = 0; |
182 | |
|
183 | 0 | if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':')) |
184 | 0 | return 0; |
185 | 0 | } else { |
186 | 0 | memcpy(buf, value, buf_n); |
187 | 0 | } |
188 | 0 | break; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | 0 | *to = *paramdef; |
193 | 0 | to->data = buf; |
194 | 0 | to->data_size = buf_n; |
195 | 0 | to->return_size = OSSL_PARAM_UNMODIFIED; |
196 | |
|
197 | 0 | return 1; |
198 | 0 | } |
199 | | |
200 | | int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to, |
201 | | const OSSL_PARAM *paramdefs, |
202 | | const char *key, const char *value, |
203 | | size_t value_n, int *found) |
204 | 0 | { |
205 | 0 | const OSSL_PARAM *paramdef = NULL; |
206 | 0 | int ishex = 0; |
207 | 0 | void *buf = NULL; |
208 | 0 | size_t buf_n = 0; |
209 | 0 | BIGNUM *tmpbn = NULL; |
210 | 0 | int ok = 0; |
211 | |
|
212 | 0 | if (to == NULL || paramdefs == NULL) |
213 | 0 | return 0; |
214 | | |
215 | 0 | if (!prepare_from_text(paramdefs, key, value, value_n, |
216 | 0 | ¶mdef, &ishex, &buf_n, &tmpbn, found)) |
217 | 0 | goto err; |
218 | | |
219 | 0 | if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) |
220 | 0 | goto err; |
221 | | |
222 | 0 | ok = construct_from_text(to, paramdef, value, value_n, ishex, |
223 | 0 | buf, buf_n, tmpbn); |
224 | 0 | BN_free(tmpbn); |
225 | 0 | if (!ok) |
226 | 0 | OPENSSL_free(buf); |
227 | 0 | return ok; |
228 | 0 | err: |
229 | 0 | BN_free(tmpbn); |
230 | 0 | return 0; |
231 | 0 | } |