params.c 46 KB

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