params.c 43 KB

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