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