| 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 cab |
| 18 | |
| 19 | // Sign Microsoft cabinet files |
| 20 | |
| 21 | import ( |
| 22 | "io" |
| 23 | "os" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/lib/authenticode" |
| 26 | "github.com/sassoftware/relic/v7/lib/cabfile" |
| 27 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 28 | "github.com/sassoftware/relic/v7/lib/magic" |
| 29 | "github.com/sassoftware/relic/v7/signers" |
| 30 | "github.com/sassoftware/relic/v7/signers/pecoff" |
| 31 | ) |
| 32 | |
| 33 | var CabSigner = &signers.Signer{ |
| 34 | Name: "cab", |
| 35 | Magic: magic.FileTypeCAB, |
| 36 | CertTypes: signers.CertTypeX509, |
| 37 | Sign: sign, |
| 38 | Verify: verify, |
| 39 | } |
| 40 | |
| 41 | func init() { |
| 42 | pecoff.AddOpusFlags(CabSigner) |
| 43 | signers.Register(CabSigner) |
| 44 | } |
| 45 | |
| 46 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 47 | digest, err := cabfile.Digest(r, opts.Hash) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | patch, ts, err := authenticode.SignCabImprint(opts.Context(), digest, cert, pecoff.OpusFlags(opts)) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | opts.Audit.SetCounterSignature(ts.CounterSignature) |
| 56 | return opts.SetBinPatch(patch) |
| 57 | } |
| 58 | |
| 59 | func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 60 | sig, err := authenticode.VerifyCab(f, opts.NoDigests) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return []*signers.Signature{{ |
| 65 | Hash: sig.HashFunc, |
| 66 | X509Signature: &sig.TimestampedSignature, |
| 67 | SigInfo: pecoff.FormatOpus(sig.OpusInfo), |
| 68 | }}, nil |
| 69 | } |