tls-provider.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. /* We define a dummy TLS group called "xorgroup" for test purposes */
  39. static unsigned int group_id = 0; /* IANA reserved for private use */
  40. static unsigned int secbits = 128;
  41. static unsigned int mintls = TLS1_3_VERSION;
  42. static unsigned int maxtls = 0;
  43. static unsigned int mindtls = -1;
  44. static unsigned int maxdtls = -1;
  45. #define GROUP_NAME "xorgroup"
  46. #define GROUP_NAME_INTERNAL "xorgroup-int"
  47. #define ALGORITHM "XOR"
  48. static const OSSL_PARAM xor_group_params[] = {
  49. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
  50. GROUP_NAME, sizeof(GROUP_NAME)),
  51. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
  52. GROUP_NAME_INTERNAL, sizeof(GROUP_NAME_INTERNAL)),
  53. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
  54. sizeof(ALGORITHM)),
  55. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &group_id),
  56. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS, &secbits),
  57. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &mintls),
  58. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &maxtls),
  59. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &mindtls),
  60. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &maxdtls),
  61. OSSL_PARAM_END
  62. };
  63. static int tls_prov_get_capabilities(void *provctx, const char *capability,
  64. OSSL_CALLBACK *cb, void *arg)
  65. {
  66. /* We're only adding one group so we only call the callback once */
  67. if (strcmp(capability, "TLS-GROUP") == 0)
  68. return cb(xor_group_params, arg);
  69. /* We don't support this capability */
  70. return 0;
  71. }
  72. /*
  73. * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
  74. * together. Don't use this!
  75. */
  76. static OSSL_FUNC_keyexch_newctx_fn xor_newctx;
  77. static OSSL_FUNC_keyexch_init_fn xor_init;
  78. static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer;
  79. static OSSL_FUNC_keyexch_derive_fn xor_derive;
  80. static OSSL_FUNC_keyexch_freectx_fn xor_freectx;
  81. static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx;
  82. typedef struct {
  83. XORKEY *key;
  84. XORKEY *peerkey;
  85. } PROV_XOR_CTX;
  86. static void *xor_newctx(void *provctx)
  87. {
  88. PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX));
  89. if (pxorctx == NULL)
  90. return NULL;
  91. return pxorctx;
  92. }
  93. static int xor_init(void *vpxorctx, void *vkey)
  94. {
  95. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  96. if (pxorctx == NULL || vkey == NULL)
  97. return 0;
  98. pxorctx->key = vkey;
  99. return 1;
  100. }
  101. static int xor_set_peer(void *vpxorctx, void *vpeerkey)
  102. {
  103. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  104. if (pxorctx == NULL || vpeerkey == NULL)
  105. return 0;
  106. pxorctx->peerkey = vpeerkey;
  107. return 1;
  108. }
  109. static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
  110. size_t outlen)
  111. {
  112. PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
  113. int i;
  114. if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
  115. return 0;
  116. *secretlen = XOR_KEY_SIZE;
  117. if (secret == NULL)
  118. return 1;
  119. if (outlen < XOR_KEY_SIZE)
  120. return 0;
  121. for (i = 0; i < XOR_KEY_SIZE; i++)
  122. secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
  123. return 1;
  124. }
  125. static void xor_freectx(void *pxorctx)
  126. {
  127. OPENSSL_free(pxorctx);
  128. }
  129. static void *xor_dupctx(void *vpxorctx)
  130. {
  131. PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx;
  132. PROV_XOR_CTX *dstctx;
  133. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  134. if (dstctx == NULL)
  135. return NULL;
  136. *dstctx = *srcctx;
  137. return dstctx;
  138. }
  139. static const OSSL_DISPATCH xor_keyexch_functions[] = {
  140. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx },
  141. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
  142. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
  143. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
  144. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
  145. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
  146. { 0, NULL }
  147. };
  148. static const OSSL_ALGORITHM tls_prov_keyexch[] = {
  149. { "XOR", "provider=tls-provider", xor_keyexch_functions },
  150. { NULL, NULL, NULL }
  151. };
  152. /* Key Management for the dummy XOR key exchange algorithm */
  153. static OSSL_FUNC_keymgmt_new_fn xor_newdata;
  154. static OSSL_FUNC_keymgmt_free_fn xor_freedata;
  155. static OSSL_FUNC_keymgmt_has_fn xor_has;
  156. static OSSL_FUNC_keymgmt_copy_fn xor_copy;
  157. static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init;
  158. static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params;
  159. static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params;
  160. static OSSL_FUNC_keymgmt_gen_fn xor_gen;
  161. static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup;
  162. static OSSL_FUNC_keymgmt_get_params_fn xor_get_params;
  163. static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params;
  164. static OSSL_FUNC_keymgmt_set_params_fn xor_set_params;
  165. static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params;
  166. static void *xor_newdata(void *provctx)
  167. {
  168. return OPENSSL_zalloc(sizeof(XORKEY));
  169. }
  170. static void xor_freedata(void *keydata)
  171. {
  172. OPENSSL_free(keydata);
  173. }
  174. static int xor_has(void *vkey, int selection)
  175. {
  176. XORKEY *key = vkey;
  177. int ok = 0;
  178. if (key != NULL) {
  179. ok = 1;
  180. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  181. ok = ok && key->haspubkey;
  182. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  183. ok = ok && key->hasprivkey;
  184. }
  185. return ok;
  186. }
  187. static int xor_copy(void *vtokey, const void *vfromkey, int selection)
  188. {
  189. XORKEY *tokey = vtokey;
  190. const XORKEY *fromkey = vfromkey;
  191. int ok = 0;
  192. if (tokey != NULL && fromkey != NULL) {
  193. ok = 1;
  194. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  195. if (fromkey->haspubkey) {
  196. memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
  197. tokey->haspubkey = 1;
  198. } else {
  199. tokey->haspubkey = 0;
  200. }
  201. }
  202. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  203. if (fromkey->hasprivkey) {
  204. memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
  205. tokey->hasprivkey = 1;
  206. } else {
  207. tokey->hasprivkey = 0;
  208. }
  209. }
  210. }
  211. return ok;
  212. }
  213. static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
  214. {
  215. XORKEY *key = vkey;
  216. OSSL_PARAM *p;
  217. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  218. && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
  219. return 0;
  220. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  221. && !OSSL_PARAM_set_int(p, secbits))
  222. return 0;
  223. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
  224. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  225. return 0;
  226. p->return_size = XOR_KEY_SIZE;
  227. if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
  228. memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
  229. }
  230. return 1;
  231. }
  232. static const OSSL_PARAM xor_params[] = {
  233. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  234. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  235. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
  236. OSSL_PARAM_END
  237. };
  238. static const OSSL_PARAM *xor_gettable_params(void)
  239. {
  240. return xor_params;
  241. }
  242. static int xor_set_params(void *vkey, const OSSL_PARAM params[])
  243. {
  244. XORKEY *key = vkey;
  245. const OSSL_PARAM *p;
  246. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
  247. if (p != NULL) {
  248. if (p->data_type != OSSL_PARAM_OCTET_STRING
  249. || p->data_size != XOR_KEY_SIZE)
  250. return 0;
  251. memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
  252. key->haspubkey = 1;
  253. }
  254. return 1;
  255. }
  256. static const OSSL_PARAM xor_known_settable_params[] = {
  257. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
  258. OSSL_PARAM_END
  259. };
  260. static const OSSL_PARAM *xor_settable_params(void)
  261. {
  262. return xor_known_settable_params;
  263. }
  264. struct xor_gen_ctx {
  265. int selection;
  266. OPENSSL_CTX *libctx;
  267. };
  268. static void *xor_gen_init(void *provctx, int selection)
  269. {
  270. struct xor_gen_ctx *gctx = NULL;
  271. if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
  272. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
  273. return NULL;
  274. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
  275. gctx->selection = selection;
  276. /* Our provctx is really just an OPENSSL_CTX */
  277. gctx->libctx = (OPENSSL_CTX *)provctx;
  278. return gctx;
  279. }
  280. static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
  281. {
  282. struct xor_gen_ctx *gctx = genctx;
  283. const OSSL_PARAM *p;
  284. if (gctx == NULL)
  285. return 0;
  286. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  287. if (p != NULL) {
  288. if (p->data_type != OSSL_PARAM_UTF8_STRING
  289. || strcmp(p->data, GROUP_NAME_INTERNAL) != 0)
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. static const OSSL_PARAM *xor_gen_settable_params(void *provctx)
  295. {
  296. static OSSL_PARAM settable[] = {
  297. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  298. OSSL_PARAM_END
  299. };
  300. return settable;
  301. }
  302. static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  303. {
  304. struct xor_gen_ctx *gctx = genctx;
  305. XORKEY *key = OPENSSL_zalloc(sizeof(*key));
  306. size_t i;
  307. if (key == NULL)
  308. return NULL;
  309. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  310. if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE) <= 0) {
  311. OPENSSL_free(key);
  312. return NULL;
  313. }
  314. for (i = 0; i < XOR_KEY_SIZE; i++)
  315. key->pubkey[i] = key->privkey[i] ^ private_constant[i];
  316. key->hasprivkey = 1;
  317. key->haspubkey = 1;
  318. }
  319. return key;
  320. }
  321. static void xor_gen_cleanup(void *genctx)
  322. {
  323. OPENSSL_free(genctx);
  324. }
  325. static const OSSL_DISPATCH xor_keymgmt_functions[] = {
  326. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata },
  327. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
  328. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
  329. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  330. (void (*)(void))xor_gen_settable_params },
  331. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
  332. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
  333. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
  334. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
  335. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
  336. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
  337. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
  338. { OSSL_FUNC_KEYMGMT_COPY, (void (*)(void))xor_copy },
  339. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata },
  340. { 0, NULL }
  341. };
  342. static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
  343. { "XOR", "provider=tls-provider", xor_keymgmt_functions },
  344. { NULL, NULL, NULL }
  345. };
  346. static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
  347. int *no_cache)
  348. {
  349. *no_cache = 0;
  350. switch (operation_id) {
  351. case OSSL_OP_KEYMGMT:
  352. return tls_prov_keymgmt;
  353. case OSSL_OP_KEYEXCH:
  354. return tls_prov_keyexch;
  355. }
  356. return NULL;
  357. }
  358. /* Functions we provide to the core */
  359. static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
  360. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
  361. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
  362. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
  363. { 0, NULL }
  364. };
  365. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  366. const OSSL_DISPATCH *in,
  367. const OSSL_DISPATCH **out,
  368. void **provctx)
  369. {
  370. OPENSSL_CTX *libctx = OPENSSL_CTX_new();
  371. *provctx = libctx;
  372. /*
  373. * Randomise the group_id we're going to use to ensure we don't interoperate
  374. * with anything but ourselves.
  375. */
  376. if (!RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id)))
  377. return 0;
  378. /*
  379. * Ensure group_id is within the IANA Reserved for private use range
  380. * (65024-65279)
  381. */
  382. group_id %= 65279 - 65024;
  383. group_id += 65024;
  384. *out = tls_prov_dispatch_table;
  385. return 1;
  386. }