forked from zmap/zcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mozilla.go
284 lines (253 loc) · 7.69 KB
/
mozilla.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package mozilla
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"time"
"github.com/dmin12/zcrypto1.3/encoding/asn1"
"github.com/dmin12/zcrypto1.3/x509"
"github.com/dmin12/zcrypto1.3/x509/pkix"
)
// Provider specifies OneCRL provider interface
type Provider interface {
FetchAndParse() (*OneCRL, error)
}
const (
// KintoRequestURL specifies a pre-populated URL where to send request
KintoRequestURL = "https://settings.prod.mozaws.net/v1/buckets/security-state-staging/collections/onecrl/records"
// OneCRLDistPoint specifies a pre-populated URL where to send request
OneCRLDistPoint = "https://firefox.settings.services.mozilla.com/v1/buckets/blocklists/collections/certificates/records"
)
// defaultProvider provides default Provider
type defaultProvider struct {
requestURL string
}
// NewProvider returns default Provider
func NewProvider(requestURL string) Provider {
return &defaultProvider{
requestURL: requestURL,
}
}
// FetchAndParse - fetch from distribution point, parse to OneCRL struct as defined above
func FetchAndParse() (*OneCRL, error) {
return NewProvider(OneCRLDistPoint).FetchAndParse()
}
// OneCRL - data structure for storing OneCRL data, used by methods below
type OneCRL struct {
IssuerLists map[string]*IssuerList
// Blocked provides a list of revoked entries by Subject and PubKeyHash
Blocked []*SubjectAndPublicKey
}
// IssuerList - list of Entry for a given issuer
type IssuerList struct {
Issuer *pkix.Name
Entries []*Entry
}
// Entry - entry for a single certificate
type Entry struct {
ID string
Schema time.Time
Details EntryDetails
Enabled bool
Issuer *pkix.Name
SerialNumber *big.Int
SubjectAndPublicKey *SubjectAndPublicKey
LastModified time.Time
}
// SubjectAndPublicKey specifies a revocation entry by Subject and PubKeyHash
type SubjectAndPublicKey struct {
RawSubject []byte
Subject *pkix.Name
PubKeyHash []byte
}
// EntryDetails - revocation details for a single entry
type EntryDetails struct {
Bug string `json:"bug,omitempty"`
Who string `json:"who,omitempty"`
Why string `json:"why,omitempty"`
Name string `json:"name,omitempty"`
Created *time.Time `json:"created,omitempty"`
}
type record struct {
ID string `json:"id,omitempty"`
IssuerName string `json:"issuerName,omitempty"`
SerialNumber string `json:"serialNumber,omitempty"`
Subject string `json:"subject,omitempty"`
PubKeyHash string `json:"pubKeyHash,omitempty"`
Enabled bool `json:"enabled"`
Schema int `json:"schema"`
LastModified int `json:"last_modified"`
Details struct {
Who string `json:"who"`
Created string `json:"created"`
Bug string `json:"bug"`
Name string `json:"name"`
Why string `json:"why"`
} `json:"details"`
}
func decodePkixName(name string) (*pkix.Name, []byte, error) {
issuerBytes, err := base64.StdEncoding.DecodeString(name)
if err != nil {
return nil, nil, err
}
var issuerRDN pkix.RDNSequence
_, err = asn1.Unmarshal(issuerBytes, &issuerRDN)
if err != nil {
return nil, nil, err
}
iss := new(pkix.Name)
iss.FillFromRDNSequence(&issuerRDN)
return iss, issuerBytes, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (entry *Entry) UnmarshalJSON(b []byte) error {
aux := &record{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
schemaSeconds := int64(aux.Schema) / 1000
schema := time.Unix(schemaSeconds, 0)
lastModifiedSeconds := int64(aux.LastModified) / 1000
lastModified := time.Unix(lastModifiedSeconds, 0)
var createdAt *time.Time
if aux.Details.Created != "" {
var t time.Time
if err := t.UnmarshalJSON([]byte(aux.Details.Created)); err == nil {
createdAt = &t
}
}
var err error
var subjectAndPublicKey *SubjectAndPublicKey
var issuer *pkix.Name
var serialNumber *big.Int
if aux.Subject != "" && aux.PubKeyHash != "" {
subj, rawSubj, err := decodePkixName(aux.Subject)
if err != nil {
return fmt.Errorf("failed to unbase64 Subject: %v", err)
}
rawPubKey, err := base64.StdEncoding.DecodeString(aux.PubKeyHash)
if err != nil {
return fmt.Errorf("failed to unbase64 Subject: %v", err)
}
subjectAndPublicKey = &SubjectAndPublicKey{
Subject: subj,
RawSubject: rawSubj,
PubKeyHash: rawPubKey,
}
} else {
serialNumberBytes, _ := base64.StdEncoding.DecodeString(aux.SerialNumber)
serialNumber = new(big.Int).SetBytes(serialNumberBytes)
issuer, _, err = decodePkixName(aux.IssuerName)
if err != nil {
return fmt.Errorf("failed to unbase64 IssuerName: %v", err)
}
}
*entry = Entry{
ID: aux.ID,
Schema: schema,
Details: EntryDetails{
Created: createdAt,
Who: aux.Details.Who,
Bug: aux.Details.Bug,
Name: aux.Details.Name,
Why: aux.Details.Why,
},
Enabled: aux.Enabled,
Issuer: issuer,
SerialNumber: serialNumber,
SubjectAndPublicKey: subjectAndPublicKey,
LastModified: lastModified,
}
return nil
}
// FindIssuer - given an issuer pkix.name, find its corresponding IssuerList
func (c *OneCRL) FindIssuer(issuer *pkix.Name) *IssuerList {
issuerStr := issuer.String()
return c.IssuerLists[issuerStr]
}
// FetchAndParse - fetch from distribution point, parse to OneCRL struct as defined above
func (p *defaultProvider) FetchAndParse() (*OneCRL, error) {
raw, err := fetch(p.requestURL)
if err != nil {
return nil, err
}
return Parse(raw)
}
func fetch(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Failed to get current OneCRL: %v", err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("Failed to read OneCRL response: %v", err)
}
return bodyBytes, nil
}
// Parse - given raw bytes of OneCRL, parse and create OneCRL Object
func Parse(raw []byte) (*OneCRL, error) {
rawOneCRL := struct {
Data []Entry `json:"data"`
}{}
if err := json.Unmarshal(raw, &rawOneCRL); err != nil {
return nil, errors.New("Could not parse OneCRL: " + err.Error())
}
oneCRL := &OneCRL{
IssuerLists: make(map[string]*IssuerList),
Blocked: make([]*SubjectAndPublicKey, 0),
}
for i := range rawOneCRL.Data {
entry := &(rawOneCRL.Data[i])
if entry.SubjectAndPublicKey != nil {
oneCRL.Blocked = append(oneCRL.Blocked, entry.SubjectAndPublicKey)
continue
}
issuerList := oneCRL.FindIssuer(entry.Issuer)
if issuerList != nil { // if list already exists for this issuer, append
issuerList.Entries = append(issuerList.Entries, entry)
} else { // create new list for this issuer
newList := &IssuerList{
Issuer: entry.Issuer,
}
newList.Entries = append(newList.Entries, entry)
oneCRL.IssuerLists[entry.Issuer.String()] = newList
}
}
return oneCRL, nil
}
// Check - Given a parsed OneCRL, check if a given cert is present
func (c *OneCRL) Check(cert *x509.Certificate) *Entry {
// check for BlockedSPKIs first
for _, blocked := range c.Blocked {
if bytes.Equal(blocked.RawSubject, cert.RawSubject) {
pubKeyData, _ := x509.MarshalPKIXPublicKey(cert.PublicKey)
hash := sha256.Sum256(pubKeyData)
if bytes.Equal(blocked.PubKeyHash, hash[:]) {
return &Entry{
SubjectAndPublicKey: &SubjectAndPublicKey{
RawSubject: cert.RawSubject,
Subject: &cert.Subject,
PubKeyHash: hash[:],
},
}
}
}
}
issuersRevokedCerts := c.FindIssuer(&cert.Issuer)
if issuersRevokedCerts == nil { // no entries for this issuer
return nil
}
for _, entry := range issuersRevokedCerts.Entries {
if entry.SerialNumber.Cmp(cert.SerialNumber) == 0 {
return entry
} // cert not found if for loop completes
}
return nil
}