|
|
|
|
@ -11,6 +11,7 @@ import (
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
|
"encoding/pem"
|
|
|
|
|
"io"
|
|
|
|
|
"math"
|
|
|
|
|
"math/big"
|
|
|
|
|
"net"
|
|
|
|
|
@ -27,10 +28,10 @@ import (
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func generatePrivAndPubKeys(ctx context.Context) (privKeyBytes []byte, pubKeyBytes []byte, err error) {
|
|
|
|
|
func generatePrivAndPubKeys(ctx context.Context, randomReader io.Reader) (privKeyBytes []byte, pubKeyBytes []byte, err error) {
|
|
|
|
|
const op = "target.generatePrivAndPubKeys"
|
|
|
|
|
// Generate a private key using the P521 curve
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P521(), randomReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "failed to generate ECDSA key")
|
|
|
|
|
}
|
|
|
|
|
@ -62,7 +63,7 @@ func generateTargetCert(ctx context.Context, privKey *ecdsa.PrivateKey, exp time
|
|
|
|
|
|
|
|
|
|
opts := GetOpts(opt...)
|
|
|
|
|
|
|
|
|
|
randomSerialNumber, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
|
|
|
|
|
randomSerialNumber, err := rand.Int(opts.withRandomReader, big.NewInt(int64(math.MaxInt64)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error generating random serial number"))
|
|
|
|
|
}
|
|
|
|
|
@ -87,7 +88,7 @@ func generateTargetCert(ctx context.Context, privKey *ecdsa.PrivateKey, exp time
|
|
|
|
|
template.DNSNames = append(template.DNSNames, opts.WithAlias.Value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, &privKey.PublicKey, privKey)
|
|
|
|
|
certBytes, err := x509.CreateCertificate(opts.withRandomReader, template, template, &privKey.PublicKey, privKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.GenCert))
|
|
|
|
|
}
|
|
|
|
|
@ -97,7 +98,9 @@ func generateTargetCert(ctx context.Context, privKey *ecdsa.PrivateKey, exp time
|
|
|
|
|
func generateKeysAndCert(ctx context.Context, notValidAfter time.Time, opt ...Option) (privKey []byte, pubKey []byte, cert []byte, err error) {
|
|
|
|
|
const op = "target.generateKeysAndCert"
|
|
|
|
|
|
|
|
|
|
privKey, pubKey, err = generatePrivAndPubKeys(ctx)
|
|
|
|
|
opts := GetOpts(opt...)
|
|
|
|
|
|
|
|
|
|
privKey, pubKey, err = generatePrivAndPubKeys(ctx, opts.withRandomReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, errors.Wrap(ctx, err, op)
|
|
|
|
|
}
|
|
|
|
|
@ -128,7 +131,7 @@ func NewTargetProxyCertificate(ctx context.Context, opt ...Option) (*TargetProxy
|
|
|
|
|
opts := GetOpts(opt...)
|
|
|
|
|
|
|
|
|
|
notValidAfter := time.Now().AddDate(1, 0, 0) // 1 year from now
|
|
|
|
|
privKey, pubKey, cert, err := generateKeysAndCert(ctx, notValidAfter)
|
|
|
|
|
privKey, pubKey, cert, err := generateKeysAndCert(ctx, notValidAfter, opt...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error generating target proxy cert and keys"))
|
|
|
|
|
}
|
|
|
|
|
@ -245,7 +248,7 @@ type TargetAliasProxyCertificate struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewTargetAliasProxyCertificate creates a new in memory TargetAliasProxyCertificate
|
|
|
|
|
func NewTargetAliasProxyCertificate(ctx context.Context, targetId string, alias *talias.Alias) (*TargetAliasProxyCertificate, error) {
|
|
|
|
|
func NewTargetAliasProxyCertificate(ctx context.Context, targetId string, alias *talias.Alias, opt ...Option) (*TargetAliasProxyCertificate, error) {
|
|
|
|
|
const op = "target.NewTargetAliasProxyCertificate"
|
|
|
|
|
switch {
|
|
|
|
|
case targetId == "":
|
|
|
|
|
@ -255,7 +258,9 @@ func NewTargetAliasProxyCertificate(ctx context.Context, targetId string, alias
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notValidAfter := time.Now().AddDate(1, 0, 0) // 1 year from now
|
|
|
|
|
privKey, pubKey, cert, err := generateKeysAndCert(ctx, notValidAfter, WithAlias(alias))
|
|
|
|
|
|
|
|
|
|
opt = append(opt, WithAlias(alias))
|
|
|
|
|
privKey, pubKey, cert, err := generateKeysAndCert(ctx, notValidAfter, opt...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error generating target proxy cert and keys"))
|
|
|
|
|
}
|
|
|
|
|
|