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