#define GNU_SOURCE #include #include "creddy.h" #include #define ROLE_SEPARATOR " <- " #define INTERSECTION_SEP " & " #define SHA1_LENGTH 40 /* Callback configuration */ struct cb_opts { bool use_prompt; /* Print a prompt to stderr */ bool use_echo; /* If true, turn off input echo on stdin */ unsigned int tries; /* Number of attempts allowed */ char prompt[20]; /* The prompt to display if use_echo is true */ }; chunk_t passphrase_callback(void *user, int try) { /* Get a password from stdin and return it as a chunk_t. If too many tries * have occurred or there is any other problem, return an empty chunk_t, * which libstrongswan takes as giving up. The chunk is alloated here * (inside getline), and presumably freed by libstrongswan. User points to * a cb_opts struct, which affects this routine in the obvious ways. */ /* Configuration options */ struct cb_opts *opts = (struct cb_opts *) user; chunk_t rv = chunk_empty; /* Return value, starts empty */ if (try -1 < opts->tries ) { struct termios t; /* Terminal settings */ size_t len = 0; /* Length of string from getline */ tcflag_t orig = 0; /* Holds the original local flags (echo in here) */ if (!opts->use_echo) { /* Use tc{get,set}attr to turn echo off and restore the intial * echo settings */ if (!tcgetattr(0, &t)) { orig = t.c_lflag; t.c_lflag &= ~ECHO; if ( tcsetattr(0, TCSANOW, &t) ) { perror("Cannot turn off echo"); return rv; } } else { perror("Cannot turn get attributes to off echo"); return rv; } } if (opts->use_prompt) printf("%s", opts->prompt); /* Because rv.ptr starts as NULL, getline allocates memory. The size * of the allocation returns in rv.len and the size of the string * (including newline and NUL) is in len. */ len = getline(&rv.ptr, &rv.len, stdin); if (!opts->use_echo ) { /* Pop echo beck to its original setting. */ t.c_lflag = orig; if ( tcsetattr(0, TCSANOW, &t) ) perror("Cannot restore echo setting?"); if (opts->use_prompt) printf("\n"); } /* Readjust the chunk_t's len field to the size of the string w/o the * newline or NUL */ if ( len != ~0 ) { if (rv.ptr[len-2] == '\n') rv.len = len-2; else rv.len = len -1; } else { /* A ~0 len (size_t is unsigned) indicates EOF with an empty line. * Deallocate any empty allocation */ if (rv.ptr) free(rv.ptr); rv = chunk_empty; } } else fprintf(stderr, "Too many tries (%d)", try-1); return rv; } void attribute_main(options_t *opts) { struct cb_opts c_opts = { 1, 0, 3, "Key password:" }; int i, role_len = 1; if ( opts->issuer == NULL || opts->key == NULL || opts->role == NULL || opts->out == NULL ) usage(opts); if (!clean_name(opts->role)) { printf("bad role name %s\n", opts->role); usage(opts); } role_len += SHA1_LENGTH + 1 + strlen(opts->role); role_len += strlen(ROLE_SEPARATOR); for (i = 0; i < opts->num_subjects; ++i) { subject_t *cur = &opts->subjects[i]; // if we have an ID, make sure it's SHA1 and lowercase if (cur->id) { int j; char *subject_id = cur->id; // hex chars (also convert to lowercase) for (j = 0; subject_id[j]; ++j) { subject_id[j] = tolower(subject_id[j]); if (!isxdigit(subject_id[j])) { printf("Invalid subject ID %s: must be SHA1\n", subject_id); usage(opts); } } // correct length if (j != SHA1_LENGTH) { printf("Invalid subject ID %s: must be SHA1 (wrong length)\n", subject_id); usage(opts); } } // otherwise we have a cert else { certificate_t *subject = cert_from_file(cur->cert); cur->id = cert_keyid(subject); DESTROY_IF(subject); } role_len += SHA1_LENGTH; // verify the subject role name if present if (cur->role) { char *role = cur->role; char *start[3]; int name_parts = 0, j; start[name_parts++] = role; for (j = 0; role[j] != '\0'; ++j) if (role[j] == '.') { if (name_parts == 3) { printf("bad subject role name (too many dots)\n"); usage(opts); } start[name_parts++] = &role[j+1]; role[j] = 0; } for (j = 0; j < name_parts; ++j) if (!clean_name(start[j])) { printf("bad subject role name\n"); usage(opts); } for (j = 1; j < name_parts; ++j) *(start[j]-1) = '.'; // replace the dot role_len += strlen(cur->role) + 1; } role_len += sizeof(INTERSECTION_SEP) - 1; } if (opts->validity < 0) { printf("Validity must be >= 1 day\n"); usage(opts); } chunk_t pw = chunk_empty; // load signer key private_key_t *key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA, BUILD_FROM_FILE, opts->key, /* Ask for password if the key's encrypted */ BUILD_PASSPHRASE_CALLBACK, passphrase_callback, &c_opts, BUILD_END ); if (key == NULL) errx(1, "can't open private key file %s", opts->key); // get the keyids certificate_t *issuer = cert_from_file(opts->issuer); char *issuer_id = cert_keyid(issuer); // build the role encoding char *role_encoding = xmalloc(role_len); role_encoding[0] = '\0'; // role sprintf(role_encoding, "%s.%s" ROLE_SEPARATOR, issuer_id, opts->role); // subject(s) for (i = 0; i < opts->num_subjects; ++i) { subject_t *cur = &opts->subjects[i]; strcat(role_encoding, cur->id); if (cur->role) { strcat(role_encoding, "."); strcat(role_encoding, cur->role); } if (i < opts->num_subjects - 1) strcat(role_encoding, INTERSECTION_SEP); } free(issuer_id); // create attribute cert time_t not_before = time(NULL); time_t not_after = not_before + opts->validity * 3600 * 60 * 60; chunk_t serial = generate_serial(); certificate_t *attr_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_AC, BUILD_CERT, issuer, BUILD_NOT_BEFORE_TIME, not_before, BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial, BUILD_IETF_GROUP_ATTR, role_encoding, BUILD_SIGNING_CERT, issuer, BUILD_SIGNING_KEY, key, BUILD_END ); if (attr_cert == NULL) errx(1, "Couldn't build attribute cert"); // write to file chunk_t encoding = attr_cert->get_encoding(attr_cert); FILE *out = fopen(opts->out, "w"); if (out == NULL) err(1, "Can't open attribute cert output file %s", opts->out); fwrite(encoding.ptr, encoding.len, 1, out); fclose(out); free(role_encoding); free(serial.ptr); DESTROY_IF(attr_cert); DESTROY_IF(issuer); DESTROY_IF(key); }