params.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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_MALLOC_FAILURE);
  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. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  1188. return 0;
  1189. }
  1190. *val = q;
  1191. *max_len = alloc_sz;
  1192. }
  1193. if (*max_len < sz) {
  1194. err_too_small;
  1195. return 0;
  1196. }
  1197. memcpy(*val, p->data, sz);
  1198. return 1;
  1199. }
  1200. int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
  1201. {
  1202. int ret = get_string_internal(p, (void **)val, &max_len, NULL,
  1203. OSSL_PARAM_UTF8_STRING);
  1204. /*
  1205. * We try to ensure that the copied string is terminated with a
  1206. * NUL byte. That should be easy, just place a NUL byte at
  1207. * |((char*)*val)[p->data_size]|.
  1208. * Unfortunately, we have seen cases where |p->data_size| doesn't
  1209. * correctly reflect the length of the string, and just happens
  1210. * to be out of bounds according to |max_len|, so in that case, we
  1211. * make the extra step of trying to find the true length of the
  1212. * string that |p->data| points at, and use that as an index to
  1213. * place the NUL byte in |*val|.
  1214. */
  1215. size_t data_length = p->data_size;
  1216. if (ret == 0)
  1217. return 0;
  1218. if (data_length >= max_len)
  1219. data_length = OPENSSL_strnlen(p->data, data_length);
  1220. if (data_length >= max_len) {
  1221. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
  1222. return 0; /* No space for a terminating NUL byte */
  1223. }
  1224. (*val)[data_length] = '\0';
  1225. return ret;
  1226. }
  1227. int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
  1228. size_t *used_len)
  1229. {
  1230. return get_string_internal(p, val, &max_len, used_len,
  1231. OSSL_PARAM_OCTET_STRING);
  1232. }
  1233. static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
  1234. unsigned int type)
  1235. {
  1236. p->return_size = len;
  1237. if (p->data == NULL)
  1238. return 1;
  1239. if (p->data_type != type) {
  1240. err_bad_type;
  1241. return 0;
  1242. }
  1243. if (p->data_size < len) {
  1244. err_too_small;
  1245. return 0;
  1246. }
  1247. memcpy(p->data, val, len);
  1248. /* If possible within the size of p->data, add a NUL terminator byte */
  1249. if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
  1250. ((char *)p->data)[len] = '\0';
  1251. return 1;
  1252. }
  1253. int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
  1254. {
  1255. if (p == NULL) {
  1256. err_null_argument;
  1257. return 0;
  1258. }
  1259. p->return_size = 0;
  1260. if (val == NULL) {
  1261. err_null_argument;
  1262. return 0;
  1263. }
  1264. return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
  1265. }
  1266. int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
  1267. size_t len)
  1268. {
  1269. if (p == NULL) {
  1270. err_null_argument;
  1271. return 0;
  1272. }
  1273. p->return_size = 0;
  1274. if (val == NULL) {
  1275. err_null_argument;
  1276. return 0;
  1277. }
  1278. return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
  1279. }
  1280. OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
  1281. size_t bsize)
  1282. {
  1283. if (buf != NULL && bsize == 0)
  1284. bsize = strlen(buf);
  1285. return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
  1286. }
  1287. OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
  1288. size_t bsize)
  1289. {
  1290. return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
  1291. }
  1292. static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
  1293. size_t *used_len, unsigned int type)
  1294. {
  1295. if (val == NULL || p == NULL) {
  1296. err_null_argument;
  1297. return 0;
  1298. }
  1299. if (p->data_type != type) {
  1300. err_bad_type;
  1301. return 0;
  1302. }
  1303. if (used_len != NULL)
  1304. *used_len = p->data_size;
  1305. *val = *(const void **)p->data;
  1306. return 1;
  1307. }
  1308. int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
  1309. {
  1310. return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
  1311. }
  1312. int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
  1313. size_t *used_len)
  1314. {
  1315. return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
  1316. }
  1317. static int set_ptr_internal(OSSL_PARAM *p, const void *val,
  1318. unsigned int type, size_t len)
  1319. {
  1320. p->return_size = len;
  1321. if (p->data_type != type) {
  1322. err_bad_type;
  1323. return 0;
  1324. }
  1325. if (p->data != NULL)
  1326. *(const void **)p->data = val;
  1327. return 1;
  1328. }
  1329. int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
  1330. {
  1331. if (p == NULL) {
  1332. err_null_argument;
  1333. return 0;
  1334. }
  1335. p->return_size = 0;
  1336. return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
  1337. val == NULL ? 0 : strlen(val));
  1338. }
  1339. int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
  1340. size_t used_len)
  1341. {
  1342. if (p == NULL) {
  1343. err_null_argument;
  1344. return 0;
  1345. }
  1346. p->return_size = 0;
  1347. return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
  1348. }
  1349. OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
  1350. size_t bsize)
  1351. {
  1352. return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
  1353. }
  1354. OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  1355. size_t bsize)
  1356. {
  1357. return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
  1358. }
  1359. OSSL_PARAM OSSL_PARAM_construct_end(void)
  1360. {
  1361. OSSL_PARAM end = OSSL_PARAM_END;
  1362. return end;
  1363. }
  1364. static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
  1365. size_t *used_len, unsigned int type)
  1366. {
  1367. if (val == NULL || p == NULL) {
  1368. err_null_argument;
  1369. return 0;
  1370. }
  1371. if (p->data_type != type) {
  1372. err_bad_type;
  1373. return 0;
  1374. }
  1375. if (used_len != NULL)
  1376. *used_len = p->data_size;
  1377. *val = p->data;
  1378. return 1;
  1379. }
  1380. int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
  1381. {
  1382. int rv;
  1383. ERR_set_mark();
  1384. rv = OSSL_PARAM_get_utf8_ptr(p, val);
  1385. ERR_pop_to_mark();
  1386. return rv || get_string_ptr_internal(p, (const void **)val, NULL,
  1387. OSSL_PARAM_UTF8_STRING);
  1388. }
  1389. int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
  1390. size_t *used_len)
  1391. {
  1392. int rv;
  1393. ERR_set_mark();
  1394. rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
  1395. ERR_pop_to_mark();
  1396. return rv || get_string_ptr_internal(p, val, used_len,
  1397. OSSL_PARAM_OCTET_STRING);
  1398. }