Coverage Report

Created: 2025-06-13 06:36

/src/openssl/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
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
263
    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
402
{
55
402
    if (p != NULL && key != NULL)
56
588
        for (; p->key != NULL; p++)
57
588
            if (strcmp(key, p->key) == 0)
58
402
                return p;
59
0
    return NULL;
60
402
}
61
62
const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
63
139
{
64
139
    return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
65
139
}
66
67
static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
68
                                       void *data, size_t data_size)
69
263
{
70
263
    OSSL_PARAM res;
71
72
263
    res.key = key;
73
263
    res.data_type = data_type;
74
263
    res.data = data;
75
263
    res.data_size = data_size;
76
263
    res.return_size = OSSL_PARAM_UNMODIFIED;
77
263
    return res;
78
263
}
79
80
int OSSL_PARAM_modified(const OSSL_PARAM *p)
81
0
{
82
0
    return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
83
0
}
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
0
{
268
0
#ifndef OPENSSL_SMALL_FOOTPRINT
269
0
    switch (sizeof(int)) {
270
0
    case sizeof(int32_t):
271
0
        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
0
    }
275
0
#endif
276
0
    return general_get_int(p, val, sizeof(*val));
277
0
}
278
279
int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
280
62
{
281
62
#ifndef OPENSSL_SMALL_FOOTPRINT
282
62
    switch (sizeof(int)) {
283
62
    case sizeof(int32_t):
284
62
        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
62
    }
288
0
#endif
289
0
    return general_set_int(p, &val, sizeof(val));
290
62
}
291
292
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
293
62
{
294
62
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
295
62
}
296
297
int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
298
0
{
299
0
#ifndef OPENSSL_SMALL_FOOTPRINT
300
0
    switch (sizeof(unsigned int)) {
301
0
    case sizeof(uint32_t):
302
0
        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
0
    }
306
0
#endif
307
0
    return general_get_uint(p, val, sizeof(*val));
308
0
}
309
310
int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
311
139
{
312
139
#ifndef OPENSSL_SMALL_FOOTPRINT
313
139
    switch (sizeof(unsigned int)) {
314
139
    case sizeof(uint32_t):
315
139
        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
139
    }
319
0
#endif
320
0
    return general_set_uint(p, &val, sizeof(val));
321
139
}
322
323
OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
324
0
{
325
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
326
0
                                sizeof(unsigned int));
327
0
}
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
0
{
394
0
    if (val == NULL || p == NULL) {
395
0
        err_null_argument;
396
0
        return 0;
397
0
    }
398
399
0
    if (p->data == NULL) {
400
0
        err_null_argument;
401
0
        return 0;
402
0
    }
403
404
0
    if (p->data_type == OSSL_PARAM_INTEGER) {
405
0
#ifndef OPENSSL_SMALL_FOOTPRINT
406
0
        int64_t i64;
407
408
0
        switch (p->data_size) {
409
0
        case sizeof(int32_t):
410
0
            *val = *(const int32_t *)p->data;
411
0
            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
0
        }
421
0
#endif
422
0
        return general_get_int(p, val, sizeof(*val));
423
424
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
425
0
#ifndef OPENSSL_SMALL_FOOTPRINT
426
0
        uint32_t u32;
427
0
        uint64_t u64;
428
429
0
        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
0
        case sizeof(uint64_t):
439
0
            u64 = *(const uint64_t *)p->data;
440
0
            if (u64 <= INT32_MAX) {
441
0
                *val = (int32_t)u64;
442
0
                return 1;
443
0
            }
444
0
            err_out_of_range;
445
0
            return 0;
446
0
        }
447
0
#endif
448
0
        return general_get_int(p, val, sizeof(*val));
449
450
0
    } 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
