use of javax.smartcardio.CommandAPDU in project jdk8u_jdk by JetBrains.
the class TestChannel method main.
public static void main(String[] args) throws Exception {
CardTerminal terminal = getTerminal(args);
if (terminal == null) {
System.out.println("Skipping the test: " + "no card terminals available");
return;
}
// establish a connection with the card
Card card = terminal.connect("T=0");
System.out.println("card: " + card);
CardChannel basicChannel = card.getBasicChannel();
try {
basicChannel.transmit(new CommandAPDU(openChannel));
} catch (IllegalArgumentException e) {
System.out.println("OK: " + e);
}
try {
basicChannel.transmit(new CommandAPDU(closeChannel));
} catch (IllegalArgumentException e) {
System.out.println("OK: " + e);
}
byte[] atr = card.getATR().getBytes();
System.out.println("atr: " + toString(atr));
// semi-accurate test to see if the card appears to support logical channels
boolean supportsChannels = false;
for (int i = 0; i < atr.length; i++) {
if (atr[i] == 0x73) {
supportsChannels = true;
break;
}
}
if (supportsChannels == false) {
System.out.println("Card does not support logical channels, skipping...");
} else {
CardChannel channel = card.openLogicalChannel();
System.out.println("channel: " + channel);
/*
// XXX bug in Oberthur card??
System.out.println("Transmitting...");
transmitTestCommand(channel);
System.out.println("OK");
/**/
channel.close();
}
// disconnect
card.disconnect(true);
System.out.println("OK.");
}
use of javax.smartcardio.CommandAPDU in project jdk8u_jdk by JetBrains.
the class Utils method transmitTestCommand.
static void transmitTestCommand(CardChannel channel) throws Exception {
ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
byte[] rb = r.getBytes();
if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
System.out.println("expected: " + toString(R1a));
System.out.println("received: " + toString(rb));
throw new Exception("Response does not match");
}
}
use of javax.smartcardio.CommandAPDU in project jdk8u_jdk by JetBrains.
the class TestTransmit method main.
public static void main(String[] args) throws Exception {
CardTerminal terminal = getTerminal(args);
if (terminal == null) {
System.out.println("Skipping the test: " + "no card terminals available");
return;
}
Card card = terminal.connect("T=0");
CardChannel channel = card.getBasicChannel();
BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));
byte[] command = null;
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
if (line.startsWith(CMD_MARKER)) {
System.out.println(line);
line = line.substring(CMD_MARKER.length());
command = parse(line);
} else if (line.startsWith(RES_MARKER)) {
System.out.println(line);
line = line.substring(RES_MARKER.length());
Bytes response = parseWildcard(line);
CommandAPDU capdu = new CommandAPDU(command);
ResponseAPDU rapdu = channel.transmit(capdu);
byte[] received = rapdu.getBytes();
if (received.length != response.bytes.length) {
throw new Exception("Length mismatch: " + toString(received));
}
for (int i = 0; i < received.length; i++) {
byte mask = response.mask[i];
if ((received[i] & response.mask[i]) != response.bytes[i]) {
throw new Exception("Mismatch: " + toString(received));
}
}
}
// else ignore
}
// disconnect
card.disconnect(true);
System.out.println("OK.");
}
use of javax.smartcardio.CommandAPDU in project jdk8u_jdk by JetBrains.
the class CommandAPDUTest method setUpClass.
@BeforeClass
public static void setUpClass() throws Exception {
//expected values of apdu, data, headers, nc, ne
CommandAPDU capdu = new CommandAPDU(C1);
apdu = capdu.getBytes();
data = capdu.getData();
cla = capdu.getCLA();
if (cla != (C1[0] & 0xff)) {
throw new RuntimeException("Failure: cla is not right");
}
ins = capdu.getINS();
if (ins != (C1[1] & 0xff)) {
throw new RuntimeException("Failure: ins is not right");
}
p1 = capdu.getP1();
if (p1 != (C1[2] & 0xff)) {
throw new RuntimeException("Failure: p1 is not right");
}
p2 = capdu.getP2();
if (p2 != (C1[3] & 0xff)) {
throw new RuntimeException("Failure: p2 is not right");
}
nc = capdu.getNc();
ne = capdu.getNe();
//Test on following constructors
cm1 = new CommandAPDU(apdu);
cm2 = new CommandAPDU(cla, ins, p1, p2);
cm3 = new CommandAPDU(cla, ins, p1, p2, data);
cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
cm7 = new CommandAPDU(apdu, 0, apdu.length);
cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
use of javax.smartcardio.CommandAPDU in project jmulticard by ctt-gob-es.
the class SmartcardIoConnection method transmit.
/**
* {@inheritDoc}
*/
@Override
public ResponseApdu transmit(final CommandApdu command) throws ApduConnectionException {
if (this.canal == null) {
throw new ApduConnectionException(// $NON-NLS-1$
"No se puede transmitir sobre una conexion cerrada");
}
if (command == null) {
throw new IllegalArgumentException(// $NON-NLS-1$
"No se puede transmitir una APDU nula");
}
if (DEBUG) {
// $NON-NLS-1$
Logger.getLogger("es.gob.jmulticard").info(// $NON-NLS-1$
"Enviada APDU:\n" + HexUtils.hexify(command.getBytes(), true));
}
try {
byte[] sendApdu;
if (command.getData() != null) {
// Si la APDU es mayor que 0xFF la troceamos y la envolvemos
if (command.getData().length > 0xFF) {
int sentLength = 0;
final int totalLength = command.getBytes().length;
final int CONTENT_SIZE_ENVELOPE = 250;
while (totalLength - sentLength > CONTENT_SIZE_ENVELOPE) {
final byte[] apduChunk = Arrays.copyOfRange(command.getBytes(), sentLength, sentLength + CONTENT_SIZE_ENVELOPE);
final CommandAPDU apdu = new CommandAPDU((byte) 0x90, (byte) 0xC2, (byte) 0x00, (byte) 0x00, apduChunk);
final ResponseApdu response = new ResponseApdu(this.canal.transmit(apdu).getBytes());
if (!response.isOk()) {
return response;
}
sentLength += CONTENT_SIZE_ENVELOPE;
}
final byte[] apduChunk = Arrays.copyOfRange(command.getBytes(), sentLength, totalLength);
sendApdu = new CommandAPDU((byte) 0x90, (byte) 0xC2, (byte) 0x00, (byte) 0x00, apduChunk).getBytes();
} else // Si es pequena, se envia directamente
{
sendApdu = command.getBytes();
}
} else {
sendApdu = command.getBytes();
}
final ResponseApdu response = new ResponseApdu(this.canal.transmit(new CommandAPDU(sendApdu)).getBytes());
// Solicitamos el resultado de la operacion si es necesario
if (response.getStatusWord().getMsb() == TAG_RESPONSE_PENDING) {
// Si ya se ha devuelto parte de los datos, los concatenamos al resultado
if (response.getData().length > 0) {
final byte[] data = response.getData();
final byte[] additionalData = transmit(new GetResponseApduCommand((byte) 0x00, response.getStatusWord().getLsb())).getBytes();
final byte[] fullResponse = new byte[data.length + additionalData.length];
System.arraycopy(data, 0, fullResponse, 0, data.length);
System.arraycopy(additionalData, 0, fullResponse, data.length, additionalData.length);
return new ResponseApdu(fullResponse);
}
return transmit(new GetResponseApduCommand((byte) 0x00, response.getStatusWord().getLsb()));
} else // (de eso se encargara la clase de conexion con canal seguro)
if (response.getStatusWord().getMsb() == TAG_RESPONSE_INVALID_LENGTH && command.getCla() == (byte) 0x00) {
command.setLe(response.getStatusWord().getLsb());
return transmit(command);
}
if (DEBUG) {
// $NON-NLS-1$
Logger.getLogger("es.gob.jmulticard").info(// $NON-NLS-1$
"Respuesta:\n" + HexUtils.hexify(response.getBytes(), true));
}
return response;
} catch (final CardException e) {
final Throwable t = e.getCause();
if (t != null && SCARD_W_RESET_CARD.equals(t.getMessage())) {
throw new LostChannelException(t.getMessage());
}
throw new ApduConnectionException(// $NON-NLS-1$
"Error de comunicacion con la tarjeta tratando de transmitir la APDU " + HexUtils.hexify(command.getBytes(), true) + " al lector " + // $NON-NLS-1$
Integer.toString(this.terminalNumber) + // $NON-NLS-1$
" en modo EXCLUSIVE=" + Boolean.toString(this.exclusive) + " con el protocolo " + // $NON-NLS-1$
this.protocol.toString(), // $NON-NLS-1$
e);
} catch (final Exception e) {
e.printStackTrace();
throw new ApduConnectionException(// $NON-NLS-1$
"Error tratando de transmitir la APDU " + HexUtils.hexify(command.getBytes(), true) + " al lector " + // $NON-NLS-1$
Integer.toString(this.terminalNumber) + // $NON-NLS-1$
" en modo EXCLUSIVE=" + Boolean.toString(this.exclusive) + " con el protocolo " + // $NON-NLS-1$
this.protocol.toString(), // $NON-NLS-1$
e);
}
}
Aggregations