go-gemini/cert.go

136 lines
3.7 KiB
Go
Raw Normal View History

2020-09-28 00:20:59 +00:00
package gmi
2020-09-27 17:50:48 +00:00
import (
2020-10-13 21:33:14 +00:00
"crypto"
2020-09-27 17:50:48 +00:00
"crypto/ed25519"
"crypto/rand"
2020-09-28 03:49:41 +00:00
"crypto/tls"
2020-09-27 17:50:48 +00:00
"crypto/x509"
"math/big"
"net"
"path/filepath"
2020-09-27 17:50:48 +00:00
"strings"
"time"
)
// CertificateStore maps hostnames to certificates.
2020-10-12 20:34:52 +00:00
// The zero value of CertificateStore is an empty store ready to use.
2020-10-12 03:48:18 +00:00
type CertificateStore struct {
store map[string]tls.Certificate
}
// Add adds a certificate for the given hostname to the store.
// It tries to parse the certificate if it is not already parsed.
2020-10-12 03:48:18 +00:00
func (c *CertificateStore) Add(hostname string, cert tls.Certificate) {
if c.store == nil {
c.store = map[string]tls.Certificate{}
}
// Parse certificate if not already parsed
if cert.Leaf == nil {
parsed, err := x509.ParseCertificate(cert.Certificate[0])
if err == nil {
cert.Leaf = parsed
}
}
2020-10-12 03:48:18 +00:00
c.store[hostname] = cert
}
// Lookup returns the certificate for the given hostname.
func (c *CertificateStore) Lookup(hostname string) (*tls.Certificate, error) {
cert, ok := c.store[hostname]
if !ok {
return nil, ErrCertificateUnknown
2020-10-12 03:48:18 +00:00
}
// Ensure that the certificate is not expired
if cert.Leaf != nil && cert.Leaf.NotAfter.Before(time.Now()) {
return &cert, ErrCertificateExpired
}
2020-10-12 03:48:18 +00:00
return &cert, nil
}
// Load loads certificates from the given path.
// The path should lead to a directory containing certificates and private keys
// in the form hostname.crt and hostname.key.
// For example, the hostname "localhost" would have the corresponding files
// localhost.crt (certificate) and localhost.key (private key).
func (c *CertificateStore) Load(path string) error {
matches, err := filepath.Glob(filepath.Join(path, "*.crt"))
if err != nil {
return err
}
for _, crtPath := range matches {
keyPath := strings.TrimSuffix(crtPath, ".crt") + ".key"
cert, err := tls.LoadX509KeyPair(crtPath, keyPath)
if err != nil {
continue
}
2020-10-12 04:13:24 +00:00
hostname := strings.TrimSuffix(filepath.Base(crtPath), ".crt")
c.Add(hostname, cert)
}
2020-10-12 03:48:18 +00:00
return nil
}
2020-09-27 17:50:48 +00:00
2020-09-28 03:49:41 +00:00
// NewCertificate creates and returns a new parsed certificate.
func NewCertificate(host string, duration time.Duration) (tls.Certificate, error) {
2020-10-13 21:33:14 +00:00
crt, priv, err := newX509KeyPair(host, duration)
2020-09-28 03:49:41 +00:00
if err != nil {
return tls.Certificate{}, err
}
2020-10-13 21:33:14 +00:00
var cert tls.Certificate
cert.Leaf = crt
cert.Certificate = append(cert.Certificate, crt.Raw)
cert.PrivateKey = priv
return cert, nil
2020-09-27 17:50:48 +00:00
}
2020-10-13 21:33:14 +00:00
// newX509KeyPair creates and returns a new certificate and private key.
func newX509KeyPair(host string, duration time.Duration) (*x509.Certificate, crypto.PrivateKey, error) {
// Generate an ED25519 private key
2020-09-27 17:50:48 +00:00
_, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}
2020-10-13 21:33:14 +00:00
public := priv.Public()
2020-09-27 17:50:48 +00:00
// ED25519 keys should have the DigitalSignature KeyUsage bits set
// in the x509.Certificate template
keyUsage := x509.KeyUsageDigitalSignature
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
2020-10-13 21:33:14 +00:00
notBefore := time.Now()
notAfter := notBefore.Add(duration)
2020-09-27 17:50:48 +00:00
template := x509.Certificate{
SerialNumber: serialNumber,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
hosts := strings.Split(host, ",")
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
2020-10-13 21:33:14 +00:00
crt, err := x509.CreateCertificate(rand.Reader, &template, &template, public, priv)
2020-09-27 17:50:48 +00:00
if err != nil {
return nil, nil, err
}
2020-10-13 21:33:14 +00:00
cert, err := x509.ParseCertificate(crt)
2020-09-27 17:50:48 +00:00
if err != nil {
return nil, nil, err
}
2020-10-13 21:33:14 +00:00
return cert, priv, nil
2020-09-27 17:50:48 +00:00
}