| 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 authenticode |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "crypto" |
| 22 | "crypto/hmac" |
| 23 | "encoding/binary" |
| 24 | "errors" |
| 25 | "fmt" |
| 26 | "io" |
| 27 | "sort" |
| 28 | |
| 29 | "github.com/sassoftware/relic/v7/lib/comdoc" |
| 30 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 31 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 32 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 33 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 34 | ) |
| 35 | |
| 36 | type MSISignature struct { |
| 37 | pkcs9.TimestampedSignature |
| 38 | Indirect *SpcIndirectDataContentMsi |
| 39 | HashFunc crypto.Hash |
| 40 | OpusInfo *SpcSpOpusInfo |
| 41 | } |
| 42 | |
| 43 | // Extract and verify the signature of a MSI file. Does not check X509 chains. |
| 44 | func VerifyMSI(f io.ReaderAt, skipDigests bool) (*MSISignature, error) { |
| 45 | cdf, err := comdoc.ReadFile(f) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | var sig, exsig []byte |
| 50 | files, err := cdf.ListDir(nil) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | for _, item := range files { |
| 55 | name := item.Name() |
| 56 | if name == msiDigitalSignature { |
| 57 | r, err := cdf.ReadStream(item) |
| 58 | if err == nil { |
| 59 | sig, err = io.ReadAll(r) |
| 60 | } |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | } else if name == msiDigitalSignatureEx { |
| 65 | r, err := cdf.ReadStream(item) |
| 66 | if err == nil { |
| 67 | exsig, err = io.ReadAll(r) |
| 68 | } |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | if len(sig) == 0 { |
| 75 | return nil, sigerrors.NotSignedError{Type: "MSI"} |
| 76 | } |
| 77 | psd, err := pkcs7.Unmarshal(sig) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | if !psd.Content.ContentInfo.ContentType.Equal(OidSpcIndirectDataContent) { |
| 82 | return nil, errors.New("not an authenticode signature") |
| 83 | } |
| 84 | pksig, err := psd.Content.Verify(nil, false) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | ts, err := pkcs9.VerifyOptionalTimestamp(pksig) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | indirect := new(SpcIndirectDataContentMsi) |
| 93 | if err := psd.Content.ContentInfo.Unmarshal(indirect); err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | hash, err := x509tools.PkixDigestToHashE(indirect.MessageDigest.DigestAlgorithm) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | opus, err := GetOpusInfo(pksig.SignerInfo) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | msisig := &MSISignature{ |
| 105 | TimestampedSignature: ts, |
| 106 | Indirect: indirect, |
| 107 | HashFunc: hash, |
| 108 | OpusInfo: opus, |
| 109 | } |
| 110 | if !skipDigests { |
| 111 | imprint, prehash, err := DigestMSI(cdf, hash, exsig != nil) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | if exsig != nil && !hmac.Equal(prehash, exsig) { |
| 116 | return nil, fmt.Errorf("MSI extended digest mismatch: %x != %x", prehash, exsig) |
| 117 | } |
| 118 | if !hmac.Equal(imprint, indirect.MessageDigest.Digest) { |
| 119 | return nil, fmt.Errorf("MSI digest mismatch: %x != %x", imprint, indirect.MessageDigest.Digest) |
| 120 | } |
| 121 | } |
| 122 | return msisig, nil |
| 123 | } |
| 124 | |
| 125 | // Calculate the digest (imprint) of a MSI file. If extended is true then the |
| 126 | // MsiDigitalSignatureEx value is also hashed and returned. |
| 127 | func DigestMSI(cdf *comdoc.ComDoc, hash crypto.Hash, extended bool) (imprint, prehash []byte, err error) { |
| 128 | d := hash.New() |
| 129 | if extended { |
| 130 | prehash, err = PrehashMSI(cdf, hash) |
| 131 | if err != nil { |
| 132 | return nil, nil, err |
| 133 | } |
| 134 | d.Write(prehash) |
| 135 | } |
| 136 | if err := hashMsiDir(cdf, cdf.RootStorage(), d); err != nil { |
| 137 | return nil, nil, err |
| 138 | } |
| 139 | imprint = d.Sum(nil) |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | // Calculates the MsiDigitalSignatureEx blob for a MSI file |
| 144 | func PrehashMSI(cdf *comdoc.ComDoc, hash crypto.Hash) ([]byte, error) { |
| 145 | d2 := hash.New() |
| 146 | if err := prehashMsiDir(cdf, cdf.RootStorage(), d2); err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | return d2.Sum(nil), nil |
| 150 | } |
| 151 | |
| 152 | // Recursively hash a MSI directory (storage) |
| 153 | func hashMsiDir(cdf *comdoc.ComDoc, parent *comdoc.DirEnt, d io.Writer) error { |
| 154 | files, err := cdf.ListDir(parent) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | sortMsiFiles(files) |
| 159 | for _, item := range files { |
| 160 | name := item.Name() |
| 161 | if name == msiDigitalSignature || name == msiDigitalSignatureEx { |
| 162 | continue |
| 163 | } |
| 164 | switch item.Type { |
| 165 | case comdoc.DirStream: |
| 166 | r, err := cdf.ReadStream(item) |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | if _, err := io.Copy(d, r); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | case comdoc.DirStorage: |
| 174 | if err := hashMsiDir(cdf, item, d); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | _, _ = d.Write(parent.UID[:]) |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | // Recursively hash a MSI directory's extended metadata |
| 184 | func prehashMsiDir(cdf *comdoc.ComDoc, parent *comdoc.DirEnt, d io.Writer) error { |
| 185 | files, err := cdf.ListDir(parent) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | sortMsiFiles(files) |
| 190 | prehashMsiDirent(parent, d) |
| 191 | for _, item := range files { |
| 192 | name := item.Name() |
| 193 | if name == msiDigitalSignature || name == msiDigitalSignatureEx { |
| 194 | continue |
| 195 | } |
| 196 | switch item.Type { |
| 197 | case comdoc.DirStream: |
| 198 | prehashMsiDirent(item, d) |
| 199 | case comdoc.DirStorage: |
| 200 | if err := prehashMsiDir(cdf, item, d); err != nil { |
| 201 | return err |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | return nil |
| 206 | } |
| 207 | |
| 208 | // Hash a MSI stream's extended metadata |
| 209 | func prehashMsiDirent(item *comdoc.DirEnt, d io.Writer) { |
| 210 | buf := bytes.NewBuffer(make([]byte, 0, 128)) |
| 211 | _ = binary.Write(buf, binary.LittleEndian, item.RawDirEnt) |
| 212 | enc := buf.Bytes() |
| 213 | // Name |
| 214 | if item.Type != comdoc.DirRoot { |
| 215 | _, _ = d.Write(enc[:item.NameLength-2]) |
| 216 | } |
| 217 | // UID |
| 218 | if item.Type == comdoc.DirRoot || item.Type == comdoc.DirStorage { |
| 219 | _, _ = d.Write(item.UID[:]) |
| 220 | } |
| 221 | // Size |
| 222 | if item.Type == comdoc.DirStream { |
| 223 | _, _ = d.Write(enc[120:124]) |
| 224 | } |
| 225 | // flags |
| 226 | _, _ = d.Write(enc[96:100]) |
| 227 | // ctime, mtime |
| 228 | if item.Type != comdoc.DirRoot { |
| 229 | _, _ = d.Write(enc[100:116]) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Sort a list of MSI streams in the order needed for hashing |
| 234 | func sortMsiFiles(files []*comdoc.DirEnt) { |
| 235 | sort.Slice(files, func(i, j int) bool { |
| 236 | a, b := files[i], files[j] |
| 237 | n := a.NameLength |
| 238 | if b.NameLength < n { |
| 239 | n = b.NameLength |
| 240 | } |
| 241 | // do a comparison of the utf16 in its original LE form |
| 242 | for k := uint16(0); k < n; k++ { |
| 243 | x, y := a.NameRunes[k], b.NameRunes[k] |
| 244 | x1, y1 := x&0xff, y&0xff |
| 245 | if x1 != y1 { |
| 246 | return x1 < y1 |
| 247 | } |
| 248 | x2, y2 := x>>8, y>>8 |
| 249 | if x2 != y2 { |
| 250 | return x2 < y2 |
| 251 | } |
| 252 | } |
| 253 | return a.NameLength > b.NameLength // yes, greater than |
| 254 | }) |
| 255 | } |