crypto/params: drop float for UEFI
[openssl.git] / crypto / params.c
1 /*
2  * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4  *
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
9  */
10
11 #include <string.h>
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"
19
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)
27 #define err_inexact           \
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)
40
41 #ifndef OPENSSL_SYS_UEFI
42 /*
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
45  * double.
46  */
47 static unsigned int real_shift(void)
48 {
49     return sizeof(double) == 4 ? 24 : 53;
50 }
51 #endif
52
53 OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
54 {
55     if (p != NULL && key != NULL)
56         for (; p->key != NULL; p++)
57             if (strcmp(key, p->key) == 0)
58                 return p;
59     return NULL;
60 }
61
62 const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
63 {
64     return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
65 }
66
67 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
68                                        void *data, size_t data_size)
69 {
70     OSSL_PARAM res;
71
72     res.key = key;
73     res.data_type = data_type;
74     res.data = data;
75     res.data_size = data_size;
76     res.return_size = OSSL_PARAM_UNMODIFIED;
77     return res;
78 }
79
80 int OSSL_PARAM_modified(const OSSL_PARAM *p)
81 {
82     return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
83 }
84
85 void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
86 {
87     if (p != NULL)
88         while (p->key != NULL)
89             p++->return_size = OSSL_PARAM_UNMODIFIED;
90 }
91
92 /* Return non-zero if the signed number is negative */
93 static int is_negative(const void *number, size_t s)
94 {
95     const unsigned char *n = number;
96     DECLARE_IS_ENDIAN;
97
98     return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
99 }
100
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)
103 {
104     size_t i;
105
106     for (i = 0; i < n; i++)
107         if (p[i] != s)
108             return 0;
109     return 1;
110 }
111
112 /*
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.
116  */
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)
120 {
121     size_t n;
122     DECLARE_IS_ENDIAN;
123
124     if (IS_BIG_ENDIAN) {
125         if (src_len < dest_len) {
126             n = dest_len - src_len;
127             memset(dest, pad, n);
128             memcpy(dest + n, src, src_len);
129         } else {
130             n = src_len - dest_len;
131             if (!check_sign_bytes(src, n, pad)
132                     /*
133                      * Shortening a signed value must retain the correct sign.
134                      * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
135                      */
136                     || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
137                 err_out_of_range;
138                 return 0;
139             }
140             memcpy(dest, src + n, dest_len);
141         }
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);
147         } else {
148             n = src_len - dest_len;
149             if (!check_sign_bytes(src + dest_len, n, pad)
150                     /*
151                      * Shortening a signed value must retain the correct sign.
152                      * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
153                      */
154                     || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
155                 err_out_of_range;
156                 return 0;
157             }
158             memcpy(dest, src, dest_len);
159         }
160     }
161     return 1;
162 }
163
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)
167 {
168     return copy_integer(dest, dest_len, src, src_len,
169                         is_negative(src, src_len) ? 0xff : 0, 1);
170 }
171
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)
175 {
176     return copy_integer(dest, dest_len, src, src_len, 0, 1);
177 }
178
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)
182 {
183     if (is_negative(src, src_len)) {
184         err_unsigned_negative;
185         return 0;
186     }
187     return copy_integer(dest, dest_len, src, src_len, 0, 0);
188 }
189
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)
193 {
194     return copy_integer(dest, dest_len, src, src_len, 0, 0);
195 }
196
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)
199 {
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);
204     err_not_integer;
205     return 0;
206 }
207
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)
210 {
211     int r = 0;
212
213     p->return_size = val_size; /* Expected size */
214     if (p->data == NULL)
215         return 1;
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);
220     else
221         err_not_integer;
222     p->return_size = r ? p->data_size : val_size;
223     return r;
224 }
225
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)
228 {
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);
233     err_not_integer;
234     return 0;
235 }
236
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)
239 {
240     int r = 0;
241
242     p->return_size = val_size; /* Expected size */
243     if (p->data == NULL)
244         return 1;
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);
249     else
250         err_not_integer;
251     p->return_size = r ? p->data_size : val_size;
252     return r;
253 }
254
255 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
256 {
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);
263     }
264 #endif
265     return general_get_int(p, val, sizeof(*val));
266 }
267
268 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
269 {
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);
276     }
277 #endif
278     return general_set_int(p, &val, sizeof(val));
279 }
280
281 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
282 {
283     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
284 }
285
286 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
287 {
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);
294     }
295 #endif
296     return general_get_uint(p, val, sizeof(*val));
297 }
298
299 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
300 {
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);
307     }
308 #endif
309     return general_set_uint(p, &val, sizeof(val));
310 }
311
312 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
313 {
314     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
315                                 sizeof(unsigned int));
316 }
317
318 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
319 {
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);
326     }
327 #endif
328     return general_get_int(p, val, sizeof(*val));
329 }
330
331 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
332 {
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);
339     }
340 #endif
341     return general_set_int(p, &val, sizeof(val));
342 }
343
344 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
345 {
346     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
347 }
348
349 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
350 {
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);
357     }
358 #endif
359     return general_get_uint(p, val, sizeof(*val));
360 }
361
362 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
363 {
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);
370     }
371 #endif
372     return general_set_uint(p, &val, sizeof(val));
373 }
374
375 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
376 {
377     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
378                                 sizeof(unsigned long int));
379 }
380
381 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
382 {
383     if (val == NULL || p == NULL) {
384         err_null_argument;
385         return 0;
386     }
387
388     if (p->data_type == OSSL_PARAM_INTEGER) {
389 #ifndef OPENSSL_SMALL_FOOTPRINT
390         int64_t i64;
391
392         switch (p->data_size) {
393         case sizeof(int32_t):
394             *val = *(const int32_t *)p->data;
395             return 1;
396         case sizeof(int64_t):
397             i64 = *(const int64_t *)p->data;
398             if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
399                 *val = (int32_t)i64;
400                 return 1;
401             }
402             err_out_of_range;
403             return 0;
404         }
405 #endif
406         return general_get_int(p, val, sizeof(*val));
407
408     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
409 #ifndef OPENSSL_SMALL_FOOTPRINT
410         uint32_t u32;
411         uint64_t u64;
412
413         switch (p->data_size) {
414         case sizeof(uint32_t):
415             u32 = *(const uint32_t *)p->data;
416             if (u32 <= INT32_MAX) {
417                 *val = (int32_t)u32;
418                 return 1;
419             }
420             err_out_of_range;
421             return 0;
422         case sizeof(uint64_t):
423             u64 = *(const uint64_t *)p->data;
424             if (u64 <= INT32_MAX) {
425                 *val = (int32_t)u64;
426                 return 1;
427             }
428             err_out_of_range;
429             return 0;
430         }
431 #endif
432         return general_get_int(p, val, sizeof(*val));
433
434     } else if (p->data_type == OSSL_PARAM_REAL) {
435 #ifndef OPENSSL_SYS_UEFI
436         double d;
437
438         switch (p->data_size) {
439         case sizeof(double):
440             d = *(const double *)p->data;
441             if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
442                 *val = (int32_t)d;
443                 return 1;
444             }
445             err_out_of_range;
446             return 0;
447         }
448         err_unsupported_real;
449         return 0;
450 #endif
451     }
452     err_bad_type;
453     return 0;
454 }
455
456 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
457 {
458     uint32_t u32;
459     unsigned int shift;
460
461     if (p == NULL) {
462         err_null_argument;
463         return 0;
464     }
465     p->return_size = 0;
466     if (p->data_type == OSSL_PARAM_INTEGER) {
467 #ifndef OPENSSL_SMALL_FOOTPRINT
468         p->return_size = sizeof(int32_t); /* Minimum expected size */
469         if (p->data == NULL)
470             return 1;
471         switch (p->data_size) {
472         case sizeof(int32_t):
473             *(int32_t *)p->data = val;
474             return 1;
475         case sizeof(int64_t):
476             p->return_size = sizeof(int64_t);
477             *(int64_t *)p->data = (int64_t)val;
478             return 1;
479         }
480 #endif
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 */
485         if (p->data == NULL)
486             return 1;
487         switch (p->data_size) {
488         case sizeof(uint32_t):
489             *(uint32_t *)p->data = (uint32_t)val;
490             return 1;
491         case sizeof(uint64_t):
492             p->return_size = sizeof(uint64_t);
493             *(uint64_t *)p->data = (uint64_t)val;
494             return 1;
495         }
496 #endif
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);
501         if (p->data == NULL)
502             return 1;
503         switch (p->data_size) {
504         case sizeof(double):
505             shift = real_shift();
506             if (shift < 8 * sizeof(val) - 1) {
507                 u32 = val < 0 ? -val : val;
508                 if ((u32 >> shift) != 0) {
509                     err_inexact;
510                     return 0;
511                 }
512             }
513             *(double *)p->data = (double)val;
514             return 1;
515         }
516         err_unsupported_real;
517         return 0;
518 #endif
519     }
520     err_bad_type;
521     return 0;
522 }
523
524 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
525 {
526     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
527                                 sizeof(int32_t));
528 }
529
530 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
531 {
532     if (val == NULL || p == NULL) {
533         err_null_argument;
534         return 0;
535     }
536
537     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
538 #ifndef OPENSSL_SMALL_FOOTPRINT
539         uint64_t u64;
540
541         switch (p->data_size) {
542         case sizeof(uint32_t):
543             *val = *(const uint32_t *)p->data;
544             return 1;
545         case sizeof(uint64_t):
546             u64 = *(const uint64_t *)p->data;
547             if (u64 <= UINT32_MAX) {
548                 *val = (uint32_t)u64;
549                 return 1;
550             }
551             err_out_of_range;
552             return 0;
553         }
554 #endif
555         return general_get_uint(p, val, sizeof(*val));
556     } else if (p->data_type == OSSL_PARAM_INTEGER) {
557 #ifndef OPENSSL_SMALL_FOOTPRINT
558         int32_t i32;
559         int64_t i64;
560
561         switch (p->data_size) {
562         case sizeof(int32_t):
563             i32 = *(const int32_t *)p->data;
564             if (i32 >= 0) {
565                 *val = i32;
566                 return 1;
567             }
568             err_unsigned_negative;
569             return 0;
570         case sizeof(int64_t):
571             i64 = *(const int64_t *)p->data;
572             if (i64 >= 0 && i64 <= UINT32_MAX) {
573                 *val = (uint32_t)i64;
574                 return 1;
575             }
576             if (i64 < 0)
577                 err_unsigned_negative;
578             else
579                 err_out_of_range;
580             return 0;
581         }
582 #endif
583         return general_get_uint(p, val, sizeof(*val));
584     } else if (p->data_type == OSSL_PARAM_REAL) {
585 #ifndef OPENSSL_SYS_UEFI
586         double d;
587
588         switch (p->data_size) {
589         case sizeof(double):
590             d = *(const double *)p->data;
591             if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
592                 *val = (uint32_t)d;
593                 return 1;
594             }
595             err_inexact;
596             return 0;
597         }
598         err_unsupported_real;
599         return 0;
600 #endif
601     }
602     err_bad_type;
603     return 0;
604 }
605
606 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
607 {
608     unsigned int shift;
609
610     if (p == NULL) {
611         err_null_argument;
612         return 0;
613     }
614     p->return_size = 0;
615
616     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
617 #ifndef OPENSSL_SMALL_FOOTPRINT
618         p->return_size = sizeof(uint32_t); /* Minimum expected size */
619         if (p->data == NULL)
620             return 1;
621         switch (p->data_size) {
622         case sizeof(uint32_t):
623             *(uint32_t *)p->data = val;
624             return 1;
625         case sizeof(uint64_t):
626             p->return_size = sizeof(uint64_t);
627             *(uint64_t *)p->data = val;
628             return 1;
629         }
630 #endif
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 */
635         if (p->data == NULL)
636             return 1;
637         switch (p->data_size) {
638         case sizeof(int32_t):
639             if (val <= INT32_MAX) {
640                 *(int32_t *)p->data = (int32_t)val;
641                 return 1;
642             }
643             err_out_of_range;
644             return 0;
645         case sizeof(int64_t):
646             p->return_size = sizeof(int64_t);
647             *(int64_t *)p->data = (int64_t)val;
648             return 1;
649         }
650 #endif
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);
655         if (p->data == NULL)
656             return 1;
657         switch (p->data_size) {
658         case sizeof(double):
659             shift = real_shift();
660             if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
661                 err_inexact;
662                 return 0;
663             }
664             *(double *)p->data = (double)val;
665             return 1;
666         }
667         err_unsupported_real;
668         return 0;
669 #endif
670     }
671     err_bad_type;
672     return 0;
673 }
674
675 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
676 {
677     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
678                                 sizeof(uint32_t));
679 }
680
681 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
682 {
683     if (val == NULL || p == NULL) {
684         err_null_argument;
685         return 0;
686     }
687
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;
693             return 1;
694         case sizeof(int64_t):
695             *val = *(const int64_t *)p->data;
696             return 1;
697         }
698 #endif
699         return general_get_int(p, val, sizeof(*val));
700     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
701 #ifndef OPENSSL_SMALL_FOOTPRINT
702         uint64_t u64;
703
704         switch (p->data_size) {
705         case sizeof(uint32_t):
706             *val = *(const uint32_t *)p->data;
707             return 1;
708         case sizeof(uint64_t):
709             u64 = *(const uint64_t *)p->data;
710             if (u64 <= INT64_MAX) {
711                 *val = (int64_t)u64;
712                 return 1;
713             }
714             err_out_of_range;
715             return 0;
716         }
717 #endif
718         return general_get_int(p, val, sizeof(*val));
719     } else if (p->data_type == OSSL_PARAM_REAL) {
720 #ifndef OPENSSL_SYS_UEFI
721         double d;
722
723         switch (p->data_size) {
724         case sizeof(double):
725             d = *(const double *)p->data;
726             if (d >= INT64_MIN
727                     /*
728                      * By subtracting 65535 (2^16-1) we cancel the low order
729                      * 15 bits of INT64_MAX to avoid using imprecise floating
730                      * point values.
731                      */
732                     && d < (double)(INT64_MAX - 65535) + 65536.0
733                     && d == (int64_t)d) {
734                 *val = (int64_t)d;
735                 return 1;
736             }
737             err_inexact;
738             return 0;
739         }
740         err_unsupported_real;
741         return 0;
742 #endif
743     }
744     err_bad_type;
745     return 0;
746 }
747
748 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
749 {
750     if (p == NULL) {
751         err_null_argument;
752         return 0;
753     }
754     p->return_size = 0;
755     if (p->data_type == OSSL_PARAM_INTEGER) {
756 #ifndef OPENSSL_SMALL_FOOTPRINT
757         p->return_size = sizeof(int64_t); /* Expected size */
758         if (p->data == NULL)
759             return 1;
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;
765                 return 1;
766             }
767             err_out_of_range;
768             return 0;
769         case sizeof(int64_t):
770             *(int64_t *)p->data = val;
771             return 1;
772         }
773 #endif
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 */
778         if (p->data == NULL)
779             return 1;
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;
785                 return 1;
786             }
787             err_out_of_range;
788             return 0;
789         case sizeof(uint64_t):
790             *(uint64_t *)p->data = (uint64_t)val;
791             return 1;
792         }
793 #endif
794         return general_set_int(p, &val, sizeof(val));
795     } else if (p->data_type == OSSL_PARAM_REAL) {
796 #ifndef OPENSSL_SYS_UEFI
797         uint64_t u64;
798
799         p->return_size = sizeof(double);
800         if (p->data == NULL)
801             return 1;
802         switch (p->data_size) {
803         case sizeof(double):
804             u64 = val < 0 ? -val : val;
805             if ((u64 >> real_shift()) == 0) {
806                 *(double *)p->data = (double)val;
807                 return 1;
808             }
809             err_inexact;
810             return 0;
811         }
812         err_unsupported_real;
813         return 0;
814 #endif
815     }
816     err_bad_type;
817     return 0;
818 }
819
820 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
821 {
822     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
823 }
824
825 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
826 {
827     if (val == NULL || p == NULL) {
828         err_null_argument;
829         return 0;
830     }
831
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;
837             return 1;
838         case sizeof(uint64_t):
839             *val = *(const uint64_t *)p->data;
840             return 1;
841         }
842 #endif
843         return general_get_uint(p, val, sizeof(*val));
844     } else if (p->data_type == OSSL_PARAM_INTEGER) {
845 #ifndef OPENSSL_SMALL_FOOTPRINT
846         int32_t i32;
847         int64_t i64;
848
849         switch (p->data_size) {
850         case sizeof(int32_t):
851             i32 = *(const int32_t *)p->data;
852             if (i32 >= 0) {
853                 *val = (uint64_t)i32;
854                 return 1;
855             }
856             err_unsigned_negative;
857             return 0;
858         case sizeof(int64_t):
859             i64 = *(const int64_t *)p->data;
860             if (i64 >= 0) {
861                 *val = (uint64_t)i64;
862                 return 1;
863             }
864             err_unsigned_negative;
865             return 0;
866         }
867 #endif
868         return general_get_uint(p, val, sizeof(*val));
869     } else if (p->data_type == OSSL_PARAM_REAL) {
870 #ifndef OPENSSL_SYS_UEFI
871         double d;
872
873         switch (p->data_size) {
874         case sizeof(double):
875             d = *(const double *)p->data;
876             if (d >= 0
877                     /*
878                      * By subtracting 65535 (2^16-1) we cancel the low order
879                      * 15 bits of UINT64_MAX to avoid using imprecise floating
880                      * point values.
881                      */
882                     && d < (double)(UINT64_MAX - 65535) + 65536.0
883                     && d == (uint64_t)d) {
884                 *val = (uint64_t)d;
885                 return 1;
886             }
887             err_inexact;
888             return 0;
889         }
890         err_unsupported_real;
891         return 0;
892 #endif
893     }
894     err_bad_type;
895     return 0;
896 }
897
898 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
899 {
900     if (p == NULL) {
901         err_null_argument;
902         return 0;
903     }
904     p->return_size = 0;
905
906     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
907 #ifndef OPENSSL_SMALL_FOOTPRINT
908         p->return_size = sizeof(uint64_t); /* Expected size */
909         if (p->data == NULL)
910             return 1;
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;
916                 return 1;
917             }
918             err_out_of_range;
919             return 0;
920         case sizeof(uint64_t):
921             *(uint64_t *)p->data = val;
922             return 1;
923         }
924 #endif
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 */
929         if (p->data == NULL)
930             return 1;
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;
936                 return 1;
937             }
938             err_out_of_range;
939             return 0;
940         case sizeof(int64_t):
941             if (val <= INT64_MAX) {
942                 *(int64_t *)p->data = (int64_t)val;
943                 return 1;
944             }
945             err_out_of_range;
946             return 0;
947         }
948 #endif
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) {
954         case sizeof(double):
955             if ((val >> real_shift()) == 0) {
956                 *(double *)p->data = (double)val;
957                 return 1;
958             }
959             err_inexact;
960             return 0;
961         }
962         err_unsupported_real;
963         return 0;
964 #endif
965     }
966     err_bad_type;
967     return 0;
968 }
969
970 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
971 {
972     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
973                                 sizeof(uint64_t));
974 }
975
976 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
977 {
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);
984     }
985 #endif
986     return general_get_uint(p, val, sizeof(*val));
987 }
988
989 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
990 {
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);
997     }
998 #endif
999     return general_set_uint(p, &val, sizeof(val));
1000 }
1001
1002 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1003 {
1004     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1005                                 sizeof(size_t));
1006 }
1007
1008 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1009 {
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);
1016     }
1017 #endif
1018     return general_get_int(p, val, sizeof(*val));
1019 }
1020
1021 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1022 {
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);
1029     }
1030 #endif
1031     return general_set_int(p, &val, sizeof(val));
1032 }
1033
1034 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1035 {
1036     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1037 }
1038
1039 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1040 {
1041     BIGNUM *b = NULL;
1042
1043     if (val == NULL || p == NULL) {
1044         err_null_argument;
1045         return 0;
1046     }
1047
1048     switch (p->data_type) {
1049     case OSSL_PARAM_UNSIGNED_INTEGER:
1050         b = BN_native2bn(p->data, (int)p->data_size, *val);
1051         break;
1052     case OSSL_PARAM_INTEGER:
1053         b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1054         break;
1055     default:
1056         err_bad_type;
1057         break;
1058     }
1059
1060     if (b == NULL) {
1061         ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1062         return 0;
1063     }
1064
1065     *val = b;
1066     return 1;
1067 }
1068
1069 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1070 {
1071     size_t bytes;
1072
1073     if (p == NULL) {
1074         err_null_argument;
1075         return 0;
1076     }
1077     p->return_size = 0;
1078     if (val == NULL) {
1079         err_null_argument;
1080         return 0;
1081     }
1082     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1083         err_bad_type;
1084         return 0;
1085     }
1086
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)
1090         bytes++;
1091     /* We make sure that at least one byte is used, so zero is properly set */
1092     if (bytes == 0)
1093         bytes++;
1094
1095     p->return_size = bytes;
1096     if (p->data == NULL)
1097         return 1;
1098     if (p->data_size >= bytes) {
1099         p->return_size = p->data_size;
1100
1101         switch (p->data_type) {
1102         case OSSL_PARAM_UNSIGNED_INTEGER:
1103             if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1104                 return 1;
1105             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1106             break;
1107         case OSSL_PARAM_INTEGER:
1108             if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1109                 return 1;
1110             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1111             break;
1112         default:
1113             err_bad_type;
1114             break;
1115         }
1116         return 0;
1117     }
1118     err_too_small;
1119     return 0;
1120 }
1121
1122 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1123                                    size_t bsize)
1124 {
1125     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1126                                 buf, bsize);
1127 }
1128
1129 #ifndef OPENSSL_SYS_UEFI
1130 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1131 {
1132     int64_t i64;
1133     uint64_t u64;
1134
1135     if (val == NULL || p == NULL) {
1136         err_null_argument;
1137         return 0;
1138     }
1139
1140     if (p->data_type == OSSL_PARAM_REAL) {
1141         switch (p->data_size) {
1142         case sizeof(double):
1143             *val = *(const double *)p->data;
1144             return 1;
1145         }
1146         err_unsupported_real;
1147         return 0;
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;
1152             return 1;
1153         case sizeof(uint64_t):
1154             u64 = *(const uint64_t *)p->data;
1155             if ((u64 >> real_shift()) == 0) {
1156                 *val = (double)u64;
1157                 return 1;
1158             }
1159             err_inexact;
1160             return 0;
1161         }
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;
1166             return 1;
1167         case sizeof(int64_t):
1168             i64 = *(const int64_t *)p->data;
1169             u64 = i64 < 0 ? -i64 : i64;
1170             if ((u64 >> real_shift()) == 0) {
1171                 *val = 0.0 + i64;
1172                 return 1;
1173             }
1174             err_inexact;
1175             return 0;
1176         }
1177     }
1178     err_bad_type;
1179     return 0;
1180 }
1181
1182 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1183 {
1184     if (p == NULL) {
1185         err_null_argument;
1186         return 0;
1187     }
1188     p->return_size = 0;
1189
1190     if (p->data_type == OSSL_PARAM_REAL) {
1191         p->return_size = sizeof(double);
1192         if (p->data == NULL)
1193             return 1;
1194         switch (p->data_size) {
1195         case sizeof(double):
1196             *(double *)p->data = val;
1197             return 1;
1198         }
1199         err_unsupported_real;
1200         return 0;
1201     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1202         p->return_size = sizeof(double);
1203         if (p->data == NULL)
1204             return 1;
1205         if (val != (uint64_t)val) {
1206             err_inexact;
1207             return 0;
1208         }
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;
1214                 return 1;
1215             }
1216             err_out_of_range;
1217             return 0;
1218         case sizeof(uint64_t):
1219             if (val >= 0
1220                     /*
1221                      * By subtracting 65535 (2^16-1) we cancel the low order
1222                      * 15 bits of UINT64_MAX to avoid using imprecise floating
1223                      * point values.
1224                      */
1225                     && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1226                 p->return_size = sizeof(uint64_t);
1227                 *(uint64_t *)p->data = (uint64_t)val;
1228                 return 1;
1229             }
1230             err_out_of_range;
1231             return 0;
1232         }
1233     } else if (p->data_type == OSSL_PARAM_INTEGER) {
1234         p->return_size = sizeof(double);
1235         if (p->data == NULL)
1236             return 1;
1237         if (val != (int64_t)val) {
1238             err_inexact;
1239             return 0;
1240         }
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;
1246                 return 1;
1247             }
1248             err_out_of_range;
1249             return 0;
1250         case sizeof(int64_t):
1251             if (val >= INT64_MIN
1252                     /*
1253                      * By subtracting 65535 (2^16-1) we cancel the low order
1254                      * 15 bits of INT64_MAX to avoid using imprecise floating
1255                      * point values.
1256                      */
1257                     && val < (double)(INT64_MAX - 65535) + 65536.0) {
1258                 p->return_size = sizeof(int64_t);
1259                 *(int64_t *)p->data = (int64_t)val;
1260                 return 1;
1261             }
1262             err_out_of_range;
1263             return 0;
1264         }
1265     }
1266     err_bad_type;
1267     return 0;
1268 }
1269
1270 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1271 {
1272     return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1273 }
1274 #endif
1275
1276 static int get_string_internal(const OSSL_PARAM *p, void **val,
1277                                size_t *max_len, size_t *used_len,
1278                                unsigned int type)
1279 {
1280     size_t sz, alloc_sz;
1281
1282     if ((val == NULL && used_len == NULL) || p == NULL) {
1283         err_null_argument;
1284         return 0;
1285     }
1286     if (p->data_type != type) {
1287         err_bad_type;
1288         return 0;
1289     }
1290
1291     sz = p->data_size;
1292     /*
1293      * If the input size is 0, or the input string needs NUL byte
1294      * termination, allocate an extra byte.
1295      */
1296     alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1297
1298     if (used_len != NULL)
1299         *used_len = sz;
1300
1301     if (p->data == NULL) {
1302         err_null_argument;
1303         return 0;
1304     }
1305
1306     if (val == NULL)
1307         return 1;
1308
1309     if (*val == NULL) {
1310         char *const q = OPENSSL_malloc(alloc_sz);
1311
1312         if (q == NULL)
1313             return 0;
1314         *val = q;
1315         *max_len = alloc_sz;
1316     }
1317
1318     if (*max_len < sz) {
1319         err_too_small;
1320         return 0;
1321     }
1322     memcpy(*val, p->data, sz);
1323     return 1;
1324 }
1325
1326 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1327 {
1328     int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1329                                   OSSL_PARAM_UTF8_STRING);
1330
1331     /*
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|.
1341      */
1342     size_t data_length = p->data_size;
1343
1344     if (ret == 0)
1345         return 0;
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 */
1351     }
1352     (*val)[data_length] = '\0';
1353
1354     return ret;
1355 }
1356
1357 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1358                                 size_t *used_len)
1359 {
1360     return get_string_internal(p, val, &max_len, used_len,
1361                                OSSL_PARAM_OCTET_STRING);
1362 }
1363
1364 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1365                                unsigned int type)
1366 {
1367     p->return_size = len;
1368     if (p->data == NULL)
1369         return 1;
1370     if (p->data_type != type) {
1371         err_bad_type;
1372         return 0;
1373     }
1374     if (p->data_size < len) {
1375         err_too_small;
1376         return 0;
1377     }
1378
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';
1383     return 1;
1384 }
1385
1386 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1387 {
1388     if (p == NULL) {
1389         err_null_argument;
1390         return 0;
1391     }
1392
1393     p->return_size = 0;
1394     if (val == NULL) {
1395         err_null_argument;
1396         return 0;
1397     }
1398     return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1399 }
1400
1401 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1402                                 size_t len)
1403 {
1404     if (p == NULL) {
1405         err_null_argument;
1406         return 0;
1407     }
1408
1409     p->return_size = 0;
1410     if (val == NULL) {
1411         err_null_argument;
1412         return 0;
1413     }
1414     return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1415 }
1416
1417 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1418                                             size_t bsize)
1419 {
1420     if (buf != NULL && bsize == 0)
1421         bsize = strlen(buf);
1422     return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1423 }
1424
1425 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1426                                              size_t bsize)
1427 {
1428     return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1429 }
1430
1431 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1432                             size_t *used_len, unsigned int type)
1433 {
1434     if (val == NULL || p == NULL) {
1435         err_null_argument;
1436         return 0;
1437     }
1438     if (p->data_type != type) {
1439         err_bad_type;
1440         return 0;
1441     }
1442     if (used_len != NULL)
1443         *used_len = p->data_size;
1444     *val = *(const void **)p->data;
1445     return 1;
1446 }
1447
1448 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1449 {
1450     return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1451 }
1452
1453 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1454                              size_t *used_len)
1455 {
1456     return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1457 }
1458
1459 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1460                             unsigned int type, size_t len)
1461 {
1462     p->return_size = len;
1463     if (p->data_type != type) {
1464         err_bad_type;
1465         return 0;
1466     }
1467     if (p->data != NULL)
1468         *(const void **)p->data = val;
1469     return 1;
1470 }
1471
1472 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1473 {
1474     if (p == NULL) {
1475         err_null_argument;
1476         return 0;
1477     }
1478     p->return_size = 0;
1479     return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1480                             val == NULL ? 0 : strlen(val));
1481 }
1482
1483 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1484                              size_t used_len)
1485 {
1486     if (p == NULL) {
1487         err_null_argument;
1488         return 0;
1489     }
1490     p->return_size = 0;
1491     return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1492 }
1493
1494 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1495                                          size_t bsize)
1496 {
1497     return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1498 }
1499
1500 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1501                                           size_t bsize)
1502 {
1503     return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1504 }
1505
1506 /*
1507  * Extract the parameter into an allocated buffer.
1508  * Any existing allocation in *out is cleared and freed.
1509  *
1510  * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1511  *
1512  * *out and *out_len are guaranteed to be untouched if this function
1513  * doesn't return success.
1514  */
1515 int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1516                                  unsigned char **out, size_t *out_len)
1517 {
1518     const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1519     void *buf = NULL;
1520     size_t len = 0;
1521
1522     if (p == NULL)
1523         return -1;
1524
1525     if (p->data != NULL
1526             && p->data_size > 0
1527             && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1528         return 0;
1529
1530     OPENSSL_clear_free(*out, *out_len);
1531     *out = buf;
1532     *out_len = len;
1533     return 1;
1534 }
1535
1536 static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1537                              unsigned char *out, size_t *outlen)
1538 {
1539     int ret = 0;
1540     WPACKET pkt;
1541
1542     if (out == NULL) {
1543         if (!WPACKET_init_null(&pkt, 0))
1544             return 0;
1545     } else {
1546         if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1547             return 0;
1548     }
1549
1550     for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1551         if (p->data_type != OSSL_PARAM_OCTET_STRING)
1552             goto err;
1553         if (p->data != NULL
1554                 && p->data_size != 0
1555                 && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1556             goto err;
1557     }
1558     if (!WPACKET_get_total_written(&pkt, outlen)
1559             || !WPACKET_finish(&pkt))
1560         goto err;
1561     ret = 1;
1562 err:
1563     WPACKET_cleanup(&pkt);
1564     return ret;
1565 }
1566
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)
1570 {
1571     const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1572     unsigned char *res;
1573     size_t sz = 0;
1574
1575     if (p == NULL)
1576         return -1;
1577
1578     /* Calculate the total size */
1579     if (!setbuf_fromparams(p, name, NULL, &sz))
1580         return 0;
1581
1582     /* Check that it's not oversized */
1583     if (maxsize > 0 && sz > maxsize)
1584         return 0;
1585
1586     /* Special case zero length */
1587     if (sz == 0) {
1588         if ((res = OPENSSL_zalloc(1)) == NULL)
1589             return 0;
1590         goto fin;
1591     }
1592
1593     /* Allocate the buffer */
1594     res = OPENSSL_malloc(sz);
1595     if (res == NULL)
1596         return 0;
1597
1598     /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1599     if (!setbuf_fromparams(p, name, res, &sz)) {
1600         OPENSSL_clear_free(res, sz);
1601         return 0;
1602     }
1603
1604  fin:
1605     OPENSSL_clear_free(*out, *out_len);
1606     *out = res;
1607     *out_len = sz;
1608     return 1;
1609 }
1610
1611 OSSL_PARAM OSSL_PARAM_construct_end(void)
1612 {
1613     OSSL_PARAM end = OSSL_PARAM_END;
1614
1615     return end;
1616 }
1617
1618 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1619                                    size_t *used_len, unsigned int type)
1620 {
1621     if (val == NULL || p == NULL) {
1622         err_null_argument;
1623         return 0;
1624     }
1625     if (p->data_type != type) {
1626         err_bad_type;
1627         return 0;
1628     }
1629     if (used_len != NULL)
1630         *used_len = p->data_size;
1631     *val = p->data;
1632     return 1;
1633 }
1634
1635 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1636 {
1637     int rv;
1638
1639     ERR_set_mark();
1640     rv = OSSL_PARAM_get_utf8_ptr(p, val);
1641     ERR_pop_to_mark();
1642
1643     return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1644                                          OSSL_PARAM_UTF8_STRING);
1645 }
1646
1647 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1648                                     size_t *used_len)
1649 {
1650     int rv;
1651
1652     ERR_set_mark();
1653     rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1654     ERR_pop_to_mark();
1655
1656     return rv || get_string_ptr_internal(p, val, used_len,
1657                                          OSSL_PARAM_OCTET_STRING);
1658 }