| 1 |
package testutils |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/elliptic" |
| 6 |
"crypto/rand" |
| 7 |
"crypto/x509" |
| 8 |
"crypto/x509/pkix" |
| 9 |
"encoding/pem" |
| 10 |
"math/big" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
type Certificate struct { |
| 18 |
CertPath string |
| 19 |
KeyPath string |
| 20 |
} |
| 21 |
|
| 22 |
func newCertificate(parentDir string, kind string, cert *x509.Certificate, key *ecdsa.PrivateKey) (Certificate, error) { |
| 23 |
certPath := filepath.Join(parentDir, kind+"-cert.pem") |
| 24 |
certFile, err := os.Create(certPath) |
| 25 |
if err != nil { |
| 26 |
return Certificate{}, err |
| 27 |
} |
| 28 |
defer certFile.Close() |
| 29 |
|
| 30 |
err = pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}) |
| 31 |
if err != nil { |
| 32 |
return Certificate{}, err |
| 33 |
} |
| 34 |
|
| 35 |
keyPath := filepath.Join(parentDir, kind+"-key.pem") |
| 36 |
keyFile, err := os.Create(keyPath) |
| 37 |
if err != nil { |
| 38 |
return Certificate{}, err |
| 39 |
} |
| 40 |
defer keyFile.Close() |
| 41 |
|
| 42 |
privBytes, err := x509.MarshalPKCS8PrivateKey(key) |
| 43 |
if err != nil { |
| 44 |
return Certificate{}, err |
| 45 |
} |
| 46 |
pem.Encode(keyFile, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) |
| 47 |
|
| 48 |
return Certificate{ |
| 49 |
CertPath: certPath, |
| 50 |
KeyPath: keyPath, |
| 51 |
}, nil |
| 52 |
} |
| 53 |
|
| 54 |
func GenCertificate(t *testing.T) (ca Certificate, server Certificate) { |
| 55 |
caCert, caKey, err := generateCA() |
| 56 |
if err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
|
| 60 |
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 61 |
if err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 64 |
|
| 65 |
template := x509.Certificate{ |
| 66 |
SerialNumber: big.NewInt(1), |
| 67 |
Subject: pkix.Name{Organization: []string{"Example Inc"}}, |
| 68 |
DNSNames: []string{"example.com", "www.example.com"}, |
| 69 |
NotBefore: time.Now(), |
| 70 |
NotAfter: time.Now().Add(24 * time.Hour), |
| 71 |
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 72 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 73 |
BasicConstraintsValid: true, |
| 74 |
} |
| 75 |
|
| 76 |
certBytes, err := x509.CreateCertificate(rand.Reader, &template, caCert, &privateKey.PublicKey, caKey) |
| 77 |
if err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
|
| 81 |
serverCert, err := x509.ParseCertificate(certBytes) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
|
| 86 |
ca, err = newCertificate(t.TempDir(), "ca", caCert, caKey) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
server, err = newCertificate(t.TempDir(), "server", serverCert, privateKey) |
| 92 |
if err != nil { |
| 93 |
t.Fatal(err) |
| 94 |
} |
| 95 |
|
| 96 |
return ca, server |
| 97 |
} |
| 98 |
|
| 99 |
func generateCA() (*x509.Certificate, *ecdsa.PrivateKey, error) { |
| 100 |
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 101 |
if err != nil { |
| 102 |
return nil, nil, err |
| 103 |
} |
| 104 |
|
| 105 |
template := x509.Certificate{ |
| 106 |
SerialNumber: big.NewInt(1), |
| 107 |
Subject: pkix.Name{ |
| 108 |
Organization: []string{"Test CA"}, |
| 109 |
}, |
| 110 |
NotBefore: time.Now(), |
| 111 |
NotAfter: time.Now().Add(time.Hour), |
| 112 |
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, |
| 113 |
BasicConstraintsValid: true, |
| 114 |
IsCA: true, |
| 115 |
MaxPathLen: 1, |
| 116 |
} |
| 117 |
|
| 118 |
certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) |
| 119 |
if err != nil { |
| 120 |
return nil, nil, err |
| 121 |
} |
| 122 |
|
| 123 |
cert, err := x509.ParseCertificate(certBytes) |
| 124 |
if err != nil { |
| 125 |
return nil, nil, err |
| 126 |
} |
| 127 |
|
| 128 |
return cert, privateKey, nil |
| 129 |
} |