| 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 |
| 26 class CertInstallError(Exception): | 27 class CertInstallError(Exception): |
| 27 pass | 28 pass |
| 28 | 29 |
| 30 |
| 29 class CertRemovalError(Exception): | 31 class CertRemovalError(Exception): |
| 30 pass | 32 pass |
| 31 | 33 |
| 32 | 34 |
| 35 |
| 36 _ANDROID_M_BUILD_VERSION = 23 |
| 37 |
| 38 |
| 33 class AndroidCertInstaller(object): | 39 class AndroidCertInstaller(object): |
| 34 """Certificate installer for phones with KitKat.""" | 40 """Certificate installer for phones with KitKat.""" |
| 35 | 41 |
| 36 def __init__(self, device_id, cert_name, cert_path): | 42 def __init__(self, device_id, cert_name, cert_path): |
| 37 if not os.path.exists(cert_path): | 43 if not os.path.exists(cert_path): |
| 38 raise ValueError('Not a valid certificate path') | 44 raise ValueError('Not a valid certificate path') |
| 39 self.device_id = device_id | 45 self.device_id = device_id |
| 40 self.cert_name = cert_name | 46 self.cert_name = cert_name |
| 41 self.cert_path = cert_path | 47 self.cert_path = cert_path |
| 42 self.file_name = os.path.basename(self.cert_path) | 48 self.file_name = os.path.basename(self.cert_path) |
| 43 self.reformatted_cert_fname = None | 49 self.reformatted_cert_fname = None |
| 44 self.reformatted_cert_path = None | 50 self.reformatted_cert_path = None |
| 45 self.android_cacerts_path = None | 51 self.android_cacerts_path = None |
| 46 | 52 |
| 47 @staticmethod | 53 @staticmethod |
| 48 def _run_cmd(cmd, dirname=None): | 54 def _run_cmd(cmd, dirname=None): |
| 49 return subprocess.check_output(cmd, cwd=dirname) | 55 return subprocess.check_output(cmd, cwd=dirname) |
| 50 | 56 |
| 51 def _adb(self, *args): | 57 def _adb(self, *args): |
| 52 """Runs the adb command.""" | 58 """Runs the adb command.""" |
| 53 cmd = ['adb'] | 59 cmd = ['adb'] |
| 54 if self.device_id: | 60 if self.device_id: |
| 55 cmd.extend(['-s', self.device_id]) | 61 cmd.extend(['-s', self.device_id]) |
| 56 cmd.extend(args) | 62 cmd.extend(args) |
| 57 return self._run_cmd(cmd) | 63 return self._run_cmd(cmd) |
| 58 | 64 |
| 59 def _adb_su_shell(self, *args): | 65 def _adb_su_shell(self, *args): |
| 60 """Runs command as root.""" | 66 """Runs command as root.""" |
| 61 cmd = ['shell', 'su', '-c'] | 67 build_version_sdk = int(self._get_property('ro.build.version.sdk')) |
| 68 if build_version_sdk >= _ANDROID_M_BUILD_VERSION: |
| 69 cmd = ['shell', 'su', '0'] |
| 70 else: |
| 71 cmd = ['shell', 'su', '-c'] |
| 62 cmd.extend(args) | 72 cmd.extend(args) |
| 63 return self._adb(*cmd) | 73 return self._adb(*cmd) |
| 64 | 74 |
| 65 def _get_property(self, prop): | 75 def _get_property(self, prop): |
| 66 return self._adb('shell', 'getprop', prop).strip() | 76 return self._adb('shell', 'getprop', prop).strip() |
| 67 | 77 |
| 68 def check_device(self): | 78 def check_device(self): |
| 69 install_warning = False | 79 install_warning = False |
| 70 if self._get_property('ro.product.device') != 'hammerhead': | 80 if self._get_property('ro.product.device') != 'hammerhead': |
| 71 logging.warning('Device is not hammerhead') | 81 logging.warning('Device is not hammerhead') |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, | 230 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, |
| 221 args.cert_path) | 231 args.cert_path) |
| 222 if args.remove: | 232 if args.remove: |
| 223 cert_installer.remove_cert() | 233 cert_installer.remove_cert() |
| 224 else: | 234 else: |
| 225 cert_installer.install_cert(args.overwrite) | 235 cert_installer.install_cert(args.overwrite) |
| 226 | 236 |
| 227 | 237 |
| 228 if __name__ == '__main__': | 238 if __name__ == '__main__': |
| 229 sys.exit(main()) | 239 sys.exit(main()) |
| OLD | NEW |