Skip to content

Commit 6593d76

Browse files
committed
Revert to pkix.CertificateList instead of x509.RevocationList
Allows easier backporting to releases reliant on go 1.18 or older
1 parent 5416aa3 commit 6593d76

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

pkg/router/crl/crl.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crl
33
import (
44
"bytes"
55
"crypto/x509"
6+
"crypto/x509/pkix"
67
"encoding/asn1"
78
"encoding/hex"
89
"encoding/pem"
@@ -258,7 +259,7 @@ func commitCACRLUpdate(stagingDirectory, caBundleFilename string, stagingCRLUpda
258259
return nil
259260
}
260261

261-
var existingCRLs map[string]*x509.RevocationList
262+
var existingCRLs map[string]*pkix.CertificateList
262263

263264
// writeCRLFile reads the CA bundle at caBundleFilename, and makes sure all CRLs specified in the CA bundle are written
264265
// into the crl file at newCRLFilename. If any of the specified CRLs are in existingCRLFilename and have not expired,
@@ -291,9 +292,13 @@ func writeCRLFile(caBundleFilename, existingCRLFilename, newCRLFilename string)
291292

292293
buf := &bytes.Buffer{}
293294
for subjectKeyId, crl := range crls {
295+
asn1Data, err := asn1.Marshal(*crl)
296+
if err != nil {
297+
return time.Time{}, false, fmt.Errorf("failed to encode ASN.1 for CRL for certificate key %s: %w", subjectKeyId, err)
298+
}
294299
block := &pem.Block{
295300
Type: "X509 CRL",
296-
Bytes: crl.Raw,
301+
Bytes: asn1Data,
297302
}
298303
if err := pem.Encode(buf, block); err != nil {
299304
return time.Time{}, false, fmt.Errorf("failed to encode PEM for CRL for certificate key %s: %w", subjectKeyId, err)
@@ -319,9 +324,9 @@ func writeCRLFile(caBundleFilename, existingCRLFilename, newCRLFilename string)
319324
// existingCRLs are no longer required
320325
//
321326
// Returns an error if CRL downloading or parsing fails.
322-
func downloadMissingCRLs(existingCRLs map[string]*x509.RevocationList, clientCAData []byte) (map[string]*x509.RevocationList, time.Time, bool, error) {
327+
func downloadMissingCRLs(existingCRLs map[string]*pkix.CertificateList, clientCAData []byte) (map[string]*pkix.CertificateList, time.Time, bool, error) {
323328
var nextCRLUpdate time.Time
324-
crls := make(map[string]*x509.RevocationList)
329+
crls := make(map[string]*pkix.CertificateList)
325330
updated := false
326331
now := time.Now()
327332
for len(clientCAData) > 0 {
@@ -343,12 +348,12 @@ func downloadMissingCRLs(existingCRLs map[string]*x509.RevocationList, clientCAD
343348
continue
344349
}
345350
if crl, ok := existingCRLs[subjectKeyId]; ok {
346-
if crl.NextUpdate.Before(now) {
347-
log.Info("certificate revocation list has expired", "subject key identifier", subjectKeyId, "next update", crl.NextUpdate.Format(time.RFC3339))
351+
if crl.TBSCertList.NextUpdate.Before(now) {
352+
log.Info("certificate revocation list has expired", "subject key identifier", subjectKeyId, "next update", crl.TBSCertList.NextUpdate.Format(time.RFC3339))
348353
} else {
349354
crls[subjectKeyId] = existingCRLs[subjectKeyId]
350-
if nextCRLUpdate.IsZero() || crl.NextUpdate.Before(nextCRLUpdate) {
351-
nextCRLUpdate = crl.NextUpdate
355+
if nextCRLUpdate.IsZero() || crl.TBSCertList.NextUpdate.Before(nextCRLUpdate) {
356+
nextCRLUpdate = crl.TBSCertList.NextUpdate
352357
}
353358
continue
354359
}
@@ -360,9 +365,9 @@ func downloadMissingCRLs(existingCRLs map[string]*x509.RevocationList, clientCAD
360365
return nil, time.Time{}, false, fmt.Errorf("failed to get certificate revocation list for certificate key %s: %w", subjectKeyId, err)
361366
} else {
362367
crls[subjectKeyId] = crl
363-
log.Info("new certificate revocation list", "subject key identifier", subjectKeyId, "next update", crl.NextUpdate.Format(time.RFC3339))
364-
if nextCRLUpdate.IsZero() || crl.NextUpdate.Before(nextCRLUpdate) {
365-
nextCRLUpdate = crl.NextUpdate
368+
log.Info("new certificate revocation list", "subject key identifier", subjectKeyId, "next update", crl.TBSCertList.NextUpdate.Format(time.RFC3339))
369+
if nextCRLUpdate.IsZero() || crl.TBSCertList.NextUpdate.Before(nextCRLUpdate) {
370+
nextCRLUpdate = crl.TBSCertList.NextUpdate
366371
}
367372
updated = true
368373
}
@@ -382,7 +387,7 @@ func downloadMissingCRLs(existingCRLs map[string]*x509.RevocationList, clientCAD
382387

383388
// getCRL gets a certificate revocation list using the provided distribution points and returns the certificate list.
384389
// Returns an error if the CRL could not be downloaded.
385-
func getCRL(distributionPoints []string, now time.Time) (*x509.RevocationList, error) {
390+
func getCRL(distributionPoints []string, now time.Time) (*pkix.CertificateList, error) {
386391
var errs []error
387392
for _, distributionPoint := range distributionPoints {
388393
// The distribution point is typically a URL with the "http" scheme. "https" is generally not used because the
@@ -399,8 +404,8 @@ func getCRL(distributionPoints []string, now time.Time) (*x509.RevocationList, e
399404
errs = append(errs, fmt.Errorf("error getting %q: %w", distributionPoint, err))
400405
continue
401406
}
402-
if crl.NextUpdate.Before(now) {
403-
log.Info("CRL expired. trying next distribution point", "nextUpdate", crl.NextUpdate.Format(time.RFC3339))
407+
if crl.TBSCertList.NextUpdate.Before(now) {
408+
log.Info("CRL expired. trying next distribution point", "nextUpdate", crl.TBSCertList.NextUpdate.Format(time.RFC3339))
404409
errs = append(errs, fmt.Errorf("retrieved expired CRL from %s", distributionPoint))
405410
continue
406411
}
@@ -415,7 +420,7 @@ func getCRL(distributionPoints []string, now time.Time) (*x509.RevocationList, e
415420

416421
// getHTTPCRL gets a certificate revocation list using the provided HTTP URL. Returns an error if the CRL could not be
417422
// downloaded, or if parsing the CRL fails.
418-
func getHTTPCRL(url string) (*x509.RevocationList, error) {
423+
func getHTTPCRL(url string) (*pkix.CertificateList, error) {
419424
resp, err := http.Get(url)
420425
if err != nil {
421426
return nil, fmt.Errorf("http.Get failed: %w", err)
@@ -440,7 +445,7 @@ func getHTTPCRL(url string) (*x509.RevocationList, error) {
440445
return nil, fmt.Errorf("error parsing response: file is not CRL type")
441446
}
442447
}
443-
crl, err := x509.ParseRevocationList(crlBytes)
448+
crl, err := x509.ParseCRL(crlBytes)
444449
if err != nil {
445450
return nil, fmt.Errorf("error parsing response: %w", err)
446451
}

0 commit comments

Comments
 (0)