0
}
471
472
int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
473
62
{
474
62
    if (p == NULL) {
475
0
        err_null_argument;
476
0
        return 0;
477
0
    }
478
62
    p->return_size = 0;
479
62
    if (p->data_type == OSSL_PARAM_INTEGER) {
480
62
#ifndef OPENSSL_SMALL_FOOTPRINT
481
62
        p->return_size = sizeof(int32_t); /* Minimum expected size */
482
62
        if (p->data == NULL)
483
0
            return 1;
484
62
        switch (p->data_size) {
485
62
        case sizeof(int32_t):
486
62
            *(int32_t *)p->data = val;
487
62
            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
62
        }
493
0
#endif
494
0
        return general_set_int(p, &val, sizeof(val));
495
62
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
496
0
#ifndef OPENSSL_SMALL_FOOTPRINT
497
0
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
498
0
        if (p->data == NULL)
499
0
            return 1;
500
0
        switch (p->data_size) {
501
0
        case sizeof(uint32_t):
502
0
            *(uint32_t *)p->data = (uint32_t)val;
503
0
            return 1;
504
0
        case sizeof(uint64_t):
505
0
            p->return_size = sizeof(uint64_t);
506
0
            *(uint64_t *)p->data = (uint64_t)val;
507
0
            return 1;
508
0
        }
509
0
#endif
510
0
        return general_set_int(p, &val, sizeof(val));
511
0
    } 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
62
}
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
0
{
548
0
    if (val == NULL || p == NULL) {
549
0
        err_null_argument;
550
0
        return 0;
551
0
    }
552
553
0
    if (p->data == NULL) {
554
0
        err_null_argument;
555
0
        return 0;
556
0
    }
557
558
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
559
0
#ifndef OPENSSL_SMALL_FOOTPRINT
560
0
        uint64_t u64;
561
562
0
        switch (p->data_size) {
563
0
        case sizeof(uint32_t):
564
0
            *val = *(const uint32_t *)p->data;
565
0
            return 1;
566
0
        case sizeof(uint64_t):
567
0
            u64 = *(const uint64_t *)p->data;
568
0
            if (u64 <= UINT32_MAX) {
569
0
                *val = (uint32_t)u64;
570
0
                return 1;
571
0
            }
572
0
            err_out_of_range;
573
0
            return 0;
574
0
        }
575
0
#endif
576
0
        return general_get_uint(p, val, sizeof(*val));
577
0
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
578
0
#ifndef OPENSSL_SMALL_FOOTPRINT
579
0
        int32_t i32;
580
0
        int64_t i64;
581
582
0
        switch (p->data_size) {
583
0
        case sizeof(int32_t):
584
0
            i32 = *(const int32_t *)p->data;
585
0
            if (i32 >= 0) {
586
0
                *val = i32;
587
0
                return 1;
588
0
            }
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
0
        }
603
0
#endif
604
0
        return general_get_uint(p, val, sizeof(*val));
605
0
    } 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
0
}
626
627
int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
628
139
{
629
139
    if (p == NULL) {
630
0
        err_null_argument;
631
0
        return 0;
632
0
    }
633
139
    p->return_size = 0;
634
635
139
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
636
139
#ifndef OPENSSL_SMALL_FOOTPRINT
637
139
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
638
139
        if (p->data == NULL)
639
0
            return 1;
640
139
        switch (p->data_size) {
641
0
        case sizeof(uint32_t):
642
0
            *(uint32_t *)p->data = val;
643
0
            return 1;
644
139
        case sizeof(uint64_t):
645
139
            p->return_size = sizeof(uint64_t);
646
139
            *(uint64_t *)p->data = val;
647
139
            return 1;
648
139
        }
649
0
#endif
650
0
        return general_set_uint(p, &val, sizeof(val));
651
139
    } 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
139
}
697
698
OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
699
0
{
700
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
701
0
                                sizeof(uint32_t));
