2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/core_names.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/rand.h>
14 #include <openssl/params.h>
15 /* For TLS1_3_VERSION */
16 #include <openssl/ssl.h>
18 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
19 const OSSL_DISPATCH *in,
20 const OSSL_DISPATCH **out,
23 #define XOR_KEY_SIZE 32
26 * Top secret. This algorithm only works if no one knows what this number is.
27 * Please don't tell anyone what it is.
29 * This algorithm is for testing only - don't really use it!
31 static const unsigned char private_constant[XOR_KEY_SIZE] = {
32 0xd3, 0x6b, 0x54, 0xec, 0x5b, 0xac, 0x89, 0x96, 0x8c, 0x2c, 0x66, 0xa5,
33 0x67, 0x0d, 0xe3, 0xdd, 0x43, 0x69, 0xbc, 0x83, 0x3d, 0x60, 0xc7, 0xb8,
34 0x2b, 0x1c, 0x5a, 0xfd, 0xb5, 0xcd, 0xd0, 0xf8
37 typedef struct xorkey_st {
38 unsigned char privkey[XOR_KEY_SIZE];
39 unsigned char pubkey[XOR_KEY_SIZE];
44 /* We define a dummy TLS group called "xorgroup" for test purposes */
46 unsigned int group_id; /* for "tls-group-id", see provider-base(7) */
52 unsigned int is_kem; /* boolean */
55 #define XORGROUP_NAME "xorgroup"
56 #define XORGROUP_NAME_INTERNAL "xorgroup-int"
57 static struct tls_group_st xor_group = {
58 0, /* group_id, set by randomize_tls_group_id() */
60 TLS1_3_VERSION, /* mintls */
67 #define XORKEMGROUP_NAME "xorkemgroup"
68 #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int"
69 static struct tls_group_st xor_kemgroup = {
70 0, /* group_id, set by randomize_tls_group_id() */
72 TLS1_3_VERSION, /* mintls */
79 #define ALGORITHM "XOR"
81 static const OSSL_PARAM xor_group_params[] = {
82 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
83 XORGROUP_NAME, sizeof(XORGROUP_NAME)),
84 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
85 XORGROUP_NAME_INTERNAL,
86 sizeof(XORGROUP_NAME_INTERNAL)),
87 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
89 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_group.group_id),
90 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
92 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_group.mintls),
93 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_group.maxtls),
94 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_group.mindtls),
95 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_group.maxdtls),
96 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_group.is_kem),
100 static const OSSL_PARAM xor_kemgroup_params[] = {
101 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
102 XORKEMGROUP_NAME, sizeof(XORKEMGROUP_NAME)),
103 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
104 XORKEMGROUP_NAME_INTERNAL,
105 sizeof(XORKEMGROUP_NAME_INTERNAL)),
106 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
108 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_kemgroup.group_id),
109 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
110 &xor_kemgroup.secbits),
111 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_kemgroup.mintls),
112 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_kemgroup.maxtls),
113 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_kemgroup.mindtls),
114 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_kemgroup.maxdtls),
115 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_kemgroup.is_kem),
120 static int tls_prov_get_capabilities(void *provctx, const char *capability,
121 OSSL_CALLBACK *cb, void *arg)
123 if (strcmp(capability, "TLS-GROUP") == 0)
124 return cb(xor_group_params, arg)
125 && cb(xor_kemgroup_params, arg);
127 /* We don't support this capability */
132 * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
133 * together. Don't use this!
136 static OSSL_FUNC_keyexch_newctx_fn xor_newctx;
137 static OSSL_FUNC_keyexch_init_fn xor_init;
138 static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer;
139 static OSSL_FUNC_keyexch_derive_fn xor_derive;
140 static OSSL_FUNC_keyexch_freectx_fn xor_freectx;
141 static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx;
148 static void *xor_newctx(void *provctx)
150 PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX));
158 static int xor_init(void *vpxorctx, void *vkey)
160 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
162 if (pxorctx == NULL || vkey == NULL)
168 static int xor_set_peer(void *vpxorctx, void *vpeerkey)
170 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
172 if (pxorctx == NULL || vpeerkey == NULL)
174 pxorctx->peerkey = vpeerkey;
178 static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
181 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
184 if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
187 *secretlen = XOR_KEY_SIZE;
191 if (outlen < XOR_KEY_SIZE)
194 for (i = 0; i < XOR_KEY_SIZE; i++)
195 secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
200 static void xor_freectx(void *pxorctx)
202 OPENSSL_free(pxorctx);
205 static void *xor_dupctx(void *vpxorctx)
207 PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx;
208 PROV_XOR_CTX *dstctx;
210 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
219 static const OSSL_DISPATCH xor_keyexch_functions[] = {
220 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx },
221 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
222 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
223 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
224 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
225 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
229 static const OSSL_ALGORITHM tls_prov_keyexch[] = {
231 * Obviously this is not FIPS approved, but in order to test in conjuction
232 * with the FIPS provider we pretend that it is.
234 { "XOR", "provider=tls-provider,fips=yes", xor_keyexch_functions },
238 /* Key Management for the dummy XOR key exchange algorithm */
240 static OSSL_FUNC_keymgmt_new_fn xor_newdata;
241 static OSSL_FUNC_keymgmt_free_fn xor_freedata;
242 static OSSL_FUNC_keymgmt_has_fn xor_has;
243 static OSSL_FUNC_keymgmt_copy_fn xor_copy;
244 static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init;
245 static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params;
246 static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params;
247 static OSSL_FUNC_keymgmt_gen_fn xor_gen;
248 static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup;
249 static OSSL_FUNC_keymgmt_get_params_fn xor_get_params;
250 static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params;
251 static OSSL_FUNC_keymgmt_set_params_fn xor_set_params;
252 static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params;
254 static void *xor_newdata(void *provctx)
256 return OPENSSL_zalloc(sizeof(XORKEY));
259 static void xor_freedata(void *keydata)
261 OPENSSL_free(keydata);
264 static int xor_has(void *vkey, int selection)
272 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
273 ok = ok && key->haspubkey;
274 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
275 ok = ok && key->hasprivkey;
280 static int xor_copy(void *vtokey, const void *vfromkey, int selection)
282 XORKEY *tokey = vtokey;
283 const XORKEY *fromkey = vfromkey;
286 if (tokey != NULL && fromkey != NULL) {
289 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
290 if (fromkey->haspubkey) {
291 memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
292 tokey->haspubkey = 1;
294 tokey->haspubkey = 0;
297 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
298 if (fromkey->hasprivkey) {
299 memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
300 tokey->hasprivkey = 1;
302 tokey->hasprivkey = 0;
309 static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
314 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
315 && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
318 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
319 && !OSSL_PARAM_set_int(p, xor_group.secbits))
322 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
323 if (p->data_type != OSSL_PARAM_OCTET_STRING)
325 p->return_size = XOR_KEY_SIZE;
326 if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
327 memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
333 static const OSSL_PARAM xor_params[] = {
334 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
335 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
336 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
340 static const OSSL_PARAM *xor_gettable_params(void *provctx)
345 static int xor_set_params(void *vkey, const OSSL_PARAM params[])
350 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
352 if (p->data_type != OSSL_PARAM_OCTET_STRING
353 || p->data_size != XOR_KEY_SIZE)
355 memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
362 static const OSSL_PARAM xor_known_settable_params[] = {
363 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
367 static const OSSL_PARAM *xor_settable_params(void *provctx)
369 return xor_known_settable_params;
377 static void *xor_gen_init(void *provctx, int selection)
379 struct xor_gen_ctx *gctx = NULL;
381 if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
382 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
385 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
386 gctx->selection = selection;
388 /* Our provctx is really just an OPENSSL_CTX */
389 gctx->libctx = (OPENSSL_CTX *)provctx;
394 static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
396 struct xor_gen_ctx *gctx = genctx;
402 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
404 if (p->data_type != OSSL_PARAM_UTF8_STRING
405 || (strcmp(p->data, XORGROUP_NAME_INTERNAL) != 0
406 && strcmp(p->data, XORKEMGROUP_NAME_INTERNAL) != 0))
413 static const OSSL_PARAM *xor_gen_settable_params(void *provctx)
415 static OSSL_PARAM settable[] = {
416 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
422 static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
424 struct xor_gen_ctx *gctx = genctx;
425 XORKEY *key = OPENSSL_zalloc(sizeof(*key));
431 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
432 if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE) <= 0) {
436 for (i = 0; i < XOR_KEY_SIZE; i++)
437 key->pubkey[i] = key->privkey[i] ^ private_constant[i];
445 static void xor_gen_cleanup(void *genctx)
447 OPENSSL_free(genctx);
450 static const OSSL_DISPATCH xor_keymgmt_functions[] = {
451 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata },
452 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
453 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
454 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
455 (void (*)(void))xor_gen_settable_params },
456 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
457 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
458 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
459 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
460 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
461 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
462 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
463 { OSSL_FUNC_KEYMGMT_COPY, (void (*)(void))xor_copy },
464 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata },
468 static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
470 * Obviously this is not FIPS approved, but in order to test in conjuction
471 * with the FIPS provider we pretend that it is.
473 { "XOR", "provider=tls-provider,fips=yes", xor_keymgmt_functions },
477 static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
481 switch (operation_id) {
482 case OSSL_OP_KEYMGMT:
483 return tls_prov_keymgmt;
484 case OSSL_OP_KEYEXCH:
485 return tls_prov_keyexch;
490 /* Functions we provide to the core */
491 static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
492 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
493 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
494 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
499 unsigned int randomize_tls_group_id(OPENSSL_CTX *libctx)
502 * Randomise the group_id we're going to use to ensure we don't interoperate
503 * with anything but ourselves.
505 unsigned int group_id;
506 static unsigned int mem[10] = { 0 };
507 static int in_mem = 0;
511 if (!RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id)))
514 * Ensure group_id is within the IANA Reserved for private use range
517 group_id %= 65279 - 65024;
520 /* Ensure we did not already issue this group_id */
521 for (i = 0; i < in_mem; i++)
522 if (mem[i] == group_id)
525 /* Add this group_id to the list of ids issued by this function */
526 mem[in_mem++] = group_id;
531 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
532 const OSSL_DISPATCH *in,
533 const OSSL_DISPATCH **out,
536 OPENSSL_CTX *libctx = OPENSSL_CTX_new();
541 * Randomise the group_id we're going to use to ensure we don't interoperate
542 * with anything but ourselves.
544 xor_group.group_id = randomize_tls_group_id(libctx);
545 xor_kemgroup.group_id = randomize_tls_group_id(libctx);
547 *out = tls_prov_dispatch_table;