[abd7c25] | 1 | #include <assert.h> |
---|
[00d21a1] | 2 | #include <errno.h> |
---|
[abd7c25] | 3 | #include <getopt.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | |
---|
[04f5da1] | 8 | #include "creddy_common.h" |
---|
[abd7c25] | 9 | |
---|
| 10 | #define OPT_CN 1 |
---|
| 11 | #define OPT_VALIDITY 2 |
---|
[fd0febd] | 12 | #define OPT_CERT 3 |
---|
[085f159] | 13 | #define OPT_ISSUER 4 |
---|
| 14 | #define OPT_KEY 5 |
---|
| 15 | #define OPT_ROLE 6 |
---|
[ee5afdd] | 16 | #define OPT_SUBJECT_CERT 7 |
---|
[085f159] | 17 | #define OPT_SUBJECT_ROLE 8 |
---|
| 18 | #define OPT_OUT 9 // the world oughta be opt-in only :( |
---|
[784a398] | 19 | #define OPT_ATTRCERT 10 |
---|
[ee5afdd] | 20 | #define OPT_SUBJECT_ID 11 |
---|
[0aaa651] | 21 | #define OPT_SHOW 12 |
---|
[abd7c25] | 22 | |
---|
[980a7b6] | 23 | subject_t *subjects = NULL; |
---|
| 24 | int num_subjects = 0; |
---|
| 25 | int subjects_size = 0; |
---|
| 26 | |
---|
| 27 | char **roles = NULL; |
---|
| 28 | int num_roles = 0; |
---|
| 29 | int roles_size = 0; |
---|
| 30 | |
---|
| 31 | void subject(char *subject, int cert) { |
---|
| 32 | if (num_subjects == subjects_size) { |
---|
| 33 | subjects_size *= 2; |
---|
| 34 | subjects = xrealloc(subjects, sizeof(subject_t) * subjects_size); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | int i = num_subjects++; |
---|
| 38 | subjects[i].id = NULL; |
---|
| 39 | subjects[i].cert = NULL; |
---|
| 40 | subjects[i].role = NULL; |
---|
| 41 | |
---|
| 42 | if (cert) |
---|
| 43 | subjects[i].cert = subject; |
---|
| 44 | else |
---|
| 45 | subjects[i].id = subject; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void role(char *role) { |
---|
| 49 | if (num_roles == roles_size) { |
---|
| 50 | roles_size *= 2; |
---|
| 51 | roles = xrealloc(roles, sizeof(char *) * roles_size); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | int i = num_roles++; |
---|
| 55 | roles[i] = role; |
---|
| 56 | } |
---|
| 57 | |
---|
[abd7c25] | 58 | int main(int argc, char **argv) { |
---|
| 59 | int ret; |
---|
| 60 | options_t options = { 0, }; |
---|
[00d21a1] | 61 | char *validity_str = NULL; |
---|
[abd7c25] | 62 | |
---|
[980a7b6] | 63 | subjects = xmalloc(sizeof(subject_t) * 2); |
---|
| 64 | subjects_size = 2; |
---|
| 65 | |
---|
| 66 | roles = xmalloc(sizeof(char *) * 2); |
---|
| 67 | roles_size = 2; |
---|
| 68 | |
---|
[abd7c25] | 69 | struct option getopts[] = { |
---|
| 70 | { "help", 0, &options.help, 1 }, |
---|
| 71 | { "generate", 0, &options.mode, MODE_GENERATE }, |
---|
| 72 | { "verify", 0, &options.mode, MODE_VERIFY }, |
---|
| 73 | { "keyid", 0, &options.mode, MODE_KEYID }, |
---|
| 74 | { "attribute", 0, &options.mode, MODE_ATTRIBUTE }, |
---|
| 75 | { "roles", 0, &options.mode, MODE_ROLES }, |
---|
[19be896] | 76 | { "version", 0, &options.mode, MODE_VERSION }, |
---|
[0aaa651] | 77 | { "display", 0, &options.mode, MODE_DISPLAY }, |
---|
[abd7c25] | 78 | |
---|
[fd0febd] | 79 | { "cert", 1, 0, OPT_CERT }, |
---|
| 80 | |
---|
[abd7c25] | 81 | // generate options |
---|
| 82 | { "cn", 1, 0, OPT_CN }, |
---|
| 83 | { "validity", 1, 0, OPT_VALIDITY }, |
---|
| 84 | |
---|
[085f159] | 85 | // attribute options |
---|
| 86 | { "issuer", 1, 0, OPT_ISSUER }, |
---|
| 87 | { "key", 1, 0, OPT_KEY }, |
---|
| 88 | { "role", 1, 0, OPT_ROLE }, |
---|
[ee5afdd] | 89 | { "subject-cert", 1, 0, OPT_SUBJECT_CERT }, |
---|
| 90 | { "subject-id", 1, 0, OPT_SUBJECT_ID }, |
---|
[ec06cb6] | 91 | { "subject-role", 1, 0, OPT_SUBJECT_ROLE }, |
---|
[085f159] | 92 | { "out", 1, 0, OPT_OUT }, |
---|
| 93 | |
---|
[784a398] | 94 | // verify option |
---|
| 95 | { "attrcert", 1, 0, OPT_ATTRCERT }, |
---|
| 96 | |
---|
[0aaa651] | 97 | // display options |
---|
| 98 | { "show", 1, 0, OPT_SHOW }, |
---|
| 99 | |
---|
[abd7c25] | 100 | { NULL }, |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | for ( ; ; ) { |
---|
| 104 | int c = getopt_long(argc, argv, "", getopts, NULL); |
---|
| 105 | if (c < 0) |
---|
| 106 | break; |
---|
| 107 | |
---|
| 108 | switch (c) { |
---|
| 109 | // set the option from the value in the getopts struct |
---|
| 110 | case 0: |
---|
| 111 | continue; |
---|
| 112 | |
---|
[fd0febd] | 113 | case OPT_CERT: |
---|
| 114 | options.cert = xstrdup(optarg); |
---|
| 115 | break; |
---|
| 116 | |
---|
[abd7c25] | 117 | // generate options |
---|
| 118 | case OPT_CN: |
---|
| 119 | options.cn = xstrdup(optarg); |
---|
| 120 | break; |
---|
[085f159] | 121 | case OPT_VALIDITY: // also an attribute option |
---|
[00d21a1] | 122 | validity_str = xstrdup(optarg); |
---|
[abd7c25] | 123 | break; |
---|
| 124 | |
---|
[085f159] | 125 | // attribute options |
---|
| 126 | case OPT_ISSUER: |
---|
| 127 | options.issuer = xstrdup(optarg); |
---|
| 128 | break; |
---|
| 129 | case OPT_KEY: |
---|
| 130 | options.key = xstrdup(optarg); |
---|
| 131 | break; |
---|
| 132 | case OPT_ROLE: |
---|
| 133 | options.role = xstrdup(optarg); |
---|
| 134 | break; |
---|
[ee5afdd] | 135 | case OPT_SUBJECT_CERT: |
---|
[980a7b6] | 136 | subject(xstrdup(optarg), 1); |
---|
[ee5afdd] | 137 | break; |
---|
| 138 | case OPT_SUBJECT_ID: |
---|
[980a7b6] | 139 | subject(xstrdup(optarg), 0); |
---|
[085f159] | 140 | break; |
---|
| 141 | case OPT_SUBJECT_ROLE: |
---|
[980a7b6] | 142 | role(xstrdup(optarg)); |
---|
[085f159] | 143 | break; |
---|
| 144 | case OPT_OUT: |
---|
| 145 | options.out = xstrdup(optarg); |
---|
| 146 | break; |
---|
| 147 | |
---|
[784a398] | 148 | // verify options |
---|
| 149 | case OPT_ATTRCERT: |
---|
| 150 | options.attrcert = xstrdup(optarg); |
---|
| 151 | break; |
---|
| 152 | |
---|
[0aaa651] | 153 | // display options |
---|
| 154 | case OPT_SHOW: |
---|
| 155 | options.show = xstrdup(optarg); |
---|
| 156 | break; |
---|
| 157 | |
---|
[abd7c25] | 158 | case '?': |
---|
| 159 | break; |
---|
| 160 | |
---|
| 161 | default: |
---|
| 162 | printf("wat\n"); |
---|
| 163 | return 45; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if (options.help || optind < argc) { |
---|
| 168 | if (optind > 0 && optind < argc) |
---|
| 169 | printf("I don't understand %s\n", argv[optind]); |
---|
| 170 | usage(&options); |
---|
| 171 | } |
---|
| 172 | |
---|
[00d21a1] | 173 | // parse the validity |
---|
| 174 | if (validity_str != NULL) { |
---|
| 175 | char suffix = 'd'; // default suffix is days |
---|
| 176 | int multiplier; |
---|
| 177 | |
---|
| 178 | int len = strlen(validity_str); |
---|
| 179 | assert(len > 0); |
---|
| 180 | |
---|
| 181 | // get the suffix char if it's alphabetical |
---|
| 182 | if (isalpha(validity_str[len - 1])) { |
---|
| 183 | suffix = validity_str[len - 1]; |
---|
| 184 | |
---|
| 185 | // truncate |
---|
| 186 | validity_str[len - 1] = '\0'; |
---|
| 187 | --len; |
---|
| 188 | |
---|
| 189 | // make sure it's not only a suffix |
---|
| 190 | if (len == 0) { |
---|
| 191 | printf("Invalid validity\n"); |
---|
| 192 | usage(&options); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // convert the suffix to a multiplier |
---|
| 197 | switch(suffix) { |
---|
| 198 | case 's': multiplier = 1; break; |
---|
| 199 | case 'm': multiplier = 60; break; |
---|
| 200 | case 'h': multiplier = 3600; break; |
---|
| 201 | case 'd': multiplier = 86400; break; |
---|
| 202 | case 'y': multiplier = 31536000; break; |
---|
| 203 | default: |
---|
| 204 | printf("Invalid suffix, must be s m h d y\n"); |
---|
| 205 | usage(&options); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // ascii to int |
---|
| 209 | char *end; |
---|
| 210 | options.validity = strtol(validity_str, &end, 10); |
---|
| 211 | if (errno != 0 || end - validity_str < len) { |
---|
| 212 | printf("Invalid validity\n"); |
---|
| 213 | usage(&options); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if (options.validity <= 0) { |
---|
| 217 | printf("Invalid validity: must be > 0\n"); |
---|
| 218 | usage(&options); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | // multiply! |
---|
| 222 | options.validity *= multiplier; |
---|
| 223 | |
---|
| 224 | free(validity_str); |
---|
| 225 | } |
---|
| 226 | |
---|
[250f2e1] | 227 | if (options.mode == MODE_ATTRIBUTE) { |
---|
[980a7b6] | 228 | int i; |
---|
| 229 | |
---|
| 230 | // have to do error checking on subjects here |
---|
| 231 | if ( |
---|
| 232 | (num_subjects == 0) || |
---|
| 233 | (num_subjects != num_roles && num_subjects != 1 && num_roles != 0) |
---|
| 234 | ) { |
---|
| 235 | printf( |
---|
| 236 | "You have %d subject%s and %d role%s, which is invalid\n", |
---|
| 237 | num_subjects, num_subjects == 1 ? "" : "s", |
---|
| 238 | num_roles, num_roles == 1 ? "" : "s" |
---|
| 239 | ); |
---|
| 240 | usage(&options); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | for (i = 0; i < num_roles; ++i) |
---|
| 244 | subjects[i].role = roles[i]; |
---|
| 245 | free(roles); |
---|
| 246 | |
---|
| 247 | options.subjects = subjects; |
---|
| 248 | options.num_subjects = num_subjects; |
---|
| 249 | } |
---|
| 250 | |
---|
[9410b51] | 251 | // launch the sub command |
---|
[abd7c25] | 252 | switch (options.mode) { |
---|
| 253 | case MODE_GENERATE: |
---|
[00d21a1] | 254 | if (options.validity == 0) options.validity = 1080 * 86400; |
---|
[abd7c25] | 255 | generate_main(&options); |
---|
| 256 | break; |
---|
| 257 | |
---|
[fd0febd] | 258 | case MODE_KEYID: |
---|
| 259 | keyid_main(&options); |
---|
| 260 | break; |
---|
| 261 | |
---|
[085f159] | 262 | case MODE_ATTRIBUTE: |
---|
[00d21a1] | 263 | if (options.validity == 0) options.validity = 365 * 86400; |
---|
[085f159] | 264 | attribute_main(&options); |
---|
| 265 | break; |
---|
| 266 | |
---|
[9410b51] | 267 | case MODE_ROLES: |
---|
| 268 | roles_main(&options); |
---|
| 269 | break; |
---|
| 270 | |
---|
[784a398] | 271 | case MODE_VERIFY: |
---|
| 272 | verify_main(&options); |
---|
| 273 | break; |
---|
| 274 | |
---|
[0aaa651] | 275 | case MODE_DISPLAY: |
---|
| 276 | display_main(&options); |
---|
[adc86c3] | 277 | break; |
---|
| 278 | |
---|
[19be896] | 279 | case MODE_VERSION: |
---|
| 280 | printf("ABAC/creddy " ABAC_VERSION "\n"); |
---|
| 281 | break; |
---|
| 282 | |
---|
[abd7c25] | 283 | default: |
---|
| 284 | usage(&options); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | return 0; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | void usage(options_t *opts) { |
---|
| 291 | if (opts->mode == MODE_GENERATE) |
---|
| 292 | printf( |
---|
[00d21a1] | 293 | "Usage: creddy --generate --cn <name> [ --validity <time> ] [ --out <dir> ]\n" |
---|
[f1e892e] | 294 | " cert will be in ${name}_ID.der, private key in ${name}_private.pem\n" |
---|
[8231b92] | 295 | " files output to dir if specified\n" |
---|
[abd7c25] | 296 | " default validity: 1080 days\n" |
---|
[00d21a1] | 297 | "\n" |
---|
| 298 | " time is specified with optional suffix: s m h d y\n" |
---|
| 299 | " defaults to days if unspecified\n" |
---|
[abd7c25] | 300 | ); |
---|
| 301 | |
---|
| 302 | else if (opts->mode == MODE_VERIFY) |
---|
| 303 | printf( |
---|
[91659cb] | 304 | "Usage: creddy --verify --cert <cert> [ --attrcert <cert> ]\n" |
---|
[abd7c25] | 305 | " if attrcert is provided, verify that it was issued by cert\n" |
---|
| 306 | ); |
---|
| 307 | |
---|
| 308 | else if (opts->mode == MODE_KEYID) |
---|
| 309 | printf( |
---|
[91659cb] | 310 | "Usage: creddy --keyid --cert <cert>\n" |
---|
[abd7c25] | 311 | ); |
---|
| 312 | |
---|
| 313 | else if (opts->mode == MODE_ATTRIBUTE) |
---|
| 314 | printf( |
---|
[91659cb] | 315 | "Usage: creddy --attribute \\\n" |
---|
[abd7c25] | 316 | " --issuer <cert> --key <key> --role <role> \\\n" |
---|
[980a7b6] | 317 | " [ --subject-cert <cert> | --subject-id <sha1> \\\n" |
---|
| 318 | " [ --subject-role <role> ] ... ] \\\n" |
---|
[00d21a1] | 319 | " [ --validity <time> ] --out <file>\n" |
---|
[abd7c25] | 320 | " default validity: 365 days\n" |
---|
[ee5afdd] | 321 | " provide exactly one of --subject-cert / --subject-id\n" |
---|
[980a7b6] | 322 | " give multiple --subject-{cert,id} / --subject-role pairs for intersection\n" |
---|
[00d21a1] | 323 | "\n" |
---|
| 324 | " time is specified with optional suffix: s m h d y\n" |
---|
| 325 | " defaults to days if unspecified\n" |
---|
[abd7c25] | 326 | ); |
---|
| 327 | |
---|
| 328 | else if (opts->mode == MODE_ROLES) |
---|
| 329 | printf( |
---|
[91659cb] | 330 | "Usage: creddy --roles --cert <cert>\n" |
---|
[abd7c25] | 331 | ); |
---|
| 332 | |
---|
[0aaa651] | 333 | else if (opts->mode == MODE_DISPLAY) |
---|
[adc86c3] | 334 | printf( |
---|
[405bba3] | 335 | "Usage: creddy --display --show=[issuer,..,all] --cert <cert>\n" |
---|
[0aaa651] | 336 | " values for --show are comma-separated:\n" |
---|
| 337 | " issuer DN of issuer\n" |
---|
[b19d1f0] | 338 | " subject DN of subject\n" |
---|
[405bba3] | 339 | " validity validity period\n" |
---|
[08e7235] | 340 | " roles attribute cert roles (fails silently on ID certs)\n" |
---|
[0aaa651] | 341 | " all all of the above\n" |
---|
[adc86c3] | 342 | " cert may be X.509 identity or attribute cert\n" |
---|
| 343 | ); |
---|
| 344 | |
---|
[abd7c25] | 345 | else |
---|
| 346 | printf( |
---|
[66462c5] | 347 | "Usage: creddy [ --<mode> ] [ --help ]\n" |
---|
[abd7c25] | 348 | " --generate: generate X.509 identity cert and private key\n" |
---|
| 349 | " --verify: check validity of X.509 ID or attribute cert\n" |
---|
| 350 | " --keyid: get fingerprint from X.509 ID cert\n" |
---|
| 351 | " --attribute: generate an X.509 attribute cert\n" |
---|
| 352 | " --roles: list roles from an X.509 attribute cert\n" |
---|
[0aaa651] | 353 | " --display: list metadata from an X.509 identity or attribute cert\n" |
---|
[19be896] | 354 | " --version: display ABAC version\n" |
---|
[abd7c25] | 355 | ); |
---|
| 356 | |
---|
| 357 | |
---|
| 358 | exit(1); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | void *xmalloc(size_t len) { |
---|
| 362 | void *ret = malloc(len); |
---|
| 363 | if (ret == NULL) |
---|
| 364 | err(1, "couldn't malloc %d bytes\n", len); |
---|
| 365 | return ret; |
---|
| 366 | } |
---|
| 367 | |
---|
[980a7b6] | 368 | void *xrealloc(void *ptr, size_t size) { |
---|
| 369 | void *ret = realloc(ptr, size); |
---|
| 370 | if (ret == NULL) |
---|
| 371 | err(1, "couldn't realloc %d bytes\n", size); |
---|
| 372 | return ret; |
---|
| 373 | } |
---|
| 374 | |
---|
[abd7c25] | 375 | char *xstrdup(char *string) { |
---|
| 376 | char *dup = strdup(string); |
---|
| 377 | if (dup == NULL) |
---|
| 378 | err(1, "Can't dup %s", string); |
---|
| 379 | return dup; |
---|
| 380 | } |
---|
| 381 | |
---|
[784a398] | 382 | certificate_t *attr_cert_from_file(char *filename) { |
---|
[f2ca623] | 383 | libabac_init(); |
---|
[784a398] | 384 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
| 385 | CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 386 | BUILD_FROM_FILE, filename, |
---|
| 387 | BUILD_END |
---|
| 388 | ); |
---|
| 389 | if (cert == NULL) |
---|
| 390 | errx(1, "Couldn't load attribute cert %s", filename); |
---|
| 391 | |
---|
| 392 | return cert; |
---|
| 393 | } |
---|