2 * Copyright 2019-2024 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"
17 #include "internal/params.h"
18 #include "internal/packet.h"
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)
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)
41 #ifndef OPENSSL_SYS_UEFI
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
47 static unsigned int real_shift(void)
49 return sizeof(double) == 4 ? 24 : 53;
53 OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
55 if (p != NULL && key != NULL)
56 for (; p->key != NULL; p++)
57 if (strcmp(key, p->key) == 0)
62 const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
64 return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
67 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
68 void *data, size_t data_size)
73 res.data_type = data_type;
75 res.data_size = data_size;
76 res.return_size = OSSL_PARAM_UNMODIFIED;
80 int OSSL_PARAM_modified(const OSSL_PARAM *p)
82 return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
85 void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
88 while (p->key != NULL)
89 p++->return_size = OSSL_PARAM_UNMODIFIED;
92 /* Return non-zero if the signed number is negative */
93 static int is_negative(const void *number, size_t s)
95 const unsigned char *n = number;
98 return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
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)
106 for (i = 0; i < n; i++)
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.
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)
125 if (src_len < dest_len) {
126 n = dest_len - src_len;
127 memset(dest, pad, n);
128 memcpy(dest + n, src, src_len);
130 n = src_len - dest_len;
131 if (!check_sign_bytes(src, n, pad)
133 * Shortening a signed value must retain the correct sign.
134 * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
136 || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
140 memcpy(dest, src + n, dest_len);
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);
148 n = src_len - dest_len;
149 if (!check_sign_bytes(src + dest_len, n, pad)
151 * Shortening a signed value must retain the correct sign.
152 * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
154 || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
158 memcpy(dest, src, dest_len);
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)
168 return copy_integer(dest, dest_len, src, src_len,
169 is_negative(src, src_len) ? 0xff : 0, 1);
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)
176 return copy_integer(dest, dest_len, src, src_len, 0, 1);
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)
183 if (is_negative(src, src_len)) {
184 err_unsigned_negative;
187 return copy_integer(dest, dest_len, src, src_len, 0, 0);
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)
194 return copy_integer(dest, dest_len, src, src_len, 0, 0);
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)
200 if (p->data == NULL) {
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);
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)
217 p->return_size = val_size; /* Expected size */
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);
226 p->return_size = r ? p->data_size : val_size;
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)
234 if (p->data == NULL) {
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);
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)
251 p->return_size = val_size; /* Expected size */
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);
260 p->return_size = r ? p->data_size : val_size;
264 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
266 #ifndef OPENSSL_SMALL_FOOTPRINT
267 switch (sizeof(int)) {
268 case sizeof(int32_t):
269 return OSSL_PARAM_get_int32(p, (int32_t *)val);
270 case sizeof(int64_t):
271 return OSSL_PARAM_get_int64(p, (int64_t *)val);
274 return general_get_int(p, val, sizeof(*val));
277 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
279 #ifndef OPENSSL_SMALL_FOOTPRINT
280 switch (sizeof(int)) {
281 case sizeof(int32_t):
282 return OSSL_PARAM_set_int32(p, (int32_t)val);
283 case sizeof(int64_t):
284 return OSSL_PARAM_set_int64(p, (int64_t)val);
287 return general_set_int(p, &val, sizeof(val));
290 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
292 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
295 int OSSL_PARAM_get_uint(const 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_get_uint32(p, (uint32_t *)val);
301 case sizeof(uint64_t):
302 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
305 return general_get_uint(p, val, sizeof(*val));
308 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
310 #ifndef OPENSSL_SMALL_FOOTPRINT
311 switch (sizeof(unsigned int)) {
312 case sizeof(uint32_t):
313 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
314 case sizeof(uint64_t):
315 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
318 return general_set_uint(p, &val, sizeof(val));
321 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
323 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
324 sizeof(unsigned int));
327 int OSSL_PARAM_get_long(const 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_get_int32(p, (int32_t *)val);
333 case sizeof(int64_t):
334 return OSSL_PARAM_get_int64(p, (int64_t *)val);
337 return general_get_int(p, val, sizeof(*val));
340 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
342 #ifndef OPENSSL_SMALL_FOOTPRINT
343 switch (sizeof(long int)) {
344 case sizeof(int32_t):
345 return OSSL_PARAM_set_int32(p, (int32_t)val);
346 case sizeof(int64_t):
347 return OSSL_PARAM_set_int64(p, (int64_t)val);
350 return general_set_int(p, &val, sizeof(val));
353 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
355 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
358 int OSSL_PARAM_get_ulong(const 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_get_uint32(p, (uint32_t *)val);
364 case sizeof(uint64_t):
365 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
368 return general_get_uint(p, val, sizeof(*val));
371 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
373 #ifndef OPENSSL_SMALL_FOOTPRINT
374 switch (sizeof(unsigned long int)) {
375 case sizeof(uint32_t):
376 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
377 case sizeof(uint64_t):
378 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
381 return general_set_uint(p, &val, sizeof(val));
384 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
386 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
387 sizeof(unsigned long int));
390 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
392 if (val == NULL || p == NULL) {
397 if (p->data == NULL) {
402 if (p->data_type == OSSL_PARAM_INTEGER) {
403 #ifndef OPENSSL_SMALL_FOOTPRINT
406 switch (p->data_size) {
407 case sizeof(int32_t):
408 *val = *(const int32_t *)p->data;
410 case sizeof(int64_t):
411 i64 = *(const int64_t *)p->data;
412 if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
420 return general_get_int(p, val, sizeof(*val));
422 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
423 #ifndef OPENSSL_SMALL_FOOTPRINT
427 switch (p->data_size) {
428 case sizeof(uint32_t):
429 u32 = *(const uint32_t *)p->data;
430 if (u32 <= INT32_MAX) {
436 case sizeof(uint64_t):
437 u64 = *(const uint64_t *)p->data;
438 if (u64 <= INT32_MAX) {
446 return general_get_int(p, val, sizeof(*val));
448 } else if (p->data_type == OSSL_PARAM_REAL) {
449 #ifndef OPENSSL_SYS_UEFI
452 switch (p->data_size) {
454 d = *(const double *)p->data;
455 if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
462 err_unsupported_real;
470 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
480 if (p->data_type == OSSL_PARAM_INTEGER) {
481 #ifndef OPENSSL_SMALL_FOOTPRINT
482 p->return_size = sizeof(int32_t); /* Minimum expected size */
485 switch (p->data_size) {
486 case sizeof(int32_t):
487 *(int32_t *)p->data = val;
489 case sizeof(int64_t):
490 p->return_size = sizeof(int64_t);
491 *(int64_t *)p->data = (int64_t)val;
495 return general_set_int(p, &val, sizeof(val));
496 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
497 #ifndef OPENSSL_SMALL_FOOTPRINT
498 p->return_size = sizeof(uint32_t); /* Minimum expected size */
501 switch (p->data_size) {
502 case sizeof(uint32_t):
503 *(uint32_t *)p->data = (uint32_t)val;
505 case sizeof(uint64_t):
506 p->return_size = sizeof(uint64_t);
507 *(uint64_t *)p->data = (uint64_t)val;
511 return general_set_int(p, &val, sizeof(val));
512 } else if (p->data_type == OSSL_PARAM_REAL) {
513 #ifndef OPENSSL_SYS_UEFI
514 p->return_size = sizeof(double);
517 switch (p->data_size) {
519 shift = real_shift();
520 if (shift < 8 * sizeof(val) - 1) {
521 u32 = val < 0 ? -val : val;
522 if ((u32 >> shift) != 0) {
527 *(double *)p->data = (double)val;
530 err_unsupported_real;
538 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
540 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
544 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
546 if (val == NULL || p == NULL) {
551 if (p->data == NULL) {
556 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
557 #ifndef OPENSSL_SMALL_FOOTPRINT
560 switch (p->data_size) {
561 case sizeof(uint32_t):
562 *val = *(const uint32_t *)p->data;
564 case sizeof(uint64_t):
565 u64 = *(const uint64_t *)p->data;
566 if (u64 <= UINT32_MAX) {
567 *val = (uint32_t)u64;
574 return general_get_uint(p, val, sizeof(*val));
575 } else if (p->data_type == OSSL_PARAM_INTEGER) {
576 #ifndef OPENSSL_SMALL_FOOTPRINT
580 switch (p->data_size) {
581 case sizeof(int32_t):
582 i32 = *(const int32_t *)p->data;
587 err_unsigned_negative;
589 case sizeof(int64_t):
590 i64 = *(const int64_t *)p->data;
591 if (i64 >= 0 && i64 <= UINT32_MAX) {
592 *val = (uint32_t)i64;
596 err_unsigned_negative;
602 return general_get_uint(p, val, sizeof(*val));
603 } else if (p->data_type == OSSL_PARAM_REAL) {
604 #ifndef OPENSSL_SYS_UEFI
607 switch (p->data_size) {
609 d = *(const double *)p->data;
610 if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
617 err_unsupported_real;
625 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
635 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
636 #ifndef OPENSSL_SMALL_FOOTPRINT
637 p->return_size = sizeof(uint32_t); /* Minimum expected size */
640 switch (p->data_size) {
641 case sizeof(uint32_t):
642 *(uint32_t *)p->data = val;
644 case sizeof(uint64_t):
645 p->return_size = sizeof(uint64_t);
646 *(uint64_t *)p->data = val;
650 return general_set_uint(p, &val, sizeof(val));
651 } else if (p->data_type == OSSL_PARAM_INTEGER) {
652 #ifndef OPENSSL_SMALL_FOOTPRINT
653 p->return_size = sizeof(int32_t); /* Minimum expected size */
656 switch (p->data_size) {
657 case sizeof(int32_t):
658 if (val <= INT32_MAX) {
659 *(int32_t *)p->data = (int32_t)val;
664 case sizeof(int64_t):
665 p->return_size = sizeof(int64_t);
666 *(int64_t *)p->data = (int64_t)val;
670 return general_set_uint(p, &val, sizeof(val));
671 } else if (p->data_type == OSSL_PARAM_REAL) {
672 #ifndef OPENSSL_SYS_UEFI
673 p->return_size = sizeof(double);
676 switch (p->data_size) {
678 shift = real_shift();
679 if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
683 *(double *)p->data = (double)val;
686 err_unsupported_real;
694 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
696 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
700 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
702 if (val == NULL || p == NULL) {
707 if (p->data == NULL) {
712 if (p->data_type == OSSL_PARAM_INTEGER) {
713 #ifndef OPENSSL_SMALL_FOOTPRINT
714 switch (p->data_size) {
715 case sizeof(int32_t):
716 *val = *(const int32_t *)p->data;
718 case sizeof(int64_t):
719 *val = *(const int64_t *)p->data;
723 return general_get_int(p, val, sizeof(*val));
724 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
725 #ifndef OPENSSL_SMALL_FOOTPRINT
728 switch (p->data_size) {
729 case sizeof(uint32_t):
730 *val = *(const uint32_t *)p->data;
732 case sizeof(uint64_t):
733 u64 = *(const uint64_t *)p->data;
734 if (u64 <= INT64_MAX) {
742 return general_get_int(p, val, sizeof(*val));
743 } else if (p->data_type == OSSL_PARAM_REAL) {
744 #ifndef OPENSSL_SYS_UEFI
747 switch (p->data_size) {
749 d = *(const double *)p->data;
752 * By subtracting 65535 (2^16-1) we cancel the low order
753 * 15 bits of INT64_MAX to avoid using imprecise floating
756 && d < (double)(INT64_MAX - 65535) + 65536.0
757 && d == (int64_t)d) {
764 err_unsupported_real;
772 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
779 if (p->data_type == OSSL_PARAM_INTEGER) {
780 #ifndef OPENSSL_SMALL_FOOTPRINT
781 p->return_size = sizeof(int64_t); /* Expected size */
784 switch (p->data_size) {
785 case sizeof(int32_t):
786 if (val >= INT32_MIN && val <= INT32_MAX) {
787 p->return_size = sizeof(int32_t);
788 *(int32_t *)p->data = (int32_t)val;
793 case sizeof(int64_t):
794 *(int64_t *)p->data = val;
798 return general_set_int(p, &val, sizeof(val));
799 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
800 #ifndef OPENSSL_SMALL_FOOTPRINT
801 p->return_size = sizeof(uint64_t); /* Expected size */
804 switch (p->data_size) {
805 case sizeof(uint32_t):
806 if (val <= UINT32_MAX) {
807 p->return_size = sizeof(uint32_t);
808 *(uint32_t *)p->data = (uint32_t)val;
813 case sizeof(uint64_t):
814 *(uint64_t *)p->data = (uint64_t)val;
818 return general_set_int(p, &val, sizeof(val));
819 } else if (p->data_type == OSSL_PARAM_REAL) {
820 #ifndef OPENSSL_SYS_UEFI
823 p->return_size = sizeof(double);
826 switch (p->data_size) {
828 u64 = val < 0 ? -val : val;
829 if ((u64 >> real_shift()) == 0) {
830 *(double *)p->data = (double)val;
836 err_unsupported_real;
844 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
846 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
849 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
851 if (val == NULL || p == NULL) {
856 if (p->data == NULL) {
861 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
862 #ifndef OPENSSL_SMALL_FOOTPRINT
863 switch (p->data_size) {
864 case sizeof(uint32_t):
865 *val = *(const uint32_t *)p->data;
867 case sizeof(uint64_t):
868 *val = *(const uint64_t *)p->data;
872 return general_get_uint(p, val, sizeof(*val));
873 } else if (p->data_type == OSSL_PARAM_INTEGER) {
874 #ifndef OPENSSL_SMALL_FOOTPRINT
878 switch (p->data_size) {
879 case sizeof(int32_t):
880 i32 = *(const int32_t *)p->data;
882 *val = (uint64_t)i32;
885 err_unsigned_negative;
887 case sizeof(int64_t):
888 i64 = *(const int64_t *)p->data;
890 *val = (uint64_t)i64;
893 err_unsigned_negative;
897 return general_get_uint(p, val, sizeof(*val));
898 } else if (p->data_type == OSSL_PARAM_REAL) {
899 #ifndef OPENSSL_SYS_UEFI
902 switch (p->data_size) {
904 d = *(const double *)p->data;
907 * By subtracting 65535 (2^16-1) we cancel the low order
908 * 15 bits of UINT64_MAX to avoid using imprecise floating
911 && d < (double)(UINT64_MAX - 65535) + 65536.0
912 && d == (uint64_t)d) {
919 err_unsupported_real;
927 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
935 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
936 #ifndef OPENSSL_SMALL_FOOTPRINT
937 p->return_size = sizeof(uint64_t); /* Expected size */
940 switch (p->data_size) {
941 case sizeof(uint32_t):
942 if (val <= UINT32_MAX) {
943 p->return_size = sizeof(uint32_t);
944 *(uint32_t *)p->data = (uint32_t)val;
949 case sizeof(uint64_t):
950 *(uint64_t *)p->data = val;
954 return general_set_uint(p, &val, sizeof(val));
955 } else if (p->data_type == OSSL_PARAM_INTEGER) {
956 #ifndef OPENSSL_SMALL_FOOTPRINT
957 p->return_size = sizeof(int64_t); /* Expected size */
960 switch (p->data_size) {
961 case sizeof(int32_t):
962 if (val <= INT32_MAX) {
963 p->return_size = sizeof(int32_t);
964 *(int32_t *)p->data = (int32_t)val;
969 case sizeof(int64_t):
970 if (val <= INT64_MAX) {
971 *(int64_t *)p->data = (int64_t)val;
978 return general_set_uint(p, &val, sizeof(val));
979 } else if (p->data_type == OSSL_PARAM_REAL) {
980 #ifndef OPENSSL_SYS_UEFI
981 p->return_size = sizeof(double);
982 switch (p->data_size) {
984 if ((val >> real_shift()) == 0) {
985 *(double *)p->data = (double)val;
991 err_unsupported_real;
999 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
1001 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1005 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
1007 #ifndef OPENSSL_SMALL_FOOTPRINT
1008 switch (sizeof(size_t)) {
1009 case sizeof(uint32_t):
1010 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
1011 case sizeof(uint64_t):
1012 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
1015 return general_get_uint(p, val, sizeof(*val));
1018 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
1020 #ifndef OPENSSL_SMALL_FOOTPRINT
1021 switch (sizeof(size_t)) {
1022 case sizeof(uint32_t):
1023 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
1024 case sizeof(uint64_t):
1025 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
1028 return general_set_uint(p, &val, sizeof(val));
1031 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1033 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1037 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1039 #ifndef OPENSSL_SMALL_FOOTPRINT
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);
1047 return general_get_int(p, val, sizeof(*val));
1050 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1052 #ifndef OPENSSL_SMALL_FOOTPRINT
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);
1060 return general_set_int(p, &val, sizeof(val));
1063 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1065 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1068 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1072 if (val == NULL || p == NULL || p->data == NULL) {
1077 switch (p->data_type) {
1078 case OSSL_PARAM_UNSIGNED_INTEGER:
1079 b = BN_native2bn(p->data, (int)p->data_size, *val);
1081 case OSSL_PARAM_INTEGER:
1082 b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1090 ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1098 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1111 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1116 bytes = (size_t)BN_num_bytes(val);
1117 /* We add 1 byte for signed numbers, to make space for a sign extension */
1118 if (p->data_type == OSSL_PARAM_INTEGER)
1120 /* We make sure that at least one byte is used, so zero is properly set */
1124 p->return_size = bytes;
1125 if (p->data == NULL)
1127 if (p->data_size >= bytes) {
1128 p->return_size = p->data_size;
1130 switch (p->data_type) {
1131 case OSSL_PARAM_UNSIGNED_INTEGER:
1132 if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1134 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1136 case OSSL_PARAM_INTEGER:
1137 if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1139 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1151 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1154 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1158 #ifndef OPENSSL_SYS_UEFI
1159 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1164 if (val == NULL || p == NULL || p->data == NULL) {
1169 if (p->data_type == OSSL_PARAM_REAL) {
1170 switch (p->data_size) {
1171 case sizeof(double):
1172 *val = *(const double *)p->data;
1175 err_unsupported_real;
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;
1182 case sizeof(uint64_t):
1183 u64 = *(const uint64_t *)p->data;
1184 if ((u64 >> real_shift()) == 0) {
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;
1196 case sizeof(int64_t):
1197 i64 = *(const int64_t *)p->data;
1198 u64 = i64 < 0 ? -i64 : i64;
1199 if ((u64 >> real_shift()) == 0) {
1211 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1219 if (p->data_type == OSSL_PARAM_REAL) {
1220 p->return_size = sizeof(double);
1221 if (p->data == NULL)
1223 switch (p->data_size) {
1224 case sizeof(double):
1225 *(double *)p->data = val;
1228 err_unsupported_real;
1230 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1231 p->return_size = sizeof(double);
1232 if (p->data == NULL)
1234 if (val != (uint64_t)val) {
1238 switch (p->data_size) {
1239 case sizeof(uint32_t):
1240 if (val >= 0 && val <= UINT32_MAX) {
1241 p->return_size = sizeof(uint32_t);
1242 *(uint32_t *)p->data = (uint32_t)val;
1247 case sizeof(uint64_t):
1250 * By subtracting 65535 (2^16-1) we cancel the low order
1251 * 15 bits of UINT64_MAX to avoid using imprecise floating
1254 && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1255 p->return_size = sizeof(uint64_t);
1256 *(uint64_t *)p->data = (uint64_t)val;
1262 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1263 p->return_size = sizeof(double);
1264 if (p->data == NULL)
1266 if (val != (int64_t)val) {
1270 switch (p->data_size) {
1271 case sizeof(int32_t):
1272 if (val >= INT32_MIN && val <= INT32_MAX) {
1273 p->return_size = sizeof(int32_t);
1274 *(int32_t *)p->data = (int32_t)val;
1279 case sizeof(int64_t):
1280 if (val >= INT64_MIN
1282 * By subtracting 65535 (2^16-1) we cancel the low order
1283 * 15 bits of INT64_MAX to avoid using imprecise floating
1286 && val < (double)(INT64_MAX - 65535) + 65536.0) {
1287 p->return_size = sizeof(int64_t);
1288 *(int64_t *)p->data = (int64_t)val;
1299 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1301 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1305 static int get_string_internal(const OSSL_PARAM *p, void **val,
1306 size_t *max_len, size_t *used_len,
1309 size_t sz, alloc_sz;
1311 if ((val == NULL && used_len == NULL) || p == NULL) {
1315 if (p->data_type != type) {
1322 * If the input size is 0, or the input string needs NUL byte
1323 * termination, allocate an extra byte.
1325 alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1327 if (used_len != NULL)
1330 if (p->data == NULL) {
1339 char *const q = OPENSSL_malloc(alloc_sz);
1344 *max_len = alloc_sz;
1347 if (*max_len < sz) {
1351 memcpy(*val, p->data, sz);
1355 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1357 int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1358 OSSL_PARAM_UTF8_STRING);
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|.
1371 size_t data_length = p->data_size;
1375 if (data_length >= max_len)
1376 data_length = OPENSSL_strnlen(p->data, data_length);
1377 if (data_length >= max_len) {
1378 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1379 return 0; /* No space for a terminating NUL byte */
1381 (*val)[data_length] = '\0';
1386 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1389 return get_string_internal(p, val, &max_len, used_len,
1390 OSSL_PARAM_OCTET_STRING);
1393 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1396 p->return_size = len;
1397 if (p->data == NULL)
1399 if (p->data_type != type) {
1403 if (p->data_size < len) {
1408 memcpy(p->data, val, len);
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';
1415 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1427 return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1430 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1443 return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1446 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1449 if (buf != NULL && bsize == 0)
1450 bsize = strlen(buf);
1451 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1454 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1457 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1460 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1461 size_t *used_len, unsigned int type)
1463 if (val == NULL || p == NULL) {
1467 if (p->data_type != type) {
1471 if (used_len != NULL)
1472 *used_len = p->data_size;
1473 *val = *(const void **)p->data;
1477 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1479 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1482 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1485 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1488 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1489 unsigned int type, size_t len)
1491 p->return_size = len;
1492 if (p->data_type != type) {
1496 if (p->data != NULL)
1497 *(const void **)p->data = val;
1501 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1508 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1509 val == NULL ? 0 : strlen(val));
1512 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1520 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1523 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1526 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1529 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1532 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1536 * Extract the parameter into an allocated buffer.
1537 * Any existing allocation in *out is cleared and freed.
1539 * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1541 * *out and *out_len are guaranteed to be untouched if this function
1542 * doesn't return success.
1544 int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1545 unsigned char **out, size_t *out_len)
1547 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1556 && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1559 OPENSSL_clear_free(*out, *out_len);
1565 static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1566 unsigned char *out, size_t *outlen)
1572 if (!WPACKET_init_null(&pkt, 0))
1575 if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1579 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1580 if (p->data_type != OSSL_PARAM_OCTET_STRING)
1583 && p->data_size != 0
1584 && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1587 if (!WPACKET_get_total_written(&pkt, outlen)
1588 || !WPACKET_finish(&pkt))
1592 WPACKET_cleanup(&pkt);
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)
1600 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1607 /* Calculate the total size */
1608 if (!setbuf_fromparams(p, name, NULL, &sz))
1611 /* Check that it's not oversized */
1612 if (maxsize > 0 && sz > maxsize)
1615 /* Special case zero length */
1617 if ((res = OPENSSL_zalloc(1)) == NULL)
1622 /* Allocate the buffer */
1623 res = OPENSSL_malloc(sz);
1627 /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1628 if (!setbuf_fromparams(p, name, res, &sz)) {
1629 OPENSSL_clear_free(res, sz);
1634 OPENSSL_clear_free(*out, *out_len);
1640 OSSL_PARAM OSSL_PARAM_construct_end(void)
1642 OSSL_PARAM end = OSSL_PARAM_END;
1647 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1648 size_t *used_len, unsigned int type)
1650 if (val == NULL || p == NULL) {
1654 if (p->data_type != type) {
1658 if (used_len != NULL)
1659 *used_len = p->data_size;
1664 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1669 rv = OSSL_PARAM_get_utf8_ptr(p, val);
1672 return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1673 OSSL_PARAM_UTF8_STRING);
1676 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1682 rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1685 return rv || get_string_ptr_internal(p, val, used_len,
1686 OSSL_PARAM_OCTET_STRING);