/
mozilla.go
138 lines (126 loc) · 3.89 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
package mozilla
import (
"encoding/asn1"
"encoding/base64"
"encoding/json"
"errors"
"math/big"
"time"
"github.com/zzylydx/zcrypto/x509"
"github.com/zzylydx/zcrypto/x509/pkix"
)
// const oneCRLDistPoint := "https://firefox.settings.services.mozilla.com/v1/buckets/blocklists/collections/certificates/records"
// OneCRL - data structure for storing OneCRL data, used by methods below
type OneCRL struct {
IssuerLists map[string]*IssuerList
}
// 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 {
Schema time.Time
Details EntryDetails
Enabled bool
Issuer pkix.Name
SerialNumber *big.Int
ID string
LastModified time.Time
}
// RawEntry - structure of a raw oneCRL entry
type RawEntry struct {
Schema int `json:"schema"`
Details EntryDetails `json:"details"`
Enabled bool `json:"enabled"`
Issuer string `json:"issuerName"`
SerialNumber string `json:"serialNumber"`
ID string `json:"id"`
LastModified int `json:"last_modified"`
}
// 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"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (entry *Entry) UnmarshalJSON(b []byte) error {
aux := &RawEntry{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
serialNumberBytes, _ := base64.StdEncoding.DecodeString(aux.SerialNumber)
serialNumber := new(big.Int).SetBytes(serialNumberBytes)
schemaSeconds := int64(aux.Schema) / 1000
schema := time.Unix(schemaSeconds, 0)
lastModifiedSeconds := int64(aux.LastModified) / 1000
lastModified := time.Unix(lastModifiedSeconds, 0)
issuerBytes, err := base64.StdEncoding.DecodeString(aux.Issuer)
if err != nil {
return err
}
var issuerRDN pkix.RDNSequence
_, err = asn1.Unmarshal(issuerBytes, &issuerRDN)
if err != nil {
return err
}
var issuer pkix.Name
issuer.FillFromRDNSequence(&issuerRDN)
*entry = Entry{
Schema: schema,
Details: aux.Details,
Enabled: aux.Enabled,
Issuer: issuer,
SerialNumber: serialNumber,
ID: aux.ID,
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]
}
// 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{}
oneCRL.IssuerLists = map[string]*IssuerList{}
for i := range rawOneCRL.Data {
entry := &(rawOneCRL.Data[i])
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 {
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
}