Skip to content

Commit 493594d

Browse files
authored
Revert TLS CA rotation (#1008)
Also, remove authority/subject key id generation as that is done by crypto/x509 library since go1.15.
1 parent fe293da commit 493594d

File tree

1 file changed

+8
-49
lines changed

1 file changed

+8
-49
lines changed

pkg/util/certificates.go

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package util
33
import (
44
cryptorand "crypto/rand"
55
"crypto/rsa"
6-
"crypto/sha1"
76
"crypto/x509"
87
"crypto/x509/pkix"
9-
"encoding/asn1"
108
"encoding/json"
119
"encoding/pem"
1210
"errors"
@@ -15,7 +13,6 @@ import (
1513
"math/big"
1614
"net"
1715
"reflect"
18-
"slices"
1916
"strings"
2017
"time"
2118

@@ -79,10 +76,6 @@ type AltNames struct {
7976
IPs []net.IP
8077
}
8178

82-
type authorityKeyId struct {
83-
KeyIdentifier []byte `asn1:"optional,tag:0"`
84-
}
85-
8679
func (ca Bundle) Sign(config Config) (*Bundle, error) {
8780
if !ca.Certificate.IsCA {
8881
return nil, errors.New("You can't use this certificate for signing. It's not a CA...")
@@ -102,17 +95,6 @@ func (ca Bundle) Sign(config Config) (*Bundle, error) {
10295
notBefore = ca.Certificate.NotBefore
10396
}
10497

105-
var authorityKeyIdent []byte
106-
if ca.Certificate.SubjectKeyId != nil {
107-
var err error
108-
authorityKeyIdent, err = asn1.Marshal(authorityKeyId{
109-
KeyIdentifier: ca.Certificate.SubjectKeyId,
110-
})
111-
if err != nil {
112-
return nil, fmt.Errorf("Failed to marshal authority key id: %s", err)
113-
}
114-
}
115-
11698
certTmpl := x509.Certificate{
11799
Subject: pkix.Name{
118100
CommonName: config.Sign,
@@ -121,14 +103,13 @@ func (ca Bundle) Sign(config Config) (*Bundle, error) {
121103
Province: config.Province,
122104
Locality: config.Locality,
123105
},
124-
DNSNames: config.AltNames.DNSNames,
125-
IPAddresses: config.AltNames.IPs,
126-
SerialNumber: serial,
127-
NotBefore: notBefore,
128-
NotAfter: time.Now().Add(config.ValidFor).UTC(),
129-
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
130-
ExtKeyUsage: config.Usages,
131-
AuthorityKeyId: authorityKeyIdent,
106+
DNSNames: config.AltNames.DNSNames,
107+
IPAddresses: config.AltNames.IPs,
108+
SerialNumber: serial,
109+
NotBefore: notBefore,
110+
NotAfter: time.Now().Add(config.ValidFor).UTC(),
111+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
112+
ExtKeyUsage: config.Usages,
132113
}
133114

134115
certDERBytes, _ := x509.CreateCertificate(cryptorand.Reader, &certTmpl, ca.Certificate, key.Public(), ca.PrivateKey)
@@ -394,21 +375,7 @@ func (cf *CertificateFactory) UserCert(principal *models.Principal, apiURL strin
394375
}
395376

396377
func loadOrCreateCA(kluster *v1.Kluster, name string, cert, key *string, certUpdates *[]CertUpdates) (*Bundle, error) {
397-
regenerate := false
398-
if name == "TLS" && *cert != "" {
399-
block, _ := pem.Decode([]byte(*cert))
400-
if block == nil {
401-
return nil, fmt.Errorf("Failed to decode TLS CA certificate")
402-
}
403-
caCert, err := x509.ParseCertificate(block.Bytes)
404-
if err != nil {
405-
return nil, fmt.Errorf("Failed to parse TLS CA certificate: %s", err)
406-
}
407-
if caCert.SubjectKeyId == nil {
408-
regenerate = true
409-
}
410-
}
411-
if *cert != "" && *key != "" && !regenerate {
378+
if *cert != "" && *key != "" {
412379
return NewBundle([]byte(*key), []byte(*cert))
413380
}
414381
caBundle, err := createCA(kluster.Name, name)
@@ -522,9 +489,6 @@ func createCA(klusterName, name string) (*Bundle, error) {
522489
return nil, fmt.Errorf("Failed to generate private key for %s ca: %s", name, err)
523490
}
524491

525-
keyBytes := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey)
526-
keyHash := sha1.Sum(keyBytes)
527-
528492
now := time.Now()
529493
tmpl := x509.Certificate{
530494
SerialNumber: new(big.Int).SetInt64(0),
@@ -537,7 +501,6 @@ func createCA(klusterName, name string) (*Bundle, error) {
537501
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
538502
BasicConstraintsValid: true,
539503
IsCA: true,
540-
SubjectKeyId: keyHash[:],
541504
}
542505

543506
certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &tmpl, &tmpl, privateKey.Public(), privateKey)
@@ -560,10 +523,6 @@ func isCertChangedOrExpires(origCert, newCert, caCert *x509.Certificate, duratio
560523
return "SAN IP changes: " + strings.Join(IPSliceDiff(origCert.IPAddresses, newCert.IPAddresses), " "), true
561524
}
562525

563-
if !slices.Equal(origCert.AuthorityKeyId, newCert.AuthorityKeyId) {
564-
return "AuthorityKeyId changed", true
565-
}
566-
567526
expire := time.Now().Add(duration)
568527
if expire.After(origCert.NotAfter) {
569528
return fmt.Sprintf("Certificate expires at %s", origCert.NotAfter), true

0 commit comments

Comments
 (0)