Debian Codegraph @

lib/authenticode/msitar.go

raw symbols SCIP
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 "archive/tar"
21 "bytes"
22 "crypto"
23 "fmt"
24 "io"
25 "io/ioutil"
26
27 "github.com/sassoftware/relic/v7/lib/comdoc"
28 )
29
30 const (
31 msiTarExMeta = "__exmeta"
32 msiTarStorageUID = "__storage_uid"
33 )
34
35 // Convert MSI to a tar archive in a form that can be digested and signed as a
36 // stream
37 func MsiToTar(cdf *comdoc.ComDoc, w io.Writer) error {
38 tw := tar.NewWriter(w)
39 // First write the metadata that is needed for an extended signature
40 var buf bytes.Buffer
41 if err := prehashMsiDir(cdf, cdf.RootStorage(), &buf); err != nil {
42 return err
43 }
44 if err := tarAddFile(tw, msiTarExMeta, buf.Bytes()); err != nil {
45 return err
46 }
47 // Now all of the files in the same order they would get digested in
48 if err := msiToTarDir(cdf, tw, cdf.RootStorage(), ""); err != nil {
49 return err
50 }
51 return tw.Close()
52 }
53
54 // Digset a tarball produced by MsiToTar
55 func DigestMsiTar(r io.Reader, hash crypto.Hash, extended bool) ([]byte, error) {
56 tr := tar.NewReader(r)
57 d := hash.New()
58 for {
59 hdr, err := tr.Next()
60 if err == io.EOF {
61 break
62 } else if err != nil {
63 return nil, err
64 }
65 if hdr.Name == msiTarExMeta {
66 if !extended {
67 continue
68 }
69 exmeta, err := ioutil.ReadAll(tr)
70 if err != nil {
71 return nil, err
72 }
73 d2 := hash.New()
74 d2.Write(exmeta)
75 prehash := d2.Sum(nil)
76 d.Write(prehash)
77 } else if hdr.Name == msiDigitalSignature || hdr.Name == msiDigitalSignatureEx {
78 continue
79 }
80 if _, err := io.Copy(d, tr); err != nil {
81 return nil, err
82 }
83 }
84 return d.Sum(nil), nil
85 }
86
87 // Add a file with contents blob to an open tar.Writer
88 func tarAddFile(tw *tar.Writer, name string, contents []byte) error {
89 hdr := &tar.Header{Name: name, Mode: 0644, Size: int64(len(contents))}
90 if err := tw.WriteHeader(hdr); err != nil {
91 return err
92 }
93 _, err := tw.Write(contents)
94 return err
95 }
96
97 // Recursively copy streams from a MSI directory (storage) to a tar.Writer
98 func msiToTarDir(cdf *comdoc.ComDoc, tw *tar.Writer, parent *comdoc.DirEnt, path string) error {
99 files, err := cdf.ListDir(parent)
100 if err != nil {
101 if path == "" {
102 return fmt.Errorf("listing root storage: %w", err)
103 }
104 return fmt.Errorf("listing storage %q: %w", path, err)
105 }
106 sortMsiFiles(files)
107 for _, item := range files {
108 itemPath := path + msiDecodeName(item.Name())
109 switch item.Type {
110 case comdoc.DirStream:
111 r, err := cdf.ReadStream(item)
112 if err != nil {
113 return fmt.Errorf("reading MSI stream %q: %w", itemPath, err)
114 }
115 hdr := &tar.Header{
116 Name: itemPath,
117 Mode: 0644,
118 Size: int64(item.StreamSize),
119 }
120 if err := tw.WriteHeader(hdr); err != nil {
121 return err
122 }
123 if _, err := io.Copy(tw, r); err != nil {
124 return fmt.Errorf("transforming MSI stream %q: %w", itemPath, err)
125 }
126 case comdoc.DirStorage:
127 if err := msiToTarDir(cdf, tw, item, itemPath+"/"); err != nil {
128 return err
129 }
130 }
131 }
132 // The UID of each storage gets hashed after its contents so include that, too
133 return tarAddFile(tw, path+msiTarStorageUID, parent.UID[:])
134 }