VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.0/crypto/params.c@ 100832

Last change on this file since 100832 was 99366, checked in by vboxsync, 23 months ago

openssl-3.1.0: Applied and adjusted our OpenSSL changes to 3.0.7. bugref:10418

File size: 42.6 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/*
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 */
44static unsigned int real_shift(void)
45{
46 return sizeof(double) == 4 ? 24 : 53;
47}
48
49OSSL_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
58const 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
63static 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
76int OSSL_PARAM_modified(const OSSL_PARAM *p)
77{
78 return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
79}
80
81void 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 */
89static 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 */
98static 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 */
113static 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 */
161static 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 */
169static 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 */
176static 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 */
187static 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 */
194static 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 */
205static 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 */
223static 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 */
234static 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
251int 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
264int 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
277OSSL_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
282int 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
295int 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
308OSSL_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
314int 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
327int 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
340OSSL_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
345int 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
358int 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
371OSSL_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
377int 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
450int 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
516OSSL_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
522int 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
596int 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
663OSSL_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
669int 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
734int 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
804OSSL_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
809int 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
880int 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
950OSSL_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
956int 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
969int 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
982OSSL_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
988int 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
1001int 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
1014OSSL_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
1019int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1020{
1021 BIGNUM *b;
1022
1023 if (val == NULL || p == NULL) {
1024 err_null_argument;
1025 return 0;
1026 }
1027 if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
1028 err_bad_type;
1029 return 0;
1030 }
1031
1032 b = BN_native2bn(p->data, (int)p->data_size, *val);
1033 if (b != NULL) {
1034 *val = b;
1035 return 1;
1036 }
1037 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1038 return 0;
1039}
1040
1041int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1042{
1043 size_t bytes;
1044
1045 if (p == NULL) {
1046 err_null_argument;
1047 return 0;
1048 }
1049 p->return_size = 0;
1050 if (val == NULL) {
1051 err_null_argument;
1052 return 0;
1053 }
1054 if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
1055 err_bad_type;
1056 return 0;
1057 }
1058
1059 /* For the moment, only positive values are permitted */
1060 if (BN_is_negative(val)) {
1061 err_unsigned_negative;
1062 return 0;
1063 }
1064
1065 bytes = (size_t)BN_num_bytes(val);
1066 /* We make sure that at least one byte is used, so zero is properly set */
1067 if (bytes == 0)
1068 bytes++;
1069
1070 p->return_size = bytes;
1071 if (p->data == NULL)
1072 return 1;
1073 if (p->data_size >= bytes) {
1074 p->return_size = p->data_size;
1075 if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1076 return 1;
1077 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1078 return 0;
1079 }
1080 err_too_small;
1081 return 0;
1082}
1083
1084OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1085 size_t bsize)
1086{
1087 return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1088 buf, bsize);
1089}
1090
1091int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1092{
1093 int64_t i64;
1094 uint64_t u64;
1095
1096 if (val == NULL || p == NULL) {
1097 err_null_argument;
1098 return 0;
1099 }
1100
1101 if (p->data_type == OSSL_PARAM_REAL) {
1102 switch (p->data_size) {
1103 case sizeof(double):
1104 *val = *(const double *)p->data;
1105 return 1;
1106 }
1107 err_unsupported_real;
1108 return 0;
1109 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1110 switch (p->data_size) {
1111 case sizeof(uint32_t):
1112 *val = *(const uint32_t *)p->data;
1113 return 1;
1114 case sizeof(uint64_t):
1115 u64 = *(const uint64_t *)p->data;
1116 if ((u64 >> real_shift()) == 0) {
1117 *val = (double)u64;
1118 return 1;
1119 }
1120 err_inexact;
1121 return 0;
1122 }
1123 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1124 switch (p->data_size) {
1125 case sizeof(int32_t):
1126 *val = *(const int32_t *)p->data;
1127 return 1;
1128 case sizeof(int64_t):
1129 i64 = *(const int64_t *)p->data;
1130 u64 = i64 < 0 ? -i64 : i64;
1131 if ((u64 >> real_shift()) == 0) {
1132 *val = 0.0 + i64;
1133 return 1;
1134 }
1135 err_inexact;
1136 return 0;
1137 }
1138 }
1139 err_bad_type;
1140 return 0;
1141}
1142
1143int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1144{
1145 if (p == NULL) {
1146 err_null_argument;
1147 return 0;
1148 }
1149 p->return_size = 0;
1150
1151 if (p->data_type == OSSL_PARAM_REAL) {
1152 p->return_size = sizeof(double);
1153 if (p->data == NULL)
1154 return 1;
1155 switch (p->data_size) {
1156 case sizeof(double):
1157 *(double *)p->data = val;
1158 return 1;
1159 }
1160 err_unsupported_real;
1161 return 0;
1162 } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1163 p->return_size = sizeof(double);
1164 if (p->data == NULL)
1165 return 1;
1166 if (val != (uint64_t)val) {
1167 err_inexact;
1168 return 0;
1169 }
1170 switch (p->data_size) {
1171 case sizeof(uint32_t):
1172 if (val >= 0 && val <= UINT32_MAX) {
1173 p->return_size = sizeof(uint32_t);
1174 *(uint32_t *)p->data = (uint32_t)val;
1175 return 1;
1176 }
1177 err_out_of_range;
1178 return 0;
1179 case sizeof(uint64_t):
1180 if (val >= 0
1181 /*
1182 * By subtracting 65535 (2^16-1) we cancel the low order
1183 * 15 bits of UINT64_MAX to avoid using imprecise floating
1184 * point values.
1185 */
1186 && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1187 p->return_size = sizeof(uint64_t);
1188 *(uint64_t *)p->data = (uint64_t)val;
1189 return 1;
1190 }
1191 err_out_of_range;
1192 return 0;
1193 }
1194 } else if (p->data_type == OSSL_PARAM_INTEGER) {
1195 p->return_size = sizeof(double);
1196 if (p->data == NULL)
1197 return 1;
1198 if (val != (int64_t)val) {
1199 err_inexact;
1200 return 0;
1201 }
1202 switch (p->data_size) {
1203 case sizeof(int32_t):
1204 if (val >= INT32_MIN && val <= INT32_MAX) {
1205 p->return_size = sizeof(int32_t);
1206 *(int32_t *)p->data = (int32_t)val;
1207 return 1;
1208 }
1209 err_out_of_range;
1210 return 0;
1211 case sizeof(int64_t):
1212 if (val >= INT64_MIN
1213 /*
1214 * By subtracting 65535 (2^16-1) we cancel the low order
1215 * 15 bits of INT64_MAX to avoid using imprecise floating
1216 * point values.
1217 */
1218 && val < (double)(INT64_MAX - 65535) + 65536.0) {
1219 p->return_size = sizeof(int64_t);
1220 *(int64_t *)p->data = (int64_t)val;
1221 return 1;
1222 }
1223 err_out_of_range;
1224 return 0;
1225 }
1226 }
1227 err_bad_type;
1228 return 0;
1229}
1230
1231OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1232{
1233 return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1234}
1235
1236static int get_string_internal(const OSSL_PARAM *p, void **val,
1237 size_t *max_len, size_t *used_len,
1238 unsigned int type)
1239{
1240 size_t sz, alloc_sz;
1241
1242 if ((val == NULL && used_len == NULL) || p == NULL) {
1243 err_null_argument;
1244 return 0;
1245 }
1246 if (p->data_type != type) {
1247 err_bad_type;
1248 return 0;
1249 }
1250
1251 sz = p->data_size;
1252 /*
1253 * If the input size is 0, or the input string needs NUL byte
1254 * termination, allocate an extra byte.
1255 */
1256 alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1257
1258 if (used_len != NULL)
1259 *used_len = sz;
1260
1261 if (p->data == NULL) {
1262 err_null_argument;
1263 return 0;
1264 }
1265
1266 if (val == NULL)
1267 return 1;
1268
1269 if (*val == NULL) {
1270 char *const q = OPENSSL_malloc(alloc_sz);
1271
1272 if (q == NULL) {
1273 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1274 return 0;
1275 }
1276 *val = q;
1277 *max_len = alloc_sz;
1278 }
1279
1280 if (*max_len < sz) {
1281 err_too_small;
1282 return 0;
1283 }
1284 memcpy(*val, p->data, sz);
1285 return 1;
1286}
1287
1288int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1289{
1290 int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1291 OSSL_PARAM_UTF8_STRING);
1292
1293 /*
1294 * We try to ensure that the copied string is terminated with a
1295 * NUL byte. That should be easy, just place a NUL byte at
1296 * |((char*)*val)[p->data_size]|.
1297 * Unfortunately, we have seen cases where |p->data_size| doesn't
1298 * correctly reflect the length of the string, and just happens
1299 * to be out of bounds according to |max_len|, so in that case, we
1300 * make the extra step of trying to find the true length of the
1301 * string that |p->data| points at, and use that as an index to
1302 * place the NUL byte in |*val|.
1303 */
1304 size_t data_length = p->data_size;
1305
1306 if (ret == 0)
1307 return 0;
1308 if (data_length >= max_len)
1309 data_length = OPENSSL_strnlen(p->data, data_length);
1310 if (data_length >= max_len) {
1311 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1312 return 0; /* No space for a terminating NUL byte */
1313 }
1314 (*val)[data_length] = '\0';
1315
1316 return ret;
1317}
1318
1319int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1320 size_t *used_len)
1321{
1322 return get_string_internal(p, val, &max_len, used_len,
1323 OSSL_PARAM_OCTET_STRING);
1324}
1325
1326static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1327 unsigned int type)
1328{
1329 p->return_size = len;
1330 if (p->data == NULL)
1331 return 1;
1332 if (p->data_type != type) {
1333 err_bad_type;
1334 return 0;
1335 }
1336 if (p->data_size < len) {
1337 err_too_small;
1338 return 0;
1339 }
1340
1341 memcpy(p->data, val, len);
1342 /* If possible within the size of p->data, add a NUL terminator byte */
1343 if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1344 ((char *)p->data)[len] = '\0';
1345 return 1;
1346}
1347
1348int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1349{
1350 if (p == NULL) {
1351 err_null_argument;
1352 return 0;
1353 }
1354
1355 p->return_size = 0;
1356 if (val == NULL) {
1357 err_null_argument;
1358 return 0;
1359 }
1360 return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1361}
1362
1363int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1364 size_t len)
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, len, OSSL_PARAM_OCTET_STRING);
1377}
1378
1379OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1380 size_t bsize)
1381{
1382 if (buf != NULL && bsize == 0)
1383 bsize = strlen(buf);
1384 return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1385}
1386
1387OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1388 size_t bsize)
1389{
1390 return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1391}
1392
1393static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1394 size_t *used_len, unsigned int type)
1395{
1396 if (val == NULL || p == NULL) {
1397 err_null_argument;
1398 return 0;
1399 }
1400 if (p->data_type != type) {
1401 err_bad_type;
1402 return 0;
1403 }
1404 if (used_len != NULL)
1405 *used_len = p->data_size;
1406 *val = *(const void **)p->data;
1407 return 1;
1408}
1409
1410int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1411{
1412 return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1413}
1414
1415int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1416 size_t *used_len)
1417{
1418 return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1419}
1420
1421static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1422 unsigned int type, size_t len)
1423{
1424 p->return_size = len;
1425 if (p->data_type != type) {
1426 err_bad_type;
1427 return 0;
1428 }
1429 if (p->data != NULL)
1430 *(const void **)p->data = val;
1431 return 1;
1432}
1433
1434int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1435{
1436 if (p == NULL) {
1437 err_null_argument;
1438 return 0;
1439 }
1440 p->return_size = 0;
1441 return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1442 val == NULL ? 0 : strlen(val));
1443}
1444
1445int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1446 size_t used_len)
1447{
1448 if (p == NULL) {
1449 err_null_argument;
1450 return 0;
1451 }
1452 p->return_size = 0;
1453 return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1454}
1455
1456OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1457 size_t bsize)
1458{
1459 return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1460}
1461
1462OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1463 size_t bsize)
1464{
1465 return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1466}
1467
1468OSSL_PARAM OSSL_PARAM_construct_end(void)
1469{
1470 OSSL_PARAM end = OSSL_PARAM_END;
1471
1472 return end;
1473}
1474
1475static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1476 size_t *used_len, unsigned int type)
1477{
1478 if (val == NULL || p == NULL) {
1479 err_null_argument;
1480 return 0;
1481 }
1482 if (p->data_type != type) {
1483 err_bad_type;
1484 return 0;
1485 }
1486 if (used_len != NULL)
1487 *used_len = p->data_size;
1488 *val = p->data;
1489 return 1;
1490}
1491
1492int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1493{
1494 int rv;
1495
1496 ERR_set_mark();
1497 rv = OSSL_PARAM_get_utf8_ptr(p, val);
1498 ERR_pop_to_mark();
1499
1500 return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1501 OSSL_PARAM_UTF8_STRING);
1502}
1503
1504int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1505 size_t *used_len)
1506{
1507 int rv;
1508
1509 ERR_set_mark();
1510 rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1511 ERR_pop_to_mark();
1512
1513 return rv || get_string_ptr_internal(p, val, used_len,
1514 OSSL_PARAM_OCTET_STRING);
1515}
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