| 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 signxap |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/hmac" |
| 22 | "encoding/binary" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "io" |
| 26 | |
| 27 | "github.com/sassoftware/relic/v7/lib/authenticode" |
| 28 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 29 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 30 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 31 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 32 | ) |
| 33 | |
| 34 | type XapSignature struct { |
| 35 | pkcs9.TimestampedSignature |
| 36 | Hash crypto.Hash |
| 37 | OpusInfo *authenticode.SpcSpOpusInfo |
| 38 | } |
| 39 | |
| 40 | func Verify(r io.ReaderAt, size int64, skipDigests bool) (*XapSignature, error) { |
| 41 | var tr xapTrailer |
| 42 | if err := binary.Read(io.NewSectionReader(r, size-10, 10), binary.LittleEndian, &tr); err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | if tr.Magic != trailerMagic { |
| 46 | var zipMagic uint32 |
| 47 | if err := binary.Read(io.NewSectionReader(r, size-22, 4), binary.LittleEndian, &zipMagic); err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if zipMagic == 0x06054b50 { |
| 51 | return nil, sigerrors.NotSignedError{Type: "XAP"} |
| 52 | } |
| 53 | return nil, errors.New("invalid xap file") |
| 54 | } |
| 55 | size -= int64(tr.TrailerSize) + 10 |
| 56 | var hdr xapHeader |
| 57 | if err := binary.Read(io.NewSectionReader(r, size, 8), binary.LittleEndian, &hdr); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | if hdr.SignatureSize != tr.TrailerSize-8 { |
| 61 | return nil, errors.New("invalid xap file") |
| 62 | } |
| 63 | blob := make([]byte, hdr.SignatureSize) |
| 64 | if n, err := r.ReadAt(blob, size+8); err != nil { |
| 65 | return nil, err |
| 66 | } else if n < len(blob) { |
| 67 | return nil, io.ErrUnexpectedEOF |
| 68 | } |
| 69 | psd, err := pkcs7.Unmarshal(blob) |
| 70 | if err != nil { |
| 71 | return nil, fmt.Errorf("invalid signature: %w", err) |
| 72 | } |
| 73 | if !psd.Content.ContentInfo.ContentType.Equal(authenticode.OidSpcIndirectDataContent) { |
| 74 | return nil, fmt.Errorf("invalid signature: %s", "not an authenticode signature") |
| 75 | } |
| 76 | pksig, err := psd.Content.Verify(nil, false) |
| 77 | if err != nil { |
| 78 | return nil, fmt.Errorf("invalid signature: %w", err) |
| 79 | } |
| 80 | ts, err := pkcs9.VerifyOptionalTimestamp(pksig) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | indirect := new(authenticode.SpcIndirectDataContentMsi) |
| 85 | if err := psd.Content.ContentInfo.Unmarshal(indirect); err != nil { |
| 86 | return nil, fmt.Errorf("invalid signature: %w", err) |
| 87 | } |
| 88 | hash, err := x509tools.PkixDigestToHashE(indirect.MessageDigest.DigestAlgorithm) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | opus, err := authenticode.GetOpusInfo(pksig.SignerInfo) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | if !skipDigests { |
| 97 | d := hash.New() |
| 98 | if _, err := io.Copy(d, io.NewSectionReader(r, 0, size)); err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | calc := d.Sum(nil) |
| 102 | expected := indirect.MessageDigest.Digest |
| 103 | if !hmac.Equal(calc, expected) { |
| 104 | return nil, fmt.Errorf("digest mismatch: calculated %x != found %x", calc, expected) |
| 105 | } |
| 106 | } |
| 107 | return &XapSignature{ |
| 108 | TimestampedSignature: ts, |
| 109 | Hash: hash, |
| 110 | OpusInfo: opus, |
| 111 | }, nil |
| 112 | } |