truststore_nss.go 2.8 KB

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