VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/crypto/params.c@ 104078

Last change on this file since 104078 was 104078, checked in by vboxsync, 8 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette