Coverage Report

Created: 2024-01-12 07:13

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