truststore_nss.go 3.0 KB

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