| 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 msi |
| 18 | |
| 19 | // Sign Microsoft Installer files |
| 20 | |
| 21 | import ( |
| 22 | "io" |
| 23 | "io/ioutil" |
| 24 | "os" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/lib/atomicfile" |
| 27 | "github.com/sassoftware/relic/v7/lib/authenticode" |
| 28 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 29 | "github.com/sassoftware/relic/v7/lib/comdoc" |
| 30 | "github.com/sassoftware/relic/v7/lib/magic" |
| 31 | "github.com/sassoftware/relic/v7/signers" |
| 32 | "github.com/sassoftware/relic/v7/signers/pecoff" |
| 33 | ) |
| 34 | |
| 35 | var MsiSigner = &signers.Signer{ |
| 36 | Name: "msi", |
| 37 | Aliases: []string{"msi-tar"}, |
| 38 | Magic: magic.FileTypeMSI, |
| 39 | CertTypes: signers.CertTypeX509, |
| 40 | Transform: transform, |
| 41 | Sign: sign, |
| 42 | Verify: verify, |
| 43 | } |
| 44 | |
| 45 | func init() { |
| 46 | MsiSigner.Flags().Bool("no-extended-sig", false, "(MSI) Don't emit a MsiDigitalSignatureEx digest") |
| 47 | pecoff.AddOpusFlags(MsiSigner) |
| 48 | signers.Register(MsiSigner) |
| 49 | } |
| 50 | |
| 51 | type msiTransformer struct { |
| 52 | f *os.File |
| 53 | cdf *comdoc.ComDoc |
| 54 | exsig []byte |
| 55 | } |
| 56 | |
| 57 | func transform(f *os.File, opts signers.SignOpts) (signers.Transformer, error) { |
| 58 | cdf, err := comdoc.ReadFile(f) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | var exsig []byte |
| 63 | noExtended := opts.Flags.GetBool("no-extended-sig") |
| 64 | if !noExtended { |
| 65 | exsig, err = authenticode.PrehashMSI(cdf, opts.Hash) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | } |
| 70 | return &msiTransformer{f, cdf, exsig}, nil |
| 71 | } |
| 72 | |
| 73 | // transform the MSI to a tar stream for upload |
| 74 | func (t *msiTransformer) GetReader() (io.Reader, error) { |
| 75 | r, w := io.Pipe() |
| 76 | go func() { |
| 77 | _ = w.CloseWithError(authenticode.MsiToTar(t.cdf, w)) |
| 78 | }() |
| 79 | return r, nil |
| 80 | } |
| 81 | |
| 82 | // apply a signed PKCS#7 blob to an already-open MSI document |
| 83 | func (t *msiTransformer) Apply(dest, mimeType string, result io.Reader) error { |
| 84 | t.cdf.Close() |
| 85 | blob, err := ioutil.ReadAll(result) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | // copy src to dest if needed, otherwise open in-place |
| 90 | f, err := atomicfile.WriteInPlace(t.f, dest) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | defer f.Close() |
| 95 | cdf, err := comdoc.WriteFile(f.GetFile()) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if err := authenticode.InsertMSISignature(cdf, blob, t.exsig); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if err := cdf.Close(); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | return f.Commit() |
| 106 | } |
| 107 | |
| 108 | // sign a transformed tarball and return the PKCS#7 blob |
| 109 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 110 | noExtended := opts.Flags.GetBool("no-extended-sig") |
| 111 | sum, err := authenticode.DigestMsiTar(r, opts.Hash, !noExtended) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | ts, err := authenticode.SignMSIImprint(opts.Context(), sum, opts.Hash, cert, pecoff.OpusFlags(opts)) |
| 116 | if err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | return opts.SetPkcs7(ts) |
| 120 | } |
| 121 | |
| 122 | func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 123 | sig, err := authenticode.VerifyMSI(f, opts.NoDigests) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | return []*signers.Signature{{ |
| 128 | Hash: sig.HashFunc, |
| 129 | X509Signature: &sig.TimestampedSignature, |
| 130 | SigInfo: pecoff.FormatOpus(sig.OpusInfo), |
| 131 | }}, nil |
| 132 | } |