Debian Codegraph @

ietf-cms/verify.go

raw symbols SCIP
1 package cms
2
3 import (
4 "bytes"
5 "crypto/x509"
6 "errors"
7
8 "github.com/github/smimesign/ietf-cms/protocol"
9 )
10
11 // Verify verifies the SingerInfos' signatures. Each signature's associated
12 // certificate is verified using the provided roots. UnsafeNoVerify may be
13 // specified to skip this verification. Nil may be provided to use system roots.
14 // The full chains for the certificates whose keys made the signatures are
15 // returned.
16 //
17 // WARNING: this function doesn't do any revocation checking.
18 func (sd *SignedData) Verify(opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
19 econtent, err := sd.psd.EncapContentInfo.EContentValue()
20 if err != nil {
21 return nil, err
22 }
23 if econtent == nil {
24 return nil, errors.New("detached signature")
25 }
26
27 return sd.verify(econtent, opts)
28 }
29
30 // VerifyDetached verifies the SingerInfos' detached signatures over the
31 // provided data message. Each signature's associated certificate is verified
32 // using the provided roots. UnsafeNoVerify may be specified to skip this
33 // verification. Nil may be provided to use system roots. The full chains for
34 // the certificates whose keys made the signatures are returned.
35 //
36 // WARNING: this function doesn't do any revocation checking.
37 func (sd *SignedData) VerifyDetached(message []byte, opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
38 if sd.psd.EncapContentInfo.EContent.Bytes != nil {
39 return nil, errors.New("signature not detached")
40 }
41 return sd.verify(message, opts)
42 }
43
44 func (sd *SignedData) verify(econtent []byte, opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
45 if len(sd.psd.SignerInfos) == 0 {
46 return nil, protocol.ASN1Error{Message: "no signatures found"}
47 }
48
49 certs, err := sd.psd.X509Certificates()
50 if err != nil {
51 return nil, err
52 }
53
54 if opts.Intermediates == nil {
55 opts.Intermediates = x509.NewCertPool()
56 }
57
58 for _, cert := range certs {
59 opts.Intermediates.AddCert(cert)
60 }
61
62 // Use provided verification options for timestamp verification also, but
63 // explicitly ask for key-usage=timestamping.
64 tsOpts := opts
65 tsOpts.KeyUsages = []x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping}
66
67 chains := make([][][]*x509.Certificate, 0, len(sd.psd.SignerInfos))
68
69 for _, si := range sd.psd.SignerInfos {
70 var signedMessage []byte
71
72 // SignedAttrs is optional if EncapContentInfo eContentType isn't id-data.
73 if si.SignedAttrs == nil {
74 // SignedAttrs may only be absent if EncapContentInfo eContentType is
75 // id-data.
76 if !sd.psd.EncapContentInfo.IsTypeData() {
77 return nil, protocol.ASN1Error{Message: "missing SignedAttrs"}
78 }
79
80 // If SignedAttrs is absent, the signature is over the original
81 // encapsulated content itself.
82 signedMessage = econtent
83 } else {
84 // If SignedAttrs is present, we validate the mandatory ContentType and
85 // MessageDigest attributes.
86 siContentType, err := si.GetContentTypeAttribute()
87 if err != nil {
88 return nil, err
89 }
90 if !siContentType.Equal(sd.psd.EncapContentInfo.EContentType) {
91 return nil, protocol.ASN1Error{Message: "invalid SignerInfo ContentType attribute"}
92 }
93
94 // Calculate the digest over the actual message.
95 hash, err := si.Hash()
96 if err != nil {
97 return nil, err
98 }
99 actualMessageDigest := hash.New()
100 if _, err = actualMessageDigest.Write(econtent); err != nil {
101 return nil, err
102 }
103
104 // Get the digest from the SignerInfo.
105 messageDigestAttr, err := si.GetMessageDigestAttribute()
106 if err != nil {
107 return nil, err
108 }
109
110 // Make sure message digests match.
111 if !bytes.Equal(messageDigestAttr, actualMessageDigest.Sum(nil)) {
112 return nil, errors.New("invalid message digest")
113 }
114
115 // The signature is over the DER encoded signed attributes, minus the
116 // leading class/tag/length bytes. This includes the digest of the
117 // original message, so it is implicitly signed too.
118 if signedMessage, err = si.SignedAttrs.MarshaledForVerification(); err != nil {
119 return nil, err
120 }
121 }
122
123 cert, err := si.FindCertificate(certs)
124 if err != nil {
125 return nil, err
126 }
127
128 algo := si.X509SignatureAlgorithm()
129 if algo == x509.UnknownSignatureAlgorithm {
130 return nil, protocol.ErrUnsupported
131 }
132
133 if err := cert.CheckSignature(algo, signedMessage, si.Signature); err != nil {
134 return nil, err
135 }
136
137 // If the caller didn't specify the signature time, we'll use the verified
138 // timestamp. If there's no timestamp we use the current time when checking
139 // the cert validity window. This isn't perfect because the signature may
140 // have been created before the cert's not-before date, but this is the best
141 // we can do. We update a copy of opts because we are verifying multiple
142 // signatures in a loop and only want the timestamp to affect this one.
143 optsCopy := opts
144
145 if hasTS, err := hasTimestamp(si); err != nil {
146 return nil, err
147 } else if hasTS {
148 tsti, err := getTimestamp(si, tsOpts)
149 if err != nil {
150 return nil, err
151 }
152
153 // This check is slightly redundant, given that the cert validity times
154 // are checked by cert.Verify. We take the timestamp accuracy into account
155 // here though, whereas cert.Verify will not.
156 if !tsti.Before(cert.NotAfter) || !tsti.After(cert.NotBefore) {
157 return nil, x509.CertificateInvalidError{Cert: cert, Reason: x509.Expired, Detail: ""}
158 }
159
160 if optsCopy.CurrentTime.IsZero() {
161 optsCopy.CurrentTime = tsti.GenTime
162 }
163 }
164
165 if chain, err := cert.Verify(optsCopy); err != nil {
166 return nil, err
167 } else {
168 chains = append(chains, chain)
169 }
170 }
171
172 // OK
173 return chains, nil
174 }