source: creddy/creddy.c @ cfcdfd29

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since cfcdfd29 was fd0febd, checked in by Mike Ryan <mikeryan@…>, 14 years ago

creddy keyid

  • Property mode set to 100644
File size: 4.9 KB
Line 
1#include <assert.h>
2#include <getopt.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include "creddy.h"
8
9#define OPT_CN              1
10#define OPT_VALIDITY        2
11#define OPT_CERT            3
12
13int main(int argc, char **argv) {
14    int ret;
15    options_t options = { 0, };
16
17    options.validity = 1080;
18
19    struct option getopts[] = {
20        { "help",       0, &options.help, 1 },
21        { "generate",   0, &options.mode, MODE_GENERATE },
22        { "verify",     0, &options.mode, MODE_VERIFY },
23        { "keyid",      0, &options.mode, MODE_KEYID },
24        { "attribute",  0, &options.mode, MODE_ATTRIBUTE },
25        { "roles",      0, &options.mode, MODE_ROLES },
26
27        { "cert",       1, 0, OPT_CERT },
28
29        // generate options
30        { "cn",         1, 0, OPT_CN },
31        { "validity",   1, 0, OPT_VALIDITY },
32
33        { NULL },
34    };
35
36    for ( ; ; ) {
37        int c = getopt_long(argc, argv, "", getopts, NULL);
38        if (c < 0)
39            break;
40
41        switch (c) {
42            // set the option from the value in the getopts struct
43            case 0:
44                continue;
45
46            case OPT_CERT:
47                options.cert = xstrdup(optarg);
48                break;
49
50            // generate options
51            case OPT_CN:
52                options.cn = xstrdup(optarg);
53                break;
54            case OPT_VALIDITY:
55                options.validity = atoi(optarg);
56                break;
57
58            case '?':
59                break;
60
61            default:
62                printf("wat\n");
63                return 45;
64        }
65    }
66
67    if (options.help || optind < argc) {
68        if (optind > 0 && optind < argc)
69            printf("I don't understand %s\n", argv[optind]);
70        usage(&options);
71    }
72
73    // init libstrongswan
74    atexit(library_deinit);
75    dbg_default_set_level(-1);
76    ret = library_init(NULL);
77    if (!ret)
78        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
79    ret = lib->plugins->load(
80        lib->plugins,
81        NULL,
82        lib->settings->get_str(lib->settings, "", PLUGINS)
83    );
84    if (!ret)
85        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
86
87    switch (options.mode) {
88        case MODE_GENERATE:
89            generate_main(&options);
90            break;
91
92        case MODE_KEYID:
93            keyid_main(&options);
94            break;
95
96        default:
97            usage(&options);
98    }
99
100    return 0;
101}
102
103void usage(options_t *opts) {
104    if (opts->mode == MODE_GENERATE)
105        printf(
106            "Usage: creddy --generate --cn <name> [ --validity <day> ]\n"
107            "    cert will be in ${name}_ID.der, private key in ${name}_private.der\n"
108            "    default validity: 1080 days\n"
109        );
110
111    else if (opts->mode == MODE_VERIFY)
112        printf(
113            "Usage: identity --verify --cert <cert> [ --attrcert <cert> ]\n"
114            "    if attrcert is provided, verify that it was issued by cert\n"
115        );
116
117    else if (opts->mode == MODE_KEYID)
118        printf(
119            "Usage: identity --keyid --cert <cert>\n"
120        );
121
122    else if (opts->mode == MODE_ATTRIBUTE)
123        printf(
124            "Usage: identity --attribute \\\n"
125            "                --issuer <cert> --key <key> --role <role> \\\n"
126            "                --subject <cert> [ --subject-role <role> ] \\\n"
127            "                [ --validity <days> ] --out <file>\n"
128            "    default validity: 365 days\n"
129        );
130
131    else if (opts->mode == MODE_ROLES)
132        printf(
133            "Usage: identity --roles --cert <cert>\n"
134        );
135
136    else
137        printf(
138            "Usage: creddy [--<mode>] [--help]\n"
139            "    --generate:  generate X.509 identity cert and private key\n"
140            "    --verify:    check validity of X.509 ID or attribute cert\n"
141            "    --keyid:     get fingerprint from X.509 ID cert\n"
142            "    --attribute: generate an X.509 attribute cert\n"
143            "    --roles:     list roles from an X.509 attribute cert\n"
144        );
145
146
147    exit(1);
148}
149
150void *xmalloc(size_t len) {
151    void *ret = malloc(len);
152    if (ret == NULL)
153        err(1, "couldn't malloc %d bytes\n", len);
154    return ret;
155}
156
157char *xstrdup(char *string) {
158    char *dup = strdup(string);
159    if (dup == NULL)
160        err(1, "Can't dup %s", string);
161    return dup;
162}
163
164int clean_name(char *string) {
165    int i;
166
167    assert(string != NULL);
168
169    // must start with a letter
170    if (!isalpha(string[0]))
171        return 0;
172
173    // rest must be alphanumeric
174    for (i = 1; string[i] != '\0'; ++i)
175        if (!isalnum(string[i]))
176            return 0;
177
178    return 1;
179}
180
181// load an ID cert from file
182certificate_t *cert_from_file(char *filename) {
183    certificate_t *cert = lib->creds->create(lib->creds,
184        CRED_CERTIFICATE, CERT_X509,
185        BUILD_FROM_FILE, filename,
186        BUILD_X509_FLAG, X509_AA,
187        BUILD_END
188    );
189
190    if (cert == NULL)
191        errx(1, "Can't open cert from %s", filename);
192
193    return cert;
194}
Note: See TracBrowser for help on using the repository browser.