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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/* $Id: acctproc.c,v 1.5 2016/09/01 00:35:21 florian Exp $ */
/*
* Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include "extern.h"
#include "rsa.h"
/*
* Converts a BIGNUM to the form used in JWK.
* This is essentially a base64-encoded big-endian binary string
* representation of the number.
*/
static char *
bn2string(const BIGNUM *bn)
{
int len;
char *buf, *bbuf;
/* Extract big-endian representation of BIGNUM. */
len = BN_num_bytes(bn);
if (NULL == (buf = malloc(len))) {
warn("malloc");
return (NULL);
} else if (len != BN_bn2bin(bn, (unsigned char *)buf)) {
warnx("BN_bn2bin");
free(buf);
return (NULL);
}
/* Convert to base64url. */
if (NULL == (bbuf = base64buf_url(buf, len))) {
warnx("base64buf_url");
free(buf);
return (NULL);
}
free(buf);
return (bbuf);
}
/*
* Extract the relevant RSA components from the key and create the JSON
* thumbprint from them.
*/
static char *
op_thumb_rsa(EVP_PKEY *pkey)
{
char *exp, *mod, *json;
RSA *r;
exp = mod = json = NULL;
if (NULL == (r = EVP_PKEY_get1_RSA(pkey)))
warnx("EVP_PKEY_get1_RSA");
else if (NULL == (mod = bn2string(r->n)))
warnx("bn2string");
else if (NULL == (exp = bn2string(r->e)))
warnx("bn2string");
else if (NULL == (json = json_fmt_thumb_rsa(exp, mod)))
warnx("json_fmt_thumb_rsa");
free(exp);
free(mod);
return (json);
}
/*
* The thumbprint operation is used for the challenge sequence.
*/
static int
op_thumbprint(int fd, EVP_PKEY *pkey)
{
char *thumb, *dig64;
int rc;
unsigned int digsz;
unsigned char *dig;
EVP_MD_CTX *ctx;
rc = 0;
thumb = dig64 = NULL;
dig = NULL;
ctx = NULL;
/* Construct the thumbprint input itself. */
switch (EVP_PKEY_type(pkey->type)) {
case EVP_PKEY_RSA:
if (NULL != (thumb = op_thumb_rsa(pkey)))
break;
goto out;
default:
warnx("EVP_PKEY_type: unknown key type");
goto out;
}
/*
* Compute the SHA256 digest of the thumbprint then
* base64-encode the digest itself.
* If the reader is closed when we write, ignore it (we'll pick
* it up in the read loop).
*/
if (NULL == (dig = malloc(EVP_MAX_MD_SIZE))) {
warn("malloc");
goto out;
} else if (NULL == (ctx = EVP_MD_CTX_create())) {
warnx("EVP_MD_CTX_create");
goto out;
} else if ( ! EVP_DigestInit_ex(ctx, EVP_sha256(), NULL)) {
warnx("EVP_SignInit_ex");
goto out;
} else if ( ! EVP_DigestUpdate(ctx, thumb, strlen(thumb))) {
warnx("EVP_SignUpdate");
goto out;
} else if ( ! EVP_DigestFinal_ex(ctx, dig, &digsz)) {
warnx("EVP_SignFinal");
goto out;
} else if (NULL == (dig64 = base64buf_url((char *)dig, digsz))) {
warnx("base64buf_url");
goto out;
} else if (writestr(fd, COMM_THUMB, dig64) < 0)
goto out;
rc = 1;
out:
if (NULL != ctx)
EVP_MD_CTX_destroy(ctx);
free(thumb);
free(dig);
free(dig64);
return (rc);
}
static int
op_sign_rsa(char **head, char **prot, EVP_PKEY *pkey, const char *nonce)
{
RSA *r;
char *exp, *mod;
int rc;
*head = *prot = exp = mod = NULL;
rc = 0;
/*
* First, extract relevant portions of our private key.
* Then construct the public header.
* Finally, format the header combined with the nonce.
*/
if (NULL == (r = EVP_PKEY_get1_RSA(pkey)))
warnx("EVP_PKEY_get1_RSA");
else if (NULL == (mod = bn2string(r->n)))
warnx("bn2string");
else if (NULL == (exp = bn2string(r->e)))
warnx("bn2string");
else if (NULL == (*head = json_fmt_header_rsa(exp, mod)))
warnx("json_fmt_header_rsa");
else if (NULL == (*prot = json_fmt_protected_rsa(exp, mod, nonce)))
warnx("json_fmt_protected_rsa");
else
rc = 1;
free(exp);
free(mod);
return (rc);
}
/*
* Operation to sign a message with the account key.
* This requires the sender ("fd") to provide the payload and a nonce.
*/
static int
op_sign(int fd, EVP_PKEY *pkey)
{
char *nonce, *pay,
*pay64, *prot, *prot64, *head,
*sign, *dig64, *fin;
int cc, rc;
unsigned int digsz;
unsigned char *dig;
EVP_MD_CTX *ctx;
rc = 0;
pay = nonce = head = fin =
sign = prot = prot64 = pay64 = dig64 = NULL;
dig = NULL;
ctx = NULL;
/* Read our payload and nonce from the requestor. */
if (NULL == (pay = readstr(fd, COMM_PAY)))
goto out;
else if (NULL == (nonce = readstr(fd, COMM_NONCE)))
goto out;
/* Base64-encode the payload. */
if (NULL == (pay64 = base64buf_url(pay, strlen(pay)))) {
warnx("base64buf_url");
goto out;
}
switch (EVP_PKEY_type(pkey->type)) {
case EVP_PKEY_RSA:
if ( ! op_sign_rsa(&head, &prot, pkey, nonce))
goto out;
break;
default:
warnx("EVP_PKEY_type");
goto out;
}
/* The header combined with the nonce, base64. */
if (NULL == (prot64 = base64buf_url(prot, strlen(prot)))) {
warnx("base64buf_url");
goto out;
}
/* Now the signature material. */
cc = asprintf(&sign, "%s.%s", prot64, pay64);
if (-1 == cc) {
warn("asprintf");
sign = NULL;
goto out;
}
if (NULL == (dig = malloc(EVP_PKEY_size(pkey)))) {
warn("malloc");
goto out;
}
/*
* Here we go: using our RSA key as merged into the envelope,
* sign a SHA256 digest of our message.
*/
if (NULL == (ctx = EVP_MD_CTX_create())) {
warnx("EVP_MD_CTX_create");
goto out;
} else if ( ! EVP_SignInit_ex(ctx, EVP_sha256(), NULL)) {
warnx("EVP_SignInit_ex");
goto out;
} else if ( ! EVP_SignUpdate(ctx, sign, strlen(sign))) {
warnx("EVP_SignUpdate");
goto out;
} else if ( ! EVP_SignFinal(ctx, dig, &digsz, pkey)) {
warnx("EVP_SignFinal");
goto out;
} else if (NULL == (dig64 = base64buf_url((char *)dig, digsz))) {
warnx("base64buf_url");
goto out;
}
/*
* Write back in the correct JSON format.
* If the reader is closed, just ignore it (we'll pick it up
* when we next enter the read loop).
*/
if (NULL == (fin = json_fmt_signed(head, prot64, pay64, dig64))) {
warnx("json_fmt_signed");
goto out;
} else if (writestr(fd, COMM_REQ, fin) < 0)
goto out;
rc = 1;
out:
if (NULL != ctx)
EVP_MD_CTX_destroy(ctx);
free(pay);
free(sign);
free(pay64);
free(nonce);
free(head);
free(prot);
free(prot64);
free(dig);
free(dig64);
free(fin);
return (rc);
}
int
acctproc(int netsock, const char *acctkey, int newacct)
{
FILE *f;
EVP_PKEY *pkey;
long lval;
enum acctop op;
unsigned char rbuf[64];
int rc, cc;
mode_t prev;
f = NULL;
pkey = NULL;
rc = 0;
/*
* First, open our private key file read-only or write-only if
* we're creating from scratch.
* Set our umask to be maximally restrictive.
*/
prev = umask((S_IWUSR | S_IXUSR) | S_IRWXG | S_IRWXO);
f = fopen(acctkey, newacct ? "wx" : "r");
umask(prev);
if (NULL == f) {
warn("%s", acctkey);
goto out;
}
/* File-system, user, and sandbox jailing. */
ERR_load_crypto_strings();
if (pledge("stdio", NULL) == -1) {
warn("pledge");
goto out;
}
/*
* Seed our PRNG with data from arc4random().
* Do this until we're told it's ok and use increments of 64
* bytes (arbitrarily).
*/
while (0 == RAND_status()) {
arc4random_buf(rbuf, sizeof(rbuf));
RAND_seed(rbuf, sizeof(rbuf));
}
if (newacct) {
if (NULL == (pkey = rsa_key_create(f, acctkey)))
goto out;
dodbg("%s: generated RSA account key", acctkey);
} else {
if (NULL == (pkey = rsa_key_load(f, acctkey)))
goto out;
doddbg("%s: loaded RSA account key", acctkey);
}
fclose(f);
f = NULL;
/* Notify the netproc that we've started up. */
if (0 == (cc = writeop(netsock, COMM_ACCT_STAT, ACCT_READY)))
rc = 1;
if (cc <= 0)
goto out;
/*
* Now we wait for requests from the network-facing process.
* It might ask us for our thumbprint, for example, or for us to
* sign a message.
*/
for (;;) {
op = ACCT__MAX;
if (0 == (lval = readop(netsock, COMM_ACCT)))
op = ACCT_STOP;
else if (ACCT_SIGN == lval || ACCT_THUMBPRINT == lval)
op = lval;
if (ACCT__MAX == op) {
warnx("unknown operation from netproc");
goto out;
} else if (ACCT_STOP == op)
break;
switch (op) {
case (ACCT_SIGN):
if (op_sign(netsock, pkey))
break;
warnx("op_sign");
goto out;
case (ACCT_THUMBPRINT):
if (op_thumbprint(netsock, pkey))
break;
warnx("op_thumbprint");
goto out;
default:
abort();
}
}
rc = 1;
out:
close(netsock);
if (NULL != f)
fclose(f);
if (NULL != pkey)
EVP_PKEY_free(pkey);
ERR_print_errors_fp(stderr);
ERR_free_strings();
return (rc);
}
|