diff options
author | djm@openbsd.org <djm@openbsd.org> | 2020-04-28 04:02:29 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2020-05-01 13:13:29 +1000 |
commit | 59d2de956ed29aa5565ed5e5947a7abdb27ac013 (patch) | |
tree | 983def72c15590c50a0319c8048b9a7d56961edf /sk-usbhid.c | |
parent | c9d10dbc0ccfb1c7568bbb784f7aeb7a0b5ded12 (diff) |
upstream: when signing a challenge using a FIDO toke, perform the
hashing in the middleware layer rather than in ssh code. This allows
middlewares that call APIs that perform the hashing implicitly (including
Microsoft's AFAIK). ok markus@
OpenBSD-Commit-ID: c9fc8630aba26c75d5016884932f08a5a237f37d
Diffstat (limited to 'sk-usbhid.c')
-rw-r--r-- | sk-usbhid.c | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/sk-usbhid.c b/sk-usbhid.c index ad83054ad..db6c0057e 100644 --- a/sk-usbhid.c +++ b/sk-usbhid.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <stdio.h> | 24 | #include <stdio.h> |
25 | #include <stddef.h> | 25 | #include <stddef.h> |
26 | #include <stdarg.h> | 26 | #include <stdarg.h> |
27 | #include <sha2.h> | ||
27 | 28 | ||
28 | #ifdef WITH_OPENSSL | 29 | #ifdef WITH_OPENSSL |
29 | #include <openssl/opensslv.h> | 30 | #include <openssl/opensslv.h> |
@@ -31,6 +32,7 @@ | |||
31 | #include <openssl/bn.h> | 32 | #include <openssl/bn.h> |
32 | #include <openssl/ec.h> | 33 | #include <openssl/ec.h> |
33 | #include <openssl/ecdsa.h> | 34 | #include <openssl/ecdsa.h> |
35 | #include <openssl/evp.h> | ||
34 | #endif /* WITH_OPENSSL */ | 36 | #endif /* WITH_OPENSSL */ |
35 | 37 | ||
36 | #include <fido.h> | 38 | #include <fido.h> |
@@ -710,8 +712,28 @@ check_sign_load_resident_options(struct sk_option **options, char **devicep) | |||
710 | return 0; | 712 | return 0; |
711 | } | 713 | } |
712 | 714 | ||
715 | /* Calculate SHA256(m) */ | ||
716 | static int | ||
717 | sha256_mem(const void *m, size_t mlen, u_char *d, size_t dlen) | ||
718 | { | ||
719 | #ifdef WITH_OPENSSL | ||
720 | u_int mdlen; | ||
721 | #endif | ||
722 | |||
723 | if (dlen != 32) | ||
724 | return -1; | ||
725 | #ifdef WITH_OPENSSL | ||
726 | mdlen = dlen; | ||
727 | if (!EVP_Digest(m, mlen, d, &mdlen, EVP_sha256(), NULL)) | ||
728 | return -1; | ||
729 | #else | ||
730 | SHA256Data(m, mlen, d); | ||
731 | #endif | ||
732 | return 0; | ||
733 | } | ||
734 | |||
713 | int | 735 | int |
714 | sk_sign(uint32_t alg, const uint8_t *message, size_t message_len, | 736 | sk_sign(uint32_t alg, const uint8_t *data, size_t datalen, |
715 | const char *application, | 737 | const char *application, |
716 | const uint8_t *key_handle, size_t key_handle_len, | 738 | const uint8_t *key_handle, size_t key_handle_len, |
717 | uint8_t flags, const char *pin, struct sk_option **options, | 739 | uint8_t flags, const char *pin, struct sk_option **options, |
@@ -721,6 +743,7 @@ sk_sign(uint32_t alg, const uint8_t *message, size_t message_len, | |||
721 | char *device = NULL; | 743 | char *device = NULL; |
722 | fido_dev_t *dev = NULL; | 744 | fido_dev_t *dev = NULL; |
723 | struct sk_sign_response *response = NULL; | 745 | struct sk_sign_response *response = NULL; |
746 | uint8_t message[32]; | ||
724 | int ret = SSH_SK_ERR_GENERAL; | 747 | int ret = SSH_SK_ERR_GENERAL; |
725 | int r; | 748 | int r; |
726 | 749 | ||
@@ -735,7 +758,12 @@ sk_sign(uint32_t alg, const uint8_t *message, size_t message_len, | |||
735 | *sign_response = NULL; | 758 | *sign_response = NULL; |
736 | if (check_sign_load_resident_options(options, &device) != 0) | 759 | if (check_sign_load_resident_options(options, &device) != 0) |
737 | goto out; /* error already logged */ | 760 | goto out; /* error already logged */ |
738 | if ((dev = find_device(device, message, message_len, | 761 | /* hash data to be signed before it goes to the security key */ |
762 | if ((r = sha256_mem(data, datalen, message, sizeof(message))) != 0) { | ||
763 | skdebug(__func__, "hash message failed"); | ||
764 | goto out; | ||
765 | } | ||
766 | if ((dev = find_device(device, message, sizeof(message), | ||
739 | application, key_handle, key_handle_len)) == NULL) { | 767 | application, key_handle, key_handle_len)) == NULL) { |
740 | skdebug(__func__, "couldn't find device for key handle"); | 768 | skdebug(__func__, "couldn't find device for key handle"); |
741 | goto out; | 769 | goto out; |
@@ -745,7 +773,7 @@ sk_sign(uint32_t alg, const uint8_t *message, size_t message_len, | |||
745 | goto out; | 773 | goto out; |
746 | } | 774 | } |
747 | if ((r = fido_assert_set_clientdata_hash(assert, message, | 775 | if ((r = fido_assert_set_clientdata_hash(assert, message, |
748 | message_len)) != FIDO_OK) { | 776 | sizeof(message))) != FIDO_OK) { |
749 | skdebug(__func__, "fido_assert_set_clientdata_hash: %s", | 777 | skdebug(__func__, "fido_assert_set_clientdata_hash: %s", |
750 | fido_strerr(r)); | 778 | fido_strerr(r)); |
751 | goto out; | 779 | goto out; |
@@ -783,6 +811,7 @@ sk_sign(uint32_t alg, const uint8_t *message, size_t message_len, | |||
783 | response = NULL; | 811 | response = NULL; |
784 | ret = 0; | 812 | ret = 0; |
785 | out: | 813 | out: |
814 | explicit_bzero(message, sizeof(message)); | ||
786 | free(device); | 815 | free(device); |
787 | if (response != NULL) { | 816 | if (response != NULL) { |
788 | free(response->sig_r); | 817 | free(response->sig_r); |