truststore_nss.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2018 The mkcert 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. package main
  5. import (
  6. "log"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. )
  13. var (
  14. hasNSS bool
  15. hasCertutil bool
  16. certutilPath string
  17. nssDBs = []string{
  18. filepath.Join(os.Getenv("HOME"), ".pki/nssdb"),
  19. filepath.Join(os.Getenv("HOME"), "snap/chromium/current/.pki/nssdb"), // Snapcraft
  20. "/etc/pki/nssdb", // CentOS 7
  21. }
  22. firefoxPaths = []string{
  23. "/usr/bin/firefox", "/Applications/Firefox.app",
  24. "/Applications/Firefox Developer Edition.app",
  25. "/Applications/Firefox Nightly.app",
  26. "C:\\Program Files\\Mozilla Firefox",
  27. }
  28. )
  29. func init() {
  30. allPaths := append(append([]string{}, nssDBs...), firefoxPaths...)
  31. for _, path := range allPaths {
  32. if pathExists(path) {
  33. hasNSS = true
  34. break
  35. }
  36. }
  37. switch runtime.GOOS {
  38. case "darwin":
  39. switch {
  40. case binaryExists("certutil"):
  41. certutilPath, _ = exec.LookPath("certutil")
  42. hasCertutil = true
  43. case binaryExists("/usr/local/opt/nss/bin/certutil"):
  44. // Check the default Homebrew path, to save executing Ruby. #135
  45. certutilPath = "/usr/local/opt/nss/bin/certutil"
  46. hasCertutil = true
  47. default:
  48. out, err := exec.Command("brew", "--prefix", "nss").Output()
  49. if err == nil {
  50. certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
  51. hasCertutil = pathExists(certutilPath)
  52. }
  53. }
  54. case "linux":
  55. if hasCertutil = binaryExists("certutil"); hasCertutil {
  56. certutilPath, _ = exec.LookPath("certutil")
  57. }
  58. }
  59. }
  60. func (m *mkcert) checkNSS() bool {
  61. if !hasCertutil {
  62. return false
  63. }
  64. success := true
  65. if m.forEachNSSProfile(func(profile string) {
  66. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  67. if err != nil {
  68. success = false
  69. }
  70. }) == 0 {
  71. success = false
  72. }
  73. return success
  74. }
  75. func (m *mkcert) installNSS() bool {
  76. if m.forEachNSSProfile(func(profile string) {
  77. cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
  78. out, err := cmd.CombinedOutput()
  79. fatalIfCmdErr(err, "certutil -A", out)
  80. }) == 0 {
  81. log.Printf("ERROR: no %s security databases found", NSSBrowsers)
  82. return false
  83. }
  84. if !m.checkNSS() {
  85. log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
  86. log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
  87. return false
  88. }
  89. return true
  90. }
  91. func (m *mkcert) uninstallNSS() {
  92. m.forEachNSSProfile(func(profile string) {
  93. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  94. if err != nil {
  95. return
  96. }
  97. cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
  98. out, err := cmd.CombinedOutput()
  99. fatalIfCmdErr(err, "certutil -D", out)
  100. })
  101. }
  102. func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
  103. profiles, _ := filepath.Glob(FirefoxProfile)
  104. profiles = append(profiles, nssDBs...)
  105. for _, profile := range profiles {
  106. if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
  107. continue
  108. }
  109. if pathExists(filepath.Join(profile, "cert9.db")) {
  110. f("sql:" + profile)
  111. found++
  112. } else if pathExists(filepath.Join(profile, "cert8.db")) {
  113. f("dbm:" + profile)
  114. found++
  115. }
  116. }
  117. return
  118. }