source: creddy/generate.c @ f89b991

mei_rt2
Last change on this file since f89b991 was ba6027a, checked in by Mei <mei@…>, 12 years ago

1) modified code all around to add support for encrypted private key for

ID credential

2) add new abac_key_t structure (abac_key.c)
3) add new keycheck option to creddy
4) add 2 new test suites

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/***
2    generate.c
3***/
4
5#include <fcntl.h>
6#include <credentials/keys/private_key.h>
7#include "creddy_internal.h"
8
9void generate_main(options_t *opts) {
10    int ret, fd;
11    abac_id_t *id;
12    char *filename=NULL;
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    if(opts->key) {
27        ret = abac_id_generate_with_key(&id, opts->cn, opts->validity, opts->key, opts->pfile);
28        } else {
29            printf("Generating key, this will take a while. Create entropy!\n");
30            printf("    - move the mouse\n");
31            printf("    - generate disk activity (run find)\n");
32            ret = abac_id_generate(&id, opts->cn, opts->validity);
33    }
34
35    if (ret == ABAC_ID_GENERATE_INVALID_CN) {
36        printf("Invalid CN: must start with a letter and be alphanumeric\n");
37        usage(opts);
38    }
39    if (ret == ABAC_ID_GENERATE_INVALID_VALIDITY) {
40        printf("Validity must be >= 1 day\n");
41        usage(opts);
42    }
43    // in both above cases: usage(opts) exits
44
45    //
46    // success!
47    //
48
49    // write the cert
50    filename = abac_id_cert_filename(id);
51    out = fopen(filename, "w");
52    abac_id_write_cert(id, out);
53    fclose(out);
54    free(filename);
55
56    // write the key
57    if(! opts->key ) {
58        filename = abac_id_privkey_filename(id);
59        fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); // mode 600
60        if (fd < 0)
61            err(1, "Can't open private key file %s", filename);
62        out = fdopen(fd, "w");
63        abac_id_write_privkey(id, out);
64        fclose(out);
65        free(filename);
66    }
67
68    abac_id_free(id);
69}
Note: See TracBrowser for help on using the repository browser.