sk-ecdsa.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "includes.h"
  2. #if DROPBEAR_SK_ECDSA
  3. #include "dbutil.h"
  4. #include "ecc.h"
  5. #include "ecdsa.h"
  6. #include "sk-ecdsa.h"
  7. #include "ssh.h"
  8. int buf_sk_ecdsa_verify(buffer *buf, const ecc_key *key, const buffer *data_buf, const char* app, unsigned int applen) {
  9. hash_state hs;
  10. unsigned char subhash[SHA256_HASH_SIZE];
  11. buffer *sk_buffer = NULL, *sig_buffer = NULL;
  12. unsigned char flags;
  13. unsigned int counter;
  14. int ret;
  15. TRACE(("buf_sk_ecdsa_verify"))
  16. /* from https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.u2f */
  17. /* ecdsa signature to verify (r, s) */
  18. sig_buffer = buf_getbuf(buf);
  19. flags = buf_getbyte (buf);
  20. counter = buf_getint (buf);
  21. /* create the message to be signed */
  22. sk_buffer = buf_new (2*SHA256_HASH_SIZE+5);
  23. sha256_init (&hs);
  24. sha256_process (&hs, app, applen);
  25. sha256_done (&hs, subhash);
  26. buf_putbytes (sk_buffer, subhash, sizeof (subhash));
  27. buf_putbyte (sk_buffer, flags);
  28. buf_putint (sk_buffer, counter);
  29. sha256_init (&hs);
  30. sha256_process (&hs, data_buf->data, data_buf->len);
  31. sha256_done (&hs, subhash);
  32. buf_putbytes (sk_buffer, subhash, sizeof (subhash));
  33. ret = buf_ecdsa_verify(sig_buffer, key, sk_buffer);
  34. buf_free(sk_buffer);
  35. buf_free(sig_buffer);
  36. /* TODO: allow "no-touch-required" or "verify-required" authorized_keys options */
  37. if (!(flags & SSH_SK_USER_PRESENCE_REQD)) {
  38. if (ret == DROPBEAR_SUCCESS) {
  39. dropbear_log(LOG_WARNING, "Rejecting, user-presence not set");
  40. }
  41. ret = DROPBEAR_FAILURE;
  42. }
  43. TRACE(("leave buf_sk_ecdsa_verify, ret=%d", ret))
  44. return ret;
  45. }
  46. #endif /* DROPBEAR_SK_ECDSA */