signatures.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package ct
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rsa"
  7. "crypto/sha256"
  8. "crypto/x509"
  9. "encoding/asn1"
  10. "encoding/pem"
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "log"
  15. "math/big"
  16. )
  17. var allowVerificationWithNonCompliantKeys = flag.Bool("allow_verification_with_non_compliant_keys", false,
  18. "Allow a SignatureVerifier to use keys which are technically non-compliant with RFC6962.")
  19. // PublicKeyFromPEM parses a PEM formatted block and returns the public key contained within and any remaining unread bytes, or an error.
  20. func PublicKeyFromPEM(b []byte) (crypto.PublicKey, SHA256Hash, []byte, error) {
  21. p, rest := pem.Decode(b)
  22. if p == nil {
  23. return nil, [sha256.Size]byte{}, rest, fmt.Errorf("no PEM block found in %s", string(b))
  24. }
  25. k, err := x509.ParsePKIXPublicKey(p.Bytes)
  26. return k, sha256.Sum256(p.Bytes), rest, err
  27. }
  28. // SignatureVerifier can verify signatures on SCTs and STHs
  29. type SignatureVerifier struct {
  30. pubKey crypto.PublicKey
  31. }
  32. // NewSignatureVerifier creates a new SignatureVerifier using the passed in PublicKey.
  33. func NewSignatureVerifier(pk crypto.PublicKey) (*SignatureVerifier, error) {
  34. switch pkType := pk.(type) {
  35. case *rsa.PublicKey:
  36. if pkType.N.BitLen() < 2048 {
  37. e := fmt.Errorf("public key is RSA with < 2048 bits (size:%d)", pkType.N.BitLen())
  38. if !(*allowVerificationWithNonCompliantKeys) {
  39. return nil, e
  40. }
  41. log.Printf("WARNING: %v", e)
  42. }
  43. case *ecdsa.PublicKey:
  44. params := *(pkType.Params())
  45. if params != *elliptic.P256().Params() {
  46. e := fmt.Errorf("public is ECDSA, but not on the P256 curve")
  47. if !(*allowVerificationWithNonCompliantKeys) {
  48. return nil, e
  49. }
  50. log.Printf("WARNING: %v", e)
  51. }
  52. default:
  53. return nil, fmt.Errorf("Unsupported public key type %v", pkType)
  54. }
  55. return &SignatureVerifier{
  56. pubKey: pk,
  57. }, nil
  58. }
  59. // verifySignature verifies that the passed in signature over data was created by our PublicKey.
  60. // Currently, only SHA256 is supported as a HashAlgorithm, and only ECDSA and RSA signatures are supported.
  61. func (s SignatureVerifier) verifySignature(data []byte, sig DigitallySigned) error {
  62. if sig.HashAlgorithm != SHA256 {
  63. return fmt.Errorf("unsupported HashAlgorithm in signature: %v", sig.HashAlgorithm)
  64. }
  65. hasherType := crypto.SHA256
  66. hasher := hasherType.New()
  67. if _, err := hasher.Write(data); err != nil {
  68. return fmt.Errorf("failed to write to hasher: %v", err)
  69. }
  70. hash := hasher.Sum([]byte{})
  71. switch sig.SignatureAlgorithm {
  72. case RSA:
  73. rsaKey, ok := s.pubKey.(*rsa.PublicKey)
  74. if !ok {
  75. return fmt.Errorf("cannot verify RSA signature with %T key", s.pubKey)
  76. }
  77. if err := rsa.VerifyPKCS1v15(rsaKey, hasherType, hash, sig.Signature); err != nil {
  78. return fmt.Errorf("failed to verify rsa signature: %v", err)
  79. }
  80. case ECDSA:
  81. ecdsaKey, ok := s.pubKey.(*ecdsa.PublicKey)
  82. if !ok {
  83. return fmt.Errorf("cannot verify ECDSA signature with %T key", s.pubKey)
  84. }
  85. var ecdsaSig struct {
  86. R, S *big.Int
  87. }
  88. rest, err := asn1.Unmarshal(sig.Signature, &ecdsaSig)
  89. if err != nil {
  90. return fmt.Errorf("failed to unmarshal ECDSA signature: %v", err)
  91. }
  92. if len(rest) != 0 {
  93. log.Printf("Garbage following signature %v", rest)
  94. }
  95. if !ecdsa.Verify(ecdsaKey, hash, ecdsaSig.R, ecdsaSig.S) {
  96. return errors.New("failed to verify ecdsa signature")
  97. }
  98. default:
  99. return fmt.Errorf("unsupported signature type %v", sig.SignatureAlgorithm)
  100. }
  101. return nil
  102. }
  103. // VerifySCTSignature verifies that the SCT's signature is valid for the given LogEntry
  104. func (s SignatureVerifier) VerifySCTSignature(sct SignedCertificateTimestamp, entry LogEntry) error {
  105. sctData, err := SerializeSCTSignatureInput(sct, entry)
  106. if err != nil {
  107. return err
  108. }
  109. return s.verifySignature(sctData, sct.Signature)
  110. }
  111. // VerifySTHSignature verifies that the STH's signature is valid.
  112. func (s SignatureVerifier) VerifySTHSignature(sth SignedTreeHead) error {
  113. sthData, err := SerializeSTHSignatureInput(sth)
  114. if err != nil {
  115. return err
  116. }
  117. return s.verifySignature(sthData, sth.TreeHeadSignature)
  118. }