702
0
}
703
704
int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
705
0
{
706
0
    if (val == NULL || p == NULL) {
707
0
        err_null_argument;
708
0
        return 0;
709
0
    }
710
711
0
    if (p->data == NULL) {
712
0
        err_null_argument;
713
0
        return 0;
714
0
    }
715
716
0
    if (p->data_type == OSSL_PARAM_INTEGER) {
717
0
#ifndef OPENSSL_SMALL_FOOTPRINT
718
0
        switch (p->data_size) {
719
0
        case sizeof(int32_t):
720
0
            *val = *(const int32_t *)p->data;
721
0
            return 1;
722
0
        case sizeof(int64_t):
723
0
            *val = *(const int64_t *)p->data;
724
0
            return 1;
725
0
        }
726
0
#endif
727
0
        return general_get_int(p, val, sizeof(*val));
728
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
729
0
#ifndef OPENSSL_SMALL_FOOTPRINT
730
0
        uint64_t u64;
731
732
0
        switch (p->data_size) {
733
0
        case sizeof(uint32_t):
734
0
            *val = *(const uint32_t *)p->data;
735
0
            return 1;
736
0
        case sizeof(uint64_t):
737
0
            u64 = *(const uint64_t *)p->data;
738
0
            if (u64 <= INT64_MAX) {
739
0
                *val = (int64_t)u64;
740
0
                return 1;
741
0
            }
742
0
            err_out_of_range;
743
0
            return 0;
744
0
        }
745
0
#endif
746
0
        return general_get_int(p, val, sizeof(*val));
747
0
    } 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
0
}
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
0
{
861
0
    if (val == NULL || p == NULL) {
862
0
        err_null_argument;
863
0
        return 0;
864
0
    }
865
866
0
    if (p->data == NULL) {
867
0
        err_null_argument;
868
0
        return 0;
869
0
    }
870
871
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
872
0
#ifndef OPENSSL_SMALL_FOOTPRINT
873
0
        switch (p->data_size) {
874
0
        case sizeof(uint32_t):
875
0
            *val = *(const uint32_t *)p->data;
876
0
            return 1;
877
0
        case sizeof(uint64_t):
878
0
            *val = *(const uint64_t *)p->data;
879
0
            return 1;
880
0
        }
881
0
#endif
882
0
        return general_get_uint(p, val, sizeof(*val));
883
0
    } 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
0
}
936
937
int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
938
62
{
939
62
    if (p == NULL) {
940
0
        err_null_argument;
941
0
        return 0;
942
0
    }
943
62
    p->return_size = 0;
944
945
62
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
946
62
#ifndef OPENSSL_SMALL_FOOTPRINT
947
62
        if (p->data == NULL) {
948
0
            p->return_size = sizeof(uint64_t); /* Expected size */
949
0
            return 1;
950
0
        }
951
62
        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
62
        case sizeof(uint64_t):
961
62
            p->return_size = sizeof(uint64_t);
962
62
            *(uint64_t *)p->data = val;
963
62
            return 1;
964
62
        }
965
0
#endif
966
0
        return general_set_uint(p, &val, sizeof(val));
967
62
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
968
0
#ifndef OPENSSL_SMALL_FOOTPRINT
969
0
        if (p->data == NULL) {
970
0
            p->return_size = sizeof(int64_t); /* Expected size */
971
0
            return 1;
972
0
        }
973
0
        switch (p->data_size) {
974
0
        case sizeof(int32_t):
975
0
            if (val <= INT32_MAX) {
976
0
                p->return_size = sizeof(int32_t);
977
0
                *(int32_t *)p->data = (int32_t)val;
978
0
                return 1;
979
0
            }
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
0
        }
991
0
#endif
992
0
        return general_set_uint(p, &val, sizeof(val));
993
0
    } 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
62
}
1012
1013
OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
1014
0
{
1015
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1016
0
                                sizeof(uint64_t));
1017
0
}
1018
1019
int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
1020
0
{
1021
0
#ifndef OPENSSL_SMALL_FOOTPRINT
1022
0
    switch (sizeof(size_t)) {
1023
0
    case sizeof(uint32_t):
1024
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
1025
0
    case sizeof(uint64_t):
1026
0
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
1027
0
    }
1028
0
#endif
1029
0
    return general_get_uint(p, val, sizeof(*val));
1030
0
}
1031
1032
int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
1033
62
{
1034
62
#ifndef OPENSSL_SMALL_FOOTPRINT
1035
62
    switch (sizeof(size_t)) {
1036
0
    case sizeof(uint32_t):
1037
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
1038
62
    case sizeof(uint64_t):
1039
62
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
1040
62
    }
1041
0
#endif
1042
0
    return general_set_uint(p, &val, sizeof(val));
1043
62
}
1044
1045
OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1046
201
{
1047
201
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1048
201
                                sizeof(size_t));
