Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: web_page_replay_go/src/webpagereplay/adb_cert_installer.go

Issue 2997573003: [wpr-go] Add support install test root CA on Android (Closed)
Patch Set: roll binary Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « web_page_replay_go/README.md ('k') | web_page_replay_go/src/webpagereplay/installroot.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package webpagereplay
6
7 import (
8 "bytes"
9 "fmt"
10 "io/ioutil"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strings"
15 )
16
17 var (
18 // A temporary directory created to install a test root CA. This is used as a
19 // substititue for android system directory for root CA certs in bind mo unt.
20 androidTempCertDir = "/data/cacerts"
21
22 // Android system directory for root CA certs.
23 androidSystemCertDir = "/system/etc/security/cacerts"
24 )
25
26 type Installer struct {
27 AndroidDeviceId string
28 AdbBinaryPath string
29 }
30
31 // Runs the adb command.
32 func (i *Installer) adb(args ...string) error {
33 newArgs := append([]string{"-s", i.AndroidDeviceId}, args...)
34 cmd := exec.Command(i.AdbBinaryPath, newArgs...)
35 fmt.Println(cmd.Args)
36 var out bytes.Buffer
37 cmd.Stdout = &out
38 if err := cmd.Run(); err != nil {
39 return err
40 }
41 fmt.Print(out.String())
42 return nil
43 }
44
45 // Runs the adb shell command.
46 func (i *Installer) adbShell(args ...string) error {
47 shellArgs := append([]string{"shell"}, args...)
48 return i.adb(shellArgs...)
49 }
50
51 // The issuer hash is used as filename for the installed cert.
52 func getIssuerHashFileName(certPath string) (string, error) {
53 cmd := exec.Command("openssl", "x509", "-in", certPath, "-issuer_hash_ol d", "-noout")
54 var out bytes.Buffer
55 cmd.Stdout = &out
56 err := cmd.Run()
57 if err != nil {
58 return "", err
59 }
60 fmt.Print(out.String())
61 return strings.Trim(out.String(), "\r\n") + ".0", nil
62 }
63
64 // Formats the cert and returns the formatted cert.
65 func formatCert(certPath string) (string, error) {
66 cmd := exec.Command("openssl", "x509", "-inform", "PEM", "-text", "-in", certPath)
67 var out bytes.Buffer
68 cmd.Stdout = &out
69 err := cmd.Run()
70 if err != nil {
71 return "", err
72 }
73 output := out.String()
74 index := strings.Index(output, "-----BEGIN CERTIFICATE")
75 return strings.Join([]string{output[index:], output[:index]}, ""), nil
76 }
77
78 func (i *Installer) AdbInstallRoot(certPath string) error {
79 var err error
80 issuerHashFileName, err := getIssuerHashFileName(certPath)
81 newCert, err := formatCert(certPath)
82 tmpdir, err := ioutil.TempDir("", "adb_install_root")
83 if err != nil {
84 return fmt.Errorf("cannot make tempdir: %v", err)
85 }
86 defer os.RemoveAll(tmpdir)
87 newCertFilePath := filepath.Join(tmpdir, issuerHashFileName)
88 if err = ioutil.WriteFile(newCertFilePath, []byte(newCert), 0666); err ! = nil {
89 return fmt.Errorf("failed to write to temp file %v", err)
90 }
91 if err = i.adbShell("mkdir", androidTempCertDir); err != nil {
92 return err
93 }
94 if err = i.adbShell("cp", androidSystemCertDir+"/*", androidTempCertDir) ; err != nil {
95 return err
96 }
97 if err = i.adbShell("mount", "-o", "bind", androidTempCertDir, androidSy stemCertDir); err != nil {
98 return err
99 }
100 if err = i.adb("push", newCertFilePath, androidSystemCertDir); err != ni l {
101 return err
102 }
103 return nil
104 }
105
106 func (i *Installer) AdbUninstallRoot() error {
107 var err error
108 if err = i.adbShell("umount", androidSystemCertDir); err != nil {
109 return err
110 }
111 if err = i.adbShell("rm", "-r", androidTempCertDir); err != nil {
112 return err
113 }
114 return nil
115 }
OLDNEW
« no previous file with comments | « web_page_replay_go/README.md ('k') | web_page_replay_go/src/webpagereplay/installroot.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698