12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295 |
- /*
- * Copyright 2019-2021 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"
- #include "internal/endian.h"
- /*
- * Return the number of bits in the mantissa of a double. This is used to
- * shift a larger integral value to determine if it will exactly fit into a
- * double.
- */
- static unsigned int real_shift(void)
- {
- return sizeof(double) == 4 ? 24 : 53;
- }
- 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;
- }
- /* Return non-zero if the signed number is negative */
- static int is_negative(const void *number, size_t s)
- {
- const unsigned char *n = number;
- DECLARE_IS_ENDIAN;
- return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
- }
- /* Check that all the bytes specified match the expected sign byte */
- static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
- {
- size_t i;
- for (i = 0; i < n; i++)
- if (p[i] != s)
- return 0;
- return 1;
- }
- /*
- * Copy an integer to another integer.
- * Handle different length integers and signed and unsigned integers.
- * Both integers are in native byte ordering.
- */
- static int copy_integer(unsigned char *dest, size_t dest_len,
- const unsigned char *src, size_t src_len,
- unsigned char pad, int signed_int)
- {
- size_t n;
- DECLARE_IS_ENDIAN;
- if (IS_BIG_ENDIAN) {
- if (src_len < dest_len) {
- n = dest_len - src_len;
- memset(dest, pad, n);
- memcpy(dest + n, src, src_len);
- } else {
- n = src_len - dest_len;
- if (!check_sign_bytes(src, n, pad)
- /*
- * Shortening a signed value must retain the correct sign.
- * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
- */
- || (signed_int && ((pad ^ src[n]) & 0x80) != 0))
- return 0;
- memcpy(dest, src + n, dest_len);
- }
- } else /* IS_LITTLE_ENDIAN */ {
- if (src_len < dest_len) {
- n = dest_len - src_len;
- memset(dest + src_len, pad, n);
- memcpy(dest, src, src_len);
- } else {
- n = src_len - dest_len;
- if (!check_sign_bytes(src + dest_len, n, pad)
- /*
- * Shortening a signed value must retain the correct sign.
- * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
- */
- || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0))
- return 0;
- memcpy(dest, src, dest_len);
- }
- }
- return 1;
- }
- /* Copy a signed number to a signed number of possibly different length */
- static int signed_from_signed(void *dest, size_t dest_len,
- const void *src, size_t src_len)
- {
- return copy_integer(dest, dest_len, src, src_len,
- is_negative(src, src_len) ? 0xff : 0, 1);
- }
- /* Copy an unsigned number to a signed number of possibly different length */
- static int signed_from_unsigned(void *dest, size_t dest_len,
- const void *src, size_t src_len)
- {
- return copy_integer(dest, dest_len, src, src_len, 0, 1);
- }
- /* Copy a signed number to an unsigned number of possibly different length */
- static int unsigned_from_signed(void *dest, size_t dest_len,
- const void *src, size_t src_len)
- {
- if (is_negative(src, src_len))
- return 0;
- return copy_integer(dest, dest_len, src, src_len, 0, 0);
- }
- /* Copy an unsigned number to an unsigned number of possibly different length */
- static int unsigned_from_unsigned(void *dest, size_t dest_len,
- const void *src, size_t src_len)
- {
- return copy_integer(dest, dest_len, src, src_len, 0, 0);
- }
- /* General purpose get integer parameter call that handles odd sizes */
- static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
- {
- if (p->data_type == OSSL_PARAM_INTEGER)
- return signed_from_signed(val, val_size, p->data, p->data_size);
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
- return signed_from_unsigned(val, val_size, p->data, p->data_size);
- return 0;
- }
- /* General purpose set integer parameter call that handles odd sizes */
- static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
- {
- int r = 0;
- p->return_size = val_size; /* Expected size */
- if (p->data == NULL)
- return 1;
- if (p->data_type == OSSL_PARAM_INTEGER)
- r = signed_from_signed(p->data, p->data_size, val, val_size);
- else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
- r = unsigned_from_signed(p->data, p->data_size, val, val_size);
- p->return_size = r ? p->data_size : val_size;
- return r;
- }
- /* General purpose get unsigned integer parameter call that handles odd sizes */
- static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
- {
- if (p->data_type == OSSL_PARAM_INTEGER)
- return unsigned_from_signed(val, val_size, p->data, p->data_size);
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
- return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
- return 0;
- }
- /* General purpose set unsigned integer parameter call that handles odd sizes */
- static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
- {
- int r = 0;
- p->return_size = val_size; /* Expected size */
- if (p->data == NULL)
- return 1;
- if (p->data_type == OSSL_PARAM_INTEGER)
- r = signed_from_unsigned(p->data, p->data_size, val, val_size);
- else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
- r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
- p->return_size = r ? p->data_size : val_size;
- return r;
- }
- int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- }
- 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)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- }
- 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)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- }
- 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)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- }
- 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)
- {
- double d;
- if (val == NULL || p == NULL )
- return 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- int64_t i64;
- 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;
- }
- return 0;
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- uint32_t u32;
- uint64_t u64;
- switch (p->data_size) {
- case sizeof(uint32_t):
- u32 = *(const uint32_t *)p->data;
- if (u32 <= INT32_MAX) {
- *val = (int32_t)u32;
- return 1;
- }
- return 0;
- case sizeof(uint64_t):
- u64 = *(const uint64_t *)p->data;
- if (u64 <= INT32_MAX) {
- *val = (int32_t)u64;
- return 1;
- }
- return 0;
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- } 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) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- } 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)
- {
- double d;
- if (val == NULL || p == NULL)
- return 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- uint64_t u64;
- 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;
- }
- return 0;
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- int32_t i32;
- int64_t i64;
- switch (p->data_size) {
- case sizeof(int32_t):
- i32 = *(const int32_t *)p->data;
- if (i32 >= 0) {
- *val = i32;
- return 1;
- }
- return 0;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- if (i64 >= 0 && i64 <= UINT32_MAX) {
- *val = (uint32_t)i64;
- return 1;
- }
- return 0;
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- } 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) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- return 0;
- case sizeof(int64_t):
- p->return_size = sizeof(int64_t);
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- } 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)
- {
- double d;
- if (val == NULL || p == NULL )
- return 0;
- if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- uint64_t u64;
- 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;
- }
- return 0;
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= INT64_MIN
- /*
- * By subtracting 65535 (2^16-1) we cancel the low order
- * 15 bits of INT64_MAX to avoid using imprecise floating
- * point values.
- */
- && d < (double)(INT64_MAX - 65535) + 65536.0
- && 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) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- return 0;
- case sizeof(int64_t):
- *(int64_t *)p->data = val;
- return 1;
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- return 0;
- case sizeof(uint64_t):
- *(uint64_t *)p->data = (uint64_t)val;
- return 1;
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- } 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 >> real_shift()) == 0) {
- *(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)
- {
- double d;
- if (val == NULL || p == NULL)
- return 0;
- if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- int32_t i32;
- int64_t i64;
- switch (p->data_size) {
- case sizeof(int32_t):
- i32 = *(const int32_t *)p->data;
- if (i32 >= 0) {
- *val = (uint64_t)i32;
- return 1;
- }
- return 0;
- case sizeof(int64_t):
- i64 = *(const int64_t *)p->data;
- if (i64 >= 0) {
- *val = (uint64_t)i64;
- return 1;
- }
- return 0;
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- } else if (p->data_type == OSSL_PARAM_REAL) {
- switch (p->data_size) {
- case sizeof(double):
- d = *(const double *)p->data;
- if (d >= 0
- /*
- * By subtracting 65535 (2^16-1) we cancel the low order
- * 15 bits of UINT64_MAX to avoid using imprecise floating
- * point values.
- */
- && d < (double)(UINT64_MAX - 65535) + 65536.0
- && 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) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- return 0;
- case sizeof(uint64_t):
- *(uint64_t *)p->data = val;
- return 1;
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- } else if (p->data_type == OSSL_PARAM_INTEGER) {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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;
- }
- return 0;
- case sizeof(int64_t):
- if (val <= INT64_MAX) {
- *(int64_t *)p->data = (int64_t)val;
- return 1;
- }
- return 0;
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- } else if (p->data_type == OSSL_PARAM_REAL) {
- p->return_size = sizeof(double);
- switch (p->data_size) {
- case sizeof(double):
- if ((val >> real_shift()) == 0) {
- *(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)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_uint(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_uint(p, &val, sizeof(val));
- }
- 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)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_get_int(p, val, sizeof(*val));
- }
- int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
- {
- #ifndef OPENSSL_SMALL_FOOTPRINT
- 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);
- }
- #endif
- return general_set_int(p, &val, sizeof(val));
- }
- 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 >> real_shift()) == 0) {
- *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 >> real_shift()) == 0) {
- *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
- /*
- * By subtracting 65535 (2^16-1) we cancel the low order
- * 15 bits of UINT64_MAX to avoid using imprecise floating
- * point values.
- */
- && (double)(UINT64_MAX - 65535) + 65536.0) {
- 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
- /*
- * By subtracting 65535 (2^16-1) we cancel the low order
- * 15 bits of INT64_MAX to avoid using imprecise floating
- * point values.
- */
- && val < (double)(INT64_MAX - 65535) + 65536.0) {
- 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, alloc_sz;
- if ((val == NULL && used_len == NULL) || p == NULL || p->data_type != type)
- return 0;
- sz = p->data_size;
- /*
- * If the input size is 0, or the input string needs NUL byte
- * termination, allocate an extra byte.
- */
- alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
- 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(alloc_sz);
- if (q == NULL)
- return 0;
- *val = q;
- *max_len = alloc_sz;
- }
- 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)
- {
- int ret = get_string_internal(p, (void **)val, &max_len, NULL,
- OSSL_PARAM_UTF8_STRING);
- /*
- * We try to ensure that the copied string is terminated with a
- * NUL byte. That should be easy, just place a NUL byte at
- * |((char*)*val)[p->data_size]|.
- * Unfortunately, we have seen cases where |p->data_size| doesn't
- * correctly reflect the length of the string, and just happens
- * to be out of bounds according to |max_len|, so in that case, we
- * make the extra step of trying to find the true length of the
- * string that |p->data| points at, and use that as an index to
- * place the NUL byte in |*val|.
- */
- size_t data_length = p->data_size;
- if (ret == 0)
- return 0;
- if (data_length >= max_len)
- data_length = OPENSSL_strnlen(p->data, data_length);
- if (data_length >= max_len)
- return 0; /* No space for a terminating NUL byte */
- (*val)[data_length] = '\0';
- return ret;
- }
- 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);
- /* If possible within the size of p->data, add a NUL terminator byte */
- if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
- ((char *)p->data)[len] = '\0';
- 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), 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);
- 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));
- }
- 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;
- }
- static int get_string_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 = p->data;
- return 1;
- }
- int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
- {
- return OSSL_PARAM_get_utf8_ptr(p, val)
- || get_string_ptr_internal(p, (const void **)val, NULL,
- OSSL_PARAM_UTF8_STRING);
- }
- int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
- size_t *used_len)
- {
- return OSSL_PARAM_get_octet_ptr(p, val, used_len)
- || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING);
- }
|