| 1 | package ctscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/ |
| 2 | |
| 3 | import ( |
| 4 | "cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/" |
| 5 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/" |
| 6 | "crypto/ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/" |
| 7 | "crypto/rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/" |
| 8 | "crypto/sha256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/" |
| 9 | "crypto/x509scip-go gomod github.com/golang/go/src go1.16 `crypto/x509`/" |
| 10 | "encoding/asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/" |
| 11 | "encoding/pemscip-go gomod github.com/golang/go/src go1.16 `encoding/pem`/" |
| 12 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 13 | //"flag" |
| 14 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 15 | "logscip-go gomod github.com/golang/go/src go1.16 log/" |
| 16 | "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/" |
| 17 | ) |
| 18 | |
| 19 | var allowVerificationWithNonCompliantKeysscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/allowVerificationWithNonCompliantKeys. = false //flag.Bool("allow_verification_with_non_compliant_keys", false, |
| 20 | //"Allow a SignatureVerifier to use keys which are technically non-compliant with RFC6962.") |
| 21 | |
| 22 | // PublicKeyFromPEM parses a PEM formatted block and returns the public key contained within and any remaining unread bytes, or an error. |
| 23 | func PublicKeyFromPEMscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/PublicKeyFromPEM().(b []byte) (cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 crypto/PublicKey#, SHA256Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SHA256Hash#, []byte, error) { |
| 24 | p, rest := pemscip-go gomod github.com/golang/go/src go1.16 `encoding/pem`/.Decodescip-go gomod github.com/golang/go/src go1.16 `encoding/pem`/Decode().(b) |
| 25 | if p == nil { |
| 26 | return nil, [sha256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/.Sizescip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/Size.]byte{}, rest, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("no PEM block found in %s", string(b)) |
| 27 | } |
| 28 | k, err := x509scip-go gomod github.com/golang/go/src go1.16 `crypto/x509`/.ParsePKIXPublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/x509`/ParsePKIXPublicKey().(p.Bytesscip-go gomod github.com/golang/go/src go1.16 `encoding/pem`/Block#Bytes.) |
| 29 | return k, sha256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/.Sum256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/Sum256().(p.Bytesscip-go gomod github.com/golang/go/src go1.16 `encoding/pem`/Block#Bytes.), rest, err |
| 30 | } |
| 31 | |
| 32 | // SignatureVerifier can verify signatures on SCTs and STHs |
| 33 | type SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier# struct { |
| 34 | pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey. cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 crypto/PublicKey# |
| 35 | } |
| 36 | |
| 37 | // NewSignatureVerifier creates a new SignatureVerifier using the passed in PublicKey. |
| 38 | func NewSignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/NewSignatureVerifier().(pk cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 crypto/PublicKey#) (*SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#, error) { |
| 39 | switch pkType := pk.(type) { |
| 40 | case *rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#: |
| 41 | if pkType.Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N..BitLenscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#BitLen().() < 2048 { |
| 42 | e := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("public key is RSA with < 2048 bits (size:%d)", pkType.Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N..BitLenscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#BitLen().()) |
| 43 | if !(allowVerificationWithNonCompliantKeysscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/allowVerificationWithNonCompliantKeys.) { |
| 44 | return nil, e |
| 45 | } |
| 46 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("WARNING: %v", e) |
| 47 | } |
| 48 | case *ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#: |
| 49 | params := *(pkType.Paramsscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#Params().()) |
| 50 | if params != *ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.P256scip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/P256().().Paramsscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#Params().() { |
| 51 | e := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("public is ECDSA, but not on the P256 curve") |
| 52 | if !(allowVerificationWithNonCompliantKeysscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/allowVerificationWithNonCompliantKeys.) { |
| 53 | return nil, e |
| 54 | } |
| 55 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("WARNING: %v", e) |
| 56 | |
| 57 | } |
| 58 | default: |
| 59 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("Unsupported public key type %v", pkType) |
| 60 | } |
| 61 | |
| 62 | return &SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#{ |
| 63 | pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey.: pk, |
| 64 | }, nil |
| 65 | } |
| 66 | |
| 67 | // verifySignature verifies that the passed in signature over data was created by our PublicKey. |
| 68 | // Currently, only SHA256 is supported as a HashAlgorithm, and only ECDSA and RSA signatures are supported. |
| 69 | func (s SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#) verifySignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#verifySignature().(data []byte, sig DigitallySignedscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#) error { |
| 70 | if sig.HashAlgorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#HashAlgorithm. != SHA256scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SHA256. { |
| 71 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("unsupported HashAlgorithm in signature: %v", sig.HashAlgorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#HashAlgorithm.) |
| 72 | } |
| 73 | |
| 74 | hasherType := cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.16 crypto/SHA256. |
| 75 | hasher := hasherType.Newscip-go gomod github.com/golang/go/src go1.16 crypto/Hash#New().() |
| 76 | if _, err := hasher.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(data); err != nil { |
| 77 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to write to hasher: %v", err) |
| 78 | } |
| 79 | hash := hasher.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().([]byte{}) |
| 80 | |
| 81 | switch sig.SignatureAlgorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#SignatureAlgorithm. { |
| 82 | case RSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/RSA.: |
| 83 | rsaKey, ok := s.pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey..(*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#) |
| 84 | if !ok { |
| 85 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("cannot verify RSA signature with %T key", s.pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey.) |
| 86 | } |
| 87 | if err := rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.VerifyPKCS1v15scip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/VerifyPKCS1v15().(rsaKey, hasherType, hash, sig.Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#Signature.); err != nil { |
| 88 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to verify rsa signature: %v", err) |
| 89 | } |
| 90 | case ECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/ECDSA.: |
| 91 | ecdsaKey, ok := s.pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey..(*ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#) |
| 92 | if !ok { |
| 93 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("cannot verify ECDSA signature with %T key", s.pubKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#pubKey.) |
| 94 | } |
| 95 | var ecdsaSig struct { |
| 96 | R, S *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int# |
| 97 | } |
| 98 | rest, err := asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/Unmarshal().(sig.Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#Signature., &ecdsaSig) |
| 99 | if err != nil { |
| 100 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to unmarshal ECDSA signature: %v", err) |
| 101 | } |
| 102 | if len(rest) != 0 { |
| 103 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Garbage following signature %v", rest) |
| 104 | } |
| 105 | |
| 106 | if !ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.Verifyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/Verify().(ecdsaKey, hash, ecdsaSig.R, ecdsaSig.S) { |
| 107 | return errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("failed to verify ecdsa signature") |
| 108 | } |
| 109 | default: |
| 110 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("unsupported signature type %v", sig.SignatureAlgorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/DigitallySigned#SignatureAlgorithm.) |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | // VerifySCTSignature verifies that the SCT's signature is valid for the given LogEntry |
| 116 | func (s SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#) VerifySCTSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#VerifySCTSignature().(sct SignedCertificateTimestampscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignedCertificateTimestamp#, entry LogEntryscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/LogEntry#) error { |
| 117 | sctData, err := SerializeSCTSignatureInputscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SerializeSCTSignatureInput().(sct, entry) |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | return s.verifySignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#verifySignature().(sctData, sct.Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignedCertificateTimestamp#Signature.) |
| 122 | } |
| 123 | |
| 124 | // VerifySTHSignature verifies that the STH's signature is valid. |
| 125 | func (s SignatureVerifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#) VerifySTHSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#VerifySTHSignature().(sth SignedTreeHeadscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignedTreeHead#) error { |
| 126 | sthData, err := SerializeSTHSignatureInputscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SerializeSTHSignatureInput().(sth) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | return s.verifySignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignatureVerifier#verifySignature().(sthData, sth.TreeHeadSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct`/SignedTreeHead#TreeHeadSignature.) |
| 131 | } |