source: creddy/generate.c @ 8f58012

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 8f58012 was 8231b92, checked in by Mike Ryan <mikeryan@…>, 13 years ago

add output dir option to creddy generate
closes #19

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <fcntl.h>
2
3#include <credentials/keys/private_key.h>
4
5#include <creddy.h>
6
7#include "creddy_common.h"
8
9void 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    creddy_id_write_cert(id, out);
50    fclose(out);
51    free(filename);
52
53    // write the key
54    filename = creddy_id_privkey_filename(id);
55    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); // mode 600
56    if (fd < 0)
57        err(1, "Can't open private key file %s", filename);
58    out = fdopen(fd, "w");
59    creddy_id_write_privkey(id, out);
60    fclose(out);
61
62    creddy_id_free(id);
63}
Note: See TracBrowser for help on using the repository browser.