1049
201
}
1050
1051
int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1052
0
{
1053
0
#ifndef OPENSSL_SMALL_FOOTPRINT
1054
0
    switch (sizeof(time_t)) {
1055
0
    case sizeof(int32_t):
1056
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
1057
0
    case sizeof(int64_t):
1058
0
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
1059
0
    }
1060
0
#endif
1061
0
    return general_get_int(p, val, sizeof(*val));
1062
0
}
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
0
{
1079
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1080
0
}
1081
1082
int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1083
0
{
1084
0
    BIGNUM *b = NULL;
1085
1086
0
    if (val == NULL || p == NULL || p->data == NULL) {
1087
0
        err_null_argument;
1088
0
        return 0;
1089
0
    }
1090
1091
0
    switch (p->data_type) {
1092
0
    case OSSL_PARAM_UNSIGNED_INTEGER:
1093
0
        b = BN_native2bn(p->data, (int)p->data_size, *val);
1094
0
        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
0
    }
1102
1103
0
    if (b == NULL) {
1104
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1105
0
        return 0;
1106
0
    }
1107
1108
0
    *val = b;
1109
0
    return 1;
1110
0
}
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
0
{
1333
0
    size_t sz, alloc_sz;
1334
1335
0
    if ((val == NULL && used_len == NULL) || p == NULL) {
1336
0
        err_null_argument;
1337
0
        return 0;
1338
0
    }
1339
0
    if (p->data_type != type) {
1340
0
        err_bad_type;
1341
0
        return 0;
1342
0
    }
1343
1344
0
    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
0
    alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1350
1351
0
    if (used_len != NULL)
1352
0
        *used_len = sz;
1353
1354
0
    if (p->data == NULL) {
1355
0
        err_null_argument;
1356
0
        return 0;
1357
0
    }
1358
1359
0
    if (val == NULL)
1360
0
        return 1;
1361
1362
0
    if (*val == NULL) {
1363
0
        char *const q = OPENSSL_malloc(alloc_sz);
1364
1365
0
        if (q == NULL)
1366
0
            return 0;
1367
0
        *val = q;
1368
0
        *max_len = alloc_sz;
1369
0
    }
1370
1371
0
    if (*max_len < sz) {
1372
0
        err_too_small;
1373
0
        return 0;
1374
0
    }
1375
0
    memcpy(*val, p->data, sz);
1376
0
    return 1;
1377
0
}
1378
1379
int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1380
0
{
1381
0
    int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1382
0
                                  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
0
    size_t data_length = p->data_size;
1396
1397
0
    if (ret == 0)
1398
0
        return 0;
1399
0
    if (data_length >= max_len)
1400
0
        data_length = OPENSSL_strnlen(p->data, data_length);
1401
0
    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
0
    (*val)[data_length] = '\0';
1406
1407
0
    return ret;
1408
0
}
1409
1410
int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1411
                                size_t *used_len)
1412
0
{
1413
0
    return get_string_internal(p, val, &max_len, used_len,
1414
0
                               OSSL_PARAM_OCTET_STRING);
1415
0
}
1416
1417
static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1418
                               unsigned int type)
1419
0
{
1420
0
    if (p->data_type != type) {
1421
0
        err_bad_type;
1422
0
        return 0;
1423
0
    }
1424
0
    p->return_size = len;
1425
0
    if (p->data == NULL)
1426
0
        return 1;
1427
0
    if (p->data_size < len) {
1428
0
        err_too_small;
1429
0
        return 0;
1430
0
    }
1431
1432
0
    memcpy(p->data, val, len);
1433
    /* If possible within the size of p->data, add a NUL terminator byte */
1434
0
    if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1435
0
        ((char *)p->data)[len] = '\0';
1436
0
    return 1;
1437
0
}
1438
1439
int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1440
0
{
1441
0
    if (p == NULL || val == NULL) {
1442
0
        err_null_argument;
1443
0
        return 0;
1444
0
    }
1445
0
    p->return_size = 0;
1446
0
    return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1447
0
}
1448
1449
int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1450
                                size_t len)
