1 package test
2
3 import (
4 "crypto"
5 "crypto/ecdsa"
6 "crypto/rand"
7 "crypto/rsa"
8 "crypto/x509"
9 "encoding/hex"
10 "encoding/pem"
11 "errors"
12 "fmt"
13 "math/big"
14 "os"
15 "testing"
16 )
17
18
19
20
21 func LoadSigner(filename string) (crypto.Signer, error) {
22 keyBytes, err := os.ReadFile(filename)
23 if err != nil {
24 return nil, err
25 }
26
27
28
29
30 block, _ := pem.Decode(keyBytes)
31 if block == nil {
32 return nil, errors.New("Unable to decode private key PEM bytes")
33 }
34
35
36 if rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil {
37 return rsaKey, nil
38 }
39
40
41 if key, err := x509.ParsePKCS8PrivateKey(block.Bytes); err == nil {
42
43 switch k := key.(type) {
44 case *rsa.PrivateKey:
45 return k, nil
46 case *ecdsa.PrivateKey:
47 return k, nil
48 }
49 }
50
51
52 if ecdsaKey, err := x509.ParseECPrivateKey(block.Bytes); err == nil {
53 return ecdsaKey, nil
54 }
55
56
57 return nil, errors.New("Unable to decode private key PEM bytes")
58 }
59
60
61
62
63
64
65 func ThrowAwayCert(t *testing.T, nameCount int) (string, *x509.Certificate) {
66 var serialBytes [16]byte
67 _, _ = rand.Read(serialBytes[:])
68 sn := big.NewInt(0).SetBytes(serialBytes[:])
69
70 return ThrowAwayCertWithSerial(t, nameCount, sn, nil)
71 }
72
73
74
75
76
77
78
79
80 func ThrowAwayCertWithSerial(t *testing.T, nameCount int, sn *big.Int, issuer *x509.Certificate) (string, *x509.Certificate) {
81 k, err := rsa.GenerateKey(rand.Reader, 512)
82 AssertNotError(t, err, "rsa.GenerateKey failed")
83
84 var names []string
85 for i := 0; i < nameCount; i++ {
86 var nameBytes [3]byte
87 _, _ = rand.Read(nameBytes[:])
88 names = append(names, fmt.Sprintf("%s.example.com", hex.EncodeToString(nameBytes[:])))
89 }
90
91 template := &x509.Certificate{
92 SerialNumber: sn,
93 DNSNames: names,
94 IssuingCertificateURL: []string{"http://localhost:4001/acme/issuer-cert/1234"},
95 }
96
97 if issuer == nil {
98 issuer = template
99 }
100
101 testCertDER, err := x509.CreateCertificate(rand.Reader, template, issuer, &k.PublicKey, k)
102 AssertNotError(t, err, "x509.CreateCertificate failed")
103 testCert, err := x509.ParseCertificate(testCertDER)
104 AssertNotError(t, err, "failed to parse self-signed cert DER")
105 return fmt.Sprintf("%036x", sn), testCert
106 }
107
View as plain text