2017-05-23 20:56:10 +02:00
|
|
|
package crypto
|
2016-08-01 13:40:12 +12:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2017-05-23 20:56:10 +02:00
|
|
|
// CreateTLSConfiguration initializes a tls.Config using a CA certificate, a certificate and a key
|
|
|
|
func CreateTLSConfiguration(caCertPath, certPath, keyPath string) (*tls.Config, error) {
|
2016-08-03 15:11:09 +12:00
|
|
|
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
|
2016-08-01 13:40:12 +12:00
|
|
|
if err != nil {
|
2016-12-18 18:21:29 +13:00
|
|
|
return nil, err
|
2016-08-01 13:40:12 +12:00
|
|
|
}
|
2016-08-03 15:11:09 +12:00
|
|
|
caCert, err := ioutil.ReadFile(caCertPath)
|
2016-08-01 13:40:12 +12:00
|
|
|
if err != nil {
|
2016-12-18 18:21:29 +13:00
|
|
|
return nil, err
|
2016-08-01 13:40:12 +12:00
|
|
|
}
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
2016-12-18 18:21:29 +13:00
|
|
|
config := &tls.Config{
|
2016-08-01 13:40:12 +12:00
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caCertPool,
|
|
|
|
}
|
2016-12-18 18:21:29 +13:00
|
|
|
return config, nil
|
2016-08-01 13:40:12 +12:00
|
|
|
}
|