From ebc2d877e72b12bfbe6c43197b77826887365bf1 Mon Sep 17 00:00:00 2001 From: Ludvig Michaelsson Date: Fri, 13 Sep 2024 13:37:04 +0200 Subject: [PATCH 1/2] nfc: move function definition, no functional change --- src/nfc.c | 80 +++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/nfc.c b/src/nfc.c index 7cee9c0a..5a552db4 100644 --- a/src/nfc.c +++ b/src/nfc.c @@ -143,46 +143,6 @@ fido_nfc_tx(fido_dev_t *d, uint8_t cmd, const unsigned char *buf, size_t count) return ok; } -static int -rx_init(fido_dev_t *d, unsigned char *buf, size_t count, int ms) -{ - fido_ctap_info_t *attr = (fido_ctap_info_t *)buf; - uint8_t f[64]; - int n; - - if (count != sizeof(*attr)) { - fido_log_debug("%s: count=%zu", __func__, count); - return -1; - } - - memset(attr, 0, sizeof(*attr)); - - if ((n = d->io.read(d->io_handle, f, sizeof(f), ms)) < 2 || - (f[n - 2] << 8 | f[n - 1]) != SW_NO_ERROR) { - fido_log_debug("%s: read", __func__); - return -1; - } - - n -= 2; - - if (n == sizeof(v_u2f) && memcmp(f, v_u2f, sizeof(v_u2f)) == 0) - attr->flags = FIDO_CAP_CBOR; - else if (n == sizeof(v_fido) && memcmp(f, v_fido, sizeof(v_fido)) == 0) - attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG; - else { - fido_log_debug("%s: unknown version string", __func__); -#ifdef FIDO_FUZZ - attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG; -#else - return -1; -#endif - } - - memcpy(&attr->nonce, &d->nonce, sizeof(attr->nonce)); /* XXX */ - - return (int)count; -} - static int tx_get_response(fido_dev_t *d, uint8_t count, bool cbor) { @@ -275,6 +235,46 @@ rx_cbor(fido_dev_t *d, unsigned char *buf, size_t count, int ms) return r - 2; } +static int +rx_init(fido_dev_t *d, unsigned char *buf, size_t count, int ms) +{ + fido_ctap_info_t *attr = (fido_ctap_info_t *)buf; + uint8_t f[64]; + int n; + + if (count != sizeof(*attr)) { + fido_log_debug("%s: count=%zu", __func__, count); + return -1; + } + + memset(attr, 0, sizeof(*attr)); + + if ((n = d->io.read(d->io_handle, f, sizeof(f), ms)) < 2 || + (f[n - 2] << 8 | f[n - 1]) != SW_NO_ERROR) { + fido_log_debug("%s: read", __func__); + return -1; + } + + n -= 2; + + if (n == sizeof(v_u2f) && memcmp(f, v_u2f, sizeof(v_u2f)) == 0) + attr->flags = FIDO_CAP_CBOR; + else if (n == sizeof(v_fido) && memcmp(f, v_fido, sizeof(v_fido)) == 0) + attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG; + else { + fido_log_debug("%s: unknown version string", __func__); +#ifdef FIDO_FUZZ + attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG; +#else + return -1; +#endif + } + + memcpy(&attr->nonce, &d->nonce, sizeof(attr->nonce)); /* XXX */ + + return (int)count; +} + int fido_nfc_rx(fido_dev_t *d, uint8_t cmd, unsigned char *buf, size_t count, int ms) { From 2323ca40c1bd85a0b6df7c708168c8d1cffa3954 Mon Sep 17 00:00:00 2001 From: Ludvig Michaelsson Date: Fri, 13 Sep 2024 13:40:38 +0200 Subject: [PATCH 2/2] nfc: chain rapdus in rx_init Some cards erroneously interpret Le=0 as requesting no response data. When this happens, they send a 61nn response instead. --- src/nfc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nfc.c b/src/nfc.c index 5a552db4..55e3661d 100644 --- a/src/nfc.c +++ b/src/nfc.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2022 Yubico AB. All rights reserved. + * Copyright (c) 2020-2024 Yubico AB. All rights reserved. * Use of this source code is governed by a BSD-style * license that can be found in the LICENSE file. * SPDX-License-Identifier: BSD-2-Clause @@ -249,7 +249,7 @@ rx_init(fido_dev_t *d, unsigned char *buf, size_t count, int ms) memset(attr, 0, sizeof(*attr)); - if ((n = d->io.read(d->io_handle, f, sizeof(f), ms)) < 2 || + if ((n = rx_msg(d, f, sizeof(f), ms, false)) < 2 || (f[n - 2] << 8 | f[n - 1]) != SW_NO_ERROR) { fido_log_debug("%s: read", __func__); return -1;