Skip to content

Commit 3d99a7e

Browse files
authored
Merge pull request #4 from kairoaraujo/fix_bug#3
Fix issue #3: CRL and crl is empty when CA loaded
2 parents 26efa02 + ad15ac3 commit 3d99a7e

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

ca.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ func (c *CA) loadCA(commonName string) error {
168168
caData := CAData{}
169169

170170
var (
171-
caDir string = "/" + commonName + "/ca"
172-
//caCertsDir string = folderPath + "/certs"
171+
caDir string = "/" + commonName + "/ca"
173172
keyString []byte
174173
publicKeyString []byte
175174
csrString []byte
176175
certString []byte
176+
crlString []byte
177177
loadErr error
178178
)
179179

@@ -223,6 +223,16 @@ func (c *CA) loadCA(commonName string) error {
223223
caData.certificate = cert
224224
}
225225

226+
var crlFile string = caDir + "/" + c.CommonName + crlExtension
227+
if crlString, loadErr = storage.LoadFile(crlFile); loadErr == nil {
228+
crl, err := cert.LoadCRL(crlString)
229+
if err != nil {
230+
return err
231+
}
232+
caData.CRL = string(crlString)
233+
caData.crl = crl
234+
}
235+
226236
c.Data = caData
227237

228238
return nil

cert/cert.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ func LoadCSR(csrString []byte) (*x509.CertificateRequest, error) {
125125
return csr, nil
126126
}
127127

128+
// LoadCRL loads a Certificate Revocation List from a read file.
129+
//
130+
// Using ioutil.ReadFile() satisfyies the read file.
131+
func LoadCRL(crlString []byte) (*pkix.CertificateList, error) {
132+
block, _ := pem.Decode([]byte(string(crlString)))
133+
crl, _ := x509.ParseCRL(block.Bytes)
134+
135+
return crl, nil
136+
}
137+
128138
// CreateRootCert creates a Root CA Certificate (self signed)
129139
func CreateRootCert(CACommonName, commonName, country, province, locality, organization, organizationalUnit, emailAddresses string, valid int, dnsNames []string, priv *rsa.PrivateKey, pub *rsa.PublicKey, creationType storage.CreationType) (cert []byte, err error) {
130140

goca.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import (
2626

2727
// CA represents the basic CA data
2828
type CA struct {
29-
CommonName string // Certificate Authority Common Name
30-
Identity Identity // Certificate Authority Identity (Identity{})
31-
Data CAData // Certificate Authority Data (CAData{})
29+
CommonName string // Certificate Authority Common Name
30+
Data CAData // Certificate Authority Data (CAData{})
3231
}
3332

3433
// Certificate represents a Certificate data

goca_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestFunctionalRootCACreation(t *testing.T) {
4949
func TestFunctionalIntermediateCACration(t *testing.T) {
5050
os.Setenv("CAPATH", CaTestFolder)
5151

52-
rootCAIdentity := Identity{
52+
intermediateCAIdentity := Identity{
5353
Organization: "Intermediate CA Company Inc.",
5454
OrganizationalUnit: "Intermediate Certificates Management",
5555
Country: "NL",
@@ -58,7 +58,7 @@ func TestFunctionalIntermediateCACration(t *testing.T) {
5858
Intermediate: true,
5959
}
6060

61-
IntermediateCA, err := New("go-itermediate.ca", rootCAIdentity)
61+
IntermediateCA, err := New("go-itermediate.ca", intermediateCAIdentity)
6262
if err != nil {
6363
t.Log(err)
6464
t.Errorf("Failing to create the CA")
@@ -93,6 +93,10 @@ func TestFunctionalRootCASignsIntermediateCA(t *testing.T) {
9393
t.Errorf("Failed to load Root CA")
9494
}
9595

96+
if RootCA.GetCRL() == "" {
97+
t.Error("Empty CRL")
98+
}
99+
96100
t.Log(RootCA.GoCertificate().DNSNames)
97101

98102
if RootCA.IsIntermediate() {
@@ -168,7 +172,11 @@ func TestFunctionalRootCALoadCertificates(t *testing.T) {
168172
t.Errorf("Failed to load Root CA")
169173
}
170174

171-
intranetCert, _ := RootCA.LoadCertificate("intranet.go-root.ca")
175+
intranetCert, err := RootCA.LoadCertificate("intranet.go-root.ca")
176+
if err != nil {
177+
fmt.Println(err)
178+
t.Log(err)
179+
}
172180

173181
if intranetCert.GetCACertificate() != "" {
174182
t.Log("Failed to load intranet")
@@ -187,8 +195,8 @@ func TestFunctionalRevokeCertificate(t *testing.T) {
187195
RootCA, _ := Load("go-root.ca")
188196
intermediateCert, _ := RootCA.LoadCertificate("go-itermediate.ca")
189197

190-
if RootCA.Data.crl != nil {
191-
t.Error("CRL is not nil")
198+
if RootCA.Data.crl == nil {
199+
t.Error("CRL is nil")
192200
}
193201

194202
err := RootCA.RevokeCertificate("go-itermediate.ca")

0 commit comments

Comments
 (0)