forked from zmap/zcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crl.go
159 lines (139 loc) · 5.12 KB
/
crl.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
package crl
import (
"encoding/json"
"time"
"github.com/dmin12/zcrypto1.3/encoding/asn1"
"github.com/dmin12/zcrypto1.3/x509"
"github.com/dmin12/zcrypto1.3/x509/pkix"
)
// RevocationReasonCode - status codes that explain revocation reason see RFC 5280, Section 5.3.1
type RevocationReasonCode int
var reasonCodeNames map[RevocationReasonCode]string
func init() {
reasonCodeNames = make(map[RevocationReasonCode]string)
reasonCodeNames[0] = "unspecified"
reasonCodeNames[1] = "keyCompromise"
reasonCodeNames[2] = "cACompromise"
reasonCodeNames[3] = "affiliationChanged"
reasonCodeNames[4] = "superseded"
reasonCodeNames[5] = "cessationOfOperation"
reasonCodeNames[6] = "certificateHold"
// STATUS CODE 7 IS NOT USED
reasonCodeNames[8] = "removeFromCRL"
reasonCodeNames[9] = "privilegeWithdrawn"
reasonCodeNames[10] = "aACompromise"
}
// MarshalJSON implements the json.Marshler interface
func (code *RevocationReasonCode) MarshalJSON() ([]byte, error) {
aux := struct {
Value int `json:"value"`
Name string `json:"name"`
}{
Value: int(*code),
Name: code.String(),
}
return json.Marshal(&aux)
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (code *RevocationReasonCode) UnmarshalJSON(b []byte) error {
aux := struct {
Value int `json:"value"`
Name string `json:"name"`
}{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
*code = RevocationReasonCode(aux.Value)
return nil
}
func (code *RevocationReasonCode) String() string {
return reasonCodeNames[*code]
}
var (
crlNumberExtensionOID = asn1.ObjectIdentifier{2, 5, 29, 20}
revocationReasonExtensionOID = asn1.ObjectIdentifier{2, 5, 29, 21}
invalidityDateExtensionOID = asn1.ObjectIdentifier{2, 5, 29, 24}
)
type crlNumberExtension struct {
ID asn1.ObjectIdentifier
Critical bool `asn1:"optional"`
CRLNumber int
}
// TODO: handle additional CRL Extensions
// 2.5.29.21,cRLReason
// id-ce-cRLReasons OBJECT IDENTIFIER ::= { id-ce 21 }
// -- reasonCode ::= { CRLReasonCode }
//2.5.29.24,invalidityDate
// id-ce-invalidityDate OBJECT IDENTIFIER ::= { id-ce 24 }
// -- InvalidityDate ::= GeneralizedTime
// RevocationData - information on whether a certificate has been
// revoked by a specified CRL, and information on the CRL
type RevocationData struct {
CRLSignatureAlgorithm x509.SignatureAlgorithm
CRLSignatureValue []byte
Version int `asn1:"optional,default:0"`
Issuer pkix.Name
ThisUpdate time.Time
NextUpdate time.Time `asn1:"optional"`
CRLExtensions ListExtensionData
UnknownCRLExtensions []pkix.Extension `asn1:"tag:0,optional,explicit"`
UnknownCriticalCRLExtensions []pkix.Extension `asn1:"tag:0,optional,explicit"`
IsRevoked bool
RevocationTime time.Time
CertificateEntryExtensions RevokedCertExtensionData
RawCertificateEntryExtensions []pkix.Extension `asn1:"optional"`
}
// ListExtensionData - Data from optional, non-critical pkix.CertificateList extensions
type ListExtensionData struct {
CRLNumber int
AuthKeyID x509.SubjAuthKeyId `json:"authority_key_id,omitempty"`
}
// RevokedCertExtensionData - Data from optional, non-critical pkix.RevokedCertificate extensions
type RevokedCertExtensionData struct {
Reason *RevocationReasonCode
invalidityDate time.Time
}
func gatherListExtensionInfo(certList *pkix.CertificateList, ret *RevocationData) {
for _, extension := range certList.TBSCertList.Extensions {
if extension.Id.Equal(crlNumberExtensionOID) {
var ext crlNumberExtension
asn1.Unmarshal(extension.Value, &ext.CRLNumber)
ret.CRLExtensions.CRLNumber = ext.CRLNumber
} else if extension.Critical {
ret.UnknownCriticalCRLExtensions = append(ret.UnknownCriticalCRLExtensions, extension)
} else {
ret.UnknownCRLExtensions = append(ret.UnknownCRLExtensions, extension)
}
}
}
// CheckCRLForCert - parses through a given CRL and to see if a given certificate
// is present, and returns data on the revocation and CRL in general
func CheckCRLForCert(certList *pkix.CertificateList, cert *x509.Certificate, cache map[string]*pkix.RevokedCertificate) (*RevocationData, error) {
ret := &RevocationData{
CRLSignatureAlgorithm: x509.GetSignatureAlgorithmFromAI(certList.SignatureAlgorithm),
CRLSignatureValue: certList.SignatureValue.Bytes,
Version: certList.TBSCertList.Version,
ThisUpdate: certList.TBSCertList.ThisUpdate,
NextUpdate: certList.TBSCertList.NextUpdate,
IsRevoked: false,
}
ret.Issuer.FillFromRDNSequence(&certList.TBSCertList.Issuer)
gatherListExtensionInfo(certList, ret)
if cache != nil {
if val, ok := cache[cert.SerialNumber.String()]; ok {
ret.IsRevoked = true
ret.RevocationTime = val.RevocationTime
}
return ret, nil
}
// else no cache was given, must linear search through
revokedCerts := certList.TBSCertList.RevokedCertificates
for i := range revokedCerts {
if revokedCerts[i].SerialNumber.Cmp(cert.SerialNumber) == 0 {
ret.IsRevoked = true
ret.RevocationTime = revokedCerts[i].RevocationTime
break
}
}
return ret, nil
}