Skip to content

Commit 7d024cc

Browse files
author
Raal Goff
committed
change GenerateCertificateRevocationList to return DER, store DER in db instead of PEM, nicer PEM encoding of CRL, add Mock stubs
1 parent e8fdb70 commit 7d024cc

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Authority interface {
5050
GetRoots() ([]*x509.Certificate, error)
5151
GetFederation() ([]*x509.Certificate, error)
5252
Version() authority.Version
53-
GenerateCertificateRevocationList(force bool) (string, error)
53+
GenerateCertificateRevocationList(force bool) ([]byte, error)
5454
}
5555

5656
// TimeDuration is an alias of provisioner.TimeDuration

api/crl.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package api
22

3-
import "net/http"
3+
import (
4+
"encoding/pem"
5+
"net/http"
6+
)
47

5-
// CRL is an HTTP handler that returns the current CRL
8+
// CRL is an HTTP handler that returns the current CRL in PEM format
69
func (h *caHandler) CRL(w http.ResponseWriter, r *http.Request) {
7-
crl, err := h.Authority.GenerateCertificateRevocationList(false)
10+
crlBytes, err := h.Authority.GenerateCertificateRevocationList(false)
811

912
if err != nil {
1013
w.WriteHeader(500)
1114
return
1215
}
1316

17+
pemBytes := pem.EncodeToMemory(&pem.Block{
18+
Type: "X509 CRL",
19+
Bytes: crlBytes,
20+
})
21+
1422
w.WriteHeader(200)
15-
_, err = w.Write([]byte(crl))
23+
_, err = w.Write(pemBytes)
1624
}

authority/tls.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -514,24 +514,24 @@ func (a *Authority) revokeSSH(crt *ssh.Certificate, rci *db.RevokedCertificateIn
514514
// a new CRL on demand if it has expired (or a CRL does not already exist).
515515
//
516516
// force set to true will force regeneration of the CRL regardless of whether it has actually expired
517-
func (a *Authority) GenerateCertificateRevocationList(force bool) (string, error) {
517+
func (a *Authority) GenerateCertificateRevocationList(force bool) ([]byte, error) {
518518

519519
// check for an existing CRL in the database, and return that if its valid
520520
crlInfo, err := a.db.GetCRL()
521521

522522
if err != nil {
523-
return "", err
523+
return nil, err
524524
}
525525

526526
if !force && crlInfo != nil && crlInfo.ExpiresAt.After(time.Now().UTC()) {
527-
return crlInfo.PEM, nil
527+
return crlInfo.DER, nil
528528
}
529529

530530
// some CAS may not implement the CRLGenerator interface, so check before we proceed
531531
caCRLGenerator, ok := a.x509CAService.(casapi.CertificateAuthorityCRLGenerator)
532532

533533
if !ok {
534-
return "", errors.Errorf("CRL Generator not implemented")
534+
return nil, errors.Errorf("CRL Generator not implemented")
535535
}
536536

537537
revokedList, err := a.db.GetRevokedCertificates()
@@ -574,28 +574,24 @@ func (a *Authority) GenerateCertificateRevocationList(force bool) (string, error
574574

575575
certificateRevocationList, err := caCRLGenerator.CreateCertificateRevocationList(&revocationList)
576576
if err != nil {
577-
return "", err
577+
return nil, err
578578
}
579579

580-
// Quick and dirty PEM encoding
581-
// TODO: clean this up
582-
pemCRL := fmt.Sprintf("-----BEGIN X509 CRL-----\n%s\n-----END X509 CRL-----\n", base64.StdEncoding.EncodeToString(certificateRevocationList))
583-
584580
// Create a new db.CertificateRevocationListInfo, which stores the new Number we just generated, the
585581
// expiry time, and the byte-encoded CRL - then store it in the DB
586582
newCRLInfo := db.CertificateRevocationListInfo{
587583
Number: n,
588584
ExpiresAt: revocationList.NextUpdate,
589-
PEM: pemCRL,
585+
DER: certificateRevocationList,
590586
}
591587

592588
err = a.db.StoreCRL(&newCRLInfo)
593589
if err != nil {
594-
return "", err
590+
return nil, err
595591
}
596592

597593
// Finally, return our CRL PEM
598-
return pemCRL, nil
594+
return certificateRevocationList, nil
599595
}
600596

601597
// GetTLSCertificate creates a new leaf certificate to be used by the CA HTTPS server.

db/db.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ type RevokedCertificateInfo struct {
114114
ACME bool
115115
}
116116

117-
// CertificateRevocationListInfo contains a CRL in PEM and associated metadata to allow a decision on whether
118-
// to regenerate the CRL or not easier
117+
// CertificateRevocationListInfo contains a CRL in DER format and associated
118+
// metadata to allow a decision on whether to regenerate the CRL or not easier
119119
type CertificateRevocationListInfo struct {
120120
Number int64
121121
ExpiresAt time.Time
122-
PEM string
122+
DER []byte
123123
}
124124

125125
// IsRevoked returns whether or not a certificate with the given identifier
@@ -379,6 +379,18 @@ type MockAuthDB struct {
379379
MShutdown func() error
380380
}
381381

382+
func (m *MockAuthDB) GetRevokedCertificates() (*[]RevokedCertificateInfo, error) {
383+
panic("implement me")
384+
}
385+
386+
func (m *MockAuthDB) GetCRL() (*CertificateRevocationListInfo, error) {
387+
panic("implement me")
388+
}
389+
390+
func (m *MockAuthDB) StoreCRL(info *CertificateRevocationListInfo) error {
391+
panic("implement me")
392+
}
393+
382394
// IsRevoked mock.
383395
func (m *MockAuthDB) IsRevoked(sn string) (bool, error) {
384396
if m.MIsRevoked != nil {

0 commit comments

Comments
 (0)