-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclientdata.c
47 lines (38 loc) · 950 Bytes
/
clientdata.c
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
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"
#include "clientdata.h"
#include "param.h"
char *
clientdata_json(const char *type, const char *challenge)
{
char buf[MAX_CLIENTDATA_JSON_LEN];
int n;
int ok = -1;
void *ptr = NULL;
size_t len = 0;
memset(buf, 0, sizeof(buf));
if (base64_decode(challenge, &ptr, &len) < 0 || len < 32) {
warnx("%s: challenge", __func__);
goto fail;
}
if ((n = snprintf(buf, sizeof(buf), "{\"type\":\"%s\",\"challenge\":"
"\"%s\",\"origin\":\"%s\",\"crossOrigin\":false}", type, challenge,
ORIGIN)) < 0 || (size_t)n >= sizeof(buf)) {
warnx("%s: snprintf", __func__);
goto fail;
}
ok = 0;
fail:
free(ptr);
if (ok < 0)
return NULL;
return strdup(buf);
}