forked from CESNET/netopeer2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystored.c
626 lines (540 loc) · 18.5 KB
/
keystored.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/**
* @file keystored.c
* @author Michal Vasko <[email protected]>
* @brief keystored plugin for sysrepo
*
* Copyright (c) 2016 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <sysrepo.h>
#include <sysrepo/plugins.h>
#include "config.h"
struct keystored_ctx {
sr_subscription_ctx_t *subscription;
sr_session_ctx_t *session;
};
static char *
keystored_read_pubkey_skip_type(const char *path)
{
char *content = NULL;
unsigned long size;
FILE *file;
file = fopen(path, "r");
if (!file) {
SRP_LOG_ERR("Opening the file \"%s\" failed (%s).", path, strerror(errno));
return NULL;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
content = malloc(size + 1);
if (!content) {
SRP_LOG_ERR("Memory allocation error (%s).", strerror(errno));
goto error;
}
if (size < 55) {
SRP_LOG_ERR("File \"%s\" is not a public key in PEM format.", path);
goto error;
}
if (fread(content, 1, 27, file) < 27) {
SRP_LOG_ERR("Reading from \"%s\" failed (%s).", path, strerror(ferror(file)));
goto error;
}
size -= 27;
if (strncmp(content, "-----BEGIN PUBLIC KEY-----\n", 27)) {
SRP_LOG_ERR("File \"%s\" is not a public key in PEM format.", path);
goto error;
}
if (fread(content, 1, size, file) < size) {
SRP_LOG_ERR("Reading from \"%s\" failed (%s).", path, strerror(ferror(file)));
goto error;
}
if (content[size - 1] == '\n') {
--size;
}
if (strncmp(content + size - 24, "-----END PUBLIC KEY-----", 24)) {
SRP_LOG_ERR("File \"%s\" is not a public key in PEM format.", path);
goto error;
}
size -= 24;
if (content[size - 1] == '\n') {
--size;
}
content[size] = '\0';
fclose(file);
return content;
error:
fclose(file);
free(content);
return NULL;
}
static int
ks_privkey_change_cb(sr_session_ctx_t *UNUSED(session), const char *UNUSED(module_name), sr_notif_event_t UNUSED(event),
void *UNUSED(private_ctx))
{
/* TODO forbid adding keys this way */
return SR_ERR_OK;
}
static int
ks_cert_change_cb(sr_session_ctx_t *UNUSED(session), const char *UNUSED(module_name), sr_notif_event_t UNUSED(event),
void *UNUSED(private_ctx))
{
/* nothing to do */
return 0;
}
static int
ks_privkey_get_cb(const char *xpath, sr_val_t **values, size_t *values_cnt, void *UNUSED(private_ctx))
{
int ret;
const char *name;
char *path;
sr_val_t *val = NULL;
SRP_LOG_INF("Providing node \"%s\".", xpath);
name = strstr(xpath, "private-key[name='");
if (!name) {
SRP_LOG_ERR("Internal error (%s:%d).", __FILE__, __LINE__);
return SR_ERR_INTERNAL;
}
name += 18;
if (asprintf(&path, "%s/%.*s.pub.pem", KEYSTORED_KEYS_DIR, (int)(strchr(name, '\'') - name), name) == -1) {
SRP_LOG_ERR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
return SR_ERR_NOMEM;
}
if (access(path, F_OK) == -1) {
SRP_LOG_ERR("File \"%s\" could not be accessed (%s).", path, strerror(errno));
ret = SR_ERR_IO;
goto error;
}
if (!strcmp(xpath + strlen(xpath) - 9, "algorithm")) {
/* algorithm */
val = calloc(1, sizeof *val);
if (!val) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto error;
}
val->xpath = strdup(xpath);
val->type = SR_IDENTITYREF_T;
val->data.identityref_val = strdup("rsa");
if (!val->xpath || !val->data.identityref_val) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto error;
}
} else if (!strcmp(xpath + strlen(xpath) - 10, "key-length")) {
/* no key-length */
} else if (!strcmp(xpath + strlen(xpath) - 10, "public-key")) {
/* public-key */
val = calloc(1, sizeof *val);
if (!val) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto error;
}
val->xpath = strdup(xpath);
val->type = SR_BINARY_T;
val->data.binary_val = keystored_read_pubkey_skip_type(path);
if (!val->xpath || !val->data.binary_val) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto error;
}
} else {
SRP_LOG_ERR("Unknown node \"%s\" value requested.", xpath);
ret = SR_ERR_INTERNAL;
goto error;
}
if (val) {
*values = val;
*values_cnt = 1;
}
return SR_ERR_OK;
error:
sr_free_val(val);
free(path);
return ret;
}
struct thread_arg {
sr_session_ctx_t *session;
char *key_name;
};
static void *
ks_privkey_add_thread(void *arg)
{
struct thread_arg *targ = (struct thread_arg *)arg;
int ret;
char *xpath;
if (asprintf(&xpath, "/ietf-keystore:keystore/private-keys/private-key[name='%s']", targ->key_name) == -1) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
return NULL;
}
ret = sr_session_switch_ds(targ->session, SR_DS_RUNNING);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to switch datastore (%s).", sr_strerror(ret));
goto cleanup;
}
ret = sr_set_item(targ->session, xpath, NULL, 0);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to set item (%s).", sr_strerror(ret));
goto cleanup;
}
ret = sr_commit(targ->session);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to commit (%s).", sr_strerror(ret));
goto cleanup;
}
ret = sr_session_switch_ds(targ->session, SR_DS_STARTUP);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to switch datastore (%s).", sr_strerror(ret));
goto cleanup;
}
ret = sr_set_item(targ->session, xpath, NULL, 0);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to set item (%s).", sr_strerror(ret));
goto cleanup;
}
ret = sr_commit(targ->session);
if (ret != SR_ERR_OK) {
SRP_LOG_ERR("Failed to commit (%s).", sr_strerror(ret));
goto cleanup;
}
cleanup:
free(xpath);
free(targ->key_name);
free(targ);
return NULL;
}
static int
ks_privkey_add(sr_session_ctx_t *session, const char *key_name)
{
int ret;
pthread_t tid;
struct thread_arg *targ;
targ = malloc(sizeof *targ);
if (!targ) {
SRP_LOG_ERR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
return 1;
}
targ->session = session;
targ->key_name = strdup(key_name);
if (!targ->key_name) {
SRP_LOG_ERR("Memoy allocation failed (%s:%d).", __FILE__, __LINE__);
return 1;
}
ret = pthread_create(&tid, NULL, ks_privkey_add_thread, targ);
if (ret) {
SRP_LOG_ERR("Creating new thread failed (%s).", strerror(ret));
return 1;
}
pthread_detach(tid);
return 0;
}
static int
ks_privkey_gen_cb(const char *UNUSED(xpath), const sr_node_t *input, const size_t input_cnt, sr_node_t **UNUSED(output),
size_t *UNUSED(output_cnt), void *private_ctx)
{
struct keystored_ctx *ctx = (struct keystored_ctx *)private_ctx;
pid_t pid;
int ret = SR_ERR_OK, status;
char *priv_path = NULL, *pub_path = NULL, len_arg[27];
if ((input_cnt < 2) || (input[0].type != SR_STRING_T) || (input[1].type != SR_IDENTITYREF_T)
|| ((input_cnt == 3) && (input[2].type != SR_UINT32_T))) {
SRP_LOG_ERR_MSG("Unexpected input from sysrepo.");
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (strcmp(input[1].data.string_val, "rsa")) {
SRP_LOG_ERR_MSG("Generating EC private keys not supported yet.");
ret = SR_ERR_UNSUPPORTED;
goto cleanup;
}
/* TODO check that name is unique */
if (input_cnt == 3) {
sprintf(len_arg, "rsa_keygen_bits:%u", input[2].data.uint32_val);
} else {
len_arg[0] = '\0';
}
priv_path = malloc(strlen(KEYSTORED_KEYS_DIR) + 1 + strlen(input[0].data.string_val) + 4 + 1);
pub_path = malloc(strlen(KEYSTORED_KEYS_DIR) + 1 + strlen(input[0].data.string_val) + 4 + 4 + 1);
if (!priv_path || !pub_path) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto cleanup;
}
sprintf(priv_path, "%s/%s.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
sprintf(pub_path, "%s/%s.pub.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
if (!(pid = fork())) {
/* child */
if (len_arg[0]) {
execl(OPENSSL_EXECUTABLE, "genrsa", "-out", priv_path, len_arg, NULL);
} else {
execl(OPENSSL_EXECUTABLE, "genrsa", "-out", priv_path, NULL);
}
SRP_LOG_ERR("Exec failed (%s).", strerror(errno));
exit(1);
}
/* parent */
if (pid == -1) {
SRP_LOG_ERR("Fork failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
waitpid(pid, &status, 0);
/* since we are not catching SIGCHLD, we cannot do more... seriously Linux?
if (waitpid(pid, &status, 0) == -1) {
SRP_LOG_ERR("Waiting for child process failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (!WIFEXITED(status)) {
SRP_LOG_ERR_MSG("Child process ended in a non-standard way.");
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (WEXITSTATUS(status)) {
SRP_LOG_ERR("OpenSSL utility returned %d.", WEXITSTATUS(status));
ret = SR_ERR_INTERNAL;
goto cleanup;
}*/
if (chmod(priv_path, 00600) == -1) {
SRP_LOG_ERR("Changing private key permissions failed (%s).", strerror(errno));
ret = SR_ERR_IO;
goto cleanup;
}
if (!(pid = fork())) {
/* child */
execl(OPENSSL_EXECUTABLE, "rsa", "-pubout", "-in", priv_path, "-out", pub_path, NULL);
SRP_LOG_ERR("Exec failed (%s).", strerror(errno));
exit(1);
}
/* parent */
if (pid == -1) {
SRP_LOG_ERR("Fork failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
waitpid(pid, &status, 0);
/*
if (waitpid(pid, &status, 0) == -1) {
SRP_LOG_ERR("Waiting for child process failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (!WIFEXITED(status)) {
SRP_LOG_ERR_MSG("Child process ended in a non-standard way.");
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (WEXITSTATUS(status)) {
SRP_LOG_ERR("OpenSSL utility returned %d.", WEXITSTATUS(status));
ret = SR_ERR_INTERNAL;
goto cleanup;
}*/
/* add the key to the configuration */
ks_privkey_add(ctx->session, input[0].data.string_val);
cleanup:
free(priv_path);
free(pub_path);
return ret;
}
static int
ks_privkey_load_cb(const char *UNUSED(xpath), const sr_node_t *input, const size_t input_cnt, sr_node_t **UNUSED(output),
size_t *UNUSED(output_cnt), void *private_ctx)
{
struct keystored_ctx *ctx = (struct keystored_ctx *)private_ctx;
pid_t pid;
int ret = SR_ERR_OK, status, len, fd;
char *priv_path = NULL, *pub_path = NULL;
FILE *privkey = NULL;
if ((input_cnt != 2) || (input[0].type != SR_STRING_T) || (input[1].type != SR_BINARY_T)) {
SRP_LOG_ERR_MSG("Unexpected input from sysrepo.");
ret = SR_ERR_INTERNAL;
goto cleanup;
}
SRP_LOG_INF_MSG("Only RSA keys can be loaded, key presumed to be RSA.");
/* TODO check that name is unique */
priv_path = malloc(strlen(KEYSTORED_KEYS_DIR) + 1 + strlen(input[0].data.string_val) + 4 + 1);
pub_path = malloc(strlen(KEYSTORED_KEYS_DIR) + 1 + strlen(input[0].data.string_val) + 4 + 4 + 1);
if (!priv_path || !pub_path) {
SRP_LOG_ERR("Memory allocation failed (%s).", strerror(errno));
ret = SR_ERR_NOMEM;
goto cleanup;
}
sprintf(priv_path, "%s/%s.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
sprintf(pub_path, "%s/%s.pub.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
fd = open(priv_path, O_CREAT | O_TRUNC | O_WRONLY, 00600);
if (fd == -1) {
SRP_LOG_ERR("Failed to open file \"%s\" for writing (%s).", priv_path, strerror(errno));
ret = SR_ERR_IO;
goto cleanup;
}
privkey = fdopen(fd, "w");
if (!privkey) {
SRP_LOG_ERR("Failed to open file \"%s\" for writing (%s).", priv_path, strerror(errno));
ret = SR_ERR_IO;
close(fd);
goto cleanup;
}
if ((status = fwrite("-----BEGIN RSA PRIVATE KEY-----\n", 1, 32, privkey)) < 32) {
if (status == -1) {
SRP_LOG_ERR("Failed to write to \"%s\" (%s).", priv_path, strerror(errno));
} else {
SRP_LOG_ERR("Failed to write to \"%s\" (witten %d instead %d).", priv_path, status, 32);
}
ret = SR_ERR_IO;
goto cleanup;
}
len = strlen(input[1].data.binary_val);
if ((status = fwrite(input[1].data.binary_val, 1, len, privkey)) < len) {
if (status == -1) {
SRP_LOG_ERR("Failed to write to \"%s\" (%s).", priv_path, strerror(errno));
} else {
SRP_LOG_ERR("Failed to write to \"%s\" (witten %d instead %d).", priv_path, status, len);
}
ret = SR_ERR_IO;
goto cleanup;
}
if ((status = fwrite("\n-----END RSA PRIVATE KEY-----\n", 1, 31, privkey)) < 31) {
if (status == -1) {
SRP_LOG_ERR("Failed to write to \"%s\" (%s).", priv_path, strerror(errno));
} else {
SRP_LOG_ERR("Failed to write to \"%s\" (witten %d instead %d).", priv_path, status, 31);
}
ret = SR_ERR_IO;
goto cleanup;
}
fflush(privkey);
if (!(pid = fork())) {
/* child */
execl(OPENSSL_EXECUTABLE, "rsa", "-pubout", "-in", priv_path, "-out", pub_path, NULL);
SRP_LOG_ERR("Exec failed (%s).", strerror(errno));
exit(1);
}
/* parent */
if (pid == -1) {
SRP_LOG_ERR("Fork failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
waitpid(pid, &status, 0);
/*
if (waitpid(pid, &status, 0) == -1) {
SRP_LOG_ERR("Waiting for child process failed (%s).", strerror(errno));
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (!WIFEXITED(status)) {
SRP_LOG_ERR_MSG("Child process ended in a non-standard way.");
ret = SR_ERR_INTERNAL;
goto cleanup;
}
if (WEXITSTATUS(status)) {
SRP_LOG_ERR("OpenSSL utility returned %d.", WEXITSTATUS(status));
ret = SR_ERR_INTERNAL;
goto cleanup;
}*/
/* add the key to the configuration */
ks_privkey_add(ctx->session, input[0].data.string_val);
cleanup:
if (privkey) {
fclose(privkey);
}
free(priv_path);
free(pub_path);
return ret;
}
int
sr_plugin_init_cb(sr_session_ctx_t *session, void **private_ctx)
{
int rc;
struct keystored_ctx *ctx;
ctx = calloc(1, sizeof *ctx);
if (!ctx) {
rc = SR_ERR_NOMEM;
goto error;
}
/* private keys (for server) */
rc = sr_subtree_change_subscribe(session, "/ietf-keystore:keystore/private-keys/private-key",
ks_privkey_change_cb, ctx, 0, SR_SUBSCR_DEFAULT, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}
rc = sr_dp_get_items_subscribe(session, "/ietf-keystore:keystore/private-keys/private-key",
ks_privkey_get_cb, ctx, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}
/*rc = sr_action_subscribe_tree(session, "/ietf-keystore:keystore/private-keys/private-key/generate-certificate-signing-request",
callback, ctx, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}*/
rc = sr_action_subscribe_tree(session, "/ietf-keystore:keystore/private-keys/generate-private-key",
ks_privkey_gen_cb, ctx, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}
rc = sr_action_subscribe_tree(session, "/ietf-keystore:keystore/private-keys/load-private-key",
ks_privkey_load_cb, ctx, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}
/* trusted certificates (for server/client) */
rc = sr_subtree_change_subscribe(session, "/ietf-keystore:keystore/trusted-certificates",
ks_cert_change_cb, ctx, 0, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}
/*rc = sr_event_notif_subscribe_tree(session, "/ietf-keystore:keystore/certificate-expiration",
callback, ctx, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}*/
/* trusted SSH host keys (for client) */
/*rc = sr_subtree_change_subscribe(session, "/ietf-keystore:keystore/trusted-ssh-host-keys",
privkeys_change_cb, ctx, 0, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}*/
/* user auth credentials (for client) */
/*rc = sr_subtree_change_subscribe(session, "/ietf-keystore:keystore/user-auth-credentials",
cb, ctx, 0, SR_SUBSCR_CTX_REUSE, &ctx->subscription);
if (SR_ERR_OK != rc) {
goto error;
}*/
SRP_LOG_DBG_MSG("keystored plugin initialized successfully.");
ctx->session = session;
*private_ctx = ctx;
return SR_ERR_OK;
error:
SRP_LOG_ERR("keystored plugin initialization failed (%s).", sr_strerror(rc));
sr_unsubscribe(session, ctx->subscription);
free(ctx);
return rc;
}
void
sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *private_ctx)
{
struct keystored_ctx *ctx = (struct keystored_ctx *)private_ctx;
sr_unsubscribe(session, ctx->subscription);
free(ctx);
SRP_LOG_DBG_MSG("keystored plugin cleanup finished.");
}