2 * Copyright 2019-2023 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/params.h>
13 #include <openssl/err.h>
14 #include "internal/thread_once.h"
15 #include "internal/numbers.h"
16 #include "internal/endian.h"
18 /* Shortcuts for raising errors that are widely used */
19 #define err_unsigned_negative \
20 ERR_raise(ERR_LIB_CRYPTO, \
21 CRYPTO_R_PARAM_UNSIGNED_INTEGER_NEGATIVE_VALUE_UNSUPPORTED)
22 #define err_out_of_range \
23 ERR_raise(ERR_LIB_CRYPTO, \
24 CRYPTO_R_PARAM_VALUE_TOO_LARGE_FOR_DESTINATION)
26 ERR_raise(ERR_LIB_CRYPTO, \
27 CRYPTO_R_PARAM_CANNOT_BE_REPRESENTED_EXACTLY)
28 #define err_not_integer \
29 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_NOT_INTEGER_TYPE)
30 #define err_too_small \
31 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER)
32 #define err_bad_type \
33 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_OF_INCOMPATIBLE_TYPE)
34 #define err_null_argument \
35 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER)
36 #define err_unsupported_real \
37 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_UNSUPPORTED_FLOATING_POINT_FORMAT)
39 #ifndef OPENSSL_SYS_UEFI
41 * Return the number of bits in the mantissa of a double. This is used to
42 * shift a larger integral value to determine if it will exactly fit into a
45 static unsigned int real_shift(void)
47 return sizeof(double) == 4 ? 24 : 53;
51 OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
53 if (p != NULL && key != NULL)
54 for (; p->key != NULL; p++)
55 if (strcmp(key, p->key) == 0)
60 const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
62 return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
65 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
66 void *data, size_t data_size)
71 res.data_type = data_type;
73 res.data_size = data_size;
74 res.return_size = OSSL_PARAM_UNMODIFIED;
78 int OSSL_PARAM_modified(const OSSL_PARAM *p)
80 return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
83 void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
86 while (p->key != NULL)
87 p++->return_size = OSSL_PARAM_UNMODIFIED;
90 /* Return non-zero if the signed number is negative */
91 static int is_negative(const void *number, size_t s)
93 const unsigned char *n = number;
96 return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
99 /* Check that all the bytes specified match the expected sign byte */
100 static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
104 for (i = 0; i < n; i++)
111 * Copy an integer to another integer.
112 * Handle different length integers and signed and unsigned integers.
113 * Both integers are in native byte ordering.
115 static int copy_integer(unsigned char *dest, size_t dest_len,
116 const unsigned char *src, size_t src_len,
117 unsigned char pad, int signed_int)
123 if (src_len < dest_len) {
124 n = dest_len - src_len;
125 memset(dest, pad, n);
126 memcpy(dest + n, src, src_len);
128 n = src_len - dest_len;
129 if (!check_sign_bytes(src, n, pad)
131 * Shortening a signed value must retain the correct sign.
132 * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
134 || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
138 memcpy(dest, src + n, dest_len);
140 } else /* IS_LITTLE_ENDIAN */ {
141 if (src_len < dest_len) {
142 n = dest_len - src_len;
143 memset(dest + src_len, pad, n);
144 memcpy(dest, src, src_len);
146 n = src_len - dest_len;
147 if (!check_sign_bytes(src + dest_len, n, pad)
149 * Shortening a signed value must retain the correct sign.
150 * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
152 || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
156 memcpy(dest, src, dest_len);
162 /* Copy a signed number to a signed number of possibly different length */
163 static int signed_from_signed(void *dest, size_t dest_len,
164 const void *src, size_t src_len)
166 return copy_integer(dest, dest_len, src, src_len,
167 is_negative(src, src_len) ? 0xff : 0, 1);
170 /* Copy an unsigned number to a signed number of possibly different length */
171 static int signed_from_unsigned(void *dest, size_t dest_len,
172 const void *src, size_t src_len)
174 return copy_integer(dest, dest_len, src, src_len, 0, 1);
177 /* Copy a signed number to an unsigned number of possibly different length */
178 static int unsigned_from_signed(void *dest, size_t dest_len,
179 const void *src, size_t src_len)
181 if (is_negative(src, src_len)) {
182 err_unsigned_negative;
185 return copy_integer(dest, dest_len, src, src_len, 0, 0);
188 /* Copy an unsigned number to an unsigned number of possibly different length */
189 static int unsigned_from_unsigned(void *dest, size_t dest_len,
190 const void *src, size_t src_len)
192 return copy_integer(dest, dest_len, src, src_len, 0, 0);
195 /* General purpose get integer parameter call that handles odd sizes */
196 static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
198 if (p->data_type == OSSL_PARAM_INTEGER)
199 return signed_from_signed(val, val_size, p->data, p->data_size);
200 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
201 return signed_from_unsigned(val, val_size, p->data, p->data_size);
206 /* General purpose set integer parameter call that handles odd sizes */
207 static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
211 p->return_size = val_size; /* Expected size */
214 if (p->data_type == OSSL_PARAM_INTEGER)
215 r = signed_from_signed(p->data, p->data_size, val, val_size);
216 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
217 r = unsigned_from_signed(p->data, p->data_size, val, val_size);
220 p->return_size = r ? p->data_size : val_size;
224 /* General purpose get unsigned integer parameter call that handles odd sizes */
225 static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
227 if (p->data_type == OSSL_PARAM_INTEGER)
228 return unsigned_from_signed(val, val_size, p->data, p->data_size);
229 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
230 return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
235 /* General purpose set unsigned integer parameter call that handles odd sizes */
236 static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
240 p->return_size = val_size; /* Expected size */
243 if (p->data_type == OSSL_PARAM_INTEGER)
244 r = signed_from_unsigned(p->data, p->data_size, val, val_size);
245 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
246 r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
249 p->return_size = r ? p->data_size : val_size;
253 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
255 #ifndef OPENSSL_SMALL_FOOTPRINT
256 switch (sizeof(int)) {
257 case sizeof(int32_t):
258 return OSSL_PARAM_get_int32(p, (int32_t *)val);
259 case sizeof(int64_t):
260 return OSSL_PARAM_get_int64(p, (int64_t *)val);
263 return general_get_int(p, val, sizeof(*val));
266 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
268 #ifndef OPENSSL_SMALL_FOOTPRINT
269 switch (sizeof(int)) {
270 case sizeof(int32_t):
271 return OSSL_PARAM_set_int32(p, (int32_t)val);
272 case sizeof(int64_t):
273 return OSSL_PARAM_set_int64(p, (int64_t)val);
276 return general_set_int(p, &val, sizeof(val));
279 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
281 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
284 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
286 #ifndef OPENSSL_SMALL_FOOTPRINT
287 switch (sizeof(unsigned int)) {
288 case sizeof(uint32_t):
289 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
290 case sizeof(uint64_t):
291 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
294 return general_get_uint(p, val, sizeof(*val));
297 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
299 #ifndef OPENSSL_SMALL_FOOTPRINT
300 switch (sizeof(unsigned int)) {
301 case sizeof(uint32_t):
302 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
303 case sizeof(uint64_t):
304 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
307 return general_set_uint(p, &val, sizeof(val));
310 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
312 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
313 sizeof(unsigned int));
316 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
318 #ifndef OPENSSL_SMALL_FOOTPRINT
319 switch (sizeof(long int)) {
320 case sizeof(int32_t):
321 return OSSL_PARAM_get_int32(p, (int32_t *)val);
322 case sizeof(int64_t):
323 return OSSL_PARAM_get_int64(p, (int64_t *)val);
326 return general_get_int(p, val, sizeof(*val));
329 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
331 #ifndef OPENSSL_SMALL_FOOTPRINT
332 switch (sizeof(long int)) {
333 case sizeof(int32_t):
334 return OSSL_PARAM_set_int32(p, (int32_t)val);
335 case sizeof(int64_t):
336 return OSSL_PARAM_set_int64(p, (int64_t)val);
339 return general_set_int(p, &val, sizeof(val));
342 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
344 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
347 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
349 #ifndef OPENSSL_SMALL_FOOTPRINT
350 switch (sizeof(unsigned long int)) {
351 case sizeof(uint32_t):
352 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
353 case sizeof(uint64_t):
354 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
357 return general_get_uint(p, val, sizeof(*val));
360 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
362 #ifndef OPENSSL_SMALL_FOOTPRINT
363 switch (sizeof(unsigned long int)) {
364 case sizeof(uint32_t):
365 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
366 case sizeof(uint64_t):
367 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
370 return general_set_uint(p, &val, sizeof(val));
373 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
375 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
376 sizeof(unsigned long int));
379 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
381 if (val == NULL || p == NULL) {
386 if (p->data_type == OSSL_PARAM_INTEGER) {
387 #ifndef OPENSSL_SMALL_FOOTPRINT
390 switch (p->data_size) {
391 case sizeof(int32_t):
392 *val = *(const int32_t *)p->data;
394 case sizeof(int64_t):
395 i64 = *(const int64_t *)p->data;
396 if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
404 return general_get_int(p, val, sizeof(*val));
406 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
407 #ifndef OPENSSL_SMALL_FOOTPRINT
411 switch (p->data_size) {
412 case sizeof(uint32_t):
413 u32 = *(const uint32_t *)p->data;
414 if (u32 <= INT32_MAX) {
420 case sizeof(uint64_t):
421 u64 = *(const uint64_t *)p->data;
422 if (u64 <= INT32_MAX) {
430 return general_get_int(p, val, sizeof(*val));
432 } else if (p->data_type == OSSL_PARAM_REAL) {
433 #ifndef OPENSSL_SYS_UEFI
436 switch (p->data_size) {
438 d = *(const double *)p->data;
439 if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
446 err_unsupported_real;
454 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
464 if (p->data_type == OSSL_PARAM_INTEGER) {
465 #ifndef OPENSSL_SMALL_FOOTPRINT
466 p->return_size = sizeof(int32_t); /* Minimum expected size */
469 switch (p->data_size) {
470 case sizeof(int32_t):
471 *(int32_t *)p->data = val;
473 case sizeof(int64_t):
474 p->return_size = sizeof(int64_t);
475 *(int64_t *)p->data = (int64_t)val;
479 return general_set_int(p, &val, sizeof(val));
480 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
481 #ifndef OPENSSL_SMALL_FOOTPRINT
482 p->return_size = sizeof(uint32_t); /* Minimum expected size */
485 switch (p->data_size) {
486 case sizeof(uint32_t):
487 *(uint32_t *)p->data = (uint32_t)val;
489 case sizeof(uint64_t):
490 p->return_size = sizeof(uint64_t);
491 *(uint64_t *)p->data = (uint64_t)val;
495 return general_set_int(p, &val, sizeof(val));
496 } else if (p->data_type == OSSL_PARAM_REAL) {
497 #ifndef OPENSSL_SYS_UEFI
498 p->return_size = sizeof(double);
501 switch (p->data_size) {
503 shift = real_shift();
504 if (shift < 8 * sizeof(val) - 1) {
505 u32 = val < 0 ? -val : val;
506 if ((u32 >> shift) != 0) {
511 *(double *)p->data = (double)val;
514 err_unsupported_real;
522 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
524 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
528 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
530 if (val == NULL || p == NULL) {
535 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
536 #ifndef OPENSSL_SMALL_FOOTPRINT
539 switch (p->data_size) {
540 case sizeof(uint32_t):
541 *val = *(const uint32_t *)p->data;
543 case sizeof(uint64_t):
544 u64 = *(const uint64_t *)p->data;
545 if (u64 <= UINT32_MAX) {
546 *val = (uint32_t)u64;
553 return general_get_uint(p, val, sizeof(*val));
554 } else if (p->data_type == OSSL_PARAM_INTEGER) {
555 #ifndef OPENSSL_SMALL_FOOTPRINT
559 switch (p->data_size) {
560 case sizeof(int32_t):
561 i32 = *(const int32_t *)p->data;
566 err_unsigned_negative;
568 case sizeof(int64_t):
569 i64 = *(const int64_t *)p->data;
570 if (i64 >= 0 && i64 <= UINT32_MAX) {
571 *val = (uint32_t)i64;
575 err_unsigned_negative;
581 return general_get_uint(p, val, sizeof(*val));
582 } else if (p->data_type == OSSL_PARAM_REAL) {
583 #ifndef OPENSSL_SYS_UEFI
586 switch (p->data_size) {
588 d = *(const double *)p->data;
589 if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
596 err_unsupported_real;
604 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
614 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
615 #ifndef OPENSSL_SMALL_FOOTPRINT
616 p->return_size = sizeof(uint32_t); /* Minimum expected size */
619 switch (p->data_size) {
620 case sizeof(uint32_t):
621 *(uint32_t *)p->data = val;
623 case sizeof(uint64_t):
624 p->return_size = sizeof(uint64_t);
625 *(uint64_t *)p->data = val;
629 return general_set_uint(p, &val, sizeof(val));
630 } else if (p->data_type == OSSL_PARAM_INTEGER) {
631 #ifndef OPENSSL_SMALL_FOOTPRINT
632 p->return_size = sizeof(int32_t); /* Minimum expected size */
635 switch (p->data_size) {
636 case sizeof(int32_t):
637 if (val <= INT32_MAX) {
638 *(int32_t *)p->data = (int32_t)val;
643 case sizeof(int64_t):
644 p->return_size = sizeof(int64_t);
645 *(int64_t *)p->data = (int64_t)val;
649 return general_set_uint(p, &val, sizeof(val));
650 } else if (p->data_type == OSSL_PARAM_REAL) {
651 #ifndef OPENSSL_SYS_UEFI
652 p->return_size = sizeof(double);
655 switch (p->data_size) {
657 shift = real_shift();
658 if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
662 *(double *)p->data = (double)val;
665 err_unsupported_real;
673 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
675 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
679 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
681 if (val == NULL || p == NULL) {
686 if (p->data_type == OSSL_PARAM_INTEGER) {
687 #ifndef OPENSSL_SMALL_FOOTPRINT
688 switch (p->data_size) {
689 case sizeof(int32_t):
690 *val = *(const int32_t *)p->data;
692 case sizeof(int64_t):
693 *val = *(const int64_t *)p->data;
697 return general_get_int(p, val, sizeof(*val));
698 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
699 #ifndef OPENSSL_SMALL_FOOTPRINT
702 switch (p->data_size) {
703 case sizeof(uint32_t):
704 *val = *(const uint32_t *)p->data;
706 case sizeof(uint64_t):
707 u64 = *(const uint64_t *)p->data;
708 if (u64 <= INT64_MAX) {
716 return general_get_int(p, val, sizeof(*val));
717 } else if (p->data_type == OSSL_PARAM_REAL) {
718 #ifndef OPENSSL_SYS_UEFI
721 switch (p->data_size) {
723 d = *(const double *)p->data;
726 * By subtracting 65535 (2^16-1) we cancel the low order
727 * 15 bits of INT64_MAX to avoid using imprecise floating
730 && d < (double)(INT64_MAX - 65535) + 65536.0
731 && d == (int64_t)d) {
738 err_unsupported_real;
746 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
753 if (p->data_type == OSSL_PARAM_INTEGER) {
754 #ifndef OPENSSL_SMALL_FOOTPRINT
755 p->return_size = sizeof(int64_t); /* Expected size */
758 switch (p->data_size) {
759 case sizeof(int32_t):
760 if (val >= INT32_MIN && val <= INT32_MAX) {
761 p->return_size = sizeof(int32_t);
762 *(int32_t *)p->data = (int32_t)val;
767 case sizeof(int64_t):
768 *(int64_t *)p->data = val;
772 return general_set_int(p, &val, sizeof(val));
773 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
774 #ifndef OPENSSL_SMALL_FOOTPRINT
775 p->return_size = sizeof(uint64_t); /* Expected size */
778 switch (p->data_size) {
779 case sizeof(uint32_t):
780 if (val <= UINT32_MAX) {
781 p->return_size = sizeof(uint32_t);
782 *(uint32_t *)p->data = (uint32_t)val;
787 case sizeof(uint64_t):
788 *(uint64_t *)p->data = (uint64_t)val;
792 return general_set_int(p, &val, sizeof(val));
793 } else if (p->data_type == OSSL_PARAM_REAL) {
794 #ifndef OPENSSL_SYS_UEFI
797 p->return_size = sizeof(double);
800 switch (p->data_size) {
802 u64 = val < 0 ? -val : val;
803 if ((u64 >> real_shift()) == 0) {
804 *(double *)p->data = (double)val;
810 err_unsupported_real;
818 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
820 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
823 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
825 if (val == NULL || p == NULL) {
830 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
831 #ifndef OPENSSL_SMALL_FOOTPRINT
832 switch (p->data_size) {
833 case sizeof(uint32_t):
834 *val = *(const uint32_t *)p->data;
836 case sizeof(uint64_t):
837 *val = *(const uint64_t *)p->data;
841 return general_get_uint(p, val, sizeof(*val));
842 } else if (p->data_type == OSSL_PARAM_INTEGER) {
843 #ifndef OPENSSL_SMALL_FOOTPRINT
847 switch (p->data_size) {
848 case sizeof(int32_t):
849 i32 = *(const int32_t *)p->data;
851 *val = (uint64_t)i32;
854 err_unsigned_negative;
856 case sizeof(int64_t):
857 i64 = *(const int64_t *)p->data;
859 *val = (uint64_t)i64;
862 err_unsigned_negative;
866 return general_get_uint(p, val, sizeof(*val));
867 } else if (p->data_type == OSSL_PARAM_REAL) {
868 #ifndef OPENSSL_SYS_UEFI
871 switch (p->data_size) {
873 d = *(const double *)p->data;
876 * By subtracting 65535 (2^16-1) we cancel the low order
877 * 15 bits of UINT64_MAX to avoid using imprecise floating
880 && d < (double)(UINT64_MAX - 65535) + 65536.0
881 && d == (uint64_t)d) {
888 err_unsupported_real;
896 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
904 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
905 #ifndef OPENSSL_SMALL_FOOTPRINT
906 p->return_size = sizeof(uint64_t); /* Expected size */
909 switch (p->data_size) {
910 case sizeof(uint32_t):
911 if (val <= UINT32_MAX) {
912 p->return_size = sizeof(uint32_t);
913 *(uint32_t *)p->data = (uint32_t)val;
918 case sizeof(uint64_t):
919 *(uint64_t *)p->data = val;
923 return general_set_uint(p, &val, sizeof(val));
924 } else if (p->data_type == OSSL_PARAM_INTEGER) {
925 #ifndef OPENSSL_SMALL_FOOTPRINT
926 p->return_size = sizeof(int64_t); /* Expected size */
929 switch (p->data_size) {
930 case sizeof(int32_t):
931 if (val <= INT32_MAX) {
932 p->return_size = sizeof(int32_t);
933 *(int32_t *)p->data = (int32_t)val;
938 case sizeof(int64_t):
939 if (val <= INT64_MAX) {
940 *(int64_t *)p->data = (int64_t)val;
947 return general_set_uint(p, &val, sizeof(val));
948 } else if (p->data_type == OSSL_PARAM_REAL) {
949 #ifndef OPENSSL_SYS_UEFI
950 p->return_size = sizeof(double);
951 switch (p->data_size) {
953 if ((val >> real_shift()) == 0) {
954 *(double *)p->data = (double)val;
960 err_unsupported_real;
968 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
970 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
974 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
976 #ifndef OPENSSL_SMALL_FOOTPRINT
977 switch (sizeof(size_t)) {
978 case sizeof(uint32_t):
979 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
980 case sizeof(uint64_t):
981 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
984 return general_get_uint(p, val, sizeof(*val));
987 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
989 #ifndef OPENSSL_SMALL_FOOTPRINT
990 switch (sizeof(size_t)) {
991 case sizeof(uint32_t):
992 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
993 case sizeof(uint64_t):
994 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
997 return general_set_uint(p, &val, sizeof(val));
1000 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1002 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1006 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1008 #ifndef OPENSSL_SMALL_FOOTPRINT
1009 switch (sizeof(time_t)) {
1010 case sizeof(int32_t):
1011 return OSSL_PARAM_get_int32(p, (int32_t *)val);
1012 case sizeof(int64_t):
1013 return OSSL_PARAM_get_int64(p, (int64_t *)val);
1016 return general_get_int(p, val, sizeof(*val));
1019 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1021 #ifndef OPENSSL_SMALL_FOOTPRINT
1022 switch (sizeof(time_t)) {
1023 case sizeof(int32_t):
1024 return OSSL_PARAM_set_int32(p, (int32_t)val);
1025 case sizeof(int64_t):
1026 return OSSL_PARAM_set_int64(p, (int64_t)val);
1029 return general_set_int(p, &val, sizeof(val));
1032 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1034 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1037 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1041 if (val == NULL || p == NULL) {
1045 if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
1050 b = BN_native2bn(p->data, (int)p->data_size, *val);
1055 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1059 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1072 if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
1077 /* For the moment, only positive values are permitted */
1078 if (BN_is_negative(val)) {
1079 err_unsigned_negative;
1083 bytes = (size_t)BN_num_bytes(val);
1084 /* We make sure that at least one byte is used, so zero is properly set */
1088 p->return_size = bytes;
1089 if (p->data == NULL)
1091 if (p->data_size >= bytes) {
1092 p->return_size = p->data_size;
1093 if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1095 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1102 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1105 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1109 #ifndef OPENSSL_SYS_UEFI
1110 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1115 if (val == NULL || p == NULL) {
1120 if (p->data_type == OSSL_PARAM_REAL) {
1121 switch (p->data_size) {
1122 case sizeof(double):
1123 *val = *(const double *)p->data;
1126 err_unsupported_real;
1128 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1129 switch (p->data_size) {
1130 case sizeof(uint32_t):
1131 *val = *(const uint32_t *)p->data;
1133 case sizeof(uint64_t):
1134 u64 = *(const uint64_t *)p->data;
1135 if ((u64 >> real_shift()) == 0) {
1142 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1143 switch (p->data_size) {
1144 case sizeof(int32_t):
1145 *val = *(const int32_t *)p->data;
1147 case sizeof(int64_t):
1148 i64 = *(const int64_t *)p->data;
1149 u64 = i64 < 0 ? -i64 : i64;
1150 if ((u64 >> real_shift()) == 0) {
1162 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1170 if (p->data_type == OSSL_PARAM_REAL) {
1171 p->return_size = sizeof(double);
1172 if (p->data == NULL)
1174 switch (p->data_size) {
1175 case sizeof(double):
1176 *(double *)p->data = val;
1179 err_unsupported_real;
1181 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1182 p->return_size = sizeof(double);
1183 if (p->data == NULL)
1185 if (val != (uint64_t)val) {
1189 switch (p->data_size) {
1190 case sizeof(uint32_t):
1191 if (val >= 0 && val <= UINT32_MAX) {
1192 p->return_size = sizeof(uint32_t);
1193 *(uint32_t *)p->data = (uint32_t)val;
1198 case sizeof(uint64_t):
1201 * By subtracting 65535 (2^16-1) we cancel the low order
1202 * 15 bits of UINT64_MAX to avoid using imprecise floating
1205 && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1206 p->return_size = sizeof(uint64_t);
1207 *(uint64_t *)p->data = (uint64_t)val;
1213 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1214 p->return_size = sizeof(double);
1215 if (p->data == NULL)
1217 if (val != (int64_t)val) {
1221 switch (p->data_size) {
1222 case sizeof(int32_t):
1223 if (val >= INT32_MIN && val <= INT32_MAX) {
1224 p->return_size = sizeof(int32_t);
1225 *(int32_t *)p->data = (int32_t)val;
1230 case sizeof(int64_t):
1231 if (val >= INT64_MIN
1233 * By subtracting 65535 (2^16-1) we cancel the low order
1234 * 15 bits of INT64_MAX to avoid using imprecise floating
1237 && val < (double)(INT64_MAX - 65535) + 65536.0) {
1238 p->return_size = sizeof(int64_t);
1239 *(int64_t *)p->data = (int64_t)val;
1250 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1252 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1256 static int get_string_internal(const OSSL_PARAM *p, void **val,
1257 size_t *max_len, size_t *used_len,
1260 size_t sz, alloc_sz;
1262 if ((val == NULL && used_len == NULL) || p == NULL) {
1266 if (p->data_type != type) {
1273 * If the input size is 0, or the input string needs NUL byte
1274 * termination, allocate an extra byte.
1276 alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1278 if (used_len != NULL)
1281 if (p->data == NULL) {
1290 char *const q = OPENSSL_malloc(alloc_sz);
1293 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1297 *max_len = alloc_sz;
1300 if (*max_len < sz) {
1304 memcpy(*val, p->data, sz);
1308 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1310 int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1311 OSSL_PARAM_UTF8_STRING);
1314 * We try to ensure that the copied string is terminated with a
1315 * NUL byte. That should be easy, just place a NUL byte at
1316 * |((char*)*val)[p->data_size]|.
1317 * Unfortunately, we have seen cases where |p->data_size| doesn't
1318 * correctly reflect the length of the string, and just happens
1319 * to be out of bounds according to |max_len|, so in that case, we
1320 * make the extra step of trying to find the true length of the
1321 * string that |p->data| points at, and use that as an index to
1322 * place the NUL byte in |*val|.
1324 size_t data_length = p->data_size;
1328 if (data_length >= max_len)
1329 data_length = OPENSSL_strnlen(p->data, data_length);
1330 if (data_length >= max_len) {
1331 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1332 return 0; /* No space for a terminating NUL byte */
1334 (*val)[data_length] = '\0';
1339 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1342 return get_string_internal(p, val, &max_len, used_len,
1343 OSSL_PARAM_OCTET_STRING);
1346 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1349 p->return_size = len;
1350 if (p->data == NULL)
1352 if (p->data_type != type) {
1356 if (p->data_size < len) {
1361 memcpy(p->data, val, len);
1362 /* If possible within the size of p->data, add a NUL terminator byte */
1363 if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1364 ((char *)p->data)[len] = '\0';
1368 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1380 return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1383 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1396 return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1399 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1402 if (buf != NULL && bsize == 0)
1403 bsize = strlen(buf);
1404 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1407 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1410 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1413 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1414 size_t *used_len, unsigned int type)
1416 if (val == NULL || p == NULL) {
1420 if (p->data_type != type) {
1424 if (used_len != NULL)
1425 *used_len = p->data_size;
1426 *val = *(const void **)p->data;
1430 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1432 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1435 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1438 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1441 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1442 unsigned int type, size_t len)
1444 p->return_size = len;
1445 if (p->data_type != type) {
1449 if (p->data != NULL)
1450 *(const void **)p->data = val;
1454 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1461 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1462 val == NULL ? 0 : strlen(val));
1465 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1473 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1476 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1479 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1482 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1485 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1488 OSSL_PARAM OSSL_PARAM_construct_end(void)
1490 OSSL_PARAM end = OSSL_PARAM_END;
1495 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1496 size_t *used_len, unsigned int type)
1498 if (val == NULL || p == NULL) {
1502 if (p->data_type != type) {
1506 if (used_len != NULL)
1507 *used_len = p->data_size;
1512 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1517 rv = OSSL_PARAM_get_utf8_ptr(p, val);
1520 return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1521 OSSL_PARAM_UTF8_STRING);
1524 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1530 rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1533 return rv || get_string_ptr_internal(p, val, used_len,
1534 OSSL_PARAM_OCTET_STRING);