fake_rsaprov.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <string.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_object.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/provider.h>
  15. #include "testutil.h"
  16. #include "fake_rsaprov.h"
  17. static OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new;
  18. static OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free;
  19. static OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has;
  20. static OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query;
  21. static OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import;
  22. static OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes;
  23. static OSSL_FUNC_keymgmt_export_fn fake_rsa_keymgmt_export;
  24. static OSSL_FUNC_keymgmt_export_types_fn fake_rsa_keymgmt_exptypes;
  25. static OSSL_FUNC_keymgmt_load_fn fake_rsa_keymgmt_load;
  26. static int has_selection;
  27. static int imptypes_selection;
  28. static int exptypes_selection;
  29. static int query_id;
  30. static int key_deleted;
  31. struct fake_rsa_keydata {
  32. int selection;
  33. int status;
  34. };
  35. void fake_rsa_restore_store_state(void)
  36. {
  37. key_deleted = 0;
  38. }
  39. static void *fake_rsa_keymgmt_new(void *provctx)
  40. {
  41. struct fake_rsa_keydata *key;
  42. if (!TEST_ptr(key = OPENSSL_zalloc(sizeof(struct fake_rsa_keydata))))
  43. return NULL;
  44. /* clear test globals */
  45. has_selection = 0;
  46. imptypes_selection = 0;
  47. exptypes_selection = 0;
  48. query_id = 0;
  49. return key;
  50. }
  51. static void fake_rsa_keymgmt_free(void *keydata)
  52. {
  53. OPENSSL_free(keydata);
  54. }
  55. static int fake_rsa_keymgmt_has(const void *key, int selection)
  56. {
  57. /* record global for checking */
  58. has_selection = selection;
  59. return 1;
  60. }
  61. static const char *fake_rsa_keymgmt_query(int id)
  62. {
  63. /* record global for checking */
  64. query_id = id;
  65. return "RSA";
  66. }
  67. static int fake_rsa_keymgmt_import(void *keydata, int selection,
  68. const OSSL_PARAM *p)
  69. {
  70. struct fake_rsa_keydata *fake_rsa_key = keydata;
  71. /* key was imported */
  72. fake_rsa_key->status = 1;
  73. return 1;
  74. }
  75. static unsigned char fake_rsa_n[] =
  76. "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
  77. "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
  78. "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
  79. "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
  80. "\xF5";
  81. static unsigned char fake_rsa_e[] = "\x11";
  82. static unsigned char fake_rsa_d[] =
  83. "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
  84. "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
  85. "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
  86. "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
  87. static unsigned char fake_rsa_p[] =
  88. "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
  89. "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
  90. "\x0D";
  91. static unsigned char fake_rsa_q[] =
  92. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  93. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
  94. "\x89";
  95. static unsigned char fake_rsa_dmp1[] =
  96. "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
  97. "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
  98. static unsigned char fake_rsa_dmq1[] =
  99. "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
  100. "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
  101. "\x51";
  102. static unsigned char fake_rsa_iqmp[] =
  103. "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
  104. "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
  105. OSSL_PARAM *fake_rsa_key_params(int priv)
  106. {
  107. if (priv) {
  108. OSSL_PARAM params[] = {
  109. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
  110. sizeof(fake_rsa_n) -1),
  111. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
  112. sizeof(fake_rsa_e) -1),
  113. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, fake_rsa_d,
  114. sizeof(fake_rsa_d) -1),
  115. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, fake_rsa_p,
  116. sizeof(fake_rsa_p) -1),
  117. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, fake_rsa_q,
  118. sizeof(fake_rsa_q) -1),
  119. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, fake_rsa_dmp1,
  120. sizeof(fake_rsa_dmp1) -1),
  121. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, fake_rsa_dmq1,
  122. sizeof(fake_rsa_dmq1) -1),
  123. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, fake_rsa_iqmp,
  124. sizeof(fake_rsa_iqmp) -1),
  125. OSSL_PARAM_END
  126. };
  127. return OSSL_PARAM_dup(params);
  128. } else {
  129. OSSL_PARAM params[] = {
  130. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
  131. sizeof(fake_rsa_n) -1),
  132. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
  133. sizeof(fake_rsa_e) -1),
  134. OSSL_PARAM_END
  135. };
  136. return OSSL_PARAM_dup(params);
  137. }
  138. }
  139. static int fake_rsa_keymgmt_export(void *keydata, int selection,
  140. OSSL_CALLBACK *param_callback, void *cbarg)
  141. {
  142. OSSL_PARAM *params = NULL;
  143. int ret;
  144. if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
  145. return 0;
  146. if (!TEST_ptr(params = fake_rsa_key_params(0)))
  147. return 0;
  148. ret = param_callback(params, cbarg);
  149. OSSL_PARAM_free(params);
  150. return ret;
  151. }
  152. static const OSSL_PARAM fake_rsa_import_key_types[] = {
  153. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
  154. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
  155. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
  156. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),
  157. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),
  158. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),
  159. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),
  160. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
  161. OSSL_PARAM_END
  162. };
  163. static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
  164. {
  165. /* record global for checking */
  166. imptypes_selection = selection;
  167. return fake_rsa_import_key_types;
  168. }
  169. static const OSSL_PARAM fake_rsa_export_key_types[] = {
  170. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
  171. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
  172. OSSL_PARAM_END
  173. };
  174. static const OSSL_PARAM *fake_rsa_keymgmt_exptypes(int selection)
  175. {
  176. /* record global for checking */
  177. exptypes_selection = selection;
  178. return fake_rsa_export_key_types;
  179. }
  180. static void *fake_rsa_keymgmt_load(const void *reference, size_t reference_sz)
  181. {
  182. struct fake_rsa_keydata *key = NULL;
  183. if (reference_sz != sizeof(*key))
  184. return NULL;
  185. key = *(struct fake_rsa_keydata **)reference;
  186. if (key->status != 1)
  187. return NULL;
  188. /* detach the reference */
  189. *(struct fake_rsa_keydata **)reference = NULL;
  190. return key;
  191. }
  192. static void *fake_rsa_gen_init(void *provctx, int selection,
  193. const OSSL_PARAM params[])
  194. {
  195. unsigned char *gctx = NULL;
  196. if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
  197. return NULL;
  198. *gctx = 1;
  199. return gctx;
  200. }
  201. static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  202. {
  203. unsigned char *gctx = genctx;
  204. static const unsigned char inited[] = { 1 };
  205. struct fake_rsa_keydata *keydata;
  206. if (!TEST_ptr(gctx)
  207. || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
  208. return NULL;
  209. if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
  210. return NULL;
  211. keydata->status = 2;
  212. return keydata;
  213. }
  214. static void fake_rsa_gen_cleanup(void *genctx)
  215. {
  216. OPENSSL_free(genctx);
  217. }
  218. static const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = {
  219. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new },
  220. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} ,
  221. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has },
  222. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  223. (void (*)(void))fake_rsa_keymgmt_query },
  224. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import },
  225. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES,
  226. (void (*)(void))fake_rsa_keymgmt_imptypes },
  227. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))fake_rsa_keymgmt_export },
  228. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES,
  229. (void (*)(void))fake_rsa_keymgmt_exptypes },
  230. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))fake_rsa_keymgmt_load },
  231. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init },
  232. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen },
  233. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup },
  234. OSSL_DISPATCH_END
  235. };
  236. static const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = {
  237. { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" },
  238. { NULL, NULL, NULL, NULL }
  239. };
  240. static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx;
  241. static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx;
  242. static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init;
  243. static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign;
  244. static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
  245. {
  246. unsigned char *sigctx = OPENSSL_zalloc(1);
  247. TEST_ptr(sigctx);
  248. return sigctx;
  249. }
  250. static void fake_rsa_sig_freectx(void *sigctx)
  251. {
  252. OPENSSL_free(sigctx);
  253. }
  254. static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
  255. const OSSL_PARAM params[])
  256. {
  257. unsigned char *sigctx = ctx;
  258. struct fake_rsa_keydata *keydata = provkey;
  259. /* we must have a ctx */
  260. if (!TEST_ptr(sigctx))
  261. return 0;
  262. /* we must have some initialized key */
  263. if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0))
  264. return 0;
  265. /* record that sign init was called */
  266. *sigctx = 1;
  267. return 1;
  268. }
  269. static int fake_rsa_sig_sign(void *ctx, unsigned char *sig,
  270. size_t *siglen, size_t sigsize,
  271. const unsigned char *tbs, size_t tbslen)
  272. {
  273. unsigned char *sigctx = ctx;
  274. /* we must have a ctx and init was called upon it */
  275. if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
  276. return 0;
  277. *siglen = 256;
  278. /* record that the real sign operation was called */
  279. if (sig != NULL) {
  280. if (!TEST_int_ge(sigsize, *siglen))
  281. return 0;
  282. *sigctx = 2;
  283. /* produce a fake signature */
  284. memset(sig, 'a', *siglen);
  285. }
  286. return 1;
  287. }
  288. #define FAKE_DGSTSGN_SIGN 0x01
  289. #define FAKE_DGSTSGN_VERIFY 0x02
  290. #define FAKE_DGSTSGN_UPDATED 0x04
  291. #define FAKE_DGSTSGN_FINALISED 0x08
  292. #define FAKE_DGSTSGN_NO_DUP 0xA0
  293. static void *fake_rsa_sig_dupctx(void *ctx)
  294. {
  295. unsigned char *sigctx = ctx;
  296. unsigned char *newctx;
  297. if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0)
  298. return NULL;
  299. if (!TEST_ptr(newctx = OPENSSL_zalloc(1)))
  300. return NULL;
  301. *newctx = *sigctx;
  302. return newctx;
  303. }
  304. static int fake_rsa_dgstsgnvfy_init(void *ctx, unsigned char type,
  305. void *provkey, const OSSL_PARAM params[])
  306. {
  307. unsigned char *sigctx = ctx;
  308. struct fake_rsa_keydata *keydata = provkey;
  309. /* we must have a ctx */
  310. if (!TEST_ptr(sigctx))
  311. return 0;
  312. /* we must have some initialized key */
  313. if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0))
  314. return 0;
  315. /* record that sign/verify init was called */
  316. *sigctx = type;
  317. if (params) {
  318. const OSSL_PARAM *p;
  319. int dup;
  320. p = OSSL_PARAM_locate_const(params, "NO_DUP");
  321. if (p != NULL) {
  322. if (OSSL_PARAM_get_int(p, &dup)) {
  323. *sigctx |= FAKE_DGSTSGN_NO_DUP;
  324. }
  325. }
  326. }
  327. return 1;
  328. }
  329. static int fake_rsa_dgstsgn_init(void *ctx, const char *mdname,
  330. void *provkey, const OSSL_PARAM params[])
  331. {
  332. return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_SIGN, provkey, params);
  333. }
  334. static int fake_rsa_dgstvfy_init(void *ctx, const char *mdname,
  335. void *provkey, const OSSL_PARAM params[])
  336. {
  337. return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_VERIFY, provkey, params);
  338. }
  339. static int fake_rsa_dgstsgnvfy_update(void *ctx, const unsigned char *data,
  340. size_t datalen)
  341. {
  342. unsigned char *sigctx = ctx;
  343. /* we must have a ctx */
  344. if (!TEST_ptr(sigctx))
  345. return 0;
  346. if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0)
  347. return 0;
  348. *sigctx |= FAKE_DGSTSGN_UPDATED;
  349. return 1;
  350. }
  351. static int fake_rsa_dgstsgnvfy_final(void *ctx, unsigned char *sig,
  352. size_t *siglen, size_t sigsize)
  353. {
  354. unsigned char *sigctx = ctx;
  355. /* we must have a ctx */
  356. if (!TEST_ptr(sigctx))
  357. return 0;
  358. if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0)
  359. return 0;
  360. if ((*sigctx & FAKE_DGSTSGN_SIGN) != 0 && (siglen == NULL))
  361. return 0;
  362. if ((*sigctx & FAKE_DGSTSGN_VERIFY) != 0 && (siglen != NULL))
  363. return 0;
  364. /* this is sign op */
  365. if (siglen) {
  366. *siglen = 256;
  367. /* record that the real sign operation was called */
  368. if (sig != NULL) {
  369. if (!TEST_int_ge(sigsize, *siglen))
  370. return 0;
  371. /* produce a fake signature */
  372. memset(sig, 'a', *siglen);
  373. }
  374. }
  375. /* simulate inability to duplicate context and finalise it */
  376. if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0) {
  377. *sigctx |= FAKE_DGSTSGN_FINALISED;
  378. }
  379. return 1;
  380. }
  381. static int fake_rsa_dgstvfy_final(void *ctx, unsigned char *sig,
  382. size_t siglen)
  383. {
  384. return fake_rsa_dgstsgnvfy_final(ctx, sig, NULL, siglen);
  385. }
  386. static int fake_rsa_dgstsgn(void *ctx, unsigned char *sig, size_t *siglen,
  387. size_t sigsize, const unsigned char *tbs,
  388. size_t tbslen)
  389. {
  390. if (!fake_rsa_dgstsgnvfy_update(ctx, tbs, tbslen))
  391. return 0;
  392. return fake_rsa_dgstsgnvfy_final(ctx, sig, siglen, sigsize);
  393. }
  394. static int fake_rsa_dgstvfy(void *ctx, unsigned char *sig, size_t siglen,
  395. const unsigned char *tbv, size_t tbvlen)
  396. {
  397. if (!fake_rsa_dgstsgnvfy_update(ctx, tbv, tbvlen))
  398. return 0;
  399. return fake_rsa_dgstvfy_final(ctx, sig, siglen);
  400. }
  401. static const OSSL_DISPATCH fake_rsa_sig_funcs[] = {
  402. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx },
  403. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx },
  404. { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init },
  405. { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign },
  406. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))fake_rsa_sig_dupctx },
  407. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  408. (void (*)(void))fake_rsa_dgstsgn_init },
  409. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
  410. (void (*)(void))fake_rsa_dgstsgnvfy_update },
  411. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
  412. (void (*)(void))fake_rsa_dgstsgnvfy_final },
  413. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  414. (void (*)(void))fake_rsa_dgstsgn },
  415. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  416. (void (*)(void))fake_rsa_dgstvfy_init },
  417. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
  418. (void (*)(void))fake_rsa_dgstsgnvfy_update },
  419. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
  420. (void (*)(void))fake_rsa_dgstvfy_final },
  421. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  422. (void (*)(void))fake_rsa_dgstvfy },
  423. OSSL_DISPATCH_END
  424. };
  425. static const OSSL_ALGORITHM fake_rsa_sig_algs[] = {
  426. { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" },
  427. { NULL, NULL, NULL, NULL }
  428. };
  429. static OSSL_FUNC_store_open_fn fake_rsa_st_open;
  430. static OSSL_FUNC_store_open_ex_fn fake_rsa_st_open_ex;
  431. static OSSL_FUNC_store_settable_ctx_params_fn fake_rsa_st_settable_ctx_params;
  432. static OSSL_FUNC_store_set_ctx_params_fn fake_rsa_st_set_ctx_params;
  433. static OSSL_FUNC_store_load_fn fake_rsa_st_load;
  434. static OSSL_FUNC_store_eof_fn fake_rsa_st_eof;
  435. static OSSL_FUNC_store_close_fn fake_rsa_st_close;
  436. static OSSL_FUNC_store_delete_fn fake_rsa_st_delete;
  437. static const char fake_rsa_scheme[] = "fake_rsa:";
  438. static const char fake_rsa_openpwtest[] = "fake_rsa:openpwtest";
  439. static const char fake_rsa_prompt[] = "Fake Prompt Info";
  440. static void *fake_rsa_st_open_ex(void *provctx, const char *uri,
  441. const OSSL_PARAM params[],
  442. OSSL_PASSPHRASE_CALLBACK *pw_cb,
  443. void *pw_cbarg)
  444. {
  445. unsigned char *storectx = NULL;
  446. /* First check whether the uri is ours */
  447. if (strncmp(uri, fake_rsa_scheme, sizeof(fake_rsa_scheme) - 1) != 0)
  448. return NULL;
  449. if (strncmp(uri, fake_rsa_openpwtest,
  450. sizeof(fake_rsa_openpwtest) - 1) == 0) {
  451. const char *pw_check = FAKE_PASSPHRASE;
  452. char fakepw[sizeof(FAKE_PASSPHRASE) + 1] = { 0 };
  453. size_t fakepw_len = 0;
  454. OSSL_PARAM pw_params[2] = {
  455. OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO,
  456. (void *)fake_rsa_prompt,
  457. sizeof(fake_rsa_prompt) - 1),
  458. OSSL_PARAM_END,
  459. };
  460. if (pw_cb == NULL) {
  461. return NULL;
  462. }
  463. if (!pw_cb(fakepw, sizeof(fakepw), &fakepw_len, pw_params, pw_cbarg)) {
  464. TEST_info("fake_rsa_open_ex failed passphrase callback");
  465. return NULL;
  466. }
  467. if (strncmp(pw_check, fakepw, sizeof(pw_check) - 1) != 0) {
  468. TEST_info("fake_rsa_open_ex failed passphrase check");
  469. return NULL;
  470. }
  471. }
  472. storectx = OPENSSL_zalloc(1);
  473. if (!TEST_ptr(storectx))
  474. return NULL;
  475. TEST_info("fake_rsa_open_ex called");
  476. return storectx;
  477. }
  478. static void *fake_rsa_st_open(void *provctx, const char *uri)
  479. {
  480. unsigned char *storectx = NULL;
  481. storectx = fake_rsa_st_open_ex(provctx, uri, NULL, NULL, NULL);
  482. TEST_info("fake_rsa_open called");
  483. return storectx;
  484. }
  485. static const OSSL_PARAM *fake_rsa_st_settable_ctx_params(void *provctx)
  486. {
  487. static const OSSL_PARAM known_settable_ctx_params[] = {
  488. OSSL_PARAM_END
  489. };
  490. return known_settable_ctx_params;
  491. }
  492. static int fake_rsa_st_set_ctx_params(void *loaderctx,
  493. const OSSL_PARAM params[])
  494. {
  495. return 1;
  496. }
  497. static int fake_rsa_st_load(void *loaderctx,
  498. OSSL_CALLBACK *object_cb, void *object_cbarg,
  499. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  500. {
  501. unsigned char *storectx = loaderctx;
  502. OSSL_PARAM params[4];
  503. int object_type = OSSL_OBJECT_PKEY;
  504. struct fake_rsa_keydata *key = NULL;
  505. int rv = 0;
  506. switch (*storectx) {
  507. case 0:
  508. if (key_deleted == 1) {
  509. *storectx = 1;
  510. break;
  511. }
  512. /* Construct a new key using our keymgmt functions */
  513. if (!TEST_ptr(key = fake_rsa_keymgmt_new(NULL)))
  514. break;
  515. if (!TEST_int_gt(fake_rsa_keymgmt_import(key, 0, NULL), 0))
  516. break;
  517. params[0] =
  518. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  519. params[1] =
  520. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  521. "RSA", 0);
  522. /* The address of the key becomes the octet string */
  523. params[2] =
  524. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  525. &key, sizeof(*key));
  526. params[3] = OSSL_PARAM_construct_end();
  527. rv = object_cb(params, object_cbarg);
  528. *storectx = 1;
  529. break;
  530. case 2:
  531. TEST_info("fake_rsa_load() called in error state");
  532. break;
  533. default:
  534. TEST_info("fake_rsa_load() called in eof state");
  535. break;
  536. }
  537. TEST_info("fake_rsa_load called - rv: %d", rv);
  538. if (rv == 0 && key_deleted == 0) {
  539. fake_rsa_keymgmt_free(key);
  540. *storectx = 2;
  541. }
  542. return rv;
  543. }
  544. static int fake_rsa_st_delete(void *loaderctx, const char *uri,
  545. const OSSL_PARAM params[],
  546. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  547. {
  548. key_deleted = 1;
  549. return 1;
  550. }
  551. static int fake_rsa_st_eof(void *loaderctx)
  552. {
  553. unsigned char *storectx = loaderctx;
  554. /* just one key for now in the fake_rsa store */
  555. return *storectx != 0;
  556. }
  557. static int fake_rsa_st_close(void *loaderctx)
  558. {
  559. OPENSSL_free(loaderctx);
  560. return 1;
  561. }
  562. static const OSSL_DISPATCH fake_rsa_store_funcs[] = {
  563. { OSSL_FUNC_STORE_OPEN, (void (*)(void))fake_rsa_st_open },
  564. { OSSL_FUNC_STORE_OPEN_EX, (void (*)(void))fake_rsa_st_open_ex },
  565. { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
  566. (void (*)(void))fake_rsa_st_settable_ctx_params },
  567. { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))fake_rsa_st_set_ctx_params },
  568. { OSSL_FUNC_STORE_LOAD, (void (*)(void))fake_rsa_st_load },
  569. { OSSL_FUNC_STORE_EOF, (void (*)(void))fake_rsa_st_eof },
  570. { OSSL_FUNC_STORE_CLOSE, (void (*)(void))fake_rsa_st_close },
  571. { OSSL_FUNC_STORE_DELETE, (void (*)(void))fake_rsa_st_delete },
  572. OSSL_DISPATCH_END,
  573. };
  574. static const OSSL_ALGORITHM fake_rsa_store_algs[] = {
  575. { "fake_rsa", "provider=fake-rsa", fake_rsa_store_funcs },
  576. { NULL, NULL, NULL }
  577. };
  578. static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
  579. int operation_id,
  580. int *no_cache)
  581. {
  582. *no_cache = 0;
  583. switch (operation_id) {
  584. case OSSL_OP_SIGNATURE:
  585. return fake_rsa_sig_algs;
  586. case OSSL_OP_KEYMGMT:
  587. return fake_rsa_keymgmt_algs;
  588. case OSSL_OP_STORE:
  589. return fake_rsa_store_algs;
  590. }
  591. return NULL;
  592. }
  593. /* Functions we provide to the core */
  594. static const OSSL_DISPATCH fake_rsa_method[] = {
  595. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
  596. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query },
  597. OSSL_DISPATCH_END
  598. };
  599. static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
  600. const OSSL_DISPATCH *in,
  601. const OSSL_DISPATCH **out, void **provctx)
  602. {
  603. if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
  604. return 0;
  605. *out = fake_rsa_method;
  606. return 1;
  607. }
  608. OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
  609. {
  610. OSSL_PROVIDER *p;
  611. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa",
  612. fake_rsa_provider_init))
  613. || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1)))
  614. return NULL;
  615. return p;
  616. }
  617. void fake_rsa_finish(OSSL_PROVIDER *p)
  618. {
  619. OSSL_PROVIDER_unload(p);
  620. }