| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package webpagereplay | 5 package webpagereplay |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/tls" |
| 8 "fmt" | 9 "fmt" |
| 9 "os" | 10 "os" |
| 10 "os/exec" | 11 "os/exec" |
| 11 "path/filepath" | 12 "path/filepath" |
| 12 "runtime" | 13 "runtime" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 func getCAName() string { | 16 func getCAName() string { |
| 16 return "wpr-local" | 17 return "wpr-local" |
| 17 } | 18 } |
| 18 func getDbPath() string { | 19 func getDbPath() string { |
| 19 return "sql:" + filepath.Join(os.Getenv("HOME"), ".pki/nssdb") | 20 return "sql:" + filepath.Join(os.Getenv("HOME"), ".pki/nssdb") |
| 20 } | 21 } |
| 21 | 22 |
| 22 // TODO: Implement root CA installation for platforms other than Linux. | 23 // TODO: Implement root CA installation for platforms other than Linux and Andro
id. |
| 23 func InstallRoot(derBytes []byte) error { | 24 func (i *Installer) InstallRoot(certFile string, keyFile string) error { |
| 24 if runtime.GOOS != "linux" { | 25 if runtime.GOOS != "linux" { |
| 25 fmt.Printf("Root certificate is skipped for %s\n", runtime.GOOS) | 26 fmt.Printf("Root certificate is skipped for %s\n", runtime.GOOS) |
| 26 return nil | 27 return nil |
| 27 } | 28 } |
| 29 if i.AndroidDeviceId != "" { |
| 30 if runtime.GOOS != "linux" { |
| 31 return fmt.Errorf("test root CA for Android is only supp
orted on a Linux host machine") |
| 32 } |
| 33 fmt.Println("Installing test root CA on Android...") |
| 34 return i.AdbInstallRoot(certFile) |
| 35 } |
| 36 fmt.Printf("Loading cert from %v\n", certFile) |
| 37 fmt.Printf("Loading key from %v\n", keyFile) |
| 38 rootCert, err := tls.LoadX509KeyPair(certFile, keyFile) |
| 39 if err != nil { |
| 40 return fmt.Errorf("error opening cert or key files: %v", err) |
| 41 } |
| 42 derBytes := rootCert.Certificate[0] |
| 28 CAName := getCAName() | 43 CAName := getCAName() |
| 29 dbPath := getDbPath() | 44 dbPath := getDbPath() |
| 30 | 45 |
| 31 fmt.Printf("Attempting to install root certificate in %q\n", dbPath) | 46 fmt.Printf("Attempting to install root certificate in %q\n", dbPath) |
| 32 | 47 |
| 33 » RemoveRoot() | 48 » i.RemoveRoot() |
| 34 cmd := exec.Command("certutil", "-d", dbPath, "-A", "-n", CAName, "-t",
"C,p,p") | 49 cmd := exec.Command("certutil", "-d", dbPath, "-A", "-n", CAName, "-t",
"C,p,p") |
| 35 cmd.Stdout = os.Stdout | 50 cmd.Stdout = os.Stdout |
| 36 cmd.Stderr = os.Stderr | 51 cmd.Stderr = os.Stderr |
| 37 | 52 |
| 38 stdin, err := cmd.StdinPipe() | 53 stdin, err := cmd.StdinPipe() |
| 39 if err != nil { | 54 if err != nil { |
| 40 return err | 55 return err |
| 41 } | 56 } |
| 42 if err := cmd.Start(); err != nil { | 57 if err := cmd.Start(); err != nil { |
| 43 return err | 58 return err |
| 44 } | 59 } |
| 45 if _, err := stdin.Write(derBytes); err != nil { | 60 if _, err := stdin.Write(derBytes); err != nil { |
| 46 return err | 61 return err |
| 47 } | 62 } |
| 48 stdin.Close() | 63 stdin.Close() |
| 49 if err := cmd.Wait(); err != nil { | 64 if err := cmd.Wait(); err != nil { |
| 50 return fmt.Errorf("NSS certutil failed: %s\n", err) | 65 return fmt.Errorf("NSS certutil failed: %s\n", err) |
| 51 } | 66 } |
| 52 | 67 |
| 53 fmt.Println("Root certificate should now be installed for NSS (i.e. Chro
me).") | 68 fmt.Println("Root certificate should now be installed for NSS (i.e. Chro
me).") |
| 54 return err | 69 return err |
| 55 } | 70 } |
| 56 | 71 |
| 57 func RemoveRoot() { | 72 func (i *Installer) RemoveRoot() { |
| 58 if runtime.GOOS != "linux" { | 73 if runtime.GOOS != "linux" { |
| 59 fmt.Printf("Root certificate is skipped for %s\n", runtime.GOOS) | 74 fmt.Printf("Root certificate is skipped for %s\n", runtime.GOOS) |
| 60 return | 75 return |
| 61 } | 76 } |
| 77 if i.AndroidDeviceId != "" { |
| 78 if runtime.GOOS != "linux" { |
| 79 fmt.Printf("test root CA for Android is only supported o
n a Linux host machine") |
| 80 return |
| 81 } |
| 82 fmt.Println("Uninstalling test root CA on Android...") |
| 83 err := i.AdbUninstallRoot() |
| 84 if err != nil { |
| 85 fmt.Fprintf(os.Stderr, "remove test root CA on android d
evice failed %v", err) |
| 86 } |
| 87 return |
| 88 } |
| 62 fmt.Printf("Removing root certificate %s from NSS (i.e. Chrome)\n", getC
AName()) | 89 fmt.Printf("Removing root certificate %s from NSS (i.e. Chrome)\n", getC
AName()) |
| 63 // Try to delete any existing certificate. We ignore failures since the | 90 // Try to delete any existing certificate. We ignore failures since the |
| 64 // root might not yet exist. | 91 // root might not yet exist. |
| 65 cmd := exec.Command("certutil", "-d", getDbPath(), "-D", "-n", getCAName
()) | 92 cmd := exec.Command("certutil", "-d", getDbPath(), "-D", "-n", getCAName
()) |
| 66 cmd.Run() | 93 cmd.Run() |
| 67 } | 94 } |
| OLD | NEW |