1451
0
{
1452
0
    if (p == NULL || val == NULL) {
1453
0
        err_null_argument;
1454
0
        return 0;
1455
0
    }
1456
0
    p->return_size = 0;
1457
0
    return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1458
0
}
1459
1460
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1461
                                            size_t bsize)
1462
0
{
1463
0
    if (buf != NULL && bsize == 0)
1464
0
        bsize = strlen(buf);
1465
0
    return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1466
0
}
1467
1468
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1469
                                             size_t bsize)
1470
0
{
1471
0
    return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1472
0
}
1473
1474
static int get_ptr_internal_skip_checks(const OSSL_PARAM *p, const void **val,
1475
                                        size_t *used_len)
1476
0
{
1477
0
    if (used_len != NULL)
1478
0
        *used_len = p->data_size;
1479
0
    *val = *(const void **)p->data;
1480
0
    return 1;
1481
0
}
1482
1483
static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1484
                            size_t *used_len, unsigned int type)
1485
0
{
1486
0
    if (val == NULL || p == NULL) {
1487
0
        err_null_argument;
1488
0
        return 0;
1489
0
    }
1490
0
    if (p->data_type != type) {
1491
0
        err_bad_type;
1492
0
        return 0;
1493
0
    }
1494
0
    return get_ptr_internal_skip_checks(p, val, used_len);
1495
0
}
1496
1497
int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1498
0
{
1499
0
    return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1500
0
}
1501
1502
int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1503
                             size_t *used_len)
1504
0
{
1505
0
    return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1506
0
}
1507
1508
static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1509
                            unsigned int type, size_t len)
1510
0
{
1511
0
    if (p->data_type != type) {
1512
0
        err_bad_type;
1513
0
        return 0;
1514
0
    }
1515
0
    p->return_size = len;
1516
0
    if (p->data != NULL)
1517
0
        *(const void **)p->data = val;
1518
0
    return 1;
1519
0
}
1520
1521
int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1522
0
{
1523
0
    if (p == NULL) {
1524
0
        err_null_argument;
1525
0
        return 0;
1526
0
    }
1527
0
    p->return_size = 0;
1528
0
    return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1529
0
                            val == NULL ? 0 : strlen(val));
1530
0
}
1531
1532
int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1533
                             size_t used_len)
1534
0
{
1535
0
    if (p == NULL) {
1536
0
        err_null_argument;
1537
0
        return 0;
1538
0
    }
1539
0
    p->return_size = 0;
1540
0
    return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1541
0
}
1542
1543
OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1544
                                         size_t bsize)
1545
0
{
1546
0
    return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1547
0
}
1548
1549
OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1550
                                          size_t bsize)
1551
0
{
1552
0
    return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1553
0
}
1554
1555
/*
1556
 * Extract the parameter into an allocated buffer.
1557
 * Any existing allocation in *out is cleared and freed.
1558
 *
1559
 * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1560
 *
1561
 * *out and *out_len are guaranteed to be untouched if this function
1562
 * doesn't return success.
1563
 */
1564
int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1565
                                 unsigned char **out, size_t *out_len)
1566
0
{
1567
0
    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1568
0
    void *buf = NULL;
1569
0
    size_t len = 0;
1570
1571
0
    if (p == NULL)
1572
0
        return -1;
1573
1574
0
    if (p->data != NULL
1575
0
            && p->data_size > 0
1576
0
            && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1577
0
        return 0;
1578
1579
0
    OPENSSL_clear_free(*out, *out_len);
1580
0
    *out = buf;
1581
0
    *out_len = len;
1582
0
    return 1;
1583
0
}
1584
1585
static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1586
                             unsigned char *out, size_t *outlen)
