Skip to content
Snippets Groups Projects
Commit 3faac668 authored by Mariano Cano's avatar Mariano Cano
Browse files

Add initial support for java.

parent ad5f4a56
No related branches found
No related tags found
No related merge requests found
...@@ -58,8 +58,12 @@ func InstallFile(filename string, opts ...Option) error { ...@@ -58,8 +58,12 @@ func InstallFile(filename string, opts ...Option) error {
func installCertificate(filename string, cert *x509.Certificate, opts []Option) error { func installCertificate(filename string, cert *x509.Certificate, opts []Option) error {
o := newOptions(opts) o := newOptions(opts)
if o.withJava { if o.withJava && hasJava {
if !checkJava(cert) {
if err := installJava(filename, cert); err != nil {
return err
}
}
} }
if o.withFirefox && hasNSS() { if o.withFirefox && hasNSS() {
if !checkNSS(cert) { if !checkNSS(cert) {
...@@ -99,7 +103,9 @@ func UninstallFile(filename string, opts ...Option) error { ...@@ -99,7 +103,9 @@ func UninstallFile(filename string, opts ...Option) error {
func uninstallCertificate(filename string, cert *x509.Certificate, opts []Option) error { func uninstallCertificate(filename string, cert *x509.Certificate, opts []Option) error {
o := newOptions(opts) o := newOptions(opts)
if o.withJava { if o.withJava {
if err := uninstallJava(filename, cert); err != nil {
return err
}
} }
if o.withFirefox && checkNSS(cert) { if o.withFirefox && checkNSS(cert) {
if err := uninstallNSS(filename, cert); err != nil { if err := uninstallNSS(filename, cert); err != nil {
......
// +build ignore // Copyright (c) 2018 The truststore Authors. All rights reserved.
// Copyright 2018 The Go Authors. All rights reserved. // Copyright (c) 2018 The mkcert Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package truststore package truststore
...@@ -55,10 +53,12 @@ func init() { ...@@ -55,10 +53,12 @@ func init() {
if err == nil { if err == nil {
cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts") cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts")
} }
println(cacertsPath)
} }
} }
func (m *mkcert) checkJava() bool { func checkJava(cert *x509.Certificate) bool {
if !hasKeytool { if !hasKeytool {
return false return false
} }
...@@ -72,46 +72,57 @@ func (m *mkcert) checkJava() bool { ...@@ -72,46 +72,57 @@ func (m *mkcert) checkJava() bool {
} }
keytoolOutput, err := exec.Command(keytoolPath, "-list", "-keystore", cacertsPath, "-storepass", storePass).CombinedOutput() keytoolOutput, err := exec.Command(keytoolPath, "-list", "-keystore", cacertsPath, "-storepass", storePass).CombinedOutput()
fatalIfCmdErr(err, "keytool -list", keytoolOutput) if err != nil {
debug("failed to execute \"keytool -list\": %s\n\n%s", err, keytoolOutput)
return false
}
// keytool outputs SHA1 and SHA256 (Java 9+) certificates in uppercase hex // keytool outputs SHA1 and SHA256 (Java 9+) certificates in uppercase hex
// with each octet pair delimitated by ":". Drop them from the keytool output // with each octet pair delimitated by ":". Drop them from the keytool output
keytoolOutput = bytes.Replace(keytoolOutput, []byte(":"), nil, -1) keytoolOutput = bytes.Replace(keytoolOutput, []byte(":"), nil, -1)
// pre-Java 9 uses SHA1 fingerprints // pre-Java 9 uses SHA1 fingerprints
s1, s256 := sha1.New(), sha256.New() s1, s256 := sha1.New(), sha256.New()
return exists(m.caCert, s1, keytoolOutput) || exists(m.caCert, s256, keytoolOutput) return exists(cert, s1, keytoolOutput) || exists(cert, s256, keytoolOutput)
} }
func (m *mkcert) installJava() { func installJava(filename string, cert *x509.Certificate) error {
args := []string{ args := []string{
"-importcert", "-noprompt", "-importcert", "-noprompt",
"-keystore", cacertsPath, "-keystore", cacertsPath,
"-storepass", storePass, "-storepass", storePass,
"-file", filepath.Join(m.CAROOT, rootName), "-file", filename,
"-alias", m.caUniqueName(), "-alias", uniqueName(cert),
} }
out, err := m.execKeytool(exec.Command(keytoolPath, args...)) out, err := execKeytool(exec.Command(keytoolPath, args...))
fatalIfCmdErr(err, "keytool -importcert", out) if err != nil {
return cmdError(err, "keytool -importcert", out)
}
return nil
} }
func (m *mkcert) uninstallJava() { func uninstallJava(filename string, cert *x509.Certificate) error {
args := []string{ args := []string{
"-delete", "-delete",
"-alias", m.caUniqueName(), "-alias", uniqueName(cert),
"-keystore", cacertsPath, "-keystore", cacertsPath,
"-storepass", storePass, "-storepass", storePass,
} }
out, err := m.execKeytool(exec.Command(keytoolPath, args...)) out, err := execKeytool(exec.Command(keytoolPath, args...))
if bytes.Contains(out, []byte("does not exist")) { if bytes.Contains(out, []byte("does not exist")) {
return // cert didn't exist return nil
}
if err != nil {
cmdError(err, "keytool -delete", out)
} }
fatalIfCmdErr(err, "keytool -delete", out) return nil
} }
// execKeytool will execute a "keytool" command and if needed re-execute // execKeytool will execute a "keytool" command and if needed re-execute
// the command wrapped in 'sudo' to work around file permissions. // the command wrapped in 'sudo' to work around file permissions.
func (m *mkcert) execKeytool(cmd *exec.Cmd) ([]byte, error) { func execKeytool(cmd *exec.Cmd) ([]byte, error) {
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(out, []byte("java.io.FileNotFoundException")) && runtime.GOOS != "windows" { if err != nil && bytes.Contains(out, []byte("java.io.FileNotFoundException")) && runtime.GOOS != "windows" {
origArgs := cmd.Args[1:] origArgs := cmd.Args[1:]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment