]>
Commit | Line | Data |
---|---|---|
7ffbd7ca | 1 | /* |
b6461792 | 2 | * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. |
7ffbd7ca P |
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 <string.h> | |
12 | #include <openssl/params.h> | |
ac1082f0 | 13 | #include <openssl/err.h> |
7ffbd7ca | 14 | #include "internal/thread_once.h" |
c04a9916 | 15 | #include "internal/numbers.h" |
e9c5e642 | 16 | #include "internal/endian.h" |
79523d55 P |
17 | #include "internal/params.h" |
18 | #include "internal/packet.h" | |
7ffbd7ca | 19 | |
ac1082f0 P |
20 | /* Shortcuts for raising errors that are widely used */ |
21 | #define err_unsigned_negative \ | |
22 | ERR_raise(ERR_LIB_CRYPTO, \ | |
23 | CRYPTO_R_PARAM_UNSIGNED_INTEGER_NEGATIVE_VALUE_UNSUPPORTED) | |
24 | #define err_out_of_range \ | |
25 | ERR_raise(ERR_LIB_CRYPTO, \ | |
26 | CRYPTO_R_PARAM_VALUE_TOO_LARGE_FOR_DESTINATION) | |
27 | #define err_inexact \ | |
28 | ERR_raise(ERR_LIB_CRYPTO, \ | |
29 | CRYPTO_R_PARAM_CANNOT_BE_REPRESENTED_EXACTLY) | |
30 | #define err_not_integer \ | |
31 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_NOT_INTEGER_TYPE) | |
32 | #define err_too_small \ | |
33 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER) | |
34 | #define err_bad_type \ | |
35 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_OF_INCOMPATIBLE_TYPE) | |
36 | #define err_null_argument \ | |
37 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER) | |
38 | #define err_unsupported_real \ | |
39 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_UNSUPPORTED_FLOATING_POINT_FORMAT) | |
40 | ||
2c500578 | 41 | #ifndef OPENSSL_SYS_UEFI |
5b1d94c1 P |
42 | /* |
43 | * Return the number of bits in the mantissa of a double. This is used to | |
44 | * shift a larger integral value to determine if it will exactly fit into a | |
45 | * double. | |
46 | */ | |
47 | static unsigned int real_shift(void) | |
48 | { | |
49 | return sizeof(double) == 4 ? 24 : 53; | |
50 | } | |
2c500578 | 51 | #endif |
5b1d94c1 | 52 | |
4e7991b4 | 53 | OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key) |
7ffbd7ca P |
54 | { |
55 | if (p != NULL && key != NULL) | |
56 | for (; p->key != NULL; p++) | |
57 | if (strcmp(key, p->key) == 0) | |
58 | return p; | |
59 | return NULL; | |
60 | } | |
61 | ||
4e7991b4 P |
62 | const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key) |
63 | { | |
64 | return OSSL_PARAM_locate((OSSL_PARAM *)p, key); | |
65 | } | |
66 | ||
7ffbd7ca | 67 | static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type, |
4e7991b4 | 68 | void *data, size_t data_size) |
7ffbd7ca P |
69 | { |
70 | OSSL_PARAM res; | |
71 | ||
72 | res.key = key; | |
73 | res.data_type = data_type; | |
74 | res.data = data; | |
75 | res.data_size = data_size; | |
8d5fb648 | 76 | res.return_size = OSSL_PARAM_UNMODIFIED; |
7ffbd7ca P |
77 | return res; |
78 | } | |
79 | ||
8d5fb648 P |
80 | int OSSL_PARAM_modified(const OSSL_PARAM *p) |
81 | { | |
82 | return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED; | |
83 | } | |
84 | ||
3873887e | 85 | void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p) |
8d5fb648 P |
86 | { |
87 | if (p != NULL) | |
3873887e P |
88 | while (p->key != NULL) |
89 | p++->return_size = OSSL_PARAM_UNMODIFIED; | |
8d5fb648 P |
90 | } |
91 | ||
e9c5e642 P |
92 | /* Return non-zero if the signed number is negative */ |
93 | static int is_negative(const void *number, size_t s) | |
94 | { | |
95 | const unsigned char *n = number; | |
96 | DECLARE_IS_ENDIAN; | |
97 | ||
98 | return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]); | |
99 | } | |
100 | ||
101 | /* Check that all the bytes specified match the expected sign byte */ | |
102 | static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s) | |
103 | { | |
104 | size_t i; | |
105 | ||
106 | for (i = 0; i < n; i++) | |
107 | if (p[i] != s) | |
108 | return 0; | |
109 | return 1; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Copy an integer to another integer. | |
114 | * Handle different length integers and signed and unsigned integers. | |
115 | * Both integers are in native byte ordering. | |
116 | */ | |
117 | static int copy_integer(unsigned char *dest, size_t dest_len, | |
118 | const unsigned char *src, size_t src_len, | |
119 | unsigned char pad, int signed_int) | |
120 | { | |
121 | size_t n; | |
122 | DECLARE_IS_ENDIAN; | |
123 | ||
124 | if (IS_BIG_ENDIAN) { | |
125 | if (src_len < dest_len) { | |
126 | n = dest_len - src_len; | |
127 | memset(dest, pad, n); | |
128 | memcpy(dest + n, src, src_len); | |
129 | } else { | |
130 | n = src_len - dest_len; | |
131 | if (!check_sign_bytes(src, n, pad) | |
132 | /* | |
133 | * Shortening a signed value must retain the correct sign. | |
134 | * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3 | |
135 | */ | |
ac1082f0 P |
136 | || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) { |
137 | err_out_of_range; | |
e9c5e642 | 138 | return 0; |
ac1082f0 | 139 | } |
e9c5e642 P |
140 | memcpy(dest, src + n, dest_len); |
141 | } | |
142 | } else /* IS_LITTLE_ENDIAN */ { | |
143 | if (src_len < dest_len) { | |
144 | n = dest_len - src_len; | |
145 | memset(dest + src_len, pad, n); | |
146 | memcpy(dest, src, src_len); | |
147 | } else { | |
148 | n = src_len - dest_len; | |
149 | if (!check_sign_bytes(src + dest_len, n, pad) | |
150 | /* | |
151 | * Shortening a signed value must retain the correct sign. | |
152 | * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126 | |
153 | */ | |
ac1082f0 P |
154 | || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) { |
155 | err_out_of_range; | |
e9c5e642 | 156 | return 0; |
ac1082f0 | 157 | } |
e9c5e642 P |
158 | memcpy(dest, src, dest_len); |
159 | } | |
160 | } | |
161 | return 1; | |
162 | } | |
163 | ||
164 | /* Copy a signed number to a signed number of possibly different length */ | |
165 | static int signed_from_signed(void *dest, size_t dest_len, | |
166 | const void *src, size_t src_len) | |
167 | { | |
168 | return copy_integer(dest, dest_len, src, src_len, | |
169 | is_negative(src, src_len) ? 0xff : 0, 1); | |
170 | } | |
171 | ||
172 | /* Copy an unsigned number to a signed number of possibly different length */ | |
173 | static int signed_from_unsigned(void *dest, size_t dest_len, | |
174 | const void *src, size_t src_len) | |
175 | { | |
176 | return copy_integer(dest, dest_len, src, src_len, 0, 1); | |
177 | } | |
178 | ||
179 | /* Copy a signed number to an unsigned number of possibly different length */ | |
180 | static int unsigned_from_signed(void *dest, size_t dest_len, | |
181 | const void *src, size_t src_len) | |
182 | { | |
ac1082f0 P |
183 | if (is_negative(src, src_len)) { |
184 | err_unsigned_negative; | |
e9c5e642 | 185 | return 0; |
ac1082f0 | 186 | } |
e9c5e642 P |
187 | return copy_integer(dest, dest_len, src, src_len, 0, 0); |
188 | } | |
189 | ||
190 | /* Copy an unsigned number to an unsigned number of possibly different length */ | |
191 | static int unsigned_from_unsigned(void *dest, size_t dest_len, | |
192 | const void *src, size_t src_len) | |
193 | { | |
194 | return copy_integer(dest, dest_len, src, src_len, 0, 0); | |
195 | } | |
196 | ||
197 | /* General purpose get integer parameter call that handles odd sizes */ | |
198 | static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size) | |
199 | { | |
806bbafe NH |
200 | if (p->data == NULL) { |
201 | err_null_argument; | |
202 | return 0; | |
203 | } | |
e9c5e642 P |
204 | if (p->data_type == OSSL_PARAM_INTEGER) |
205 | return signed_from_signed(val, val_size, p->data, p->data_size); | |
206 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) | |
207 | return signed_from_unsigned(val, val_size, p->data, p->data_size); | |
ac1082f0 | 208 | err_not_integer; |
e9c5e642 P |
209 | return 0; |
210 | } | |
211 | ||
212 | /* General purpose set integer parameter call that handles odd sizes */ | |
213 | static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size) | |
214 | { | |
215 | int r = 0; | |
216 | ||
217 | p->return_size = val_size; /* Expected size */ | |
218 | if (p->data == NULL) | |
219 | return 1; | |
220 | if (p->data_type == OSSL_PARAM_INTEGER) | |
221 | r = signed_from_signed(p->data, p->data_size, val, val_size); | |
222 | else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) | |
223 | r = unsigned_from_signed(p->data, p->data_size, val, val_size); | |
ac1082f0 P |
224 | else |
225 | err_not_integer; | |
e9c5e642 P |
226 | p->return_size = r ? p->data_size : val_size; |
227 | return r; | |
228 | } | |
229 | ||
230 | /* General purpose get unsigned integer parameter call that handles odd sizes */ | |
231 | static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size) | |
232 | { | |
806bbafe NH |
233 | |
234 | if (p->data == NULL) { | |
235 | err_null_argument; | |
236 | return 0; | |
237 | } | |
e9c5e642 P |
238 | if (p->data_type == OSSL_PARAM_INTEGER) |
239 | return unsigned_from_signed(val, val_size, p->data, p->data_size); | |
240 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) | |
241 | return unsigned_from_unsigned(val, val_size, p->data, p->data_size); | |
ac1082f0 | 242 | err_not_integer; |
e9c5e642 P |
243 | return 0; |
244 | } | |
245 | ||
246 | /* General purpose set unsigned integer parameter call that handles odd sizes */ | |
247 | static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size) | |
248 | { | |
249 | int r = 0; | |
250 | ||
251 | p->return_size = val_size; /* Expected size */ | |
252 | if (p->data == NULL) | |
253 | return 1; | |
254 | if (p->data_type == OSSL_PARAM_INTEGER) | |
255 | r = signed_from_unsigned(p->data, p->data_size, val, val_size); | |
256 | else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) | |
257 | r = unsigned_from_unsigned(p->data, p->data_size, val, val_size); | |
ac1082f0 P |
258 | else |
259 | err_not_integer; | |
e9c5e642 P |
260 | p->return_size = r ? p->data_size : val_size; |
261 | return r; | |
262 | } | |
263 | ||
7ffbd7ca P |
264 | int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val) |
265 | { | |
e9c5e642 | 266 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 267 | switch (sizeof(int)) { |
7ffbd7ca | 268 | case sizeof(int32_t): |
863360fb | 269 | return OSSL_PARAM_get_int32(p, (int32_t *)val); |
7ffbd7ca | 270 | case sizeof(int64_t): |
863360fb | 271 | return OSSL_PARAM_get_int64(p, (int64_t *)val); |
7ffbd7ca | 272 | } |
e9c5e642 P |
273 | #endif |
274 | return general_get_int(p, val, sizeof(*val)); | |
7ffbd7ca P |
275 | } |
276 | ||
4e7991b4 | 277 | int OSSL_PARAM_set_int(OSSL_PARAM *p, int val) |
7ffbd7ca | 278 | { |
e9c5e642 | 279 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 280 | switch (sizeof(int)) { |
7ffbd7ca | 281 | case sizeof(int32_t): |
863360fb | 282 | return OSSL_PARAM_set_int32(p, (int32_t)val); |
7ffbd7ca | 283 | case sizeof(int64_t): |
863360fb | 284 | return OSSL_PARAM_set_int64(p, (int64_t)val); |
7ffbd7ca | 285 | } |
e9c5e642 P |
286 | #endif |
287 | return general_set_int(p, &val, sizeof(val)); | |
7ffbd7ca P |
288 | } |
289 | ||
4e7991b4 | 290 | OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf) |
7ffbd7ca | 291 | { |
4e7991b4 | 292 | return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int)); |
7ffbd7ca P |
293 | } |
294 | ||
295 | int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val) | |
296 | { | |
e9c5e642 | 297 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 298 | switch (sizeof(unsigned int)) { |
7ffbd7ca | 299 | case sizeof(uint32_t): |
863360fb | 300 | return OSSL_PARAM_get_uint32(p, (uint32_t *)val); |
7ffbd7ca | 301 | case sizeof(uint64_t): |
863360fb | 302 | return OSSL_PARAM_get_uint64(p, (uint64_t *)val); |
7ffbd7ca | 303 | } |
e9c5e642 P |
304 | #endif |
305 | return general_get_uint(p, val, sizeof(*val)); | |
7ffbd7ca P |
306 | } |
307 | ||
4e7991b4 | 308 | int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val) |
7ffbd7ca | 309 | { |
e9c5e642 | 310 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 311 | switch (sizeof(unsigned int)) { |
7ffbd7ca | 312 | case sizeof(uint32_t): |
863360fb | 313 | return OSSL_PARAM_set_uint32(p, (uint32_t)val); |
7ffbd7ca | 314 | case sizeof(uint64_t): |
863360fb | 315 | return OSSL_PARAM_set_uint64(p, (uint64_t)val); |
7ffbd7ca | 316 | } |
e9c5e642 P |
317 | #endif |
318 | return general_set_uint(p, &val, sizeof(val)); | |
7ffbd7ca P |
319 | } |
320 | ||
4e7991b4 | 321 | OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf) |
7ffbd7ca P |
322 | { |
323 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf, | |
4e7991b4 | 324 | sizeof(unsigned int)); |
7ffbd7ca P |
325 | } |
326 | ||
327 | int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val) | |
328 | { | |
e9c5e642 | 329 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 330 | switch (sizeof(long int)) { |
7ffbd7ca | 331 | case sizeof(int32_t): |
863360fb | 332 | return OSSL_PARAM_get_int32(p, (int32_t *)val); |
7ffbd7ca | 333 | case sizeof(int64_t): |
863360fb | 334 | return OSSL_PARAM_get_int64(p, (int64_t *)val); |
7ffbd7ca | 335 | } |
e9c5e642 P |
336 | #endif |
337 | return general_get_int(p, val, sizeof(*val)); | |
7ffbd7ca P |
338 | } |
339 | ||
4e7991b4 | 340 | int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val) |
7ffbd7ca | 341 | { |
e9c5e642 | 342 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 343 | switch (sizeof(long int)) { |
7ffbd7ca | 344 | case sizeof(int32_t): |
863360fb | 345 | return OSSL_PARAM_set_int32(p, (int32_t)val); |
7ffbd7ca | 346 | case sizeof(int64_t): |
863360fb | 347 | return OSSL_PARAM_set_int64(p, (int64_t)val); |
7ffbd7ca | 348 | } |
e9c5e642 P |
349 | #endif |
350 | return general_set_int(p, &val, sizeof(val)); | |
7ffbd7ca P |
351 | } |
352 | ||
4e7991b4 | 353 | OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf) |
7ffbd7ca | 354 | { |
4e7991b4 | 355 | return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int)); |
7ffbd7ca P |
356 | } |
357 | ||
358 | int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val) | |
359 | { | |
e9c5e642 | 360 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 361 | switch (sizeof(unsigned long int)) { |
7ffbd7ca | 362 | case sizeof(uint32_t): |
863360fb | 363 | return OSSL_PARAM_get_uint32(p, (uint32_t *)val); |
7ffbd7ca | 364 | case sizeof(uint64_t): |
863360fb | 365 | return OSSL_PARAM_get_uint64(p, (uint64_t *)val); |
7ffbd7ca | 366 | } |
e9c5e642 P |
367 | #endif |
368 | return general_get_uint(p, val, sizeof(*val)); | |
7ffbd7ca P |
369 | } |
370 | ||
4e7991b4 | 371 | int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val) |
7ffbd7ca | 372 | { |
e9c5e642 | 373 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 374 | switch (sizeof(unsigned long int)) { |
7ffbd7ca | 375 | case sizeof(uint32_t): |
863360fb | 376 | return OSSL_PARAM_set_uint32(p, (uint32_t)val); |
7ffbd7ca | 377 | case sizeof(uint64_t): |
863360fb | 378 | return OSSL_PARAM_set_uint64(p, (uint64_t)val); |
7ffbd7ca | 379 | } |
e9c5e642 P |
380 | #endif |
381 | return general_set_uint(p, &val, sizeof(val)); | |
7ffbd7ca P |
382 | } |
383 | ||
4e7991b4 | 384 | OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf) |
7ffbd7ca P |
385 | { |
386 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf, | |
4e7991b4 | 387 | sizeof(unsigned long int)); |
7ffbd7ca P |
388 | } |
389 | ||
390 | int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val) | |
391 | { | |
ac1082f0 P |
392 | if (val == NULL || p == NULL) { |
393 | err_null_argument; | |
7ffbd7ca | 394 | return 0; |
ac1082f0 | 395 | } |
7ffbd7ca | 396 | |
806bbafe NH |
397 | if (p->data == NULL) { |
398 | err_null_argument; | |
399 | return 0; | |
400 | } | |
401 | ||
9830e7ea | 402 | if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 P |
403 | #ifndef OPENSSL_SMALL_FOOTPRINT |
404 | int64_t i64; | |
405 | ||
9830e7ea P |
406 | switch (p->data_size) { |
407 | case sizeof(int32_t): | |
408 | *val = *(const int32_t *)p->data; | |
409 | return 1; | |
410 | case sizeof(int64_t): | |
411 | i64 = *(const int64_t *)p->data; | |
412 | if (i64 >= INT32_MIN && i64 <= INT32_MAX) { | |
413 | *val = (int32_t)i64; | |
414 | return 1; | |
415 | } | |
ac1082f0 | 416 | err_out_of_range; |
e9c5e642 | 417 | return 0; |
9830e7ea | 418 | } |
e9c5e642 P |
419 | #endif |
420 | return general_get_int(p, val, sizeof(*val)); | |
421 | ||
9830e7ea | 422 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 P |
423 | #ifndef OPENSSL_SMALL_FOOTPRINT |
424 | uint32_t u32; | |
425 | uint64_t u64; | |
426 | ||
9830e7ea P |
427 | switch (p->data_size) { |
428 | case sizeof(uint32_t): | |
429 | u32 = *(const uint32_t *)p->data; | |
430 | if (u32 <= INT32_MAX) { | |
431 | *val = (int32_t)u32; | |
432 | return 1; | |
433 | } | |
ac1082f0 | 434 | err_out_of_range; |
e9c5e642 | 435 | return 0; |
9830e7ea P |
436 | case sizeof(uint64_t): |
437 | u64 = *(const uint64_t *)p->data; | |
438 | if (u64 <= INT32_MAX) { | |
439 | *val = (int32_t)u64; | |
440 | return 1; | |
441 | } | |
ac1082f0 | 442 | err_out_of_range; |
e9c5e642 | 443 | return 0; |
9830e7ea | 444 | } |
e9c5e642 P |
445 | #endif |
446 | return general_get_int(p, val, sizeof(*val)); | |
447 | ||
9830e7ea | 448 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 YL |
449 | #ifndef OPENSSL_SYS_UEFI |
450 | double d; | |
451 | ||
9830e7ea P |
452 | switch (p->data_size) { |
453 | case sizeof(double): | |
454 | d = *(const double *)p->data; | |
455 | if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) { | |
456 | *val = (int32_t)d; | |
457 | return 1; | |
458 | } | |
ac1082f0 P |
459 | err_out_of_range; |
460 | return 0; | |
9830e7ea | 461 | } |
ac1082f0 P |
462 | err_unsupported_real; |
463 | return 0; | |
2c500578 | 464 | #endif |
7ffbd7ca | 465 | } |
ac1082f0 | 466 | err_bad_type; |
7ffbd7ca P |
467 | return 0; |
468 | } | |
469 | ||
4e7991b4 | 470 | int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val) |
7ffbd7ca | 471 | { |
ac1082f0 P |
472 | uint32_t u32; |
473 | unsigned int shift; | |
474 | ||
475 | if (p == NULL) { | |
476 | err_null_argument; | |
7ffbd7ca | 477 | return 0; |
ac1082f0 | 478 | } |
4e7991b4 | 479 | p->return_size = 0; |
9830e7ea | 480 | if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 | 481 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 482 | p->return_size = sizeof(int32_t); /* Minimum expected size */ |
8f3b8fd6 RL |
483 | if (p->data == NULL) |
484 | return 1; | |
9830e7ea P |
485 | switch (p->data_size) { |
486 | case sizeof(int32_t): | |
487 | *(int32_t *)p->data = val; | |
488 | return 1; | |
489 | case sizeof(int64_t): | |
4e7991b4 | 490 | p->return_size = sizeof(int64_t); |
9830e7ea P |
491 | *(int64_t *)p->data = (int64_t)val; |
492 | return 1; | |
493 | } | |
e9c5e642 P |
494 | #endif |
495 | return general_set_int(p, &val, sizeof(val)); | |
9830e7ea | 496 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) { |
e9c5e642 | 497 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 498 | p->return_size = sizeof(uint32_t); /* Minimum expected size */ |
8f3b8fd6 RL |
499 | if (p->data == NULL) |
500 | return 1; | |
9830e7ea P |
501 | switch (p->data_size) { |
502 | case sizeof(uint32_t): | |
503 | *(uint32_t *)p->data = (uint32_t)val; | |
504 | return 1; | |
505 | case sizeof(uint64_t): | |
4e7991b4 | 506 | p->return_size = sizeof(uint64_t); |
9830e7ea P |
507 | *(uint64_t *)p->data = (uint64_t)val; |
508 | return 1; | |
509 | } | |
e9c5e642 P |
510 | #endif |
511 | return general_set_int(p, &val, sizeof(val)); | |
9830e7ea | 512 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 | 513 | #ifndef OPENSSL_SYS_UEFI |
4e7991b4 | 514 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
515 | if (p->data == NULL) |
516 | return 1; | |
9830e7ea P |
517 | switch (p->data_size) { |
518 | case sizeof(double): | |
ac1082f0 P |
519 | shift = real_shift(); |
520 | if (shift < 8 * sizeof(val) - 1) { | |
521 | u32 = val < 0 ? -val : val; | |
522 | if ((u32 >> shift) != 0) { | |
523 | err_inexact; | |
524 | return 0; | |
525 | } | |
526 | } | |
9830e7ea P |
527 | *(double *)p->data = (double)val; |
528 | return 1; | |
529 | } | |
ac1082f0 P |
530 | err_unsupported_real; |
531 | return 0; | |
2c500578 | 532 | #endif |
7ffbd7ca | 533 | } |
ac1082f0 | 534 | err_bad_type; |
7ffbd7ca P |
535 | return 0; |
536 | } | |
537 | ||
4e7991b4 | 538 | OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf) |
7ffbd7ca P |
539 | { |
540 | return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, | |
4e7991b4 | 541 | sizeof(int32_t)); |
7ffbd7ca P |
542 | } |
543 | ||
544 | int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val) | |
545 | { | |
ac1082f0 P |
546 | if (val == NULL || p == NULL) { |
547 | err_null_argument; | |
7ffbd7ca | 548 | return 0; |
ac1082f0 | 549 | } |
7ffbd7ca | 550 | |
806bbafe NH |
551 | if (p->data == NULL) { |
552 | err_null_argument; | |
553 | return 0; | |
554 | } | |
555 | ||
9830e7ea | 556 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 P |
557 | #ifndef OPENSSL_SMALL_FOOTPRINT |
558 | uint64_t u64; | |
559 | ||
9830e7ea P |
560 | switch (p->data_size) { |
561 | case sizeof(uint32_t): | |
562 | *val = *(const uint32_t *)p->data; | |
563 | return 1; | |
564 | case sizeof(uint64_t): | |
565 | u64 = *(const uint64_t *)p->data; | |
566 | if (u64 <= UINT32_MAX) { | |
567 | *val = (uint32_t)u64; | |
568 | return 1; | |
569 | } | |
ac1082f0 | 570 | err_out_of_range; |
e9c5e642 | 571 | return 0; |
9830e7ea | 572 | } |
e9c5e642 P |
573 | #endif |
574 | return general_get_uint(p, val, sizeof(*val)); | |
9830e7ea | 575 | } else if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 P |
576 | #ifndef OPENSSL_SMALL_FOOTPRINT |
577 | int32_t i32; | |
578 | int64_t i64; | |
579 | ||
9830e7ea P |
580 | switch (p->data_size) { |
581 | case sizeof(int32_t): | |
582 | i32 = *(const int32_t *)p->data; | |
583 | if (i32 >= 0) { | |
584 | *val = i32; | |
585 | return 1; | |
586 | } | |
ac1082f0 | 587 | err_unsigned_negative; |
e9c5e642 | 588 | return 0; |
9830e7ea P |
589 | case sizeof(int64_t): |
590 | i64 = *(const int64_t *)p->data; | |
591 | if (i64 >= 0 && i64 <= UINT32_MAX) { | |
592 | *val = (uint32_t)i64; | |
593 | return 1; | |
594 | } | |
ac1082f0 P |
595 | if (i64 < 0) |
596 | err_unsigned_negative; | |
597 | else | |
598 | err_out_of_range; | |
e9c5e642 | 599 | return 0; |
9830e7ea | 600 | } |
e9c5e642 P |
601 | #endif |
602 | return general_get_uint(p, val, sizeof(*val)); | |
9830e7ea | 603 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 YL |
604 | #ifndef OPENSSL_SYS_UEFI |
605 | double d; | |
606 | ||
9830e7ea P |
607 | switch (p->data_size) { |
608 | case sizeof(double): | |
609 | d = *(const double *)p->data; | |
610 | if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) { | |
611 | *val = (uint32_t)d; | |
612 | return 1; | |
613 | } | |
ac1082f0 P |
614 | err_inexact; |
615 | return 0; | |
9830e7ea | 616 | } |
ac1082f0 P |
617 | err_unsupported_real; |
618 | return 0; | |
2c500578 | 619 | #endif |
7ffbd7ca | 620 | } |
ac1082f0 | 621 | err_bad_type; |
7ffbd7ca P |
622 | return 0; |
623 | } | |
624 | ||
4e7991b4 | 625 | int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val) |
7ffbd7ca | 626 | { |
ac1082f0 P |
627 | unsigned int shift; |
628 | ||
629 | if (p == NULL) { | |
630 | err_null_argument; | |
7ffbd7ca | 631 | return 0; |
ac1082f0 | 632 | } |
4e7991b4 | 633 | p->return_size = 0; |
7ffbd7ca | 634 | |
9830e7ea | 635 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 | 636 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 637 | p->return_size = sizeof(uint32_t); /* Minimum expected size */ |
8f3b8fd6 RL |
638 | if (p->data == NULL) |
639 | return 1; | |
9830e7ea P |
640 | switch (p->data_size) { |
641 | case sizeof(uint32_t): | |
642 | *(uint32_t *)p->data = val; | |
643 | return 1; | |
644 | case sizeof(uint64_t): | |
4e7991b4 | 645 | p->return_size = sizeof(uint64_t); |
9830e7ea P |
646 | *(uint64_t *)p->data = val; |
647 | return 1; | |
648 | } | |
e9c5e642 P |
649 | #endif |
650 | return general_set_uint(p, &val, sizeof(val)); | |
9830e7ea | 651 | } else if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 | 652 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 653 | p->return_size = sizeof(int32_t); /* Minimum expected size */ |
8f3b8fd6 RL |
654 | if (p->data == NULL) |
655 | return 1; | |
9830e7ea P |
656 | switch (p->data_size) { |
657 | case sizeof(int32_t): | |
658 | if (val <= INT32_MAX) { | |
659 | *(int32_t *)p->data = (int32_t)val; | |
660 | return 1; | |
661 | } | |
ac1082f0 | 662 | err_out_of_range; |
e9c5e642 | 663 | return 0; |
9830e7ea | 664 | case sizeof(int64_t): |
4e7991b4 | 665 | p->return_size = sizeof(int64_t); |
9830e7ea P |
666 | *(int64_t *)p->data = (int64_t)val; |
667 | return 1; | |
668 | } | |
e9c5e642 P |
669 | #endif |
670 | return general_set_uint(p, &val, sizeof(val)); | |
9830e7ea | 671 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 | 672 | #ifndef OPENSSL_SYS_UEFI |
4e7991b4 | 673 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
674 | if (p->data == NULL) |
675 | return 1; | |
9830e7ea P |
676 | switch (p->data_size) { |
677 | case sizeof(double): | |
ac1082f0 P |
678 | shift = real_shift(); |
679 | if (shift < 8 * sizeof(val) && (val >> shift) != 0) { | |
680 | err_inexact; | |
681 | return 0; | |
682 | } | |
9830e7ea P |
683 | *(double *)p->data = (double)val; |
684 | return 1; | |
685 | } | |
ac1082f0 P |
686 | err_unsupported_real; |
687 | return 0; | |
2c500578 | 688 | #endif |
7ffbd7ca | 689 | } |
ac1082f0 | 690 | err_bad_type; |
7ffbd7ca P |
691 | return 0; |
692 | } | |
693 | ||
4e7991b4 | 694 | OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf) |
7ffbd7ca P |
695 | { |
696 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf, | |
4e7991b4 | 697 | sizeof(uint32_t)); |
7ffbd7ca P |
698 | } |
699 | ||
700 | int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val) | |
701 | { | |
ac1082f0 P |
702 | if (val == NULL || p == NULL) { |
703 | err_null_argument; | |
7ffbd7ca | 704 | return 0; |
ac1082f0 | 705 | } |
7ffbd7ca | 706 | |
806bbafe NH |
707 | if (p->data == NULL) { |
708 | err_null_argument; | |
709 | return 0; | |
710 | } | |
711 | ||
9830e7ea | 712 | if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 | 713 | #ifndef OPENSSL_SMALL_FOOTPRINT |
9830e7ea P |
714 | switch (p->data_size) { |
715 | case sizeof(int32_t): | |
716 | *val = *(const int32_t *)p->data; | |
717 | return 1; | |
718 | case sizeof(int64_t): | |
719 | *val = *(const int64_t *)p->data; | |
720 | return 1; | |
721 | } | |
e9c5e642 P |
722 | #endif |
723 | return general_get_int(p, val, sizeof(*val)); | |
9830e7ea | 724 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 P |
725 | #ifndef OPENSSL_SMALL_FOOTPRINT |
726 | uint64_t u64; | |
727 | ||
9830e7ea P |
728 | switch (p->data_size) { |
729 | case sizeof(uint32_t): | |
730 | *val = *(const uint32_t *)p->data; | |
731 | return 1; | |
732 | case sizeof(uint64_t): | |
733 | u64 = *(const uint64_t *)p->data; | |
734 | if (u64 <= INT64_MAX) { | |
735 | *val = (int64_t)u64; | |
736 | return 1; | |
737 | } | |
ac1082f0 | 738 | err_out_of_range; |
e9c5e642 | 739 | return 0; |
9830e7ea | 740 | } |
e9c5e642 P |
741 | #endif |
742 | return general_get_int(p, val, sizeof(*val)); | |
9830e7ea | 743 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 YL |
744 | #ifndef OPENSSL_SYS_UEFI |
745 | double d; | |
746 | ||
9830e7ea P |
747 | switch (p->data_size) { |
748 | case sizeof(double): | |
749 | d = *(const double *)p->data; | |
5b1d94c1 P |
750 | if (d >= INT64_MIN |
751 | /* | |
752 | * By subtracting 65535 (2^16-1) we cancel the low order | |
753 | * 15 bits of INT64_MAX to avoid using imprecise floating | |
754 | * point values. | |
755 | */ | |
756 | && d < (double)(INT64_MAX - 65535) + 65536.0 | |
757 | && d == (int64_t)d) { | |
9830e7ea P |
758 | *val = (int64_t)d; |
759 | return 1; | |
760 | } | |
ac1082f0 P |
761 | err_inexact; |
762 | return 0; | |
9830e7ea | 763 | } |
ac1082f0 P |
764 | err_unsupported_real; |
765 | return 0; | |
2c500578 | 766 | #endif |
7ffbd7ca | 767 | } |
ac1082f0 | 768 | err_bad_type; |
7ffbd7ca P |
769 | return 0; |
770 | } | |
771 | ||
4e7991b4 | 772 | int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val) |
7ffbd7ca | 773 | { |
ac1082f0 P |
774 | if (p == NULL) { |
775 | err_null_argument; | |
7ffbd7ca | 776 | return 0; |
ac1082f0 | 777 | } |
4e7991b4 | 778 | p->return_size = 0; |
9830e7ea | 779 | if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 | 780 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 781 | p->return_size = sizeof(int64_t); /* Expected size */ |
8f3b8fd6 RL |
782 | if (p->data == NULL) |
783 | return 1; | |
9830e7ea P |
784 | switch (p->data_size) { |
785 | case sizeof(int32_t): | |
786 | if (val >= INT32_MIN && val <= INT32_MAX) { | |
4e7991b4 | 787 | p->return_size = sizeof(int32_t); |
9830e7ea P |
788 | *(int32_t *)p->data = (int32_t)val; |
789 | return 1; | |
790 | } | |
ac1082f0 | 791 | err_out_of_range; |
e9c5e642 | 792 | return 0; |
9830e7ea P |
793 | case sizeof(int64_t): |
794 | *(int64_t *)p->data = val; | |
795 | return 1; | |
796 | } | |
e9c5e642 P |
797 | #endif |
798 | return general_set_int(p, &val, sizeof(val)); | |
9830e7ea | 799 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) { |
e9c5e642 | 800 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 801 | p->return_size = sizeof(uint64_t); /* Expected size */ |
8f3b8fd6 RL |
802 | if (p->data == NULL) |
803 | return 1; | |
9830e7ea P |
804 | switch (p->data_size) { |
805 | case sizeof(uint32_t): | |
806 | if (val <= UINT32_MAX) { | |
4e7991b4 | 807 | p->return_size = sizeof(uint32_t); |
9830e7ea P |
808 | *(uint32_t *)p->data = (uint32_t)val; |
809 | return 1; | |
810 | } | |
ac1082f0 | 811 | err_out_of_range; |
e9c5e642 | 812 | return 0; |
9830e7ea P |
813 | case sizeof(uint64_t): |
814 | *(uint64_t *)p->data = (uint64_t)val; | |
815 | return 1; | |
816 | } | |
e9c5e642 P |
817 | #endif |
818 | return general_set_int(p, &val, sizeof(val)); | |
9830e7ea | 819 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 YL |
820 | #ifndef OPENSSL_SYS_UEFI |
821 | uint64_t u64; | |
822 | ||
4e7991b4 | 823 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
824 | if (p->data == NULL) |
825 | return 1; | |
9830e7ea P |
826 | switch (p->data_size) { |
827 | case sizeof(double): | |
828 | u64 = val < 0 ? -val : val; | |
5b1d94c1 | 829 | if ((u64 >> real_shift()) == 0) { |
9830e7ea P |
830 | *(double *)p->data = (double)val; |
831 | return 1; | |
832 | } | |
ac1082f0 P |
833 | err_inexact; |
834 | return 0; | |
9830e7ea | 835 | } |
ac1082f0 P |
836 | err_unsupported_real; |
837 | return 0; | |
2c500578 | 838 | #endif |
7ffbd7ca | 839 | } |
ac1082f0 | 840 | err_bad_type; |
7ffbd7ca P |
841 | return 0; |
842 | } | |
843 | ||
4e7991b4 | 844 | OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf) |
7ffbd7ca | 845 | { |
4e7991b4 | 846 | return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t)); |
7ffbd7ca P |
847 | } |
848 | ||
849 | int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val) | |
850 | { | |
ac1082f0 P |
851 | if (val == NULL || p == NULL) { |
852 | err_null_argument; | |
7ffbd7ca | 853 | return 0; |
ac1082f0 | 854 | } |
7ffbd7ca | 855 | |
806bbafe NH |
856 | if (p->data == NULL) { |
857 | err_null_argument; | |
858 | return 0; | |
859 | } | |
860 | ||
9830e7ea | 861 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 | 862 | #ifndef OPENSSL_SMALL_FOOTPRINT |
9830e7ea P |
863 | switch (p->data_size) { |
864 | case sizeof(uint32_t): | |
865 | *val = *(const uint32_t *)p->data; | |
866 | return 1; | |
867 | case sizeof(uint64_t): | |
868 | *val = *(const uint64_t *)p->data; | |
869 | return 1; | |
870 | } | |
e9c5e642 P |
871 | #endif |
872 | return general_get_uint(p, val, sizeof(*val)); | |
9830e7ea | 873 | } else if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 P |
874 | #ifndef OPENSSL_SMALL_FOOTPRINT |
875 | int32_t i32; | |
876 | int64_t i64; | |
877 | ||
9830e7ea P |
878 | switch (p->data_size) { |
879 | case sizeof(int32_t): | |
880 | i32 = *(const int32_t *)p->data; | |
881 | if (i32 >= 0) { | |
882 | *val = (uint64_t)i32; | |
883 | return 1; | |
884 | } | |
ac1082f0 | 885 | err_unsigned_negative; |
e9c5e642 | 886 | return 0; |
9830e7ea P |
887 | case sizeof(int64_t): |
888 | i64 = *(const int64_t *)p->data; | |
889 | if (i64 >= 0) { | |
890 | *val = (uint64_t)i64; | |
891 | return 1; | |
892 | } | |
ac1082f0 | 893 | err_unsigned_negative; |
e9c5e642 | 894 | return 0; |
9830e7ea | 895 | } |
e9c5e642 P |
896 | #endif |
897 | return general_get_uint(p, val, sizeof(*val)); | |
9830e7ea | 898 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 YL |
899 | #ifndef OPENSSL_SYS_UEFI |
900 | double d; | |
901 | ||
9830e7ea P |
902 | switch (p->data_size) { |
903 | case sizeof(double): | |
904 | d = *(const double *)p->data; | |
5b1d94c1 P |
905 | if (d >= 0 |
906 | /* | |
907 | * By subtracting 65535 (2^16-1) we cancel the low order | |
908 | * 15 bits of UINT64_MAX to avoid using imprecise floating | |
909 | * point values. | |
910 | */ | |
911 | && d < (double)(UINT64_MAX - 65535) + 65536.0 | |
912 | && d == (uint64_t)d) { | |
9830e7ea P |
913 | *val = (uint64_t)d; |
914 | return 1; | |
915 | } | |
ac1082f0 P |
916 | err_inexact; |
917 | return 0; | |
9830e7ea | 918 | } |
ac1082f0 P |
919 | err_unsupported_real; |
920 | return 0; | |
2c500578 | 921 | #endif |
7ffbd7ca | 922 | } |
ac1082f0 | 923 | err_bad_type; |
7ffbd7ca P |
924 | return 0; |
925 | } | |
926 | ||
4e7991b4 | 927 | int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val) |
7ffbd7ca | 928 | { |
ac1082f0 P |
929 | if (p == NULL) { |
930 | err_null_argument; | |
7ffbd7ca | 931 | return 0; |
ac1082f0 | 932 | } |
4e7991b4 | 933 | p->return_size = 0; |
7ffbd7ca | 934 | |
9830e7ea | 935 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
e9c5e642 | 936 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 937 | p->return_size = sizeof(uint64_t); /* Expected size */ |
8f3b8fd6 RL |
938 | if (p->data == NULL) |
939 | return 1; | |
9830e7ea P |
940 | switch (p->data_size) { |
941 | case sizeof(uint32_t): | |
942 | if (val <= UINT32_MAX) { | |
4e7991b4 | 943 | p->return_size = sizeof(uint32_t); |
9830e7ea P |
944 | *(uint32_t *)p->data = (uint32_t)val; |
945 | return 1; | |
946 | } | |
ac1082f0 | 947 | err_out_of_range; |
e9c5e642 | 948 | return 0; |
9830e7ea P |
949 | case sizeof(uint64_t): |
950 | *(uint64_t *)p->data = val; | |
951 | return 1; | |
952 | } | |
e9c5e642 P |
953 | #endif |
954 | return general_set_uint(p, &val, sizeof(val)); | |
9830e7ea | 955 | } else if (p->data_type == OSSL_PARAM_INTEGER) { |
e9c5e642 | 956 | #ifndef OPENSSL_SMALL_FOOTPRINT |
4e7991b4 | 957 | p->return_size = sizeof(int64_t); /* Expected size */ |
8f3b8fd6 RL |
958 | if (p->data == NULL) |
959 | return 1; | |
9830e7ea P |
960 | switch (p->data_size) { |
961 | case sizeof(int32_t): | |
962 | if (val <= INT32_MAX) { | |
4e7991b4 | 963 | p->return_size = sizeof(int32_t); |
9830e7ea P |
964 | *(int32_t *)p->data = (int32_t)val; |
965 | return 1; | |
966 | } | |
ac1082f0 | 967 | err_out_of_range; |
e9c5e642 | 968 | return 0; |
9830e7ea P |
969 | case sizeof(int64_t): |
970 | if (val <= INT64_MAX) { | |
971 | *(int64_t *)p->data = (int64_t)val; | |
972 | return 1; | |
973 | } | |
ac1082f0 | 974 | err_out_of_range; |
e9c5e642 | 975 | return 0; |
9830e7ea | 976 | } |
e9c5e642 P |
977 | #endif |
978 | return general_set_uint(p, &val, sizeof(val)); | |
9830e7ea | 979 | } else if (p->data_type == OSSL_PARAM_REAL) { |
2c500578 | 980 | #ifndef OPENSSL_SYS_UEFI |
4e7991b4 | 981 | p->return_size = sizeof(double); |
9830e7ea P |
982 | switch (p->data_size) { |
983 | case sizeof(double): | |
5b1d94c1 | 984 | if ((val >> real_shift()) == 0) { |
9830e7ea P |
985 | *(double *)p->data = (double)val; |
986 | return 1; | |
987 | } | |
ac1082f0 P |
988 | err_inexact; |
989 | return 0; | |
9830e7ea | 990 | } |
ac1082f0 P |
991 | err_unsupported_real; |
992 | return 0; | |
2c500578 | 993 | #endif |
7ffbd7ca | 994 | } |
ac1082f0 | 995 | err_bad_type; |
7ffbd7ca P |
996 | return 0; |
997 | } | |
998 | ||
4e7991b4 P |
999 | OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf) |
1000 | { | |
7ffbd7ca | 1001 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf, |
4e7991b4 | 1002 | sizeof(uint64_t)); |
7ffbd7ca P |
1003 | } |
1004 | ||
1005 | int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val) | |
1006 | { | |
e9c5e642 | 1007 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 1008 | switch (sizeof(size_t)) { |
7ffbd7ca | 1009 | case sizeof(uint32_t): |
863360fb | 1010 | return OSSL_PARAM_get_uint32(p, (uint32_t *)val); |
7ffbd7ca | 1011 | case sizeof(uint64_t): |
863360fb | 1012 | return OSSL_PARAM_get_uint64(p, (uint64_t *)val); |
7ffbd7ca | 1013 | } |
e9c5e642 P |
1014 | #endif |
1015 | return general_get_uint(p, val, sizeof(*val)); | |
7ffbd7ca P |
1016 | } |
1017 | ||
4e7991b4 | 1018 | int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val) |
7ffbd7ca | 1019 | { |
e9c5e642 | 1020 | #ifndef OPENSSL_SMALL_FOOTPRINT |
863360fb | 1021 | switch (sizeof(size_t)) { |
7ffbd7ca | 1022 | case sizeof(uint32_t): |
863360fb | 1023 | return OSSL_PARAM_set_uint32(p, (uint32_t)val); |
7ffbd7ca | 1024 | case sizeof(uint64_t): |
863360fb | 1025 | return OSSL_PARAM_set_uint64(p, (uint64_t)val); |
7ffbd7ca | 1026 | } |
e9c5e642 P |
1027 | #endif |
1028 | return general_set_uint(p, &val, sizeof(val)); | |
7ffbd7ca P |
1029 | } |
1030 | ||
4e7991b4 | 1031 | OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf) |
7ffbd7ca P |
1032 | { |
1033 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf, | |
4e7991b4 P |
1034 | sizeof(size_t)); |
1035 | } | |
7ffbd7ca | 1036 | |
5fdaa38f P |
1037 | int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val) |
1038 | { | |
e9c5e642 | 1039 | #ifndef OPENSSL_SMALL_FOOTPRINT |
5fdaa38f P |
1040 | switch (sizeof(time_t)) { |
1041 | case sizeof(int32_t): | |
1042 | return OSSL_PARAM_get_int32(p, (int32_t *)val); | |
1043 | case sizeof(int64_t): | |
1044 | return OSSL_PARAM_get_int64(p, (int64_t *)val); | |
1045 | } | |
e9c5e642 P |
1046 | #endif |
1047 | return general_get_int(p, val, sizeof(*val)); | |
5fdaa38f P |
1048 | } |
1049 | ||
1050 | int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val) | |
1051 | { | |
e9c5e642 | 1052 | #ifndef OPENSSL_SMALL_FOOTPRINT |
5fdaa38f P |
1053 | switch (sizeof(time_t)) { |
1054 | case sizeof(int32_t): | |
1055 | return OSSL_PARAM_set_int32(p, (int32_t)val); | |
1056 | case sizeof(int64_t): | |
1057 | return OSSL_PARAM_set_int64(p, (int64_t)val); | |
1058 | } | |
e9c5e642 P |
1059 | #endif |
1060 | return general_set_int(p, &val, sizeof(val)); | |
5fdaa38f P |
1061 | } |
1062 | ||
1063 | OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf) | |
1064 | { | |
1065 | return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t)); | |
1066 | } | |
1067 | ||
7ffbd7ca P |
1068 | int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val) |
1069 | { | |
f1719858 | 1070 | BIGNUM *b = NULL; |
7ffbd7ca | 1071 | |
806bbafe | 1072 | if (val == NULL || p == NULL || p->data == NULL) { |
ac1082f0 P |
1073 | err_null_argument; |
1074 | return 0; | |
1075 | } | |
f1719858 RL |
1076 | |
1077 | switch (p->data_type) { | |
1078 | case OSSL_PARAM_UNSIGNED_INTEGER: | |
1079 | b = BN_native2bn(p->data, (int)p->data_size, *val); | |
1080 | break; | |
1081 | case OSSL_PARAM_INTEGER: | |
1082 | b = BN_signed_native2bn(p->data, (int)p->data_size, *val); | |
1083 | break; | |
1084 | default: | |
ac1082f0 | 1085 | err_bad_type; |
f1719858 | 1086 | break; |
ac1082f0 | 1087 | } |
7ffbd7ca | 1088 | |
f1719858 | 1089 | if (b == NULL) { |
e077455e | 1090 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB); |
f1719858 | 1091 | return 0; |
7ffbd7ca | 1092 | } |
f1719858 RL |
1093 | |
1094 | *val = b; | |
1095 | return 1; | |
7ffbd7ca P |
1096 | } |
1097 | ||
4e7991b4 | 1098 | int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val) |
7ffbd7ca P |
1099 | { |
1100 | size_t bytes; | |
1101 | ||
ac1082f0 P |
1102 | if (p == NULL) { |
1103 | err_null_argument; | |
7ffbd7ca | 1104 | return 0; |
ac1082f0 | 1105 | } |
4e7991b4 | 1106 | p->return_size = 0; |
ac1082f0 P |
1107 | if (val == NULL) { |
1108 | err_null_argument; | |
7ffbd7ca | 1109 | return 0; |
ac1082f0 | 1110 | } |
f1719858 | 1111 | if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) { |
ac1082f0 P |
1112 | err_bad_type; |
1113 | return 0; | |
1114 | } | |
7ffbd7ca P |
1115 | |
1116 | bytes = (size_t)BN_num_bytes(val); | |
f1719858 RL |
1117 | /* We add 1 byte for signed numbers, to make space for a sign extension */ |
1118 | if (p->data_type == OSSL_PARAM_INTEGER) | |
1119 | bytes++; | |
c2ae8914 RL |
1120 | /* We make sure that at least one byte is used, so zero is properly set */ |
1121 | if (bytes == 0) | |
1122 | bytes++; | |
f1719858 | 1123 | |
4e7991b4 | 1124 | p->return_size = bytes; |
8f3b8fd6 RL |
1125 | if (p->data == NULL) |
1126 | return 1; | |
0f73e719 RL |
1127 | if (p->data_size >= bytes) { |
1128 | p->return_size = p->data_size; | |
f1719858 RL |
1129 | |
1130 | switch (p->data_type) { | |
1131 | case OSSL_PARAM_UNSIGNED_INTEGER: | |
1132 | if (BN_bn2nativepad(val, p->data, p->data_size) >= 0) | |
1133 | return 1; | |
1134 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW); | |
1135 | break; | |
1136 | case OSSL_PARAM_INTEGER: | |
1137 | if (BN_signed_bn2native(val, p->data, p->data_size) >= 0) | |
1138 | return 1; | |
1139 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW); | |
1140 | break; | |
1141 | default: | |
1142 | err_bad_type; | |
1143 | break; | |
1144 | } | |
ac1082f0 | 1145 | return 0; |
0f73e719 | 1146 | } |
ac1082f0 | 1147 | err_too_small; |
0f73e719 | 1148 | return 0; |
7ffbd7ca P |
1149 | } |
1150 | ||
1151 | OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf, | |
4e7991b4 | 1152 | size_t bsize) |
7ffbd7ca P |
1153 | { |
1154 | return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, | |
4e7991b4 | 1155 | buf, bsize); |
7ffbd7ca P |
1156 | } |
1157 | ||
2c500578 | 1158 | #ifndef OPENSSL_SYS_UEFI |
7ffbd7ca P |
1159 | int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val) |
1160 | { | |
9830e7ea P |
1161 | int64_t i64; |
1162 | uint64_t u64; | |
1163 | ||
806bbafe | 1164 | if (val == NULL || p == NULL || p->data == NULL) { |
ac1082f0 | 1165 | err_null_argument; |
7ffbd7ca | 1166 | return 0; |
ac1082f0 | 1167 | } |
7ffbd7ca | 1168 | |
9830e7ea P |
1169 | if (p->data_type == OSSL_PARAM_REAL) { |
1170 | switch (p->data_size) { | |
1171 | case sizeof(double): | |
1172 | *val = *(const double *)p->data; | |
1173 | return 1; | |
1174 | } | |
ac1082f0 P |
1175 | err_unsupported_real; |
1176 | return 0; | |
9830e7ea P |
1177 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { |
1178 | switch (p->data_size) { | |
1179 | case sizeof(uint32_t): | |
1180 | *val = *(const uint32_t *)p->data; | |
1181 | return 1; | |
1182 | case sizeof(uint64_t): | |
1183 | u64 = *(const uint64_t *)p->data; | |
5b1d94c1 | 1184 | if ((u64 >> real_shift()) == 0) { |
9830e7ea P |
1185 | *val = (double)u64; |
1186 | return 1; | |
1187 | } | |
ac1082f0 P |
1188 | err_inexact; |
1189 | return 0; | |
9830e7ea P |
1190 | } |
1191 | } else if (p->data_type == OSSL_PARAM_INTEGER) { | |
1192 | switch (p->data_size) { | |
1193 | case sizeof(int32_t): | |
1194 | *val = *(const int32_t *)p->data; | |
1195 | return 1; | |
1196 | case sizeof(int64_t): | |
1197 | i64 = *(const int64_t *)p->data; | |
1198 | u64 = i64 < 0 ? -i64 : i64; | |
5b1d94c1 | 1199 | if ((u64 >> real_shift()) == 0) { |
9830e7ea P |
1200 | *val = 0.0 + i64; |
1201 | return 1; | |
1202 | } | |
ac1082f0 P |
1203 | err_inexact; |
1204 | return 0; | |
9830e7ea | 1205 | } |
7ffbd7ca | 1206 | } |
ac1082f0 | 1207 | err_bad_type; |
7ffbd7ca P |
1208 | return 0; |
1209 | } | |
1210 | ||
4e7991b4 | 1211 | int OSSL_PARAM_set_double(OSSL_PARAM *p, double val) |
7ffbd7ca | 1212 | { |
ac1082f0 P |
1213 | if (p == NULL) { |
1214 | err_null_argument; | |
7ffbd7ca | 1215 | return 0; |
ac1082f0 | 1216 | } |
4e7991b4 | 1217 | p->return_size = 0; |
7ffbd7ca | 1218 | |
9830e7ea | 1219 | if (p->data_type == OSSL_PARAM_REAL) { |
4e7991b4 | 1220 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
1221 | if (p->data == NULL) |
1222 | return 1; | |
9830e7ea P |
1223 | switch (p->data_size) { |
1224 | case sizeof(double): | |
1225 | *(double *)p->data = val; | |
1226 | return 1; | |
1227 | } | |
ac1082f0 P |
1228 | err_unsupported_real; |
1229 | return 0; | |
1230 | } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { | |
4e7991b4 | 1231 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
1232 | if (p->data == NULL) |
1233 | return 1; | |
ac1082f0 P |
1234 | if (val != (uint64_t)val) { |
1235 | err_inexact; | |
1236 | return 0; | |
1237 | } | |
9830e7ea P |
1238 | switch (p->data_size) { |
1239 | case sizeof(uint32_t): | |
1240 | if (val >= 0 && val <= UINT32_MAX) { | |
4e7991b4 | 1241 | p->return_size = sizeof(uint32_t); |
9830e7ea P |
1242 | *(uint32_t *)p->data = (uint32_t)val; |
1243 | return 1; | |
1244 | } | |
ac1082f0 P |
1245 | err_out_of_range; |
1246 | return 0; | |
9830e7ea | 1247 | case sizeof(uint64_t): |
5b1d94c1 P |
1248 | if (val >= 0 |
1249 | /* | |
1250 | * By subtracting 65535 (2^16-1) we cancel the low order | |
1251 | * 15 bits of UINT64_MAX to avoid using imprecise floating | |
1252 | * point values. | |
1253 | */ | |
b9d022d7 | 1254 | && val < (double)(UINT64_MAX - 65535) + 65536.0) { |
4e7991b4 | 1255 | p->return_size = sizeof(uint64_t); |
9830e7ea P |
1256 | *(uint64_t *)p->data = (uint64_t)val; |
1257 | return 1; | |
1258 | } | |
ac1082f0 P |
1259 | err_out_of_range; |
1260 | return 0; | |
1261 | } | |
1262 | } else if (p->data_type == OSSL_PARAM_INTEGER) { | |
4e7991b4 | 1263 | p->return_size = sizeof(double); |
8f3b8fd6 RL |
1264 | if (p->data == NULL) |
1265 | return 1; | |
ac1082f0 P |
1266 | if (val != (int64_t)val) { |
1267 | err_inexact; | |
1268 | return 0; | |
1269 | } | |
9830e7ea P |
1270 | switch (p->data_size) { |
1271 | case sizeof(int32_t): | |
1272 | if (val >= INT32_MIN && val <= INT32_MAX) { | |
4e7991b4 | 1273 | p->return_size = sizeof(int32_t); |
9830e7ea P |
1274 | *(int32_t *)p->data = (int32_t)val; |
1275 | return 1; | |
1276 | } | |
ac1082f0 P |
1277 | err_out_of_range; |
1278 | return 0; | |
9830e7ea | 1279 | case sizeof(int64_t): |
5b1d94c1 P |
1280 | if (val >= INT64_MIN |
1281 | /* | |
1282 | * By subtracting 65535 (2^16-1) we cancel the low order | |
1283 | * 15 bits of INT64_MAX to avoid using imprecise floating | |
1284 | * point values. | |
1285 | */ | |
1286 | && val < (double)(INT64_MAX - 65535) + 65536.0) { | |
4e7991b4 | 1287 | p->return_size = sizeof(int64_t); |
9830e7ea P |
1288 | *(int64_t *)p->data = (int64_t)val; |
1289 | return 1; | |
1290 | } | |
ac1082f0 P |
1291 | err_out_of_range; |
1292 | return 0; | |
9830e7ea | 1293 | } |
7ffbd7ca | 1294 | } |
ac1082f0 | 1295 | err_bad_type; |
7ffbd7ca P |
1296 | return 0; |
1297 | } | |
1298 | ||
4e7991b4 | 1299 | OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf) |
7ffbd7ca | 1300 | { |
4e7991b4 | 1301 | return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double)); |
7ffbd7ca | 1302 | } |
2c500578 | 1303 | #endif |
7ffbd7ca | 1304 | |
247a1786 RL |
1305 | static int get_string_internal(const OSSL_PARAM *p, void **val, |
1306 | size_t *max_len, size_t *used_len, | |
1307 | unsigned int type) | |
7ffbd7ca | 1308 | { |
247a1786 | 1309 | size_t sz, alloc_sz; |
7ffbd7ca | 1310 | |
ac1082f0 P |
1311 | if ((val == NULL && used_len == NULL) || p == NULL) { |
1312 | err_null_argument; | |
1313 | return 0; | |
1314 | } | |
1315 | if (p->data_type != type) { | |
1316 | err_bad_type; | |
7ffbd7ca | 1317 | return 0; |
ac1082f0 | 1318 | } |
7ffbd7ca P |
1319 | |
1320 | sz = p->data_size; | |
247a1786 RL |
1321 | /* |
1322 | * If the input size is 0, or the input string needs NUL byte | |
1323 | * termination, allocate an extra byte. | |
1324 | */ | |
1325 | alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0); | |
7ffbd7ca P |
1326 | |
1327 | if (used_len != NULL) | |
1328 | *used_len = sz; | |
1329 | ||
ac1082f0 P |
1330 | if (p->data == NULL) { |
1331 | err_null_argument; | |
86f32187 | 1332 | return 0; |
ac1082f0 | 1333 | } |
46ef075a | 1334 | |
b756626a MC |
1335 | if (val == NULL) |
1336 | return 1; | |
1337 | ||
7ffbd7ca | 1338 | if (*val == NULL) { |
247a1786 | 1339 | char *const q = OPENSSL_malloc(alloc_sz); |
7ffbd7ca | 1340 | |
e077455e | 1341 | if (q == NULL) |
7ffbd7ca P |
1342 | return 0; |
1343 | *val = q; | |
247a1786 | 1344 | *max_len = alloc_sz; |
7ffbd7ca | 1345 | } |
247a1786 | 1346 | |
ac1082f0 P |
1347 | if (*max_len < sz) { |
1348 | err_too_small; | |
7ffbd7ca | 1349 | return 0; |
ac1082f0 | 1350 | } |
7ffbd7ca P |
1351 | memcpy(*val, p->data, sz); |
1352 | return 1; | |
1353 | } | |
1354 | ||
1355 | int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len) | |
1356 | { | |
247a1786 RL |
1357 | int ret = get_string_internal(p, (void **)val, &max_len, NULL, |
1358 | OSSL_PARAM_UTF8_STRING); | |
1359 | ||
1360 | /* | |
1361 | * We try to ensure that the copied string is terminated with a | |
1362 | * NUL byte. That should be easy, just place a NUL byte at | |
1363 | * |((char*)*val)[p->data_size]|. | |
1364 | * Unfortunately, we have seen cases where |p->data_size| doesn't | |
1365 | * correctly reflect the length of the string, and just happens | |
1366 | * to be out of bounds according to |max_len|, so in that case, we | |
1367 | * make the extra step of trying to find the true length of the | |
1368 | * string that |p->data| points at, and use that as an index to | |
1369 | * place the NUL byte in |*val|. | |
1370 | */ | |
1371 | size_t data_length = p->data_size; | |
1372 | ||
1fac2705 TM |
1373 | if (ret == 0) |
1374 | return 0; | |
247a1786 RL |
1375 | if (data_length >= max_len) |
1376 | data_length = OPENSSL_strnlen(p->data, data_length); | |
ac1082f0 P |
1377 | if (data_length >= max_len) { |
1378 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL); | |
247a1786 | 1379 | return 0; /* No space for a terminating NUL byte */ |
ac1082f0 | 1380 | } |
1fac2705 | 1381 | (*val)[data_length] = '\0'; |
247a1786 RL |
1382 | |
1383 | return ret; | |
7ffbd7ca P |
1384 | } |
1385 | ||
1386 | int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len, | |
1387 | size_t *used_len) | |
1388 | { | |
247a1786 | 1389 | return get_string_internal(p, val, &max_len, used_len, |
7ffbd7ca P |
1390 | OSSL_PARAM_OCTET_STRING); |
1391 | } | |
1392 | ||
4e7991b4 | 1393 | static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len, |
7ffbd7ca P |
1394 | unsigned int type) |
1395 | { | |
4e7991b4 | 1396 | p->return_size = len; |
8f3b8fd6 RL |
1397 | if (p->data == NULL) |
1398 | return 1; | |
ac1082f0 P |
1399 | if (p->data_type != type) { |
1400 | err_bad_type; | |
7ffbd7ca | 1401 | return 0; |
ac1082f0 P |
1402 | } |
1403 | if (p->data_size < len) { | |
1404 | err_too_small; | |
1405 | return 0; | |
1406 | } | |
7ffbd7ca P |
1407 | |
1408 | memcpy(p->data, val, len); | |
247a1786 RL |
1409 | /* If possible within the size of p->data, add a NUL terminator byte */ |
1410 | if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len) | |
1411 | ((char *)p->data)[len] = '\0'; | |
7ffbd7ca P |
1412 | return 1; |
1413 | } | |
1414 | ||
4e7991b4 | 1415 | int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val) |
7ffbd7ca | 1416 | { |
ac1082f0 P |
1417 | if (p == NULL) { |
1418 | err_null_argument; | |
7ffbd7ca | 1419 | return 0; |
ac1082f0 | 1420 | } |
7ffbd7ca | 1421 | |
4e7991b4 | 1422 | p->return_size = 0; |
ac1082f0 P |
1423 | if (val == NULL) { |
1424 | err_null_argument; | |
7ffbd7ca | 1425 | return 0; |
ac1082f0 | 1426 | } |
247a1786 | 1427 | return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING); |
7ffbd7ca P |
1428 | } |
1429 | ||
4e7991b4 | 1430 | int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val, |
7ffbd7ca P |
1431 | size_t len) |
1432 | { | |
ac1082f0 P |
1433 | if (p == NULL) { |
1434 | err_null_argument; | |
7ffbd7ca | 1435 | return 0; |
ac1082f0 | 1436 | } |
7ffbd7ca | 1437 | |
4e7991b4 | 1438 | p->return_size = 0; |
ac1082f0 P |
1439 | if (val == NULL) { |
1440 | err_null_argument; | |
7ffbd7ca | 1441 | return 0; |
ac1082f0 | 1442 | } |
7ffbd7ca P |
1443 | return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING); |
1444 | } | |
1445 | ||
1446 | OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf, | |
4e7991b4 | 1447 | size_t bsize) |
7ffbd7ca | 1448 | { |
7f588d20 | 1449 | if (buf != NULL && bsize == 0) |
247a1786 | 1450 | bsize = strlen(buf); |
4e7991b4 | 1451 | return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize); |
7ffbd7ca P |
1452 | } |
1453 | ||
1454 | OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf, | |
4e7991b4 | 1455 | size_t bsize) |
7ffbd7ca | 1456 | { |
4e7991b4 | 1457 | return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize); |
7ffbd7ca P |
1458 | } |
1459 | ||
1460 | static int get_ptr_internal(const OSSL_PARAM *p, const void **val, | |
1461 | size_t *used_len, unsigned int type) | |
1462 | { | |
ac1082f0 P |
1463 | if (val == NULL || p == NULL) { |
1464 | err_null_argument; | |
1465 | return 0; | |
1466 | } | |
1467 | if (p->data_type != type) { | |
1468 | err_bad_type; | |
7ffbd7ca | 1469 | return 0; |
ac1082f0 | 1470 | } |
7ffbd7ca P |
1471 | if (used_len != NULL) |
1472 | *used_len = p->data_size; | |
1473 | *val = *(const void **)p->data; | |
1474 | return 1; | |
1475 | } | |
1476 | ||
1477 | int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val) | |
1478 | { | |
1479 | return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR); | |
1480 | } | |
1481 | ||
1482 | int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val, | |
1483 | size_t *used_len) | |
1484 | { | |
1485 | return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR); | |
1486 | } | |
1487 | ||
4e7991b4 | 1488 | static int set_ptr_internal(OSSL_PARAM *p, const void *val, |
7ffbd7ca P |
1489 | unsigned int type, size_t len) |
1490 | { | |
4e7991b4 | 1491 | p->return_size = len; |
ac1082f0 P |
1492 | if (p->data_type != type) { |
1493 | err_bad_type; | |
7ffbd7ca | 1494 | return 0; |
ac1082f0 | 1495 | } |
8f3b8fd6 RL |
1496 | if (p->data != NULL) |
1497 | *(const void **)p->data = val; | |
7ffbd7ca P |
1498 | return 1; |
1499 | } | |
1500 | ||
4e7991b4 | 1501 | int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val) |
7ffbd7ca | 1502 | { |
ac1082f0 P |
1503 | if (p == NULL) { |
1504 | err_null_argument; | |
7ffbd7ca | 1505 | return 0; |
ac1082f0 | 1506 | } |
4e7991b4 | 1507 | p->return_size = 0; |
b3f3ba70 | 1508 | return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR, |
247a1786 | 1509 | val == NULL ? 0 : strlen(val)); |
7ffbd7ca P |
1510 | } |
1511 | ||
4e7991b4 | 1512 | int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val, |
7ffbd7ca P |
1513 | size_t used_len) |
1514 | { | |
ac1082f0 P |
1515 | if (p == NULL) { |
1516 | err_null_argument; | |
7ffbd7ca | 1517 | return 0; |
ac1082f0 | 1518 | } |
4e7991b4 | 1519 | p->return_size = 0; |
7ffbd7ca P |
1520 | return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len); |
1521 | } | |
1522 | ||
1523 | OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf, | |
4e7991b4 | 1524 | size_t bsize) |
7ffbd7ca | 1525 | { |
4e7991b4 | 1526 | return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize); |
7ffbd7ca P |
1527 | } |
1528 | ||
1529 | OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf, | |
4e7991b4 | 1530 | size_t bsize) |
7ffbd7ca | 1531 | { |
4e7991b4 | 1532 | return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize); |
7ffbd7ca | 1533 | } |
195852fe | 1534 | |
79523d55 P |
1535 | /* |
1536 | * Extract the parameter into an allocated buffer. | |
1537 | * Any existing allocation in *out is cleared and freed. | |
1538 | * | |
1539 | * Returns 1 on success, 0 on failure and -1 if there are no matching params. | |
1540 | * | |
1541 | * *out and *out_len are guaranteed to be untouched if this function | |
1542 | * doesn't return success. | |
1543 | */ | |
1544 | int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name, | |
1545 | unsigned char **out, size_t *out_len) | |
1546 | { | |
1547 | const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name); | |
1548 | void *buf = NULL; | |
1549 | size_t len = 0; | |
1550 | ||
1551 | if (p == NULL) | |
1552 | return -1; | |
1553 | ||
1554 | if (p->data != NULL | |
1555 | && p->data_size > 0 | |
1556 | && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len)) | |
1557 | return 0; | |
1558 | ||
1559 | OPENSSL_clear_free(*out, *out_len); | |
1560 | *out = buf; | |
1561 | *out_len = len; | |
1562 | return 1; | |
1563 | } | |
1564 | ||
1565 | static int setbuf_fromparams(const OSSL_PARAM *p, const char *name, | |
1566 | unsigned char *out, size_t *outlen) | |
1567 | { | |
1568 | int ret = 0; | |
1569 | WPACKET pkt; | |
1570 | ||
1571 | if (out == NULL) { | |
1572 | if (!WPACKET_init_null(&pkt, 0)) | |
1573 | return 0; | |
1574 | } else { | |
1575 | if (!WPACKET_init_static_len(&pkt, out, *outlen, 0)) | |
1576 | return 0; | |
1577 | } | |
1578 | ||
1579 | for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) { | |
1580 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
1581 | goto err; | |
1582 | if (p->data != NULL | |
1583 | && p->data_size != 0 | |
1584 | && !WPACKET_memcpy(&pkt, p->data, p->data_size)) | |
1585 | goto err; | |
1586 | } | |
1587 | if (!WPACKET_get_total_written(&pkt, outlen) | |
1588 | || !WPACKET_finish(&pkt)) | |
1589 | goto err; | |
1590 | ret = 1; | |
1591 | err: | |
1592 | WPACKET_cleanup(&pkt); | |
1593 | return ret; | |
1594 | } | |
1595 | ||
1596 | int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name, | |
1597 | unsigned char **out, | |
1598 | size_t *out_len, size_t maxsize) | |
1599 | { | |
1600 | const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name); | |
1601 | unsigned char *res; | |
1602 | size_t sz = 0; | |
1603 | ||
1604 | if (p == NULL) | |
1605 | return -1; | |
1606 | ||
1607 | /* Calculate the total size */ | |
1608 | if (!setbuf_fromparams(p, name, NULL, &sz)) | |
1609 | return 0; | |
1610 | ||
1611 | /* Check that it's not oversized */ | |
1612 | if (maxsize > 0 && sz > maxsize) | |
1613 | return 0; | |
1614 | ||
1615 | /* Special case zero length */ | |
1616 | if (sz == 0) { | |
1617 | if ((res = OPENSSL_zalloc(1)) == NULL) | |
1618 | return 0; | |
1619 | goto fin; | |
1620 | } | |
1621 | ||
1622 | /* Allocate the buffer */ | |
1623 | res = OPENSSL_malloc(sz); | |
1624 | if (res == NULL) | |
1625 | return 0; | |
1626 | ||
1627 | /* Concat one or more OSSL_KDF_PARAM_INFO fields */ | |
1628 | if (!setbuf_fromparams(p, name, res, &sz)) { | |
1629 | OPENSSL_clear_free(res, sz); | |
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | fin: | |
1634 | OPENSSL_clear_free(*out, *out_len); | |
1635 | *out = res; | |
1636 | *out_len = sz; | |
1637 | return 1; | |
1638 | } | |
1639 | ||
195852fe RL |
1640 | OSSL_PARAM OSSL_PARAM_construct_end(void) |
1641 | { | |
1642 | OSSL_PARAM end = OSSL_PARAM_END; | |
1643 | ||
1644 | return end; | |
1645 | } | |
ab00ddb5 RL |
1646 | |
1647 | static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val, | |
1648 | size_t *used_len, unsigned int type) | |
1649 | { | |
ac1082f0 P |
1650 | if (val == NULL || p == NULL) { |
1651 | err_null_argument; | |
ab00ddb5 | 1652 | return 0; |
ac1082f0 P |
1653 | } |
1654 | if (p->data_type != type) { | |
1655 | err_bad_type; | |
1656 | return 0; | |
1657 | } | |
ab00ddb5 RL |
1658 | if (used_len != NULL) |
1659 | *used_len = p->data_size; | |
1660 | *val = p->data; | |
1661 | return 1; | |
1662 | } | |
1663 | ||
1664 | int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val) | |
1665 | { | |
327a720d TM |
1666 | int rv; |
1667 | ||
1668 | ERR_set_mark(); | |
1669 | rv = OSSL_PARAM_get_utf8_ptr(p, val); | |
1670 | ERR_pop_to_mark(); | |
1671 | ||
1672 | return rv || get_string_ptr_internal(p, (const void **)val, NULL, | |
1673 | OSSL_PARAM_UTF8_STRING); | |
ab00ddb5 RL |
1674 | } |
1675 | ||
1676 | int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val, | |
1677 | size_t *used_len) | |
1678 | { | |
327a720d TM |
1679 | int rv; |
1680 | ||
1681 | ERR_set_mark(); | |
1682 | rv = OSSL_PARAM_get_octet_ptr(p, val, used_len); | |
1683 | ERR_pop_to_mark(); | |
1684 | ||
1685 | return rv || get_string_ptr_internal(p, val, used_len, | |
1686 | OSSL_PARAM_OCTET_STRING); | |
ab00ddb5 | 1687 | } |