1587
0
{
1588
0
    int ret = 0;
1589
0
    WPACKET pkt;
1590
1591
0
    if (out == NULL) {
1592
0
        if (!WPACKET_init_null(&pkt, 0))
1593
0
            return 0;
1594
0
    } else {
1595
0
        if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1596
0
            return 0;
1597
0
    }
1598
1599
0
    for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1600
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
1601
0
            goto err;
1602
0
        if (p->data != NULL
1603
0
                && p->data_size != 0
1604
0
                && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1605
0
            goto err;
1606
0
    }
1607
0
    if (!WPACKET_get_total_written(&pkt, outlen)
1608
0
            || !WPACKET_finish(&pkt))
1609
0
        goto err;
1610
0
    ret = 1;
1611
0
err:
1612
0
    WPACKET_cleanup(&pkt);
1613
0
    return ret;
1614
0
}
1615
1616
int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
1617
                                        unsigned char **out,
1618
                                        size_t *out_len, size_t maxsize)
1619
0
{
1620
0
    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1621
0
    unsigned char *res;
1622
0
    size_t sz = 0;
1623
1624
0
    if (p == NULL)
1625
0
        return -1;
1626
1627
    /* Calculate the total size */
1628
0
    if (!setbuf_fromparams(p, name, NULL, &sz))
1629
0
        return 0;
1630
1631
    /* Check that it's not oversized */
1632
0
    if (maxsize > 0 && sz > maxsize)
1633
0
        return 0;
1634
1635
    /* Special case zero length */
1636
0
    if (sz == 0) {
1637
0
        if ((res = OPENSSL_zalloc(1)) == NULL)
1638
0
            return 0;
1639
0
        goto fin;
1640
0
    }
1641
1642
    /* Allocate the buffer */
1643
0
    res = OPENSSL_malloc(sz);
1644
0
    if (res == NULL)
1645
0
        return 0;
1646
1647
    /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1648
0
    if (!setbuf_fromparams(p, name, res, &sz)) {
1649
0
        OPENSSL_clear_free(res, sz);
1650
0
        return 0;
1651
0
    }
1652
1653
0
 fin:
1654
0
    OPENSSL_clear_free(*out, *out_len);
1655
0
    *out = res;
1656
0
    *out_len = sz;
1657
0
    return 1;
1658
0
}
1659
1660
OSSL_PARAM OSSL_PARAM_construct_end(void)
1661
31
{
1662
31
    OSSL_PARAM end = OSSL_PARAM_END;
1663
1664
31
    return end;
1665
31
}
1666
1667
static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1668
                                   size_t *used_len, unsigned int ref_type,
1669
                                   unsigned int type)
1670
0
{
1671
0
    if (val == NULL || p == NULL) {
1672
0
        err_null_argument;
1673
0
        return 0;
1674
0
    }
1675
1676
0
    if (p->data_type == ref_type)
1677
0
        return get_ptr_internal_skip_checks(p, (const void **)val, used_len);
1678
1679
0
    if (p->data_type != type) {
1680
0
        err_bad_type;
1681
0
        return 0;
1682
0
    }
1683
0
    if (used_len != NULL)
1684
0
        *used_len = p->data_size;
1685
0
    *val = p->data;
1686
0
    return 1;
1687
0
}
1688
1689
int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1690
0
{
1691
0
    return get_string_ptr_internal(p, (const void **)val, NULL,
1692
0
                                   OSSL_PARAM_UTF8_PTR,
1693
0
                                   OSSL_PARAM_UTF8_STRING);
1694
0
}
1695
1696
int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1697
                                    size_t *used_len)
1698
0
{
1699
0
    return get_string_ptr_internal(p, (const void **)val, used_len,
1700
0
                                   OSSL_PARAM_OCTET_PTR,
1701
0
                                   OSSL_PARAM_OCTET_STRING);
1702
0
}
1703
1704
int OSSL_PARAM_set_octet_string_or_ptr(OSSL_PARAM *p, const void *val,
1705
                                       size_t len)
1706
0
{
1707
0
    if (p == NULL) {
1708
0
        err_null_argument;
1709
0
        return 0;
1710
0
    }
1711
1712
0
    if (p->data_type == OSSL_PARAM_OCTET_STRING)
1713
0
        return OSSL_PARAM_set_octet_string(p, val, len);
1714
0
    else if (p->data_type == OSSL_PARAM_OCTET_PTR)
1715
0
        return OSSL_PARAM_set_octet_ptr(p, val, len);
1716
1717
0
    err_bad_type;
1718
0
    return 0;
1719
0
}