newKeyPair returns a freshly generated RSA key and a matching self-signed certificate. The example uses a throwaway key so that it never ships a private key or a certificate that can expire.
()
| 197 | // certificate. The example uses a throwaway key so that it never ships a |
| 198 | // private key or a certificate that can expire. |
| 199 | func newKeyPair() (*rsa.PrivateKey, *x509.Certificate, error) { |
| 200 | key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 201 | if err != nil { |
| 202 | return nil, nil, err |
| 203 | } |
| 204 | template := &x509.Certificate{ |
| 205 | SerialNumber: big.NewInt(1), |
| 206 | Subject: pkix.Name{CommonName: "example service"}, |
| 207 | NotBefore: time.Now().Add(-time.Hour), |
| 208 | NotAfter: time.Now().Add(365 * 24 * time.Hour), |
| 209 | KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 210 | } |
| 211 | der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) |
| 212 | if err != nil { |
| 213 | return nil, nil, err |
| 214 | } |
| 215 | cert, err := x509.ParseCertificate(der) |
| 216 | if err != nil { |
| 217 | return nil, nil, err |
| 218 | } |
| 219 | return key, cert, nil |
| 220 | } |
| 221 | |
| 222 | func main() { |
| 223 | rootURLstr := flag.String("url", "http://localhost:8000", "The base URL of this service") |