Achuith Bhandarkar | 06b98e2 | 2014-05-13 11:56:16 -0700 | [diff] [blame] | 1 | # Copyright 2014 The Chromium OS 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 | import logging, os |
| 6 | |
| 7 | from autotest_lib.client.bin import utils |
| 8 | from autotest_lib.client.cros import cros_ui, cryptohome, ownership |
| 9 | from telemetry.core import exceptions |
| 10 | from telemetry.core.backends.chrome import cros_interface |
| 11 | |
| 12 | |
| 13 | _PASSWD_FILE = '/var/tmp/tpm_passwd' |
| 14 | |
| 15 | |
| 16 | def ClearTPM(): |
| 17 | """Clears the TPM (if it is owned) using the password stored in |
| 18 | /var/tmp/tpm_passwd. Returns True if tpm was owned. |
| 19 | |
| 20 | @return True if the TPM was owned - enrollment should not be attempted. |
| 21 | """ |
| 22 | status = cryptohome.get_tpm_status() |
| 23 | if not status['Owned']: |
| 24 | logging.debug('TPM is not owned') |
| 25 | return False |
| 26 | password = status['Password'] |
| 27 | if not password: |
| 28 | if not os.path.isfile(_PASSWD_FILE): |
| 29 | logging.warn('Password file %s doesn\'t exist, cannot clear TPM. ' |
| 30 | 'You need to have the firmware clear the TPM, for ' |
| 31 | 'instance using crossystem or by toggling the dev ' |
| 32 | 'switch.', _PASSWD_FILE) |
| 33 | return True |
| 34 | with open(_PASSWD_FILE) as f: |
| 35 | password = f.read().rstrip() |
| 36 | |
| 37 | if not password: |
| 38 | logging.warn('Password file %s empty, cannot clear TPM. ' |
| 39 | 'You need to have the firmware clear the TPM, for ' |
| 40 | 'instance using crossystem or by toggling the dev switch.', |
| 41 | _PASSWD_FILE) |
| 42 | return True |
| 43 | |
| 44 | cros_ui.stop() |
| 45 | res = utils.system_output('tpm_clear --pass ' + password) |
| 46 | logging.warn(repr(res)) |
| 47 | |
| 48 | cryptohome.remove_all_vaults() |
| 49 | ownership.clear_ownership_files_no_restart() |
| 50 | logging.warn('Please reboot the system') |
| 51 | return True |
| 52 | |
| 53 | |
| 54 | def _SaveTPMPassword(): |
| 55 | """Save TPM Password to /var/tpm/tpm_passwd. |
| 56 | |
| 57 | During enrollment, the TPM password becomes visible - we capture it and |
| 58 | save it in to a local file, so we can clear the TPM at the end of the test. |
| 59 | """ |
| 60 | password = utils.poll_for_condition( |
| 61 | lambda: cryptohome.get_tpm_status()['Password'], |
| 62 | sleep_interval=0.5, timeout=60) |
| 63 | if password: |
| 64 | with open(_PASSWD_FILE, 'w') as f: |
| 65 | f.write(password) |
| 66 | else: |
| 67 | logging.warn('Could not save TPM password') |
| 68 | logging.info('TPM Password: ' + password) |
| 69 | return password |
| 70 | |
| 71 | |
| 72 | def _ExecuteOobeCmd(browser, cmd): |
| 73 | logging.info('Invoking ' + cmd) |
| 74 | oobe = browser.oobe |
| 75 | oobe.WaitForJavaScriptExpression('typeof Oobe !== \'undefined\'', 10) |
| 76 | oobe.ExecuteJavaScript(cmd) |
| 77 | |
| 78 | |
| 79 | def SwitchToRemora(browser): |
| 80 | """Switch to Remora enrollment. |
| 81 | |
| 82 | @param browser: telemetry browser object. |
| 83 | """ |
| 84 | _cri = cros_interface.CrOSInterface() |
| 85 | pid = _cri.GetChromePid() |
| 86 | try: |
| 87 | # This will restart the browser. |
| 88 | _ExecuteOobeCmd(browser, 'Oobe.remoraRequisitionForTesting();') |
| 89 | except (exceptions.BrowserConnectionGoneException, |
| 90 | exceptions.TabCrashException): |
| 91 | pass |
| 92 | utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60) |
| 93 | utils.poll_for_condition(lambda: browser.oobe_exists, timeout=30) |
| 94 | |
| 95 | _ExecuteOobeCmd(browser, 'Oobe.skipToLoginForTesting();') |
| 96 | _SaveTPMPassword() |
| 97 | |
| 98 | |
| 99 | def FinishEnrollment(oobe): |
| 100 | """Wait for enrollment to finish and dismiss the last enrollment screen. |
| 101 | |
| 102 | @param oobe: telemetry oobe object. |
| 103 | """ |
| 104 | oobe.WaitForJavaScriptExpression( |
| 105 | "document.getElementById('oauth-enrollment').className." |
| 106 | "search('oauth-enroll-state-success') != -1", 30) |
| 107 | oobe.EvaluateJavaScript('Oobe.enterpriseEnrollmentDone();') |
| 108 | |
| 109 | |
| 110 | def RemoraEnrollment(browser, user_id, password): |
| 111 | """Enterprise login for a Remora device. |
| 112 | |
| 113 | @param browser: telemetry browser object. |
| 114 | @param user_id: login credentials user_id. |
| 115 | @param password: login credentials password. |
| 116 | """ |
| 117 | SwitchToRemora(browser) |
| 118 | browser.oobe.NavigateGaiaLogin(user_id, password) |
| 119 | FinishEnrollment(browser.oobe) |