...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package signappx
18
19 import (
20 "bytes"
21 "crypto/x509"
22 "encoding/xml"
23 "fmt"
24
25 "github.com/beevik/etree"
26
27 "github.com/sassoftware/relic/lib/x509tools"
28 )
29
30 type appxPackage struct {
31 XMLName xml.Name
32
33 Identity appxIdentity
34
35 DisplayName string `xml:"Properties>DisplayName"`
36 PublisherDisplayName string `xml:"Properties>PublisherDisplayName"`
37 Logo string `xml:"Properties>Logo"`
38
39 Etree *etree.Document `xml:"-"`
40 }
41
42 type appxIdentity struct {
43 Name string `xml:",attr"`
44 Publisher string `xml:",attr"`
45 Version string `xml:",attr"`
46 ProcessorArchitecture string `xml:",attr"`
47 }
48
49 func parseManifest(blob []byte) (*appxPackage, error) {
50 manifest := new(appxPackage)
51 if err := xml.Unmarshal(blob, manifest); err != nil {
52 return nil, err
53 }
54 manifest.Etree = etree.NewDocument()
55 if err := manifest.Etree.ReadFromBytes(blob); err != nil {
56 return nil, err
57 }
58 return manifest, nil
59 }
60
61 func checkManifest(files zipFiles, sig *AppxSignature) error {
62 blob, err := readZipFile(files[appxManifest])
63 if err != nil {
64 return fmt.Errorf("appx manifest: %s", err)
65 }
66 var manifest appxPackage
67 if err := xml.Unmarshal(blob, &manifest); err != nil {
68 return fmt.Errorf("appx manifest: %s", err)
69 }
70 sig.Name = manifest.Identity.Name
71 sig.DisplayName = manifest.DisplayName
72 sig.Version = manifest.Identity.Version
73 publisher := x509tools.FormatPkixName(sig.Signature.Certificate.RawSubject, x509tools.NameStyleMsOsco)
74 if manifest.Identity.Publisher != publisher {
75 return fmt.Errorf("appx manifest: publisher identity mismatch:\nexpected: %s\nactual: %s", publisher, manifest.Identity.Publisher)
76 }
77 return nil
78 }
79
80 func (m *appxPackage) SetPublisher(cert *x509.Certificate) {
81 subj := x509tools.FormatPkixName(cert.RawSubject, x509tools.NameStyleMsOsco)
82 m.Identity.Publisher = subj
83 el := m.Etree.FindElement("Package/Identity")
84 if el != nil {
85 el.CreateAttr("Publisher", subj)
86 }
87 }
88
89 func (m *appxPackage) Marshal() ([]byte, error) {
90 b, err := m.Etree.WriteToBytes()
91 if err != nil {
92 return nil, err
93 }
94 return bytes.Replace(b, []byte{'\n'}, []byte{'\r', '\n'}, -1), nil
95 }
96
View as plain text