Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1153)

Delta Between Two Patch Sets: src/pkg/crypto/tls/generate_cert.go

Issue 4635092: code review 4635092: crypto/tls: fix generate_cert.go (Closed)
Left Patch Set: Created 13 years, 8 months ago
Right Patch Set: diff -r 332835d5b61e https://go.googlecode.com/hg/ Created 13 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(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
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 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b