truststore_nss.go 4.0 KB

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