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