Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/params.c

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