truststore_nss.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2018 The mkcert Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. )
  14. var (
  15. hasNSS bool
  16. hasCertutil bool
  17. certutilPath string
  18. nssDBs = []string{
  19. filepath.Join(os.Getenv("HOME"), ".pki/nssdb"),
  20. filepath.Join(os.Getenv("HOME"), "snap/chromium/current/.pki/nssdb"), // Snapcraft
  21. "/etc/pki/nssdb", // CentOS 7
  22. }
  23. firefoxPaths = []string{
  24. "/usr/bin/firefox", "/Applications/Firefox.app",
  25. "/Applications/Firefox Developer Edition.app",
  26. "/Applications/Firefox Nightly.app",
  27. "C:\\Program Files\\Mozilla Firefox",
  28. }
  29. )
  30. func init() {
  31. allPaths := append(append([]string{}, nssDBs...), firefoxPaths...)
  32. for _, path := range allPaths {
  33. if pathExists(path) {
  34. hasNSS = true
  35. break
  36. }
  37. }
  38. switch runtime.GOOS {
  39. case "darwin":
  40. switch {
  41. case binaryExists("certutil"):
  42. certutilPath, _ = exec.LookPath("certutil")
  43. hasCertutil = true
  44. case binaryExists("/usr/local/opt/nss/bin/certutil"):
  45. // Check the default Homebrew path, to save executing Ruby. #135
  46. certutilPath = "/usr/local/opt/nss/bin/certutil"
  47. hasCertutil = true
  48. default:
  49. out, err := exec.Command("brew", "--prefix", "nss").Output()
  50. if err == nil {
  51. certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
  52. hasCertutil = pathExists(certutilPath)
  53. }
  54. }
  55. case "linux":
  56. if hasCertutil = binaryExists("certutil"); hasCertutil {
  57. certutilPath, _ = exec.LookPath("certutil")
  58. }
  59. }
  60. }
  61. func (m *mkcert) checkNSS() bool {
  62. if !hasCertutil {
  63. return false
  64. }
  65. success := true
  66. if m.forEachNSSProfile(func(profile string) {
  67. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  68. if err != nil {
  69. success = false
  70. }
  71. }) == 0 {
  72. success = false
  73. }
  74. return success
  75. }
  76. func (m *mkcert) installNSS() bool {
  77. if m.forEachNSSProfile(func(profile string) {
  78. cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
  79. out, err := execCertutil(cmd)
  80. fatalIfCmdErr(err, "certutil -A -d "+profile, out)
  81. }) == 0 {
  82. log.Printf("ERROR: no %s security databases found", NSSBrowsers)
  83. return false
  84. }
  85. if !m.checkNSS() {
  86. log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
  87. log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
  88. return false
  89. }
  90. return true
  91. }
  92. func (m *mkcert) uninstallNSS() {
  93. m.forEachNSSProfile(func(profile string) {
  94. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  95. if err != nil {
  96. return
  97. }
  98. cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
  99. out, err := execCertutil(cmd)
  100. fatalIfCmdErr(err, "certutil -D -d "+profile, out)
  101. })
  102. }
  103. // execCertutil will execute a "certutil" command and if needed re-execute
  104. // the command with commandWithSudo to work around file permissions.
  105. func execCertutil(cmd *exec.Cmd) ([]byte, error) {
  106. out, err := cmd.CombinedOutput()
  107. if err != nil && bytes.Contains(out, []byte("SEC_ERROR_READ_ONLY")) && runtime.GOOS != "windows" {
  108. origArgs := cmd.Args[1:]
  109. cmd = commandWithSudo(cmd.Path)
  110. cmd.Args = append(cmd.Args, origArgs...)
  111. out, err = cmd.CombinedOutput()
  112. }
  113. return out, err
  114. }
  115. func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
  116. profiles, _ := filepath.Glob(FirefoxProfile)
  117. profiles = append(profiles, nssDBs...)
  118. for _, profile := range profiles {
  119. if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
  120. continue
  121. }
  122. if pathExists(filepath.Join(profile, "cert9.db")) {
  123. f("sql:" + profile)
  124. found++
  125. } else if pathExists(filepath.Join(profile, "cert8.db")) {
  126. f("dbm:" + profile)
  127. found++
  128. }
  129. }
  130. return
  131. }