1 | #include <fcntl.h> |
---|
2 | |
---|
3 | #include <credentials/keys/private_key.h> |
---|
4 | |
---|
5 | #include <creddy.h> |
---|
6 | |
---|
7 | #include "creddy_common.h" |
---|
8 | |
---|
9 | void generate_main(options_t *opts) { |
---|
10 | int ret, fd; |
---|
11 | creddy_id_t *id; |
---|
12 | char *filename; |
---|
13 | FILE *out; |
---|
14 | |
---|
15 | // make sure we have at least a CN |
---|
16 | if (opts->cn == NULL) |
---|
17 | usage(opts); |
---|
18 | |
---|
19 | // if we have an outdir, chdir there |
---|
20 | if (opts->out) { |
---|
21 | ret = chdir(opts->out); |
---|
22 | if (ret < 0) |
---|
23 | err(1, "can't open output directory '%s'", opts->out); |
---|
24 | } |
---|
25 | |
---|
26 | printf("Generating key, this will take a while. Create entropy!\n"); |
---|
27 | printf(" - move the mouse\n"); |
---|
28 | printf(" - generate disk activity (run find)\n"); |
---|
29 | |
---|
30 | ret = creddy_id_generate(&id, opts->cn, opts->validity); |
---|
31 | |
---|
32 | if (ret == CREDDY_GENERATE_INVALID_CN) { |
---|
33 | printf("Invalid CN: must start with a letter and be alphanumeric\n"); |
---|
34 | usage(opts); |
---|
35 | } |
---|
36 | if (ret == CREDDY_GENERATE_INVALID_VALIDITY) { |
---|
37 | printf("Validity must be >= 1 day\n"); |
---|
38 | usage(opts); |
---|
39 | } |
---|
40 | // in both above cases: usage(opts) exits |
---|
41 | |
---|
42 | // |
---|
43 | // success! |
---|
44 | // |
---|
45 | |
---|
46 | // write the cert |
---|
47 | filename = creddy_id_cert_filename(id); |
---|
48 | out = fopen(filename, "w"); |
---|
49 | if (out == NULL) |
---|
50 | err(1, "Can't open cert file %s", filename); |
---|
51 | creddy_id_write_cert(id, out); |
---|
52 | fclose(out); |
---|
53 | free(filename); |
---|
54 | |
---|
55 | // write the key |
---|
56 | filename = creddy_id_privkey_filename(id); |
---|
57 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); // mode 600 |
---|
58 | if (fd < 0) |
---|
59 | err(1, "Can't open private key file %s", filename); |
---|
60 | out = fdopen(fd, "w"); |
---|
61 | creddy_id_write_privkey(id, out); |
---|
62 | fclose(out); |
---|
63 | |
---|
64 | creddy_id_free(id); |
---|
65 | } |
---|