Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

119 wiersze
3.0 KiB

  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. return
  39. }
  40. var notBefore time.Time
  41. if len(*validFrom) == 0 {
  42. notBefore = time.Now()
  43. } else {
  44. notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom)
  45. if err != nil {
  46. fmt.Fprintf(os.Stderr, "Failed to parse creation date: %s\n", err)
  47. os.Exit(1)
  48. }
  49. }
  50. notAfter := notBefore.Add(*validFor)
  51. // end of ASN.1 time
  52. endOfTime := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
  53. if notAfter.After(endOfTime) {
  54. notAfter = endOfTime
  55. }
  56. template := x509.Certificate{
  57. SerialNumber: new(big.Int).SetInt64(0),
  58. Subject: pkix.Name{
  59. Organization: []string{"Acme Co"},
  60. },
  61. NotBefore: notBefore,
  62. NotAfter: notAfter,
  63. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
  64. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  65. BasicConstraintsValid: true,
  66. }
  67. hosts := strings.Split(*host, ",")
  68. for _, h := range hosts {
  69. if ip := net.ParseIP(h); ip != nil {
  70. template.IPAddresses = append(template.IPAddresses, ip)
  71. } else {
  72. template.DNSNames = append(template.DNSNames, h)
  73. }
  74. }
  75. if *isCA {
  76. template.IsCA = true
  77. template.KeyUsage |= x509.KeyUsageCertSign
  78. }
  79. derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
  80. if err != nil {
  81. log.Fatalf("Failed to create certificate: %s", err)
  82. return
  83. }
  84. certOut, err := os.Create("cert.pem")
  85. if err != nil {
  86. log.Fatalf("failed to open cert.pem for writing: %s", err)
  87. return
  88. }
  89. pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
  90. certOut.Close()
  91. log.Print("written cert.pem\n")
  92. keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  93. if err != nil {
  94. log.Print("failed to open key.pem for writing:", err)
  95. return
  96. }
  97. pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
  98. keyOut.Close()
  99. log.Print("written key.pem\n")
  100. }