123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- /*
- * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
- *
- * Licensed under the Apache License 2.0 (the "License"). You may not use
- * this file except in compliance with the License. You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
- #include <string.h>
- #include <openssl/params.h>
- #include "internal/thread_once.h"
- #include "internal/numbers.h"
- OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
- {
- if (p != NULL && key != NULL)
- for (; p->key != NULL; p++)
- if (strcmp(key, p->key) == 0)
- return p;
- return NULL;
- }
- const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
- {
- return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
- }
- static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
- void *data, size_t data_size)
- {
- OSSL_PARAM res;
- res.key = key;
- res.data_type = data_type;
- res.data = data;
- res.data_size = data_size;
- res.return_size = OSSL_PARAM_UNMODIFIED;
- return res;
- }
- int OSSL_PARAM_modified(const OSSL_PARAM *p)
- {
- return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
- }
- void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
- {
- if (p != NULL)
- while (p->key != NULL)
- p++->return_size = OSSL_PARAM_UNMODIFIED;
- }
- int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
- {
- switch (sizeof(int)) {
- case sizeof(int32_t):
- return OSSL_PARAM_get_int32(p, (int32_t *)val);
- case sizeof(int64_t):
- return OSSL_PARAM_get_int64(p, (int64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
- {
- switch (sizeof(int)) {
- case sizeof(int32_t):
- return OSSL_PARAM_set_int32(p, (int32_t)val);
- case sizeof(int64_t):
- return OSSL_PARAM_set_int64(p, (int64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
- }
- int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
- {
- switch (sizeof(unsigned int)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
- {
- switch (sizeof(unsigned int)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_set_uint32(p, (uint32_t)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_set_uint64(p, (uint64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
- sizeof(unsigned int));
- }
- int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
- {
- switch (sizeof(long int)) {
- case sizeof(int32_t):
- return OSSL_PARAM_get_int32(p, (int32_t *)val);
- case sizeof(int64_t):
- return OSSL_PARAM_get_int64(p, (int64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
- {
- switch (sizeof(long int)) {
- case sizeof(int32_t):
- return OSSL_PARAM_set_int32(p, (int32_t)val);
- case sizeof(int64_t):
- return OSSL_PARAM_set_int64(p, (int64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
- }
- int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
- {
- switch (sizeof(unsigned long int)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
- {
- switch (sizeof(unsigned long int)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_set_uint32(p, (uint32_t)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_set_uint64(p, (uint64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
- sizeof(unsigned long int));
- }
- int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
- {
- int64_t i64;
- uint32_t u32;
- uint64_t u64;
- double d;
- if (val == NULL || p == NULL )
- return 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- switch (p->data_size) {
- case sizeof(int32_t):
- *val = *(const int32_t *)p->data;
- return 1;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
- *val = (int32_t)i64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- switch (p->data_size) {
- case sizeof(uint32_t):
- u32 = *(const uint32_t *)p->data;
- if (u32 <= INT32_MAX) {
- *val = (int32_t)u32;
- return 1;
- }
- break;
- case sizeof(uint64_t):
- u64 = *(const uint64_t *)p->data;
- if (u64 <= INT32_MAX) {
- *val = (int32_t)u64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
- *val = (int32_t)d;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- p->return_size = sizeof(int32_t); /* Minimum expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(int32_t):
- *(int32_t *)p->data = val;
- return 1;
- case sizeof(int64_t):
- p->return_size = sizeof(int64_t);
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
- p->return_size = sizeof(uint32_t); /* Minimum expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(uint32_t):
- *(uint32_t *)p->data = (uint32_t)val;
- return 1;
- case sizeof(uint64_t):
- p->return_size = sizeof(uint64_t);
- *(uint64_t *)p->data = (uint64_t)val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(double):
- *(double *)p->data = (double)val;
- return 1;
- }
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
- sizeof(int32_t));
- }
- int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
- {
- int32_t i32;
- int64_t i64;
- uint64_t u64;
- double d;
- if (val == NULL || p == NULL)
- return 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- switch (p->data_size) {
- case sizeof(uint32_t):
- *val = *(const uint32_t *)p->data;
- return 1;
- case sizeof(uint64_t):
- u64 = *(const uint64_t *)p->data;
- if (u64 <= UINT32_MAX) {
- *val = (uint32_t)u64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- switch (p->data_size) {
- case sizeof(int32_t):
- i32 = *(const int32_t *)p->data;
- if (i32 >= 0) {
- *val = i32;
- return 1;
- }
- break;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- if (i64 >= 0 && i64 <= UINT32_MAX) {
- *val = (uint32_t)i64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
- *val = (uint32_t)d;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- p->return_size = sizeof(uint32_t); /* Minimum expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(uint32_t):
- *(uint32_t *)p->data = val;
- return 1;
- case sizeof(uint64_t):
- p->return_size = sizeof(uint64_t);
- *(uint64_t *)p->data = val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- p->return_size = sizeof(int32_t); /* Minimum expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(int32_t):
- if (val <= INT32_MAX) {
- *(int32_t *)p->data = (int32_t)val;
- return 1;
- }
- break;
- case sizeof(int64_t):
- p->return_size = sizeof(int64_t);
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(double):
- *(double *)p->data = (double)val;
- return 1;
- }
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
- sizeof(uint32_t));
- }
- int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
- {
- uint64_t u64;
- double d;
- if (val == NULL || p == NULL )
- return 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- switch (p->data_size) {
- case sizeof(int32_t):
- *val = *(const int32_t *)p->data;
- return 1;
- case sizeof(int64_t):
- *val = *(const int64_t *)p->data;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- switch (p->data_size) {
- case sizeof(uint32_t):
- *val = *(const uint32_t *)p->data;
- return 1;
- case sizeof(uint64_t):
- u64 = *(const uint64_t *)p->data;
- if (u64 <= INT64_MAX) {
- *val = (int64_t)u64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d) {
- *val = (int64_t)d;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
- {
- uint64_t u64;
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- p->return_size = sizeof(int64_t); /* Expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(int32_t):
- if (val >= INT32_MIN && val <= INT32_MAX) {
- p->return_size = sizeof(int32_t);
- *(int32_t *)p->data = (int32_t)val;
- return 1;
- }
- break;
- case sizeof(int64_t):
- *(int64_t *)p->data = val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
- p->return_size = sizeof(uint64_t); /* Expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(uint32_t):
- if (val <= UINT32_MAX) {
- p->return_size = sizeof(uint32_t);
- *(uint32_t *)p->data = (uint32_t)val;
- return 1;
- }
- break;
- case sizeof(uint64_t):
- *(uint64_t *)p->data = (uint64_t)val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(double):
- u64 = val < 0 ? -val : val;
- if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
- *(double *)p->data = (double)val;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
- }
- int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
- {
- int32_t i32;
- int64_t i64;
- double d;
- if (val == NULL || p == NULL)
- return 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- switch (p->data_size) {
- case sizeof(uint32_t):
- *val = *(const uint32_t *)p->data;
- return 1;
- case sizeof(uint64_t):
- *val = *(const uint64_t *)p->data;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- switch (p->data_size) {
- case sizeof(int32_t):
- i32 = *(const int32_t *)p->data;
- if (i32 >= 0) {
- *val = (uint64_t)i32;
- return 1;
- }
- break;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- if (i64 >= 0) {
- *val = (uint64_t)i64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= 0 && d <= INT64_MAX && d == (uint64_t)d) {
- *val = (uint64_t)d;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- p->return_size = sizeof(uint64_t); /* Expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(uint32_t):
- if (val <= UINT32_MAX) {
- p->return_size = sizeof(uint32_t);
- *(uint32_t *)p->data = (uint32_t)val;
- return 1;
- }
- break;
- case sizeof(uint64_t):
- *(uint64_t *)p->data = val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- p->return_size = sizeof(int64_t); /* Expected size */
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(int32_t):
- if (val <= INT32_MAX) {
- p->return_size = sizeof(int32_t);
- *(int32_t *)p->data = (int32_t)val;
- return 1;
- }
- break;
- case sizeof(int64_t):
- if (val <= INT64_MAX) {
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- switch (p->data_size) {
- case sizeof(double):
- if ((val >> 53) == 0) { /* 53 significant bits in the mantissa */
- *(double *)p->data = (double)val;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
- sizeof(uint64_t));
- }
- int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
- {
- switch (sizeof(size_t)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
- {
- switch (sizeof(size_t)) {
- case sizeof(uint32_t):
- return OSSL_PARAM_set_uint32(p, (uint32_t)val);
- case sizeof(uint64_t):
- return OSSL_PARAM_set_uint64(p, (uint64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
- sizeof(size_t));
- }
- int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
- {
- switch (sizeof(time_t)) {
- case sizeof(int32_t):
- return OSSL_PARAM_get_int32(p, (int32_t *)val);
- case sizeof(int64_t):
- return OSSL_PARAM_get_int64(p, (int64_t *)val);
- }
- return 0;
- }
- int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
- {
- switch (sizeof(time_t)) {
- case sizeof(int32_t):
- return OSSL_PARAM_set_int32(p, (int32_t)val);
- case sizeof(int64_t):
- return OSSL_PARAM_set_int64(p, (int64_t)val);
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
- }
- int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
- {
- BIGNUM *b;
- if (val == NULL
- || p == NULL
- || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
- return 0;
- b = BN_native2bn(p->data, (int)p->data_size, *val);
- if (b != NULL) {
- *val = b;
- return 1;
- }
- return 0;
- }
- int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
- {
- size_t bytes;
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
- return 0;
- /* For the moment, only positive values are permitted */
- if (BN_is_negative(val))
- return 0;
- bytes = (size_t)BN_num_bytes(val);
- p->return_size = bytes;
- if (p->data == NULL)
- return 1;
- if (p->data_size >= bytes) {
- p->return_size = p->data_size;
- return BN_bn2nativepad(val, p->data, p->data_size) >= 0;
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
- size_t bsize)
- {
- return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
- buf, bsize);
- }
- int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
- {
- int64_t i64;
- uint64_t u64;
- if (val == NULL || p == NULL)
- return 0;
- if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- *val = *(const double *)p->data;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- switch (p->data_size) {
- case sizeof(uint32_t):
- *val = *(const uint32_t *)p->data;
- return 1;
- case sizeof(uint64_t):
- u64 = *(const uint64_t *)p->data;
- if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
- *val = (double)u64;
- return 1;
- }
- break;
- }
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- switch (p->data_size) {
- case sizeof(int32_t):
- *val = *(const int32_t *)p->data;
- return 1;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- u64 = i64 < 0 ? -i64 : i64;
- if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
- *val = 0.0 + i64;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(double):
- *(double *)p->data = val;
- return 1;
- }
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
- && val == (ossl_uintmax_t)val) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(uint32_t):
- if (val >= 0 && val <= UINT32_MAX) {
- p->return_size = sizeof(uint32_t);
- *(uint32_t *)p->data = (uint32_t)val;
- return 1;
- }
- break;
- case sizeof(uint64_t):
- if (val >= 0 && val <= UINT64_MAX) {
- p->return_size = sizeof(uint64_t);
- *(uint64_t *)p->data = (uint64_t)val;
- return 1;
- }
- break; }
- } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
- p->return_size = sizeof(double);
- if (p->data == NULL)
- return 1;
- switch (p->data_size) {
- case sizeof(int32_t):
- if (val >= INT32_MIN && val <= INT32_MAX) {
- p->return_size = sizeof(int32_t);
- *(int32_t *)p->data = (int32_t)val;
- return 1;
- }
- break;
- case sizeof(int64_t):
- if (val >= INT64_MIN && val <= INT64_MAX) {
- p->return_size = sizeof(int64_t);
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- break;
- }
- }
- return 0;
- }
- OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
- {
- return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
- }
- static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len,
- size_t *used_len, unsigned int type)
- {
- size_t sz;
- if ((val == NULL && used_len == NULL) || p == NULL || p->data_type != type)
- return 0;
- sz = p->data_size;
- if (used_len != NULL)
- *used_len = sz;
- if (p->data == NULL)
- return 0;
- if (val == NULL)
- return 1;
- if (*val == NULL) {
- char *const q = OPENSSL_malloc(sz > 0 ? sz : 1);
- if (q == NULL)
- return 0;
- *val = q;
- if (sz != 0)
- memcpy(q, p->data, sz);
- return 1;
- }
- if (max_len < sz)
- return 0;
- memcpy(*val, p->data, sz);
- return 1;
- }
- int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
- {
- return get_string_internal(p, (void **)val, max_len, NULL,
- OSSL_PARAM_UTF8_STRING);
- }
- int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
- size_t *used_len)
- {
- return get_string_internal(p, val, max_len, used_len,
- OSSL_PARAM_OCTET_STRING);
- }
- static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
- unsigned int type)
- {
- p->return_size = len;
- if (p->data == NULL)
- return 1;
- if (p->data_type != type || p->data_size < len)
- return 0;
- memcpy(p->data, val, len);
- return 1;
- }
- int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (val == NULL)
- return 0;
- return set_string_internal(p, val, strlen(val) + 1, OSSL_PARAM_UTF8_STRING);
- }
- int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
- size_t len)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- if (val == NULL)
- return 0;
- return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
- }
- OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
- size_t bsize)
- {
- if (buf != NULL && bsize == 0)
- bsize = strlen(buf) + 1;
- return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
- }
- OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
- size_t bsize)
- {
- return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
- }
- static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
- size_t *used_len, unsigned int type)
- {
- if (val == NULL || p == NULL || p->data_type != type)
- return 0;
- if (used_len != NULL)
- *used_len = p->data_size;
- *val = *(const void **)p->data;
- return 1;
- }
- int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
- {
- return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
- }
- int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
- size_t *used_len)
- {
- return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
- }
- static int set_ptr_internal(OSSL_PARAM *p, const void *val,
- unsigned int type, size_t len)
- {
- p->return_size = len;
- if (p->data_type != type)
- return 0;
- if (p->data != NULL)
- *(const void **)p->data = val;
- return 1;
- }
- int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
- val == NULL ? 0 : strlen(val) + 1);
- }
- int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
- size_t used_len)
- {
- if (p == NULL)
- return 0;
- p->return_size = 0;
- return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
- }
- OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
- size_t bsize)
- {
- return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
- }
- OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
- size_t bsize)
- {
- return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
- }
- OSSL_PARAM OSSL_PARAM_construct_end(void)
- {
- OSSL_PARAM end = OSSL_PARAM_END;
- return end;
- }
|