truststore_nss.go 2.9 KB

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