signature.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tls
  15. import (
  16. "crypto"
  17. "crypto/dsa"
  18. "crypto/ecdsa"
  19. _ "crypto/md5" // For registration side-effect
  20. "crypto/rand"
  21. "crypto/rsa"
  22. _ "crypto/sha1" // For registration side-effect
  23. _ "crypto/sha256" // For registration side-effect
  24. _ "crypto/sha512" // For registration side-effect
  25. "errors"
  26. "fmt"
  27. "log"
  28. "math/big"
  29. "github.com/google/certificate-transparency-go/asn1"
  30. )
  31. type dsaSig struct {
  32. R, S *big.Int
  33. }
  34. func generateHash(algo HashAlgorithm, data []byte) ([]byte, crypto.Hash, error) {
  35. var hashType crypto.Hash
  36. switch algo {
  37. case MD5:
  38. hashType = crypto.MD5
  39. case SHA1:
  40. hashType = crypto.SHA1
  41. case SHA224:
  42. hashType = crypto.SHA224
  43. case SHA256:
  44. hashType = crypto.SHA256
  45. case SHA384:
  46. hashType = crypto.SHA384
  47. case SHA512:
  48. hashType = crypto.SHA512
  49. default:
  50. return nil, hashType, fmt.Errorf("unsupported Algorithm.Hash in signature: %v", algo)
  51. }
  52. hasher := hashType.New()
  53. if _, err := hasher.Write(data); err != nil {
  54. return nil, hashType, fmt.Errorf("failed to write to hasher: %v", err)
  55. }
  56. return hasher.Sum([]byte{}), hashType, nil
  57. }
  58. // VerifySignature verifies that the passed in signature over data was created by the given PublicKey.
  59. func VerifySignature(pubKey crypto.PublicKey, data []byte, sig DigitallySigned) error {
  60. hash, hashType, err := generateHash(sig.Algorithm.Hash, data)
  61. if err != nil {
  62. return err
  63. }
  64. switch sig.Algorithm.Signature {
  65. case RSA:
  66. rsaKey, ok := pubKey.(*rsa.PublicKey)
  67. if !ok {
  68. return fmt.Errorf("cannot verify RSA signature with %T key", pubKey)
  69. }
  70. if err := rsa.VerifyPKCS1v15(rsaKey, hashType, hash, sig.Signature); err != nil {
  71. return fmt.Errorf("failed to verify rsa signature: %v", err)
  72. }
  73. case DSA:
  74. dsaKey, ok := pubKey.(*dsa.PublicKey)
  75. if !ok {
  76. return fmt.Errorf("cannot verify DSA signature with %T key", pubKey)
  77. }
  78. var dsaSig dsaSig
  79. rest, err := asn1.Unmarshal(sig.Signature, &dsaSig)
  80. if err != nil {
  81. return fmt.Errorf("failed to unmarshal DSA signature: %v", err)
  82. }
  83. if len(rest) != 0 {
  84. log.Printf("Garbage following signature %v", rest)
  85. }
  86. if dsaSig.R.Sign() <= 0 || dsaSig.S.Sign() <= 0 {
  87. return errors.New("DSA signature contained zero or negative values")
  88. }
  89. if !dsa.Verify(dsaKey, hash, dsaSig.R, dsaSig.S) {
  90. return errors.New("failed to verify DSA signature")
  91. }
  92. case ECDSA:
  93. ecdsaKey, ok := pubKey.(*ecdsa.PublicKey)
  94. if !ok {
  95. return fmt.Errorf("cannot verify ECDSA signature with %T key", pubKey)
  96. }
  97. var ecdsaSig dsaSig
  98. rest, err := asn1.Unmarshal(sig.Signature, &ecdsaSig)
  99. if err != nil {
  100. return fmt.Errorf("failed to unmarshal ECDSA signature: %v", err)
  101. }
  102. if len(rest) != 0 {
  103. log.Printf("Garbage following signature %v", rest)
  104. }
  105. if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
  106. return errors.New("ECDSA signature contained zero or negative values")
  107. }
  108. if !ecdsa.Verify(ecdsaKey, hash, ecdsaSig.R, ecdsaSig.S) {
  109. return errors.New("failed to verify ECDSA signature")
  110. }
  111. default:
  112. return fmt.Errorf("unsupported Algorithm.Signature in signature: %v", sig.Algorithm.Hash)
  113. }
  114. return nil
  115. }
  116. // CreateSignature builds a signature over the given data using the specified hash algorithm and private key.
  117. func CreateSignature(privKey crypto.PrivateKey, hashAlgo HashAlgorithm, data []byte) (DigitallySigned, error) {
  118. var sig DigitallySigned
  119. sig.Algorithm.Hash = hashAlgo
  120. hash, hashType, err := generateHash(sig.Algorithm.Hash, data)
  121. if err != nil {
  122. return sig, err
  123. }
  124. switch privKey := privKey.(type) {
  125. case rsa.PrivateKey:
  126. sig.Algorithm.Signature = RSA
  127. sig.Signature, err = rsa.SignPKCS1v15(rand.Reader, &privKey, hashType, hash)
  128. return sig, err
  129. case ecdsa.PrivateKey:
  130. sig.Algorithm.Signature = ECDSA
  131. var ecdsaSig dsaSig
  132. ecdsaSig.R, ecdsaSig.S, err = ecdsa.Sign(rand.Reader, &privKey, hash)
  133. if err != nil {
  134. return sig, err
  135. }
  136. sig.Signature, err = asn1.Marshal(ecdsaSig)
  137. return sig, err
  138. default:
  139. return sig, fmt.Errorf("unsupported private key type %T", privKey)
  140. }
  141. }