xref: /aosp_15_r20/external/autotest/client/common_lib/cros/g2f_utils.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7import time
8
9from autotest_lib.client.common_lib import error
10
11# USB ID for the virtual U2F HID Device.
12U2F_VID = '18D1'
13U2F_PID = '502C'
14
15QUERY_U2F_DEVICE_ATTEMPTS=5
16QUERY_U2F_RETRY_DELAY_SEC=1
17
18def ChromeOSLogin(client):
19    """Logs in to ChromeOS, so that u2fd can start up."""
20    client.run('/usr/local/autotest/bin/autologin.py')
21
22def ChromeOSLogout(client):
23    """Logs out of ChromeOS, to return the device to a known state."""
24    client.run('restart ui')
25
26def StartU2fd(client):
27    """Starts u2fd on the client.
28
29    @param client: client object to run commands on.
30    """
31    client.run('touch /var/lib/u2f/force/u2f.force')
32    client.run('restart u2fd')
33
34    path = '/sys/bus/hid/devices/*:%s:%s.*/hidraw' % (U2F_VID, U2F_PID)
35    attempts = 0
36    while attempts < QUERY_U2F_DEVICE_ATTEMPTS:
37      attempts += 1
38      try:
39        return '/dev/' + client.run('ls ' + path).stdout.strip()
40      except error.AutoservRunError as e:
41        logging.info('Could not find U2F device on attempt ' +
42                     str(attempts))
43      time.sleep(QUERY_U2F_RETRY_DELAY_SEC)
44
45def G2fRegister(client, dev, challenge, application, p1=0):
46    """Returns a dictionary with TPM status.
47
48    @param client: client object to run commands on.
49    """
50    return client.run('g2ftool --reg --dev=' + dev +
51                      ' --challenge=' + challenge +
52                      ' --application=' + application +
53                      ' --p1=' + str(p1),
54                      ignore_status=True)
55
56def G2fAuth(client, dev, challenge, application, key_handle, p1=0):
57    """Returns a dictionary with TPM status.
58
59    @param client: client object to run commands on.
60    """
61    return client.run('g2ftool --auth --dev=' + dev +
62                      ' --challenge=' + challenge +
63                      ' --application=' + application +
64                      ' --key_handle=' + key_handle +
65                      ' --p1=' + str(p1),
66                      ignore_status=True)
67