| 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 signappx |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "context" |
| 22 | "crypto/x509" |
| 23 | "errors" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/lib/authenticode" |
| 26 | "github.com/sassoftware/relic/v7/lib/binpatch" |
| 27 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 28 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | SpcUUIDSipInfoAppx = []byte{0x4B, 0xDF, 0xC5, 0x0A, 0x07, 0xCE, 0xE2, 0x4D, 0xB7, 0x6E, 0x23, 0xC8, 0x39, 0xA0, 0x9F, 0xD1} |
| 33 | appxSipInfo = authenticode.SpcSipInfo{A: 0x1010000, UUID: SpcUUIDSipInfoAppx} |
| 34 | ) |
| 35 | |
| 36 | func (i *AppxDigest) Sign(ctx context.Context, cert *certloader.Certificate, params *authenticode.OpusParams) (patch *binpatch.PatchSet, priSig, catSig *pkcs9.TimestampedSignature, err error) { |
| 37 | if err := i.writeManifest(cert.Leaf); err != nil { |
| 38 | return nil, nil, nil, err |
| 39 | } |
| 40 | if err := i.writeBlockMap(); err != nil { |
| 41 | return nil, nil, nil, err |
| 42 | } |
| 43 | if err := i.writeContentTypes(); err != nil { |
| 44 | return nil, nil, nil, err |
| 45 | } |
| 46 | catSig, err = i.writeCodeIntegrity(ctx, cert, params) |
| 47 | if err != nil { |
| 48 | return nil, nil, nil, err |
| 49 | } |
| 50 | ts, err := i.writeSignature(ctx, cert, params) |
| 51 | if err != nil { |
| 52 | return nil, nil, nil, err |
| 53 | } |
| 54 | w := &i.patchBuf |
| 55 | if err := i.outz.WriteDirectory(w, w, true); err != nil { |
| 56 | return nil, nil, nil, err |
| 57 | } |
| 58 | patch = binpatch.New() |
| 59 | patch.Add(i.patchStart, i.patchLen, i.patchBuf.Bytes()) |
| 60 | return patch, ts, catSig, nil |
| 61 | } |
| 62 | |
| 63 | func (i *AppxDigest) addZipEntry(name string, contents []byte) error { |
| 64 | // Don't deflate the manifest because I can't figure out how to correctly |
| 65 | // calculate block sizes. The blocks for the rest of the files can be |
| 66 | // cribbed from the old blockmap. |
| 67 | deflate := !(name == appxManifest || name == bundleManifestFile) |
| 68 | // Don't use descriptors for the 3 files that signtool adds, it seems to |
| 69 | // aggrevate the generic verifier although the appx works fine. |
| 70 | useDesc := !(name == appxContentTypes || name == appxCodeIntegrity || name == appxSignature) |
| 71 | f, err := i.outz.NewFile(name, nil, contents, &i.patchBuf, i.mtime, deflate, useDesc) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | return i.blockMap.AddFile(f, i.axpc, nil) |
| 76 | } |
| 77 | |
| 78 | func (i *AppxDigest) writeManifest(leaf *x509.Certificate) error { |
| 79 | if i.manifest != nil { |
| 80 | i.manifest.SetPublisher(leaf) |
| 81 | manifest, err := i.manifest.Marshal() |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | return i.addZipEntry(appxManifest, manifest) |
| 86 | } else if i.bundle != nil { |
| 87 | i.bundle.SetPublisher(leaf) |
| 88 | manifest, err := i.bundle.Marshal() |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | return i.addZipEntry(bundleManifestFile, manifest) |
| 93 | } |
| 94 | return errors.New("manifest not found") |
| 95 | } |
| 96 | |
| 97 | func (i *AppxDigest) writeBlockMap() error { |
| 98 | blockmap, err := i.blockMap.Marshal() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if err := i.addZipEntry(appxBlockMap, blockmap); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | d := i.Hash.New() |
| 106 | d.Write(blockmap) |
| 107 | i.axbm = d.Sum(nil) |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (i *AppxDigest) writeContentTypes() error { |
| 112 | for _, f := range i.outz.File { |
| 113 | if f.Name != appxContentTypes { |
| 114 | i.contentTypes.Add(f.Name) |
| 115 | } |
| 116 | } |
| 117 | if len(i.peDigests) != 0 { |
| 118 | i.contentTypes.Add(appxCodeIntegrity) |
| 119 | } |
| 120 | i.contentTypes.Add(appxSignature) |
| 121 | ctypes, err := i.contentTypes.Marshal() |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | if err := i.addZipEntry(appxContentTypes, ctypes); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | d := i.Hash.New() |
| 129 | d.Write(ctypes) |
| 130 | i.axct = d.Sum(nil) |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func (i *AppxDigest) writeCodeIntegrity(ctx context.Context, cert *certloader.Certificate, params *authenticode.OpusParams) (*pkcs9.TimestampedSignature, error) { |
| 135 | if len(i.peDigests) == 0 { |
| 136 | return nil, nil |
| 137 | } |
| 138 | cat := authenticode.NewCatalog(i.Hash) |
| 139 | for _, d := range i.peDigests { |
| 140 | indirect, err := d.GetIndirect() |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | if err := cat.Add(indirect); err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | } |
| 148 | ts, err := cat.Sign(ctx, cert, params) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | catalog := ts.Raw |
| 153 | if err := i.addZipEntry(appxCodeIntegrity, catalog); err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | d := i.Hash.New() |
| 157 | d.Write(catalog) |
| 158 | i.axci = d.Sum(nil) |
| 159 | return ts, nil |
| 160 | } |
| 161 | |
| 162 | func (i *AppxDigest) writeSignature(ctx context.Context, cert *certloader.Certificate, params *authenticode.OpusParams) (*pkcs9.TimestampedSignature, error) { |
| 163 | axcd := i.Hash.New() |
| 164 | if err := i.outz.WriteDirectory(axcd, axcd, true); err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | digest := bytes.NewBuffer(make([]byte, 0, 4+5*(4+i.Hash.Size()))) |
| 168 | digest.WriteString("APPX") |
| 169 | digest.WriteString("AXPC") |
| 170 | digest.Write(i.axpc.Sum(nil)) |
| 171 | digest.WriteString("AXCD") |
| 172 | digest.Write(axcd.Sum(nil)) |
| 173 | digest.WriteString("AXCT") |
| 174 | digest.Write(i.axct) |
| 175 | digest.WriteString("AXBM") |
| 176 | digest.Write(i.axbm) |
| 177 | if len(i.axci) != 0 { |
| 178 | digest.WriteString("AXCI") |
| 179 | digest.Write(i.axci) |
| 180 | } |
| 181 | ts, err := authenticode.SignSip(ctx, digest.Bytes(), i.Hash, appxSipInfo, cert, params) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | pkcx := make([]byte, 4, 4+len(ts.Raw)) |
| 186 | copy(pkcx, "PKCX") |
| 187 | pkcx = append(pkcx, ts.Raw...) |
| 188 | if err := i.addZipEntry(appxSignature, pkcx); err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | return ts, nil |
| 192 | } |