Debian Codegraph @

internal/truststore_java.go

raw symbols SCIP
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
5 package internal
6
7 import (
8 "bytes"
9 "crypto/sha1"
10 "crypto/sha256"
11 "crypto/x509"
12 "encoding/hex"
13 "hash"
14 "os"
15 "os/exec"
16 "path/filepath"
17 "runtime"
18 "strings"
19 )
20
21 var (
22 hasJava bool
23 hasKeytool bool
24
25 javaHome string
26 cacertsPath string
27 keytoolPath string
28 storePass string = "changeit"
29 )
30
31 func init() {
32 if runtime.GOOS == "windows" {
33 keytoolPath = filepath.Join("bin", "keytool.exe")
34 } else {
35 keytoolPath = filepath.Join("bin", "keytool")
36 }
37
38 if v := os.Getenv("JAVA_HOME"); v != "" {
39 hasJava = true
40 javaHome = v
41
42 if pathExists(filepath.Join(v, keytoolPath)) {
43 hasKeytool = true
44 keytoolPath = filepath.Join(v, keytoolPath)
45 }
46
47 if pathExists(filepath.Join(v, "lib", "security", "cacerts")) {
48 cacertsPath = filepath.Join(v, "lib", "security", "cacerts")
49 }
50
51 if pathExists(filepath.Join(v, "jre", "lib", "security", "cacerts")) {
52 cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts")
53 }
54 }
55 }
56
57 func (m *mkcert) checkJava() bool {
58 if !hasKeytool {
59 return false
60 }
61
62 // exists returns true if the given x509.Certificate's fingerprint
63 // is in the keytool -list output
64 exists := func(c *x509.Certificate, h hash.Hash, keytoolOutput []byte) bool {
65 h.Write(c.Raw)
66 fp := strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
67 return bytes.Contains(keytoolOutput, []byte(fp))
68 }
69
70 keytoolOutput, err := exec.Command(keytoolPath, "-list", "-keystore", cacertsPath, "-storepass", storePass).CombinedOutput()
71 fatalIfCmdErr(err, "keytool -list", keytoolOutput)
72 // keytool outputs SHA1 and SHA256 (Java 9+) certificates in uppercase hex
73 // with each octet pair delimitated by ":". Drop them from the keytool output
74 keytoolOutput = bytes.Replace(keytoolOutput, []byte(":"), nil, -1)
75
76 // pre-Java 9 uses SHA1 fingerprints
77 s1, s256 := sha1.New(), sha256.New()
78 return exists(m.caCert, s1, keytoolOutput) || exists(m.caCert, s256, keytoolOutput)
79 }
80
81 func (m *mkcert) installJava() {
82 args := []string{
83 "-importcert", "-noprompt",
84 "-keystore", cacertsPath,
85 "-storepass", storePass,
86 "-file", filepath.Join(m.CAROOT, rootName),
87 "-alias", m.caUniqueName(),
88 }
89
90 out, err := execKeytool(exec.Command(keytoolPath, args...))
91 fatalIfCmdErr(err, "keytool -importcert", out)
92 }
93
94 func (m *mkcert) uninstallJava() {
95 args := []string{
96 "-delete",
97 "-alias", m.caUniqueName(),
98 "-keystore", cacertsPath,
99 "-storepass", storePass,
100 }
101 out, err := execKeytool(exec.Command(keytoolPath, args...))
102 if bytes.Contains(out, []byte("does not exist")) {
103 return // cert didn't exist
104 }
105 fatalIfCmdErr(err, "keytool -delete", out)
106 }
107
108 // execKeytool will execute a "keytool" command and if needed re-execute
109 // the command with commandWithSudo to work around file permissions.
110 func execKeytool(cmd *exec.Cmd) ([]byte, error) {
111 out, err := cmd.CombinedOutput()
112 if err != nil && bytes.Contains(out, []byte("java.io.FileNotFoundException")) && runtime.GOOS != "windows" {
113 origArgs := cmd.Args[1:]
114 cmd = commandWithSudo(cmd.Path)
115 cmd.Args = append(cmd.Args, origArgs...)
116 cmd.Env = []string{
117 "JAVA_HOME=" + javaHome,
118 }
119 out, err = cmd.CombinedOutput()
120 }
121 return out, err
122 }