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

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: 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
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 // Runs the adb command.
27 func adb(args ...string) error {
28 cmd := exec.Command("adb", args...)
29 fmt.Println(args)
Tom Bergan 2017/08/09 15:20:50 Suggest cmd.Args here, as that will include "adb".
xunjieli 2017/08/09 15:51:31 Done. Good idea! This is much more informative.
30 var out bytes.Buffer
31 cmd.Stdout = &out
32 if err := cmd.Run(); err != nil {
33 return err
34 }
35 fmt.Print(out.String())
36 return nil
37 }
38
39 // Runs the adb shell command.
40 func adbShell(args ...string) error {
41 shellArgs := append([]string{"shell"}, args...)
42 return adb(shellArgs...)
43 }
44
45 // The issuer hash is used as filename for the installed cert.
46 func getIssuerHashFileName(certPath string) (string, error) {
47 cmd := exec.Command("openssl", "x509", "-in", certPath, "-issuer_hash_ol d", "-noout")
48 var out bytes.Buffer
49 cmd.Stdout = &out
50 err := cmd.Run()
51 if err != nil {
52 return "", err
53 }
54 fmt.Print(out.String())
55 return strings.Trim(out.String(), "\r\n") + ".0", nil
56 }
57
58 // Formats the cert and returns the formatted cert.
59 func formatCert(certPath string) (string, error) {
60 cmd := exec.Command("openssl", "x509", "-inform", "PEM", "-text", "-in", certPath)
61 var out bytes.Buffer
62 cmd.Stdout = &out
63 err := cmd.Run()
64 if err != nil {
65 return "", err
66 }
67 output := out.String()
68 index := strings.Index(output, "-----BEGIN CERTIFICATE")
69 return strings.Join([]string{output[index:], output[:index]}, ""), nil
70 }
71
72 func AdbInstallRoot(certPath string) error {
73 certPath = "newcert.pem"
Tom Bergan 2017/08/09 15:20:50 Is this temporary code?
xunjieli 2017/08/09 15:51:31 Done. Good catch!
74 var err error
75 issuerHashFileName, err := getIssuerHashFileName(certPath)
76 newCert, err := formatCert(certPath)
77 tmpdir, err := ioutil.TempDir("", "adb_install_root")
78 if err != nil {
79 return fmt.Errorf("cannot make tempdir: %v", err)
80 }
81 defer os.RemoveAll(tmpdir)
82 newCertFilePath := filepath.Join(tmpdir, issuerHashFileName)
83 if err = ioutil.WriteFile(newCertFilePath, []byte(newCert), 0666); err ! = nil {
84 return fmt.Errorf("failed to write to temp file %v", err)
85 }
86 if err = adbShell("mkdir", androidTempCertDir); err != nil {
87 return err
88 }
89 if err = adbShell("cp", androidSystemCertDir+"/*", androidTempCertDir); err != nil {
90 return err
91 }
92 if err = adbShell("mount", "-o", "bind", androidTempCertDir, androidSyst emCertDir); err != nil {
93 return err
94 }
95 if err = adb("push", newCertFilePath, androidSystemCertDir); err != nil {
96 return err
97 }
98 return nil
99 }
100
101 func AdbUninstallRoot() error {
102 var err error
103 if err = adbShell("umount", androidSystemCertDir); err != nil {
104 return err
105 }
106 if err = adbShell("rm", "-r", androidTempCertDir); err != nil {
107 return err
108 }
109 return nil
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698