1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
package tlsutil
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/pkg/errors"
)
// ServerRenewFunc defines the type of the functions used to get a new tls
// certificate.
type ServerRenewFunc func(hello *tls.ClientHelloInfo) (*tls.Certificate, *tls.Config, error)
// ServerCredentials is a type that manages the credentials of a server.
type ServerCredentials struct {
RenewFunc ServerRenewFunc
cache *credentialsCache
}
// NewServerCredentials returns a new ServerCredentials that will get
// certificates from the given function.
func NewServerCredentials(fn ServerRenewFunc) (*ServerCredentials, error) {
return &ServerCredentials{
RenewFunc: fn,
cache: newCredentialsCache(),
}, nil
}
// NewServerCredentialsFromFile returns a ServerCredentials that renews the
// certificate from a file on disk.
func NewServerCredentialsFromFile(certFile, keyFile string) (*ServerCredentials, error) {
if _, err := tls.LoadX509KeyPair(certFile, keyFile); err != nil {
return nil, errors.Wrap(err, "error loading certificate")
}
return NewServerCredentials(func(*tls.ClientHelloInfo) (*tls.Certificate, *tls.Config, error) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, nil, errors.Wrap(err, "error loading certificate")
}
if cert.Leaf == nil {
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, nil, errors.Wrap(err, "error parsing certificate")
}
}
return &cert, &tls.Config{
MinVersion: tls.VersionTLS12,
}, nil
})
}
// TLSConfig returns a *tls.Config with GetCertificate and GetConfigForClient
// set.
func (c *ServerCredentials) TLSConfig() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS12,
GetCertificate: c.GetCertificate,
GetConfigForClient: c.GetConfigForClient,
}
}
// GetCertificate returns the certificate for the SNI in the hello message.
func (c *ServerCredentials) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if hello.ServerName == "" {
return nil, fmt.Errorf("server name indication cannot be empty")
}
sni, err := SanitizeName(hello.ServerName)
if err != nil {
return nil, err
}
// Attempt to load a certificate.
if v, ok := c.cache.Load(sni); ok {
return v.renewer.GetCertificate(hello)
}
renewer, err := c.getCertificate(sni, hello)
if err != nil {
return nil, err
}
return renewer.GetCertificate(hello)
}
// GetConfigForClient returns the tls.Config used per request.
func (c *ServerCredentials) GetConfigForClient(hello *tls.ClientHelloInfo) (*tls.Config, error) {
if hello.ServerName == "" {
return nil, fmt.Errorf("server name indication cannot be empty")
}
sni, err := SanitizeName(hello.ServerName)
if err != nil {
return nil, err
}
if v, ok := c.cache.Load(sni); ok {
return v.renewer.GetConfigForClient(hello)
}
renewer, err := c.getCertificate(sni, hello)
if err != nil {
return nil, err
}
return renewer.GetConfigForClient(hello)
}
func (c *ServerCredentials) getCertificate(sni string, hello *tls.ClientHelloInfo) (*Renewer, error) {
cert, tlsConfig, err := c.RenewFunc(hello)
if err != nil {
return nil, err
}
renewer, err := NewRenewer(cert, tlsConfig, func() (*tls.Certificate, *tls.Config, error) {
return c.RenewFunc(hello)
})
if err != nil {
return nil, err
}
renewer.Run()
c.cache.Store(sni, &credentialsCacheElement{
sni: sni,
renewer: renewer,
})
return renewer, nil
}
|