-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (159 loc) · 3.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
var (
plistModDateFormat = "2006-01-02T15:04:05Z"
nonContentRegex = regexp.MustCompile(`[^a-zA-Z0-9\+\/=]*`)
whitespaceReplacer = strings.NewReplacer("\t", "", "\n", "", " ", "", "\r", "")
flagDirectory = flag.String("directory", "./examples/", "Directory of files to parse")
)
type plist struct {
Dict []*Dict `xml:"dict,omitempty"`
}
type Dict struct {
Array *Array `xml:"array,omitempty"`
Data []*Data `xml:"data,omitempty"`
Date *Date `xml:"date,omitempty"`
Dict []*Dict `xml:"dict,omitempty"`
Integer []*Integer `xml:"integer,omitempty"`
Key []*Key `xml:"key,omitempty"`
String *String `xml:"string,omitempty"`
}
type Key struct {
Text string `xml:",chardata"`
}
type Data struct {
Text string `xml:",chardata"`
}
type Date struct {
Text string `xml:",chardata"`
}
type Array struct {
Dict []*Dict `xml:"dict,omitempty"`
}
type Integer struct {
Text string `xml:",chardata"`
}
type String struct {
Text string `xml:",chardata"`
}
func main() {
fds, err := ioutil.ReadDir(*flagDirectory)
if err != nil {
panic(err)
}
for i := range fds {
parse(filepath.Join(*flagDirectory, fds[i].Name()))
}
}
type certTrust struct {
hash string
issuer *pkix.RDNSequence
trustSettings map[string]string
}
func (c *certTrust) String() string {
return fmt.Sprintf(`hash=%v issuer=%q trustSettings=%#v`, c.hash[:8], c.issuer, c.trustSettings)
}
func parse(where string) {
fmt.Printf("parsing %s\n", where)
bs, err := ioutil.ReadFile(where)
if err != nil {
panic(err)
}
plist := plist{}
err = xml.Unmarshal(bs, &plist)
if err != nil {
panic(err)
}
var items []*certTrust
for i := range plist.Dict {
// <key>02FAF3E291435468607857694DF5E45B68851868</key>
// <dict>
// ...
// </dict>
hashes := plist.Dict[i].Dict[0].Key
for j := range plist.Dict[i].Dict {
for k := range plist.Dict[i].Dict[j].Dict {
dict := plist.Dict[i].Dict[j].Dict[k]
item := &certTrust{
hash: hashes[j].Text,
trustSettings: make(map[string]string, 0),
}
items = append(items, item)
for l := range dict.Key {
switch dict.Key[l].Text {
case "issuerName":
issuer, err := parseIssuerName(dict.Data[0].Text)
if err != nil {
panic(err)
}
item.issuer = issuer
case "trustSettings":
err := parseTrustSettings(item, dict)
if err != nil {
panic(err)
}
}
}
fmt.Printf("%s\n\n", item.String())
}
}
}
fmt.Printf("found %d items\n", len(items))
}
// <key>issuerName</key>
// <data>
// MG8xCzAJBgNVBAYTAlNFMRQwEgYDVQQKEwtBZGRUcnVzdCBBQjEm
// MCQGA1UECxMdQWRkVHJ1c3QgRXh0ZXJuYWwgVFRQIE5ldHdvcmsx
// IjAgBgNVBAMTGUFkZFRydXN0IEV4dGVybmFsIENBIFJvb3Q=
// </data>
func parseIssuerName(value string) (*pkix.RDNSequence, error) {
raw := whitespaceReplacer.Replace(nonContentRegex.ReplaceAllString(value, ""))
data, _ := base64.StdEncoding.DecodeString(raw)
// The issuerName's <data></data> block is only under asn1 encoding for the
// issuerName field from 4.1.2.4 (https://tools.ietf.org/rfc/rfc5280)
name := pkix.Name{}
var issuer pkix.RDNSequence
_, err := asn1.Unmarshal(data, &issuer)
if err == nil {
name.FillFromRDNSequence(&issuer)
}
return &issuer, err
}
// <key>trustSettings</key>
// <array>
// <dict>
// <key>kSecTrustSettingsResult</key>
// <integer>4</integer>
// </dict>
// </array>
func parseTrustSettings(item *certTrust, dict *Dict) error {
for l := range dict.Array.Dict {
for m := range dict.Array.Dict[l].Key {
key := dict.Array.Dict[l].Key[m].Text
switch key {
case "kSecTrustSettingsResult":
if len(dict.Array.Dict) >= l+1 {
value := dict.Array.Dict[l].Integer[0].Text
item.trustSettings[key] = value
}
case "kSecTrustSettingsPolicyName":
if len(dict.Array.Dict) >= l+1 {
value := dict.Array.Dict[l].String.Text
item.trustSettings[key] = value
}
}
}
}
return nil
}