Skip to content

Commit ca2f4eb

Browse files
committed
Added the methods to CA and CRL
Added the methods to CA{}.GetCRL() that returns the x509 CRL as string and CA{}.GoCRL() that returns pkix.Certificatelist. It makes easier to use the CRL data. Small fix on CSR added.
1 parent b190184 commit ca2f4eb

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

ca.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
const (
2121
certExtension string = ".crt"
2222
csrExtension string = ".csr"
23+
crlExtension string = ".crl"
2324
)
2425

2526
// A Identity represents the Certificate Authority Identity Information
@@ -43,6 +44,7 @@ type CAData struct {
4344
PublicKey string
4445
CSR string
4546
Certificate string
47+
CRL string
4648
privateKey rsa.PrivateKey
4749
certificate *x509.Certificate
4850
publicKey rsa.PublicKey
@@ -77,6 +79,7 @@ func (c *CA) create(commonName string, id Identity) error {
7779
publicKeyString []byte
7880
csrString []byte
7981
certString []byte
82+
crlString []byte
8083
)
8184

8285
if id.Organization == "" || id.OrganizationalUnit == "" || id.Country == "" || id.Locality == "" || id.Province == "" {
@@ -125,6 +128,7 @@ func (c *CA) create(commonName string, id Identity) error {
125128

126129
caData.certificate = certificate
127130
caData.Certificate = string(certString)
131+
128132
crlBytes, err := cert.RevokeCertificate(c.CommonName, []pkix.RevokedCertificate{}, certificate, privKey)
129133
if err != nil {
130134
crl, err := x509.ParseCRL(crlBytes)
@@ -133,6 +137,12 @@ func (c *CA) create(commonName string, id Identity) error {
133137
}
134138
}
135139

140+
if crlString, err = storage.LoadFile(caDir + "/" + commonName + crlExtension); err != nil {
141+
crlString = []byte{}
142+
}
143+
144+
c.Data.CRL = string(crlString)
145+
136146
} else {
137147
csrBytes, err := cert.CreateCSR(commonName, commonName, id.Country, id.Province, id.Locality, id.Organization, id.OrganizationalUnit, id.EmailAddresses, id.DNSNames, privKey, storage.CreationTypeCA)
138148
if err != nil {
@@ -373,6 +383,8 @@ func (c *CA) loadCertificate(commonName string) (certificate Certificate, err er
373383
func (c *CA) revokeCertificate(certificate *x509.Certificate) error {
374384

375385
var revokedCerts []pkix.RevokedCertificate
386+
var caDir string = "/" + c.CommonName + "/ca"
387+
var crlString []byte
376388

377389
if c.Data.crl != nil {
378390
revokedCerts = c.Data.crl.TBSCertList.RevokedCertificates
@@ -396,5 +408,12 @@ func (c *CA) revokeCertificate(certificate *x509.Certificate) error {
396408
}
397409
c.Data.crl = crl
398410

411+
var crlFile string = caDir + "/" + c.CommonName + crlExtension
412+
if crlString, err = storage.LoadFile(crlFile); err != nil {
413+
crlString = []byte{}
414+
}
415+
416+
c.Data.CRL = string(crlString)
417+
399418
return nil
400419
}

goca.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package goca
1919
import (
2020
"crypto/rsa"
2121
"crypto/x509"
22+
"crypto/x509/pkix"
2223

2324
storage "github.com/kairoaraujo/goca/_storage"
2425
)
@@ -118,14 +119,24 @@ func (c *CA) GoCertificate() *x509.Certificate {
118119
return c.Data.certificate
119120
}
120121

122+
// GetCRL returns Certificate Revocation List as x509 CRL string
123+
func (c *CA) GetCRL() string {
124+
return c.Data.CRL
125+
}
126+
127+
// GoCRL returns Certificate Revocation List as Go bytes *pkix.CertificateList
128+
func (c *CA) GoCRL() *pkix.CertificateList {
129+
return c.Data.crl
130+
}
131+
121132
// IsIntermediate returns if the CA is Intermediate CA (true)
122133
func (c *CA) IsIntermediate() bool {
123134
if c.Data.CSR == "" {
124135
return false
125136

126-
} else {
127-
return true
128137
}
138+
139+
return true
129140
}
130141

131142
// ListCertificates returns all certificates in the CA

goca_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,7 @@ func TestFunctionalRevokeCertificate(t *testing.T) {
213213
}
214214
t.Logf("Test appending revoked certificates")
215215

216+
if RootCA.GetCRL() == "" {
217+
t.Error("CRL X509 file is empty!")
218+
}
216219
}

0 commit comments

Comments
 (0)