0baf3fc174040ecd508b6f90406e1fe2e6837bd3
[openssl.git] / crypto / params.c
1 /*
2  * Copyright 2019-2024 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 == NULL) {
201         err_null_argument;
202         return 0;
203     }
204     if (p->data_type == OSSL_PARAM_INTEGER)
205         return signed_from_signed(val, val_size, p->data, p->data_size);
206     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
207         return signed_from_unsigned(val, val_size, p->data, p->data_size);
208     err_not_integer;
209     return 0;
210 }
211
212 /* General purpose set integer parameter call that handles odd sizes */
213 static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
214 {
215     int r = 0;
216
217     p->return_size = val_size; /* Expected size */
218     if (p->data == NULL)
219         return 1;
220     if (p->data_type == OSSL_PARAM_INTEGER)
221         r = signed_from_signed(p->data, p->data_size, val, val_size);
222     else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
223         r = unsigned_from_signed(p->data, p->data_size, val, val_size);
224     else
225         err_not_integer;
226     p->return_size = r ? p->data_size : val_size;
227     return r;
228 }
229
230 /* General purpose get unsigned integer parameter call that handles odd sizes */
231 static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
232 {
233
234     if (p->data == NULL) {
235         err_null_argument;
236         return 0;
237     }
238     if (p->data_type == OSSL_PARAM_INTEGER)
239         return unsigned_from_signed(val, val_size, p->data, p->data_size);
240     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
241         return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
242     err_not_integer;
243     return 0;
244 }
245
246 /* General purpose set unsigned integer parameter call that handles odd sizes */
247 static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
248 {
249     int r = 0;
250
251     p->return_size = val_size; /* Expected size */
252     if (p->data == NULL)
253         return 1;
254     if (p->data_type == OSSL_PARAM_INTEGER)
255         r = signed_from_unsigned(p->data, p->data_size, val, val_size);
256     else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
257         r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
258     else
259         err_not_integer;
260     p->return_size = r ? p->data_size : val_size;
261     return r;
262 }
263
264 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
265 {
266 #ifndef OPENSSL_SMALL_FOOTPRINT
267     switch (sizeof(int)) {
268     case sizeof(int32_t):
269         return OSSL_PARAM_get_int32(p, (int32_t *)val);
270     case sizeof(int64_t):
271         return OSSL_PARAM_get_int64(p, (int64_t *)val);
272     }
273 #endif
274     return general_get_int(p, val, sizeof(*val));
275 }
276
277 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
278 {
279 #ifndef OPENSSL_SMALL_FOOTPRINT
280     switch (sizeof(int)) {
281     case sizeof(int32_t):
282         return OSSL_PARAM_set_int32(p, (int32_t)val);
283     case sizeof(int64_t):
284         return OSSL_PARAM_set_int64(p, (int64_t)val);
285     }
286 #endif
287     return general_set_int(p, &val, sizeof(val));
288 }
289
290 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
291 {
292     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
293 }
294
295 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
296 {
297 #ifndef OPENSSL_SMALL_FOOTPRINT
298     switch (sizeof(unsigned int)) {
299     case sizeof(uint32_t):
300         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
301     case sizeof(uint64_t):
302         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
303     }
304 #endif
305     return general_get_uint(p, val, sizeof(*val));
306 }
307
308 int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
309 {
310 #ifndef OPENSSL_SMALL_FOOTPRINT
311     switch (sizeof(unsigned int)) {
312     case sizeof(uint32_t):
313         return OSSL_PARAM_set_uint32(p, (uint32_t)val);
314     case sizeof(uint64_t):
315         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
316     }
317 #endif
318     return general_set_uint(p, &val, sizeof(val));
319 }
320
321 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
322 {
323     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
324                                 sizeof(unsigned int));
325 }
326
327 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
328 {
329 #ifndef OPENSSL_SMALL_FOOTPRINT
330     switch (sizeof(long int)) {
331     case sizeof(int32_t):
332         return OSSL_PARAM_get_int32(p, (int32_t *)val);
333     case sizeof(int64_t):
334         return OSSL_PARAM_get_int64(p, (int64_t *)val);
335     }
336 #endif
337     return general_get_int(p, val, sizeof(*val));
338 }
339
340 int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
341 {
342 #ifndef OPENSSL_SMALL_FOOTPRINT
343     switch (sizeof(long int)) {
344     case sizeof(int32_t):
345         return OSSL_PARAM_set_int32(p, (int32_t)val);
346     case sizeof(int64_t):
347         return OSSL_PARAM_set_int64(p, (int64_t)val);
348     }
349 #endif
350     return general_set_int(p, &val, sizeof(val));
351 }
352
353 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
354 {
355     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
356 }
357
358 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
359 {
360 #ifndef OPENSSL_SMALL_FOOTPRINT
361     switch (sizeof(unsigned long int)) {
362     case sizeof(uint32_t):
363         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
364     case sizeof(uint64_t):
365         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
366     }
367 #endif
368     return general_get_uint(p, val, sizeof(*val));
369 }
370
371 int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
372 {
373 #ifndef OPENSSL_SMALL_FOOTPRINT
374     switch (sizeof(unsigned long int)) {
375     case sizeof(uint32_t):
376         return OSSL_PARAM_set_uint32(p, (uint32_t)val);
377     case sizeof(uint64_t):
378         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
379     }
380 #endif
381     return general_set_uint(p, &val, sizeof(val));
382 }
383
384 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
385 {
386     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
387                                 sizeof(unsigned long int));
388 }
389
390 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
391 {
392     if (val == NULL || p == NULL) {
393         err_null_argument;
394         return 0;
395     }
396
397     if (p->data == NULL) {
398         err_null_argument;
399         return 0;
400     }
401
402     if (p->data_type == OSSL_PARAM_INTEGER) {
403 #ifndef OPENSSL_SMALL_FOOTPRINT
404         int64_t i64;
405
406         switch (p->data_size) {
407         case sizeof(int32_t):
408             *val = *(const int32_t *)p->data;
409             return 1;
410         case sizeof(int64_t):
411             i64 = *(const int64_t *)p->data;
412             if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
413                 *val = (int32_t)i64;
414                 return 1;
415             }
416             err_out_of_range;
417             return 0;
418         }
419 #endif
420         return general_get_int(p, val, sizeof(*val));
421
422     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
423 #ifndef OPENSSL_SMALL_FOOTPRINT
424         uint32_t u32;
425         uint64_t u64;
426
427         switch (p->data_size) {
428         case sizeof(uint32_t):
429             u32 = *(const uint32_t *)p->data;
430             if (u32 <= INT32_MAX) {
431                 *val = (int32_t)u32;
432                 return 1;
433             }
434             err_out_of_range;
435             return 0;
436         case sizeof(uint64_t):
437             u64 = *(const uint64_t *)p->data;
438             if (u64 <= INT32_MAX) {
439                 *val = (int32_t)u64;
440                 return 1;
441             }
442             err_out_of_range;
443             return 0;
444         }
445 #endif
446         return general_get_int(p, val, sizeof(*val));
447
448     } else if (p->data_type == OSSL_PARAM_REAL) {
449 #ifndef OPENSSL_SYS_UEFI
450         double d;
451
452         switch (p->data_size) {
453         case sizeof(double):
454             d = *(const double *)p->data;
455             if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
456                 *val = (int32_t)d;
457                 return 1;
458             }
459             err_out_of_range;
460             return 0;
461         }
462         err_unsupported_real;
463         return 0;
464 #endif
465     }
466     err_bad_type;
467     return 0;
468 }
469
470 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
471 {
472     uint32_t u32;
473     unsigned int shift;
474
475     if (p == NULL) {
476         err_null_argument;
477         return 0;
478     }
479     p->return_size = 0;
480     if (p->data_type == OSSL_PARAM_INTEGER) {
481 #ifndef OPENSSL_SMALL_FOOTPRINT
482         p->return_size = sizeof(int32_t); /* Minimum expected size */
483         if (p->data == NULL)
484             return 1;
485         switch (p->data_size) {
486         case sizeof(int32_t):
487             *(int32_t *)p->data = val;
488             return 1;
489         case sizeof(int64_t):
490             p->return_size = sizeof(int64_t);
491             *(int64_t *)p->data = (int64_t)val;
492             return 1;
493         }
494 #endif
495         return general_set_int(p, &val, sizeof(val));
496     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
497 #ifndef OPENSSL_SMALL_FOOTPRINT
498         p->return_size = sizeof(uint32_t); /* Minimum expected size */
499         if (p->data == NULL)
500             return 1;
501         switch (p->data_size) {
502         case sizeof(uint32_t):
503             *(uint32_t *)p->data = (uint32_t)val;
504             return 1;
505         case sizeof(uint64_t):
506             p->return_size = sizeof(uint64_t);
507             *(uint64_t *)p->data = (uint64_t)val;
508             return 1;
509         }
510 #endif
511         return general_set_int(p, &val, sizeof(val));
512     } else if (p->data_type == OSSL_PARAM_REAL) {
513 #ifndef OPENSSL_SYS_UEFI
514         p->return_size = sizeof(double);
515         if (p->data == NULL)
516             return 1;
517         switch (p->data_size) {
518         case sizeof(double):
519             shift = real_shift();
520             if (shift < 8 * sizeof(val) - 1) {
521                 u32 = val < 0 ? -val : val;
522                 if ((u32 >> shift) != 0) {
523                     err_inexact;
524                     return 0;
525                 }
526             }
527             *(double *)p->data = (double)val;
528             return 1;
529         }
530         err_unsupported_real;
531         return 0;
532 #endif
533     }
534     err_bad_type;
535     return 0;
536 }
537
538 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
539 {
540     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
541                                 sizeof(int32_t));
542 }
543
544 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
545 {
546     if (val == NULL || p == NULL) {
547         err_null_argument;
548         return 0;
549     }
550
551     if (p->data == NULL) {
552         err_null_argument;
553         return 0;
554     }
555
556     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
557 #ifndef OPENSSL_SMALL_FOOTPRINT
558         uint64_t u64;
559
560         switch (p->data_size) {
561         case sizeof(uint32_t):
562             *val = *(const uint32_t *)p->data;
563             return 1;
564         case sizeof(uint64_t):
565             u64 = *(const uint64_t *)p->data;
566             if (u64 <= UINT32_MAX) {
567                 *val = (uint32_t)u64;
568                 return 1;
569             }
570             err_out_of_range;
571             return 0;
572         }
573 #endif
574         return general_get_uint(p, val, sizeof(*val));
575     } else if (p->data_type == OSSL_PARAM_INTEGER) {
576 #ifndef OPENSSL_SMALL_FOOTPRINT
577         int32_t i32;
578         int64_t i64;
579
580         switch (p->data_size) {
581         case sizeof(int32_t):
582             i32 = *(const int32_t *)p->data;
583             if (i32 >= 0) {
584                 *val = i32;
585                 return 1;
586             }
587             err_unsigned_negative;
588             return 0;
589         case sizeof(int64_t):
590             i64 = *(const int64_t *)p->data;
591             if (i64 >= 0 && i64 <= UINT32_MAX) {
592                 *val = (uint32_t)i64;
593                 return 1;
594             }
595             if (i64 < 0)
596                 err_unsigned_negative;
597             else
598                 err_out_of_range;
599             return 0;
600         }
601 #endif
602         return general_get_uint(p, val, sizeof(*val));
603     } else if (p->data_type == OSSL_PARAM_REAL) {
604 #ifndef OPENSSL_SYS_UEFI
605         double d;
606
607         switch (p->data_size) {
608         case sizeof(double):
609             d = *(const double *)p->data;
610             if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
611                 *val = (uint32_t)d;
612                 return 1;
613             }
614             err_inexact;
615             return 0;
616         }
617         err_unsupported_real;
618         return 0;
619 #endif
620     }
621     err_bad_type;
622     return 0;
623 }
624
625 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
626 {
627     unsigned int shift;
628
629     if (p == NULL) {
630         err_null_argument;
631         return 0;
632     }
633     p->return_size = 0;
634
635     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
636 #ifndef OPENSSL_SMALL_FOOTPRINT
637         p->return_size = sizeof(uint32_t); /* Minimum expected size */
638         if (p->data == NULL)
639             return 1;
640         switch (p->data_size) {
641         case sizeof(uint32_t):
642             *(uint32_t *)p->data = val;
643             return 1;
644         case sizeof(uint64_t):
645             p->return_size = sizeof(uint64_t);
646             *(uint64_t *)p->data = val;
647             return 1;
648         }
649 #endif
650         return general_set_uint(p, &val, sizeof(val));
651     } else if (p->data_type == OSSL_PARAM_INTEGER) {
652 #ifndef OPENSSL_SMALL_FOOTPRINT
653         p->return_size = sizeof(int32_t); /* Minimum expected size */
654         if (p->data == NULL)
655             return 1;
656         switch (p->data_size) {
657         case sizeof(int32_t):
658             if (val <= INT32_MAX) {
659                 *(int32_t *)p->data = (int32_t)val;
660                 return 1;
661             }
662             err_out_of_range;
663             return 0;
664         case sizeof(int64_t):
665             p->return_size = sizeof(int64_t);
666             *(int64_t *)p->data = (int64_t)val;
667             return 1;
668         }
669 #endif
670         return general_set_uint(p, &val, sizeof(val));
671     } else if (p->data_type == OSSL_PARAM_REAL) {
672 #ifndef OPENSSL_SYS_UEFI
673         p->return_size = sizeof(double);
674         if (p->data == NULL)
675             return 1;
676         switch (p->data_size) {
677         case sizeof(double):
678             shift = real_shift();
679             if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
680                 err_inexact;
681                 return 0;
682             }
683             *(double *)p->data = (double)val;
684             return 1;
685         }
686         err_unsupported_real;
687         return 0;
688 #endif
689     }
690     err_bad_type;
691     return 0;
692 }
693
694 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
695 {
696     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
697                                 sizeof(uint32_t));
698 }
699
700 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
701 {
702     if (val == NULL || p == NULL) {
703         err_null_argument;
704         return 0;
705     }
706
707     if (p->data == NULL) {
708         err_null_argument;
709         return 0;
710     }
711
712     if (p->data_type == OSSL_PARAM_INTEGER) {
713 #ifndef OPENSSL_SMALL_FOOTPRINT
714         switch (p->data_size) {
715         case sizeof(int32_t):
716             *val = *(const int32_t *)p->data;
717             return 1;
718         case sizeof(int64_t):
719             *val = *(const int64_t *)p->data;
720             return 1;
721         }
722 #endif
723         return general_get_int(p, val, sizeof(*val));
724     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
725 #ifndef OPENSSL_SMALL_FOOTPRINT
726         uint64_t u64;
727
728         switch (p->data_size) {
729         case sizeof(uint32_t):
730             *val = *(const uint32_t *)p->data;
731             return 1;
732         case sizeof(uint64_t):
733             u64 = *(const uint64_t *)p->data;
734             if (u64 <= INT64_MAX) {
735                 *val = (int64_t)u64;
736                 return 1;
737             }
738             err_out_of_range;
739             return 0;
740         }
741 #endif
742         return general_get_int(p, val, sizeof(*val));
743     } else if (p->data_type == OSSL_PARAM_REAL) {
744 #ifndef OPENSSL_SYS_UEFI
745         double d;
746
747         switch (p->data_size) {
748         case sizeof(double):
749             d = *(const double *)p->data;
750             if (d >= INT64_MIN
751                     /*
752                      * By subtracting 65535 (2^16-1) we cancel the low order
753                      * 15 bits of INT64_MAX to avoid using imprecise floating
754                      * point values.
755                      */
756                     && d < (double)(INT64_MAX - 65535) + 65536.0
757                     && d == (int64_t)d) {
758                 *val = (int64_t)d;
759                 return 1;
760             }
761             err_inexact;
762             return 0;
763         }
764         err_unsupported_real;
765         return 0;
766 #endif
767     }
768     err_bad_type;
769     return 0;
770 }
771
772 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
773 {
774     if (p == NULL) {
775         err_null_argument;
776         return 0;
777     }
778     p->return_size = 0;
779     if (p->data_type == OSSL_PARAM_INTEGER) {
780 #ifndef OPENSSL_SMALL_FOOTPRINT
781         p->return_size = sizeof(int64_t); /* Expected size */
782         if (p->data == NULL)
783             return 1;
784         switch (p->data_size) {
785         case sizeof(int32_t):
786             if (val >= INT32_MIN && val <= INT32_MAX) {
787                 p->return_size = sizeof(int32_t);
788                 *(int32_t *)p->data = (int32_t)val;
789                 return 1;
790             }
791             err_out_of_range;
792             return 0;
793         case sizeof(int64_t):
794             *(int64_t *)p->data = val;
795             return 1;
796         }
797 #endif
798         return general_set_int(p, &val, sizeof(val));
799     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
800 #ifndef OPENSSL_SMALL_FOOTPRINT
801         p->return_size = sizeof(uint64_t); /* Expected size */
802         if (p->data == NULL)
803             return 1;
804         switch (p->data_size) {
805         case sizeof(uint32_t):
806             if (val <= UINT32_MAX) {
807                 p->return_size = sizeof(uint32_t);
808                 *(uint32_t *)p->data = (uint32_t)val;
809                 return 1;
810             }
811             err_out_of_range;
812             return 0;
813         case sizeof(uint64_t):
814             *(uint64_t *)p->data = (uint64_t)val;
815             return 1;
816         }
817 #endif
818         return general_set_int(p, &val, sizeof(val));
819     } else if (p->data_type == OSSL_PARAM_REAL) {
820 #ifndef OPENSSL_SYS_UEFI
821         uint64_t u64;
822
823         p->return_size = sizeof(double);
824         if (p->data == NULL)
825             return 1;
826         switch (p->data_size) {
827         case sizeof(double):
828             u64 = val < 0 ? -val : val;
829             if ((u64 >> real_shift()) == 0) {
830                 *(double *)p->data = (double)val;
831                 return 1;
832             }
833             err_inexact;
834             return 0;
835         }
836         err_unsupported_real;
837         return 0;
838 #endif
839     }
840     err_bad_type;
841     return 0;
842 }
843
844 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
845 {
846     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
847 }
848
849 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
850 {
851     if (val == NULL || p == NULL) {
852         err_null_argument;
853         return 0;
854     }
855
856     if (p->data == NULL) {
857         err_null_argument;
858         return 0;
859     }
860
861     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
862 #ifndef OPENSSL_SMALL_FOOTPRINT
863         switch (p->data_size) {
864         case sizeof(uint32_t):
865             *val = *(const uint32_t *)p->data;
866             return 1;
867         case sizeof(uint64_t):
868             *val = *(const uint64_t *)p->data;
869             return 1;
870         }
871 #endif
872         return general_get_uint(p, val, sizeof(*val));
873     } else if (p->data_type == OSSL_PARAM_INTEGER) {
874 #ifndef OPENSSL_SMALL_FOOTPRINT
875         int32_t i32;
876         int64_t i64;
877
878         switch (p->data_size) {
879         case sizeof(int32_t):
880             i32 = *(const int32_t *)p->data;
881             if (i32 >= 0) {
882                 *val = (uint64_t)i32;
883                 return 1;
884             }
885             err_unsigned_negative;
886             return 0;
887         case sizeof(int64_t):
888             i64 = *(const int64_t *)p->data;
889             if (i64 >= 0) {
890                 *val = (uint64_t)i64;
891                 return 1;
892             }
893             err_unsigned_negative;
894             return 0;
895         }
896 #endif
897         return general_get_uint(p, val, sizeof(*val));
898     } else if (p->data_type == OSSL_PARAM_REAL) {
899 #ifndef OPENSSL_SYS_UEFI
900         double d;
901
902         switch (p->data_size) {
903         case sizeof(double):
904             d = *(const double *)p->data;
905             if (d >= 0
906                     /*
907                      * By subtracting 65535 (2^16-1) we cancel the low order
908                      * 15 bits of UINT64_MAX to avoid using imprecise floating
909                      * point values.
910                      */
911                     && d < (double)(UINT64_MAX - 65535) + 65536.0
912                     && d == (uint64_t)d) {
913                 *val = (uint64_t)d;
914                 return 1;
915             }
916             err_inexact;
917             return 0;
918         }
919         err_unsupported_real;
920         return 0;
921 #endif
922     }
923     err_bad_type;
924     return 0;
925 }
926
927 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
928 {
929     if (p == NULL) {
930         err_null_argument;
931         return 0;
932     }
933     p->return_size = 0;
934
935     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
936 #ifndef OPENSSL_SMALL_FOOTPRINT
937         p->return_size = sizeof(uint64_t); /* Expected size */
938         if (p->data == NULL)
939             return 1;
940         switch (p->data_size) {
941         case sizeof(uint32_t):
942             if (val <= UINT32_MAX) {
943                 p->return_size = sizeof(uint32_t);
944                 *(uint32_t *)p->data = (uint32_t)val;
945                 return 1;
946             }
947             err_out_of_range;
948             return 0;
949         case sizeof(uint64_t):
950             *(uint64_t *)p->data = val;
951             return 1;
952         }
953 #endif
954         return general_set_uint(p, &val, sizeof(val));
955     } else if (p->data_type == OSSL_PARAM_INTEGER) {
956 #ifndef OPENSSL_SMALL_FOOTPRINT
957         p->return_size = sizeof(int64_t); /* Expected size */
958         if (p->data == NULL)
959             return 1;
960         switch (p->data_size) {
961         case sizeof(int32_t):
962             if (val <= INT32_MAX) {
963                 p->return_size = sizeof(int32_t);
964                 *(int32_t *)p->data = (int32_t)val;
965                 return 1;
966             }
967             err_out_of_range;
968             return 0;
969         case sizeof(int64_t):
970             if (val <= INT64_MAX) {
971                 *(int64_t *)p->data = (int64_t)val;
972                 return 1;
973             }
974             err_out_of_range;
975             return 0;
976         }
977 #endif
978         return general_set_uint(p, &val, sizeof(val));
979     } else if (p->data_type == OSSL_PARAM_REAL) {
980 #ifndef OPENSSL_SYS_UEFI
981         p->return_size = sizeof(double);
982         switch (p->data_size) {
983         case sizeof(double):
984             if ((val >> real_shift()) == 0) {
985                 *(double *)p->data = (double)val;
986                 return 1;
987             }
988             err_inexact;
989             return 0;
990         }
991         err_unsupported_real;
992         return 0;
993 #endif
994     }
995     err_bad_type;
996     return 0;
997 }
998
999 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
1000 {
1001     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1002                                 sizeof(uint64_t));
1003 }
1004
1005 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
1006 {
1007 #ifndef OPENSSL_SMALL_FOOTPRINT
1008     switch (sizeof(size_t)) {
1009     case sizeof(uint32_t):
1010         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
1011     case sizeof(uint64_t):
1012         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
1013     }
1014 #endif
1015     return general_get_uint(p, val, sizeof(*val));
1016 }
1017
1018 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
1019 {
1020 #ifndef OPENSSL_SMALL_FOOTPRINT
1021     switch (sizeof(size_t)) {
1022     case sizeof(uint32_t):
1023         return OSSL_PARAM_set_uint32(p, (uint32_t)val);
1024     case sizeof(uint64_t):
1025         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
1026     }
1027 #endif
1028     return general_set_uint(p, &val, sizeof(val));
1029 }
1030
1031 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1032 {
1033     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1034                                 sizeof(size_t));
1035 }
1036
1037 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1038 {
1039 #ifndef OPENSSL_SMALL_FOOTPRINT
1040     switch (sizeof(time_t)) {
1041     case sizeof(int32_t):
1042         return OSSL_PARAM_get_int32(p, (int32_t *)val);
1043     case sizeof(int64_t):
1044         return OSSL_PARAM_get_int64(p, (int64_t *)val);
1045     }
1046 #endif
1047     return general_get_int(p, val, sizeof(*val));
1048 }
1049
1050 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1051 {
1052 #ifndef OPENSSL_SMALL_FOOTPRINT
1053     switch (sizeof(time_t)) {
1054     case sizeof(int32_t):
1055         return OSSL_PARAM_set_int32(p, (int32_t)val);
1056     case sizeof(int64_t):
1057         return OSSL_PARAM_set_int64(p, (int64_t)val);
1058     }
1059 #endif
1060     return general_set_int(p, &val, sizeof(val));
1061 }
1062
1063 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1064 {
1065     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1066 }
1067
1068 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1069 {
1070     BIGNUM *b = NULL;
1071
1072     if (val == NULL || p == NULL || p->data == NULL) {
1073         err_null_argument;
1074         return 0;
1075     }
1076
1077     switch (p->data_type) {
1078     case OSSL_PARAM_UNSIGNED_INTEGER:
1079         b = BN_native2bn(p->data, (int)p->data_size, *val);
1080         break;
1081     case OSSL_PARAM_INTEGER:
1082         b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1083         break;
1084     default:
1085         err_bad_type;
1086         break;
1087     }
1088
1089     if (b == NULL) {
1090         ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1091         return 0;
1092     }
1093
1094     *val = b;
1095     return 1;
1096 }
1097
1098 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1099 {
1100     size_t bytes;
1101
1102     if (p == NULL) {
1103         err_null_argument;
1104         return 0;
1105     }
1106     p->return_size = 0;
1107     if (val == NULL) {
1108         err_null_argument;
1109         return 0;
1110     }
1111     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1112         err_bad_type;
1113         return 0;
1114     }
1115
1116     bytes = (size_t)BN_num_bytes(val);
1117     /* We add 1 byte for signed numbers, to make space for a sign extension */
1118     if (p->data_type == OSSL_PARAM_INTEGER)
1119         bytes++;
1120     /* We make sure that at least one byte is used, so zero is properly set */
1121     if (bytes == 0)
1122         bytes++;
1123
1124     p->return_size = bytes;
1125     if (p->data == NULL)
1126         return 1;
1127     if (p->data_size >= bytes) {
1128         p->return_size = p->data_size;
1129
1130         switch (p->data_type) {
1131         case OSSL_PARAM_UNSIGNED_INTEGER:
1132             if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1133                 return 1;
1134             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1135             break;
1136         case OSSL_PARAM_INTEGER:
1137             if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1138                 return 1;
1139             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1140             break;
1141         default:
1142             err_bad_type;
1143             break;
1144         }
1145         return 0;
1146     }
1147     err_too_small;
1148     return 0;
1149 }
1150
1151 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1152                                    size_t bsize)
1153 {
1154     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1155                                 buf, bsize);
1156 }
1157
1158 #ifndef OPENSSL_SYS_UEFI
1159 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1160 {
1161     int64_t i64;
1162     uint64_t u64;
1163
1164     if (val == NULL || p == NULL || p->data == NULL) {
1165         err_null_argument;
1166         return 0;
1167     }
1168
1169     if (p->data_type == OSSL_PARAM_REAL) {
1170         switch (p->data_size) {
1171         case sizeof(double):
1172             *val = *(const double *)p->data;
1173             return 1;
1174         }
1175         err_unsupported_real;
1176         return 0;
1177     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1178         switch (p->data_size) {
1179         case sizeof(uint32_t):
1180             *val = *(const uint32_t *)p->data;
1181             return 1;
1182         case sizeof(uint64_t):
1183             u64 = *(const uint64_t *)p->data;
1184             if ((u64 >> real_shift()) == 0) {
1185                 *val = (double)u64;
1186                 return 1;
1187             }
1188             err_inexact;
1189             return 0;
1190         }
1191     } else if (p->data_type == OSSL_PARAM_INTEGER) {
1192         switch (p->data_size) {
1193         case sizeof(int32_t):
1194             *val = *(const int32_t *)p->data;
1195             return 1;
1196         case sizeof(int64_t):
1197             i64 = *(const int64_t *)p->data;
1198             u64 = i64 < 0 ? -i64 : i64;
1199             if ((u64 >> real_shift()) == 0) {
1200                 *val = 0.0 + i64;
1201                 return 1;
1202             }
1203             err_inexact;
1204             return 0;
1205         }
1206     }
1207     err_bad_type;
1208     return 0;
1209 }
1210
1211 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1212 {
1213     if (p == NULL) {
1214         err_null_argument;
1215         return 0;
1216     }
1217     p->return_size = 0;
1218
1219     if (p->data_type == OSSL_PARAM_REAL) {
1220         p->return_size = sizeof(double);
1221         if (p->data == NULL)
1222             return 1;
1223         switch (p->data_size) {
1224         case sizeof(double):
1225             *(double *)p->data = val;
1226             return 1;
1227         }
1228         err_unsupported_real;
1229         return 0;
1230     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1231         p->return_size = sizeof(double);
1232         if (p->data == NULL)
1233             return 1;
1234         if (val != (uint64_t)val) {
1235             err_inexact;
1236             return 0;
1237         }
1238         switch (p->data_size) {
1239         case sizeof(uint32_t):
1240             if (val >= 0 && val <= UINT32_MAX) {
1241                 p->return_size = sizeof(uint32_t);
1242                 *(uint32_t *)p->data = (uint32_t)val;
1243                 return 1;
1244             }
1245             err_out_of_range;
1246             return 0;
1247         case sizeof(uint64_t):
1248             if (val >= 0
1249                     /*
1250                      * By subtracting 65535 (2^16-1) we cancel the low order
1251                      * 15 bits of UINT64_MAX to avoid using imprecise floating
1252                      * point values.
1253                      */
1254                     && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1255                 p->return_size = sizeof(uint64_t);
1256                 *(uint64_t *)p->data = (uint64_t)val;
1257                 return 1;
1258             }
1259             err_out_of_range;
1260             return 0;
1261         }
1262     } else if (p->data_type == OSSL_PARAM_INTEGER) {
1263         p->return_size = sizeof(double);
1264         if (p->data == NULL)
1265             return 1;
1266         if (val != (int64_t)val) {
1267             err_inexact;
1268             return 0;
1269         }
1270         switch (p->data_size) {
1271         case sizeof(int32_t):
1272             if (val >= INT32_MIN && val <= INT32_MAX) {
1273                 p->return_size = sizeof(int32_t);
1274                 *(int32_t *)p->data = (int32_t)val;
1275                 return 1;
1276             }
1277             err_out_of_range;
1278             return 0;
1279         case sizeof(int64_t):
1280             if (val >= INT64_MIN
1281                     /*
1282                      * By subtracting 65535 (2^16-1) we cancel the low order
1283                      * 15 bits of INT64_MAX to avoid using imprecise floating
1284                      * point values.
1285                      */
1286                     && val < (double)(INT64_MAX - 65535) + 65536.0) {
1287                 p->return_size = sizeof(int64_t);
1288                 *(int64_t *)p->data = (int64_t)val;
1289                 return 1;
1290             }
1291             err_out_of_range;
1292             return 0;
1293         }
1294     }
1295     err_bad_type;
1296     return 0;
1297 }
1298
1299 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1300 {
1301     return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1302 }
1303 #endif
1304
1305 static int get_string_internal(const OSSL_PARAM *p, void **val,
1306                                size_t *max_len, size_t *used_len,
1307                                unsigned int type)
1308 {
1309     size_t sz, alloc_sz;
1310
1311     if ((val == NULL && used_len == NULL) || p == NULL) {
1312         err_null_argument;
1313         return 0;
1314     }
1315     if (p->data_type != type) {
1316         err_bad_type;
1317         return 0;
1318     }
1319
1320     sz = p->data_size;
1321     /*
1322      * If the input size is 0, or the input string needs NUL byte
1323      * termination, allocate an extra byte.
1324      */
1325     alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1326
1327     if (used_len != NULL)
1328         *used_len = sz;
1329
1330     if (p->data == NULL) {
1331         err_null_argument;
1332         return 0;
1333     }
1334
1335     if (val == NULL)
1336         return 1;
1337
1338     if (*val == NULL) {
1339         char *const q = OPENSSL_malloc(alloc_sz);
1340
1341         if (q == NULL)
1342             return 0;
1343         *val = q;
1344         *max_len = alloc_sz;
1345     }
1346
1347     if (*max_len < sz) {
1348         err_too_small;
1349         return 0;
1350     }
1351     memcpy(*val, p->data, sz);
1352     return 1;
1353 }
1354
1355 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1356 {
1357     int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1358                                   OSSL_PARAM_UTF8_STRING);
1359
1360     /*
1361      * We try to ensure that the copied string is terminated with a
1362      * NUL byte.  That should be easy, just place a NUL byte at
1363      * |((char*)*val)[p->data_size]|.
1364      * Unfortunately, we have seen cases where |p->data_size| doesn't
1365      * correctly reflect the length of the string, and just happens
1366      * to be out of bounds according to |max_len|, so in that case, we
1367      * make the extra step of trying to find the true length of the
1368      * string that |p->data| points at, and use that as an index to
1369      * place the NUL byte in |*val|.
1370      */
1371     size_t data_length = p->data_size;
1372
1373     if (ret == 0)
1374         return 0;
1375     if (data_length >= max_len)
1376         data_length = OPENSSL_strnlen(p->data, data_length);
1377     if (data_length >= max_len) {
1378         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1379         return 0;            /* No space for a terminating NUL byte */
1380     }
1381     (*val)[data_length] = '\0';
1382
1383     return ret;
1384 }
1385
1386 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1387                                 size_t *used_len)
1388 {
1389     return get_string_internal(p, val, &max_len, used_len,
1390                                OSSL_PARAM_OCTET_STRING);
1391 }
1392
1393 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1394                                unsigned int type)
1395 {
1396     p->return_size = len;
1397     if (p->data == NULL)
1398         return 1;
1399     if (p->data_type != type) {
1400         err_bad_type;
1401         return 0;
1402     }
1403     if (p->data_size < len) {
1404         err_too_small;
1405         return 0;
1406     }
1407
1408     memcpy(p->data, val, len);
1409     /* If possible within the size of p->data, add a NUL terminator byte */
1410     if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1411         ((char *)p->data)[len] = '\0';
1412     return 1;
1413 }
1414
1415 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1416 {
1417     if (p == NULL) {
1418         err_null_argument;
1419         return 0;
1420     }
1421
1422     p->return_size = 0;
1423     if (val == NULL) {
1424         err_null_argument;
1425         return 0;
1426     }
1427     return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1428 }
1429
1430 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1431                                 size_t len)
1432 {
1433     if (p == NULL) {
1434         err_null_argument;
1435         return 0;
1436     }
1437
1438     p->return_size = 0;
1439     if (val == NULL) {
1440         err_null_argument;
1441         return 0;
1442     }
1443     return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1444 }
1445
1446 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1447                                             size_t bsize)
1448 {
1449     if (buf != NULL && bsize == 0)
1450         bsize = strlen(buf);
1451     return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1452 }
1453
1454 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1455                                              size_t bsize)
1456 {
1457     return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1458 }
1459
1460 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1461                             size_t *used_len, unsigned int type)
1462 {
1463     if (val == NULL || p == NULL) {
1464         err_null_argument;
1465         return 0;
1466     }
1467     if (p->data_type != type) {
1468         err_bad_type;
1469         return 0;
1470     }
1471     if (used_len != NULL)
1472         *used_len = p->data_size;
1473     *val = *(const void **)p->data;
1474     return 1;
1475 }
1476
1477 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1478 {
1479     return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1480 }
1481
1482 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1483                              size_t *used_len)
1484 {
1485     return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1486 }
1487
1488 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1489                             unsigned int type, size_t len)
1490 {
1491     p->return_size = len;
1492     if (p->data_type != type) {
1493         err_bad_type;
1494         return 0;
1495     }
1496     if (p->data != NULL)
1497         *(const void **)p->data = val;
1498     return 1;
1499 }
1500
1501 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1502 {
1503     if (p == NULL) {
1504         err_null_argument;
1505         return 0;
1506     }
1507     p->return_size = 0;
1508     return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1509                             val == NULL ? 0 : strlen(val));
1510 }
1511
1512 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1513                              size_t used_len)
1514 {
1515     if (p == NULL) {
1516         err_null_argument;
1517         return 0;
1518     }
1519     p->return_size = 0;
1520     return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1521 }
1522
1523 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1524                                          size_t bsize)
1525 {
1526     return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1527 }
1528
1529 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1530                                           size_t bsize)
1531 {
1532     return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1533 }
1534
1535 /*
1536  * Extract the parameter into an allocated buffer.
1537  * Any existing allocation in *out is cleared and freed.
1538  *
1539  * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1540  *
1541  * *out and *out_len are guaranteed to be untouched if this function
1542  * doesn't return success.
1543  */
1544 int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1545                                  unsigned char **out, size_t *out_len)
1546 {
1547     const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1548     void *buf = NULL;
1549     size_t len = 0;
1550
1551     if (p == NULL)
1552         return -1;
1553
1554     if (p->data != NULL
1555             && p->data_size > 0
1556             && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1557         return 0;
1558
1559     OPENSSL_clear_free(*out, *out_len);
1560     *out = buf;
1561     *out_len = len;
1562     return 1;
1563 }
1564
1565 static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1566                              unsigned char *out, size_t *outlen)
1567 {
1568     int ret = 0;
1569     WPACKET pkt;
1570
1571     if (out == NULL) {
1572         if (!WPACKET_init_null(&pkt, 0))
1573             return 0;
1574     } else {
1575         if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1576             return 0;
1577     }
1578
1579     for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1580         if (p->data_type != OSSL_PARAM_OCTET_STRING)
1581             goto err;
1582         if (p->data != NULL
1583                 && p->data_size != 0
1584                 && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1585             goto err;
1586     }
1587     if (!WPACKET_get_total_written(&pkt, outlen)
1588             || !WPACKET_finish(&pkt))
1589         goto err;
1590     ret = 1;
1591 err:
1592     WPACKET_cleanup(&pkt);
1593     return ret;
1594 }
1595
1596 int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
1597                                         unsigned char **out,
1598                                         size_t *out_len, size_t maxsize)
1599 {
1600     const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1601     unsigned char *res;
1602     size_t sz = 0;
1603
1604     if (p == NULL)
1605         return -1;
1606
1607     /* Calculate the total size */
1608     if (!setbuf_fromparams(p, name, NULL, &sz))
1609         return 0;
1610
1611     /* Check that it's not oversized */
1612     if (maxsize > 0 && sz > maxsize)
1613         return 0;
1614
1615     /* Special case zero length */
1616     if (sz == 0) {
1617         if ((res = OPENSSL_zalloc(1)) == NULL)
1618             return 0;
1619         goto fin;
1620     }
1621
1622     /* Allocate the buffer */
1623     res = OPENSSL_malloc(sz);
1624     if (res == NULL)
1625         return 0;
1626
1627     /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1628     if (!setbuf_fromparams(p, name, res, &sz)) {
1629         OPENSSL_clear_free(res, sz);
1630         return 0;
1631     }
1632
1633  fin:
1634     OPENSSL_clear_free(*out, *out_len);
1635     *out = res;
1636     *out_len = sz;
1637     return 1;
1638 }
1639
1640 OSSL_PARAM OSSL_PARAM_construct_end(void)
1641 {
1642     OSSL_PARAM end = OSSL_PARAM_END;
1643
1644     return end;
1645 }
1646
1647 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1648                                    size_t *used_len, unsigned int type)
1649 {
1650     if (val == NULL || p == NULL) {
1651         err_null_argument;
1652         return 0;
1653     }
1654     if (p->data_type != type) {
1655         err_bad_type;
1656         return 0;
1657     }
1658     if (used_len != NULL)
1659         *used_len = p->data_size;
1660     *val = p->data;
1661     return 1;
1662 }
1663
1664 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1665 {
1666     int rv;
1667
1668     ERR_set_mark();
1669     rv = OSSL_PARAM_get_utf8_ptr(p, val);
1670     ERR_pop_to_mark();
1671
1672     return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1673                                          OSSL_PARAM_UTF8_STRING);
1674 }
1675
1676 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1677                                     size_t *used_len)
1678 {
1679     int rv;
1680
1681     ERR_set_mark();
1682     rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1683     ERR_pop_to_mark();
1684
1685     return rv || get_string_ptr_internal(p, val, used_len,
1686                                          OSSL_PARAM_OCTET_STRING);
1687 }