2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-12 10:58:14 +00:00
|
|
|
"crypto"
|
2018-05-09 22:12:31 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2019-07-30 21:47:39 +00:00
|
|
|
"crypto/rsa"
|
2018-05-09 22:12:31 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2019-07-30 21:47:39 +00:00
|
|
|
"strings"
|
2020-02-10 23:05:49 +00:00
|
|
|
"time"
|
2018-05-09 22:12:31 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
const (
|
2020-02-10 23:05:49 +00:00
|
|
|
DefaultPrivateKeyType = "ec"
|
|
|
|
DefaultPrivateKeyBits = 256
|
|
|
|
DefaultIntermediateCertTTL = 24 * 365 * time.Hour
|
2024-01-10 12:15:36 +00:00
|
|
|
|
|
|
|
// RSA specific settings.
|
|
|
|
PrivateKeyTypeRSA = "rsa"
|
|
|
|
MinPrivateKeyBitsRSA = 2048
|
|
|
|
DefaultPrivateKeyBitsRSA = 4096
|
2019-07-30 21:47:39 +00:00
|
|
|
)
|
|
|
|
|
2022-01-05 23:21:04 +00:00
|
|
|
func pemEncode(value []byte, blockType string) (string, error) {
|
2019-07-30 21:47:39 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2022-01-05 23:21:04 +00:00
|
|
|
if err := pem.Encode(&buf, &pem.Block{Type: blockType, Bytes: value}); err != nil {
|
|
|
|
return "", fmt.Errorf("error encoding value %v: %s", blockType, err)
|
2019-07-30 21:47:39 +00:00
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRSAKey(keyBits int) (crypto.Signer, string, error) {
|
|
|
|
var pk *rsa.PrivateKey
|
|
|
|
|
2024-01-10 12:15:36 +00:00
|
|
|
// Check for a secure key length.
|
|
|
|
if keyBits < MinPrivateKeyBitsRSA {
|
|
|
|
return nil, "", fmt.Errorf("error generating RSA private key: invalid key size %d, must be at least %d bits", keyBits, MinPrivateKeyBitsRSA)
|
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
pk, err := rsa.GenerateKey(rand.Reader, keyBits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("error generating RSA private key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := x509.MarshalPKCS1PrivateKey(pk)
|
2022-01-05 23:21:04 +00:00
|
|
|
pemBlock, err := pemEncode(bs, "RSA PRIVATE KEY")
|
2019-07-30 21:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pk, pemBlock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateECDSAKey(keyBits int) (crypto.Signer, string, error) {
|
2018-05-09 22:12:31 +00:00
|
|
|
var pk *ecdsa.PrivateKey
|
2019-07-30 21:47:39 +00:00
|
|
|
var curve elliptic.Curve
|
2018-05-09 22:12:31 +00:00
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
switch keyBits {
|
|
|
|
case 224:
|
|
|
|
curve = elliptic.P224()
|
|
|
|
case 256:
|
|
|
|
curve = elliptic.P256()
|
|
|
|
case 384:
|
|
|
|
curve = elliptic.P384()
|
|
|
|
case 521:
|
|
|
|
curve = elliptic.P521()
|
|
|
|
default:
|
|
|
|
return nil, "", fmt.Errorf("error generating ECDSA private key: unknown curve length %d", keyBits)
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := ecdsa.GenerateKey(curve, rand.Reader)
|
2018-05-09 22:12:31 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return nil, "", fmt.Errorf("error generating ECDSA private key: %s", err)
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := x509.MarshalECPrivateKey(pk)
|
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return nil, "", fmt.Errorf("error marshaling ECDSA private key: %s", err)
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 23:21:04 +00:00
|
|
|
pemBlock, err := pemEncode(bs, "EC PRIVATE KEY")
|
2018-05-09 22:12:31 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return nil, "", err
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
return pk, pemBlock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeneratePrivateKey generates a new Private key
|
|
|
|
func GeneratePrivateKeyWithConfig(keyType string, keyBits int) (crypto.Signer, string, error) {
|
|
|
|
switch strings.ToLower(keyType) {
|
2024-01-10 12:15:36 +00:00
|
|
|
case PrivateKeyTypeRSA:
|
2019-07-30 21:47:39 +00:00
|
|
|
return generateRSAKey(keyBits)
|
2024-01-10 12:15:36 +00:00
|
|
|
case DefaultPrivateKeyType:
|
2019-07-30 21:47:39 +00:00
|
|
|
return generateECDSAKey(keyBits)
|
|
|
|
default:
|
|
|
|
return nil, "", fmt.Errorf("unknown private key type requested: %s", keyType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GeneratePrivateKey() (crypto.Signer, string, error) {
|
|
|
|
// TODO: find any calls to this func, replace with calls to GeneratePrivateKeyWithConfig()
|
|
|
|
// using prefs `private_key_type` and `private_key_bits`
|
|
|
|
return GeneratePrivateKeyWithConfig(DefaultPrivateKeyType, DefaultPrivateKeyBits)
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|