main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2018 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. // Command mkcert is a simple zero-config tool to make development certificates.
  5. package main
  6. import (
  7. "crypto"
  8. "crypto/x509"
  9. "flag"
  10. "log"
  11. "net"
  12. "os"
  13. "path/filepath"
  14. "regexp"
  15. "runtime"
  16. )
  17. func main() {
  18. log.SetFlags(0)
  19. var installFlag = flag.Bool("install", false, "install the local root CA in the system trust store")
  20. var uninstallFlag = flag.Bool("uninstall", false, "uninstall the local root CA from the system trust store")
  21. flag.Parse()
  22. if *installFlag && *uninstallFlag {
  23. log.Fatalln("ERROR: you can't set -install and -uninstall at the same time")
  24. }
  25. (&mkcert{
  26. installMode: *installFlag, uninstallMode: *uninstallFlag,
  27. }).Run(flag.Args())
  28. }
  29. const rootName = "rootCA.pem"
  30. const keyName = "rootCA-key.pem"
  31. type mkcert struct {
  32. installMode, uninstallMode bool
  33. CAROOT string
  34. caCert *x509.Certificate
  35. caKey crypto.PrivateKey
  36. // The system cert pool is only loaded once. After installing the root, checks
  37. // will keep failing until the next execution. TODO: maybe execve?
  38. // https://github.com/golang/go/issues/24540 (thanks, myself)
  39. ignoreCheckFailure bool
  40. }
  41. func (m *mkcert) Run(args []string) {
  42. m.CAROOT = getCAROOT()
  43. if m.CAROOT == "" {
  44. log.Fatalln("ERROR: failed to find the default CA location, set one as the CAROOT env var")
  45. }
  46. fatalIfErr(os.MkdirAll(m.CAROOT, 0755), "failed to create the CAROOT")
  47. m.loadCA()
  48. if m.installMode {
  49. m.install()
  50. if len(args) == 0 {
  51. return
  52. }
  53. } else if m.uninstallMode {
  54. m.uninstall()
  55. return
  56. } else if !m.check() {
  57. log.Println("Warning: the local CA is not installed in the system trust store! ⚠️")
  58. log.Println("Run \"mkcert -install\" to avoid verification errors ‼️")
  59. }
  60. if len(args) == 0 {
  61. log.Printf(`
  62. Usage:
  63. $ mkcert -install
  64. Install the local CA in the system trust store.
  65. $ mkcert example.org
  66. Generate "example.org.pem" and "example.org-key.pem".
  67. $ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
  68. Generate "example.com+4.pem" and "example.com+4-key.pem".
  69. $ mkcert '*.example.com'
  70. Generate "_wildcard.example.com.pem" and "_wildcard.example.com-key.pem".
  71. $ mkcert -uninstall
  72. Unnstall the local CA (but do not delete it).
  73. Change the CA certificate and key storage location by setting $CAROOT.
  74. `)
  75. return
  76. }
  77. hostnameRegexp := regexp.MustCompile(`(?i)^(\*\.)?[0-9a-z_-]([0-9a-z._-]*[0-9a-z_-])?$`)
  78. for _, name := range args {
  79. if ip := net.ParseIP(name); ip != nil {
  80. continue
  81. }
  82. if hostnameRegexp.MatchString(name) {
  83. continue
  84. }
  85. log.Fatalf("ERROR: %q is not a valid hostname or IP", name)
  86. }
  87. m.makeCert(args)
  88. }
  89. func getCAROOT() string {
  90. if env := os.Getenv("CAROOT"); env != "" {
  91. return env
  92. }
  93. var dir string
  94. switch runtime.GOOS {
  95. case "windows":
  96. dir = os.Getenv("LocalAppData")
  97. case "darwin":
  98. dir = os.Getenv("HOME")
  99. if dir == "" {
  100. return ""
  101. }
  102. dir = filepath.Join(dir, "Library", "Application Support")
  103. default: // Unix
  104. dir = os.Getenv("XDG_DATA_HOME")
  105. if dir == "" {
  106. dir = os.Getenv("HOME")
  107. if dir == "" {
  108. return ""
  109. }
  110. dir = filepath.Join(dir, ".local", "share")
  111. }
  112. }
  113. return filepath.Join(dir, "mkcert")
  114. }
  115. func (m *mkcert) install() {
  116. if m.check() {
  117. return
  118. }
  119. m.installPlatform()
  120. m.ignoreCheckFailure = true
  121. if m.check() { // useless, see comment on ignoreCheckFailure
  122. log.Print("The local CA is now installed in the system trust store! ⚡️\n\n")
  123. } else {
  124. log.Fatal("Installing failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎\n\n")
  125. }
  126. }
  127. func (m *mkcert) uninstall() {
  128. m.uninstallPlatform()
  129. log.Print("The local CA is now uninstalled from the system trust store! 👋\n\n")
  130. }
  131. func (m *mkcert) check() bool {
  132. if m.ignoreCheckFailure {
  133. return true
  134. }
  135. _, err := m.caCert.Verify(x509.VerifyOptions{})
  136. return err == nil
  137. }
  138. func fatalIfErr(err error, msg string) {
  139. if err != nil {
  140. log.Fatalf("ERROR: %s: %s", msg, err)
  141. }
  142. }