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_numbers.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 static unsigned int group_id = 0; /* IANA reserved for private use */
47 static unsigned int secbits = 128;
48 static unsigned int mintls = TLS1_3_VERSION;
49 static unsigned int maxtls = 0;
50 static unsigned int mindtls = -1;
51 static unsigned int maxdtls = -1;
53 #define GROUP_NAME "xorgroup"
54 #define GROUP_NAME_INTERNAL "xorgroup-int"
55 #define ALGORITHM "XOR"
57 static const OSSL_PARAM xor_group_params[] = {
58 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
59 GROUP_NAME, sizeof(GROUP_NAME)),
60 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
61 GROUP_NAME_INTERNAL, sizeof(GROUP_NAME_INTERNAL)),
62 OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
64 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &group_id),
65 OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS, &secbits),
66 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &mintls),
67 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &maxtls),
68 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &mindtls),
69 OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &maxdtls),
73 static int tls_prov_get_capabilities(void *provctx, const char *capability,
74 OSSL_CALLBACK *cb, void *arg)
76 /* We're only adding one group so we only call the callback once */
77 if (strcmp(capability, "TLS-GROUP") == 0)
78 return cb(xor_group_params, arg);
80 /* We don't support this capability */
85 * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
86 * together. Don't use this!
89 static OSSL_OP_keyexch_newctx_fn xor_newctx;
90 static OSSL_OP_keyexch_init_fn xor_init;
91 static OSSL_OP_keyexch_set_peer_fn xor_set_peer;
92 static OSSL_OP_keyexch_derive_fn xor_derive;
93 static OSSL_OP_keyexch_freectx_fn xor_freectx;
94 static OSSL_OP_keyexch_dupctx_fn xor_dupctx;
101 static void *xor_newctx(void *provctx)
103 PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX));
111 static int xor_init(void *vpxorctx, void *vkey)
113 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
115 if (pxorctx == NULL || vkey == NULL)
121 static int xor_set_peer(void *vpxorctx, void *vpeerkey)
123 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
125 if (pxorctx == NULL || vpeerkey == NULL)
127 pxorctx->peerkey = vpeerkey;
131 static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
134 PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
137 if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
140 *secretlen = XOR_KEY_SIZE;
144 if (outlen < XOR_KEY_SIZE)
147 for (i = 0; i < XOR_KEY_SIZE; i++)
148 secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
153 static void xor_freectx(void *pxorctx)
155 OPENSSL_free(pxorctx);
158 static void *xor_dupctx(void *vpxorctx)
160 PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx;
161 PROV_XOR_CTX *dstctx;
163 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
172 static const OSSL_DISPATCH xor_keyexch_functions[] = {
173 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx },
174 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
175 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
176 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
177 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
178 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
182 static const OSSL_ALGORITHM tls_prov_keyexch[] = {
183 { "XOR", "provider=tls-provider", xor_keyexch_functions },
187 /* Key Management for the dummy XOR key exchange algorithm */
189 static OSSL_OP_keymgmt_new_fn xor_newdata;
190 static OSSL_OP_keymgmt_free_fn xor_freedata;
191 static OSSL_OP_keymgmt_has_fn xor_has;
192 static OSSL_OP_keymgmt_copy_fn xor_copy;
193 static OSSL_OP_keymgmt_gen_init_fn xor_gen_init;
194 static OSSL_OP_keymgmt_gen_set_params_fn xor_gen_set_params;
195 static OSSL_OP_keymgmt_gen_settable_params_fn xor_gen_settable_params;
196 static OSSL_OP_keymgmt_gen_fn xor_gen;
197 static OSSL_OP_keymgmt_gen_cleanup_fn xor_gen_cleanup;
198 static OSSL_OP_keymgmt_get_params_fn xor_get_params;
199 static OSSL_OP_keymgmt_gettable_params_fn xor_gettable_params;
200 static OSSL_OP_keymgmt_set_params_fn xor_set_params;
201 static OSSL_OP_keymgmt_settable_params_fn xor_settable_params;
203 static void *xor_newdata(void *provctx)
205 return OPENSSL_zalloc(sizeof(XORKEY));
208 static void xor_freedata(void *keydata)
210 OPENSSL_free(keydata);
213 static int xor_has(void *vkey, int selection)
221 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
222 ok = ok && key->haspubkey;
223 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
224 ok = ok && key->hasprivkey;
229 static int xor_copy(void *vtokey, const void *vfromkey, int selection)
231 XORKEY *tokey = vtokey;
232 const XORKEY *fromkey = vfromkey;
235 if (tokey != NULL && fromkey != NULL) {
238 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
239 if (fromkey->haspubkey) {
240 memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
241 tokey->haspubkey = 1;
243 tokey->haspubkey = 0;
246 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
247 if (fromkey->hasprivkey) {
248 memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
249 tokey->hasprivkey = 1;
251 tokey->hasprivkey = 0;
258 static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
263 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
264 && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
267 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
268 && !OSSL_PARAM_set_int(p, secbits))
271 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
272 if (p->data_type != OSSL_PARAM_OCTET_STRING)
274 p->return_size = XOR_KEY_SIZE;
275 if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
276 memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
282 static const OSSL_PARAM xor_params[] = {
283 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
284 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
285 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
289 static const OSSL_PARAM *xor_gettable_params(void)
294 static int xor_set_params(void *vkey, const OSSL_PARAM params[])
299 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
301 if (p->data_type != OSSL_PARAM_OCTET_STRING
302 || p->data_size != XOR_KEY_SIZE)
304 memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
311 static const OSSL_PARAM xor_known_settable_params[] = {
312 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
316 static const OSSL_PARAM *xor_settable_params(void)
318 return xor_known_settable_params;
326 static void *xor_gen_init(void *provctx, int selection)
328 struct xor_gen_ctx *gctx = NULL;
330 if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
331 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
334 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
335 gctx->selection = selection;
337 /* Our provctx is really just an OPENSSL_CTX */
338 gctx->libctx = (OPENSSL_CTX *)provctx;
343 static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
345 struct xor_gen_ctx *gctx = genctx;
351 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
353 if (p->data_type != OSSL_PARAM_UTF8_STRING
354 || strcmp(p->data, GROUP_NAME_INTERNAL) != 0)
361 static const OSSL_PARAM *xor_gen_settable_params(void *provctx)
363 static OSSL_PARAM settable[] = {
364 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
370 static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
372 struct xor_gen_ctx *gctx = genctx;
373 XORKEY *key = OPENSSL_zalloc(sizeof(*key));
379 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
380 if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE) <= 0) {
384 for (i = 0; i < XOR_KEY_SIZE; i++)
385 key->pubkey[i] = key->privkey[i] ^ private_constant[i];
393 static void xor_gen_cleanup(void *genctx)
395 OPENSSL_free(genctx);
398 static const OSSL_DISPATCH xor_keymgmt_functions[] = {
399 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata },
400 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
401 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
402 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
403 (void (*)(void))xor_gen_settable_params },
404 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
405 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
406 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
407 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
408 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
409 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
410 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
411 { OSSL_FUNC_KEYMGMT_COPY, (void (*)(void))xor_copy },
412 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata },
416 static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
417 { "XOR", "provider=tls-provider", xor_keymgmt_functions },
421 static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
425 switch (operation_id) {
426 case OSSL_OP_KEYMGMT:
427 return tls_prov_keymgmt;
428 case OSSL_OP_KEYEXCH:
429 return tls_prov_keyexch;
434 /* Functions we provide to the core */
435 static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
436 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
437 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
438 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
442 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
443 const OSSL_DISPATCH *in,
444 const OSSL_DISPATCH **out,
447 OPENSSL_CTX *libctx = OPENSSL_CTX_new();
452 * Randomise the group_id we're going to use to ensure we don't interoperate
453 * with anything but ourselves.
455 if (!RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id)))
458 * Ensure group_id is within the IANA Reserved for private use range
461 group_id %= 65279 - 65024;
464 *out = tls_prov_dispatch_table;