| 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 vsix |
| 18 | |
| 19 | import ( |
| 20 | "archive/zip" |
| 21 | "errors" |
| 22 | "io" |
| 23 | "os" |
| 24 | "path" |
| 25 | |
| 26 | "github.com/beevik/etree" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 29 | "github.com/sassoftware/relic/v7/lib/magic" |
| 30 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 31 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 32 | "github.com/sassoftware/relic/v7/lib/xmldsig" |
| 33 | "github.com/sassoftware/relic/v7/signers" |
| 34 | "github.com/sassoftware/relic/v7/signers/zipbased" |
| 35 | ) |
| 36 | |
| 37 | var Signer = &signers.Signer{ |
| 38 | Name: "vsix", |
| 39 | Magic: magic.FileTypeVSIX, |
| 40 | CertTypes: signers.CertTypeX509, |
| 41 | Transform: zipbased.Transform, |
| 42 | Sign: sign, |
| 43 | Verify: verify, |
| 44 | } |
| 45 | |
| 46 | type zipFiles map[string]*zip.File |
| 47 | |
| 48 | func init() { |
| 49 | signers.Register(Signer) |
| 50 | Signer.Flags().Bool("detach-certs", false, "(VSIX) Package certificates separately in the archive") |
| 51 | } |
| 52 | |
| 53 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 54 | detachCerts := opts.Flags.GetBool("detach-certs") |
| 55 | m, err := mangleZip(r, opts.Hash) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | // add rels and origin to zip |
| 60 | sigName := path.Join(xmlSigPath, calcFileName(cert.Leaf)+".psdsxs") |
| 61 | if err := m.newRels("", originPath, sigOriginType); err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | if err := m.newRels(originPath, sigName, sigType); err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | if err := m.addOrigin(); err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | // add certs (optional) |
| 71 | if detachCerts { |
| 72 | if err := m.addCerts(cert, sigName); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | } |
| 76 | // sign and add ctypes |
| 77 | sigfile, err := m.makeSignature(cert, opts, detachCerts) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | if err := m.m.NewFile(sigName, sigfile); err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | if err := m.newCtypes(detachCerts); err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | patch, err := m.m.MakePatch(true) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | return opts.SetBinPatch(patch) |
| 92 | } |
| 93 | |
| 94 | func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 95 | // read zip file |
| 96 | size, err := f.Seek(0, io.SeekEnd) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | inz, err := zip.NewReader(f, size) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | files := make(zipFiles, len(inz.File)) |
| 105 | for _, f := range inz.File { |
| 106 | files[f.Name] = f |
| 107 | } |
| 108 | // find and parse the signature XML |
| 109 | sig, certs, err := readSignature(files) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | doc := etree.NewDocument() |
| 114 | if err := doc.ReadFromString(string(sig)); err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | root := doc.Root() |
| 118 | // basic verification of XML |
| 119 | xs, err := xmldsig.Verify(root, ".", certs) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | // verify digests of files |
| 124 | if err := checkManifest(files, xs.Reference); err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | // verify PKCS#9 timestamp token |
| 128 | cs, err := checkTimestamp(root, xs.EncryptedDigest) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | psig := pkcs7.Signature{Intermediates: xs.Certificates, Certificate: xs.Leaf()} |
| 133 | if psig.Certificate == nil { |
| 134 | return nil, errors.New("leaf x509 certificate not found") |
| 135 | } |
| 136 | return []*signers.Signature{&signers.Signature{ |
| 137 | Hash: xs.Hash, |
| 138 | X509Signature: &pkcs9.TimestampedSignature{ |
| 139 | Signature: psig, |
| 140 | CounterSignature: cs, |
| 141 | }, |
| 142 | }}, nil |
| 143 | } |