func GenerateCertAndKey(cfg CertConfig) ([]byte, []byte, error) {
	if len(cfg.CAKey) == 0 || len(cfg.CACert) == 0 {
		return nil, nil, errors.New("CAKey and CACert must be provided")
	}
	var privateKey *rsa.PrivateKey
	var err error
	keyBuf := bytes.NewBuffer([]byte(""))
	if len(cfg.PrivateKey) == 0 {
		privateKey, err = rsa.GenerateKey(rand.Reader, 2048)
		if err != nil {
			return nil, nil, err
		}
		marshaledKey := x509.MarshalPKCS1PrivateKey(privateKey)
		privateKeyPem := pem.Block{
			Type:  "RSA PRIVATE KEY",
			Bytes: marshaledKey,
		}
		err = pem.Encode(keyBuf, &privateKeyPem)
		if err != nil {
			return nil, nil, errors.Wrap(err, "pem Encode client key error")
		}
	} else {
		keyBlock, _ := pem.Decode(cfg.PrivateKey)
		privateKey, err = x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
		if err != nil {
			return nil, nil, errors.Wrap(err, "x509 ParsePKCS1PrivateKey error")
		}
		keyBuf = bytes.NewBuffer(cfg.PrivateKey)
	}

	caBlock, _ := pem.Decode(cfg.CACert)
	cert, err := x509.ParseCertificate(caBlock.Bytes)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 ParseCertificate error")
	}

	caKeyBlock, _ := pem.Decode(cfg.CAKey)
	caPrivate, err := x509.ParsePKCS1PrivateKey(caKeyBlock.Bytes)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 ParsePKCS1PrivateKey error")
	}

	notBefore := time.Now().UTC()
	certTemplate := x509.Certificate{
		SerialNumber:          big.NewInt(rd.Int63()),
		NotBefore:             notBefore,
		NotAfter:              cfg.NotAfter.UTC(),
		BasicConstraintsValid: true,
		IsCA:                  cfg.IsCA,
		KeyUsage:              cfg.KeyUsage,
		ExtKeyUsage:           cfg.ExtKeyUsage,
		Subject: pkix.Name{
			CommonName:   cfg.CommonName,
			Organization: cfg.Organization,
		},
		DNSNames:    cfg.DNSs,
		IPAddresses: cfg.IPs,
	}

	certEncode, err := x509.CreateCertificate(rand.Reader, &certTemplate, cert, &privateKey.PublicKey, caPrivate)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 CreateCertificate error")
	}

	var certBuf bytes.Buffer
	err = pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: certEncode})
	if err != nil {
		return nil, nil, errors.Wrap(err, "pem Encode certificate error")
	}
	return certBuf.Bytes(), keyBuf.Bytes(), nil
}
// GenerateCertAndKey will generate a certificate and private key using the provided CA.
// If the PrivateKey is not provided, we will generate one.
GenerateCertAndKey will generate a certificate and private key using the provided CA.
If the PrivateKey is not provided, we will generate one.
// GenerateCertAndKey will generate a certificate and private key using the provided CA . // If the PrivateKey is not provided we will generate one .
func GenerateCertAndKey(cfg CertConfig) ([]byte, []byte, error) {
	if len(cfg.CAKey) == 0 || len(cfg.CACert) == 0 {
		return nil, nil, errors.New("CAKey and CACert must be provided")
	}
	var privateKey *rsa.PrivateKey
	var err error
	keyBuf := bytes.NewBuffer([]byte(""))
	if len(cfg.PrivateKey) == 0 {
		privateKey, err = rsa.GenerateKey(rand.Reader, 2048)
		if err != nil {
			return nil, nil, err
		}
		marshaledKey := x509.MarshalPKCS1PrivateKey(privateKey)
		privateKeyPem := pem.Block{
			Type:  "RSA PRIVATE KEY",
			Bytes: marshaledKey,
		}
		err = pem.Encode(keyBuf, &privateKeyPem)
		if err != nil {
			return nil, nil, errors.Wrap(err, "pem Encode client key error")
		}
	} else {
		keyBlock, _ := pem.Decode(cfg.PrivateKey)
		privateKey, err = x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
		if err != nil {
			return nil, nil, errors.Wrap(err, "x509 ParsePKCS1PrivateKey error")
		}
		keyBuf = bytes.NewBuffer(cfg.PrivateKey)
	}
	caBlock, _ := pem.Decode(cfg.CACert)
	cert, err := x509.ParseCertificate(caBlock.Bytes)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 ParseCertificate error")
	}
	caKeyBlock, _ := pem.Decode(cfg.CAKey)
	caPrivate, err := x509.ParsePKCS1PrivateKey(caKeyBlock.Bytes)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 ParsePKCS1PrivateKey error")
	}
	notBefore := time.Now().UTC()
	certTemplate := x509.Certificate{
		SerialNumber:          big.NewInt(rd.Int63()),
		NotBefore:             notBefore,
		NotAfter:              cfg.NotAfter.UTC(),
		BasicConstraintsValid: true,
		IsCA:                  cfg.IsCA,
		KeyUsage:              cfg.KeyUsage,
		ExtKeyUsage:           cfg.ExtKeyUsage,
		Subject: pkix.Name{
			CommonName:   cfg.CommonName,
			Organization: cfg.Organization,
		},
		DNSNames:    cfg.DNSs,
		IPAddresses: cfg.IPs,
	}
	certEncode, err := x509.CreateCertificate(rand.Reader, &certTemplate, cert, &privateKey.PublicKey, caPrivate)
	if err != nil {
		return nil, nil, errors.Wrap(err, "x509 CreateCertificate error")
	}
	var certBuf bytes.Buffer
	err = pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: certEncode})
	if err != nil {
		return nil, nil, errors.Wrap(err, "pem Encode certificate error")
	}
	return certBuf.Bytes(), keyBuf.Bytes(), nil
}
func GenerateCertAndKey ( cfg CertConfig ) ( [ ] byte , [ ] byte , error ) { if len ( cfg . CAKey ) == 0 || len ( cfg . CACert ) == 0 { return nil , nil , errors . New ( " " ) } var privateKey * rsa . PrivateKey var err error keyBuf := bytes . NewBuffer ( [ ] byte ( " " ) ) if len ( cfg . PrivateKey ) == 0 { privateKey , err = rsa . GenerateKey ( rand . Reader , 2048 ) if err != nil { return nil , nil , err } marshaledKey := x509 . MarshalPKCS1PrivateKey ( privateKey ) privateKeyPem := pem . Block { Type : " " , Bytes : marshaledKey , } err = pem . Encode ( keyBuf , & privateKeyPem ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } } else { keyBlock , _ := pem . Decode ( cfg . PrivateKey ) privateKey , err = x509 . ParsePKCS1PrivateKey ( keyBlock . Bytes ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } keyBuf = bytes . NewBuffer ( cfg . PrivateKey ) } caBlock , _ := pem . Decode ( cfg . CACert ) cert , err := x509 . ParseCertificate ( caBlock . Bytes ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } caKeyBlock , _ := pem . Decode ( cfg . CAKey ) caPrivate , err := x509 . ParsePKCS1PrivateKey ( caKeyBlock . Bytes ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } notBefore := time . Now ( ) . UTC ( ) certTemplate := x509 . Certificate { SerialNumber : big . NewInt ( rd . Int63 ( ) ) , NotBefore : notBefore , NotAfter : cfg . NotAfter . UTC ( ) , BasicConstraintsValid : true , IsCA : cfg . IsCA , KeyUsage : cfg . KeyUsage , ExtKeyUsage : cfg . ExtKeyUsage , Subject : pkix . Name { CommonName : cfg . CommonName , Organization : cfg . Organization , } , DNSNames : cfg . DNSs , IPAddresses : cfg . IPs , } certEncode , err := x509 . CreateCertificate ( rand . Reader , & certTemplate , cert , & privateKey . PublicKey , caPrivate ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } var certBuf bytes . Buffer err = pem . Encode ( & certBuf , & pem . Block { Type : " " , Bytes : certEncode } ) if err != nil { return nil , nil , errors . Wrap ( err , " " ) } return certBuf . Bytes ( ) , keyBuf . Bytes ( ) , nil }
GenerateCertAndKey will generate a certificate and private key using the provided CA.
GenerateCertAndKey will generate a certificate and private key using the provided CA .