Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 1 | // Copyright 2017 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 | #ifndef U2FD_U2FHID_H_ |
| 6 | #define U2FD_U2FHID_H_ |
| 7 | |
Louis Collard | ca8d271 | 2019-08-26 17:57:58 +0800 | [diff] [blame] | 8 | #include <functional> |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/timer/timer.h> |
| 14 | #include <brillo/errors/error.h> |
Louis Collard | 255ca58 | 2019-05-24 12:54:33 +0800 | [diff] [blame] | 15 | #include <metrics/metrics_library.h> |
Louis Collard | 10ac9e9 | 2019-02-23 18:34:45 +0800 | [diff] [blame] | 16 | #include <trunks/cr50_headers/u2f.h> |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 17 | |
| 18 | #include "u2fd/hid_interface.h" |
Louis Collard | 10ac9e9 | 2019-02-23 18:34:45 +0800 | [diff] [blame] | 19 | #include "u2fd/u2f_adpu.h" |
Louis Collard | ca8d271 | 2019-08-26 17:57:58 +0800 | [diff] [blame] | 20 | #include "u2fd/u2f_msg_handler.h" |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 21 | #include "u2fd/user_state.h" |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 22 | |
| 23 | namespace u2f { |
| 24 | |
Andrey Pronin | 34c6faf | 2018-08-14 14:37:26 -0700 | [diff] [blame] | 25 | // Mandatory length of the U2F HID report. |
| 26 | constexpr size_t kU2fReportSize = 64; |
| 27 | |
| 28 | // HID frame CMD/SEQ byte definitions. |
| 29 | constexpr uint8_t kFrameTypeMask = 0x80; |
| 30 | constexpr uint8_t kFrameTypeInit = 0x80; |
| 31 | // when bit 7 is not set, the frame type is CONTinuation. |
| 32 | |
| 33 | // INIT command parameters |
| 34 | constexpr uint32_t kCidBroadcast = -1U; |
| 35 | constexpr size_t kInitNonceSize = 8; |
| 36 | |
Andrey Pronin | 34c6faf | 2018-08-14 14:37:26 -0700 | [diff] [blame] | 37 | constexpr uint8_t kCapFlagLock = 0x02; |
| 38 | |
| 39 | constexpr size_t kMaxPayloadSize = (64 - 7 + 128 * (64 - 5)); // 7609 bytes |
| 40 | |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 41 | // U2fHid emulates U2FHID protocol on top of the TPM U2F implementation. |
| 42 | // The object reads the HID report sent by the HIDInterface passed to the |
| 43 | // constructor, parses it and extracts the U2FHID command. If this is a U2F |
| 44 | // message, finally sends the raw U2F APDU to the |transmit_func| callback |
| 45 | // passed to the constructor. It returns the final result (response APDU or |
| 46 | // error code) inside an HID report through the HIDInterface. |
| 47 | class U2fHid { |
| 48 | public: |
Andrey Pronin | 34c6faf | 2018-08-14 14:37:26 -0700 | [diff] [blame] | 49 | // U2FHID Command codes |
| 50 | enum class U2fHidCommand : uint8_t { |
| 51 | kPing = 1, |
| 52 | kMsg = 3, |
| 53 | kLock = 4, |
| 54 | kVendorSysInfo = 5, |
| 55 | kInit = 6, |
| 56 | kWink = 8, |
| 57 | kError = 0x3f, |
| 58 | }; |
| 59 | |
| 60 | // U2FHID error codes |
| 61 | enum class U2fHidError : uint8_t { |
| 62 | kNone = 0, |
| 63 | kInvalidCmd = 1, |
| 64 | kInvalidPar = 2, |
| 65 | kInvalidLen = 3, |
| 66 | kInvalidSeq = 4, |
| 67 | kMsgTimeout = 5, |
| 68 | kChannelBusy = 6, |
| 69 | kLockRequired = 10, |
| 70 | kInvalidCid = 11, |
| 71 | kOther = 127, |
| 72 | }; |
| 73 | |
Louis Collard | ca8d271 | 2019-08-26 17:57:58 +0800 | [diff] [blame] | 74 | // Create a new virtual U2F HID Device. Does not take ownership of |
| 75 | // msg_handler, which must outlive this instance. |
Tom Hughes | 0f7203b | 2020-08-24 18:29:15 -0700 | [diff] [blame] | 76 | U2fHid(std::unique_ptr<HidInterface> hid, U2fMessageHandler* msg_handler); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 77 | U2fHid(const U2fHid&) = delete; |
| 78 | U2fHid& operator=(const U2fHid&) = delete; |
| 79 | |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 80 | ~U2fHid(); |
| 81 | bool Init(); |
| 82 | |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 83 | private: |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 84 | // U2FHID protocol commands implementation. |
| 85 | void CmdInit(uint32_t cid, const std::string& payload); |
| 86 | int CmdLock(std::string* resp); |
| 87 | int CmdMsg(std::string* resp); |
| 88 | int CmdPing(std::string* resp); |
Vincent Palatin | 828bfe9 | 2018-04-05 15:14:44 +0200 | [diff] [blame] | 89 | int CmdSysInfo(std::string* resp); |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 90 | |
| 91 | // Fully resets the state of the possibly on-going U2FHID transaction. |
| 92 | void ClearTransaction(); |
| 93 | |
| 94 | // Sends back a U2FHID report with just the |errcode| error code inside |
| 95 | // on channel |cid|. |
| 96 | // If |clear| is set, clear the transaction state at the same time. |
| 97 | void ReturnError(U2fHidError errcode, uint32_t cid, bool clear); |
| 98 | |
| 99 | // Called when we reach the deadline for the on-going transaction. |
| 100 | void TransactionTimeout(); |
| 101 | |
| 102 | // Called when we reach the deadline for an unreleased channel lock. |
| 103 | void LockTimeout(); |
| 104 | |
| 105 | // Sends back a U2FHID report indicating success and carrying the response |
| 106 | // payload |resp|. |
| 107 | void ReturnResponse(const std::string& resp); |
| 108 | |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 109 | // Executes the action requested by the command contained in the current |
| 110 | // transaction. |
| 111 | void ExecuteCmd(); |
| 112 | |
| 113 | // Parses the HID report contained in |report| and append the content to the |
| 114 | // current U2FHID transaction or create a new one. |
| 115 | void ProcessReport(const std::string& report); |
| 116 | |
| 117 | std::unique_ptr<HidInterface> hid_; |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 118 | uint32_t free_cid_; |
| 119 | uint32_t locked_cid_; |
| 120 | base::OneShotTimer lock_timeout_; |
Louis Collard | ca8d271 | 2019-08-26 17:57:58 +0800 | [diff] [blame] | 121 | U2fMessageHandler* msg_handler_; |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 122 | |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 123 | class HidPacket; |
| 124 | class HidMessage; |
| 125 | struct Transaction; |
| 126 | |
| 127 | std::unique_ptr<Transaction> transaction_; |
Vincent Palatin | c6c7e4e | 2017-06-15 15:45:05 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace u2f |
| 131 | |
| 132 | #endif // U2FD_U2FHID_H_ |