-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore non-smart-card USB interfaces (#843)
Only let the PC/SC see those USB device interfaces that have suitable properties. The exact criteria is replicated from what the CCID driver does internally: either the interface class should be "11" ("Smart Card") or both it should be "255" ("Vendor Specific") and the extra data length should be "54" (which is the standard length of the CCID descriptor). The primary goal is to workaround issues with specific composite devices on modern ChromeOS versions. Before this commit, PC/SC would see all interfaces and attempt to connect via each of them. This used to cause no harm, except spurious errors (which we suppressed heuristically). However, ChromeOS version 105 introduced behavior change, not allowing to connect to such composite devices more than once. In combination with our previously built heuristic "if connection fails, simulate that the whole device reappears under a different bus number" this results in some devices not being able to initialize at all. This commit fixes such issues (we're still discussing if ChromeOS permission_broker could behave better in this case). As a bonus, we reduce log spam for all composite devices. This fixes #849. More context in https://b.corp.google.com/issues/291810618.
- Loading branch information
1 parent
9f485e9
commit cfb267e
Showing
5 changed files
with
582 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
330 changes: 330 additions & 0 deletions
330
smart_card_connector_app/src/smart-card-filter-libusb-hook-unittest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,330 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
goog.require('GoogleSmartCard.LibusbProxyDataModel'); | ||
goog.require('GoogleSmartCard.LibusbToJsApiAdaptor'); | ||
goog.require('GoogleSmartCard.SmartCardFilterLibusbHook'); | ||
goog.require('goog.asserts'); | ||
goog.require('goog.testing'); | ||
goog.require('goog.testing.asserts'); | ||
goog.require('goog.testing.jsunit'); | ||
|
||
goog.setTestOnly(); | ||
|
||
goog.scope(function() { | ||
|
||
const GSC = GoogleSmartCard; | ||
const LibusbJsConfigurationDescriptor = | ||
GSC.LibusbProxyDataModel.LibusbJsConfigurationDescriptor; | ||
|
||
const FAKE_DEVICE_ID = 123; | ||
|
||
class FakeLibusbProxyHookDelegate extends GSC.LibusbToJsApiAdaptor { | ||
constructor() { | ||
super(); | ||
/** @type {!Array<!LibusbJsConfigurationDescriptor>} */ | ||
this.fakeConfigurations_ = []; | ||
} | ||
|
||
/** | ||
* @param {!Array<!LibusbJsConfigurationDescriptor>} fakeConfigurations | ||
*/ | ||
setFakeConfigurations(fakeConfigurations) { | ||
this.fakeConfigurations_ = fakeConfigurations; | ||
} | ||
|
||
/** @override */ | ||
async listDevices() { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async getConfigurations(deviceId) { | ||
return this.fakeConfigurations_; | ||
} | ||
/** @override */ | ||
async openDeviceHandle(deviceId) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async closeDeviceHandle(deviceId, deviceHandle) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async claimInterface(deviceId, deviceHandle, interfaceNumber) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async releaseInterface(deviceId, deviceHandle, interfaceNumber) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async resetDevice(deviceId, deviceHandle) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async controlTransfer(deviceId, deviceHandle, parameters) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async bulkTransfer(deviceId, deviceHandle, parameters) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
/** @override */ | ||
async interruptTransfer(deviceId, deviceHandle, parameters) { | ||
fail('Unexpected call'); | ||
goog.asserts.fail(); | ||
} | ||
}; | ||
|
||
/** @type {!FakeLibusbProxyHookDelegate|undefined} */ | ||
let fakeDelegate; | ||
/** @type {!GSC.SmartCardFilterLibusbHook|undefined} */ | ||
let hook; | ||
|
||
goog.exportSymbol('test_SmartCardFilterLibusbHook', { | ||
'setUp': function() { | ||
fakeDelegate = new FakeLibusbProxyHookDelegate(); | ||
hook = new GSC.SmartCardFilterLibusbHook(); | ||
hook.setDelegate(fakeDelegate); | ||
}, | ||
|
||
'tearDown': function() { | ||
hook = undefined; | ||
fakeDelegate = undefined; | ||
}, | ||
|
||
'testEmpty': async function() { | ||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
assertObjectEquals([], got); | ||
}, | ||
|
||
// A simple device with the Smart Card USB interface class is passed as-is. | ||
'testSingleInterface_SmartCardClass': async function() { | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [{ | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0xB, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
assertObjectEquals(CONFIGS, got); | ||
}, | ||
|
||
// A simple device with an unexpected USB interface class is filtered out. | ||
'testSingleInterface_OtherClass': async function() { | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [{ | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0x3, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
const EXPECTED_RESULT = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [], | ||
}]; | ||
assertObjectEquals(EXPECTED_RESULT, got); | ||
}, | ||
|
||
// A simple device with the Vendor Specific USB interface class and the | ||
// specific "extra data" (54-byte long, conforming to the CCID specs) is | ||
// passed as-is. | ||
'testSingleInterface_VendorClassLikeSmartCard': async function() { | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [{ | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0xFF, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'extraData': new ArrayBuffer(54), | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
assertObjectEquals(CONFIGS, got); | ||
}, | ||
|
||
// A simple device with the Vendor Specific USB interface class and without | ||
// "extra data" is filtered out. | ||
'testSingleInterface_VendorClassWithoutExtraData': async function() { | ||
// Unlike the test above, the "extraData" key is missing. | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [{ | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0xFF, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
const EXPECTED_RESULT = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [], | ||
}]; | ||
assertObjectEquals(EXPECTED_RESULT, got); | ||
}, | ||
|
||
// A simple device with the Vendor Specific USB interface class and with | ||
// "extra data" of unexpected length is filtered out. | ||
'testSingleInterface_VendorClassDifferentExtraDataLength': async function() { | ||
// Unlike the successful test above, the "extraData" key is missing. | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [{ | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0xFF, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'extraData': new ArrayBuffer(10), | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
const EXPECTED_RESULT = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [], | ||
}]; | ||
assertObjectEquals(EXPECTED_RESULT, got); | ||
}, | ||
|
||
// A composite device with both interfaces (one having the Smart Card class | ||
// and another having a different class) is passed only in half. | ||
'testSingleInterface_Composite_SmartCardAndOtherClass': async function() { | ||
const SMART_CARD_INTERFACE = { | ||
'interfaceNumber': 123, | ||
'interfaceClass': 0xB, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}; | ||
const HUMAN_INTERFACE_DEVICE_INTERFACE = { | ||
'interfaceNumber': 234, | ||
'interfaceClass': 0x3, | ||
'interfaceSubclass': 0, | ||
'interfaceProtocol': 0, | ||
'endpoints': [ | ||
{ | ||
'endpointNumber': 1, | ||
'direction': 'out', | ||
'type': 'bulk', | ||
'packetSize': 64, | ||
}, | ||
], | ||
}; | ||
const CONFIGS = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [SMART_CARD_INTERFACE, HUMAN_INTERFACE_DEVICE_INTERFACE], | ||
}]; | ||
fakeDelegate.setFakeConfigurations(CONFIGS); | ||
|
||
const got = await hook.getConfigurations(FAKE_DEVICE_ID); | ||
|
||
const EXPECTED_RESULT = [{ | ||
'active': true, | ||
'configurationValue': 1, | ||
'interfaces': [SMART_CARD_INTERFACE], | ||
}]; | ||
assertObjectEquals(EXPECTED_RESULT, got); | ||
}, | ||
}); | ||
}); |
Oops, something went wrong.