truststore_nss.go 4.1 KB

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