2019-10-27 06:18:58 +00:00
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
2019-11-15 23:43:02 +00:00
|
|
|
"crypto"
|
2019-10-27 06:18:58 +00:00
|
|
|
"crypto/x509"
|
2019-11-15 23:43:02 +00:00
|
|
|
"fmt"
|
2019-10-27 06:18:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2022-05-11 19:43:43 +00:00
|
|
|
"time"
|
2019-11-15 23:43:02 +00:00
|
|
|
|
|
|
|
"github.com/rancher/dynamiclistener/cert"
|
2019-10-27 06:18:58 +00:00
|
|
|
)
|
|
|
|
|
2019-11-15 23:43:02 +00:00
|
|
|
func GenCA() (*x509.Certificate, crypto.Signer, error) {
|
2019-10-27 06:18:58 +00:00
|
|
|
caKey, err := NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-11 19:43:43 +00:00
|
|
|
caCert, err := NewSelfSignedCACert(caKey, fmt.Sprintf("dynamiclistener-ca@%d", time.Now().Unix()), "dynamiclistener-org")
|
2019-10-27 06:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return caCert, caKey, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:43:02 +00:00
|
|
|
func LoadOrGenCA() (*x509.Certificate, crypto.Signer, error) {
|
2019-10-27 06:18:58 +00:00
|
|
|
cert, key, err := loadCA()
|
|
|
|
if err == nil {
|
|
|
|
return cert, key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, key, err = GenCA()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certBytes, keyBytes, err := Marshal(cert, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll("./certs", 0700); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("./certs/ca.pem", certBytes, 0600); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("./certs/ca.key", keyBytes, 0600); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, key, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:43:02 +00:00
|
|
|
func loadCA() (*x509.Certificate, crypto.Signer, error) {
|
2019-10-27 06:18:58 +00:00
|
|
|
return LoadCerts("./certs/ca.pem", "./certs/ca.key")
|
|
|
|
}
|
|
|
|
|
2020-01-31 05:18:44 +00:00
|
|
|
func LoadCA(caPem, caKey []byte) (*x509.Certificate, crypto.Signer, error) {
|
2019-11-15 23:43:02 +00:00
|
|
|
key, err := cert.ParsePrivateKeyPEM(caKey)
|
2019-10-27 06:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-15 23:43:02 +00:00
|
|
|
signer, ok := key.(crypto.Signer)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("key is not a crypto.Signer")
|
|
|
|
}
|
2019-10-27 06:18:58 +00:00
|
|
|
|
|
|
|
cert, err := ParseCertPEM(caPem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:43:02 +00:00
|
|
|
return cert, signer, nil
|
2019-10-27 06:18:58 +00:00
|
|
|
}
|
2020-01-31 05:18:44 +00:00
|
|
|
|
|
|
|
func LoadCerts(certFile, keyFile string) (*x509.Certificate, crypto.Signer, error) {
|
|
|
|
caPem, err := ioutil.ReadFile(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
caKey, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadCA(caPem, caKey)
|
|
|
|
}
|