| 29 | } |
| 30 | |
| 31 | func newCertAuthority() (*certAuthority, error) { |
| 32 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | serial, err := randomSerial() |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | now := time.Now() |
| 41 | tmpl := &x509.Certificate{ |
| 42 | SerialNumber: serial, |
| 43 | Subject: pkix.Name{CommonName: "ghmock throwaway certificate authority"}, |
| 44 | NotBefore: now.Add(-time.Hour), |
| 45 | NotAfter: now.Add(24 * time.Hour), |
| 46 | IsCA: true, |
| 47 | BasicConstraintsValid: true, |
| 48 | MaxPathLenZero: true, |
| 49 | KeyUsage: x509.KeyUsageCertSign, |
| 50 | } |
| 51 | der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | cert, err := x509.ParseCertificate(der) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | return &certAuthority{ |
| 60 | cert: cert, |
| 61 | key: key, |
| 62 | certPEM: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), |
| 63 | leaves: map[string]*tls.Certificate{}, |
| 64 | }, nil |
| 65 | } |
| 66 | |
| 67 | // randomSerial returns a random serial number, so certificates from different |
| 68 | // runs never share one. |