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
|
From 1cb2420a26924ea4b4a5b525a78cdd06e4f9e4fa Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Tue, 26 Nov 2019 19:30:10 -0800
Subject: [PATCH] cbor, u2f: avoid use of packed struct
---
src/cbor.c | 14 ++++++++--
src/fido/types.h | 13 ++-------
src/u2f.c | 70 +++++++++++++++++++++++++++++++++---------------
3 files changed, 62 insertions(+), 35 deletions(-)
diff --git a/src/cbor.c b/src/cbor.c
index 3928325..2b3c150 100644
--- a/src/cbor.c
+++ b/src/cbor.c
@@ -1278,7 +1278,12 @@ cbor_decode_cred_authdata(const cbor_item_t *item, int cose_alg,
fido_log_debug("%s: buf=%p, len=%zu", __func__, (const void *)buf, len);
fido_log_xxd(buf, len);
- if (fido_buf_read(&buf, &len, authdata, sizeof(*authdata)) < 0) {
+ if (fido_buf_read(&buf, &len, authdata->rp_id_hash,
+ sizeof(authdata->rp_id_hash)) < 0 ||
+ fido_buf_read(&buf, &len, &authdata->flags,
+ sizeof(authdata->flags)) < 0 ||
+ fido_buf_read(&buf, &len, &authdata->sigcount,
+ sizeof(authdata->sigcount)) < 0) {
fido_log_debug("%s: fido_buf_read", __func__);
return (-1);
}
@@ -1328,7 +1333,12 @@ cbor_decode_assert_authdata(const cbor_item_t *item, fido_blob_t *authdata_cbor,
fido_log_debug("%s: buf=%p, len=%zu", __func__, (const void *)buf, len);
- if (fido_buf_read(&buf, &len, authdata, sizeof(*authdata)) < 0) {
+ if (fido_buf_read(&buf, &len, authdata->rp_id_hash,
+ sizeof(authdata->rp_id_hash)) < 0 ||
+ fido_buf_read(&buf, &len, &authdata->flags,
+ sizeof(authdata->flags)) < 0 ||
+ fido_buf_read(&buf, &len, &authdata->sigcount,
+ sizeof(authdata->sigcount)) < 0) {
fido_log_debug("%s: fido_buf_read", __func__);
return (-1);
}
diff --git a/src/fido/types.h b/src/fido/types.h
index a8ce2ec..814f22c 100644
--- a/src/fido/types.h
+++ b/src/fido/types.h
@@ -44,7 +44,6 @@ typedef enum {
typedef void fido_log_handler_t(const char *);
#ifdef _FIDO_INTERNAL
-#include "packed.h"
#include "blob.h"
/* COSE ES256 (ECDSA over P-256 with SHA-256) public key */
@@ -69,20 +68,12 @@ typedef struct eddsa_pk {
unsigned char x[32];
} eddsa_pk_t;
-PACKED_TYPE(fido_authdata_t,
-struct fido_authdata {
+typedef struct fido_authdata {
unsigned char rp_id_hash[32]; /* sha256 of fido_rp.id */
uint8_t flags; /* user present/verified */
uint32_t sigcount; /* signature counter */
/* actually longer */
-})
-
-PACKED_TYPE(fido_attcred_raw_t,
-struct fido_attcred_raw {
- unsigned char aaguid[16]; /* credential's aaguid */
- uint16_t id_len; /* credential id length */
- uint8_t body[]; /* credential id + pubkey */
-})
+} fido_authdata_t;
typedef struct fido_attcred {
unsigned char aaguid[16]; /* credential's aaguid */
diff --git a/src/u2f.c b/src/u2f.c
index 3b01f61..848d2fb 100644
--- a/src/u2f.c
+++ b/src/u2f.c
@@ -16,6 +16,29 @@
#include "fido.h"
#include "fido/es256.h"
+/*
+ * Web Authentication section 6.1
+ * https://www.w3.org/TR/webauthn/#authenticator-data
+ */
+enum {
+ AUTHDATA_RP_ID_HASH = 0,
+ AUTHDATA_FLAGS = 32,
+ AUTHDATA_SIGN_COUNT = 33,
+
+ AUTHDATA_BASE_SIZE = 37
+};
+
+/*
+ * Web Authentication section 6.4.1
+ * https://www.w3.org/TR/webauthn/#sec-attested-credential-data
+ */
+enum {
+ ATTCRED_AAGUID = 0,
+ ATTCRED_CREDENTIAL_ID_LENGTH = 16,
+
+ ATTCRED_BASE_SIZE = 18
+};
+
static int
sleep_msec(unsigned int msec)
{
@@ -96,23 +119,24 @@ static int
authdata_fake(const char *rp_id, uint8_t flags, uint32_t sigcount,
fido_blob_t *fake_cbor_ad)
{
- fido_authdata_t ad;
+ uint8_t authdata[AUTHDATA_BASE_SIZE] = {0};
+ unsigned char *rp_id_hash;
cbor_item_t *item = NULL;
size_t alloc_len;
- memset(&ad, 0, sizeof(ad));
+ rp_id_hash = (unsigned char *)&authdata[AUTHDATA_RP_ID_HASH];
if (SHA256((const void *)rp_id, strlen(rp_id),
- ad.rp_id_hash) != ad.rp_id_hash) {
+ rp_id_hash) != rp_id_hash) {
fido_log_debug("%s: sha256", __func__);
return (-1);
}
- ad.flags = flags; /* XXX translate? */
- ad.sigcount = sigcount;
+ authdata[AUTHDATA_FLAGS] = flags; /* XXX translate? */
+ memcpy(&authdata[AUTHDATA_SIGN_COUNT], &sigcount, 4);
- if ((item = cbor_build_bytestring((const unsigned char *)&ad,
- sizeof(ad))) == NULL) {
+ if ((item = cbor_build_bytestring((cbor_data)authdata,
+ sizeof(authdata))) == NULL) {
fido_log_debug("%s: cbor_build_bytestring", __func__);
return (-1);
}
@@ -410,18 +434,18 @@ static int
encode_cred_authdata(const char *rp_id, const uint8_t *kh, uint8_t kh_len,
const uint8_t *pubkey, size_t pubkey_len, fido_blob_t *out)
{
- fido_authdata_t authdata;
- fido_attcred_raw_t attcred_raw;
- fido_blob_t pk_blob;
- fido_blob_t authdata_blob;
- cbor_item_t *authdata_cbor = NULL;
- unsigned char *ptr;
- size_t len;
- size_t alloc_len;
- int ok = -1;
+ uint8_t authdata[AUTHDATA_BASE_SIZE] = {0};
+ unsigned char *rp_id_hash;
+ uint8_t attcred_raw[ATTCRED_BASE_SIZE] = {0};
+ fido_blob_t pk_blob;
+ fido_blob_t authdata_blob;
+ cbor_item_t *authdata_cbor = NULL;
+ unsigned char *ptr;
+ size_t len;
+ size_t alloc_len;
+ int ok = -1;
memset(&pk_blob, 0, sizeof(pk_blob));
- memset(&authdata, 0, sizeof(authdata));
memset(&authdata_blob, 0, sizeof(authdata_blob));
memset(out, 0, sizeof(*out));
@@ -435,17 +459,19 @@ encode_cred_authdata(const char *rp_id, const uint8_t *kh, uint8_t kh_len,
goto fail;
}
+ rp_id_hash = (unsigned char *)&authdata[AUTHDATA_RP_ID_HASH];
+
if (SHA256((const void *)rp_id, strlen(rp_id),
- authdata.rp_id_hash) != authdata.rp_id_hash) {
+ rp_id_hash) != rp_id_hash) {
fido_log_debug("%s: sha256", __func__);
goto fail;
}
- authdata.flags = (CTAP_AUTHDATA_ATT_CRED | CTAP_AUTHDATA_USER_PRESENT);
- authdata.sigcount = 0;
+ authdata[AUTHDATA_FLAGS] = (CTAP_AUTHDATA_ATT_CRED |
+ CTAP_AUTHDATA_USER_PRESENT);
- memset(&attcred_raw.aaguid, 0, sizeof(attcred_raw.aaguid));
- attcred_raw.id_len = htobe16(kh_len);
+ /* big-endian, so second byte is LSB */
+ attcred_raw[ATTCRED_CREDENTIAL_ID_LENGTH + 1] = kh_len;
len = authdata_blob.len = sizeof(authdata) + sizeof(attcred_raw) +
kh_len + pk_blob.len;
--
2.26.1
|