63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
/*
|
|
* Copyright 2024 the KubeSphere Authors.
|
|
* Please refer to the LICENSE file in the root directory of the project.
|
|
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
|
|
*/
|
|
|
|
package pkiutil
|
|
|
|
import (
|
|
"crypto"
|
|
cryptorand "crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
|
|
"github.com/pkg/errors"
|
|
certutil "k8s.io/client-go/util/cert"
|
|
)
|
|
|
|
const (
|
|
rsaKeySize = 2048
|
|
)
|
|
|
|
// NewCSRAndKey generates a new key and CSR and that could be signed to create the given certificate
|
|
func NewCSRAndKey(config *certutil.Config) (*x509.CertificateRequest, *rsa.PrivateKey, error) {
|
|
key, err := NewPrivateKey()
|
|
if err != nil {
|
|
return nil, nil, errors.Wrap(err, "unable to create private key")
|
|
}
|
|
|
|
csr, err := NewCSR(*config, key)
|
|
if err != nil {
|
|
return nil, nil, errors.Wrap(err, "unable to generate CSR")
|
|
}
|
|
|
|
return csr, key, nil
|
|
}
|
|
|
|
// NewCSR creates a new CSR
|
|
func NewCSR(cfg certutil.Config, key crypto.Signer) (*x509.CertificateRequest, error) {
|
|
template := &x509.CertificateRequest{
|
|
Subject: pkix.Name{
|
|
CommonName: cfg.CommonName,
|
|
Organization: cfg.Organization,
|
|
},
|
|
DNSNames: cfg.AltNames.DNSNames,
|
|
IPAddresses: cfg.AltNames.IPs,
|
|
}
|
|
|
|
csrBytes, err := x509.CreateCertificateRequest(cryptorand.Reader, template, key)
|
|
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to create a CSR")
|
|
}
|
|
|
|
return x509.ParseCertificateRequest(csrBytes)
|
|
}
|
|
|
|
// NewPrivateKey creates an RSA private key
|
|
func NewPrivateKey() (*rsa.PrivateKey, error) {
|
|
return rsa.GenerateKey(cryptorand.Reader, rsaKeySize)
|
|
}
|