|
69 | 69 |
|
70 | 70 | #define YKPIV_ATTESTATION_OID "1.3.6.1.4.1.41482.3"
|
71 | 71 |
|
| 72 | +static bool verify_pin(ykpiv_state *state); |
| 73 | + |
72 | 74 | static enum file_mode key_file_mode(enum enum_key_format fmt, bool output) {
|
73 | 75 | if (fmt == key_format_arg_PEM) {
|
74 | 76 | if (output) {
|
@@ -126,10 +128,16 @@ static bool sign_data(ykpiv_state *state, const unsigned char *in, size_t len, u
|
126 | 128 | in = signinput;
|
127 | 129 | len = padlen;
|
128 | 130 | }
|
129 |
| - if(ykpiv_sign_data(state, in, len, out, out_len, algorithm, key) == YKPIV_OK) { |
130 |
| - return true; |
| 131 | + if(!verify_pin(state)) { |
| 132 | + return false; |
131 | 133 | }
|
132 |
| - return false; |
| 134 | + ykpiv_rc res = ykpiv_sign_data(state, in, len, out, out_len, algorithm, key); |
| 135 | + if(res != YKPIV_OK) |
| 136 | + { |
| 137 | + fprintf(stderr, "Signing data failed: '%s'\n", ykpiv_strerror(res)); |
| 138 | + return false; |
| 139 | + } |
| 140 | + return true; |
133 | 141 | }
|
134 | 142 |
|
135 | 143 | #if !((OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER))
|
@@ -870,7 +878,6 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for
|
870 | 878 | unsigned char signature[1024] = {0};
|
871 | 879 | size_t sig_len = sizeof(signature);
|
872 | 880 | if(!sign_data(state, signinput, len, signature, &sig_len, algorithm, key)) {
|
873 |
| - fprintf(stderr, "Failed signing request.\n"); |
874 | 881 | goto request_out;
|
875 | 882 | }
|
876 | 883 | ASN1_STRING_set(req->signature, signature, sig_len);
|
@@ -1124,7 +1131,6 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo
|
1124 | 1131 | unsigned char signature[1024] = {0};
|
1125 | 1132 | size_t sig_len = sizeof(signature);
|
1126 | 1133 | if(!sign_data(state, signinput, len, signature, &sig_len, algorithm, key)) {
|
1127 |
| - fprintf(stderr, "Failed signing certificate.\n"); |
1128 | 1134 | goto selfsign_out;
|
1129 | 1135 | }
|
1130 | 1136 | ASN1_STRING_set(x509->signature, signature, sig_len);
|
@@ -1186,31 +1192,6 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo
|
1186 | 1192 | return ret;
|
1187 | 1193 | }
|
1188 | 1194 |
|
1189 |
| -static bool verify_pin(ykpiv_state *state, const char *pin) { |
1190 |
| - int tries = -1; |
1191 |
| - ykpiv_rc res; |
1192 |
| - int len; |
1193 |
| - len = strlen(pin); |
1194 |
| - |
1195 |
| - if(len > 8) { |
1196 |
| - fprintf(stderr, "Maximum 8 digits of PIN supported.\n"); |
1197 |
| - } |
1198 |
| - |
1199 |
| - res = ykpiv_verify(state, pin, &tries); |
1200 |
| - if(res == YKPIV_OK) { |
1201 |
| - return true; |
1202 |
| - } else if(res == YKPIV_WRONG_PIN || res == YKPIV_PIN_LOCKED) { |
1203 |
| - if(tries > 0) { |
1204 |
| - fprintf(stderr, "Pin verification failed, %d tries left before pin is blocked.\n", tries); |
1205 |
| - } else { |
1206 |
| - fprintf(stderr, "Pin code blocked, use unblock-pin action to unblock.\n"); |
1207 |
| - } |
1208 |
| - } else { |
1209 |
| - fprintf(stderr, "Pin code verification failed: '%s'\n", ykpiv_strerror(res)); |
1210 |
| - } |
1211 |
| - return false; |
1212 |
| -} |
1213 |
| - |
1214 | 1195 | /* this function is called for all three of change-pin, change-puk and unblock pin
|
1215 | 1196 | * since they're very similar in what data they use. */
|
1216 | 1197 | static bool change_pin(ykpiv_state *state, enum enum_action action, const char *pin,
|
@@ -1416,7 +1397,6 @@ static bool sign_file(ykpiv_state *state, const char *input, const char *output,
|
1416 | 1397 | unsigned char buf[1024] = {0};
|
1417 | 1398 | size_t len = sizeof(buf);
|
1418 | 1399 | if(!sign_data(state, hashed, hash_len, buf, &len, algo, key)) {
|
1419 |
| - fprintf(stderr, "failed signing file\n"); |
1420 | 1400 | goto out;
|
1421 | 1401 | }
|
1422 | 1402 |
|
@@ -1720,7 +1700,6 @@ static bool test_signature(ykpiv_state *state, enum enum_slot slot,
|
1720 | 1700 | enc_len = data_len;
|
1721 | 1701 | }
|
1722 | 1702 | if(!sign_data(state, ptr, enc_len, signature, &sig_len, algorithm, key)) {
|
1723 |
| - fprintf(stderr, "Failed signing test data.\n"); |
1724 | 1703 | goto test_out;
|
1725 | 1704 | }
|
1726 | 1705 |
|
@@ -2060,8 +2039,44 @@ static bool read_object(ykpiv_state *state, int id, const char *output_file_name
|
2060 | 2039 | return ret;
|
2061 | 2040 | }
|
2062 | 2041 |
|
| 2042 | +static struct gengetopt_args_info args_info; |
| 2043 | + |
| 2044 | +static bool verify_pin(ykpiv_state *state) |
| 2045 | +{ |
| 2046 | + if (!args_info.pin_arg) { |
| 2047 | + args_info.pin_arg = calloc(1, 8 + 2); |
| 2048 | + if (!read_pw("PIN", args_info.pin_arg, 8 + 2, false, args_info.stdin_input_flag)) { |
| 2049 | + free(args_info.pin_arg); |
| 2050 | + args_info.pin_arg = NULL; |
| 2051 | + return false; |
| 2052 | + } |
| 2053 | + } |
| 2054 | + |
| 2055 | + if (strlen(args_info.pin_arg) > 8) { |
| 2056 | + fprintf(stderr, "Maximum 8 digits of PIN supported.\n"); |
| 2057 | + } |
| 2058 | + |
| 2059 | + int tries = -1; |
| 2060 | + ykpiv_rc res = ykpiv_verify(state, args_info.pin_arg, &tries); |
| 2061 | + if (res == YKPIV_OK) { |
| 2062 | + fprintf(stderr, "Successfully verified PIN.\n"); |
| 2063 | + return true; |
| 2064 | + } |
| 2065 | + else if (res == YKPIV_WRONG_PIN || res == YKPIV_PIN_LOCKED) { |
| 2066 | + if (tries > 0) { |
| 2067 | + fprintf(stderr, "Pin verification failed, %d tries left before pin is blocked.\n", tries); |
| 2068 | + } |
| 2069 | + else { |
| 2070 | + fprintf(stderr, "Pin code blocked, use unblock-pin action to unblock.\n"); |
| 2071 | + } |
| 2072 | + } |
| 2073 | + else { |
| 2074 | + fprintf(stderr, "Pin code verification failed: '%s'\n", ykpiv_strerror(res)); |
| 2075 | + } |
| 2076 | + return false; |
| 2077 | +} |
| 2078 | + |
2063 | 2079 | int main(int argc, char *argv[]) {
|
2064 |
| - struct gengetopt_args_info args_info; |
2065 | 2080 | ykpiv_state *state;
|
2066 | 2081 | int verbosity;
|
2067 | 2082 | enum enum_action action;
|
@@ -2352,21 +2367,7 @@ int main(int argc, char *argv[]) {
|
2352 | 2367 | }
|
2353 | 2368 | break;
|
2354 | 2369 | case action_arg_verifyMINUS_pin: {
|
2355 |
| - char pinbuf[8+2] = {0}; |
2356 |
| - char *pin = args_info.pin_arg; |
2357 |
| - |
2358 |
| - if(!pin) { |
2359 |
| - if (!read_pw("PIN", pinbuf, sizeof(pinbuf), false, args_info.stdin_input_flag)) { |
2360 |
| - fprintf(stderr, "Failed to get PIN.\n"); |
2361 |
| - ykpiv_done(state); |
2362 |
| - cmdline_parser_free(&args_info); |
2363 |
| - return EXIT_FAILURE; |
2364 |
| - } |
2365 |
| - pin = pinbuf; |
2366 |
| - } |
2367 |
| - if(verify_pin(state, pin)) { |
2368 |
| - fprintf(stderr, "Successfully verified PIN.\n"); |
2369 |
| - } else { |
| 2370 | + if(!verify_pin(state)) { |
2370 | 2371 | ret = EXIT_FAILURE;
|
2371 | 2372 | }
|
2372 | 2373 | break;
|
|
0 commit comments