truststore_nss.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if hasCertutil = binaryExists("certutil"); hasCertutil {
  40. certutilPath, _ = exec.LookPath("certutil")
  41. } else {
  42. out, err := exec.Command("brew", "--prefix", "nss").Output()
  43. if err == nil {
  44. certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
  45. hasCertutil = pathExists(certutilPath)
  46. }
  47. }
  48. case "linux":
  49. if hasCertutil = binaryExists("certutil"); hasCertutil {
  50. certutilPath, _ = exec.LookPath("certutil")
  51. }
  52. }
  53. }
  54. func (m *mkcert) checkNSS() bool {
  55. if !hasCertutil {
  56. return false
  57. }
  58. success := true
  59. if m.forEachNSSProfile(func(profile string) {
  60. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  61. if err != nil {
  62. success = false
  63. }
  64. }) == 0 {
  65. success = false
  66. }
  67. return success
  68. }
  69. func (m *mkcert) installNSS() bool {
  70. if m.forEachNSSProfile(func(profile string) {
  71. cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
  72. out, err := cmd.CombinedOutput()
  73. fatalIfCmdErr(err, "certutil -A", out)
  74. }) == 0 {
  75. log.Printf("ERROR: no %s security databases found", NSSBrowsers)
  76. return false
  77. }
  78. if !m.checkNSS() {
  79. log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
  80. log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
  81. return false
  82. }
  83. return true
  84. }
  85. func (m *mkcert) uninstallNSS() {
  86. m.forEachNSSProfile(func(profile string) {
  87. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  88. if err != nil {
  89. return
  90. }
  91. cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
  92. out, err := cmd.CombinedOutput()
  93. fatalIfCmdErr(err, "certutil -D", out)
  94. })
  95. }
  96. func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
  97. profiles, _ := filepath.Glob(FirefoxProfile)
  98. profiles = append(profiles, nssDBs...)
  99. for _, profile := range profiles {
  100. if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
  101. continue
  102. }
  103. if pathExists(filepath.Join(profile, "cert9.db")) {
  104. f("sql:" + profile)
  105. found++
  106. } else if pathExists(filepath.Join(profile, "cert8.db")) {
  107. f("dbm:" + profile)
  108. found++
  109. }
  110. }
  111. return
  112. }