truststore_nss.go 2.9 KB

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