summaryrefslogtreecommitdiffstats
path: root/src/io.c
blob: a9715b5b4bf80f1c2526920d42690d4f9fb15771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (c) 2018-2022 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
 */

#include "fido.h"
#include "packed.h"

PACKED_TYPE(frame_t,
struct frame {
	uint32_t cid; /* channel id */
	union {
		uint8_t type;
		struct {
			uint8_t cmd;
			uint8_t bcnth;
			uint8_t bcntl;
			uint8_t data[CTAP_MAX_REPORT_LEN - CTAP_INIT_HEADER_LEN];
		} init;
		struct {
			uint8_t seq;
			uint8_t data[CTAP_MAX_REPORT_LEN - CTAP_CONT_HEADER_LEN];
		} cont;
	} body;
})

#ifndef MIN
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#endif

static int
tx_pkt(fido_dev_t *d, const void *pkt, size_t len, int *ms)
{
	struct timespec ts;
	int n;

	if (fido_time_now(&ts) != 0)
		return (-1);

	n = d->io.write(d->io_handle, pkt, len);

	if (fido_time_delta(&ts, ms) != 0)
		return (-1);

	return (n);
}

static int
tx_empty(fido_dev_t *d, uint8_t cmd, int *ms)
{
	struct frame	*fp;
	unsigned char	 pkt[sizeof(*fp) + 1];
	const size_t	 len = d->tx_len + 1;
	int		 n;

	memset(&pkt, 0, sizeof(pkt));
	fp = (struct frame *)(pkt + 1);
	fp->cid = d->cid;
	fp->body.init.cmd = CTAP_FRAME_INIT | cmd;

	if (len > sizeof(pkt) || (n = tx_pkt(d, pkt, len, ms)) < 0 ||
	    (size_t)n != len)
		return (-1);

	return (0);
}

static size_t
tx_preamble(fido_dev_t *d, uint8_t cmd, const void *buf, size_t count, int *ms)
{
	struct frame	*fp;
	unsigned char	 pkt[sizeof(*fp) + 1];
	const size_t	 len = d->tx_len + 1;
	int		 n;

	if (d->tx_len - CTAP_INIT_HEADER_LEN > sizeof(fp->body.init.data))
		return (0);

	memset(&pkt, 0, sizeof(pkt));
	fp = (struct frame *)(pkt + 1);
	fp->cid = d->cid;
	fp->body.init.cmd = CTAP_FRAME_INIT | cmd;
	fp->body.init.bcnth = (count >> 8) & 0xff;
	fp->body.init.bcntl = count & 0xff;
	count = MIN(count, d->tx_len - CTAP_INIT_HEADER_LEN);
	memcpy(&fp->body.init.data, buf, count);

	if (len > sizeof(pkt) || (n = tx_pkt(d, pkt, len, ms)) < 0 ||
	    (size_t)n != len)
		return (0);

	return (count);
}

static size_t
tx_frame(fido_dev_t *d, uint8_t seq, const void *buf, size_t count, int *ms)
{
	struct frame	*fp;
	unsigned char	 pkt[sizeof(*fp) + 1];
	const size_t	 len = d->tx_len + 1;
	int		 n;

	if (d->tx_len - CTAP_CONT_HEADER_LEN > sizeof(fp->body.cont.data))
		return (0);

	memset(&pkt, 0, sizeof(pkt));
	fp = (struct frame *)(pkt + 1);
	fp->cid = d->cid;
	fp->body.cont.seq = seq;
	count = MIN(count, d->tx_len - CTAP_CONT_HEADER_LEN);
	memcpy(&fp->body.cont.data, buf, count);

	if (len > sizeof(pkt) || (n = tx_pkt(d, pkt, len, ms)) < 0 ||
	    (size_t)n != len)
		return (0);

	return (count);
}

static int
tx(fido_dev_t *d, uint8_t cmd, const unsigned char *buf, size_t count, int *ms)
{
	size_t n, sent;

	if ((sent = tx_preamble(d, cmd, buf, count, ms)) == 0) {
		fido_log_debug("%s: tx_preamble", __func__);
		return (-1);
	}

	for (uint8_t seq = 0; sent < count; sent += n) {
		if (seq & 0x80) {
			fido_log_debug("%s: seq & 0x80", __func__);
			return (-1);
		}
		if ((n = tx_frame(d, seq++, buf + sent, count - sent,
		    ms)) == 0) {
			fido_log_debug("%s: tx_frame", __func__);
			return (-1);
		}
	}

	return (0);
}

static int
transport_tx(fido_dev_t *d, uint8_t cmd, const void *buf, size_t count, int *ms)
{
	struct timespec ts;
	int n;

	if (fido_time_now(&ts) != 0)
		return (-1);

	n = d->transport.tx(d, cmd, buf, count);

	if (fido_time_delta(&ts, ms) != 0)
		return (-1);

	return (n);
}

int
fido_tx(fido_dev_t *d, uint8_t cmd, const void *buf, size_t count, int *ms)
{
	fido_log_debug("%s: dev=%p, cmd=0x%02x", __func__, (void *)d, cmd);
	fido_log_xxd(buf, count, "%s", __func__);

	if (d->transport.tx != NULL)
		return (transport_tx(d, cmd, buf, count, ms));
	if (d->io_handle == NULL || d->io.write == NULL || count > UINT16_MAX) {
		fido_log_debug("%s: invalid argument", __func__);
		return (-1);
	}

	return (count == 0 ? tx_empty(d, cmd, ms) : tx(d, cmd, buf, count, ms));
}

