keyauth.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Package keyauth verifies the signatures over ACME key authorizations that
  2. // device-attest-01 attestation statements carry.
  3. package keyauth
  4. import (
  5. "crypto"
  6. "crypto/ecdsa"
  7. "crypto/ed25519"
  8. "crypto/elliptic"
  9. "crypto/rsa"
  10. "crypto/sha256"
  11. "crypto/x509"
  12. "go.step.sm/crypto/mldsa"
  13. "github.com/smallstep/certificates/attestation"
  14. )
  15. // Verify verifies that sig is a signature of the key authorization made with
  16. // the private key of the given attestation certificate. Its errors match
  17. // attestation.ErrInvalidEvidence.
  18. func Verify(cert *x509.Certificate, keyAuth, sig []byte) error {
  19. switch pub := cert.PublicKey.(type) {
  20. case *ecdsa.PublicKey:
  21. if pub.Curve != elliptic.P256() {
  22. return attestation.Invalidf("unsupported elliptic curve %s", pub.Curve)
  23. }
  24. sum := sha256.Sum256(keyAuth)
  25. if !ecdsa.VerifyASN1(pub, sum[:], sig) {
  26. return attestation.Invalidf("failed validating signature")
  27. }
  28. case *rsa.PublicKey:
  29. sum := sha256.Sum256(keyAuth)
  30. if err := rsa.VerifyPKCS1v15(pub, crypto.SHA256, sum[:], sig); err != nil {
  31. return attestation.Invalidf("failed validating signature")
  32. }
  33. case ed25519.PublicKey:
  34. if !ed25519.Verify(pub, keyAuth, sig) {
  35. return attestation.Invalidf("failed validating signature")
  36. }
  37. case *mldsa.PublicKey:
  38. if err := mldsa.Verify(pub, keyAuth, sig, nil); err != nil {
  39. return attestation.Invalidf("failed validating signature")
  40. }
  41. default:
  42. return attestation.Invalidf("unsupported public key type %T", pub)
  43. }
  44. return nil
  45. }