2 * Copyright 2019-2022 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)
40 * Return the number of bits in the mantissa of a double. This is used to
41 * shift a larger integral value to determine if it will exactly fit into a
44 static unsigned int real_shift(void)
46 return sizeof(double) == 4 ? 24 : 53;
49 OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
51 if (p != NULL && key != NULL)
52 for (; p->key != NULL; p++)
53 if (strcmp(key, p->key) == 0)
58 const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
60 return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
63 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
64 void *data, size_t data_size)
69 res.data_type = data_type;
71 res.data_size = data_size;
72 res.return_size = OSSL_PARAM_UNMODIFIED;
76 int OSSL_PARAM_modified(const OSSL_PARAM *p)
78 return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
81 void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
84 while (p->key != NULL)
85 p++->return_size = OSSL_PARAM_UNMODIFIED;
88 /* Return non-zero if the signed number is negative */
89 static int is_negative(const void *number, size_t s)
91 const unsigned char *n = number;
94 return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
97 /* Check that all the bytes specified match the expected sign byte */
98 static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
102 for (i = 0; i < n; i++)
109 * Copy an integer to another integer.
110 * Handle different length integers and signed and unsigned integers.
111 * Both integers are in native byte ordering.
113 static int copy_integer(unsigned char *dest, size_t dest_len,
114 const unsigned char *src, size_t src_len,
115 unsigned char pad, int signed_int)
121 if (src_len < dest_len) {
122 n = dest_len - src_len;
123 memset(dest, pad, n);
124 memcpy(dest + n, src, src_len);
126 n = src_len - dest_len;
127 if (!check_sign_bytes(src, n, pad)
129 * Shortening a signed value must retain the correct sign.
130 * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
132 || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
136 memcpy(dest, src + n, dest_len);
138 } else /* IS_LITTLE_ENDIAN */ {
139 if (src_len < dest_len) {
140 n = dest_len - src_len;
141 memset(dest + src_len, pad, n);
142 memcpy(dest, src, src_len);
144 n = src_len - dest_len;
145 if (!check_sign_bytes(src + dest_len, n, pad)
147 * Shortening a signed value must retain the correct sign.
148 * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
150 || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
154 memcpy(dest, src, dest_len);
160 /* Copy a signed number to a signed number of possibly different length */
161 static int signed_from_signed(void *dest, size_t dest_len,
162 const void *src, size_t src_len)
164 return copy_integer(dest, dest_len, src, src_len,
165 is_negative(src, src_len) ? 0xff : 0, 1);
168 /* Copy an unsigned number to a signed number of possibly different length */
169 static int signed_from_unsigned(void *dest, size_t dest_len,
170 const void *src, size_t src_len)
172 return copy_integer(dest, dest_len, src, src_len, 0, 1);
175 /* Copy a signed number to an unsigned number of possibly different length */
176 static int unsigned_from_signed(void *dest, size_t dest_len,
177 const void *src, size_t src_len)
179 if (is_negative(src, src_len)) {
180 err_unsigned_negative;
183 return copy_integer(dest, dest_len, src, src_len, 0, 0);
186 /* Copy an unsigned number to an unsigned number of possibly different length */
187 static int unsigned_from_unsigned(void *dest, size_t dest_len,
188 const void *src, size_t src_len)
190 return copy_integer(dest, dest_len, src, src_len, 0, 0);
193 /* General purpose get integer parameter call that handles odd sizes */
194 static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
196 if (p->data_type == OSSL_PARAM_INTEGER)
197 return signed_from_signed(val, val_size, p->data, p->data_size);
198 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
199 return signed_from_unsigned(val, val_size, p->data, p->data_size);
204 /* General purpose set integer parameter call that handles odd sizes */
205 static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
209 p->return_size = val_size; /* Expected size */
212 if (p->data_type == OSSL_PARAM_INTEGER)
213 r = signed_from_signed(p->data, p->data_size, val, val_size);
214 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
215 r = unsigned_from_signed(p->data, p->data_size, val, val_size);
218 p->return_size = r ? p->data_size : val_size;
222 /* General purpose get unsigned integer parameter call that handles odd sizes */
223 static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
225 if (p->data_type == OSSL_PARAM_INTEGER)
226 return unsigned_from_signed(val, val_size, p->data, p->data_size);
227 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
228 return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
233 /* General purpose set unsigned integer parameter call that handles odd sizes */
234 static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
238 p->return_size = val_size; /* Expected size */
241 if (p->data_type == OSSL_PARAM_INTEGER)
242 r = signed_from_unsigned(p->data, p->data_size, val, val_size);
243 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
244 r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
247 p->return_size = r ? p->data_size : val_size;
251 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
253 #ifndef OPENSSL_SMALL_FOOTPRINT
254 switch (sizeof(int)) {
255 case sizeof(int32_t):
256 return OSSL_PARAM_get_int32(p, (int32_t *)val);
257 case sizeof(int64_t):
258 return OSSL_PARAM_get_int64(p, (int64_t *)val);
261 return general_get_int(p, val, sizeof(*val));
264 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
266 #ifndef OPENSSL_SMALL_FOOTPRINT
267 switch (sizeof(int)) {
268 case sizeof(int32_t):
269 return OSSL_PARAM_set_int32(p, (int32_t)val);
270 case sizeof(int64_t):
271 return OSSL_PARAM_set_int64(p, (int64_t)val);
274 return general_set_int(p, &val, sizeof(val));
277 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
279 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
282 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
284 #ifndef OPENSSL_SMALL_FOOTPRINT
285 switch (sizeof(unsigned int)) {
286 case sizeof(uint32_t):
287 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
288 case sizeof(uint64_t):
289 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
292 return general_get_uint(p, val, sizeof(*val));
295 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
297 #ifndef OPENSSL_SMALL_FOOTPRINT
298 switch (sizeof(unsigned int)) {
299 case sizeof(uint32_t):
300 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
301 case sizeof(uint64_t):
302 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
305 return general_set_uint(p, &val, sizeof(val));
308 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
310 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
311 sizeof(unsigned int));
314 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
316 #ifndef OPENSSL_SMALL_FOOTPRINT
317 switch (sizeof(long int)) {
318 case sizeof(int32_t):
319 return OSSL_PARAM_get_int32(p, (int32_t *)val);
320 case sizeof(int64_t):
321 return OSSL_PARAM_get_int64(p, (int64_t *)val);
324 return general_get_int(p, val, sizeof(*val));
327 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
329 #ifndef OPENSSL_SMALL_FOOTPRINT
330 switch (sizeof(long int)) {
331 case sizeof(int32_t):
332 return OSSL_PARAM_set_int32(p, (int32_t)val);
333 case sizeof(int64_t):
334 return OSSL_PARAM_set_int64(p, (int64_t)val);
337 return general_set_int(p, &val, sizeof(val));
340 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
342 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
345 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
347 #ifndef OPENSSL_SMALL_FOOTPRINT
348 switch (sizeof(unsigned long int)) {
349 case sizeof(uint32_t):
350 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
351 case sizeof(uint64_t):
352 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
355 return general_get_uint(p, val, sizeof(*val));
358 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
360 #ifndef OPENSSL_SMALL_FOOTPRINT
361 switch (sizeof(unsigned long int)) {
362 case sizeof(uint32_t):
363 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
364 case sizeof(uint64_t):
365 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
368 return general_set_uint(p, &val, sizeof(val));
371 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
373 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
374 sizeof(unsigned long int));
377 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 switch (p->data_size) {
435 d = *(const double *)p->data;
436 if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
443 err_unsupported_real;
450 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
460 if (p->data_type == OSSL_PARAM_INTEGER) {
461 #ifndef OPENSSL_SMALL_FOOTPRINT
462 p->return_size = sizeof(int32_t); /* Minimum expected size */
465 switch (p->data_size) {
466 case sizeof(int32_t):
467 *(int32_t *)p->data = val;
469 case sizeof(int64_t):
470 p->return_size = sizeof(int64_t);
471 *(int64_t *)p->data = (int64_t)val;
475 return general_set_int(p, &val, sizeof(val));
476 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
477 #ifndef OPENSSL_SMALL_FOOTPRINT
478 p->return_size = sizeof(uint32_t); /* Minimum expected size */
481 switch (p->data_size) {
482 case sizeof(uint32_t):
483 *(uint32_t *)p->data = (uint32_t)val;
485 case sizeof(uint64_t):
486 p->return_size = sizeof(uint64_t);
487 *(uint64_t *)p->data = (uint64_t)val;
491 return general_set_int(p, &val, sizeof(val));
492 } else if (p->data_type == OSSL_PARAM_REAL) {
493 p->return_size = sizeof(double);
496 switch (p->data_size) {
498 shift = real_shift();
499 if (shift < 8 * sizeof(val) - 1) {
500 u32 = val < 0 ? -val : val;
501 if ((u32 >> shift) != 0) {
506 *(double *)p->data = (double)val;
509 err_unsupported_real;
516 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
518 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
522 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
526 if (val == NULL || p == NULL) {
531 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
532 #ifndef OPENSSL_SMALL_FOOTPRINT
535 switch (p->data_size) {
536 case sizeof(uint32_t):
537 *val = *(const uint32_t *)p->data;
539 case sizeof(uint64_t):
540 u64 = *(const uint64_t *)p->data;
541 if (u64 <= UINT32_MAX) {
542 *val = (uint32_t)u64;
549 return general_get_uint(p, val, sizeof(*val));
550 } else if (p->data_type == OSSL_PARAM_INTEGER) {
551 #ifndef OPENSSL_SMALL_FOOTPRINT
555 switch (p->data_size) {
556 case sizeof(int32_t):
557 i32 = *(const int32_t *)p->data;
562 err_unsigned_negative;
564 case sizeof(int64_t):
565 i64 = *(const int64_t *)p->data;
566 if (i64 >= 0 && i64 <= UINT32_MAX) {
567 *val = (uint32_t)i64;
571 err_unsigned_negative;
577 return general_get_uint(p, val, sizeof(*val));
578 } else if (p->data_type == OSSL_PARAM_REAL) {
579 switch (p->data_size) {
581 d = *(const double *)p->data;
582 if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
589 err_unsupported_real;
596 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
606 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
607 #ifndef OPENSSL_SMALL_FOOTPRINT
608 p->return_size = sizeof(uint32_t); /* Minimum expected size */
611 switch (p->data_size) {
612 case sizeof(uint32_t):
613 *(uint32_t *)p->data = val;
615 case sizeof(uint64_t):
616 p->return_size = sizeof(uint64_t);
617 *(uint64_t *)p->data = val;
621 return general_set_uint(p, &val, sizeof(val));
622 } else if (p->data_type == OSSL_PARAM_INTEGER) {
623 #ifndef OPENSSL_SMALL_FOOTPRINT
624 p->return_size = sizeof(int32_t); /* Minimum expected size */
627 switch (p->data_size) {
628 case sizeof(int32_t):
629 if (val <= INT32_MAX) {
630 *(int32_t *)p->data = (int32_t)val;
635 case sizeof(int64_t):
636 p->return_size = sizeof(int64_t);
637 *(int64_t *)p->data = (int64_t)val;
641 return general_set_uint(p, &val, sizeof(val));
642 } else if (p->data_type == OSSL_PARAM_REAL) {
643 p->return_size = sizeof(double);
646 switch (p->data_size) {
648 shift = real_shift();
649 if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
653 *(double *)p->data = (double)val;
656 err_unsupported_real;
663 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
665 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
669 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
673 if (val == NULL || p == NULL) {
678 if (p->data_type == OSSL_PARAM_INTEGER) {
679 #ifndef OPENSSL_SMALL_FOOTPRINT
680 switch (p->data_size) {
681 case sizeof(int32_t):
682 *val = *(const int32_t *)p->data;
684 case sizeof(int64_t):
685 *val = *(const int64_t *)p->data;
689 return general_get_int(p, val, sizeof(*val));
690 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
691 #ifndef OPENSSL_SMALL_FOOTPRINT
694 switch (p->data_size) {
695 case sizeof(uint32_t):
696 *val = *(const uint32_t *)p->data;
698 case sizeof(uint64_t):
699 u64 = *(const uint64_t *)p->data;
700 if (u64 <= INT64_MAX) {
708 return general_get_int(p, val, sizeof(*val));
709 } else if (p->data_type == OSSL_PARAM_REAL) {
710 switch (p->data_size) {
712 d = *(const double *)p->data;
715 * By subtracting 65535 (2^16-1) we cancel the low order
716 * 15 bits of INT64_MAX to avoid using imprecise floating
719 && d < (double)(INT64_MAX - 65535) + 65536.0
720 && d == (int64_t)d) {
727 err_unsupported_real;
734 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
743 if (p->data_type == OSSL_PARAM_INTEGER) {
744 #ifndef OPENSSL_SMALL_FOOTPRINT
745 p->return_size = sizeof(int64_t); /* Expected size */
748 switch (p->data_size) {
749 case sizeof(int32_t):
750 if (val >= INT32_MIN && val <= INT32_MAX) {
751 p->return_size = sizeof(int32_t);
752 *(int32_t *)p->data = (int32_t)val;
757 case sizeof(int64_t):
758 *(int64_t *)p->data = val;
762 return general_set_int(p, &val, sizeof(val));
763 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
764 #ifndef OPENSSL_SMALL_FOOTPRINT
765 p->return_size = sizeof(uint64_t); /* Expected size */
768 switch (p->data_size) {
769 case sizeof(uint32_t):
770 if (val <= UINT32_MAX) {
771 p->return_size = sizeof(uint32_t);
772 *(uint32_t *)p->data = (uint32_t)val;
777 case sizeof(uint64_t):
778 *(uint64_t *)p->data = (uint64_t)val;
782 return general_set_int(p, &val, sizeof(val));
783 } else if (p->data_type == OSSL_PARAM_REAL) {
784 p->return_size = sizeof(double);
787 switch (p->data_size) {
789 u64 = val < 0 ? -val : val;
790 if ((u64 >> real_shift()) == 0) {
791 *(double *)p->data = (double)val;
797 err_unsupported_real;
804 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
806 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
809 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
813 if (val == NULL || p == NULL) {
818 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
819 #ifndef OPENSSL_SMALL_FOOTPRINT
820 switch (p->data_size) {
821 case sizeof(uint32_t):
822 *val = *(const uint32_t *)p->data;
824 case sizeof(uint64_t):
825 *val = *(const uint64_t *)p->data;
829 return general_get_uint(p, val, sizeof(*val));
830 } else if (p->data_type == OSSL_PARAM_INTEGER) {
831 #ifndef OPENSSL_SMALL_FOOTPRINT
835 switch (p->data_size) {
836 case sizeof(int32_t):
837 i32 = *(const int32_t *)p->data;
839 *val = (uint64_t)i32;
842 err_unsigned_negative;
844 case sizeof(int64_t):
845 i64 = *(const int64_t *)p->data;
847 *val = (uint64_t)i64;
850 err_unsigned_negative;
854 return general_get_uint(p, val, sizeof(*val));
855 } else if (p->data_type == OSSL_PARAM_REAL) {
856 switch (p->data_size) {
858 d = *(const double *)p->data;
861 * By subtracting 65535 (2^16-1) we cancel the low order
862 * 15 bits of UINT64_MAX to avoid using imprecise floating
865 && d < (double)(UINT64_MAX - 65535) + 65536.0
866 && d == (uint64_t)d) {
873 err_unsupported_real;
880 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
888 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
889 #ifndef OPENSSL_SMALL_FOOTPRINT
890 p->return_size = sizeof(uint64_t); /* Expected size */
893 switch (p->data_size) {
894 case sizeof(uint32_t):
895 if (val <= UINT32_MAX) {
896 p->return_size = sizeof(uint32_t);
897 *(uint32_t *)p->data = (uint32_t)val;
902 case sizeof(uint64_t):
903 *(uint64_t *)p->data = val;
907 return general_set_uint(p, &val, sizeof(val));
908 } else if (p->data_type == OSSL_PARAM_INTEGER) {
909 #ifndef OPENSSL_SMALL_FOOTPRINT
910 p->return_size = sizeof(int64_t); /* Expected size */
913 switch (p->data_size) {
914 case sizeof(int32_t):
915 if (val <= INT32_MAX) {
916 p->return_size = sizeof(int32_t);
917 *(int32_t *)p->data = (int32_t)val;
922 case sizeof(int64_t):
923 if (val <= INT64_MAX) {
924 *(int64_t *)p->data = (int64_t)val;
931 return general_set_uint(p, &val, sizeof(val));
932 } else if (p->data_type == OSSL_PARAM_REAL) {
933 p->return_size = sizeof(double);
934 switch (p->data_size) {
936 if ((val >> real_shift()) == 0) {
937 *(double *)p->data = (double)val;
943 err_unsupported_real;
950 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
952 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
956 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
958 #ifndef OPENSSL_SMALL_FOOTPRINT
959 switch (sizeof(size_t)) {
960 case sizeof(uint32_t):
961 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
962 case sizeof(uint64_t):
963 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
966 return general_get_uint(p, val, sizeof(*val));
969 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
971 #ifndef OPENSSL_SMALL_FOOTPRINT
972 switch (sizeof(size_t)) {
973 case sizeof(uint32_t):
974 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
975 case sizeof(uint64_t):
976 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
979 return general_set_uint(p, &val, sizeof(val));
982 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
984 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
988 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
990 #ifndef OPENSSL_SMALL_FOOTPRINT
991 switch (sizeof(time_t)) {
992 case sizeof(int32_t):
993 return OSSL_PARAM_get_int32(p, (int32_t *)val);
994 case sizeof(int64_t):
995 return OSSL_PARAM_get_int64(p, (int64_t *)val);
998 return general_get_int(p, val, sizeof(*val));
1001 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1003 #ifndef OPENSSL_SMALL_FOOTPRINT
1004 switch (sizeof(time_t)) {
1005 case sizeof(int32_t):
1006 return OSSL_PARAM_set_int32(p, (int32_t)val);
1007 case sizeof(int64_t):
1008 return OSSL_PARAM_set_int64(p, (int64_t)val);
1011 return general_set_int(p, &val, sizeof(val));
1014 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1016 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1019 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1023 if (val == NULL || p == NULL) {
1028 switch (p->data_type) {
1029 case OSSL_PARAM_UNSIGNED_INTEGER:
1030 b = BN_native2bn(p->data, (int)p->data_size, *val);
1032 case OSSL_PARAM_INTEGER:
1033 b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1041 ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1049 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1062 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1067 bytes = (size_t)BN_num_bytes(val);
1068 /* We add 1 byte for signed numbers, to make space for a sign extension */
1069 if (p->data_type == OSSL_PARAM_INTEGER)
1071 /* We make sure that at least one byte is used, so zero is properly set */
1075 p->return_size = bytes;
1076 if (p->data == NULL)
1078 if (p->data_size >= bytes) {
1079 p->return_size = p->data_size;
1081 switch (p->data_type) {
1082 case OSSL_PARAM_UNSIGNED_INTEGER:
1083 if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1085 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1087 case OSSL_PARAM_INTEGER:
1088 if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1090 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 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1114 if (val == NULL || p == NULL) {
1119 if (p->data_type == OSSL_PARAM_REAL) {
1120 switch (p->data_size) {
1121 case sizeof(double):
1122 *val = *(const double *)p->data;
1125 err_unsupported_real;
1127 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1128 switch (p->data_size) {
1129 case sizeof(uint32_t):
1130 *val = *(const uint32_t *)p->data;
1132 case sizeof(uint64_t):
1133 u64 = *(const uint64_t *)p->data;
1134 if ((u64 >> real_shift()) == 0) {
1141 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1142 switch (p->data_size) {
1143 case sizeof(int32_t):
1144 *val = *(const int32_t *)p->data;
1146 case sizeof(int64_t):
1147 i64 = *(const int64_t *)p->data;
1148 u64 = i64 < 0 ? -i64 : i64;
1149 if ((u64 >> real_shift()) == 0) {
1161 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1169 if (p->data_type == OSSL_PARAM_REAL) {
1170 p->return_size = sizeof(double);
1171 if (p->data == NULL)
1173 switch (p->data_size) {
1174 case sizeof(double):
1175 *(double *)p->data = val;
1178 err_unsupported_real;
1180 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1181 p->return_size = sizeof(double);
1182 if (p->data == NULL)
1184 if (val != (uint64_t)val) {
1188 switch (p->data_size) {
1189 case sizeof(uint32_t):
1190 if (val >= 0 && val <= UINT32_MAX) {
1191 p->return_size = sizeof(uint32_t);
1192 *(uint32_t *)p->data = (uint32_t)val;
1197 case sizeof(uint64_t):
1200 * By subtracting 65535 (2^16-1) we cancel the low order
1201 * 15 bits of UINT64_MAX to avoid using imprecise floating
1204 && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1205 p->return_size = sizeof(uint64_t);
1206 *(uint64_t *)p->data = (uint64_t)val;
1212 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1213 p->return_size = sizeof(double);
1214 if (p->data == NULL)
1216 if (val != (int64_t)val) {
1220 switch (p->data_size) {
1221 case sizeof(int32_t):
1222 if (val >= INT32_MIN && val <= INT32_MAX) {
1223 p->return_size = sizeof(int32_t);
1224 *(int32_t *)p->data = (int32_t)val;
1229 case sizeof(int64_t):
1230 if (val >= INT64_MIN
1232 * By subtracting 65535 (2^16-1) we cancel the low order
1233 * 15 bits of INT64_MAX to avoid using imprecise floating
1236 && val < (double)(INT64_MAX - 65535) + 65536.0) {
1237 p->return_size = sizeof(int64_t);
1238 *(int64_t *)p->data = (int64_t)val;
1249 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1251 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1254 static int get_string_internal(const OSSL_PARAM *p, void **val,
1255 size_t *max_len, size_t *used_len,
1258 size_t sz, alloc_sz;
1260 if ((val == NULL && used_len == NULL) || p == NULL) {
1264 if (p->data_type != type) {
1271 * If the input size is 0, or the input string needs NUL byte
1272 * termination, allocate an extra byte.
1274 alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1276 if (used_len != NULL)
1279 if (p->data == NULL) {
1288 char *const q = OPENSSL_malloc(alloc_sz);
1293 *max_len = alloc_sz;
1296 if (*max_len < sz) {
1300 memcpy(*val, p->data, sz);
1304 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1306 int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1307 OSSL_PARAM_UTF8_STRING);
1310 * We try to ensure that the copied string is terminated with a
1311 * NUL byte. That should be easy, just place a NUL byte at
1312 * |((char*)*val)[p->data_size]|.
1313 * Unfortunately, we have seen cases where |p->data_size| doesn't
1314 * correctly reflect the length of the string, and just happens
1315 * to be out of bounds according to |max_len|, so in that case, we
1316 * make the extra step of trying to find the true length of the
1317 * string that |p->data| points at, and use that as an index to
1318 * place the NUL byte in |*val|.
1320 size_t data_length = p->data_size;
1324 if (data_length >= max_len)
1325 data_length = OPENSSL_strnlen(p->data, data_length);
1326 if (data_length >= max_len) {
1327 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1328 return 0; /* No space for a terminating NUL byte */
1330 (*val)[data_length] = '\0';
1335 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1338 return get_string_internal(p, val, &max_len, used_len,
1339 OSSL_PARAM_OCTET_STRING);
1342 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1345 p->return_size = len;
1346 if (p->data == NULL)
1348 if (p->data_type != type) {
1352 if (p->data_size < len) {
1357 memcpy(p->data, val, len);
1358 /* If possible within the size of p->data, add a NUL terminator byte */
1359 if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1360 ((char *)p->data)[len] = '\0';
1364 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1376 return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1379 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1392 return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1395 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1398 if (buf != NULL && bsize == 0)
1399 bsize = strlen(buf);
1400 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1403 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1406 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1409 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1410 size_t *used_len, unsigned int type)
1412 if (val == NULL || p == NULL) {
1416 if (p->data_type != type) {
1420 if (used_len != NULL)
1421 *used_len = p->data_size;
1422 *val = *(const void **)p->data;
1426 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1428 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1431 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1434 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1437 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1438 unsigned int type, size_t len)
1440 p->return_size = len;
1441 if (p->data_type != type) {
1445 if (p->data != NULL)
1446 *(const void **)p->data = val;
1450 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1457 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1458 val == NULL ? 0 : strlen(val));
1461 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1469 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1472 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1475 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1478 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1481 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1484 OSSL_PARAM OSSL_PARAM_construct_end(void)
1486 OSSL_PARAM end = OSSL_PARAM_END;
1491 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1492 size_t *used_len, unsigned int type)
1494 if (val == NULL || p == NULL) {
1498 if (p->data_type != type) {
1502 if (used_len != NULL)
1503 *used_len = p->data_size;
1508 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1513 rv = OSSL_PARAM_get_utf8_ptr(p, val);
1516 return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1517 OSSL_PARAM_UTF8_STRING);
1520 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1526 rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1529 return rv || get_string_ptr_internal(p, val, used_len,
1530 OSSL_PARAM_OCTET_STRING);