You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_cert.go 3.2 KiB

log: new interface New logging interface simplifies and generalizes. 1) Loggers now have only one output. 2) log.Stdout, Stderr, Crash and friends are gone. Logging is now always to standard error by default. 3) log.Panic* replaces log.Crash*. 4) Exiting and panicking are not part of the logger's state; instead the functions Exit* and Panic* simply call Exit or panic after printing. 5) There is now one 'standard logger'. Instead of calling Stderr, use Print etc. There are now triples, by analogy with fmt: Print, Println, Printf What was log.Stderr is now best represented by log.Println, since there are now separate Print and Println functions (and methods). 6) New functions SetOutput, SetFlags, and SetPrefix allow global editing of the standard logger's properties. This is new functionality. For instance, one can call log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds) to get all logging output to show file name, line number, and time stamp. In short, for most purposes log.Stderr -> log.Println or log.Print log.Stderrf -> log.Printf log.Crash -> log.Panicln or log.Panic log.Crashf -> log.Panicf log.Exit -> log.Exitln or log.Exit log.Exitf -> log.Exitf (no change) This has a slight breakage: since loggers now write only to one output, existing calls to log.New() need to delete the second argument. Also, custom loggers with exit or panic properties will need to be reworked. All package code updated to new interface. The test has been reworked somewhat. The old interface will be removed after the new release. For now, its elements are marked 'deprecated' in their comments. Fixes #1184. R=rsc CC=golang-dev https://golang.org/cl/2419042
14 years ago
log: new interface New logging interface simplifies and generalizes. 1) Loggers now have only one output. 2) log.Stdout, Stderr, Crash and friends are gone. Logging is now always to standard error by default. 3) log.Panic* replaces log.Crash*. 4) Exiting and panicking are not part of the logger's state; instead the functions Exit* and Panic* simply call Exit or panic after printing. 5) There is now one 'standard logger'. Instead of calling Stderr, use Print etc. There are now triples, by analogy with fmt: Print, Println, Printf What was log.Stderr is now best represented by log.Println, since there are now separate Print and Println functions (and methods). 6) New functions SetOutput, SetFlags, and SetPrefix allow global editing of the standard logger's properties. This is new functionality. For instance, one can call log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds) to get all logging output to show file name, line number, and time stamp. In short, for most purposes log.Stderr -> log.Println or log.Print log.Stderrf -> log.Printf log.Crash -> log.Panicln or log.Panic log.Crashf -> log.Panicf log.Exit -> log.Exitln or log.Exit log.Exitf -> log.Exitf (no change) This has a slight breakage: since loggers now write only to one output, existing calls to log.New() need to delete the second argument. Also, custom loggers with exit or panic properties will need to be reworked. All package code updated to new interface. The test has been reworked somewhat. The old interface will be removed after the new release. For now, its elements are marked 'deprecated' in their comments. Fixes #1184. R=rsc CC=golang-dev https://golang.org/cl/2419042
14 years ago
log: new interface New logging interface simplifies and generalizes. 1) Loggers now have only one output. 2) log.Stdout, Stderr, Crash and friends are gone. Logging is now always to standard error by default. 3) log.Panic* replaces log.Crash*. 4) Exiting and panicking are not part of the logger's state; instead the functions Exit* and Panic* simply call Exit or panic after printing. 5) There is now one 'standard logger'. Instead of calling Stderr, use Print etc. There are now triples, by analogy with fmt: Print, Println, Printf What was log.Stderr is now best represented by log.Println, since there are now separate Print and Println functions (and methods). 6) New functions SetOutput, SetFlags, and SetPrefix allow global editing of the standard logger's properties. This is new functionality. For instance, one can call log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds) to get all logging output to show file name, line number, and time stamp. In short, for most purposes log.Stderr -> log.Println or log.Print log.Stderrf -> log.Printf log.Crash -> log.Panicln or log.Panic log.Crashf -> log.Panicf log.Exit -> log.Exitln or log.Exit log.Exitf -> log.Exitf (no change) This has a slight breakage: since loggers now write only to one output, existing calls to log.New() need to delete the second argument. Also, custom loggers with exit or panic properties will need to be reworked. All package code updated to new interface. The test has been reworked somewhat. The old interface will be removed after the new release. For now, its elements are marked 'deprecated' in their comments. Fixes #1184. R=rsc CC=golang-dev https://golang.org/cl/2419042
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  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.
  7. package main
  8. import (
  9. "crypto/rand"
  10. "crypto/rsa"
  11. "crypto/x509"
  12. "crypto/x509/pkix"
  13. "encoding/pem"
  14. "flag"
  15. "fmt"
  16. "log"
  17. "math/big"
  18. "net"
  19. "os"
  20. "strings"
  21. "time"
  22. )
  23. var (
  24. host = flag.String("host", "", "Comma-separated hostnames and IPs to generate a certificate for")
  25. validFrom = flag.String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011")
  26. validFor = flag.Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
  27. isCA = flag.Bool("ca", false, "whether this cert should be its own Certificate Authority")
  28. rsaBits = flag.Int("rsa-bits", 2048, "Size of RSA key to generate")
  29. )
  30. func main() {
  31. flag.Parse()
  32. if len(*host) == 0 {
  33. log.Fatalf("Missing required --host parameter")
  34. }
  35. priv, err := rsa.GenerateKey(rand.Reader, *rsaBits)
  36. if err != nil {
  37. log.Fatalf("failed to generate private key: %s", err)
  38. }
  39. var notBefore time.Time
  40. if len(*validFrom) == 0 {
  41. notBefore = time.Now()
  42. } else {
  43. notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom)
  44. if err != nil {
  45. fmt.Fprintf(os.Stderr, "Failed to parse creation date: %s\n", err)
  46. os.Exit(1)
  47. }
  48. }
  49. notAfter := notBefore.Add(*validFor)
  50. // end of ASN.1 time
  51. endOfTime := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
  52. if notAfter.After(endOfTime) {
  53. notAfter = endOfTime
  54. }
  55. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  56. serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  57. if err != nil {
  58. log.Fatalf("failed to generate serial number: %s", err)
  59. }
  60. template := x509.Certificate{
  61. SerialNumber: serialNumber,
  62. Subject: pkix.Name{
  63. Organization: []string{"Acme Co"},
  64. },
  65. NotBefore: notBefore,
  66. NotAfter: notAfter,
  67. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
  68. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  69. BasicConstraintsValid: true,
  70. }
  71. hosts := strings.Split(*host, ",")
  72. for _, h := range hosts {
  73. if ip := net.ParseIP(h); ip != nil {
  74. template.IPAddresses = append(template.IPAddresses, ip)
  75. } else {
  76. template.DNSNames = append(template.DNSNames, h)
  77. }
  78. }
  79. if *isCA {
  80. template.IsCA = true
  81. template.KeyUsage |= x509.KeyUsageCertSign
  82. }
  83. derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
  84. if err != nil {
  85. log.Fatalf("Failed to create certificate: %s", err)
  86. }
  87. certOut, err := os.Create("cert.pem")
  88. if err != nil {
  89. log.Fatalf("failed to open cert.pem for writing: %s", err)
  90. }
  91. pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
  92. certOut.Close()
  93. log.Print("written cert.pem\n")
  94. keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  95. if err != nil {
  96. log.Print("failed to open key.pem for writing:", err)
  97. return
  98. }
  99. pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
  100. keyOut.Close()
  101. log.Print("written key.pem\n")
  102. }