params.c 47 KB

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