• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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
5import logging
6import time
7
8from autotest_lib.client.common_lib import error
9
10# USB ID for the virtual U2F HID Device.
11U2F_VID = '18D1'
12U2F_PID = '502C'
13
14QUERY_U2F_DEVICE_ATTEMPTS=5
15QUERY_U2F_RETRY_DELAY_SEC=1
16
17def ChromeOSLogin(client):
18    """Logs in to ChromeOS, so that u2fd can start up."""
19    client.run('/usr/local/autotest/bin/autologin.py')
20
21def ChromeOSLogout(client):
22    """Logs out of ChromeOS, to return the device to a known state."""
23    client.run('restart ui')
24
25def StartU2fd(client):
26    """Starts u2fd on the client.
27
28    @param client: client object to run commands on.
29    """
30    client.run('touch /var/lib/u2f/force/u2f.force')
31    client.run('restart u2fd')
32
33    path = '/sys/bus/hid/devices/*:%s:%s.*/hidraw' % (U2F_VID, U2F_PID)
34    attempts = 0
35    while attempts < QUERY_U2F_DEVICE_ATTEMPTS:
36      attempts += 1
37      try:
38        return '/dev/' + client.run('ls ' + path).stdout.strip()
39      except error.AutoservRunError, e:
40        logging.info('Could not find U2F device on attempt ' +
41                     str(attempts))
42      time.sleep(QUERY_U2F_RETRY_DELAY_SEC)
43
44def G2fRegister(client, dev, challenge, application, p1=0):
45    """Returns a dictionary with TPM status.
46
47    @param client: client object to run commands on.
48    """
49    return client.run('g2ftool --reg --dev=' + dev +
50                      ' --challenge=' + challenge +
51                      ' --application=' + application +
52                      ' --p1=' + str(p1),
53                      ignore_status=True)
54
55def G2fAuth(client, dev, challenge, application, key_handle, p1=0):
56    """Returns a dictionary with TPM status.
57
58    @param client: client object to run commands on.
59    """
60    return client.run('g2ftool --auth --dev=' + dev +
61                      ' --challenge=' + challenge +
62                      ' --application=' + application +
63                      ' --key_handle=' + key_handle +
64                      ' --p1=' + str(p1),
65                      ignore_status=True)
66