params.c 26 KB

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