tls-provider.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/rand.h>
  13. #include <openssl/params.h>
  14. /* For TLS1_3_VERSION */
  15. #include <openssl/ssl.h>
  16. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  17. const OSSL_DISPATCH *in,
  18. const OSSL_DISPATCH **out,
  19. void **provctx);
  20. #define XOR_KEY_SIZE 32
  21. /*
  22. * Top secret. This algorithm only works if no one knows what this number is.
  23. * Please don't tell anyone what it is.
  24. *
  25. * This algorithm is for testing only - don't really use it!
  26. */
  27. static const unsigned char private_constant[XOR_KEY_SIZE] = {
  28. 0xd3, 0x6b, 0x54, 0xec, 0x5b, 0xac, 0x89, 0x96, 0x8c, 0x2c, 0x66, 0xa5,
  29. 0x67, 0x0d, 0xe3, 0xdd, 0x43, 0x69, 0xbc, 0x83, 0x3d, 0x60, 0xc7, 0xb8,
  30. 0x2b, 0x1c, 0x5a, 0xfd, 0xb5, 0xcd, 0xd0, 0xf8
  31. };
  32. typedef struct xorkey_st {
  33. unsigned char privkey[XOR_KEY_SIZE];
  34. unsigned char pubkey[XOR_KEY_SIZE];
  35. int hasprivkey;
  36. int haspubkey;
  37. } XORKEY;
  38. /* Key Management for the dummy XOR KEX and KEM algorithms */
  39. static OSSL_FUNC_keymgmt_new_fn xor_newdata;
  40. static OSSL_FUNC_keymgmt_free_fn xor_freedata;
  41. static OSSL_FUNC_keymgmt_has_fn xor_has;
  42. static OSSL_FUNC_keymgmt_copy_fn xor_copy;
  43. static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init;
  44. static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params;
  45. static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params;
  46. static OSSL_FUNC_keymgmt_gen_fn xor_gen;
  47. static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup;
  48. static OSSL_FUNC_keymgmt_get_params_fn xor_get_params;
  49. static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params;
  50. static OSSL_FUNC_keymgmt_set_params_fn xor_set_params;
  51. static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params;
  52. /*
  53. * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
  54. * together. Don't use this!
  55. */
  56. static OSSL_FUNC_keyexch_newctx_fn xor_newctx;
  57. static OSSL_FUNC_keyexch_init_fn xor_init;
  58. static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer;
  59. static OSSL_FUNC_keyexch_derive_fn xor_derive;
  60. static OSSL_FUNC_keyexch_freectx_fn xor_freectx;
  61. static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx;
  62. /*
  63. * Dummy "XOR" Key Encapsulation Method. We just build a KEM over the xor KEX.
  64. * Don't use this!
  65. */
  66. static OSSL_FUNC_kem_newctx_fn xor_newctx;
  67. static OSSL_FUNC_kem_freectx_fn xor_freectx;
  68. static OSSL_FUNC_kem_dupctx_fn xor_dupctx;
  69. static OSSL_FUNC_kem_encapsulate_init_fn xor_init;
  70. static OSSL_FUNC_kem_encapsulate_fn xor_encapsulate;
  71. static OSSL_FUNC_kem_decapsulate_init_fn xor_init;
  72. static OSSL_FUNC_kem_decapsulate_fn xor_decapsulate;
  73. /*
  74. * We define 2 dummy TLS groups called "xorgroup" and "xorkemgroup" for test
  75. * purposes
  76. */
  77. struct tls_group_st {
  78. unsigned int group_id; /* for "tls-group-id", see provider-base(7) */
  79. unsigned int secbits;
  80. unsigned int mintls;
  81. unsigned int maxtls;
  82. unsigned int mindtls;
  83. unsigned int maxdtls;
  84. unsigned int is_kem; /* boolean */
  85. };
  86. #define XORGROUP_NAME "xorgroup"
  87. #define XORGROUP_NAME_INTERNAL "xorgroup-int"
  88. static struct tls_group_st xor_group = {
  89. 0, /* group_id, set by randomize_tls_group_id() */
  90. 128, /* secbits */
  91. TLS1_3_VERSION, /* mintls */
  92. 0, /* maxtls */
  93. -1, /* mindtls */
  94. -1, /* maxdtls */
  95. 0 /* is_kem */
  96. };
  97. #define XORKEMGROUP_NAME "xorkemgroup"
  98. #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int"
  99. static struct tls_group_st xor_kemgroup = {
  100. 0, /* group_id, set by randomize_tls_group_id() */
  101. 128, /* secbits */
  102. TLS1_3_VERSION, /* mintls */
  103. 0, /* maxtls */
  104. -1, /* mindtls */
  105. -1, /* maxdtls */
  106. 1 /* is_kem */
  107. };
  108. #define ALGORITHM "XOR"
  109. static const OSSL_PARAM xor_group_params[] = {
  110. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
  111. XORGROUP_NAME, sizeof(XORGROUP_NAME)),
  112. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
  113. XORGROUP_NAME_INTERNAL,
  114. sizeof(XORGROUP_NAME_INTERNAL)),
  115. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
  116. sizeof(ALGORITHM)),
  117. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_group.group_id),
  118. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
  119. &xor_group.secbits),
  120. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_group.mintls),
  121. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_group.maxtls),
  122. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_group.mindtls),
  123. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_group.maxdtls),
  124. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_group.is_kem),
  125. OSSL_PARAM_END
  126. };
  127. static const OSSL_PARAM xor_kemgroup_params[] = {
  128. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
  129. XORKEMGROUP_NAME, sizeof(XORKEMGROUP_NAME)),
  130. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
  131. XORKEMGROUP_NAME_INTERNAL,
  132. sizeof(XORKEMGROUP_NAME_INTERNAL)),
  133. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
  134. sizeof(ALGORITHM)),
  135. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_kemgroup.group_id),
  136. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
  137. &xor_kemgroup.secbits),
  138. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_kemgroup.mintls),
  139. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_kemgroup.maxtls),
  140. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_kemgroup.mindtls),
  141. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_kemgroup.maxdtls),
  142. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_kemgroup.is_kem),
  143. OSSL_PARAM_END
  144. };
  145. static int tls_prov_get_capabilities(void *provctx, const char *capability,
  146. OSSL_CALLBACK *cb, void *arg)
  147. {
  148. if (strcmp(capability, "TLS-GROUP") == 0)
  149. return cb(xor_group_params, arg)
  150. && cb(xor_kemgroup_params, arg);
  151. /* We don't support this capability */
  152. return 0;
  153. }
  154. /*
  155. * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
  156. * together. Don't use this!
  157. */
  158. typedef struct {
  159. XORKEY *key;
  160. XORKEY *peerkey;
  161. void *provctx;
  162. } PROV_XOR_CTX;
  163. static void *xor_newctx(void *provctx)
  164. {
  165. PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX));
  166. if (pxorctx == NULL)
  167. return NULL;
  168. pxorctx->provctx = provctx;
  169. return pxorctx;
  170. }
  171. static int xor_init(void *vpxorctx, void *vkey)
  172. {
  173. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  174. if (pxorctx == NULL || vkey == NULL)
  175. return 0;
  176. pxorctx->key = vkey;
  177. return 1;
  178. }
  179. static int xor_set_peer(void *vpxorctx, void *vpeerkey)
  180. {
  181. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  182. if (pxorctx == NULL || vpeerkey == NULL)
  183. return 0;
  184. pxorctx->peerkey = vpeerkey;
  185. return 1;
  186. }
  187. static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
  188. size_t outlen)
  189. {
  190. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  191. int i;
  192. if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
  193. return 0;
  194. *secretlen = XOR_KEY_SIZE;
  195. if (secret == NULL)
  196. return 1;
  197. if (outlen < XOR_KEY_SIZE)
  198. return 0;
  199. for (i = 0; i < XOR_KEY_SIZE; i++)
  200. secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
  201. return 1;
  202. }
  203. static void xor_freectx(void *pxorctx)
  204. {
  205. OPENSSL_free(pxorctx);
  206. }
  207. static void *xor_dupctx(void *vpxorctx)
  208. {
  209. PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx;
  210. PROV_XOR_CTX *dstctx;
  211. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  212. if (dstctx == NULL)
  213. return NULL;
  214. *dstctx = *srcctx;
  215. return dstctx;
  216. }
  217. static const OSSL_DISPATCH xor_keyexch_functions[] = {
  218. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx },
  219. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
  220. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
  221. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
  222. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
  223. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
  224. { 0, NULL }
  225. };
  226. static const OSSL_ALGORITHM tls_prov_keyexch[] = {
  227. /*
  228. * Obviously this is not FIPS approved, but in order to test in conjuction
  229. * with the FIPS provider we pretend that it is.
  230. */
  231. { "XOR", "provider=tls-provider,fips=yes", xor_keyexch_functions },
  232. { NULL, NULL, NULL }
  233. };
  234. /*
  235. * Dummy "XOR" Key Encapsulation Method. We just build a KEM over the xor KEX.
  236. * Don't use this!
  237. */
  238. static int xor_encapsulate(void *vpxorctx,
  239. unsigned char *ct, size_t *ctlen,
  240. unsigned char *ss, size_t *sslen)
  241. {
  242. /*
  243. * We are building this around a KEX:
  244. *
  245. * 1. we generate ephemeral keypair
  246. * 2. we encode our ephemeral pubkey as the outgoing ct
  247. * 3. we derive using our ephemeral privkey in combination with the peer
  248. * pubkey from the ctx; the result is our ss.
  249. */
  250. int rv = 0;
  251. void *genctx = NULL, *derivectx = NULL;
  252. XORKEY *ourkey = NULL;
  253. PROV_XOR_CTX *pxorctx = vpxorctx;
  254. if (ct == NULL || ss == NULL) {
  255. /* Just return sizes */
  256. if (ctlen == NULL && sslen == NULL)
  257. return 0;
  258. if (ctlen != NULL)
  259. *ctlen = XOR_KEY_SIZE;
  260. if (sslen != NULL)
  261. *sslen = XOR_KEY_SIZE;
  262. return 1;
  263. }
  264. /* 1. Generate keypair */
  265. genctx = xor_gen_init(pxorctx->provctx, OSSL_KEYMGMT_SELECT_KEYPAIR);
  266. if (genctx == NULL)
  267. goto end;
  268. ourkey = xor_gen(genctx, NULL, NULL);
  269. if (ourkey == NULL)
  270. goto end;
  271. /* 2. Encode ephemeral pubkey as ct */
  272. memcpy(ct, ourkey->pubkey, XOR_KEY_SIZE);
  273. *ctlen = XOR_KEY_SIZE;
  274. /* 3. Derive ss via KEX */
  275. derivectx = xor_newctx(pxorctx->provctx);
  276. if (derivectx == NULL
  277. || !xor_init(derivectx, ourkey)
  278. || !xor_set_peer(derivectx, pxorctx->key)
  279. || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE))
  280. goto end;
  281. rv = 1;
  282. end:
  283. xor_gen_cleanup(genctx);
  284. xor_freedata(ourkey);
  285. xor_freectx(derivectx);
  286. return rv;
  287. }
  288. static int xor_decapsulate(void *vpxorctx,
  289. unsigned char *ss, size_t *sslen,
  290. const unsigned char *ct, size_t ctlen)
  291. {
  292. /*
  293. * We are building this around a KEX:
  294. *
  295. * - ct is our peer's pubkey
  296. * - decapsulate is just derive.
  297. */
  298. int rv = 0;
  299. void *derivectx = NULL;
  300. XORKEY *peerkey = NULL;
  301. PROV_XOR_CTX *pxorctx = vpxorctx;
  302. if (ss == NULL) {
  303. /* Just return size */
  304. if (sslen == NULL)
  305. return 0;
  306. *sslen = XOR_KEY_SIZE;
  307. return 1;
  308. }
  309. if (ctlen != XOR_KEY_SIZE)
  310. return 0;
  311. peerkey = xor_newdata(pxorctx->provctx);
  312. if (peerkey == NULL)
  313. goto end;
  314. memcpy(peerkey->pubkey, ct, XOR_KEY_SIZE);
  315. /* Derive ss via KEX */
  316. derivectx = xor_newctx(pxorctx->provctx);
  317. if (derivectx == NULL
  318. || !xor_init(derivectx, pxorctx->key)
  319. || !xor_set_peer(derivectx, peerkey)
  320. || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE))
  321. goto end;
  322. rv = 1;
  323. end:
  324. xor_freedata(peerkey);
  325. xor_freectx(derivectx);
  326. return rv;
  327. }
  328. static const OSSL_DISPATCH xor_kem_functions[] = {
  329. { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))xor_newctx },
  330. { OSSL_FUNC_KEM_FREECTX, (void (*)(void))xor_freectx },
  331. { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))xor_dupctx },
  332. { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (void (*)(void))xor_init },
  333. { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))xor_encapsulate },
  334. { OSSL_FUNC_KEM_DECAPSULATE_INIT, (void (*)(void))xor_init },
  335. { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))xor_decapsulate },
  336. { 0, NULL }
  337. };
  338. static const OSSL_ALGORITHM tls_prov_kem[] = {
  339. /*
  340. * Obviously this is not FIPS approved, but in order to test in conjuction
  341. * with the FIPS provider we pretend that it is.
  342. */
  343. { "XOR", "provider=tls-provider,fips=yes", xor_kem_functions },
  344. { NULL, NULL, NULL }
  345. };
  346. /* Key Management for the dummy XOR key exchange algorithm */
  347. static void *xor_newdata(void *provctx)
  348. {
  349. return OPENSSL_zalloc(sizeof(XORKEY));
  350. }
  351. static void xor_freedata(void *keydata)
  352. {
  353. OPENSSL_free(keydata);
  354. }
  355. static int xor_has(const void *vkey, int selection)
  356. {
  357. const XORKEY *key = vkey;
  358. int ok = 0;
  359. if (key != NULL) {
  360. ok = 1;
  361. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  362. ok = ok && key->haspubkey;
  363. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  364. ok = ok && key->hasprivkey;
  365. }
  366. return ok;
  367. }
  368. static int xor_copy(void *vtokey, const void *vfromkey, int selection)
  369. {
  370. XORKEY *tokey = vtokey;
  371. const XORKEY *fromkey = vfromkey;
  372. int ok = 0;
  373. if (tokey != NULL && fromkey != NULL) {
  374. ok = 1;
  375. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  376. if (fromkey->haspubkey) {
  377. memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
  378. tokey->haspubkey = 1;
  379. } else {
  380. tokey->haspubkey = 0;
  381. }
  382. }
  383. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  384. if (fromkey->hasprivkey) {
  385. memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
  386. tokey->hasprivkey = 1;
  387. } else {
  388. tokey->hasprivkey = 0;
  389. }
  390. }
  391. }
  392. return ok;
  393. }
  394. static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
  395. {
  396. XORKEY *key = vkey;
  397. OSSL_PARAM *p;
  398. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  399. && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
  400. return 0;
  401. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  402. && !OSSL_PARAM_set_int(p, xor_group.secbits))
  403. return 0;
  404. if ((p = OSSL_PARAM_locate(params,
  405. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
  406. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  407. return 0;
  408. p->return_size = XOR_KEY_SIZE;
  409. if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
  410. memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
  411. }
  412. return 1;
  413. }
  414. static const OSSL_PARAM xor_params[] = {
  415. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  416. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  417. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  418. OSSL_PARAM_END
  419. };
  420. static const OSSL_PARAM *xor_gettable_params(void *provctx)
  421. {
  422. return xor_params;
  423. }
  424. static int xor_set_params(void *vkey, const OSSL_PARAM params[])
  425. {
  426. XORKEY *key = vkey;
  427. const OSSL_PARAM *p;
  428. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  429. if (p != NULL) {
  430. if (p->data_type != OSSL_PARAM_OCTET_STRING
  431. || p->data_size != XOR_KEY_SIZE)
  432. return 0;
  433. memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
  434. key->haspubkey = 1;
  435. }
  436. return 1;
  437. }
  438. static const OSSL_PARAM xor_known_settable_params[] = {
  439. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  440. OSSL_PARAM_END
  441. };
  442. static const OSSL_PARAM *xor_settable_params(void *provctx)
  443. {
  444. return xor_known_settable_params;
  445. }
  446. struct xor_gen_ctx {
  447. int selection;
  448. OSSL_LIB_CTX *libctx;
  449. };
  450. static void *xor_gen_init(void *provctx, int selection)
  451. {
  452. struct xor_gen_ctx *gctx = NULL;
  453. if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
  454. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
  455. return NULL;
  456. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
  457. gctx->selection = selection;
  458. /* Our provctx is really just an OSSL_LIB_CTX */
  459. gctx->libctx = (OSSL_LIB_CTX *)provctx;
  460. return gctx;
  461. }
  462. static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
  463. {
  464. struct xor_gen_ctx *gctx = genctx;
  465. const OSSL_PARAM *p;
  466. if (gctx == NULL)
  467. return 0;
  468. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  469. if (p != NULL) {
  470. if (p->data_type != OSSL_PARAM_UTF8_STRING
  471. || (strcmp(p->data, XORGROUP_NAME_INTERNAL) != 0
  472. && strcmp(p->data, XORKEMGROUP_NAME_INTERNAL) != 0))
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. static const OSSL_PARAM *xor_gen_settable_params(void *provctx)
  478. {
  479. static OSSL_PARAM settable[] = {
  480. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  481. OSSL_PARAM_END
  482. };
  483. return settable;
  484. }
  485. static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  486. {
  487. struct xor_gen_ctx *gctx = genctx;
  488. XORKEY *key = OPENSSL_zalloc(sizeof(*key));
  489. size_t i;
  490. if (key == NULL)
  491. return NULL;
  492. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  493. if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE) <= 0) {
  494. OPENSSL_free(key);
  495. return NULL;
  496. }
  497. for (i = 0; i < XOR_KEY_SIZE; i++)
  498. key->pubkey[i] = key->privkey[i] ^ private_constant[i];
  499. key->hasprivkey = 1;
  500. key->haspubkey = 1;
  501. }
  502. return key;
  503. }
  504. static void xor_gen_cleanup(void *genctx)
  505. {
  506. OPENSSL_free(genctx);
  507. }
  508. static const OSSL_DISPATCH xor_keymgmt_functions[] = {
  509. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata },
  510. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
  511. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
  512. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  513. (void (*)(void))xor_gen_settable_params },
  514. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
  515. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
  516. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
  517. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
  518. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
  519. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
  520. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
  521. { OSSL_FUNC_KEYMGMT_COPY, (void (*)(void))xor_copy },
  522. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata },
  523. { 0, NULL }
  524. };
  525. static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
  526. /*
  527. * Obviously this is not FIPS approved, but in order to test in conjuction
  528. * with the FIPS provider we pretend that it is.
  529. */
  530. { "XOR", "provider=tls-provider,fips=yes", xor_keymgmt_functions },
  531. { NULL, NULL, NULL }
  532. };
  533. static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
  534. int *no_cache)
  535. {
  536. *no_cache = 0;
  537. switch (operation_id) {
  538. case OSSL_OP_KEYMGMT:
  539. return tls_prov_keymgmt;
  540. case OSSL_OP_KEYEXCH:
  541. return tls_prov_keyexch;
  542. case OSSL_OP_KEM:
  543. return tls_prov_kem;
  544. }
  545. return NULL;
  546. }
  547. /* Functions we provide to the core */
  548. static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
  549. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
  550. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
  551. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
  552. { 0, NULL }
  553. };
  554. static
  555. unsigned int randomize_tls_group_id(OSSL_LIB_CTX *libctx)
  556. {
  557. /*
  558. * Randomise the group_id we're going to use to ensure we don't interoperate
  559. * with anything but ourselves.
  560. */
  561. unsigned int group_id;
  562. static unsigned int mem[10] = { 0 };
  563. static int in_mem = 0;
  564. int i;
  565. retry:
  566. if (!RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id)))
  567. return 0;
  568. /*
  569. * Ensure group_id is within the IANA Reserved for private use range
  570. * (65024-65279)
  571. */
  572. group_id %= 65279 - 65024;
  573. group_id += 65024;
  574. /* Ensure we did not already issue this group_id */
  575. for (i = 0; i < in_mem; i++)
  576. if (mem[i] == group_id)
  577. goto retry;
  578. /* Add this group_id to the list of ids issued by this function */
  579. mem[in_mem++] = group_id;
  580. return group_id;
  581. }
  582. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  583. const OSSL_DISPATCH *in,
  584. const OSSL_DISPATCH **out,
  585. void **provctx)
  586. {
  587. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  588. *provctx = libctx;
  589. /*
  590. * Randomise the group_id we're going to use to ensure we don't interoperate
  591. * with anything but ourselves.
  592. */
  593. xor_group.group_id = randomize_tls_group_id(libctx);
  594. xor_kemgroup.group_id = randomize_tls_group_id(libctx);
  595. *out = tls_prov_dispatch_table;
  596. return 1;
  597. }