|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/elliptic" |
| 7 | + "crypto/rand" |
| 8 | + "crypto/rsa" |
| 9 | + "crypto/x509" |
| 10 | + "crypto/x509/pkix" |
| 11 | + "encoding/pem" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "log" |
| 15 | + "math/big" |
| 16 | + "os" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +//go:generate go run ${GOFILE} |
| 21 | + |
| 22 | +var certTemplate = x509.Certificate{ |
| 23 | + SerialNumber: big.NewInt(199999), |
| 24 | + Subject: pkix.Name{ |
| 25 | + CommonName: "test", |
| 26 | + }, |
| 27 | + NotBefore: time.Now().AddDate(-1, 1, 1), |
| 28 | + NotAfter: time.Now().AddDate(1, 1, 1), |
| 29 | + |
| 30 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 31 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning, x509.ExtKeyUsageAny}, |
| 32 | + |
| 33 | + BasicConstraintsValid: true, |
| 34 | +} |
| 35 | + |
| 36 | +func generateCertificate(signer crypto.Signer, out io.Writer, isCA bool) error { |
| 37 | + template := certTemplate |
| 38 | + template.IsCA = isCA |
| 39 | + if isCA { |
| 40 | + template.KeyUsage = template.KeyUsage | x509.KeyUsageCertSign |
| 41 | + template.MaxPathLen = 1 |
| 42 | + } |
| 43 | + derBytes, err := x509.CreateCertificate(rand.Reader, &template, &certTemplate, signer.Public(), signer) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("unable to generate a certificate: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + if err = pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { |
| 49 | + return fmt.Errorf("unable to write cert to file: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// generates a multiple-certificate CA file with both RSA and ECDSA certs and |
| 56 | +// returns the filename so that cleanup can be deferred. |
| 57 | +func generateMultiCert() error { |
| 58 | + certOut, err := os.Create("multi.pem") |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("unable to create file to write multi-cert to: %w", err) |
| 61 | + } |
| 62 | + defer func() { _ = certOut.Close() }() |
| 63 | + |
| 64 | + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("unable to generate RSA key for multi-cert: %w", err) |
| 67 | + } |
| 68 | + ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("unable to generate ECDSA key for multi-cert: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + for _, signer := range []crypto.Signer{rsaKey, ecKey} { |
| 74 | + if err := generateCertificate(signer, certOut, true); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func generateCertAndKey() error { |
| 83 | + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("unable to generate RSA key: %w", err) |
| 86 | + |
| 87 | + } |
| 88 | + keyBytes := x509.MarshalPKCS1PrivateKey(rsaKey) |
| 89 | + |
| 90 | + keyOut, err := os.Create("key.pem") |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("unable to create file to write key to: %w", err) |
| 93 | + } |
| 94 | + defer func() { _ = keyOut.Close() }() |
| 95 | + |
| 96 | + if err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes}); err != nil { |
| 97 | + return fmt.Errorf("unable to write key to file: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + certOut, err := os.Create("cert.pem") |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("to create file to write cert to: %w", err) |
| 103 | + } |
| 104 | + defer func() { _ = certOut.Close() }() |
| 105 | + |
| 106 | + return generateCertificate(rsaKey, certOut, false) |
| 107 | +} |
| 108 | + |
| 109 | +func main() { |
| 110 | + if err := generateCertAndKey(); err != nil { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + if err := generateMultiCert(); err != nil { |
| 114 | + log.Fatal(err) |
| 115 | + } |
| 116 | +} |
0 commit comments