params.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * Copyright 2019 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 "internal/thread_once.h"
  13. #include "internal/numbers.h"
  14. OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
  15. {
  16. if (p != NULL && key != NULL)
  17. for (; p->key != NULL; p++)
  18. if (strcmp(key, p->key) == 0)
  19. return p;
  20. return NULL;
  21. }
  22. const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
  23. {
  24. return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
  25. }
  26. static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
  27. void *data, size_t data_size)
  28. {
  29. OSSL_PARAM res;
  30. res.key = key;
  31. res.data_type = data_type;
  32. res.data = data;
  33. res.data_size = data_size;
  34. res.return_size = 0;
  35. return res;
  36. }
  37. int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
  38. {
  39. switch (sizeof(int)) {
  40. case sizeof(int32_t):
  41. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  42. case sizeof(int64_t):
  43. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  44. }
  45. return 0;
  46. }
  47. int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
  48. {
  49. switch (sizeof(int)) {
  50. case sizeof(int32_t):
  51. return OSSL_PARAM_set_int32(p, (int32_t)val);
  52. case sizeof(int64_t):
  53. return OSSL_PARAM_set_int64(p, (int64_t)val);
  54. }
  55. return 0;
  56. }
  57. OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
  58. {
  59. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
  60. }
  61. int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
  62. {
  63. switch (sizeof(unsigned int)) {
  64. case sizeof(uint32_t):
  65. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  66. case sizeof(uint64_t):
  67. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  68. }
  69. return 0;
  70. }
  71. int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
  72. {
  73. switch (sizeof(unsigned int)) {
  74. case sizeof(uint32_t):
  75. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  76. case sizeof(uint64_t):
  77. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  78. }
  79. return 0;
  80. }
  81. OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
  82. {
  83. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  84. sizeof(unsigned int));
  85. }
  86. int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
  87. {
  88. switch (sizeof(long int)) {
  89. case sizeof(int32_t):
  90. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  91. case sizeof(int64_t):
  92. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  93. }
  94. return 0;
  95. }
  96. int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
  97. {
  98. switch (sizeof(long int)) {
  99. case sizeof(int32_t):
  100. return OSSL_PARAM_set_int32(p, (int32_t)val);
  101. case sizeof(int64_t):
  102. return OSSL_PARAM_set_int64(p, (int64_t)val);
  103. }
  104. return 0;
  105. }
  106. OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
  107. {
  108. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
  109. }
  110. int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
  111. {
  112. switch (sizeof(unsigned long int)) {
  113. case sizeof(uint32_t):
  114. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  115. case sizeof(uint64_t):
  116. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  117. }
  118. return 0;
  119. }
  120. int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
  121. {
  122. switch (sizeof(unsigned long int)) {
  123. case sizeof(uint32_t):
  124. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  125. case sizeof(uint64_t):
  126. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  127. }
  128. return 0;
  129. }
  130. OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
  131. {
  132. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  133. sizeof(unsigned long int));
  134. }
  135. int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
  136. {
  137. int64_t i64;
  138. uint32_t u32;
  139. uint64_t u64;
  140. double d;
  141. if (val == NULL || p == NULL )
  142. return 0;
  143. if (p->data_type == OSSL_PARAM_INTEGER) {
  144. switch (p->data_size) {
  145. case sizeof(int32_t):
  146. *val = *(const int32_t *)p->data;
  147. return 1;
  148. case sizeof(int64_t):
  149. i64 = *(const int64_t *)p->data;
  150. if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
  151. *val = (int32_t)i64;
  152. return 1;
  153. }
  154. break;
  155. }
  156. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  157. switch (p->data_size) {
  158. case sizeof(uint32_t):
  159. u32 = *(const uint32_t *)p->data;
  160. if (u32 <= INT32_MAX) {
  161. *val = (int32_t)u32;
  162. return 1;
  163. }
  164. break;
  165. case sizeof(uint64_t):
  166. u64 = *(const uint64_t *)p->data;
  167. if (u64 <= INT32_MAX) {
  168. *val = (int32_t)u64;
  169. return 1;
  170. }
  171. break;
  172. }
  173. } else if (p->data_type == OSSL_PARAM_REAL) {
  174. switch (p->data_size) {
  175. case sizeof(double):
  176. d = *(const double *)p->data;
  177. if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
  178. *val = (int32_t)d;
  179. return 1;
  180. }
  181. break;
  182. }
  183. }
  184. return 0;
  185. }
  186. int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
  187. {
  188. if (p == NULL)
  189. return 0;
  190. p->return_size = 0;
  191. if (p->data_type == OSSL_PARAM_INTEGER) {
  192. p->return_size = sizeof(int32_t); /* Minimum expected size */
  193. if (p->data == NULL)
  194. return 1;
  195. switch (p->data_size) {
  196. case sizeof(int32_t):
  197. *(int32_t *)p->data = val;
  198. return 1;
  199. case sizeof(int64_t):
  200. p->return_size = sizeof(int64_t);
  201. *(int64_t *)p->data = (int64_t)val;
  202. return 1;
  203. }
  204. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  205. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  206. if (p->data == NULL)
  207. return 1;
  208. switch (p->data_size) {
  209. case sizeof(uint32_t):
  210. *(uint32_t *)p->data = (uint32_t)val;
  211. return 1;
  212. case sizeof(uint64_t):
  213. p->return_size = sizeof(uint64_t);
  214. *(uint64_t *)p->data = (uint64_t)val;
  215. return 1;
  216. }
  217. } else if (p->data_type == OSSL_PARAM_REAL) {
  218. p->return_size = sizeof(double);
  219. if (p->data == NULL)
  220. return 1;
  221. switch (p->data_size) {
  222. case sizeof(double):
  223. *(double *)p->data = (double)val;
  224. return 1;
  225. }
  226. }
  227. return 0;
  228. }
  229. OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
  230. {
  231. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
  232. sizeof(int32_t));
  233. }
  234. int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
  235. {
  236. int32_t i32;
  237. int64_t i64;
  238. uint64_t u64;
  239. double d;
  240. if (val == NULL || p == NULL)
  241. return 0;
  242. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  243. switch (p->data_size) {
  244. case sizeof(uint32_t):
  245. *val = *(const uint32_t *)p->data;
  246. return 1;
  247. case sizeof(uint64_t):
  248. u64 = *(const uint64_t *)p->data;
  249. if (u64 <= UINT32_MAX) {
  250. *val = (uint32_t)u64;
  251. return 1;
  252. }
  253. break;
  254. }
  255. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  256. switch (p->data_size) {
  257. case sizeof(int32_t):
  258. i32 = *(const int32_t *)p->data;
  259. if (i32 >= 0) {
  260. *val = i32;
  261. return 1;
  262. }
  263. break;
  264. case sizeof(int64_t):
  265. i64 = *(const int64_t *)p->data;
  266. if (i64 >= 0 && i64 <= UINT32_MAX) {
  267. *val = (uint32_t)i64;
  268. return 1;
  269. }
  270. break;
  271. }
  272. } else if (p->data_type == OSSL_PARAM_REAL) {
  273. switch (p->data_size) {
  274. case sizeof(double):
  275. d = *(const double *)p->data;
  276. if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
  277. *val = (uint32_t)d;
  278. return 1;
  279. }
  280. break;
  281. }
  282. }
  283. return 0;
  284. }
  285. int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
  286. {
  287. if (p == NULL)
  288. return 0;
  289. p->return_size = 0;
  290. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  291. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  292. if (p->data == NULL)
  293. return 1;
  294. switch (p->data_size) {
  295. case sizeof(uint32_t):
  296. *(uint32_t *)p->data = val;
  297. return 1;
  298. case sizeof(uint64_t):
  299. p->return_size = sizeof(uint64_t);
  300. *(uint64_t *)p->data = val;
  301. return 1;
  302. }
  303. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  304. p->return_size = sizeof(int32_t); /* Minimum expected size */
  305. if (p->data == NULL)
  306. return 1;
  307. switch (p->data_size) {
  308. case sizeof(int32_t):
  309. if (val <= INT32_MAX) {
  310. *(int32_t *)p->data = (int32_t)val;
  311. return 1;
  312. }
  313. break;
  314. case sizeof(int64_t):
  315. p->return_size = sizeof(int64_t);
  316. *(int64_t *)p->data = (int64_t)val;
  317. return 1;
  318. }
  319. } else if (p->data_type == OSSL_PARAM_REAL) {
  320. p->return_size = sizeof(double);
  321. if (p->data == NULL)
  322. return 1;
  323. switch (p->data_size) {
  324. case sizeof(double):
  325. *(double *)p->data = (double)val;
  326. return 1;
  327. }
  328. }
  329. return 0;
  330. }
  331. OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
  332. {
  333. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  334. sizeof(uint32_t));
  335. }
  336. int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
  337. {
  338. uint64_t u64;
  339. double d;
  340. if (val == NULL || p == NULL )
  341. return 0;
  342. if (p->data_type == OSSL_PARAM_INTEGER) {
  343. switch (p->data_size) {
  344. case sizeof(int32_t):
  345. *val = *(const int32_t *)p->data;
  346. return 1;
  347. case sizeof(int64_t):
  348. *val = *(const int64_t *)p->data;
  349. return 1;
  350. }
  351. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  352. switch (p->data_size) {
  353. case sizeof(uint32_t):
  354. *val = *(const uint32_t *)p->data;
  355. return 1;
  356. case sizeof(uint64_t):
  357. u64 = *(const uint64_t *)p->data;
  358. if (u64 <= INT64_MAX) {
  359. *val = (int64_t)u64;
  360. return 1;
  361. }
  362. break;
  363. }
  364. } else if (p->data_type == OSSL_PARAM_REAL) {
  365. switch (p->data_size) {
  366. case sizeof(double):
  367. d = *(const double *)p->data;
  368. if (d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d) {
  369. *val = (int64_t)d;
  370. return 1;
  371. }
  372. break;
  373. }
  374. }
  375. return 0;
  376. }
  377. int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
  378. {
  379. uint64_t u64;
  380. if (p == NULL)
  381. return 0;
  382. p->return_size = 0;
  383. if (p->data_type == OSSL_PARAM_INTEGER) {
  384. p->return_size = sizeof(int64_t); /* Expected size */
  385. if (p->data == NULL)
  386. return 1;
  387. switch (p->data_size) {
  388. case sizeof(int32_t):
  389. if (val >= INT32_MIN && val <= INT32_MAX) {
  390. p->return_size = sizeof(int32_t);
  391. *(int32_t *)p->data = (int32_t)val;
  392. return 1;
  393. }
  394. break;
  395. case sizeof(int64_t):
  396. *(int64_t *)p->data = val;
  397. return 1;
  398. }
  399. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  400. p->return_size = sizeof(uint64_t); /* Expected size */
  401. if (p->data == NULL)
  402. return 1;
  403. switch (p->data_size) {
  404. case sizeof(uint32_t):
  405. if (val <= UINT32_MAX) {
  406. p->return_size = sizeof(uint32_t);
  407. *(uint32_t *)p->data = (uint32_t)val;
  408. return 1;
  409. }
  410. break;
  411. case sizeof(uint64_t):
  412. *(uint64_t *)p->data = (uint64_t)val;
  413. return 1;
  414. }
  415. } else if (p->data_type == OSSL_PARAM_REAL) {
  416. p->return_size = sizeof(double);
  417. if (p->data == NULL)
  418. return 1;
  419. switch (p->data_size) {
  420. case sizeof(double):
  421. u64 = val < 0 ? -val : val;
  422. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  423. *(double *)p->data = (double)val;
  424. return 1;
  425. }
  426. break;
  427. }
  428. }
  429. return 0;
  430. }
  431. OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
  432. {
  433. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
  434. }
  435. int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
  436. {
  437. int32_t i32;
  438. int64_t i64;
  439. double d;
  440. if (val == NULL || p == NULL)
  441. return 0;
  442. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  443. switch (p->data_size) {
  444. case sizeof(uint32_t):
  445. *val = *(const uint32_t *)p->data;
  446. return 1;
  447. case sizeof(uint64_t):
  448. *val = *(const uint64_t *)p->data;
  449. return 1;
  450. }
  451. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  452. switch (p->data_size) {
  453. case sizeof(int32_t):
  454. i32 = *(const int32_t *)p->data;
  455. if (i32 >= 0) {
  456. *val = (uint64_t)i32;
  457. return 1;
  458. }
  459. break;
  460. case sizeof(int64_t):
  461. i64 = *(const int64_t *)p->data;
  462. if (i64 >= 0) {
  463. *val = (uint64_t)i64;
  464. return 1;
  465. }
  466. break;
  467. }
  468. } else if (p->data_type == OSSL_PARAM_REAL) {
  469. switch (p->data_size) {
  470. case sizeof(double):
  471. d = *(const double *)p->data;
  472. if (d >= 0 && d <= INT64_MAX && d == (uint64_t)d) {
  473. *val = (uint64_t)d;
  474. return 1;
  475. }
  476. break;
  477. }
  478. }
  479. return 0;
  480. }
  481. int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
  482. {
  483. if (p == NULL)
  484. return 0;
  485. p->return_size = 0;
  486. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  487. p->return_size = sizeof(uint64_t); /* Expected size */
  488. if (p->data == NULL)
  489. return 1;
  490. switch (p->data_size) {
  491. case sizeof(uint32_t):
  492. if (val <= UINT32_MAX) {
  493. p->return_size = sizeof(uint32_t);
  494. *(uint32_t *)p->data = (uint32_t)val;
  495. return 1;
  496. }
  497. break;
  498. case sizeof(uint64_t):
  499. *(uint64_t *)p->data = val;
  500. return 1;
  501. }
  502. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  503. p->return_size = sizeof(int64_t); /* Expected size */
  504. if (p->data == NULL)
  505. return 1;
  506. switch (p->data_size) {
  507. case sizeof(int32_t):
  508. if (val <= INT32_MAX) {
  509. p->return_size = sizeof(int32_t);
  510. *(int32_t *)p->data = (int32_t)val;
  511. return 1;
  512. }
  513. break;
  514. case sizeof(int64_t):
  515. if (val <= INT64_MAX) {
  516. *(int64_t *)p->data = (int64_t)val;
  517. return 1;
  518. }
  519. break;
  520. }
  521. } else if (p->data_type == OSSL_PARAM_REAL) {
  522. p->return_size = sizeof(double);
  523. switch (p->data_size) {
  524. case sizeof(double):
  525. if ((val >> 53) == 0) { /* 53 significant bits in the mantissa */
  526. *(double *)p->data = (double)val;
  527. return 1;
  528. }
  529. break;
  530. }
  531. }
  532. return 0;
  533. }
  534. OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
  535. {
  536. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  537. sizeof(uint64_t));
  538. }
  539. int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
  540. {
  541. switch (sizeof(size_t)) {
  542. case sizeof(uint32_t):
  543. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  544. case sizeof(uint64_t):
  545. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  546. }
  547. return 0;
  548. }
  549. int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
  550. {
  551. switch (sizeof(size_t)) {
  552. case sizeof(uint32_t):
  553. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  554. case sizeof(uint64_t):
  555. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  556. }
  557. return 0;
  558. }
  559. OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
  560. {
  561. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  562. sizeof(size_t));
  563. }
  564. int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
  565. {
  566. BIGNUM *b;
  567. if (val == NULL
  568. || p == NULL
  569. || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  570. return 0;
  571. b = BN_native2bn(p->data, (int)p->data_size, *val);
  572. if (b != NULL) {
  573. *val = b;
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
  579. {
  580. size_t bytes;
  581. if (p == NULL)
  582. return 0;
  583. p->return_size = 0;
  584. if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  585. return 0;
  586. /* For the moment, only positive values are permitted */
  587. if (BN_is_negative(val))
  588. return 0;
  589. bytes = (size_t)BN_num_bytes(val);
  590. p->return_size = bytes;
  591. if (p->data == NULL)
  592. return 1;
  593. return p->data_size >= bytes
  594. && BN_bn2nativepad(val, p->data, bytes) >= 0;
  595. }
  596. OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
  597. size_t bsize)
  598. {
  599. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
  600. buf, bsize);
  601. }
  602. int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
  603. {
  604. int64_t i64;
  605. uint64_t u64;
  606. if (val == NULL || p == NULL)
  607. return 0;
  608. if (p->data_type == OSSL_PARAM_REAL) {
  609. switch (p->data_size) {
  610. case sizeof(double):
  611. *val = *(const double *)p->data;
  612. return 1;
  613. }
  614. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  615. switch (p->data_size) {
  616. case sizeof(uint32_t):
  617. *val = *(const uint32_t *)p->data;
  618. return 1;
  619. case sizeof(uint64_t):
  620. u64 = *(const uint64_t *)p->data;
  621. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  622. *val = (double)u64;
  623. return 1;
  624. }
  625. break;
  626. }
  627. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  628. switch (p->data_size) {
  629. case sizeof(int32_t):
  630. *val = *(const int32_t *)p->data;
  631. return 1;
  632. case sizeof(int64_t):
  633. i64 = *(const int64_t *)p->data;
  634. u64 = i64 < 0 ? -i64 : i64;
  635. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  636. *val = 0.0 + i64;
  637. return 1;
  638. }
  639. break;
  640. }
  641. }
  642. return 0;
  643. }
  644. int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
  645. {
  646. if (p == NULL)
  647. return 0;
  648. p->return_size = 0;
  649. if (p->data_type == OSSL_PARAM_REAL) {
  650. p->return_size = sizeof(double);
  651. if (p->data == NULL)
  652. return 1;
  653. switch (p->data_size) {
  654. case sizeof(double):
  655. *(double *)p->data = val;
  656. return 1;
  657. }
  658. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
  659. && val == (ossl_uintmax_t)val) {
  660. p->return_size = sizeof(double);
  661. if (p->data == NULL)
  662. return 1;
  663. switch (p->data_size) {
  664. case sizeof(uint32_t):
  665. if (val >= 0 && val <= UINT32_MAX) {
  666. p->return_size = sizeof(uint32_t);
  667. *(uint32_t *)p->data = (uint32_t)val;
  668. return 1;
  669. }
  670. break;
  671. case sizeof(uint64_t):
  672. if (val >= 0 && val <= UINT64_MAX) {
  673. p->return_size = sizeof(uint64_t);
  674. *(uint64_t *)p->data = (uint64_t)val;
  675. return 1;
  676. }
  677. break; }
  678. } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
  679. p->return_size = sizeof(double);
  680. if (p->data == NULL)
  681. return 1;
  682. switch (p->data_size) {
  683. case sizeof(int32_t):
  684. if (val >= INT32_MIN && val <= INT32_MAX) {
  685. p->return_size = sizeof(int32_t);
  686. *(int32_t *)p->data = (int32_t)val;
  687. return 1;
  688. }
  689. break;
  690. case sizeof(int64_t):
  691. if (val >= INT64_MIN && val <= INT64_MAX) {
  692. p->return_size = sizeof(int64_t);
  693. *(int64_t *)p->data = (int64_t)val;
  694. return 1;
  695. }
  696. break;
  697. }
  698. }
  699. return 0;
  700. }
  701. OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
  702. {
  703. return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
  704. }
  705. static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len,
  706. size_t *used_len, unsigned int type)
  707. {
  708. size_t sz;
  709. if (val == NULL || p == NULL || p->data_type != type)
  710. return 0;
  711. sz = p->data_size;
  712. if (used_len != NULL)
  713. *used_len = sz;
  714. if (*val == NULL) {
  715. char *const q = OPENSSL_malloc(sz);
  716. if (q == NULL)
  717. return 0;
  718. *val = q;
  719. memcpy(q, p->data, sz);
  720. return 1;
  721. }
  722. if (max_len < sz)
  723. return 0;
  724. memcpy(*val, p->data, sz);
  725. return 1;
  726. }
  727. int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
  728. {
  729. return get_string_internal(p, (void **)val, max_len, NULL,
  730. OSSL_PARAM_UTF8_STRING);
  731. }
  732. int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
  733. size_t *used_len)
  734. {
  735. return get_string_internal(p, val, max_len, used_len,
  736. OSSL_PARAM_OCTET_STRING);
  737. }
  738. static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
  739. unsigned int type)
  740. {
  741. p->return_size = len;
  742. if (p->data == NULL)
  743. return 1;
  744. if (p->data_type != type || p->data_size < len)
  745. return 0;
  746. memcpy(p->data, val, len);
  747. return 1;
  748. }
  749. int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
  750. {
  751. if (p == NULL)
  752. return 0;
  753. p->return_size = 0;
  754. if (val == NULL)
  755. return 0;
  756. return set_string_internal(p, val, strlen(val) + 1, OSSL_PARAM_UTF8_STRING);
  757. }
  758. int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
  759. size_t len)
  760. {
  761. if (p == NULL)
  762. return 0;
  763. p->return_size = 0;
  764. if (val == NULL)
  765. return 0;
  766. return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
  767. }
  768. OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
  769. size_t bsize)
  770. {
  771. if (buf != NULL && bsize == 0)
  772. bsize = strlen(buf) + 1;
  773. return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
  774. }
  775. OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
  776. size_t bsize)
  777. {
  778. return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
  779. }
  780. static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
  781. size_t *used_len, unsigned int type)
  782. {
  783. if (val == NULL || p == NULL || p->data_type != type)
  784. return 0;
  785. if (used_len != NULL)
  786. *used_len = p->data_size;
  787. *val = *(const void **)p->data;
  788. return 1;
  789. }
  790. int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
  791. {
  792. return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
  793. }
  794. int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
  795. size_t *used_len)
  796. {
  797. return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
  798. }
  799. static int set_ptr_internal(OSSL_PARAM *p, const void *val,
  800. unsigned int type, size_t len)
  801. {
  802. p->return_size = len;
  803. if (p->data_type != type)
  804. return 0;
  805. if (p->data != NULL)
  806. *(const void **)p->data = val;
  807. return 1;
  808. }
  809. int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
  810. {
  811. if (p == NULL)
  812. return 0;
  813. p->return_size = 0;
  814. if (val == NULL)
  815. return 0;
  816. return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR, strlen(val) + 1);
  817. }
  818. int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
  819. size_t used_len)
  820. {
  821. if (p == NULL)
  822. return 0;
  823. p->return_size = 0;
  824. if (val == NULL)
  825. return 0;
  826. return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
  827. }
  828. OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
  829. size_t bsize)
  830. {
  831. return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
  832. }
  833. OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  834. size_t bsize)
  835. {
  836. return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
  837. }
  838. OSSL_PARAM OSSL_PARAM_construct_end(void)
  839. {
  840. OSSL_PARAM end = OSSL_PARAM_END;
  841. return end;
  842. }