~pedro/fido2-webauthn-client

fido2-webauthn-client/webauthn-assert.c -rw-r--r-- 7.0 KiB
084309c4pedro martelletto plug a mem leak in cbor_pack_item() 5 months ago
                                                                                
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
/*
 * Copyright (c) 2020 Pedro Martelletto. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 */

#include <err.h>
#include <stdlib.h>
#include <string.h>

#include <cbor.h>
#include <fido.h>
#include <jansson.h>
#include <openssl/sha.h>

#include "base64.h"
#include "clientdata.h"
#include "http.h"
#include "json.h"
#include "param.h"
#include "webauthn-assert.h"

struct webauthn_assert {
	fido_assert_t		*assert;
	char			*clientdata;
	char			*request_id;
	struct http_post	*begin;
	struct http_post	*finish;
};

struct webauthn_assert *
webauthn_assert_new(void)
{
	struct webauthn_assert *wa;

	if ((wa = calloc(1, sizeof(*wa))) == NULL) {
		warnx("%s: calloc", __func__);
		return NULL;
	}

	if ((wa->assert = fido_assert_new()) == NULL) {
		warnx("%s: fido_assert_new", __func__);
		webauthn_assert_free(&wa);
	}

	return wa;
}

void
webauthn_assert_free(struct webauthn_assert **wap)
{
	struct webauthn_assert *wa;

	if (wap == NULL || (wa = *wap) == NULL)
		return;

	fido_assert_free(&wa->assert);
	free(wa->clientdata);
	free(wa->request_id);
	http_free(&wa->begin);
	http_free(&wa->finish);
	free(wa);
	*wap = NULL;
}

static int
webauthn_assert_begin_parse_response(struct webauthn_assert *wa)
{
	json_t	*blob;
	json_t	*data;
	json_t	*pubkey;
	char	*challenge = NULL;
	void	*cred_ptr = NULL;
	size_t	 cred_len;
	int	 ok = -1;

	if ((blob = http_response_json(wa->begin)) == NULL ||
	    json_check_str(blob, "status", "success") < 0 ||
	    (data = json_parse_obj(blob, "data")) == NULL) {
		warnx("%s: json", __func__);
		goto fail;
	}

	if ((pubkey = json_parse_obj(data, "publicKey")) == NULL ||
	    (wa->request_id = json_parse_str(data, "requestId")) == NULL) {
		warnx("%s: json data", __func__);
		goto fail;
	}

	if ((challenge = json_parse_str(pubkey, "challenge")) == NULL ||
	    json_check_str(pubkey, "rpId", RP_ID) < 0 ||
	    json_parse_allowcred(pubkey, &cred_ptr, &cred_len) < 0) {
		warnx("%s: json publicKey", __func__);
		goto fail;
	}

	if ((wa->clientdata = clientdata_json("webauthn.get",
	    challenge)) == NULL) {
		warnx("%s: json challenge", __func__);
		goto fail;
	}

	if (fido_assert_set_rp(wa->assert, RP_ID) != FIDO_OK) {
		warnx("%s: fido_assert_set_rp", __func__);
		goto fail;
	}

	if (fido_assert_allow_cred(wa->assert, cred_ptr, cred_len) != FIDO_OK) {
		warnx("%s: fido_assert_allow_cred", __func__);
		goto fail;
	}

	ok = 0;
fail:
	if (blob)
		json_decref(blob);

	free(challenge);
	free(cred_ptr);

	return ok;
}

int
webauthn_assert_begin(struct webauthn_assert *wa, const char *cookie)
{
	if (http_request(&wa->begin, AUTH_BEGIN_URL, AUTH_BEGIN_BODY,
	    cookie) < 0) {
		warnx("%s: http_request", __func__);
		return -1;
	}

	if (webauthn_assert_begin_parse_response(wa) < 0) {
		warnx("%s: webauthn_assert_begin_parse_response", __func__);
		return -1;
	}

	return 0;
}

int
webauthn_assert_get(struct webauthn_assert *wa, fido_dev_t *token)
{
	uint8_t	dgst[SHA256_DIGEST_LENGTH];
	int	status;

	if (SHA256((const uint8_t *)wa->clientdata, strlen(wa->clientdata),
	    dgst) != dgst) {
		warnx("%s: SHA256", __func__);
		return -1;
	}

	if (fido_assert_set_clientdata_hash(wa->assert, dgst,
	    sizeof(dgst)) != FIDO_OK) {
		warnx("%s: fido_assert_set_clientdata_hash", __func__);
		return -1;
	}

	if ((status = fido_dev_get_assert(token, wa->assert, NULL)) != FIDO_OK) {
		warnx("%s: fido_dev_get_assert: %s", __func__,
		    fido_strerr(status));
		return -1;
	}

	return 0;
}

