xref: /freebsd/contrib/libfido2/src/config.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
10afa8e06SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 2020-2022 Yubico AB. All rights reserved.
30afa8e06SEd Maste  * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste  */
70afa8e06SEd Maste 
80afa8e06SEd Maste #include "fido.h"
90afa8e06SEd Maste #include "fido/config.h"
100afa8e06SEd Maste #include "fido/es256.h"
110afa8e06SEd Maste 
120afa8e06SEd Maste #define CMD_ENABLE_ENTATTEST	0x01
130afa8e06SEd Maste #define CMD_TOGGLE_ALWAYS_UV	0x02
140afa8e06SEd Maste #define CMD_SET_PIN_MINLEN	0x03
150afa8e06SEd Maste 
160afa8e06SEd Maste static int
config_prepare_hmac(uint8_t subcmd,const cbor_item_t * item,fido_blob_t * hmac)170afa8e06SEd Maste config_prepare_hmac(uint8_t subcmd, const cbor_item_t *item, fido_blob_t *hmac)
180afa8e06SEd Maste {
190afa8e06SEd Maste 	uint8_t prefix[32 + 2 * sizeof(uint8_t)], cbor[128];
20*2ccfa855SEd Maste 	size_t cbor_len = 0;
210afa8e06SEd Maste 
220afa8e06SEd Maste 	memset(prefix, 0xff, sizeof(prefix));
230afa8e06SEd Maste 	prefix[sizeof(prefix) - 2] = CTAP_CBOR_CONFIG;
240afa8e06SEd Maste 	prefix[sizeof(prefix) - 1] = subcmd;
250afa8e06SEd Maste 
26*2ccfa855SEd Maste 	if (item != NULL) {
270afa8e06SEd Maste 		if ((cbor_len = cbor_serialize(item, cbor, sizeof(cbor))) == 0) {
280afa8e06SEd Maste 			fido_log_debug("%s: cbor_serialize", __func__);
290afa8e06SEd Maste 			return -1;
300afa8e06SEd Maste 		}
31*2ccfa855SEd Maste 	}
320afa8e06SEd Maste 	if ((hmac->ptr = malloc(cbor_len + sizeof(prefix))) == NULL) {
330afa8e06SEd Maste 		fido_log_debug("%s: malloc", __func__);
340afa8e06SEd Maste 		return -1;
350afa8e06SEd Maste 	}
360afa8e06SEd Maste 	memcpy(hmac->ptr, prefix, sizeof(prefix));
370afa8e06SEd Maste 	memcpy(hmac->ptr + sizeof(prefix), cbor, cbor_len);
380afa8e06SEd Maste 	hmac->len = cbor_len + sizeof(prefix);
390afa8e06SEd Maste 
400afa8e06SEd Maste 	return 0;
410afa8e06SEd Maste }
420afa8e06SEd Maste 
430afa8e06SEd Maste static int
config_tx(fido_dev_t * dev,uint8_t subcmd,cbor_item_t ** paramv,size_t paramc,const char * pin,int * ms)440afa8e06SEd Maste config_tx(fido_dev_t *dev, uint8_t subcmd, cbor_item_t **paramv, size_t paramc,
45f540a430SEd Maste     const char *pin, int *ms)
460afa8e06SEd Maste {
470afa8e06SEd Maste 	cbor_item_t *argv[4];
480afa8e06SEd Maste 	es256_pk_t *pk = NULL;
490afa8e06SEd Maste 	fido_blob_t *ecdh = NULL, f, hmac;
500afa8e06SEd Maste 	const uint8_t cmd = CTAP_CBOR_CONFIG;
510afa8e06SEd Maste 	int r = FIDO_ERR_INTERNAL;
520afa8e06SEd Maste 
530afa8e06SEd Maste 	memset(&f, 0, sizeof(f));
540afa8e06SEd Maste 	memset(&hmac, 0, sizeof(hmac));
550afa8e06SEd Maste 	memset(&argv, 0, sizeof(argv));
560afa8e06SEd Maste 
570afa8e06SEd Maste 	/* subCommand */
580afa8e06SEd Maste 	if ((argv[0] = cbor_build_uint8(subcmd)) == NULL) {
590afa8e06SEd Maste 		fido_log_debug("%s: cbor encode", __func__);
600afa8e06SEd Maste 		goto fail;
610afa8e06SEd Maste 	}
620afa8e06SEd Maste 
63*2ccfa855SEd Maste 	/* subCommandParams */
64*2ccfa855SEd Maste 	if (paramc != 0 &&
65*2ccfa855SEd Maste 	    (argv[1] = cbor_flatten_vector(paramv, paramc)) == NULL) {
660afa8e06SEd Maste 		fido_log_debug("%s: cbor_flatten_vector", __func__);
670afa8e06SEd Maste 		goto fail;
680afa8e06SEd Maste 	}
69*2ccfa855SEd Maste 
70*2ccfa855SEd Maste 	/* pinProtocol, pinAuth */
71*2ccfa855SEd Maste 	if (pin != NULL ||
72*2ccfa855SEd Maste 	    (fido_dev_supports_permissions(dev) && fido_dev_has_uv(dev))) {
730afa8e06SEd Maste 		if (config_prepare_hmac(subcmd, argv[1], &hmac) < 0) {
740afa8e06SEd Maste 			fido_log_debug("%s: config_prepare_hmac", __func__);
750afa8e06SEd Maste 			goto fail;
760afa8e06SEd Maste 		}
77f540a430SEd Maste 		if ((r = fido_do_ecdh(dev, &pk, &ecdh, ms)) != FIDO_OK) {
780afa8e06SEd Maste 			fido_log_debug("%s: fido_do_ecdh", __func__);
790afa8e06SEd Maste 			goto fail;
800afa8e06SEd Maste 		}
810afa8e06SEd Maste 		if ((r = cbor_add_uv_params(dev, cmd, &hmac, pk, ecdh, pin,
82f540a430SEd Maste 		    NULL, &argv[3], &argv[2], ms)) != FIDO_OK) {
830afa8e06SEd Maste 			fido_log_debug("%s: cbor_add_uv_params", __func__);
840afa8e06SEd Maste 			goto fail;
850afa8e06SEd Maste 		}
860afa8e06SEd Maste 	}
870afa8e06SEd Maste 
880afa8e06SEd Maste 	/* framing and transmission */
890afa8e06SEd Maste 	if (cbor_build_frame(cmd, argv, nitems(argv), &f) < 0 ||
90f540a430SEd Maste 	    fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
910afa8e06SEd Maste 		fido_log_debug("%s: fido_tx", __func__);
920afa8e06SEd Maste 		r = FIDO_ERR_TX;
930afa8e06SEd Maste 		goto fail;
940afa8e06SEd Maste 	}
950afa8e06SEd Maste 
960afa8e06SEd Maste 	r = FIDO_OK;
970afa8e06SEd Maste fail:
980afa8e06SEd Maste 	cbor_vector_free(argv, nitems(argv));
990afa8e06SEd Maste 	es256_pk_free(&pk);
1000afa8e06SEd Maste 	fido_blob_free(&ecdh);
1010afa8e06SEd Maste 	free(f.ptr);
1020afa8e06SEd Maste 	free(hmac.ptr);
1030afa8e06SEd Maste 
1040afa8e06SEd Maste 	return r;
1050afa8e06SEd Maste }
1060afa8e06SEd Maste 
1070afa8e06SEd Maste static int
config_enable_entattest_wait(fido_dev_t * dev,const char * pin,int * ms)108f540a430SEd Maste config_enable_entattest_wait(fido_dev_t *dev, const char *pin, int *ms)
1090afa8e06SEd Maste {
1100afa8e06SEd Maste 	int r;
1110afa8e06SEd Maste 
112f540a430SEd Maste 	if ((r = config_tx(dev, CMD_ENABLE_ENTATTEST, NULL, 0, pin,
113f540a430SEd Maste 	    ms)) != FIDO_OK)
1140afa8e06SEd Maste 		return r;
1150afa8e06SEd Maste 
1160afa8e06SEd Maste 	return fido_rx_cbor_status(dev, ms);
1170afa8e06SEd Maste }
1180afa8e06SEd Maste 
1190afa8e06SEd Maste int
fido_dev_enable_entattest(fido_dev_t * dev,const char * pin)1200afa8e06SEd Maste fido_dev_enable_entattest(fido_dev_t *dev, const char *pin)
1210afa8e06SEd Maste {
122f540a430SEd Maste 	int ms = dev->timeout_ms;
123f540a430SEd Maste 
124f540a430SEd Maste 	return (config_enable_entattest_wait(dev, pin, &ms));
1250afa8e06SEd Maste }
1260afa8e06SEd Maste 
1270afa8e06SEd Maste static int
config_toggle_always_uv_wait(fido_dev_t * dev,const char * pin,int * ms)128f540a430SEd Maste config_toggle_always_uv_wait(fido_dev_t *dev, const char *pin, int *ms)
1290afa8e06SEd Maste {
1300afa8e06SEd Maste 	int r;
1310afa8e06SEd Maste 
132f540a430SEd Maste 	if ((r = config_tx(dev, CMD_TOGGLE_ALWAYS_UV, NULL, 0, pin,
133f540a430SEd Maste 	    ms)) != FIDO_OK)
1340afa8e06SEd Maste 		return r;
1350afa8e06SEd Maste 
1360afa8e06SEd Maste 	return (fido_rx_cbor_status(dev, ms));
1370afa8e06SEd Maste }
1380afa8e06SEd Maste 
1390afa8e06SEd Maste int
fido_dev_toggle_always_uv(fido_dev_t * dev,const char * pin)1400afa8e06SEd Maste fido_dev_toggle_always_uv(fido_dev_t *dev, const char *pin)
1410afa8e06SEd Maste {
142f540a430SEd Maste 	int ms = dev->timeout_ms;
143f540a430SEd Maste 
144f540a430SEd Maste 	return config_toggle_always_uv_wait(dev, pin, &ms);
1450afa8e06SEd Maste }
1460afa8e06SEd Maste 
1470afa8e06SEd Maste static int
config_pin_minlen_tx(fido_dev_t * dev,size_t len,bool force,const fido_str_array_t * rpid,const char * pin,int * ms)148f540a430SEd Maste config_pin_minlen_tx(fido_dev_t *dev, size_t len, bool force,
149f540a430SEd Maste     const fido_str_array_t *rpid, const char *pin, int *ms)
1500afa8e06SEd Maste {
1510afa8e06SEd Maste 	cbor_item_t *argv[3];
1520afa8e06SEd Maste 	int r;
1530afa8e06SEd Maste 
1540afa8e06SEd Maste 	memset(argv, 0, sizeof(argv));
1550afa8e06SEd Maste 
156f540a430SEd Maste 	if ((rpid == NULL && len == 0 && !force) || len > UINT8_MAX) {
1570afa8e06SEd Maste 		r = FIDO_ERR_INVALID_ARGUMENT;
1580afa8e06SEd Maste 		goto fail;
1590afa8e06SEd Maste 	}
1600afa8e06SEd Maste 	if (len && (argv[0] = cbor_build_uint8((uint8_t)len)) == NULL) {
1610afa8e06SEd Maste 		fido_log_debug("%s: cbor_encode_uint8", __func__);
1620afa8e06SEd Maste 		r = FIDO_ERR_INTERNAL;
1630afa8e06SEd Maste 		goto fail;
1640afa8e06SEd Maste 	}
165f540a430SEd Maste 	if (rpid != NULL && (argv[1] = cbor_encode_str_array(rpid)) == NULL) {
166f540a430SEd Maste 		fido_log_debug("%s: cbor_encode_str_array", __func__);
167f540a430SEd Maste 		r = FIDO_ERR_INTERNAL;
168f540a430SEd Maste 		goto fail;
169f540a430SEd Maste 	}
1700afa8e06SEd Maste 	if (force && (argv[2] = cbor_build_bool(true)) == NULL) {
1710afa8e06SEd Maste 		fido_log_debug("%s: cbor_build_bool", __func__);
1720afa8e06SEd Maste 		r = FIDO_ERR_INTERNAL;
1730afa8e06SEd Maste 		goto fail;
1740afa8e06SEd Maste 	}
1750afa8e06SEd Maste 	if ((r = config_tx(dev, CMD_SET_PIN_MINLEN, argv, nitems(argv),
176f540a430SEd Maste 	    pin, ms)) != FIDO_OK) {
1770afa8e06SEd Maste 		fido_log_debug("%s: config_tx", __func__);
1780afa8e06SEd Maste 		goto fail;
1790afa8e06SEd Maste 	}
1800afa8e06SEd Maste 
1810afa8e06SEd Maste fail:
1820afa8e06SEd Maste 	cbor_vector_free(argv, nitems(argv));
1830afa8e06SEd Maste 
1840afa8e06SEd Maste 	return r;
1850afa8e06SEd Maste }
1860afa8e06SEd Maste 
1870afa8e06SEd Maste static int
config_pin_minlen(fido_dev_t * dev,size_t len,bool force,const fido_str_array_t * rpid,const char * pin,int * ms)188f540a430SEd Maste config_pin_minlen(fido_dev_t *dev, size_t len, bool force,
189f540a430SEd Maste     const fido_str_array_t *rpid, const char *pin, int *ms)
1900afa8e06SEd Maste {
1910afa8e06SEd Maste 	int r;
1920afa8e06SEd Maste 
193f540a430SEd Maste 	if ((r = config_pin_minlen_tx(dev, len, force, rpid, pin,
194f540a430SEd Maste 	    ms)) != FIDO_OK)
1950afa8e06SEd Maste 		return r;
1960afa8e06SEd Maste 
1970afa8e06SEd Maste 	return fido_rx_cbor_status(dev, ms);
1980afa8e06SEd Maste }
1990afa8e06SEd Maste 
2000afa8e06SEd Maste int
fido_dev_set_pin_minlen(fido_dev_t * dev,size_t len,const char * pin)2010afa8e06SEd Maste fido_dev_set_pin_minlen(fido_dev_t *dev, size_t len, const char *pin)
2020afa8e06SEd Maste {
203f540a430SEd Maste 	int ms = dev->timeout_ms;
204f540a430SEd Maste 
205f540a430SEd Maste 	return config_pin_minlen(dev, len, false, NULL, pin, &ms);
2060afa8e06SEd Maste }
2070afa8e06SEd Maste 
2080afa8e06SEd Maste int
fido_dev_force_pin_change(fido_dev_t * dev,const char * pin)2090afa8e06SEd Maste fido_dev_force_pin_change(fido_dev_t *dev, const char *pin)
2100afa8e06SEd Maste {
211f540a430SEd Maste 	int ms = dev->timeout_ms;
212f540a430SEd Maste 
213f540a430SEd Maste 	return config_pin_minlen(dev, 0, true, NULL, pin, &ms);
214f540a430SEd Maste }
215f540a430SEd Maste 
216f540a430SEd Maste int
fido_dev_set_pin_minlen_rpid(fido_dev_t * dev,const char * const * rpid,size_t n,const char * pin)217f540a430SEd Maste fido_dev_set_pin_minlen_rpid(fido_dev_t *dev, const char * const *rpid,
218f540a430SEd Maste     size_t n, const char *pin)
219f540a430SEd Maste {
220f540a430SEd Maste 	fido_str_array_t sa;
221f540a430SEd Maste 	int ms = dev->timeout_ms;
222f540a430SEd Maste 	int r;
223f540a430SEd Maste 
224f540a430SEd Maste 	memset(&sa, 0, sizeof(sa));
225f540a430SEd Maste 	if (fido_str_array_pack(&sa, rpid, n) < 0) {
226f540a430SEd Maste 		fido_log_debug("%s: fido_str_array_pack", __func__);
227f540a430SEd Maste 		r = FIDO_ERR_INTERNAL;
228f540a430SEd Maste 		goto fail;
229f540a430SEd Maste 	}
230f540a430SEd Maste 	r = config_pin_minlen(dev, 0, false, &sa, pin, &ms);
231f540a430SEd Maste fail:
232f540a430SEd Maste 	fido_str_array_free(&sa);
233f540a430SEd Maste 
234f540a430SEd Maste 	return r;
2350afa8e06SEd Maste }
236