LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Generate a self-signed X.509 certificate for a TLS server. Outputs to | 5 // Generate a self-signed X.509 certificate for a TLS server. Outputs to |
6 // 'cert.pem' and 'key.pem' and will overwrite existing files. | 6 // 'cert.pem' and 'key.pem' and will overwrite existing files. |
7 | 7 |
8 package main | 8 package main |
9 | 9 |
10 import ( | 10 import ( |
| 11 "big" |
| 12 "crypto/x509/pkix" |
| 13 "crypto/rand" |
11 "crypto/rsa" | 14 "crypto/rsa" |
12 "crypto/rand" | |
13 "crypto/x509" | 15 "crypto/x509" |
14 "encoding/pem" | 16 "encoding/pem" |
15 "flag" | 17 "flag" |
16 "log" | 18 "log" |
17 "os" | 19 "os" |
18 "time" | 20 "time" |
19 ) | 21 ) |
20 | 22 |
21 var hostName *string = flag.String("host", "127.0.0.1", "Hostname to generate a
certificate for") | 23 var hostName *string = flag.String("host", "127.0.0.1", "Hostname to generate a
certificate for") |
22 | 24 |
23 func main() { | 25 func main() { |
24 flag.Parse() | 26 flag.Parse() |
25 | 27 |
26 priv, err := rsa.GenerateKey(rand.Reader, 1024) | 28 priv, err := rsa.GenerateKey(rand.Reader, 1024) |
27 if err != nil { | 29 if err != nil { |
28 log.Fatalf("failed to generate private key: %s", err) | 30 log.Fatalf("failed to generate private key: %s", err) |
29 return | 31 return |
30 } | 32 } |
31 | 33 |
32 now := time.Seconds() | 34 now := time.Seconds() |
33 | 35 |
34 template := x509.Certificate{ | 36 template := x509.Certificate{ |
35 » » SerialNumber: []byte{0}, | 37 » » SerialNumber: new(big.Int).SetInt64(0), |
36 » » Subject: x509.Name{ | 38 » » Subject: pkix.Name{ |
37 CommonName: *hostName, | 39 CommonName: *hostName, |
38 Organization: []string{"Acme Co"}, | 40 Organization: []string{"Acme Co"}, |
39 }, | 41 }, |
40 NotBefore: time.SecondsToUTC(now - 300), | 42 NotBefore: time.SecondsToUTC(now - 300), |
41 NotAfter: time.SecondsToUTC(now + 60*60*24*365), // valid for 1
year. | 43 NotAfter: time.SecondsToUTC(now + 60*60*24*365), // valid for 1
year. |
42 | 44 |
43 SubjectKeyId: []byte{1, 2, 3, 4}, | 45 SubjectKeyId: []byte{1, 2, 3, 4}, |
44 KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigita
lSignature, | 46 KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigita
lSignature, |
45 } | 47 } |
46 | 48 |
(...skipping 14 matching lines...) Expand all Loading... |
61 | 63 |
62 keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC
, 0600) | 64 keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC
, 0600) |
63 if err != nil { | 65 if err != nil { |
64 log.Print("failed to open key.pem for writing:", err) | 66 log.Print("failed to open key.pem for writing:", err) |
65 return | 67 return |
66 } | 68 } |
67 pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.Marsh
alPKCS1PrivateKey(priv)}) | 69 pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.Marsh
alPKCS1PrivateKey(priv)}) |
68 keyOut.Close() | 70 keyOut.Close() |
69 log.Print("written key.pem\n") | 71 log.Print("written key.pem\n") |
70 } | 72 } |
LEFT | RIGHT |