static int
webauthn_assert_finish_parse_response(struct http_post *r)
{
	json_t	*blob;
	int	 ok = 0;

	if ((blob = http_response_json(r)) == NULL ||
	    json_check_str(blob, "status", "success") < 0) {
		warnx("%s: json", __func__);
		ok = -1;
	}

	if (blob)
		json_decref(blob);

	return ok;
}

static int
webauthn_assert_encode_authdata(const fido_assert_t *assert, char **out)
{
	cbor_item_t		*body = NULL;
	const unsigned char	*ptr;
	size_t			 len;
	int			 ok = -1;
	struct cbor_load_result	 cbor;

	*out = NULL;

	if ((body = cbor_load(fido_assert_authdata_ptr(assert, 0),
	    fido_assert_authdata_len(assert, 0), &cbor)) == NULL) {
		warnx("%s: cbor_load", __func__);
		goto fail;
	}

	if (cbor_isa_bytestring(body) == false ||
	    (ptr = cbor_bytestring_handle(body)) == NULL ||
	    (len = cbor_bytestring_length(body)) == 0) {
		warnx("%s: cbor_bytestring", __func__);
		goto fail;
	}

	if (base64_encode(ptr, len, out) < 0) {
		warnx("%s: base64_encode", __func__);
		goto fail;
	}

	ok = 0;
fail:
	if (body)
		cbor_decref(&body);

	return ok;
}

static int
webauthn_assert_encode_clientdata(const char *clientdata, char **out)
{
	if (base64_encode(clientdata, strlen(clientdata), out) < 0) {
		warnx("%s: base64_encode", __func__);
		return -1;
	}

	return 0;
}

static int
webauthn_assert_encode_cred_id(const fido_assert_t *assert, char **out)
{
	const void	*ptr = fido_assert_id_ptr(assert, 0);
	const size_t	 len = fido_assert_id_len(assert, 0);

	if (base64_encode(ptr, len, out) < 0) {
		warnx("%s: base64_encode", __func__);
		return -1;
	}

	return 0;
}

static int
webauthn_assert_encode_sig(const fido_assert_t *assert, char **out)
{
	const void	*ptr = fido_assert_sig_ptr(assert, 0);
	const size_t	 len = fido_assert_sig_len(assert, 0);

	if (base64_encode(ptr, len, out) < 0) {
		warnx("%s: base64_encode", __func__);
		return -1;
	}

	return 0;
}

static int
webauthn_assert_build_response(const struct webauthn_assert *wa, char **out)
{
	char	*authdata = NULL;
	char	*clientdata = NULL;
	char	*cred_id = NULL;
	char	*response = NULL;
	char	*sig = NULL;
	int	 n;
	int	 ok = -1;

	*out = NULL;

	if (webauthn_assert_encode_authdata(wa->assert, &authdata) < 0 ||
	    webauthn_assert_encode_clientdata(wa->clientdata, &clientdata) < 0 ||
	    webauthn_assert_encode_cred_id(wa->assert, &cred_id) < 0 ||
	    webauthn_assert_encode_sig(wa->assert, &sig) < 0) {
		warnx("%s: webauthn_assert_encode", __func__);
		goto fail;
	}

	if ((response = calloc(1, MAX_RESPONSE_LEN)) == NULL) {
		warnx("%s: calloc", __func__);
		goto fail;
	}

	n = snprintf(response, MAX_RESPONSE_LEN, "{\"requestId\":\"%s\","
	    "\"assertion\":{\"credentialId\":\"%s\",\"authenticatorData\":"
	    "\"%s\",\"clientDataJSON\":\"%s\",\"signature\":\"%s\"}}",
	    wa->request_id, cred_id, authdata, clientdata, sig);

	if (n < 0 || (size_t)n >= MAX_RESPONSE_LEN) {
		warnx("%s: snprintf", __func__);
		goto fail;
	}

	*out = response;
	response = NULL;
	ok = 0;
fail:
	free(authdata);
	free(clientdata);
	free(cred_id);
	free(response);
	free(sig);

	return ok;
}

int
webauthn_assert_finish(struct webauthn_assert *wa)
{
	char	*body = NULL;
	int	 ok = -1;

	if (webauthn_assert_build_response(wa, &body) < 0) {
		warnx("%s: webauthn_assert_build_response", __func__);
		goto fail;
	}

	if (http_request(&wa->finish, AUTH_FINISH_URL, body,
	    http_response_cookie(wa->begin)) < 0) {
		warnx("%s: http_request", __func__);
		goto fail;
	}

	if (webauthn_assert_finish_parse_response(wa->finish) < 0) {
		warnx("%s: webauthn_assert_finish_parse_response", __func__);
		goto fail;
	}

	ok = 0;
fail:
	free(body);

	return ok;
}