| OLD | NEW |
| 1 # Copyright 2014 Google Inc. All Rights Reserved. | 1 # Copyright 2014 Google Inc. All Rights Reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 13 # limitations under the License. |
| 14 | 14 |
| 15 """Installs certificate on phone with KitKat.""" | 15 """Installs certificate on phone with KitKat.""" |
| 16 | 16 |
| 17 import argparse | 17 import argparse |
| 18 import logging | 18 import logging |
| 19 import os | 19 import os |
| 20 import subprocess | 20 import subprocess |
| 21 import sys | 21 import sys |
| 22 | 22 |
| 23 KEYCODE_ENTER = '66' | 23 KEYCODE_ENTER = '66' |
| 24 KEYCODE_TAB = '61' | 24 KEYCODE_TAB = '61' |
| 25 | 25 |
| 26 | |
| 27 class CertInstallError(Exception): | 26 class CertInstallError(Exception): |
| 28 pass | 27 pass |
| 29 | 28 |
| 30 | |
| 31 class CertRemovalError(Exception): | 29 class CertRemovalError(Exception): |
| 32 pass | 30 pass |
| 33 | 31 |
| 34 | 32 |
| 35 | |
| 36 _ANDROID_M_BUILD_VERSION = 23 | |
| 37 | |
| 38 | |
| 39 class AndroidCertInstaller(object): | 33 class AndroidCertInstaller(object): |
| 40 """Certificate installer for phones with KitKat.""" | 34 """Certificate installer for phones with KitKat.""" |
| 41 | 35 |
| 42 def __init__(self, device_id, cert_name, cert_path): | 36 def __init__(self, device_id, cert_name, cert_path): |
| 43 if not os.path.exists(cert_path): | 37 if not os.path.exists(cert_path): |
| 44 raise ValueError('Not a valid certificate path') | 38 raise ValueError('Not a valid certificate path') |
| 45 self.device_id = device_id | 39 self.device_id = device_id |
| 46 self.cert_name = cert_name | 40 self.cert_name = cert_name |
| 47 self.cert_path = cert_path | 41 self.cert_path = cert_path |
| 48 self.file_name = os.path.basename(self.cert_path) | 42 self.file_name = os.path.basename(self.cert_path) |
| 49 self.reformatted_cert_fname = None | 43 self.reformatted_cert_fname = None |
| 50 self.reformatted_cert_path = None | 44 self.reformatted_cert_path = None |
| 51 self.android_cacerts_path = None | 45 self.android_cacerts_path = None |
| 52 | 46 |
| 53 @staticmethod | 47 @staticmethod |
| 54 def _run_cmd(cmd, dirname=None): | 48 def _run_cmd(cmd, dirname=None): |
| 55 return subprocess.check_output(cmd, cwd=dirname) | 49 return subprocess.check_output(cmd, cwd=dirname) |
| 56 | 50 |
| 57 def _adb(self, *args): | 51 def _adb(self, *args): |
| 58 """Runs the adb command.""" | 52 """Runs the adb command.""" |
| 59 cmd = ['adb'] | 53 cmd = ['adb'] |
| 60 if self.device_id: | 54 if self.device_id: |
| 61 cmd.extend(['-s', self.device_id]) | 55 cmd.extend(['-s', self.device_id]) |
| 62 cmd.extend(args) | 56 cmd.extend(args) |
| 63 return self._run_cmd(cmd) | 57 return self._run_cmd(cmd) |
| 64 | 58 |
| 65 def _adb_su_shell(self, *args): | 59 def _adb_su_shell(self, *args): |
| 66 """Runs command as root.""" | 60 """Runs command as root.""" |
| 67 build_version_sdk = int(self._get_property('ro.build.version.sdk')) | 61 cmd = ['shell', 'su', '-c'] |
| 68 if build_version_sdk >= _ANDROID_M_BUILD_VERSION: | |
| 69 cmd = ['shell', 'su', '0'] | |
| 70 else: | |
| 71 cmd = ['shell', 'su', '-c'] | |
| 72 cmd.extend(args) | 62 cmd.extend(args) |
| 73 return self._adb(*cmd) | 63 return self._adb(*cmd) |
| 74 | 64 |
| 75 def _get_property(self, prop): | 65 def _get_property(self, prop): |
| 76 return self._adb('shell', 'getprop', prop).strip() | 66 return self._adb('shell', 'getprop', prop).strip() |
| 77 | 67 |
| 78 def check_device(self): | 68 def check_device(self): |
| 79 install_warning = False | 69 install_warning = False |
| 80 if self._get_property('ro.product.device') != 'hammerhead': | 70 if self._get_property('ro.product.device') != 'hammerhead': |
| 81 logging.warning('Device is not hammerhead') | 71 logging.warning('Device is not hammerhead') |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, | 220 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, |
| 231 args.cert_path) | 221 args.cert_path) |
| 232 if args.remove: | 222 if args.remove: |
| 233 cert_installer.remove_cert() | 223 cert_installer.remove_cert() |
| 234 else: | 224 else: |
| 235 cert_installer.install_cert(args.overwrite) | 225 cert_installer.install_cert(args.overwrite) |
| 236 | 226 |
| 237 | 227 |
| 238 if __name__ == '__main__': | 228 if __name__ == '__main__': |
| 239 sys.exit(main()) | 229 sys.exit(main()) |
| OLD | NEW |