[test][tls-provider] Add 2nd pluggable tls group for KEM
[openssl.git] / test / tls-provider.c
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
10 #include <string.h>
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>
17
18 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
19                       const OSSL_DISPATCH *in,
20                       const OSSL_DISPATCH **out,
21                       void **provctx);
22
23 #define XOR_KEY_SIZE 32
24
25 /*
26  * Top secret. This algorithm only works if no one knows what this number is.
27  * Please don't tell anyone what it is.
28  * 
29  * This algorithm is for testing only - don't really use it!
30  */
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
35 };
36
37 typedef struct xorkey_st {
38     unsigned char privkey[XOR_KEY_SIZE];
39     unsigned char pubkey[XOR_KEY_SIZE];
40     int hasprivkey;
41     int haspubkey;
42 } XORKEY;
43
44 /* We define a dummy TLS group called "xorgroup" for test purposes */
45 struct tls_group_st {
46     unsigned int group_id; /* for "tls-group-id", see provider-base(7) */
47     unsigned int secbits;
48     unsigned int mintls;
49     unsigned int maxtls;
50     unsigned int mindtls;
51     unsigned int maxdtls;
52 };
53
54 #define XORGROUP_NAME "xorgroup"
55 #define XORGROUP_NAME_INTERNAL "xorgroup-int"
56 static struct tls_group_st xor_group = {
57     0,                  /* group_id, set by randomize_tls_group_id() */
58     128,                /* secbits */
59     TLS1_3_VERSION,     /* mintls */
60     0,                  /* maxtls */
61     -1,                 /* mindtls */
62     -1                  /* maxdtls */
63 };
64
65 #define XORKEMGROUP_NAME "xorkemgroup"
66 #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int"
67 static struct tls_group_st xor_kemgroup = {
68     0,                  /* group_id, set by randomize_tls_group_id() */
69     128,                /* secbits */
70     TLS1_3_VERSION,     /* mintls */
71     0,                  /* maxtls */
72     -1,                 /* mindtls */
73     -1                  /* maxdtls */
74 };
75
76 #define ALGORITHM "XOR"
77
78 static const OSSL_PARAM xor_group_params[] = {
79     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
80                            XORGROUP_NAME, sizeof(XORGROUP_NAME)),
81     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
82                            XORGROUP_NAME_INTERNAL,
83                            sizeof(XORGROUP_NAME_INTERNAL)),
84     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
85                            sizeof(ALGORITHM)),
86     OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_group.group_id),
87     OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
88                     &xor_group.secbits),
89     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_group.mintls),
90     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_group.maxtls),
91     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_group.mindtls),
92     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_group.maxdtls),
93     OSSL_PARAM_END
94 };
95
96 static const OSSL_PARAM xor_kemgroup_params[] = {
97     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
98                            XORKEMGROUP_NAME, sizeof(XORKEMGROUP_NAME)),
99     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
100                            XORKEMGROUP_NAME_INTERNAL,
101                            sizeof(XORKEMGROUP_NAME_INTERNAL)),
102     OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
103                            sizeof(ALGORITHM)),
104     OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_kemgroup.group_id),
105     OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
106                     &xor_kemgroup.secbits),
107     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_kemgroup.mintls),
108     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_kemgroup.maxtls),
109     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_kemgroup.mindtls),
110     OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_kemgroup.maxdtls),
111     OSSL_PARAM_END
112 };
113
114
115 static int tls_prov_get_capabilities(void *provctx, const char *capability,
116                                      OSSL_CALLBACK *cb, void *arg)
117 {
118     if (strcmp(capability, "TLS-GROUP") == 0)
119         return cb(xor_group_params, arg)
120             && cb(xor_kemgroup_params, arg);
121
122     /* We don't support this capability */
123     return 0;
124 }
125
126 /*
127  * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
128  * together. Don't use this!
129  */
130
131 static OSSL_FUNC_keyexch_newctx_fn xor_newctx;
132 static OSSL_FUNC_keyexch_init_fn xor_init;
133 static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer;
134 static OSSL_FUNC_keyexch_derive_fn xor_derive;
135 static OSSL_FUNC_keyexch_freectx_fn xor_freectx;
136 static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx;
137
138 typedef struct {
139     XORKEY *key;
140     XORKEY *peerkey;
141 } PROV_XOR_CTX;
142
143 static void *xor_newctx(void *provctx)
144 {
145     PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX));
146
147     if (pxorctx == NULL)
148         return NULL;
149
150     return pxorctx;
151 }
152
153 static int xor_init(void *vpxorctx, void *vkey)
154 {
155     PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
156
157     if (pxorctx == NULL || vkey == NULL)
158         return 0;
159     pxorctx->key = vkey;
160     return 1;
161 }
162
163 static int xor_set_peer(void *vpxorctx, void *vpeerkey)
164 {
165     PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
166
167     if (pxorctx == NULL || vpeerkey == NULL)
168         return 0;
169     pxorctx->peerkey = vpeerkey;
170     return 1;
171 }
172
173 static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
174                       size_t outlen)
175 {
176     PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx;
177     int i;
178
179     if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
180         return 0;
181
182     *secretlen = XOR_KEY_SIZE;
183     if (secret == NULL)
184         return 1;
185
186     if (outlen < XOR_KEY_SIZE)
187         return 0;
188
189     for (i = 0; i < XOR_KEY_SIZE; i++)
190         secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
191
192     return 1;
193 }
194
195 static void xor_freectx(void *pxorctx)
196 {
197     OPENSSL_free(pxorctx);
198 }
199
200 static void *xor_dupctx(void *vpxorctx)
201 {
202     PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx;
203     PROV_XOR_CTX *dstctx;
204
205     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
206     if (dstctx == NULL)
207         return NULL;
208
209     *dstctx = *srcctx;
210
211     return dstctx;
212 }
213
214 static const OSSL_DISPATCH xor_keyexch_functions[] = {
215     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx },
216     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
217     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
218     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
219     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
220     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
221     { 0, NULL }
222 };
223
224 static const OSSL_ALGORITHM tls_prov_keyexch[] = {
225     /*
226      * Obviously this is not FIPS approved, but in order to test in conjuction
227      * with the FIPS provider we pretend that it is.
228      */
229     { "XOR", "provider=tls-provider,fips=yes", xor_keyexch_functions },
230     { NULL, NULL, NULL }
231 };
232
233 /* Key Management for the dummy XOR key exchange algorithm */
234
235 static OSSL_FUNC_keymgmt_new_fn xor_newdata;
236 static OSSL_FUNC_keymgmt_free_fn xor_freedata;
237 static OSSL_FUNC_keymgmt_has_fn xor_has;
238 static OSSL_FUNC_keymgmt_copy_fn xor_copy;
239 static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init;
240 static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params;
241 static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params;
242 static OSSL_FUNC_keymgmt_gen_fn xor_gen;
243 static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup;
244 static OSSL_FUNC_keymgmt_get_params_fn xor_get_params;
245 static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params;
246 static OSSL_FUNC_keymgmt_set_params_fn xor_set_params;
247 static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params;
248
249 static void *xor_newdata(void *provctx)
250 {
251     return OPENSSL_zalloc(sizeof(XORKEY));
252 }
253
254 static void xor_freedata(void *keydata)
255 {
256     OPENSSL_free(keydata);
257 }
258
259 static int xor_has(void *vkey, int selection)
260 {
261     XORKEY *key = vkey;
262     int ok = 0;
263
264     if (key != NULL) {
265         ok = 1;
266
267         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
268             ok = ok && key->haspubkey;
269         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
270             ok = ok && key->hasprivkey;
271     }
272     return ok;
273 }
274
275 static int xor_copy(void *vtokey, const void *vfromkey, int selection)
276 {
277     XORKEY *tokey = vtokey;
278     const XORKEY *fromkey = vfromkey;
279     int ok = 0;
280
281     if (tokey != NULL && fromkey != NULL) {
282         ok = 1;
283
284         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
285             if (fromkey->haspubkey) {
286                 memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
287                 tokey->haspubkey = 1;
288             } else {
289                 tokey->haspubkey = 0;
290             }
291         }
292         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
293             if (fromkey->hasprivkey) {
294                 memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
295                 tokey->hasprivkey = 1;
296             } else {
297                 tokey->hasprivkey = 0;
298             }
299         }
300     }
301     return ok;
302 }
303
304 static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
305 {
306     XORKEY *key = vkey;
307     OSSL_PARAM *p;
308
309     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
310         && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
311         return 0;
312
313     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
314         && !OSSL_PARAM_set_int(p, xor_group.secbits))
315         return 0;
316
317     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
318         if (p->data_type != OSSL_PARAM_OCTET_STRING)
319             return 0;
320         p->return_size = XOR_KEY_SIZE;
321         if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
322             memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
323     }
324
325     return 1;
326 }
327
328 static const OSSL_PARAM xor_params[] = {
329     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
330     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
331     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
332     OSSL_PARAM_END
333 };
334
335 static const OSSL_PARAM *xor_gettable_params(void *provctx)
336 {
337     return xor_params;
338 }
339
340 static int xor_set_params(void *vkey, const OSSL_PARAM params[])
341 {
342     XORKEY *key = vkey;
343     const OSSL_PARAM *p;
344
345     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
346     if (p != NULL) {
347         if (p->data_type != OSSL_PARAM_OCTET_STRING
348                 || p->data_size != XOR_KEY_SIZE)
349             return 0;
350         memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
351         key->haspubkey = 1;
352     }
353
354     return 1;
355 }
356
357 static const OSSL_PARAM xor_known_settable_params[] = {
358     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
359     OSSL_PARAM_END
360 };
361
362 static const OSSL_PARAM *xor_settable_params(void *provctx)
363 {
364     return xor_known_settable_params;
365 }
366
367 struct xor_gen_ctx {
368     int selection;
369     OPENSSL_CTX *libctx;
370 };
371
372 static void *xor_gen_init(void *provctx, int selection)
373 {
374     struct xor_gen_ctx *gctx = NULL;
375
376     if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
377                       | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
378         return NULL;
379
380     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
381         gctx->selection = selection;
382
383     /* Our provctx is really just an OPENSSL_CTX */
384     gctx->libctx = (OPENSSL_CTX *)provctx;
385
386     return gctx;
387 }
388
389 static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
390 {
391     struct xor_gen_ctx *gctx = genctx;
392     const OSSL_PARAM *p;
393
394     if (gctx == NULL)
395         return 0;
396
397     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
398     if (p != NULL) {
399         if (p->data_type != OSSL_PARAM_UTF8_STRING
400                 || (strcmp(p->data, XORGROUP_NAME_INTERNAL) != 0
401                     &&  strcmp(p->data, XORKEMGROUP_NAME_INTERNAL) != 0))
402             return 0;
403     }
404
405     return 1;
406 }
407
408 static const OSSL_PARAM *xor_gen_settable_params(void *provctx)
409 {
410     static OSSL_PARAM settable[] = {
411         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
412         OSSL_PARAM_END
413     };
414     return settable;
415 }
416
417 static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
418 {
419     struct xor_gen_ctx *gctx = genctx;
420     XORKEY *key = OPENSSL_zalloc(sizeof(*key));
421     size_t i;
422
423     if (key == NULL)
424         return NULL;
425
426     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
427         if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE) <= 0) {
428             OPENSSL_free(key);
429             return NULL;
430         }
431         for (i = 0; i < XOR_KEY_SIZE; i++)
432             key->pubkey[i] = key->privkey[i] ^ private_constant[i];
433         key->hasprivkey = 1;
434         key->haspubkey = 1;
435     }
436
437     return key;
438 }
439
440 static void xor_gen_cleanup(void *genctx)
441 {
442     OPENSSL_free(genctx);
443 }
444
445 static const OSSL_DISPATCH xor_keymgmt_functions[] = {
446     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata },
447     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
448     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
449     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
450       (void (*)(void))xor_gen_settable_params },
451     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
452     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
453     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
454     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
455     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
456     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
457     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
458     { OSSL_FUNC_KEYMGMT_COPY, (void (*)(void))xor_copy },
459     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata },
460     { 0, NULL }
461 };
462
463 static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
464     /*
465      * Obviously this is not FIPS approved, but in order to test in conjuction
466      * with the FIPS provider we pretend that it is.
467      */
468     { "XOR", "provider=tls-provider,fips=yes", xor_keymgmt_functions },
469     { NULL, NULL, NULL }
470 };
471
472 static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
473                                             int *no_cache)
474 {
475     *no_cache = 0;
476     switch (operation_id) {
477     case OSSL_OP_KEYMGMT:
478         return tls_prov_keymgmt;
479     case OSSL_OP_KEYEXCH:
480         return tls_prov_keyexch;
481     }
482     return NULL;
483 }
484
485 /* Functions we provide to the core */
486 static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
487     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
488     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
489     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
490     { 0, NULL }
491 };
492
493 static
494 unsigned int randomize_tls_group_id(OPENSSL_CTX *libctx)
495 {
496     /*
497      * Randomise the group_id we're going to use to ensure we don't interoperate
498      * with anything but ourselves.
499      */
500     unsigned int group_id;
501     static unsigned int mem[10] = { 0 };
502     static int in_mem = 0;
503     int i;
504
505  retry:
506     if (!RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id)))
507         return 0;
508     /*
509      * Ensure group_id is within the IANA Reserved for private use range
510      * (65024-65279)
511      */
512     group_id %= 65279 - 65024;
513     group_id += 65024;
514
515     /* Ensure we did not already issue this group_id */
516     for (i = 0; i < in_mem; i++)
517         if (mem[i] == group_id)
518             goto retry;
519
520     /* Add this group_id to the list of ids issued by this function */
521     mem[in_mem++] = group_id;
522
523     return group_id;
524 }
525
526 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
527                       const OSSL_DISPATCH *in,
528                       const OSSL_DISPATCH **out,
529                       void **provctx)
530 {
531     OPENSSL_CTX *libctx = OPENSSL_CTX_new();
532
533     *provctx = libctx;
534
535     /*
536      * Randomise the group_id we're going to use to ensure we don't interoperate
537      * with anything but ourselves.
538      */
539     xor_group.group_id = randomize_tls_group_id(libctx);
540     xor_kemgroup.group_id = randomize_tls_group_id(libctx);
541
542     *out = tls_prov_dispatch_table;
543     return 1;
544 }