Coverage Report

Created: 2025-08-28 07:07

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