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_MALLOC_FAILURE
);
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
)
1072 p
->return_size
= bytes
;
1073 if (p
->data
== NULL
)
1075 if (p
->data_size
>= bytes
) {
1076 p
->return_size
= p
->data_size
;
1078 switch (p
->data_type
) {
1079 case OSSL_PARAM_UNSIGNED_INTEGER
:
1080 if (BN_bn2nativepad(val
, p
->data
, p
->data_size
) >= 0)
1082 ERR_raise(ERR_LIB_CRYPTO
, CRYPTO_R_INTEGER_OVERFLOW
);
1084 case OSSL_PARAM_INTEGER
:
1085 if (BN_signed_bn2native(val
, p
->data
, p
->data_size
) >= 0)
1087 ERR_raise(ERR_LIB_CRYPTO
, CRYPTO_R_INTEGER_OVERFLOW
);
1099 OSSL_PARAM
OSSL_PARAM_construct_BN(const char *key
, unsigned char *buf
,
1102 return ossl_param_construct(key
, OSSL_PARAM_UNSIGNED_INTEGER
,
1106 int OSSL_PARAM_get_double(const OSSL_PARAM
*p
, double *val
)
1111 if (val
== NULL
|| p
== NULL
) {
1116 if (p
->data_type
== OSSL_PARAM_REAL
) {
1117 switch (p
->data_size
) {
1118 case sizeof(double):
1119 *val
= *(const double *)p
->data
;
1122 err_unsupported_real
;
1124 } else if (p
->data_type
== OSSL_PARAM_UNSIGNED_INTEGER
) {
1125 switch (p
->data_size
) {
1126 case sizeof(uint32_t):
1127 *val
= *(const uint32_t *)p
->data
;
1129 case sizeof(uint64_t):
1130 u64
= *(const uint64_t *)p
->data
;
1131 if ((u64
>> real_shift()) == 0) {
1138 } else if (p
->data_type
== OSSL_PARAM_INTEGER
) {
1139 switch (p
->data_size
) {
1140 case sizeof(int32_t):
1141 *val
= *(const int32_t *)p
->data
;
1143 case sizeof(int64_t):
1144 i64
= *(const int64_t *)p
->data
;
1145 u64
= i64
< 0 ? -i64
: i64
;
1146 if ((u64
>> real_shift()) == 0) {
1158 int OSSL_PARAM_set_double(OSSL_PARAM
*p
, double val
)
1166 if (p
->data_type
== OSSL_PARAM_REAL
) {
1167 p
->return_size
= sizeof(double);
1168 if (p
->data
== NULL
)
1170 switch (p
->data_size
) {
1171 case sizeof(double):
1172 *(double *)p
->data
= val
;
1175 err_unsupported_real
;
1177 } else if (p
->data_type
== OSSL_PARAM_UNSIGNED_INTEGER
) {
1178 p
->return_size
= sizeof(double);
1179 if (p
->data
== NULL
)
1181 if (val
!= (uint64_t)val
) {
1185 switch (p
->data_size
) {
1186 case sizeof(uint32_t):
1187 if (val
>= 0 && val
<= UINT32_MAX
) {
1188 p
->return_size
= sizeof(uint32_t);
1189 *(uint32_t *)p
->data
= (uint32_t)val
;
1194 case sizeof(uint64_t):
1197 * By subtracting 65535 (2^16-1) we cancel the low order
1198 * 15 bits of UINT64_MAX to avoid using imprecise floating
1201 && val
< (double)(UINT64_MAX
- 65535) + 65536.0) {
1202 p
->return_size
= sizeof(uint64_t);
1203 *(uint64_t *)p
->data
= (uint64_t)val
;
1209 } else if (p
->data_type
== OSSL_PARAM_INTEGER
) {
1210 p
->return_size
= sizeof(double);
1211 if (p
->data
== NULL
)
1213 if (val
!= (int64_t)val
) {
1217 switch (p
->data_size
) {
1218 case sizeof(int32_t):
1219 if (val
>= INT32_MIN
&& val
<= INT32_MAX
) {
1220 p
->return_size
= sizeof(int32_t);
1221 *(int32_t *)p
->data
= (int32_t)val
;
1226 case sizeof(int64_t):
1227 if (val
>= INT64_MIN
1229 * By subtracting 65535 (2^16-1) we cancel the low order
1230 * 15 bits of INT64_MAX to avoid using imprecise floating
1233 && val
< (double)(INT64_MAX
- 65535) + 65536.0) {
1234 p
->return_size
= sizeof(int64_t);
1235 *(int64_t *)p
->data
= (int64_t)val
;
1246 OSSL_PARAM
OSSL_PARAM_construct_double(const char *key
, double *buf
)
1248 return ossl_param_construct(key
, OSSL_PARAM_REAL
, buf
, sizeof(double));
1251 static int get_string_internal(const OSSL_PARAM
*p
, void **val
,
1252 size_t *max_len
, size_t *used_len
,
1255 size_t sz
, alloc_sz
;
1257 if ((val
== NULL
&& used_len
== NULL
) || p
== NULL
) {
1261 if (p
->data_type
!= type
) {
1268 * If the input size is 0, or the input string needs NUL byte
1269 * termination, allocate an extra byte.
1271 alloc_sz
= sz
+ (type
== OSSL_PARAM_UTF8_STRING
|| sz
== 0);
1273 if (used_len
!= NULL
)
1276 if (p
->data
== NULL
) {
1285 char *const q
= OPENSSL_malloc(alloc_sz
);
1288 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
1292 *max_len
= alloc_sz
;
1295 if (*max_len
< sz
) {
1299 memcpy(*val
, p
->data
, sz
);
1303 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM
*p
, char **val
, size_t max_len
)
1305 int ret
= get_string_internal(p
, (void **)val
, &max_len
, NULL
,
1306 OSSL_PARAM_UTF8_STRING
);
1309 * We try to ensure that the copied string is terminated with a
1310 * NUL byte. That should be easy, just place a NUL byte at
1311 * |((char*)*val)[p->data_size]|.
1312 * Unfortunately, we have seen cases where |p->data_size| doesn't
1313 * correctly reflect the length of the string, and just happens
1314 * to be out of bounds according to |max_len|, so in that case, we
1315 * make the extra step of trying to find the true length of the
1316 * string that |p->data| points at, and use that as an index to
1317 * place the NUL byte in |*val|.
1319 size_t data_length
= p
->data_size
;
1323 if (data_length
>= max_len
)
1324 data_length
= OPENSSL_strnlen(p
->data
, data_length
);
1325 if (data_length
>= max_len
) {
1326 ERR_raise(ERR_LIB_CRYPTO
, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL
);
1327 return 0; /* No space for a terminating NUL byte */
1329 (*val
)[data_length
] = '\0';
1334 int OSSL_PARAM_get_octet_string(const OSSL_PARAM
*p
, void **val
, size_t max_len
,
1337 return get_string_internal(p
, val
, &max_len
, used_len
,
1338 OSSL_PARAM_OCTET_STRING
);
1341 static int set_string_internal(OSSL_PARAM
*p
, const void *val
, size_t len
,
1344 p
->return_size
= len
;
1345 if (p
->data
== NULL
)
1347 if (p
->data_type
!= type
) {
1351 if (p
->data_size
< len
) {
1356 memcpy(p
->data
, val
, len
);
1357 /* If possible within the size of p->data, add a NUL terminator byte */
1358 if (type
== OSSL_PARAM_UTF8_STRING
&& p
->data_size
> len
)
1359 ((char *)p
->data
)[len
] = '\0';
1363 int OSSL_PARAM_set_utf8_string(OSSL_PARAM
*p
, const char *val
)
1375 return set_string_internal(p
, val
, strlen(val
), OSSL_PARAM_UTF8_STRING
);
1378 int OSSL_PARAM_set_octet_string(OSSL_PARAM
*p
, const void *val
,
1391 return set_string_internal(p
, val
, len
, OSSL_PARAM_OCTET_STRING
);
1394 OSSL_PARAM
OSSL_PARAM_construct_utf8_string(const char *key
, char *buf
,
1397 if (buf
!= NULL
&& bsize
== 0)
1398 bsize
= strlen(buf
);
1399 return ossl_param_construct(key
, OSSL_PARAM_UTF8_STRING
, buf
, bsize
);
1402 OSSL_PARAM
OSSL_PARAM_construct_octet_string(const char *key
, void *buf
,
1405 return ossl_param_construct(key
, OSSL_PARAM_OCTET_STRING
, buf
, bsize
);
1408 static int get_ptr_internal(const OSSL_PARAM
*p
, const void **val
,
1409 size_t *used_len
, unsigned int type
)
1411 if (val
== NULL
|| p
== NULL
) {
1415 if (p
->data_type
!= type
) {
1419 if (used_len
!= NULL
)
1420 *used_len
= p
->data_size
;
1421 *val
= *(const void **)p
->data
;
1425 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM
*p
, const char **val
)
1427 return get_ptr_internal(p
, (const void **)val
, NULL
, OSSL_PARAM_UTF8_PTR
);
1430 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM
*p
, const void **val
,
1433 return get_ptr_internal(p
, val
, used_len
, OSSL_PARAM_OCTET_PTR
);
1436 static int set_ptr_internal(OSSL_PARAM
*p
, const void *val
,
1437 unsigned int type
, size_t len
)
1439 p
->return_size
= len
;
1440 if (p
->data_type
!= type
) {
1444 if (p
->data
!= NULL
)
1445 *(const void **)p
->data
= val
;
1449 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM
*p
, const char *val
)
1456 return set_ptr_internal(p
, val
, OSSL_PARAM_UTF8_PTR
,
1457 val
== NULL
? 0 : strlen(val
));
1460 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM
*p
, const void *val
,
1468 return set_ptr_internal(p
, val
, OSSL_PARAM_OCTET_PTR
, used_len
);
1471 OSSL_PARAM
OSSL_PARAM_construct_utf8_ptr(const char *key
, char **buf
,
1474 return ossl_param_construct(key
, OSSL_PARAM_UTF8_PTR
, buf
, bsize
);
1477 OSSL_PARAM
OSSL_PARAM_construct_octet_ptr(const char *key
, void **buf
,
1480 return ossl_param_construct(key
, OSSL_PARAM_OCTET_PTR
, buf
, bsize
);
1483 OSSL_PARAM
OSSL_PARAM_construct_end(void)
1485 OSSL_PARAM end
= OSSL_PARAM_END
;
1490 static int get_string_ptr_internal(const OSSL_PARAM
*p
, const void **val
,
1491 size_t *used_len
, unsigned int type
)
1493 if (val
== NULL
|| p
== NULL
) {
1497 if (p
->data_type
!= type
) {
1501 if (used_len
!= NULL
)
1502 *used_len
= p
->data_size
;
1507 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM
*p
, const char **val
)
1512 rv
= OSSL_PARAM_get_utf8_ptr(p
, val
);
1515 return rv
|| get_string_ptr_internal(p
, (const void **)val
, NULL
,
1516 OSSL_PARAM_UTF8_STRING
);
1519 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM
*p
, const void **val
,
1525 rv
= OSSL_PARAM_get_octet_ptr(p
, val
, used_len
);
1528 return rv
|| get_string_ptr_internal(p
, val
, used_len
,
1529 OSSL_PARAM_OCTET_STRING
);