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