truststore_nss.go 4.0 KB

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