Skip to content

Commit

Permalink
feat: add mTLS support and ensure backward compatibility
Browse files Browse the repository at this point in the history
Introduced mutual TLS support for enhanced security
Patched existing invocations to maintain backward compatibility
Enabled mTLS functionality for all certificates issued by the local (internal) CA
  • Loading branch information
tgragnato committed Oct 7, 2024
1 parent 6b33122 commit 87fe565
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var cmdGen = &cobra.Command{
Str("algo", string(req.Algo)).
Msg("generating certificate")

crt, key, err := pki.New(req)
crt, key, err := pki.New(req, false)
if err != nil {
log.Fatal().Err(err).Msg("unable to generate certificate")
}
Expand Down
5 changes: 4 additions & 1 deletion pki/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func NewRequest(options map[string]any) Request {
return req
}

func New(req Request) (*x509.Certificate, *Key, error) {
func New(req Request, mTLS bool) (*x509.Certificate, *Key, error) {
var crt = x509.Certificate{}
crt.Subject = pkix.Name{
CommonName: req.CN,
Expand All @@ -159,6 +159,9 @@ func New(req Request) (*x509.Certificate, *Key, error) {
if req.Algo == RSA {
crt.KeyUsage |= x509.KeyUsageKeyEncipherment
}
if mTLS {
crt.ExtKeyUsage = append(crt.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
}

if serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)); err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion pki/crt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

func testNewPair(t *testing.T) {
reqCrt, reqKey, err := New(testingReq)
reqCrt, reqKey, err := New(testingReq, false)
is.New(t).NoErr(err)
testingCrt = reqCrt
testingKey = reqKey
Expand Down
2 changes: 1 addition & 1 deletion provider/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (p *Local) Get(name string, options map[string]string) ([]byte, []byte, err
}

req := pki.NewRequest(reqOptions)
crt, key, err := pki.New(req)
crt, key, err := pki.New(req, true)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 87fe565

Please sign in to comment.