| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package pkcs9 |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/hmac" |
| 22 | "crypto/x509" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | ) |
| 30 | |
| 31 | // Verify that the digest (imprint) in a timestamp token matches the given data |
| 32 | func (i MessageImprint) Verify(data []byte) error { |
| 33 | hash, err := x509tools.PkixDigestToHashE(i.HashAlgorithm) |
| 34 | if err != nil { |
| 35 | return fmt.Errorf("pkcs9: %w", err) |
| 36 | } |
| 37 | w := hash.New() |
| 38 | w.Write(data) |
| 39 | digest := w.Sum(nil) |
| 40 | if !hmac.Equal(digest, i.HashedMessage) { |
| 41 | return errors.New("pkcs9: digest check failed") |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | // Verify a timestamp token using external data |
| 47 | func Verify(tst *pkcs7.ContentInfoSignedData, data []byte, certs []*x509.Certificate) (*CounterSignature, error) { |
| 48 | if len(tst.Content.SignerInfos) != 1 { |
| 49 | return nil, errors.New("timestamp should have exactly one SignerInfo") |
| 50 | } |
| 51 | tsi := tst.Content.SignerInfos[0] |
| 52 | tsicerts, certErr := tst.Content.Certificates.Parse() |
| 53 | if len(tsicerts) != 0 { |
| 54 | // keep both sets of certs just in case |
| 55 | certs = append(certs, tsicerts...) |
| 56 | } |
| 57 | // verify the imprint in the TSTInfo |
| 58 | tstinfo, err := unpackTokenInfo(tst) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | if err := tstinfo.MessageImprint.Verify(data); err != nil { |
| 63 | return nil, fmt.Errorf("verifying timestamp imprint: %w", err) |
| 64 | } |
| 65 | imprintHash, _ := x509tools.PkixDigestToHash(tstinfo.MessageImprint.HashAlgorithm) |
| 66 | // now the signature is over the TSTInfo blob |
| 67 | verifyBlob, err := tst.Content.ContentInfo.Bytes() |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | return finishVerify(&tsi, verifyBlob, certs, imprintHash, tstinfo, certErr) |
| 73 | } |
| 74 | |
| 75 | type timeSource interface { |
| 76 | SigningTime() (time.Time, error) |
| 77 | } |
| 78 | |
| 79 | func finishVerify(tsi *pkcs7.SignerInfo, blob []byte, certs []*x509.Certificate, hash crypto.Hash, timeSource timeSource, certErr error) (*CounterSignature, error) { |
| 80 | cert, err := tsi.Verify(blob, false, certs) |
| 81 | if err != nil { |
| 82 | if errors.As(err, &pkcs7.MissingCertificateError{}) && certErr != nil { |
| 83 | // surface saved parse error |
| 84 | return nil, certErr |
| 85 | } |
| 86 | return nil, err |
| 87 | } |
| 88 | signingTime, err := timeSource.SigningTime() |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("parsing timestamp: %w", err) |
| 91 | } |
| 92 | return &CounterSignature{ |
| 93 | Signature: pkcs7.Signature{ |
| 94 | SignerInfo: tsi, |
| 95 | Certificate: cert, |
| 96 | Intermediates: certs, |
| 97 | CertError: certErr, |
| 98 | }, |
| 99 | Hash: hash, |
| 100 | SigningTime: signingTime, |
| 101 | }, nil |
| 102 | } |