forked from robstradling/authroot_parser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
authroot_parser.go
171 lines (146 loc) · 3.58 KB
/
authroot_parser.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
package main
import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"os"
"strings"
"time"
"github.com/TomOnTime/utfutil"
"go.mozilla.org/pkcs7"
)
const CERT_DIST_POINT = "http://www.download.windowsupdate.com/msdownload/update/v3/static/trustedr/en/"
type Sequence struct {
Data asn1.RawValue
}
type CTLEntryValue struct {
Data []byte
}
type CTLEntryAttribute struct {
Type asn1.ObjectIdentifier
Value CTLEntryValue `asn1:"set"`
}
type CTLEntry struct {
CertFingerprint []byte
Attributes []CTLEntryAttribute `asn1:"set"`
}
type CTL struct {
Signers []asn1.ObjectIdentifier
SequenceNumber *big.Int
EffectiveDate time.Time
DigestAlgorithm pkix.AlgorithmIdentifier
Entries []CTLEntry
}
func oidList(data []byte) string {
var oids []asn1.ObjectIdentifier
if _, err := asn1.Unmarshal(data, &oids); err != nil {
panic(err)
}
var s string
for _, oid := range oids {
s += fmt.Sprintf(" %s", oid.String())
}
return s
}
type PolicyQualifier struct {
OID asn1.ObjectIdentifier
Bits asn1.BitString
}
type CertPolicy struct {
OID asn1.ObjectIdentifier
Qualifier []PolicyQualifier
}
type CertPolicies struct {
Policies []CertPolicy
}
func policyList(data []byte) string {
// Wrap policy list in a SEQUENCE.
seq := Sequence{Data: asn1.RawValue{FullBytes: data}}
var der_pol []byte
var err error
if der_pol, err = asn1.Marshal(seq); err != nil {
panic(err)
}
var policies CertPolicies
if _, err = asn1.Unmarshal(der_pol, &policies); err != nil {
panic(err)
}
var s string
for _, pol := range policies.Policies {
if pol.OID.String() == "1.3.6.1.4.1.311.94.1.1" {
s += " EV Disabled"
} else {
s += " " + pol.OID.String()
}
}
return s
}
func msFiletime(data []byte) string {
switch len(data) {
case 8:
return fmt.Sprintf("%v", time.Date(1601, time.January, 1, 0, 0, int(binary.LittleEndian.Uint64(data)/10000000), 0, time.UTC))
case 0:
return fmt.Sprintf("Since forever")
default:
panic(fmt.Errorf("Unexpected length (%d)", len(data)))
}
}
func utf16to8(data []byte) string {
if bytes, err := ioutil.ReadAll(utfutil.BytesReader(data, utfutil.WINDOWS)); err != nil {
panic(err)
} else {
return string(bytes[0 : len(bytes)-1])
}
}
func main() {
// Read DER-encoded authroot PKCS#7 file.
var err error
var authroot_data []byte
if authroot_data, err = ioutil.ReadFile(os.Args[1]); err != nil {
panic(err)
}
// Parse the PKCS#7, whose Content is assumed to have type szOID_CTL (1.3.6.1.4.1.311.10.1).
var p7 *pkcs7.PKCS7
if p7, err = pkcs7.Parse(authroot_data); err != nil {
panic(err)
}
// Wrap p7.Content in a SEQUENCE.
seq := Sequence{Data: asn1.RawValue{FullBytes: p7.Content}}
var der_ctl []byte
if der_ctl, err = asn1.Marshal(seq); err != nil {
panic(err)
}
// Parse the CTL.
var ctl CTL
if _, err = asn1.Unmarshal(der_ctl, &ctl); err != nil {
panic(err)
}
for _, entry := range ctl.Entries {
url := CERT_DIST_POINT + hex.EncodeToString(entry.CertFingerprint) + ".crt"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
pem := base64.StdEncoding.EncodeToString(body)
pem = strings.ReplaceAll(pem, "\n", "")
pem = strings.ReplaceAll(pem, " ", "")
pem = strings.ReplaceAll(pem, "\t", "")
fmt.Println("-----BEGIN CERTIFICATE-----")
for start := 0; start < len(pem); start += 64 {
next := start + 64
if next > len(pem) {
next = len(pem)
}
fmt.Println(pem[start:next])
}
fmt.Println("-----END CERTIFICATE-----")
}
}