static int
rx_frame(fido_dev_t *d, struct frame *fp, int *ms)
{
	struct timespec ts;
	int n;

	memset(fp, 0, sizeof(*fp));

	if (fido_time_now(&ts) != 0)
		return (-1);

	if (d->rx_len > sizeof(*fp) || (n = d->io.read(d->io_handle,
	    (unsigned char *)fp, d->rx_len, *ms)) < 0 || (size_t)n != d->rx_len)
		return (-1);

	return (fido_time_delta(&ts, ms));
}

static int
rx_preamble(fido_dev_t *d, uint8_t cmd, struct frame *fp, int *ms)
{
	do {
		if (rx_frame(d, fp, ms) < 0)
			return (-1);
#ifdef FIDO_FUZZ
		fp->cid = d->cid;
#endif
	} while (fp->cid != d->cid || (fp->cid == d->cid &&
	    fp->body.init.cmd == (CTAP_FRAME_INIT | CTAP_KEEPALIVE)));

	if (d->rx_len > sizeof(*fp))
		return (-1);

	fido_log_xxd(fp, d->rx_len, "%s", __func__);
#ifdef FIDO_FUZZ
	fp->body.init.cmd = (CTAP_FRAME_INIT | cmd);
#endif

	if (fp->cid != d->cid || fp->body.init.cmd != (CTAP_FRAME_INIT | cmd)) {
		fido_log_debug("%s: cid (0x%x, 0x%x), cmd (0x%02x, 0x%02x)",
		    __func__, fp->cid, d->cid, fp->body.init.cmd, cmd);
		return (-1);
	}

	return (0);
}

static int
rx(fido_dev_t *d, uint8_t cmd, unsigned char *buf, size_t count, int *ms)
{
	struct frame f;
	size_t r, payload_len, init_data_len, cont_data_len;

	if (d->rx_len <= CTAP_INIT_HEADER_LEN ||
	    d->rx_len <= CTAP_CONT_HEADER_LEN)
		return (-1);

	init_data_len = d->rx_len - CTAP_INIT_HEADER_LEN;
	cont_data_len = d->rx_len - CTAP_CONT_HEADER_LEN;

	if (init_data_len > sizeof(f.body.init.data) ||
	    cont_data_len > sizeof(f.body.cont.data))
		return (-1);

	if (rx_preamble(d, cmd, &f, ms) < 0) {
		fido_log_debug("%s: rx_preamble", __func__);
		return (-1);
	}

	payload_len = (size_t)((f.body.init.bcnth << 8) | f.body.init.bcntl);
	fido_log_debug("%s: payload_len=%zu", __func__, payload_len);

	if (count < payload_len) {
		fido_log_debug("%s: count < payload_len", __func__);
		return (-1);
	}

	if (payload_len < init_data_len) {
		memcpy(buf, f.body.init.data, payload_len);
		return ((int)payload_len);
	}

	memcpy(buf, f.body.init.data, init_data_len);
	r = init_data_len;

	for (int seq = 0; r < payload_len; seq++) {
		if (rx_frame(d, &f, ms) < 0) {
			fido_log_debug("%s: rx_frame", __func__);
			return (-1);
		}

		fido_log_xxd(&f, d->rx_len, "%s", __func__);
#ifdef FIDO_FUZZ
		f.cid = d->cid;
		f.body.cont.seq = (uint8_t)seq;
#endif

		if (f.cid != d->cid || f.body.cont.seq != seq) {
			fido_log_debug("%s: cid (0x%x, 0x%x), seq (%d, %d)",
			    __func__, f.cid, d->cid, f.body.cont.seq, seq);
			return (-1);
		}

		if (payload_len - r > cont_data_len) {
			memcpy(buf + r, f.body.cont.data, cont_data_len);
			r += cont_data_len;
		} else {
			memcpy(buf + r, f.body.cont.data, payload_len - r);
			r += payload_len - r; /* break */
		}
	}

	return ((int)r);
}

static int
transport_rx(fido_dev_t *d, uint8_t cmd, void *buf, size_t count, int *ms)
{
	struct timespec ts;
	int n;

	if (fido_time_now(&ts) != 0)
		return (-1);

	n = d->transport.rx(d, cmd, buf, count, *ms);

	if (fido_time_delta(&ts, ms) != 0)
		return (-1);

	return (n);
}

int
fido_rx(fido_dev_t *d, uint8_t cmd, void *buf, size_t count, int *ms)
{
	int n;

	fido_log_debug("%s: dev=%p, cmd=0x%02x, ms=%d", __func__, (void *)d,
	    cmd, *ms);

	if (d->transport.rx != NULL)
		return (transport_rx(d, cmd, buf, count, ms));
	if (d->io_handle == NULL || d->io.read == NULL || count > UINT16_MAX) {
		fido_log_debug("%s: invalid argument", __func__);
		return (-1);
	}
	if ((n = rx(d, cmd, buf, count, ms)) >= 0)
		fido_log_xxd(buf, (size_t)n, "%s", __func__);

	return (n);
}

int
fido_rx_cbor_status(fido_dev_t *d, int *ms)
{
	unsigned char	*msg;
	int		 msglen;
	int		 r;

	if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
		r = FIDO_ERR_INTERNAL;
		goto out;
	}

	if ((msglen = fido_rx(d, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0 ||
	    (size_t)msglen < 1) {
		fido_log_debug("%s: fido_rx", __func__);
		r = FIDO_ERR_RX;
		goto out;
	}

	r = msg[0];
out:
	freezero(msg, FIDO_MAXMSG);

	return (r);
}