params.c 43 KB

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