Skip to content

Tracking: potential incompatibility with Select calls #17

@szszszsz

Description

@szszszsz

While I was implementing support for KeepassXC' challenge-response over CCID, I've noticed that the SELECT call used there had not contain the Le byte. This caused the response to SELECT to be 61xx, which needed additional implementation for getting the remaining data. That sprinkled the discussion, why it was not needed before for the other supported there smart card readers. Apparently the SELECT call for them always result with the data in the direct response to the request.

I am documenting this behavior here for the future reference. Perhaps the SELECT command should be exempted from testing the Le byte, and following the usual rules (e.g. to make it work with improper implementations).

Yubikey 4 responds immediately as well AFAICS. Some YK4 traffic excerpt containing the SELECT (00 A4 04 00) calls:

07477498 APDU: 00 A4 04 00 07 A0 00 00 05 27 20 01
00001034 SW: 04 03 05 03 0B 00 06 0F 00 00 90 00
00000136 APDU: 00 A4 04 00 07 A0 00 00 05 27 20 01
00001019 SW: 04 03 05 03 0B 00 06 0F 00 00 90 00
00000062 APDU: 00 03 00 00 06
00000854 SW: 04 03 05 03 0B 00 90 00
00000137 APDU: 00 A4 04 00 07 A0 00 00 05 27 20 01
00001023 SW: 04 03 05 03 0B 00 06 0F 00 00 90 00
00000055 APDU: 00 01 10 00 06
00000849 SW: 00 56 7F B0 90 00
00000130 APDU: 00 A4 04 00 07 A0 00 00 05 27 20 01
00001000 SW: 04 03 05 03 0B 00 06 0F 00 00 90 00

The call used in KeepassXC for SELECT is:

        uint8_t pbSendBuffer_head[5] = {
            CLA_ISO, INS_SELECT, SEL_APP_AID, 0, static_cast<uint8_t>(handle.second.size())};
        auto pbSendBuffer = new uint8_t[5 + handle.second.size()];
        memcpy(pbSendBuffer, pbSendBuffer_head, 5);
        memcpy(pbSendBuffer + 5, handle.second.constData(), handle.second.size());
       // Give it more space in case custom implementations have longer answer to select
        uint8_t pbRecvBuffer[64] = {0}; 
       // 3 bytes version, 1 byte program counter, other stuff for various implementations, 2 bytes status
        SCUINT dwRecvLength = sizeof pbRecvBuffer;
        auto rv = transmit(handle.first, pbSendBuffer, 5 + handle.second.size(), pbRecvBuffer, dwRecvLength);

PR: keepassxreboot/keepassxc#9397

Code in question:

fn handle_reply(&mut self) {
// Consider if we need to reply via chaining method.
// If the reader is using chaining, we will simply
// reply 61XX, and put the response in a buffer.
// It is up to the reader to then send GetResponse
// requests, to which we will return up to `Le` bytes at a time.
let (new_state, response) = match &mut self.buffer.raw {
RawApduBuffer::Request(_) | RawApduBuffer::None => {
info!("Unexpected GetResponse request.");
(RawApduBuffer::None, Status::UnspecifiedCheckingError.into())
}
RawApduBuffer::Response(res) => {
let max_response_len = self.response_len_expected.min(MAX_INTERCHANGE_DATA);
if self.was_request_chained || res.len() > max_response_len {
// Do not send more than the expected bytes
let boundary = max_response_len.min(res.len());
let to_send = &res[..boundary];
let remaining = &res[boundary..];
let mut message = interchanges::Data::from_slice(to_send).unwrap();
let return_code = if remaining.len() > 255 {
// XX = 00 indicates more than 255 bytes of data
0x6100u16
} else if !remaining.is_empty() {
0x6100 + (remaining.len() as u16)
} else {
// Last chunk has success code
0x9000
};
message
.extend_from_slice(&return_code.to_be_bytes())
.expect("Failed add to status bytes");
if return_code == 0x9000 {
(RawApduBuffer::None, message)
} else {
info!("Still {} bytes in response buffer", remaining.len());
(
RawApduBuffer::Response(response::Data::from_slice(remaining).unwrap()),
message,
)
}
} else {
// Add success code
res.extend_from_slice(&[0x90, 00])
.expect("Failed to add the status bytes");
(
RawApduBuffer::None,
interchanges::Data::from_slice(res.as_slice()).unwrap(),
)
}
}
};
self.buffer.raw = new_state;
self.respond(response);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions