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"
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_type == OSSL_PARAM_INTEGER)
201 return signed_from_signed(val, val_size, p->data, p->data_size);
202 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
203 return signed_from_unsigned(val, val_size, p->data, p->data_size);
208 /* General purpose set integer parameter call that handles odd sizes */
209 static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
213 p->return_size = val_size; /* Expected size */
216 if (p->data_type == OSSL_PARAM_INTEGER)
217 r = signed_from_signed(p->data, p->data_size, val, val_size);
218 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
219 r = unsigned_from_signed(p->data, p->data_size, val, val_size);
222 p->return_size = r ? p->data_size : val_size;
226 /* General purpose get unsigned integer parameter call that handles odd sizes */
227 static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
229 if (p->data_type == OSSL_PARAM_INTEGER)
230 return unsigned_from_signed(val, val_size, p->data, p->data_size);
231 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
232 return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
237 /* General purpose set unsigned integer parameter call that handles odd sizes */
238 static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
242 p->return_size = val_size; /* Expected size */
245 if (p->data_type == OSSL_PARAM_INTEGER)
246 r = signed_from_unsigned(p->data, p->data_size, val, val_size);
247 else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
248 r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
251 p->return_size = r ? p->data_size : val_size;
255 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
257 #ifndef OPENSSL_SMALL_FOOTPRINT
258 switch (sizeof(int)) {
259 case sizeof(int32_t):
260 return OSSL_PARAM_get_int32(p, (int32_t *)val);
261 case sizeof(int64_t):
262 return OSSL_PARAM_get_int64(p, (int64_t *)val);
265 return general_get_int(p, val, sizeof(*val));
268 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
270 #ifndef OPENSSL_SMALL_FOOTPRINT
271 switch (sizeof(int)) {
272 case sizeof(int32_t):
273 return OSSL_PARAM_set_int32(p, (int32_t)val);
274 case sizeof(int64_t):
275 return OSSL_PARAM_set_int64(p, (int64_t)val);
278 return general_set_int(p, &val, sizeof(val));
281 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
283 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
286 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
288 #ifndef OPENSSL_SMALL_FOOTPRINT
289 switch (sizeof(unsigned int)) {
290 case sizeof(uint32_t):
291 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
292 case sizeof(uint64_t):
293 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
296 return general_get_uint(p, val, sizeof(*val));
299 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
301 #ifndef OPENSSL_SMALL_FOOTPRINT
302 switch (sizeof(unsigned int)) {
303 case sizeof(uint32_t):
304 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
305 case sizeof(uint64_t):
306 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
309 return general_set_uint(p, &val, sizeof(val));
312 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
314 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
315 sizeof(unsigned int));
318 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
320 #ifndef OPENSSL_SMALL_FOOTPRINT
321 switch (sizeof(long int)) {
322 case sizeof(int32_t):
323 return OSSL_PARAM_get_int32(p, (int32_t *)val);
324 case sizeof(int64_t):
325 return OSSL_PARAM_get_int64(p, (int64_t *)val);
328 return general_get_int(p, val, sizeof(*val));
331 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
333 #ifndef OPENSSL_SMALL_FOOTPRINT
334 switch (sizeof(long int)) {
335 case sizeof(int32_t):
336 return OSSL_PARAM_set_int32(p, (int32_t)val);
337 case sizeof(int64_t):
338 return OSSL_PARAM_set_int64(p, (int64_t)val);
341 return general_set_int(p, &val, sizeof(val));
344 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
346 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
349 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
351 #ifndef OPENSSL_SMALL_FOOTPRINT
352 switch (sizeof(unsigned long int)) {
353 case sizeof(uint32_t):
354 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
355 case sizeof(uint64_t):
356 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
359 return general_get_uint(p, val, sizeof(*val));
362 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
364 #ifndef OPENSSL_SMALL_FOOTPRINT
365 switch (sizeof(unsigned long int)) {
366 case sizeof(uint32_t):
367 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
368 case sizeof(uint64_t):
369 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
372 return general_set_uint(p, &val, sizeof(val));
375 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
377 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
378 sizeof(unsigned long int));
381 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
383 if (val == NULL || p == NULL) {
388 if (p->data_type == OSSL_PARAM_INTEGER) {
389 #ifndef OPENSSL_SMALL_FOOTPRINT
392 switch (p->data_size) {
393 case sizeof(int32_t):
394 *val = *(const int32_t *)p->data;
396 case sizeof(int64_t):
397 i64 = *(const int64_t *)p->data;
398 if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
406 return general_get_int(p, val, sizeof(*val));
408 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
409 #ifndef OPENSSL_SMALL_FOOTPRINT
413 switch (p->data_size) {
414 case sizeof(uint32_t):
415 u32 = *(const uint32_t *)p->data;
416 if (u32 <= INT32_MAX) {
422 case sizeof(uint64_t):
423 u64 = *(const uint64_t *)p->data;
424 if (u64 <= INT32_MAX) {
432 return general_get_int(p, val, sizeof(*val));
434 } else if (p->data_type == OSSL_PARAM_REAL) {
435 #ifndef OPENSSL_SYS_UEFI
438 switch (p->data_size) {
440 d = *(const double *)p->data;
441 if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
448 err_unsupported_real;
456 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
466 if (p->data_type == OSSL_PARAM_INTEGER) {
467 #ifndef OPENSSL_SMALL_FOOTPRINT
468 p->return_size = sizeof(int32_t); /* Minimum expected size */
471 switch (p->data_size) {
472 case sizeof(int32_t):
473 *(int32_t *)p->data = val;
475 case sizeof(int64_t):
476 p->return_size = sizeof(int64_t);
477 *(int64_t *)p->data = (int64_t)val;
481 return general_set_int(p, &val, sizeof(val));
482 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
483 #ifndef OPENSSL_SMALL_FOOTPRINT
484 p->return_size = sizeof(uint32_t); /* Minimum expected size */
487 switch (p->data_size) {
488 case sizeof(uint32_t):
489 *(uint32_t *)p->data = (uint32_t)val;
491 case sizeof(uint64_t):
492 p->return_size = sizeof(uint64_t);
493 *(uint64_t *)p->data = (uint64_t)val;
497 return general_set_int(p, &val, sizeof(val));
498 } else if (p->data_type == OSSL_PARAM_REAL) {
499 #ifndef OPENSSL_SYS_UEFI
500 p->return_size = sizeof(double);
503 switch (p->data_size) {
505 shift = real_shift();
506 if (shift < 8 * sizeof(val) - 1) {
507 u32 = val < 0 ? -val : val;
508 if ((u32 >> shift) != 0) {
513 *(double *)p->data = (double)val;
516 err_unsupported_real;
524 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
526 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
530 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
532 if (val == NULL || p == NULL) {
537 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
538 #ifndef OPENSSL_SMALL_FOOTPRINT
541 switch (p->data_size) {
542 case sizeof(uint32_t):
543 *val = *(const uint32_t *)p->data;
545 case sizeof(uint64_t):
546 u64 = *(const uint64_t *)p->data;
547 if (u64 <= UINT32_MAX) {
548 *val = (uint32_t)u64;
555 return general_get_uint(p, val, sizeof(*val));
556 } else if (p->data_type == OSSL_PARAM_INTEGER) {
557 #ifndef OPENSSL_SMALL_FOOTPRINT
561 switch (p->data_size) {
562 case sizeof(int32_t):
563 i32 = *(const int32_t *)p->data;
568 err_unsigned_negative;
570 case sizeof(int64_t):
571 i64 = *(const int64_t *)p->data;
572 if (i64 >= 0 && i64 <= UINT32_MAX) {
573 *val = (uint32_t)i64;
577 err_unsigned_negative;
583 return general_get_uint(p, val, sizeof(*val));
584 } else if (p->data_type == OSSL_PARAM_REAL) {
585 #ifndef OPENSSL_SYS_UEFI
588 switch (p->data_size) {
590 d = *(const double *)p->data;
591 if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
598 err_unsupported_real;
606 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
616 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
617 #ifndef OPENSSL_SMALL_FOOTPRINT
618 p->return_size = sizeof(uint32_t); /* Minimum expected size */
621 switch (p->data_size) {
622 case sizeof(uint32_t):
623 *(uint32_t *)p->data = val;
625 case sizeof(uint64_t):
626 p->return_size = sizeof(uint64_t);
627 *(uint64_t *)p->data = val;
631 return general_set_uint(p, &val, sizeof(val));
632 } else if (p->data_type == OSSL_PARAM_INTEGER) {
633 #ifndef OPENSSL_SMALL_FOOTPRINT
634 p->return_size = sizeof(int32_t); /* Minimum expected size */
637 switch (p->data_size) {
638 case sizeof(int32_t):
639 if (val <= INT32_MAX) {
640 *(int32_t *)p->data = (int32_t)val;
645 case sizeof(int64_t):
646 p->return_size = sizeof(int64_t);
647 *(int64_t *)p->data = (int64_t)val;
651 return general_set_uint(p, &val, sizeof(val));
652 } else if (p->data_type == OSSL_PARAM_REAL) {
653 #ifndef OPENSSL_SYS_UEFI
654 p->return_size = sizeof(double);
657 switch (p->data_size) {
659 shift = real_shift();
660 if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
664 *(double *)p->data = (double)val;
667 err_unsupported_real;
675 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
677 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
681 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
683 if (val == NULL || p == NULL) {
688 if (p->data_type == OSSL_PARAM_INTEGER) {
689 #ifndef OPENSSL_SMALL_FOOTPRINT
690 switch (p->data_size) {
691 case sizeof(int32_t):
692 *val = *(const int32_t *)p->data;
694 case sizeof(int64_t):
695 *val = *(const int64_t *)p->data;
699 return general_get_int(p, val, sizeof(*val));
700 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
701 #ifndef OPENSSL_SMALL_FOOTPRINT
704 switch (p->data_size) {
705 case sizeof(uint32_t):
706 *val = *(const uint32_t *)p->data;
708 case sizeof(uint64_t):
709 u64 = *(const uint64_t *)p->data;
710 if (u64 <= INT64_MAX) {
718 return general_get_int(p, val, sizeof(*val));
719 } else if (p->data_type == OSSL_PARAM_REAL) {
720 #ifndef OPENSSL_SYS_UEFI
723 switch (p->data_size) {
725 d = *(const double *)p->data;
728 * By subtracting 65535 (2^16-1) we cancel the low order
729 * 15 bits of INT64_MAX to avoid using imprecise floating
732 && d < (double)(INT64_MAX - 65535) + 65536.0
733 && d == (int64_t)d) {
740 err_unsupported_real;
748 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
755 if (p->data_type == OSSL_PARAM_INTEGER) {
756 #ifndef OPENSSL_SMALL_FOOTPRINT
757 p->return_size = sizeof(int64_t); /* Expected size */
760 switch (p->data_size) {
761 case sizeof(int32_t):
762 if (val >= INT32_MIN && val <= INT32_MAX) {
763 p->return_size = sizeof(int32_t);
764 *(int32_t *)p->data = (int32_t)val;
769 case sizeof(int64_t):
770 *(int64_t *)p->data = val;
774 return general_set_int(p, &val, sizeof(val));
775 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
776 #ifndef OPENSSL_SMALL_FOOTPRINT
777 p->return_size = sizeof(uint64_t); /* Expected size */
780 switch (p->data_size) {
781 case sizeof(uint32_t):
782 if (val <= UINT32_MAX) {
783 p->return_size = sizeof(uint32_t);
784 *(uint32_t *)p->data = (uint32_t)val;
789 case sizeof(uint64_t):
790 *(uint64_t *)p->data = (uint64_t)val;
794 return general_set_int(p, &val, sizeof(val));
795 } else if (p->data_type == OSSL_PARAM_REAL) {
796 #ifndef OPENSSL_SYS_UEFI
799 p->return_size = sizeof(double);
802 switch (p->data_size) {
804 u64 = val < 0 ? -val : val;
805 if ((u64 >> real_shift()) == 0) {
806 *(double *)p->data = (double)val;
812 err_unsupported_real;
820 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
822 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
825 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
827 if (val == NULL || p == NULL) {
832 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
833 #ifndef OPENSSL_SMALL_FOOTPRINT
834 switch (p->data_size) {
835 case sizeof(uint32_t):
836 *val = *(const uint32_t *)p->data;
838 case sizeof(uint64_t):
839 *val = *(const uint64_t *)p->data;
843 return general_get_uint(p, val, sizeof(*val));
844 } else if (p->data_type == OSSL_PARAM_INTEGER) {
845 #ifndef OPENSSL_SMALL_FOOTPRINT
849 switch (p->data_size) {
850 case sizeof(int32_t):
851 i32 = *(const int32_t *)p->data;
853 *val = (uint64_t)i32;
856 err_unsigned_negative;
858 case sizeof(int64_t):
859 i64 = *(const int64_t *)p->data;
861 *val = (uint64_t)i64;
864 err_unsigned_negative;
868 return general_get_uint(p, val, sizeof(*val));
869 } else if (p->data_type == OSSL_PARAM_REAL) {
870 #ifndef OPENSSL_SYS_UEFI
873 switch (p->data_size) {
875 d = *(const double *)p->data;
878 * By subtracting 65535 (2^16-1) we cancel the low order
879 * 15 bits of UINT64_MAX to avoid using imprecise floating
882 && d < (double)(UINT64_MAX - 65535) + 65536.0
883 && d == (uint64_t)d) {
890 err_unsupported_real;
898 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
906 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
907 #ifndef OPENSSL_SMALL_FOOTPRINT
908 p->return_size = sizeof(uint64_t); /* Expected size */
911 switch (p->data_size) {
912 case sizeof(uint32_t):
913 if (val <= UINT32_MAX) {
914 p->return_size = sizeof(uint32_t);
915 *(uint32_t *)p->data = (uint32_t)val;
920 case sizeof(uint64_t):
921 *(uint64_t *)p->data = val;
925 return general_set_uint(p, &val, sizeof(val));
926 } else if (p->data_type == OSSL_PARAM_INTEGER) {
927 #ifndef OPENSSL_SMALL_FOOTPRINT
928 p->return_size = sizeof(int64_t); /* Expected size */
931 switch (p->data_size) {
932 case sizeof(int32_t):
933 if (val <= INT32_MAX) {
934 p->return_size = sizeof(int32_t);
935 *(int32_t *)p->data = (int32_t)val;
940 case sizeof(int64_t):
941 if (val <= INT64_MAX) {
942 *(int64_t *)p->data = (int64_t)val;
949 return general_set_uint(p, &val, sizeof(val));
950 } else if (p->data_type == OSSL_PARAM_REAL) {
951 #ifndef OPENSSL_SYS_UEFI
952 p->return_size = sizeof(double);
953 switch (p->data_size) {
955 if ((val >> real_shift()) == 0) {
956 *(double *)p->data = (double)val;
962 err_unsupported_real;
970 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
972 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
976 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
978 #ifndef OPENSSL_SMALL_FOOTPRINT
979 switch (sizeof(size_t)) {
980 case sizeof(uint32_t):
981 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
982 case sizeof(uint64_t):
983 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
986 return general_get_uint(p, val, sizeof(*val));
989 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
991 #ifndef OPENSSL_SMALL_FOOTPRINT
992 switch (sizeof(size_t)) {
993 case sizeof(uint32_t):
994 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
995 case sizeof(uint64_t):
996 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
999 return general_set_uint(p, &val, sizeof(val));
1002 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1004 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1008 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1010 #ifndef OPENSSL_SMALL_FOOTPRINT
1011 switch (sizeof(time_t)) {
1012 case sizeof(int32_t):
1013 return OSSL_PARAM_get_int32(p, (int32_t *)val);
1014 case sizeof(int64_t):
1015 return OSSL_PARAM_get_int64(p, (int64_t *)val);
1018 return general_get_int(p, val, sizeof(*val));
1021 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1023 #ifndef OPENSSL_SMALL_FOOTPRINT
1024 switch (sizeof(time_t)) {
1025 case sizeof(int32_t):
1026 return OSSL_PARAM_set_int32(p, (int32_t)val);
1027 case sizeof(int64_t):
1028 return OSSL_PARAM_set_int64(p, (int64_t)val);
1031 return general_set_int(p, &val, sizeof(val));
1034 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1036 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1039 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1043 if (val == NULL || p == NULL) {
1048 switch (p->data_type) {
1049 case OSSL_PARAM_UNSIGNED_INTEGER:
1050 b = BN_native2bn(p->data, (int)p->data_size, *val);
1052 case OSSL_PARAM_INTEGER:
1053 b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1061 ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1069 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1082 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1087 bytes = (size_t)BN_num_bytes(val);
1088 /* We add 1 byte for signed numbers, to make space for a sign extension */
1089 if (p->data_type == OSSL_PARAM_INTEGER)
1091 /* We make sure that at least one byte is used, so zero is properly set */
1095 p->return_size = bytes;
1096 if (p->data == NULL)
1098 if (p->data_size >= bytes) {
1099 p->return_size = p->data_size;
1101 switch (p->data_type) {
1102 case OSSL_PARAM_UNSIGNED_INTEGER:
1103 if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1105 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1107 case OSSL_PARAM_INTEGER:
1108 if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1110 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1122 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1125 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1129 #ifndef OPENSSL_SYS_UEFI
1130 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1135 if (val == NULL || p == NULL) {
1140 if (p->data_type == OSSL_PARAM_REAL) {
1141 switch (p->data_size) {
1142 case sizeof(double):
1143 *val = *(const double *)p->data;
1146 err_unsupported_real;
1148 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1149 switch (p->data_size) {
1150 case sizeof(uint32_t):
1151 *val = *(const uint32_t *)p->data;
1153 case sizeof(uint64_t):
1154 u64 = *(const uint64_t *)p->data;
1155 if ((u64 >> real_shift()) == 0) {
1162 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1163 switch (p->data_size) {
1164 case sizeof(int32_t):
1165 *val = *(const int32_t *)p->data;
1167 case sizeof(int64_t):
1168 i64 = *(const int64_t *)p->data;
1169 u64 = i64 < 0 ? -i64 : i64;
1170 if ((u64 >> real_shift()) == 0) {
1182 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1190 if (p->data_type == OSSL_PARAM_REAL) {
1191 p->return_size = sizeof(double);
1192 if (p->data == NULL)
1194 switch (p->data_size) {
1195 case sizeof(double):
1196 *(double *)p->data = val;
1199 err_unsupported_real;
1201 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1202 p->return_size = sizeof(double);
1203 if (p->data == NULL)
1205 if (val != (uint64_t)val) {
1209 switch (p->data_size) {
1210 case sizeof(uint32_t):
1211 if (val >= 0 && val <= UINT32_MAX) {
1212 p->return_size = sizeof(uint32_t);
1213 *(uint32_t *)p->data = (uint32_t)val;
1218 case sizeof(uint64_t):
1221 * By subtracting 65535 (2^16-1) we cancel the low order
1222 * 15 bits of UINT64_MAX to avoid using imprecise floating
1225 && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1226 p->return_size = sizeof(uint64_t);
1227 *(uint64_t *)p->data = (uint64_t)val;
1233 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1234 p->return_size = sizeof(double);
1235 if (p->data == NULL)
1237 if (val != (int64_t)val) {
1241 switch (p->data_size) {
1242 case sizeof(int32_t):
1243 if (val >= INT32_MIN && val <= INT32_MAX) {
1244 p->return_size = sizeof(int32_t);
1245 *(int32_t *)p->data = (int32_t)val;
1250 case sizeof(int64_t):
1251 if (val >= INT64_MIN
1253 * By subtracting 65535 (2^16-1) we cancel the low order
1254 * 15 bits of INT64_MAX to avoid using imprecise floating
1257 && val < (double)(INT64_MAX - 65535) + 65536.0) {
1258 p->return_size = sizeof(int64_t);
1259 *(int64_t *)p->data = (int64_t)val;
1270 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1272 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1276 static int get_string_internal(const OSSL_PARAM *p, void **val,
1277 size_t *max_len, size_t *used_len,
1280 size_t sz, alloc_sz;
1282 if ((val == NULL && used_len == NULL) || p == NULL) {
1286 if (p->data_type != type) {
1293 * If the input size is 0, or the input string needs NUL byte
1294 * termination, allocate an extra byte.
1296 alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1298 if (used_len != NULL)
1301 if (p->data == NULL) {
1310 char *const q = OPENSSL_malloc(alloc_sz);
1315 *max_len = alloc_sz;
1318 if (*max_len < sz) {
1322 memcpy(*val, p->data, sz);
1326 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1328 int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1329 OSSL_PARAM_UTF8_STRING);
1332 * We try to ensure that the copied string is terminated with a
1333 * NUL byte. That should be easy, just place a NUL byte at
1334 * |((char*)*val)[p->data_size]|.
1335 * Unfortunately, we have seen cases where |p->data_size| doesn't
1336 * correctly reflect the length of the string, and just happens
1337 * to be out of bounds according to |max_len|, so in that case, we
1338 * make the extra step of trying to find the true length of the
1339 * string that |p->data| points at, and use that as an index to
1340 * place the NUL byte in |*val|.
1342 size_t data_length = p->data_size;
1346 if (data_length >= max_len)
1347 data_length = OPENSSL_strnlen(p->data, data_length);
1348 if (data_length >= max_len) {
1349 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1350 return 0; /* No space for a terminating NUL byte */
1352 (*val)[data_length] = '\0';
1357 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1360 return get_string_internal(p, val, &max_len, used_len,
1361 OSSL_PARAM_OCTET_STRING);
1364 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1367 p->return_size = len;
1368 if (p->data == NULL)
1370 if (p->data_type != type) {
1374 if (p->data_size < len) {
1379 memcpy(p->data, val, len);
1380 /* If possible within the size of p->data, add a NUL terminator byte */
1381 if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1382 ((char *)p->data)[len] = '\0';
1386 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1398 return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1401 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1414 return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1417 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1420 if (buf != NULL && bsize == 0)
1421 bsize = strlen(buf);
1422 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1425 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1428 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1431 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1432 size_t *used_len, unsigned int type)
1434 if (val == NULL || p == NULL) {
1438 if (p->data_type != type) {
1442 if (used_len != NULL)
1443 *used_len = p->data_size;
1444 *val = *(const void **)p->data;
1448 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1450 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1453 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1456 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1459 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1460 unsigned int type, size_t len)
1462 p->return_size = len;
1463 if (p->data_type != type) {
1467 if (p->data != NULL)
1468 *(const void **)p->data = val;
1472 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1479 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1480 val == NULL ? 0 : strlen(val));
1483 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1491 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1494 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1497 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1500 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1503 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1507 * Extract the parameter into an allocated buffer.
1508 * Any existing allocation in *out is cleared and freed.
1510 * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1512 * *out and *out_len are guaranteed to be untouched if this function
1513 * doesn't return success.
1515 int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1516 unsigned char **out, size_t *out_len)
1518 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1527 && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1530 OPENSSL_clear_free(*out, *out_len);
1536 static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1537 unsigned char *out, size_t *outlen)
1543 if (!WPACKET_init_null(&pkt, 0))
1546 if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1550 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1551 if (p->data_type != OSSL_PARAM_OCTET_STRING)
1554 && p->data_size != 0
1555 && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1558 if (!WPACKET_get_total_written(&pkt, outlen)
1559 || !WPACKET_finish(&pkt))
1563 WPACKET_cleanup(&pkt);
1567 int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
1568 unsigned char **out,
1569 size_t *out_len, size_t maxsize)
1571 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1578 /* Calculate the total size */
1579 if (!setbuf_fromparams(p, name, NULL, &sz))
1582 /* Check that it's not oversized */
1583 if (maxsize > 0 && sz > maxsize)
1586 /* Special case zero length */
1588 if ((res = OPENSSL_zalloc(1)) == NULL)
1593 /* Allocate the buffer */
1594 res = OPENSSL_malloc(sz);
1598 /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1599 if (!setbuf_fromparams(p, name, res, &sz)) {
1600 OPENSSL_clear_free(res, sz);
1605 OPENSSL_clear_free(*out, *out_len);
1611 OSSL_PARAM OSSL_PARAM_construct_end(void)
1613 OSSL_PARAM end = OSSL_PARAM_END;
1618 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1619 size_t *used_len, unsigned int type)
1621 if (val == NULL || p == NULL) {
1625 if (p->data_type != type) {
1629 if (used_len != NULL)
1630 *used_len = p->data_size;
1635 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1640 rv = OSSL_PARAM_get_utf8_ptr(p, val);
1643 return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1644 OSSL_PARAM_UTF8_STRING);
1647 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1653 rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1656 return rv || get_string_ptr_internal(p, val, used_len,
1657 OSSL_PARAM_OCTET_STRING);