bin2bn(): When len==0, just return a zero BIGNUM
[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
18 /* Shortcuts for raising errors that are widely used */
19 #define err_unsigned_negative \
20     ERR_raise(ERR_LIB_CRYPTO, \
21               CRYPTO_R_PARAM_UNSIGNED_INTEGER_NEGATIVE_VALUE_UNSUPPORTED)
22 #define err_out_of_range      \
23     ERR_raise(ERR_LIB_CRYPTO, \
24               CRYPTO_R_PARAM_VALUE_TOO_LARGE_FOR_DESTINATION)
25 #define err_inexact           \
26     ERR_raise(ERR_LIB_CRYPTO, \
27               CRYPTO_R_PARAM_CANNOT_BE_REPRESENTED_EXACTLY)
28 #define err_not_integer       \
29     ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_NOT_INTEGER_TYPE)
30 #define err_too_small         \
31     ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER)
32 #define err_bad_type          \
33     ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_OF_INCOMPATIBLE_TYPE)
34 #define err_null_argument     \
35     ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER)
36 #define err_unsupported_real  \
37     ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_UNSUPPORTED_FLOATING_POINT_FORMAT)
38
39 /*
40  * Return the number of bits in the mantissa of a double.  This is used to
41  * shift a larger integral value to determine if it will exactly fit into a
42  * double.
43  */
44 static unsigned int real_shift(void)
45 {
46     return sizeof(double) == 4 ? 24 : 53;
47 }
48
49 OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
50 {
51     if (p != NULL && key != NULL)
52         for (; p->key != NULL; p++)
53             if (strcmp(key, p->key) == 0)
54                 return p;
55     return NULL;
56 }
57
58 const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
59 {
60     return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
61 }
62
63 static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
64                                        void *data, size_t data_size)
65 {
66     OSSL_PARAM res;
67
68     res.key = key;
69     res.data_type = data_type;
70     res.data = data;
71     res.data_size = data_size;
72     res.return_size = OSSL_PARAM_UNMODIFIED;
73     return res;
74 }
75
76 int OSSL_PARAM_modified(const OSSL_PARAM *p)
77 {
78     return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
79 }
80
81 void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
82 {
83     if (p != NULL)
84         while (p->key != NULL)
85             p++->return_size = OSSL_PARAM_UNMODIFIED;
86 }
87
88 /* Return non-zero if the signed number is negative */
89 static int is_negative(const void *number, size_t s)
90 {
91     const unsigned char *n = number;
92     DECLARE_IS_ENDIAN;
93
94     return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
95 }
96
97 /* Check that all the bytes specified match the expected sign byte */
98 static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
99 {
100     size_t i;
101
102     for (i = 0; i < n; i++)
103         if (p[i] != s)
104             return 0;
105     return 1;
106 }
107
108 /*
109  * Copy an integer to another integer.
110  * Handle different length integers and signed and unsigned integers.
111  * Both integers are in native byte ordering.
112  */
113 static int copy_integer(unsigned char *dest, size_t dest_len,
114                         const unsigned char *src, size_t src_len,
115                         unsigned char pad, int signed_int)
116 {
117     size_t n;
118     DECLARE_IS_ENDIAN;
119
120     if (IS_BIG_ENDIAN) {
121         if (src_len < dest_len) {
122             n = dest_len - src_len;
123             memset(dest, pad, n);
124             memcpy(dest + n, src, src_len);
125         } else {
126             n = src_len - dest_len;
127             if (!check_sign_bytes(src, n, pad)
128                     /*
129                      * Shortening a signed value must retain the correct sign.
130                      * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
131                      */
132                     || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
133                 err_out_of_range;
134                 return 0;
135             }
136             memcpy(dest, src + n, dest_len);
137         }
138     } else /* IS_LITTLE_ENDIAN */ {
139         if (src_len < dest_len) {
140             n = dest_len - src_len;
141             memset(dest + src_len, pad, n);
142             memcpy(dest, src, src_len);
143         } else {
144             n = src_len - dest_len;
145             if (!check_sign_bytes(src + dest_len, n, pad)
146                     /*
147                      * Shortening a signed value must retain the correct sign.
148                      * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
149                      */
150                     || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
151                 err_out_of_range;
152                 return 0;
153             }
154             memcpy(dest, src, dest_len);
155         }
156     }
157     return 1;
158 }
159
160 /* Copy a signed number to a signed number of possibly different length */
161 static int signed_from_signed(void *dest, size_t dest_len,
162                               const void *src, size_t src_len)
163 {
164     return copy_integer(dest, dest_len, src, src_len,
165                         is_negative(src, src_len) ? 0xff : 0, 1);
166 }
167
168 /* Copy an unsigned number to a signed number of possibly different length */
169 static int signed_from_unsigned(void *dest, size_t dest_len,
170                                 const void *src, size_t src_len)
171 {
172     return copy_integer(dest, dest_len, src, src_len, 0, 1);
173 }
174
175 /* Copy a signed number to an unsigned number of possibly different length */
176 static int unsigned_from_signed(void *dest, size_t dest_len,
177                                 const void *src, size_t src_len)
178 {
179     if (is_negative(src, src_len)) {
180         err_unsigned_negative;
181         return 0;
182     }
183     return copy_integer(dest, dest_len, src, src_len, 0, 0);
184 }
185
186 /* Copy an unsigned number to an unsigned number of possibly different length */
187 static int unsigned_from_unsigned(void *dest, size_t dest_len,
188                                   const void *src, size_t src_len)
189 {
190     return copy_integer(dest, dest_len, src, src_len, 0, 0);
191 }
192
193 /* General purpose get integer parameter call that handles odd sizes */
194 static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
195 {
196     if (p->data_type == OSSL_PARAM_INTEGER)
197         return signed_from_signed(val, val_size, p->data, p->data_size);
198     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
199         return signed_from_unsigned(val, val_size, p->data, p->data_size);
200     err_not_integer;
201     return 0;
202 }
203
204 /* General purpose set integer parameter call that handles odd sizes */
205 static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
206 {
207     int r = 0;
208
209     p->return_size = val_size; /* Expected size */
210     if (p->data == NULL)
211         return 1;
212     if (p->data_type == OSSL_PARAM_INTEGER)
213         r = signed_from_signed(p->data, p->data_size, val, val_size);
214     else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
215         r = unsigned_from_signed(p->data, p->data_size, val, val_size);
216     else
217         err_not_integer;
218     p->return_size = r ? p->data_size : val_size;
219     return r;
220 }
221
222 /* General purpose get unsigned integer parameter call that handles odd sizes */
223 static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
224 {
225     if (p->data_type == OSSL_PARAM_INTEGER)
226         return unsigned_from_signed(val, val_size, p->data, p->data_size);
227     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
228         return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
229     err_not_integer;
230     return 0;
231 }
232
233 /* General purpose set unsigned integer parameter call that handles odd sizes */
234 static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
235 {
236     int r = 0;
237
238     p->return_size = val_size; /* Expected size */
239     if (p->data == NULL)
240         return 1;
241     if (p->data_type == OSSL_PARAM_INTEGER)
242         r = signed_from_unsigned(p->data, p->data_size, val, val_size);
243     else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
244         r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
245     else
246         err_not_integer;
247     p->return_size = r ? p->data_size : val_size;
248     return r;
249 }
250
251 int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
252 {
253 #ifndef OPENSSL_SMALL_FOOTPRINT
254     switch (sizeof(int)) {
255     case sizeof(int32_t):
256         return OSSL_PARAM_get_int32(p, (int32_t *)val);
257     case sizeof(int64_t):
258         return OSSL_PARAM_get_int64(p, (int64_t *)val);
259     }
260 #endif
261     return general_get_int(p, val, sizeof(*val));
262 }
263
264 int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
265 {
266 #ifndef OPENSSL_SMALL_FOOTPRINT
267     switch (sizeof(int)) {
268     case sizeof(int32_t):
269         return OSSL_PARAM_set_int32(p, (int32_t)val);
270     case sizeof(int64_t):
271         return OSSL_PARAM_set_int64(p, (int64_t)val);
272     }
273 #endif
274     return general_set_int(p, &val, sizeof(val));
275 }
276
277 OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
278 {
279     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
280 }
281
282 int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
283 {
284 #ifndef OPENSSL_SMALL_FOOTPRINT
285     switch (sizeof(unsigned int)) {
286     case sizeof(uint32_t):
287         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
288     case sizeof(uint64_t):
289         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
290     }
291 #endif
292     return general_get_uint(p, val, sizeof(*val));
293 }
294
295 int OSSL_PARAM_set_uint(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_set_uint32(p, (uint32_t)val);
301     case sizeof(uint64_t):
302         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
303     }
304 #endif
305     return general_set_uint(p, &val, sizeof(val));
306 }
307
308 OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
309 {
310     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
311                                 sizeof(unsigned int));
312 }
313
314 int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
315 {
316 #ifndef OPENSSL_SMALL_FOOTPRINT
317     switch (sizeof(long int)) {
318     case sizeof(int32_t):
319         return OSSL_PARAM_get_int32(p, (int32_t *)val);
320     case sizeof(int64_t):
321         return OSSL_PARAM_get_int64(p, (int64_t *)val);
322     }
323 #endif
324     return general_get_int(p, val, sizeof(*val));
325 }
326
327 int OSSL_PARAM_set_long(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_set_int32(p, (int32_t)val);
333     case sizeof(int64_t):
334         return OSSL_PARAM_set_int64(p, (int64_t)val);
335     }
336 #endif
337     return general_set_int(p, &val, sizeof(val));
338 }
339
340 OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
341 {
342     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
343 }
344
345 int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
346 {
347 #ifndef OPENSSL_SMALL_FOOTPRINT
348     switch (sizeof(unsigned long int)) {
349     case sizeof(uint32_t):
350         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
351     case sizeof(uint64_t):
352         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
353     }
354 #endif
355     return general_get_uint(p, val, sizeof(*val));
356 }
357
358 int OSSL_PARAM_set_ulong(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_set_uint32(p, (uint32_t)val);
364     case sizeof(uint64_t):
365         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
366     }
367 #endif
368     return general_set_uint(p, &val, sizeof(val));
369 }
370
371 OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
372 {
373     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
374                                 sizeof(unsigned long int));
375 }
376
377 int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
378 {
379     double d;
380
381     if (val == NULL || p == NULL) {
382         err_null_argument;
383         return 0;
384     }
385
386     if (p->data_type == OSSL_PARAM_INTEGER) {
387 #ifndef OPENSSL_SMALL_FOOTPRINT
388         int64_t i64;
389
390         switch (p->data_size) {
391         case sizeof(int32_t):
392             *val = *(const int32_t *)p->data;
393             return 1;
394         case sizeof(int64_t):
395             i64 = *(const int64_t *)p->data;
396             if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
397                 *val = (int32_t)i64;
398                 return 1;
399             }
400             err_out_of_range;
401             return 0;
402         }
403 #endif
404         return general_get_int(p, val, sizeof(*val));
405
406     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
407 #ifndef OPENSSL_SMALL_FOOTPRINT
408         uint32_t u32;
409         uint64_t u64;
410
411         switch (p->data_size) {
412         case sizeof(uint32_t):
413             u32 = *(const uint32_t *)p->data;
414             if (u32 <= INT32_MAX) {
415                 *val = (int32_t)u32;
416                 return 1;
417             }
418             err_out_of_range;
419             return 0;
420         case sizeof(uint64_t):
421             u64 = *(const uint64_t *)p->data;
422             if (u64 <= INT32_MAX) {
423                 *val = (int32_t)u64;
424                 return 1;
425             }
426             err_out_of_range;
427             return 0;
428         }
429 #endif
430         return general_get_int(p, val, sizeof(*val));
431
432     } else if (p->data_type == OSSL_PARAM_REAL) {
433         switch (p->data_size) {
434         case sizeof(double):
435             d = *(const double *)p->data;
436             if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
437                 *val = (int32_t)d;
438                 return 1;
439             }
440             err_out_of_range;
441             return 0;
442         }
443         err_unsupported_real;
444         return 0;
445     }
446     err_bad_type;
447     return 0;
448 }
449
450 int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
451 {
452     uint32_t u32;
453     unsigned int shift;
454
455     if (p == NULL) {
456         err_null_argument;
457         return 0;
458     }
459     p->return_size = 0;
460     if (p->data_type == OSSL_PARAM_INTEGER) {
461 #ifndef OPENSSL_SMALL_FOOTPRINT
462         p->return_size = sizeof(int32_t); /* Minimum expected size */
463         if (p->data == NULL)
464             return 1;
465         switch (p->data_size) {
466         case sizeof(int32_t):
467             *(int32_t *)p->data = val;
468             return 1;
469         case sizeof(int64_t):
470             p->return_size = sizeof(int64_t);
471             *(int64_t *)p->data = (int64_t)val;
472             return 1;
473         }
474 #endif
475         return general_set_int(p, &val, sizeof(val));
476     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
477 #ifndef OPENSSL_SMALL_FOOTPRINT
478         p->return_size = sizeof(uint32_t); /* Minimum expected size */
479         if (p->data == NULL)
480             return 1;
481         switch (p->data_size) {
482         case sizeof(uint32_t):
483             *(uint32_t *)p->data = (uint32_t)val;
484             return 1;
485         case sizeof(uint64_t):
486             p->return_size = sizeof(uint64_t);
487             *(uint64_t *)p->data = (uint64_t)val;
488             return 1;
489         }
490 #endif
491         return general_set_int(p, &val, sizeof(val));
492     } else if (p->data_type == OSSL_PARAM_REAL) {
493         p->return_size = sizeof(double);
494         if (p->data == NULL)
495             return 1;
496         switch (p->data_size) {
497         case sizeof(double):
498             shift = real_shift();
499             if (shift < 8 * sizeof(val) - 1) {
500                 u32 = val < 0 ? -val : val;
501                 if ((u32 >> shift) != 0) {
502                     err_inexact;
503                     return 0;
504                 }
505             }
506             *(double *)p->data = (double)val;
507             return 1;
508         }
509         err_unsupported_real;
510         return 0;
511     }
512     err_bad_type;
513     return 0;
514 }
515
516 OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
517 {
518     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
519                                 sizeof(int32_t));
520 }
521
522 int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
523 {
524     double d;
525
526     if (val == NULL || p == NULL) {
527         err_null_argument;
528         return 0;
529     }
530
531     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
532 #ifndef OPENSSL_SMALL_FOOTPRINT
533         uint64_t u64;
534
535         switch (p->data_size) {
536         case sizeof(uint32_t):
537             *val = *(const uint32_t *)p->data;
538             return 1;
539         case sizeof(uint64_t):
540             u64 = *(const uint64_t *)p->data;
541             if (u64 <= UINT32_MAX) {
542                 *val = (uint32_t)u64;
543                 return 1;
544             }
545             err_out_of_range;
546             return 0;
547         }
548 #endif
549         return general_get_uint(p, val, sizeof(*val));
550     } else if (p->data_type == OSSL_PARAM_INTEGER) {
551 #ifndef OPENSSL_SMALL_FOOTPRINT
552         int32_t i32;
553         int64_t i64;
554
555         switch (p->data_size) {
556         case sizeof(int32_t):
557             i32 = *(const int32_t *)p->data;
558             if (i32 >= 0) {
559                 *val = i32;
560                 return 1;
561             }
562             err_unsigned_negative;
563             return 0;
564         case sizeof(int64_t):
565             i64 = *(const int64_t *)p->data;
566             if (i64 >= 0 && i64 <= UINT32_MAX) {
567                 *val = (uint32_t)i64;
568                 return 1;
569             }
570             if (i64 < 0)
571                 err_unsigned_negative;
572             else
573                 err_out_of_range;
574             return 0;
575         }
576 #endif
577         return general_get_uint(p, val, sizeof(*val));
578     } else if (p->data_type == OSSL_PARAM_REAL) {
579         switch (p->data_size) {
580         case sizeof(double):
581             d = *(const double *)p->data;
582             if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
583                 *val = (uint32_t)d;
584                 return 1;
585             }
586             err_inexact;
587             return 0;
588         }
589         err_unsupported_real;
590         return 0;
591     }
592     err_bad_type;
593     return 0;
594 }
595
596 int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
597 {
598     unsigned int shift;
599
600     if (p == NULL) {
601         err_null_argument;
602         return 0;
603     }
604     p->return_size = 0;
605
606     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
607 #ifndef OPENSSL_SMALL_FOOTPRINT
608         p->return_size = sizeof(uint32_t); /* Minimum expected size */
609         if (p->data == NULL)
610             return 1;
611         switch (p->data_size) {
612         case sizeof(uint32_t):
613             *(uint32_t *)p->data = val;
614             return 1;
615         case sizeof(uint64_t):
616             p->return_size = sizeof(uint64_t);
617             *(uint64_t *)p->data = val;
618             return 1;
619         }
620 #endif
621         return general_set_uint(p, &val, sizeof(val));
622     } else if (p->data_type == OSSL_PARAM_INTEGER) {
623 #ifndef OPENSSL_SMALL_FOOTPRINT
624         p->return_size = sizeof(int32_t); /* Minimum expected size */
625         if (p->data == NULL)
626             return 1;
627         switch (p->data_size) {
628         case sizeof(int32_t):
629             if (val <= INT32_MAX) {
630                 *(int32_t *)p->data = (int32_t)val;
631                 return 1;
632             }
633             err_out_of_range;
634             return 0;
635         case sizeof(int64_t):
636             p->return_size = sizeof(int64_t);
637             *(int64_t *)p->data = (int64_t)val;
638             return 1;
639         }
640 #endif
641         return general_set_uint(p, &val, sizeof(val));
642     } else if (p->data_type == OSSL_PARAM_REAL) {
643         p->return_size = sizeof(double);
644         if (p->data == NULL)
645             return 1;
646         switch (p->data_size) {
647         case sizeof(double):
648             shift = real_shift();
649             if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
650                 err_inexact;
651                 return 0;
652             }
653             *(double *)p->data = (double)val;
654             return 1;
655         }
656         err_unsupported_real;
657         return 0;
658     }
659     err_bad_type;
660     return 0;
661 }
662
663 OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
664 {
665     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
666                                 sizeof(uint32_t));
667 }
668
669 int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
670 {
671     double d;
672
673     if (val == NULL || p == NULL) {
674         err_null_argument;
675         return 0;
676     }
677
678     if (p->data_type == OSSL_PARAM_INTEGER) {
679 #ifndef OPENSSL_SMALL_FOOTPRINT
680         switch (p->data_size) {
681         case sizeof(int32_t):
682             *val = *(const int32_t *)p->data;
683             return 1;
684         case sizeof(int64_t):
685             *val = *(const int64_t *)p->data;
686             return 1;
687         }
688 #endif
689         return general_get_int(p, val, sizeof(*val));
690     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
691 #ifndef OPENSSL_SMALL_FOOTPRINT
692         uint64_t u64;
693
694         switch (p->data_size) {
695         case sizeof(uint32_t):
696             *val = *(const uint32_t *)p->data;
697             return 1;
698         case sizeof(uint64_t):
699             u64 = *(const uint64_t *)p->data;
700             if (u64 <= INT64_MAX) {
701                 *val = (int64_t)u64;
702                 return 1;
703             }
704             err_out_of_range;
705             return 0;
706         }
707 #endif
708         return general_get_int(p, val, sizeof(*val));
709     } else if (p->data_type == OSSL_PARAM_REAL) {
710         switch (p->data_size) {
711         case sizeof(double):
712             d = *(const double *)p->data;
713             if (d >= INT64_MIN
714                     /*
715                      * By subtracting 65535 (2^16-1) we cancel the low order
716                      * 15 bits of INT64_MAX to avoid using imprecise floating
717                      * point values.
718                      */
719                     && d < (double)(INT64_MAX - 65535) + 65536.0
720                     && d == (int64_t)d) {
721                 *val = (int64_t)d;
722                 return 1;
723             }
724             err_inexact;
725             return 0;
726         }
727         err_unsupported_real;
728         return 0;
729     }
730     err_bad_type;
731     return 0;
732 }
733
734 int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
735 {
736     uint64_t u64;
737
738     if (p == NULL) {
739         err_null_argument;
740         return 0;
741     }
742     p->return_size = 0;
743     if (p->data_type == OSSL_PARAM_INTEGER) {
744 #ifndef OPENSSL_SMALL_FOOTPRINT
745         p->return_size = sizeof(int64_t); /* Expected size */
746         if (p->data == NULL)
747             return 1;
748         switch (p->data_size) {
749         case sizeof(int32_t):
750             if (val >= INT32_MIN && val <= INT32_MAX) {
751                 p->return_size = sizeof(int32_t);
752                 *(int32_t *)p->data = (int32_t)val;
753                 return 1;
754             }
755             err_out_of_range;
756             return 0;
757         case sizeof(int64_t):
758             *(int64_t *)p->data = val;
759             return 1;
760         }
761 #endif
762         return general_set_int(p, &val, sizeof(val));
763     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
764 #ifndef OPENSSL_SMALL_FOOTPRINT
765         p->return_size = sizeof(uint64_t); /* Expected size */
766         if (p->data == NULL)
767             return 1;
768         switch (p->data_size) {
769         case sizeof(uint32_t):
770             if (val <= UINT32_MAX) {
771                 p->return_size = sizeof(uint32_t);
772                 *(uint32_t *)p->data = (uint32_t)val;
773                 return 1;
774             }
775             err_out_of_range;
776             return 0;
777         case sizeof(uint64_t):
778             *(uint64_t *)p->data = (uint64_t)val;
779             return 1;
780         }
781 #endif
782         return general_set_int(p, &val, sizeof(val));
783     } else if (p->data_type == OSSL_PARAM_REAL) {
784         p->return_size = sizeof(double);
785         if (p->data == NULL)
786             return 1;
787         switch (p->data_size) {
788         case sizeof(double):
789             u64 = val < 0 ? -val : val;
790             if ((u64 >> real_shift()) == 0) {
791                 *(double *)p->data = (double)val;
792                 return 1;
793             }
794             err_inexact;
795             return 0;
796         }
797         err_unsupported_real;
798         return 0;
799     }
800     err_bad_type;
801     return 0;
802 }
803
804 OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
805 {
806     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
807 }
808
809 int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
810 {
811     double d;
812
813     if (val == NULL || p == NULL) {
814         err_null_argument;
815         return 0;
816     }
817
818     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
819 #ifndef OPENSSL_SMALL_FOOTPRINT
820         switch (p->data_size) {
821         case sizeof(uint32_t):
822             *val = *(const uint32_t *)p->data;
823             return 1;
824         case sizeof(uint64_t):
825             *val = *(const uint64_t *)p->data;
826             return 1;
827         }
828 #endif
829         return general_get_uint(p, val, sizeof(*val));
830     } else if (p->data_type == OSSL_PARAM_INTEGER) {
831 #ifndef OPENSSL_SMALL_FOOTPRINT
832         int32_t i32;
833         int64_t i64;
834
835         switch (p->data_size) {
836         case sizeof(int32_t):
837             i32 = *(const int32_t *)p->data;
838             if (i32 >= 0) {
839                 *val = (uint64_t)i32;
840                 return 1;
841             }
842             err_unsigned_negative;
843             return 0;
844         case sizeof(int64_t):
845             i64 = *(const int64_t *)p->data;
846             if (i64 >= 0) {
847                 *val = (uint64_t)i64;
848                 return 1;
849             }
850             err_unsigned_negative;
851             return 0;
852         }
853 #endif
854         return general_get_uint(p, val, sizeof(*val));
855     } else if (p->data_type == OSSL_PARAM_REAL) {
856         switch (p->data_size) {
857         case sizeof(double):
858             d = *(const double *)p->data;
859             if (d >= 0
860                     /*
861                      * By subtracting 65535 (2^16-1) we cancel the low order
862                      * 15 bits of UINT64_MAX to avoid using imprecise floating
863                      * point values.
864                      */
865                     && d < (double)(UINT64_MAX - 65535) + 65536.0
866                     && d == (uint64_t)d) {
867                 *val = (uint64_t)d;
868                 return 1;
869             }
870             err_inexact;
871             return 0;
872         }
873         err_unsupported_real;
874         return 0;
875     }
876     err_bad_type;
877     return 0;
878 }
879
880 int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
881 {
882     if (p == NULL) {
883         err_null_argument;
884         return 0;
885     }
886     p->return_size = 0;
887
888     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
889 #ifndef OPENSSL_SMALL_FOOTPRINT
890         p->return_size = sizeof(uint64_t); /* Expected size */
891         if (p->data == NULL)
892             return 1;
893         switch (p->data_size) {
894         case sizeof(uint32_t):
895             if (val <= UINT32_MAX) {
896                 p->return_size = sizeof(uint32_t);
897                 *(uint32_t *)p->data = (uint32_t)val;
898                 return 1;
899             }
900             err_out_of_range;
901             return 0;
902         case sizeof(uint64_t):
903             *(uint64_t *)p->data = val;
904             return 1;
905         }
906 #endif
907         return general_set_uint(p, &val, sizeof(val));
908     } else if (p->data_type == OSSL_PARAM_INTEGER) {
909 #ifndef OPENSSL_SMALL_FOOTPRINT
910         p->return_size = sizeof(int64_t); /* Expected size */
911         if (p->data == NULL)
912             return 1;
913         switch (p->data_size) {
914         case sizeof(int32_t):
915             if (val <= INT32_MAX) {
916                 p->return_size = sizeof(int32_t);
917                 *(int32_t *)p->data = (int32_t)val;
918                 return 1;
919             }
920             err_out_of_range;
921             return 0;
922         case sizeof(int64_t):
923             if (val <= INT64_MAX) {
924                 *(int64_t *)p->data = (int64_t)val;
925                 return 1;
926             }
927             err_out_of_range;
928             return 0;
929         }
930 #endif
931         return general_set_uint(p, &val, sizeof(val));
932     } else if (p->data_type == OSSL_PARAM_REAL) {
933         p->return_size = sizeof(double);
934         switch (p->data_size) {
935         case sizeof(double):
936             if ((val >> real_shift()) == 0) {
937                 *(double *)p->data = (double)val;
938                 return 1;
939             }
940             err_inexact;
941             return 0;
942         }
943         err_unsupported_real;
944         return 0;
945     }
946     err_bad_type;
947     return 0;
948 }
949
950 OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
951 {
952     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
953                                 sizeof(uint64_t));
954 }
955
956 int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
957 {
958 #ifndef OPENSSL_SMALL_FOOTPRINT
959     switch (sizeof(size_t)) {
960     case sizeof(uint32_t):
961         return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
962     case sizeof(uint64_t):
963         return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
964     }
965 #endif
966     return general_get_uint(p, val, sizeof(*val));
967 }
968
969 int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
970 {
971 #ifndef OPENSSL_SMALL_FOOTPRINT
972     switch (sizeof(size_t)) {
973     case sizeof(uint32_t):
974         return OSSL_PARAM_set_uint32(p, (uint32_t)val);
975     case sizeof(uint64_t):
976         return OSSL_PARAM_set_uint64(p, (uint64_t)val);
977     }
978 #endif
979     return general_set_uint(p, &val, sizeof(val));
980 }
981
982 OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
983 {
984     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
985                                 sizeof(size_t));
986 }
987
988 int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
989 {
990 #ifndef OPENSSL_SMALL_FOOTPRINT
991     switch (sizeof(time_t)) {
992     case sizeof(int32_t):
993         return OSSL_PARAM_get_int32(p, (int32_t *)val);
994     case sizeof(int64_t):
995         return OSSL_PARAM_get_int64(p, (int64_t *)val);
996     }
997 #endif
998     return general_get_int(p, val, sizeof(*val));
999 }
1000
1001 int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1002 {
1003 #ifndef OPENSSL_SMALL_FOOTPRINT
1004     switch (sizeof(time_t)) {
1005     case sizeof(int32_t):
1006         return OSSL_PARAM_set_int32(p, (int32_t)val);
1007     case sizeof(int64_t):
1008         return OSSL_PARAM_set_int64(p, (int64_t)val);
1009     }
1010 #endif
1011     return general_set_int(p, &val, sizeof(val));
1012 }
1013
1014 OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1015 {
1016     return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1017 }
1018
1019 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1020 {
1021     BIGNUM *b = NULL;
1022
1023     if (val == NULL || p == NULL) {
1024         err_null_argument;
1025         return 0;
1026     }
1027
1028     switch (p->data_type) {
1029     case OSSL_PARAM_UNSIGNED_INTEGER:
1030         b = BN_native2bn(p->data, (int)p->data_size, *val);
1031         break;
1032     case OSSL_PARAM_INTEGER:
1033         b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1034         break;
1035     default:
1036         err_bad_type;
1037         break;
1038     }
1039
1040     if (b == NULL) {
1041         ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1042         return 0;
1043     }
1044
1045     *val = b;
1046     return 1;
1047 }
1048
1049 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1050 {
1051     size_t bytes;
1052
1053     if (p == NULL) {
1054         err_null_argument;
1055         return 0;
1056     }
1057     p->return_size = 0;
1058     if (val == NULL) {
1059         err_null_argument;
1060         return 0;
1061     }
1062     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1063         err_bad_type;
1064         return 0;
1065     }
1066
1067     bytes = (size_t)BN_num_bytes(val);
1068     /* We add 1 byte for signed numbers, to make space for a sign extension */
1069     if (p->data_type == OSSL_PARAM_INTEGER)
1070         bytes++;
1071     /* We make sure that at least one byte is used, so zero is properly set */
1072     if (bytes == 0)
1073         bytes++;
1074
1075     p->return_size = bytes;
1076     if (p->data == NULL)
1077         return 1;
1078     if (p->data_size >= bytes) {
1079         p->return_size = p->data_size;
1080
1081         switch (p->data_type) {
1082         case OSSL_PARAM_UNSIGNED_INTEGER:
1083             if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1084                 return 1;
1085             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1086             break;
1087         case OSSL_PARAM_INTEGER:
1088             if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1089                 return 1;
1090             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1091             break;
1092         default:
1093             err_bad_type;
1094             break;
1095         }
1096         return 0;
1097     }
1098     err_too_small;
1099     return 0;
1100 }
1101
1102 OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1103                                    size_t bsize)
1104 {
1105     return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1106                                 buf, bsize);
1107 }
1108
1109 int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1110 {
1111     int64_t i64;
1112     uint64_t u64;
1113
1114     if (val == NULL || p == NULL) {
1115         err_null_argument;
1116         return 0;
1117     }
1118
1119     if (p->data_type == OSSL_PARAM_REAL) {
1120         switch (p->data_size) {
1121         case sizeof(double):
1122             *val = *(const double *)p->data;
1123             return 1;
1124         }
1125         err_unsupported_real;
1126         return 0;
1127     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1128         switch (p->data_size) {
1129         case sizeof(uint32_t):
1130             *val = *(const uint32_t *)p->data;
1131             return 1;
1132         case sizeof(uint64_t):
1133             u64 = *(const uint64_t *)p->data;
1134             if ((u64 >> real_shift()) == 0) {
1135                 *val = (double)u64;
1136                 return 1;
1137             }
1138             err_inexact;
1139             return 0;
1140         }
1141     } else if (p->data_type == OSSL_PARAM_INTEGER) {
1142         switch (p->data_size) {
1143         case sizeof(int32_t):
1144             *val = *(const int32_t *)p->data;
1145             return 1;
1146         case sizeof(int64_t):
1147             i64 = *(const int64_t *)p->data;
1148             u64 = i64 < 0 ? -i64 : i64;
1149             if ((u64 >> real_shift()) == 0) {
1150                 *val = 0.0 + i64;
1151                 return 1;
1152             }
1153             err_inexact;
1154             return 0;
1155         }
1156     }
1157     err_bad_type;
1158     return 0;
1159 }
1160
1161 int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1162 {
1163     if (p == NULL) {
1164         err_null_argument;
1165         return 0;
1166     }
1167     p->return_size = 0;
1168
1169     if (p->data_type == OSSL_PARAM_REAL) {
1170         p->return_size = sizeof(double);
1171         if (p->data == NULL)
1172             return 1;
1173         switch (p->data_size) {
1174         case sizeof(double):
1175             *(double *)p->data = val;
1176             return 1;
1177         }
1178         err_unsupported_real;
1179         return 0;
1180     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1181         p->return_size = sizeof(double);
1182         if (p->data == NULL)
1183             return 1;
1184         if (val != (uint64_t)val) {
1185             err_inexact;
1186             return 0;
1187         }
1188         switch (p->data_size) {
1189         case sizeof(uint32_t):
1190             if (val >= 0 && val <= UINT32_MAX) {
1191                 p->return_size = sizeof(uint32_t);
1192                 *(uint32_t *)p->data = (uint32_t)val;
1193                 return 1;
1194             }
1195             err_out_of_range;
1196             return 0;
1197         case sizeof(uint64_t):
1198             if (val >= 0
1199                     /*
1200                      * By subtracting 65535 (2^16-1) we cancel the low order
1201                      * 15 bits of UINT64_MAX to avoid using imprecise floating
1202                      * point values.
1203                      */
1204                     && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1205                 p->return_size = sizeof(uint64_t);
1206                 *(uint64_t *)p->data = (uint64_t)val;
1207                 return 1;
1208             }
1209             err_out_of_range;
1210             return 0;
1211         }
1212     } else if (p->data_type == OSSL_PARAM_INTEGER) {
1213         p->return_size = sizeof(double);
1214         if (p->data == NULL)
1215             return 1;
1216         if (val != (int64_t)val) {
1217             err_inexact;
1218             return 0;
1219         }
1220         switch (p->data_size) {
1221         case sizeof(int32_t):
1222             if (val >= INT32_MIN && val <= INT32_MAX) {
1223                 p->return_size = sizeof(int32_t);
1224                 *(int32_t *)p->data = (int32_t)val;
1225                 return 1;
1226             }
1227             err_out_of_range;
1228             return 0;
1229         case sizeof(int64_t):
1230             if (val >= INT64_MIN
1231                     /*
1232                      * By subtracting 65535 (2^16-1) we cancel the low order
1233                      * 15 bits of INT64_MAX to avoid using imprecise floating
1234                      * point values.
1235                      */
1236                     && val < (double)(INT64_MAX - 65535) + 65536.0) {
1237                 p->return_size = sizeof(int64_t);
1238                 *(int64_t *)p->data = (int64_t)val;
1239                 return 1;
1240             }
1241             err_out_of_range;
1242             return 0;
1243         }
1244     }
1245     err_bad_type;
1246     return 0;
1247 }
1248
1249 OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1250 {
1251     return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1252 }
1253
1254 static int get_string_internal(const OSSL_PARAM *p, void **val,
1255                                size_t *max_len, size_t *used_len,
1256                                unsigned int type)
1257 {
1258     size_t sz, alloc_sz;
1259
1260     if ((val == NULL && used_len == NULL) || p == NULL) {
1261         err_null_argument;
1262         return 0;
1263     }
1264     if (p->data_type != type) {
1265         err_bad_type;
1266         return 0;
1267     }
1268
1269     sz = p->data_size;
1270     /*
1271      * If the input size is 0, or the input string needs NUL byte
1272      * termination, allocate an extra byte.
1273      */
1274     alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1275
1276     if (used_len != NULL)
1277         *used_len = sz;
1278
1279     if (p->data == NULL) {
1280         err_null_argument;
1281         return 0;
1282     }
1283
1284     if (val == NULL)
1285         return 1;
1286
1287     if (*val == NULL) {
1288         char *const q = OPENSSL_malloc(alloc_sz);
1289
1290         if (q == NULL)
1291             return 0;
1292         *val = q;
1293         *max_len = alloc_sz;
1294     }
1295
1296     if (*max_len < sz) {
1297         err_too_small;
1298         return 0;
1299     }
1300     memcpy(*val, p->data, sz);
1301     return 1;
1302 }
1303
1304 int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1305 {
1306     int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1307                                   OSSL_PARAM_UTF8_STRING);
1308
1309     /*
1310      * We try to ensure that the copied string is terminated with a
1311      * NUL byte.  That should be easy, just place a NUL byte at
1312      * |((char*)*val)[p->data_size]|.
1313      * Unfortunately, we have seen cases where |p->data_size| doesn't
1314      * correctly reflect the length of the string, and just happens
1315      * to be out of bounds according to |max_len|, so in that case, we
1316      * make the extra step of trying to find the true length of the
1317      * string that |p->data| points at, and use that as an index to
1318      * place the NUL byte in |*val|.
1319      */
1320     size_t data_length = p->data_size;
1321
1322     if (ret == 0)
1323         return 0;
1324     if (data_length >= max_len)
1325         data_length = OPENSSL_strnlen(p->data, data_length);
1326     if (data_length >= max_len) {
1327         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1328         return 0;            /* No space for a terminating NUL byte */
1329     }
1330     (*val)[data_length] = '\0';
1331
1332     return ret;
1333 }
1334
1335 int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1336                                 size_t *used_len)
1337 {
1338     return get_string_internal(p, val, &max_len, used_len,
1339                                OSSL_PARAM_OCTET_STRING);
1340 }
1341
1342 static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1343                                unsigned int type)
1344 {
1345     p->return_size = len;
1346     if (p->data == NULL)
1347         return 1;
1348     if (p->data_type != type) {
1349         err_bad_type;
1350         return 0;
1351     }
1352     if (p->data_size < len) {
1353         err_too_small;
1354         return 0;
1355     }
1356
1357     memcpy(p->data, val, len);
1358     /* If possible within the size of p->data, add a NUL terminator byte */
1359     if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1360         ((char *)p->data)[len] = '\0';
1361     return 1;
1362 }
1363
1364 int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1365 {
1366     if (p == NULL) {
1367         err_null_argument;
1368         return 0;
1369     }
1370
1371     p->return_size = 0;
1372     if (val == NULL) {
1373         err_null_argument;
1374         return 0;
1375     }
1376     return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1377 }
1378
1379 int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1380                                 size_t len)
1381 {
1382     if (p == NULL) {
1383         err_null_argument;
1384         return 0;
1385     }
1386
1387     p->return_size = 0;
1388     if (val == NULL) {
1389         err_null_argument;
1390         return 0;
1391     }
1392     return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1393 }
1394
1395 OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1396                                             size_t bsize)
1397 {
1398     if (buf != NULL && bsize == 0)
1399         bsize = strlen(buf);
1400     return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1401 }
1402
1403 OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1404                                              size_t bsize)
1405 {
1406     return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1407 }
1408
1409 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1410                             size_t *used_len, unsigned int type)
1411 {
1412     if (val == NULL || p == NULL) {
1413         err_null_argument;
1414         return 0;
1415     }
1416     if (p->data_type != type) {
1417         err_bad_type;
1418         return 0;
1419     }
1420     if (used_len != NULL)
1421         *used_len = p->data_size;
1422     *val = *(const void **)p->data;
1423     return 1;
1424 }
1425
1426 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1427 {
1428     return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1429 }
1430
1431 int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1432                              size_t *used_len)
1433 {
1434     return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1435 }
1436
1437 static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1438                             unsigned int type, size_t len)
1439 {
1440     p->return_size = len;
1441     if (p->data_type != type) {
1442         err_bad_type;
1443         return 0;
1444     }
1445     if (p->data != NULL)
1446         *(const void **)p->data = val;
1447     return 1;
1448 }
1449
1450 int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1451 {
1452     if (p == NULL) {
1453         err_null_argument;
1454         return 0;
1455     }
1456     p->return_size = 0;
1457     return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1458                             val == NULL ? 0 : strlen(val));
1459 }
1460
1461 int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1462                              size_t used_len)
1463 {
1464     if (p == NULL) {
1465         err_null_argument;
1466         return 0;
1467     }
1468     p->return_size = 0;
1469     return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1470 }
1471
1472 OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1473                                          size_t bsize)
1474 {
1475     return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1476 }
1477
1478 OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1479                                           size_t bsize)
1480 {
1481     return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1482 }
1483
1484 OSSL_PARAM OSSL_PARAM_construct_end(void)
1485 {
1486     OSSL_PARAM end = OSSL_PARAM_END;
1487
1488     return end;
1489 }
1490
1491 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1492                                    size_t *used_len, unsigned int type)
1493 {
1494     if (val == NULL || p == NULL) {
1495         err_null_argument;
1496         return 0;
1497     }
1498     if (p->data_type != type) {
1499         err_bad_type;
1500         return 0;
1501     }
1502     if (used_len != NULL)
1503         *used_len = p->data_size;
1504     *val = p->data;
1505     return 1;
1506 }
1507
1508 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1509 {
1510     int rv;
1511
1512     ERR_set_mark();
1513     rv = OSSL_PARAM_get_utf8_ptr(p, val);
1514     ERR_pop_to_mark();
1515
1516     return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1517                                          OSSL_PARAM_UTF8_STRING);
1518 }
1519
1520 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1521                                     size_t *used_len)
1522 {
1523     int rv;
1524
1525     ERR_set_mark();
1526     rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1527     ERR_pop_to_mark();
1528
1529     return rv || get_string_ptr_internal(p, val, used_len,
1530                                          OSSL_PARAM_OCTET_STRING);
1531 }