-
Notifications
You must be signed in to change notification settings - Fork 56
/
tangd.c
216 lines (177 loc) · 6.43 KB
/
tangd.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
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
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/*
* Copyright (c) 2016 Red Hat, Inc.
* Author: Nathaniel McCallum <npmccallum@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "http.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <jose/jose.h>
#include "keys.h"
static void
str_cleanup(char **str)
{
if (str)
free(*str);
}
static int
adv(enum http_method method, const char *path, const char *body,
regmatch_t matches[], void *misc)
{
__attribute__((cleanup(str_cleanup))) char *adv = NULL;
__attribute__((cleanup(str_cleanup))) char *thp = NULL;
__attribute__((cleanup(cleanup_tang_keys_info))) struct tang_keys_info *tki = NULL;
json_auto_t *jws = NULL;
const char *jwkdir = misc;
tki = read_keys(jwkdir);
if (!tki || tki->m_keys_count == 0) {
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
}
if (matches[1].rm_so < matches[1].rm_eo) {
size_t size = matches[1].rm_eo - matches[1].rm_so;
thp = strndup(&path[matches[1].rm_so], size);
if (!thp)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
}
jws = find_jws(tki, thp);
if (!jws) {
return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
}
adv = json_dumps(jws, 0);
if (!adv)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return http_reply(HTTP_STATUS_OK,
"Content-Type: application/jose+json\r\n"
"Content-Length: %zu\r\n"
"\r\n%s", strlen(adv), adv);
}
static int
rec(enum http_method method, const char *path, const char *body,
regmatch_t matches[], void *misc)
{
__attribute__((cleanup(str_cleanup))) char *enc = NULL;
__attribute__((cleanup(str_cleanup))) char *thp = NULL;
__attribute__((cleanup(cleanup_tang_keys_info))) struct tang_keys_info *tki = NULL;
size_t size = matches[1].rm_eo - matches[1].rm_so;
const char *jwkdir = misc;
json_auto_t *jwk = NULL;
json_auto_t *req = NULL;
json_auto_t *rep = NULL;
const char *alg = NULL;
const char *kty = NULL;
const char *d = NULL;
/*
* Parse and validate the request JWK
*/
req = json_loads(body, 0, NULL);
if (!req)
return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
if (!jose_jwk_prm(NULL, req, false, "deriveKey"))
return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
if (json_unpack(req, "{s:s,s?s}", "kty", &kty, "alg", &alg) < 0)
return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
if (strcmp(kty, "EC") != 0)
return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
if (alg && strcmp(alg, "ECMR") != 0)
return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
/*
* Parse and validate the server-side JWK
*/
tki = read_keys(jwkdir);
if (!tki || tki->m_keys_count == 0) {
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
}
thp = strndup(&path[matches[1].rm_so], size);
if (!thp)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
jwk = find_jwk(tki, thp);
if (!jwk)
return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
if (!jose_jwk_prm(NULL, jwk, true, "deriveKey"))
return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
if (json_unpack(jwk, "{s:s,s?s}", "d", &d, "alg", &alg) < 0)
return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
if (alg && strcmp(alg, "ECMR") != 0)
return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
/*
* Perform the exchange and return
*/
rep = jose_jwk_exc(NULL, jwk, req);
if (!rep)
return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
if (json_object_set_new(rep, "alg", json_string("ECMR")) < 0)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
if (json_object_set_new(rep, "key_ops", json_pack("[s]", "deriveKey")) < 0)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
enc = json_dumps(rep, JSON_SORT_KEYS | JSON_COMPACT);
if (!enc)
return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return http_reply(HTTP_STATUS_OK,
"Content-Type: application/jwk+json\r\n"
"Content-Length: %zu\r\n"
"\r\n%s", strlen(enc), enc);
}
static struct http_dispatch dispatch[] = {
{ adv, 1 << HTTP_GET, 2, "^/+adv/+([0-9A-Za-z_-]+)$" },
{ adv, 1 << HTTP_GET, 2, "^/+adv/*$" },
{ rec, 1 << HTTP_POST, 2, "^/+rec/+([0-9A-Za-z_-]+)$" },
{}
};
int
main(int argc, char *argv[])
{
struct http_state state = { .dispatch = dispatch, .misc = argv[1] };
struct http_parser parser = { .data = &state };
struct stat st = {};
char req[4096] = {};
size_t rcvd = 0;
int r = 0;
http_parser_init(&parser, HTTP_REQUEST);
if (argc != 2) {
fprintf(stderr, "Usage: %s <jwkdir>\n", argv[0]);
return EXIT_FAILURE;
}
if (stat(argv[1], &st) != 0) {
fprintf(stderr, "Error calling stat() on path: %s: %m\n", argv[1]);
return EXIT_FAILURE;
}
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "Path is not a directory: %s\n", argv[1]);
return EXIT_FAILURE;
}
for (;;) {
r = read(STDIN_FILENO, &req[rcvd], sizeof(req) - rcvd - 1);
if (r == 0)
return rcvd > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
if (r < 0)
return EXIT_FAILURE;
rcvd += r;
r = http_parser_execute(&parser, &http_settings, req, rcvd);
if (parser.http_errno != 0) {
fprintf(stderr, "HTTP Parsing Error: %s\n",
http_errno_description(parser.http_errno));
return EXIT_FAILURE;
}
memmove(req, &req[r], rcvd - r);
rcvd -= r;
}
return EXIT_SUCCESS;
}