-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_file.c
More file actions
574 lines (496 loc) · 13 KB
/
Copy pathkey_file.c
File metadata and controls
574 lines (496 loc) · 13 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* SPDX-FileType: SOURCE
*
* SPDX-FileCopyrightText: 2026 Nick Kossifidis <mick@ics.forth.gr>
* SPDX-FileCopyrightText: 2026 ICS/FORTH
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* File-backed keypair backend.
*
* Key material lifecycle:
* keyp_open — decrypt PKCS#8 PEM → extract 32-byte scalar →
* copy into memfd_secret page → EVP_PKEY_free → guard page
* keyp_sign — unguard → EVP_PKEY_new_raw_private_key (transient) →
* re-guard → EVP_DigestSign → EVP_PKEY_free
* keyp_close — unguard → explicit_bzero → munmap
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <utils.h>
#include "keyp_backend.h"
#include "key_file.h"
#ifndef SYS_memfd_secret
#define SYS_memfd_secret 447
#endif
#define PASS_MAX_LEN 256
#define ED25519_KEY_LEN 32
/*********************\
* Secret memory (skey)*
\*********************/
struct skey_t {
uint8_t priv[ED25519_KEY_LEN];
};
static size_t
skey_pgsz(void)
{
long pgsz = sysconf(_SC_PAGESIZE);
if (pgsz <= 0)
pgsz = 4096;
return ((sizeof(struct skey_t) + (size_t)pgsz - 1) &
~((size_t)pgsz - 1));
}
static struct skey_t *
skey_alloc(void)
{
size_t sz = skey_pgsz();
int fd = (int)syscall(SYS_memfd_secret, 0UL);
void *p = MAP_FAILED;
if (fd < 0) {
ERR("memfd_secret: %s\n", strerror(errno));
return NULL;
}
if (ftruncate(fd, (off_t)sz) < 0) {
ERR("ftruncate memfd_secret: %s\n", strerror(errno));
close(fd);
return NULL;
}
p = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (p == MAP_FAILED) {
ERR("mmap memfd_secret: %s\n", strerror(errno));
return NULL;
}
memset(p, 0, sz);
if (mprotect(p, sz, PROT_NONE) < 0) {
ERR("mprotect PROT_NONE: %s\n", strerror(errno));
munmap(p, sz);
return NULL;
}
return (struct skey_t *)p;
}
/* Returns 0 on success, -1 if mprotect fails (key material exposed). */
static int
skey_guard(struct skey_t *sk)
{
if (!sk)
return 0;
if (mprotect(sk, skey_pgsz(), PROT_NONE) < 0) {
ERR("skey_guard: mprotect failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
/* Returns 0 on success, -1 if mprotect fails (access would SIGSEGV). */
static int
skey_unguard(struct skey_t *sk)
{
if (!sk)
return 0;
if (mprotect(sk, skey_pgsz(), PROT_READ | PROT_WRITE) < 0) {
ERR("skey_unguard: mprotect failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
static void
skey_free(struct skey_t *sk)
{
size_t sz = skey_pgsz();
if (!sk)
return;
(void)mprotect(sk, sz, PROT_READ | PROT_WRITE); /* best-effort */
explicit_bzero(sk, sz);
munmap(sk, sz);
}
/*********************\
* Passphrase via pinentry *
\*********************/
static const char * const pinentry_variants[] = {
"pinentry-gnome3",
"pinentry-gtk-2",
"pinentry-qt",
"pinentry-curses",
"pinentry-tty",
"pinentry",
NULL
};
/*
* Speak the Assuan protocol to a pinentry child.
* Returns 0 on success, -1 if no pinentry found or user cancelled.
*/
static int
pass_read(const char *desc, const char *prompt, int confirm,
char *buf, size_t bufsz)
{
int pin_to[2] = { -1, -1 };
int pin_from[2] = { -1, -1 };
char line[512];
FILE *to = NULL, *from = NULL;
pid_t pid = -1;
int i = 0, ret = -1;
memset(buf, 0, bufsz);
if (pipe2(pin_to, O_CLOEXEC) < 0 || pipe2(pin_from, O_CLOEXEC) < 0)
goto out;
pid = fork();
if (pid < 0)
goto out;
if (pid == 0) {
close(pin_to[1]);
close(pin_from[0]);
dup2(pin_to[0], STDIN_FILENO);
dup2(pin_from[1], STDOUT_FILENO);
close(pin_to[0]);
close(pin_from[1]);
for (i = 0; pinentry_variants[i]; i++)
execlp(pinentry_variants[i], pinentry_variants[i], NULL);
_exit(127);
}
close(pin_to[0]); pin_to[0] = -1;
close(pin_from[1]); pin_from[1] = -1;
to = fdopen(pin_to[1], "w");
from = fdopen(pin_from[0], "r");
if (!to || !from)
goto out;
if (!fgets(line, sizeof(line), from) || strncmp(line, "OK", 2) != 0)
goto out;
fprintf(to, "SETDESC %s\n", desc);
fflush(to);
if (!fgets(line, sizeof(line), from) || strncmp(line, "OK", 2) != 0)
goto out;
fprintf(to, "SETPROMPT %s\n", prompt);
fflush(to);
if (!fgets(line, sizeof(line), from) || strncmp(line, "OK", 2) != 0)
goto out;
if (confirm) {
fprintf(to, "SETREPEAT Confirm passphrase\n");
fflush(to);
if (!fgets(line, sizeof(line), from) ||
strncmp(line, "OK", 2) != 0)
goto out;
}
fprintf(to, "GETPIN\n");
fflush(to);
/* Skip S (status/informational) lines — e.g. "S PIN_REPEATED" */
do {
if (!fgets(line, sizeof(line), from)) {
ERR("pinentry: lost connection after GETPIN\n");
goto out;
}
} while (strncmp(line, "S ", 2) == 0);
if (strncmp(line, "D ", 2) == 0) {
size_t len = strlen(line + 2);
if (len > 0 && line[2 + len - 1] == '\n') {
line[2 + len - 1] = '\0';
len--;
}
if (len >= bufsz)
len = bufsz - 1;
memcpy(buf, line + 2, len);
buf[len] = '\0';
explicit_bzero(line, sizeof(line));
if (!fgets(line, sizeof(line), from)) { /* consume trailing OK */ }
ret = 0;
} else if (strncmp(line, "ERR ", 4) == 0) {
ERR("pinentry: %s", line + 4);
} else {
ERR("pinentry: unexpected GETPIN response: %s", line);
}
fprintf(to, "BYE\n");
fflush(to);
out:
explicit_bzero(line, sizeof(line));
if (to) { fclose(to); pin_to[1] = -1; }
if (from) { fclose(from); pin_from[0] = -1; }
if (pin_to[0] >= 0) close(pin_to[0]);
if (pin_to[1] >= 0) close(pin_to[1]);
if (pin_from[0] >= 0) close(pin_from[0]);
if (pin_from[1] >= 0) close(pin_from[1]);
if (pid > 0)
waitpid(pid, NULL, 0);
return ret;
}
/*
* PEM passphrase callback.
*
* The OpenSSL-supplied `buf` cannot be cleared by us here — PEM_read_PrivateKey
* needs to read it after we return. OpenSSL ≥ 1.1 cleanses this buffer
* itself once decryption is done. Our copy of the passphrase (`userdata`,
* i.e. the `pass` array in kf_open) is explicit_bzero'd in kf_open's
* cleanup, so the only window in which the passphrase is in plaintext memory
* is bounded by PEM_read_PrivateKey's runtime.
*
* SETDESC / SETPROMPT in pass_read assume hardcoded literal arguments
* (no user-controlled strings); newline injection is not a concern as long
* as that holds.
*/
static int
pem_pass_cb(char *buf, int size, int rwflag, void *userdata)
{
const char *pass = (const char *)userdata;
int len = (int)strlen(pass);
(void)rwflag;
if (len > size)
len = size;
memcpy(buf, pass, (size_t)len);
return len;
}
/**********************\
* Backend private state *
\**********************/
struct kf_ctx {
struct skey_t *skey;
uint8_t pub[ED25519_KEY_LEN];
size_t pub_len;
};
/***********************\
* Vtable implementations *
\***********************/
static int
kf_open(struct keyp_ops *ops, int flags)
{
struct kf_ctx *kf = NULL;
EVP_PKEY_CTX *pctx = NULL;
EVP_PKEY *evp_key = NULL;
FILE *fp = NULL;
struct stat st;
const char *path = (const char *)ops->ctx;
char pass[PASS_MAX_LEN];
char pub_path[PATH_MAX];
uint8_t raw_priv[ED25519_KEY_LEN];
uint8_t raw_pub[ED25519_KEY_LEN];
size_t raw_priv_len = sizeof(raw_priv);
size_t raw_pub_len = sizeof(raw_pub);
int generate = 0, ret = -1;
/* Null ctx immediately so kf_close is safe if we fail before
* the ops->ctx = kf assignment at the end. */
ops->ctx = NULL;
kf = malloc(sizeof(struct kf_ctx));
if (!kf)
return -1;
memset(kf, 0, sizeof(struct kf_ctx));
kf->skey = skey_alloc();
if (!kf->skey)
goto cleanup;
generate = (flags & KEYP_OPEN_FORCE_NEW);
if (!generate) {
if (stat(path, &st) == 0) {
generate = 0;
} else if (errno == ENOENT) {
generate = 1;
} else {
ERR("stat %s: %s\n", path, strerror(errno));
goto cleanup;
}
}
if (generate) {
if (pass_read("Generate new Ed25519 signing key",
"New passphrase", 1,
pass, sizeof(pass)) < 0) {
ERR("Failed to read passphrase\n");
goto cleanup;
}
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL);
if (!pctx || EVP_PKEY_keygen_init(pctx) <= 0 ||
EVP_PKEY_keygen(pctx, &evp_key) <= 0) {
ERR("Key generation failed\n");
goto cleanup;
}
EVP_PKEY_CTX_free(pctx);
pctx = NULL;
if (EVP_PKEY_get_raw_private_key(evp_key, raw_priv,
&raw_priv_len) != 1 ||
EVP_PKEY_get_raw_public_key(evp_key, raw_pub,
&raw_pub_len) != 1) {
ERR("Failed to extract raw key material\n");
goto cleanup;
}
/* Write encrypted PKCS#8 PEM, mode 0600 */
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0) {
ERR("open %s: %s\n", path, strerror(errno));
goto cleanup;
}
fp = fdopen(fd, "w");
if (!fp) {
ERR("fdopen %s: %s\n", path, strerror(errno));
close(fd);
goto cleanup;
}
if (PEM_write_PrivateKey(fp, evp_key,
EVP_aes_256_cbc(),
(const unsigned char *)pass,
(int)strlen(pass),
NULL, NULL) != 1) {
ERR("Failed to write encrypted private key\n");
fclose(fp);
fp = NULL;
goto cleanup;
}
fclose(fp);
fp = NULL;
}
snprintf(pub_path, sizeof(pub_path), "%.*s.pub",
(int)(sizeof(pub_path) - 5), path);
fp = fopen(pub_path, "wb");
if (!fp) {
WRN("Could not write public key to %s: %s\n",
pub_path, strerror(errno));
} else {
if (fwrite(raw_pub, 1, raw_pub_len, fp) != raw_pub_len)
WRN("Short write to %s\n", pub_path);
fclose(fp);
fp = NULL;
}
INF("Generated Ed25519 key pair:\n");
INF(" Private : %s\n", path);
INF(" Public : %s.pub\n (", path);
for (size_t i = 0; i < raw_pub_len; i++)
INF("%02x", raw_pub[i]);
INF(")\n");
} else {
if (pass_read("Unlock Ed25519 signing key",
"Passphrase", 0,
pass, sizeof(pass)) < 0) {
ERR("Failed to read passphrase\n");
goto cleanup;
}
fp = fopen(path, "r");
if (!fp) {
ERR("fopen %s: %s\n", path, strerror(errno));
goto cleanup;
}
evp_key = PEM_read_PrivateKey(fp, NULL, pem_pass_cb, pass);
fclose(fp);
fp = NULL;
if (!evp_key) {
ERR("Failed to load private key from %s"
" (wrong passphrase?)\n", path);
goto cleanup;
}
if (EVP_PKEY_get_raw_private_key(evp_key, raw_priv,
&raw_priv_len) != 1 ||
EVP_PKEY_get_raw_public_key(evp_key, raw_pub,
&raw_pub_len) != 1) {
ERR("Failed to extract raw key material\n");
goto cleanup;
}
}
/* Scalar → guarded skey; public key → plain kf_ctx */
if (skey_unguard(kf->skey) < 0)
goto cleanup;
memcpy(kf->skey->priv, raw_priv, raw_priv_len);
if (skey_guard(kf->skey) < 0)
goto cleanup;
memcpy(kf->pub, raw_pub, raw_pub_len);
kf->pub_len = raw_pub_len;
ops->ctx = kf;
ret = 0;
cleanup:
explicit_bzero(raw_priv, sizeof(raw_priv));
explicit_bzero(pass, sizeof(pass));
if (evp_key)
EVP_PKEY_free(evp_key);
if (pctx)
EVP_PKEY_CTX_free(pctx);
if (fp)
fclose(fp);
if (ret < 0) {
skey_free(kf ? kf->skey : NULL);
free(kf);
}
return ret;
}
static const uint8_t *
kf_get_pubkey(struct keyp_ops *ops)
{
const struct kf_ctx *kf = (const struct kf_ctx *)ops->ctx;
return (kf && kf->pub_len > 0) ? kf->pub : NULL;
}
static int
kf_sign(struct keyp_ops *ops,
const uint8_t *msg, size_t msglen, uint8_t *sig_out)
{
struct kf_ctx *kf = (struct kf_ctx *)ops->ctx;
EVP_PKEY *evp_key = NULL;
EVP_MD_CTX *mdctx = NULL;
size_t siglen = 64;
int ret = -1;
if (!kf || !kf->skey)
return -1;
/* Unguard only for the memcpy inside EVP_PKEY_new_raw_private_key */
if (skey_unguard(kf->skey) < 0)
return -1;
evp_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL,
kf->skey->priv, ED25519_KEY_LEN);
if (skey_guard(kf->skey) < 0) {
/* Cannot re-protect key page — zero it and abort */
explicit_bzero(kf->skey, skey_pgsz());
EVP_PKEY_free(evp_key);
return -1;
}
if (!evp_key) {
ERR("EVP_PKEY_new_raw_private_key failed\n");
return -1;
}
mdctx = EVP_MD_CTX_new();
if (!mdctx)
goto out;
if (EVP_DigestSignInit(mdctx, NULL, NULL, NULL, evp_key) == 1 &&
EVP_DigestSign(mdctx, sig_out, &siglen, msg, msglen) == 1)
ret = 0;
else
ERR("Ed25519 signing failed\n");
EVP_MD_CTX_free(mdctx);
out:
EVP_PKEY_free(evp_key);
return ret;
}
static void
kf_close(struct keyp_ops *ops)
{
struct kf_ctx *kf = (struct kf_ctx *)ops->ctx;
if (!kf)
return;
skey_free(kf->skey);
explicit_bzero(kf->pub, sizeof(kf->pub));
free(kf);
ops->ctx = NULL;
}
/********************\
* Backend constructor *
\********************/
struct keyp_ops *
keyp_file_backend(const char *path)
{
struct keyp_ops *ops = NULL;
ops = malloc(sizeof(struct keyp_ops));
if (!ops)
return NULL;
memset(ops, 0, sizeof(struct keyp_ops));
/*
* Stash path in ctx; kf_open replaces it with the allocated
* kf_ctx once the key is loaded or generated.
*/
ops->ctx = (void *)path;
ops->algo = CRYPTO_ALGO_ED25519;
ops->keyp_open = kf_open;
ops->keyp_get_pubkey = kf_get_pubkey;
ops->keyp_sign = kf_sign;
ops->keyp_close = kf_close;
return ops;
}