| 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, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 | 26 |
| 27 class CertInstallError(Exception): | 27 class CertInstallError(Exception): |
| 28 pass | 28 pass |
| 29 | 29 |
| 30 | 30 |
| 31 class CertRemovalError(Exception): | 31 class CertRemovalError(Exception): |
| 32 pass | 32 pass |
| 33 | 33 |
| 34 | 34 |
| 35 class AdbShellError(subprocess.CalledProcessError): |
| 36 pass |
| 37 |
| 35 | 38 |
| 36 _ANDROID_M_BUILD_VERSION = 23 | 39 _ANDROID_M_BUILD_VERSION = 23 |
| 37 | 40 |
| 38 | 41 |
| 39 class AndroidCertInstaller(object): | 42 class AndroidCertInstaller(object): |
| 40 """Certificate installer for phones with KitKat.""" | 43 """Certificate installer for phones with KitKat.""" |
| 41 | 44 |
| 42 def __init__(self, device_id, cert_name, cert_path): | 45 def __init__(self, device_id, cert_name, cert_path): |
| 43 if not os.path.exists(cert_path): | 46 if not os.path.exists(cert_path): |
| 44 raise ValueError('Not a valid certificate path') | 47 raise ValueError('Not a valid certificate path') |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 raise subprocess.CalledProcessError( | 82 raise subprocess.CalledProcessError( |
| 80 cmd=adb_cmd, returncode=process.returncode, output=adb_stdout) | 83 cmd=adb_cmd, returncode=process.returncode, output=adb_stdout) |
| 81 assert adb_stdout[-1] == '\n' | 84 assert adb_stdout[-1] == '\n' |
| 82 prefix_pos = adb_stdout.rfind(RETURN_CODE_PREFIX) | 85 prefix_pos = adb_stdout.rfind(RETURN_CODE_PREFIX) |
| 83 assert prefix_pos != -1, \ | 86 assert prefix_pos != -1, \ |
| 84 'Couldn\'t find "%s" at the end of the output of %s' % ( | 87 'Couldn\'t find "%s" at the end of the output of %s' % ( |
| 85 RETURN_CODE_PREFIX, subprocess.list2cmdline(adb_cmd)) | 88 RETURN_CODE_PREFIX, subprocess.list2cmdline(adb_cmd)) |
| 86 returncode = int(adb_stdout[prefix_pos + len(RETURN_CODE_PREFIX):]) | 89 returncode = int(adb_stdout[prefix_pos + len(RETURN_CODE_PREFIX):]) |
| 87 stdout = adb_stdout[:prefix_pos] | 90 stdout = adb_stdout[:prefix_pos] |
| 88 if returncode != 0: | 91 if returncode != 0: |
| 89 raise subprocess.CalledProcessError( | 92 raise AdbShellError(cmd=args, returncode=returncode, output=stdout) |
| 90 cmd=args, returncode=returncode, output=stdout) | |
| 91 return stdout | 93 return stdout |
| 92 | 94 |
| 93 def _adb_su_shell(self, *args): | 95 def _adb_su_shell(self, *args): |
| 94 """Runs command as root.""" | 96 """Runs command as root.""" |
| 95 build_version_sdk = int(self._get_property('ro.build.version.sdk')) | 97 build_version_sdk = int(self._get_property('ro.build.version.sdk')) |
| 96 if build_version_sdk >= _ANDROID_M_BUILD_VERSION: | 98 if build_version_sdk >= _ANDROID_M_BUILD_VERSION: |
| 97 cmd = ['su', '0'] | 99 cmd = ['su', '0'] |
| 98 else: | 100 else: |
| 99 cmd = ['su', '-c'] | 101 cmd = ['su', '-c'] |
| 100 cmd.extend(args) | 102 cmd.extend(args) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 'CERTIFICATE') | 139 'CERTIFICATE') |
| 138 contents = ''.join([begin_cert, cert_body, description]) | 140 contents = ''.join([begin_cert, cert_body, description]) |
| 139 with open(self.reformatted_cert_path, 'w') as cert_file: | 141 with open(self.reformatted_cert_path, 'w') as cert_file: |
| 140 cert_file.write(contents) | 142 cert_file.write(contents) |
| 141 | 143 |
| 142 def _remove_cert_from_cacerts(self): | 144 def _remove_cert_from_cacerts(self): |
| 143 self._adb_su_shell('mount', '-o', 'remount,rw', '/system') | 145 self._adb_su_shell('mount', '-o', 'remount,rw', '/system') |
| 144 self._adb_su_shell('rm', '-f', self.android_cacerts_path) | 146 self._adb_su_shell('rm', '-f', self.android_cacerts_path) |
| 145 | 147 |
| 146 def _is_cert_installed(self): | 148 def _is_cert_installed(self): |
| 147 return (self._adb_su_shell('ls', self.android_cacerts_path).strip() == | 149 try: |
| 148 self.android_cacerts_path) | 150 return (self._adb_su_shell('ls', self.android_cacerts_path).strip() == |
| 151 self.android_cacerts_path) |
| 152 except AdbShellError: |
| 153 return False |
| 149 | 154 |
| 150 def _generate_reformatted_cert_path(self): | 155 def _generate_reformatted_cert_path(self): |
| 151 # Determine OpenSSL version, string is of the form | 156 # Determine OpenSSL version, string is of the form |
| 152 # 'OpenSSL 0.9.8za 5 Jun 2014' . | 157 # 'OpenSSL 0.9.8za 5 Jun 2014' . |
| 153 openssl_version = self._run_cmd(['openssl', 'version']).split() | 158 openssl_version = self._run_cmd(['openssl', 'version']).split() |
| 154 | 159 |
| 155 if len(openssl_version) < 2: | 160 if len(openssl_version) < 2: |
| 156 raise ValueError('Unexpected OpenSSL version string: ', openssl_version) | 161 raise ValueError('Unexpected OpenSSL version string: ', openssl_version) |
| 157 | 162 |
| 158 # subject_hash flag name changed as of OpenSSL version 1.0.0 . | 163 # subject_hash flag name changed as of OpenSSL version 1.0.0 . |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, | 263 cert_installer = AndroidCertInstaller(args.device_id, args.cert_name, |
| 259 args.cert_path) | 264 args.cert_path) |
| 260 if args.remove: | 265 if args.remove: |
| 261 cert_installer.remove_cert() | 266 cert_installer.remove_cert() |
| 262 else: | 267 else: |
| 263 cert_installer.install_cert(args.overwrite) | 268 cert_installer.install_cert(args.overwrite) |
| 264 | 269 |
| 265 | 270 |
| 266 if __name__ == '__main__': | 271 if __name__ == '__main__': |
| 267 sys.exit(main()) | 272 sys.exit(main()) |
| OLD | NEW |