| 1 | package xar |
| 2 | |
| 3 | import ( |
| 4 | "crypto" |
| 5 | "crypto/hmac" |
| 6 | "crypto/sha1" |
| 7 | "crypto/sha256" |
| 8 | "crypto/sha512" |
| 9 | "encoding/hex" |
| 10 | "fmt" |
| 11 | "hash" |
| 12 | "io" |
| 13 | "io/ioutil" |
| 14 | "sort" |
| 15 | "strconv" |
| 16 | |
| 17 | "github.com/beevik/etree" |
| 18 | ber "github.com/go-asn1-ber/asn1-ber" |
| 19 | |
| 20 | "github.com/sassoftware/relic/v7/lib/fruit/csblob" |
| 21 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 22 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 23 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 24 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 25 | ) |
| 26 | |
| 27 | type Signature struct { |
| 28 | HashFunc crypto.Hash |
| 29 | Signature *pkcs9.TimestampedSignature |
| 30 | NotaryTicket []byte |
| 31 | } |
| 32 | |
| 33 | func (x *XAR) Verify(skipDigests bool) (*Signature, error) { |
| 34 | var tsig pkcs9.TimestampedSignature |
| 35 | if x.CMSSignature != nil { |
| 36 | // repack BER as DER |
| 37 | pkt, err := ber.DecodePacketErr(x.CMSSignature) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | der := pkt.Bytes() |
| 42 | psd, err := pkcs7.Unmarshal(der) |
| 43 | if err != nil { |
| 44 | return nil, fmt.Errorf("reading CMS signature: %w", err) |
| 45 | } |
| 46 | sig, err := psd.Content.Verify(x.TOCHash, false) |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("verifying CMS signature: %w", err) |
| 49 | } |
| 50 | tsig, err = pkcs9.VerifyOptionalTimestamp(sig) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | } else if x.ClassicSignature != nil { |
| 55 | pub := x.Certificates[0].PublicKey |
| 56 | if err := x509tools.Verify(pub, x.HashFunc, x.TOCHash, x.ClassicSignature); err != nil { |
| 57 | // try again with a hash of a hash, which seems to be found on some older packages |
| 58 | d := x.HashFunc.New() |
| 59 | d.Write(x.TOCHash) |
| 60 | err2 := x509tools.Verify(pub, x.HashFunc, d.Sum(nil), x.ClassicSignature) |
| 61 | if err2 != nil { |
| 62 | // use original error |
| 63 | return nil, fmt.Errorf("verifying RSA signature: %w", err) |
| 64 | } |
| 65 | } |
| 66 | tsig = pkcs9.TimestampedSignature{ |
| 67 | Signature: pkcs7.Signature{ |
| 68 | Certificate: x.Certificates[0], |
| 69 | Intermediates: x.Certificates[1:], |
| 70 | }, |
| 71 | } |
| 72 | } else { |
| 73 | return nil, sigerrors.NotSignedError{Type: "xar"} |
| 74 | } |
| 75 | if !skipDigests { |
| 76 | if err := x.checkFiles(); err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | } |
| 80 | // mark proprietary certificate extensions as handled so it doesn't fail the chain |
| 81 | for _, cert := range tsig.Intermediates { |
| 82 | csblob.MarkHandledExtensions(cert) |
| 83 | } |
| 84 | return &Signature{ |
| 85 | HashFunc: x.HashFunc, |
| 86 | Signature: &tsig, |
| 87 | NotaryTicket: x.NotaryTicket, |
| 88 | }, nil |
| 89 | } |
| 90 | |
| 91 | func (x *XAR) checkFiles() error { |
| 92 | // gather all files with checksums into a flat list and sort by offset |
| 93 | var dataFiles []*tocFile |
| 94 | gatherDataFiles(x.toc.Files, &dataFiles) |
| 95 | sort.Slice(dataFiles, func(i, j int) bool { return dataFiles[i].Offset < dataFiles[j].Offset }) |
| 96 | for _, f := range dataFiles { |
| 97 | if err := checkFile(x.heap, f); err != nil { |
| 98 | return fmt.Errorf("checksumming %q: %w", f.Name, err) |
| 99 | } |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | func checkFiles(toc *etree.Element, heap io.ReaderAt) error { |
| 105 | // gather all files with checksums into a flat list and sort by offset |
| 106 | var dataFiles []*tocFile |
| 107 | for _, ed := range toc.FindElements("//file/data") { |
| 108 | ef := ed.Parent() |
| 109 | ek := ed.SelectElement("archived-checksum") |
| 110 | if ek == nil { |
| 111 | continue |
| 112 | } |
| 113 | offset, _ := strconv.ParseInt(textOf(ed.SelectElement("offset")), 10, 64) |
| 114 | length, _ := strconv.ParseInt(textOf(ed.SelectElement("length")), 10, 64) |
| 115 | f := &tocFile{ |
| 116 | Name: textOf(ef.SelectElement("name")), |
| 117 | Offset: offset, |
| 118 | Length: length, |
| 119 | ArchivedChecksum: tocFileSum{ |
| 120 | Style: ek.SelectAttrValue("style", ""), |
| 121 | Digest: ek.Text(), |
| 122 | }, |
| 123 | } |
| 124 | dataFiles = append(dataFiles, f) |
| 125 | } |
| 126 | sort.Slice(dataFiles, func(i, j int) bool { return dataFiles[i].Offset < dataFiles[j].Offset }) |
| 127 | for _, f := range dataFiles { |
| 128 | if err := checkFile(heap, f); err != nil { |
| 129 | return fmt.Errorf("checksumming %q: %w", f.Name, err) |
| 130 | } |
| 131 | } |
| 132 | return nil |
| 133 | } |
| 134 | |
| 135 | func textOf(e *etree.Element) string { |
| 136 | if e == nil { |
| 137 | return "" |
| 138 | } |
| 139 | return e.Text() |
| 140 | } |
| 141 | |
| 142 | func checkFile(heap io.ReaderAt, f *tocFile) error { |
| 143 | var h hash.Hash |
| 144 | switch f.ArchivedChecksum.Style { |
| 145 | case "sha1": |
| 146 | h = sha1.New() |
| 147 | case "sha256": |
| 148 | h = sha256.New() |
| 149 | case "sha512": |
| 150 | h = sha512.New() |
| 151 | default: |
| 152 | return fmt.Errorf("unsupported hash type %s", f.ArchivedChecksum.Style) |
| 153 | } |
| 154 | expected, err := hex.DecodeString(f.ArchivedChecksum.Digest) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | r := io.NewSectionReader(heap, f.Offset, f.Length) |
| 159 | if n, err := io.Copy(h, r); err != nil { |
| 160 | return err |
| 161 | } else if n != f.Length { |
| 162 | return io.ErrUnexpectedEOF |
| 163 | } |
| 164 | calculated := h.Sum(nil) |
| 165 | if !hmac.Equal(expected, calculated) { |
| 166 | return fmt.Errorf("digest mismatch: expected %x but got %x", expected, calculated) |
| 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | func gatherDataFiles(dirFiles []*tocFile, dataFiles *[]*tocFile) { |
| 172 | for _, f := range dirFiles { |
| 173 | if f.Length != 0 { |
| 174 | *dataFiles = append(*dataFiles, f) |
| 175 | } else if len(f.Files) != 0 { |
| 176 | gatherDataFiles(f.Files, dataFiles) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | type streamReaderAt struct { |
| 182 | r io.Reader |
| 183 | pos int64 |
| 184 | } |
| 185 | |
| 186 | func (r *streamReaderAt) ReadAt(d []byte, p int64) (int, error) { |
| 187 | if p > r.pos { |
| 188 | if _, err := io.CopyN(ioutil.Discard, r.r, p-r.pos); err != nil { |
| 189 | return 0, err |
| 190 | } |
| 191 | r.pos = p |
| 192 | } else if p < r.pos { |
| 193 | return 0, fmt.Errorf("attempted to seek backwards: at %d, to %d", r.pos, p) |
| 194 | } |
| 195 | n, err := io.ReadFull(r.r, d) |
| 196 | r.pos += int64(n) |
| 197 | return n, err |
| 198 | } |