diff options
author | Magnus Ahltorp <map@kth.se> | 2015-04-24 08:32:24 +0200 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-04-24 08:34:59 +0200 |
commit | ad1e479a9f7e0d6ccff7edb0409a668cae38dc55 (patch) | |
tree | de63184fd7de1a147746dbf26c46d33a69444955 | |
parent | 40b01777dd64941daffbe22fc25cb3a25eee0998 (diff) |
Optionally read pin from file
-rw-r--r-- | c_src/hsmhelper.c | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/c_src/hsmhelper.c b/c_src/hsmhelper.c index e7029a0..7985da4 100644 --- a/c_src/hsmhelper.c +++ b/c_src/hsmhelper.c @@ -44,6 +44,37 @@ loop(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey, } } +#define MAX_PIN_SIZE 1000 + +char * +read_pin(char *filename) +{ + FILE *pin_file; + char *result; + + pin_file = fopen(filename, "r"); + + if (pin_file == NULL) { + return NULL; + } + + char *pin = malloc(MAX_PIN_SIZE); + result = fgets(pin, MAX_PIN_SIZE, pin_file); + + if (result == NULL) { + free(pin); + fclose(pin_file); + return NULL; + } + + size_t newlinepos = strcspn(result, "\r\n"); + pin[newlinepos] = '\0'; + + fclose(pin_file); + + return pin; +} + int main(int argc, char *argv[]) { @@ -55,7 +86,19 @@ main(int argc, char *argv[]) char *slotstring = argv[2]; char *keytype = argv[3]; char *keyname = argv[4]; - char *pin = argv[5]; + char *pin_or_file = argv[5]; + + char *pin; + + if (pin_or_file[0] == '@') { + pin = read_pin(pin_or_file + 1); + } else { + pin = strdup(pin_or_file); + } + + if (pin == NULL) { + errx(1, "Could not read pin"); + } init(library_path); @@ -79,5 +122,7 @@ main(int argc, char *argv[]) loop(hSession, hKey, mechanism); + free(pin); + return 0; } |