2 * Copyright 2019 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 "internal/thread_once.h"
15 #define SET_RETURN_SIZE(p, sz) \
16 if ((p)->return_size != NULL) \
17 *(p)->return_size = (sz)
19 const OSSL_PARAM *OSSL_PARAM_locate(const OSSL_PARAM *p, const char *key)
21 if (p != NULL && key != NULL)
22 for (; p->key != NULL; p++)
23 if (strcmp(key, p->key) == 0)
28 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
29 void *data, size_t data_size,
35 res.data_type = data_type;
37 res.data_size = data_size;
38 res.return_size = return_size;
42 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
44 switch (sizeof(int)) {
46 return OSSL_PARAM_get_int32(p, (int32_t *)val);
48 return OSSL_PARAM_get_int64(p, (int64_t *)val);
53 int OSSL_PARAM_set_int(const OSSL_PARAM *p, int val)
55 switch (sizeof(int)) {
57 return OSSL_PARAM_set_int32(p, (int32_t)val);
59 return OSSL_PARAM_set_int64(p, (int64_t)val);
64 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf, size_t *rsize)
66 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int),
70 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
72 switch (sizeof(unsigned int)) {
73 case sizeof(uint32_t):
74 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
75 case sizeof(uint64_t):
76 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
81 int OSSL_PARAM_set_uint(const OSSL_PARAM *p, unsigned int val)
83 switch (sizeof(unsigned int)) {
84 case sizeof(uint32_t):
85 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
86 case sizeof(uint64_t):
87 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
92 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf,
95 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
96 sizeof(unsigned int), rsize);
99 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
101 switch (sizeof(long int)) {
102 case sizeof(int32_t):
103 return OSSL_PARAM_get_int32(p, (int32_t *)val);
104 case sizeof(int64_t):
105 return OSSL_PARAM_get_int64(p, (int64_t *)val);
110 int OSSL_PARAM_set_long(const OSSL_PARAM *p, long int val)
112 switch (sizeof(long int)) {
113 case sizeof(int32_t):
114 return OSSL_PARAM_set_int32(p, (int32_t)val);
115 case sizeof(int64_t):
116 return OSSL_PARAM_set_int64(p, (int64_t)val);
121 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf,
124 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int),
128 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
130 switch (sizeof(unsigned long int)) {
131 case sizeof(uint32_t):
132 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
133 case sizeof(uint64_t):
134 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
139 int OSSL_PARAM_set_ulong(const OSSL_PARAM *p, unsigned long int val)
141 switch (sizeof(unsigned long int)) {
142 case sizeof(uint32_t):
143 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
144 case sizeof(uint64_t):
145 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
150 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf,
153 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
154 sizeof(unsigned long int), rsize);
157 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
164 if (val == NULL || p == NULL )
167 if (p->data_type == OSSL_PARAM_INTEGER) {
168 switch (p->data_size) {
169 case sizeof(int32_t):
170 *val = *(const int32_t *)p->data;
172 case sizeof(int64_t):
173 i64 = *(const int64_t *)p->data;
174 if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
180 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
181 switch (p->data_size) {
182 case sizeof(uint32_t):
183 u32 = *(const uint32_t *)p->data;
184 if (u32 <= INT32_MAX) {
189 case sizeof(uint64_t):
190 u64 = *(const uint64_t *)p->data;
191 if (u64 <= INT32_MAX) {
197 } else if (p->data_type == OSSL_PARAM_REAL) {
198 switch (p->data_size) {
200 d = *(const double *)p->data;
201 if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
211 int OSSL_PARAM_set_int32(const OSSL_PARAM *p, int32_t val)
215 SET_RETURN_SIZE(p, 0);
216 if (p->data_type == OSSL_PARAM_INTEGER) {
217 SET_RETURN_SIZE(p, sizeof(int32_t)); /* Minimum expected size */
218 switch (p->data_size) {
219 case sizeof(int32_t):
220 *(int32_t *)p->data = val;
222 case sizeof(int64_t):
223 SET_RETURN_SIZE(p, sizeof(int64_t));
224 *(int64_t *)p->data = (int64_t)val;
227 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
228 SET_RETURN_SIZE(p, sizeof(uint32_t)); /* Minimum expected size */
229 switch (p->data_size) {
230 case sizeof(uint32_t):
231 *(uint32_t *)p->data = (uint32_t)val;
233 case sizeof(uint64_t):
234 SET_RETURN_SIZE(p, sizeof(uint64_t));
235 *(uint64_t *)p->data = (uint64_t)val;
238 } else if (p->data_type == OSSL_PARAM_REAL) {
239 SET_RETURN_SIZE(p, sizeof(double));
240 switch (p->data_size) {
242 *(double *)p->data = (double)val;
249 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf,
252 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
253 sizeof(int32_t), rsize);
256 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
263 if (val == NULL || p == NULL)
266 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
267 switch (p->data_size) {
268 case sizeof(uint32_t):
269 *val = *(const uint32_t *)p->data;
271 case sizeof(uint64_t):
272 u64 = *(const uint64_t *)p->data;
273 if (u64 <= UINT32_MAX) {
274 *val = (uint32_t)u64;
279 } else if (p->data_type == OSSL_PARAM_INTEGER) {
280 switch (p->data_size) {
281 case sizeof(int32_t):
282 i32 = *(const int32_t *)p->data;
288 case sizeof(int64_t):
289 i64 = *(const int64_t *)p->data;
290 if (i64 >= 0 && i64 <= UINT32_MAX) {
291 *val = (uint32_t)i64;
296 } else if (p->data_type == OSSL_PARAM_REAL) {
297 switch (p->data_size) {
299 d = *(const double *)p->data;
300 if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
310 int OSSL_PARAM_set_uint32(const OSSL_PARAM *p, uint32_t val)
314 SET_RETURN_SIZE(p, 0);
316 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
317 SET_RETURN_SIZE(p, sizeof(uint32_t)); /* Minimum expected size */
318 switch (p->data_size) {
319 case sizeof(uint32_t):
320 *(uint32_t *)p->data = val;
322 case sizeof(uint64_t):
323 SET_RETURN_SIZE(p, sizeof(uint64_t));
324 *(uint64_t *)p->data = val;
327 } else if (p->data_type == OSSL_PARAM_INTEGER) {
328 SET_RETURN_SIZE(p, sizeof(int32_t)); /* Minimum expected size */
329 switch (p->data_size) {
330 case sizeof(int32_t):
331 if (val <= INT32_MAX) {
332 *(int32_t *)p->data = (int32_t)val;
336 case sizeof(int64_t):
337 SET_RETURN_SIZE(p, sizeof(int64_t));
338 *(int64_t *)p->data = (int64_t)val;
341 } else if (p->data_type == OSSL_PARAM_REAL) {
342 SET_RETURN_SIZE(p, sizeof(double));
343 switch (p->data_size) {
345 *(double *)p->data = (double)val;
352 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf,
355 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
356 sizeof(uint32_t), rsize);
359 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
364 if (val == NULL || p == NULL )
367 if (p->data_type == OSSL_PARAM_INTEGER) {
368 switch (p->data_size) {
369 case sizeof(int32_t):
370 *val = *(const int32_t *)p->data;
372 case sizeof(int64_t):
373 *val = *(const int64_t *)p->data;
376 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
377 switch (p->data_size) {
378 case sizeof(uint32_t):
379 *val = *(const uint32_t *)p->data;
381 case sizeof(uint64_t):
382 u64 = *(const uint64_t *)p->data;
383 if (u64 <= INT64_MAX) {
389 } else if (p->data_type == OSSL_PARAM_REAL) {
390 switch (p->data_size) {
392 d = *(const double *)p->data;
393 if (d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d) {
403 int OSSL_PARAM_set_int64(const OSSL_PARAM *p, int64_t val)
409 SET_RETURN_SIZE(p, 0);
410 if (p->data_type == OSSL_PARAM_INTEGER) {
411 SET_RETURN_SIZE(p, sizeof(int64_t)); /* Expected size */
412 switch (p->data_size) {
413 case sizeof(int32_t):
414 if (val >= INT32_MIN && val <= INT32_MAX) {
415 SET_RETURN_SIZE(p, sizeof(int32_t));
416 *(int32_t *)p->data = (int32_t)val;
420 case sizeof(int64_t):
421 *(int64_t *)p->data = val;
424 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
425 SET_RETURN_SIZE(p, sizeof(uint64_t)); /* Expected size */
426 switch (p->data_size) {
427 case sizeof(uint32_t):
428 if (val <= UINT32_MAX) {
429 SET_RETURN_SIZE(p, sizeof(uint32_t));
430 *(uint32_t *)p->data = (uint32_t)val;
434 case sizeof(uint64_t):
435 *(uint64_t *)p->data = (uint64_t)val;
438 } else if (p->data_type == OSSL_PARAM_REAL) {
439 SET_RETURN_SIZE(p, sizeof(double));
440 switch (p->data_size) {
442 u64 = val < 0 ? -val : val;
443 if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
444 *(double *)p->data = (double)val;
453 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf,
456 return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t),
460 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
466 if (val == NULL || p == NULL)
469 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
470 switch (p->data_size) {
471 case sizeof(uint32_t):
472 *val = *(const uint32_t *)p->data;
474 case sizeof(uint64_t):
475 *val = *(const uint64_t *)p->data;
478 } else if (p->data_type == OSSL_PARAM_INTEGER) {
479 switch (p->data_size) {
480 case sizeof(int32_t):
481 i32 = *(const int32_t *)p->data;
483 *val = (uint64_t)i32;
487 case sizeof(int64_t):
488 i64 = *(const int64_t *)p->data;
490 *val = (uint64_t)i64;
495 } else if (p->data_type == OSSL_PARAM_REAL) {
496 switch (p->data_size) {
498 d = *(const double *)p->data;
499 if (d >= 0 && d <= INT64_MAX && d == (uint64_t)d) {
509 int OSSL_PARAM_set_uint64(const OSSL_PARAM *p, uint64_t val)
513 SET_RETURN_SIZE(p, 0);
515 if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
516 SET_RETURN_SIZE(p, sizeof(uint64_t)); /* Expected size */
517 switch (p->data_size) {
518 case sizeof(uint32_t):
519 if (val <= UINT32_MAX) {
520 SET_RETURN_SIZE(p, sizeof(uint32_t));
521 *(uint32_t *)p->data = (uint32_t)val;
525 case sizeof(uint64_t):
526 *(uint64_t *)p->data = val;
529 } else if (p->data_type == OSSL_PARAM_INTEGER) {
530 SET_RETURN_SIZE(p, sizeof(int64_t)); /* Expected size */
531 switch (p->data_size) {
532 case sizeof(int32_t):
533 if (val <= INT32_MAX) {
534 SET_RETURN_SIZE(p, sizeof(int32_t));
535 *(int32_t *)p->data = (int32_t)val;
539 case sizeof(int64_t):
540 if (val <= INT64_MAX) {
541 *(int64_t *)p->data = (int64_t)val;
546 } else if (p->data_type == OSSL_PARAM_REAL) {
547 SET_RETURN_SIZE(p, sizeof(double));
548 switch (p->data_size) {
550 if ((val >> 53) == 0) { /* 53 significant bits in the mantissa */
551 *(double *)p->data = (double)val;
560 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf,
562 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
563 sizeof(uint64_t), rsize);
566 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
568 switch (sizeof(size_t)) {
569 case sizeof(uint32_t):
570 return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
571 case sizeof(uint64_t):
572 return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
577 int OSSL_PARAM_set_size_t(const OSSL_PARAM *p, size_t val)
579 switch (sizeof(size_t)) {
580 case sizeof(uint32_t):
581 return OSSL_PARAM_set_uint32(p, (uint32_t)val);
582 case sizeof(uint64_t):
583 return OSSL_PARAM_set_uint64(p, (uint64_t)val);
588 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf,
591 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
592 sizeof(size_t), rsize); }
596 * TODO(3.0): Make this available in FIPS mode.
598 * Temporarily we don't include these functions in FIPS mode to avoid pulling
599 * in the entire BN sub-library into the module at this point.
601 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
607 || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
610 b = BN_native2bn(p->data, (int)p->data_size, *val);
618 int OSSL_PARAM_set_BN(const OSSL_PARAM *p, const BIGNUM *val)
624 SET_RETURN_SIZE(p, 0);
625 if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
628 /* For the moment, only positive values are permitted */
629 if (BN_is_negative(val))
632 bytes = (size_t)BN_num_bytes(val);
633 SET_RETURN_SIZE(p, bytes);
634 return p->data_size >= bytes
635 && BN_bn2nativepad(val, p->data, bytes) >= 0;
638 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
639 size_t bsize, size_t *rsize)
641 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
646 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
651 if (val == NULL || p == NULL)
654 if (p->data_type == OSSL_PARAM_REAL) {
655 switch (p->data_size) {
657 *val = *(const double *)p->data;
660 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
661 switch (p->data_size) {
662 case sizeof(uint32_t):
663 *val = *(const uint32_t *)p->data;
665 case sizeof(uint64_t):
666 u64 = *(const uint64_t *)p->data;
667 if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
673 } else if (p->data_type == OSSL_PARAM_INTEGER) {
674 switch (p->data_size) {
675 case sizeof(int32_t):
676 *val = *(const int32_t *)p->data;
678 case sizeof(int64_t):
679 i64 = *(const int64_t *)p->data;
680 u64 = i64 < 0 ? -i64 : i64;
681 if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
691 int OSSL_PARAM_set_double(const OSSL_PARAM *p, double val)
695 SET_RETURN_SIZE(p, 0);
697 if (p->data_type == OSSL_PARAM_REAL) {
698 SET_RETURN_SIZE(p, sizeof(double));
699 switch (p->data_size) {
701 *(double *)p->data = val;
704 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
705 && val == (uintmax_t)val) {
706 SET_RETURN_SIZE(p, sizeof(double));
707 switch (p->data_size) {
708 case sizeof(uint32_t):
709 if (val >= 0 && val <= UINT32_MAX) {
710 SET_RETURN_SIZE(p, sizeof(uint32_t));
711 *(uint32_t *)p->data = (uint32_t)val;
715 case sizeof(uint64_t):
716 if (val >= 0 && val <= UINT64_MAX) {
717 SET_RETURN_SIZE(p, sizeof(uint64_t));
718 *(uint64_t *)p->data = (uint64_t)val;
722 } else if (p->data_type == OSSL_PARAM_INTEGER && val == (intmax_t)val) {
723 SET_RETURN_SIZE(p, sizeof(double));
724 switch (p->data_size) {
725 case sizeof(int32_t):
726 if (val >= INT32_MIN && val <= INT32_MAX) {
727 SET_RETURN_SIZE(p, sizeof(int32_t));
728 *(int32_t *)p->data = (int32_t)val;
732 case sizeof(int64_t):
733 if (val >= INT64_MIN && val <= INT64_MAX) {
734 SET_RETURN_SIZE(p, sizeof(int64_t));
735 *(int64_t *)p->data = (int64_t)val;
744 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf,
747 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double),
751 static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len,
752 size_t *used_len, unsigned int type)
756 if (val == NULL || p == NULL || p->data_type != type)
761 if (used_len != NULL)
765 char *const q = OPENSSL_malloc(sz);
770 memcpy(q, p->data, sz);
775 memcpy(*val, p->data, sz);
779 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
781 return get_string_internal(p, (void **)val, max_len, NULL,
782 OSSL_PARAM_UTF8_STRING);
785 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
788 return get_string_internal(p, val, max_len, used_len,
789 OSSL_PARAM_OCTET_STRING);
792 static int set_string_internal(const OSSL_PARAM *p, const void *val, size_t len,
795 SET_RETURN_SIZE(p, len);
796 if (p->data_type != type || p->data_size < len)
799 memcpy(p->data, val, len);
803 int OSSL_PARAM_set_utf8_string(const OSSL_PARAM *p, const char *val)
808 SET_RETURN_SIZE(p, 0);
811 return set_string_internal(p, val, strlen(val) + 1, OSSL_PARAM_UTF8_STRING);
814 int OSSL_PARAM_set_octet_string(const OSSL_PARAM *p, const void *val,
820 SET_RETURN_SIZE(p, 0);
823 return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
826 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
827 size_t bsize, size_t *rsize)
829 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize,
833 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
834 size_t bsize, size_t *rsize)
836 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize,
840 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
841 size_t *used_len, unsigned int type)
843 if (val == NULL || p == NULL || p->data_type != type)
845 if (used_len != NULL)
846 *used_len = p->data_size;
847 *val = *(const void **)p->data;
851 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
853 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
856 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
859 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
862 static int set_ptr_internal(const OSSL_PARAM *p, const void *val,
863 unsigned int type, size_t len)
865 SET_RETURN_SIZE(p, len);
866 if (p->data_type != type)
868 *(const void **)p->data = val;
872 int OSSL_PARAM_set_utf8_ptr(const OSSL_PARAM *p, const char *val)
876 SET_RETURN_SIZE(p, 0);
879 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR, strlen(val) + 1);
882 int OSSL_PARAM_set_octet_ptr(const OSSL_PARAM *p, const void *val,
887 SET_RETURN_SIZE(p, 0);
890 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
893 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
894 size_t bsize, size_t *rsize)
896 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize, rsize);
899 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
900 size_t bsize, size_t *rsize)
902 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize, rsize);
905 OSSL_PARAM OSSL_PARAM_construct_end(void)
907 OSSL_PARAM end = OSSL_PARAM_END;