78 lines
2.8 KiB
Go
78 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestValidateManualCertificate(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
certificate, privateKey := testCertificatePair(t, []string{"site.hosts.example.com", "www.example.com"}, now)
|
|
if err := validateManualCertificate(certificate, privateKey, []string{"site.hosts.example.com", "www.example.com"}, now); err != nil {
|
|
t.Fatalf("valid certificate rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateManualCertificateRejectsMissingDomain(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
certificate, privateKey := testCertificatePair(t, []string{"site.hosts.example.com"}, now)
|
|
err := validateManualCertificate(certificate, privateKey, []string{"site.hosts.example.com", "www.example.com"}, now)
|
|
if err == nil || !strings.Contains(err.Error(), "www.example.com") {
|
|
t.Fatalf("missing domain error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateManualCertificateRejectsMismatchedKey(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
certificate, _ := testCertificatePair(t, []string{"site.hosts.example.com"}, now)
|
|
_, privateKey := testCertificatePair(t, []string{"site.hosts.example.com"}, now)
|
|
err := validateManualCertificate(certificate, privateKey, []string{"site.hosts.example.com"}, now)
|
|
if err == nil || !strings.Contains(err.Error(), "不匹配") {
|
|
t.Fatalf("mismatched key error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateManualCertificateRejectsTrailingPEMData(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
certificate, privateKey := testCertificatePair(t, []string{"site.hosts.example.com"}, now)
|
|
err := validateManualCertificate(certificate+"not-pem", privateKey, []string{"site.hosts.example.com"}, now)
|
|
if err == nil || !strings.Contains(err.Error(), "PEM CERTIFICATE") {
|
|
t.Fatalf("trailing data error = %v", err)
|
|
}
|
|
}
|
|
|
|
func testCertificatePair(t *testing.T, domains []string, now time.Time) (string, string) {
|
|
t.Helper()
|
|
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
template := &x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{CommonName: domains[0]},
|
|
DNSNames: domains,
|
|
NotBefore: now.Add(-time.Hour),
|
|
NotAfter: now.Add(24 * time.Hour),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, template, template, publicKey, privateKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keyDER, err := x509.MarshalPKCS8PrivateKey(privateKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certificatePEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
|
|
privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER})
|
|
return string(certificatePEM), string(privateKeyPEM)
|
|
}
|