Git Product home page Git Product logo

apdu-dispatch's Introduction

Apdu Dispatch

This layer accepts APDU (application packet data units) from a contact and/or contactless interface and passes them to a selected app.

It handles parsing APDU's, chaining, T=0, and T=1.

Run tests via cargo test --features std,log-all

apdu-dispatch's People

Contributors

conorpp avatar nickray avatar robin-nitrokey avatar sosthene-nitrokey avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

apdu-dispatch's Issues

Tracking: potential incompatibility with Select calls

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);
}

Panic on Select command with an invalid Aid.

Hi!

A bit of fuzzing this library found that a panic happens when apdu_dispatch receives a select command with an Aid in the data field that is incorrect. This is because Aid::new panics when the input data is invalid.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.