use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.
the class ChipAuthentication method mseSetAT.
/**
* Initializes the Chip Authentication protocol.
* Sends an MSE:Set AT APDU. (Protocol step 1)
* See BSI-TR-03110, version 2.10, part 3, B.11.1.
*
* @param oID Chip Authentication object identifier
* @param keyID Key identifier
* @throws ProtocolException
*/
public void mseSetAT(byte[] oID, byte[] keyID) throws ProtocolException {
try {
CardCommandAPDU mseSetAT = new MSESetATCA(oID, keyID);
mseSetAT.transmit(dispatcher, slotHandle);
} catch (APDUException e) {
throw new ProtocolException(e.getResult());
}
}
use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.
the class ChipAuthentication method readEFCardSecurity.
/**
* Reads the EFCardSecurity from the card.
*
* @return EFCardSecurtiy
* @throws ProtocolException Thrown in case there is a problem reading the file.
*/
public byte[] readEFCardSecurity() throws ProtocolException {
try {
byte[] file = ShortUtils.toByteArray(EACConstants.EF_CARDSECURITY_FID);
CardResponseAPDU resp = CardUtils.selectFileWithOptions(dispatcher, slotHandle, file, null, CardUtils.FCP_RESPONSE_DATA);
FCP efCardSecurityFCP = new FCP(TLV.fromBER(resp.getData()));
byte[] efCardSecurity = CardUtils.readFile(efCardSecurityFCP, dispatcher, slotHandle);
return efCardSecurity;
} catch (APDUException ex) {
throw new ProtocolException(ex.getResult());
} catch (TLVException ex) {
throw new ProtocolException("Failed to parse FCP.", ex);
}
}
use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.
the class ChipAuthentication method generalAuthenticate.
/**
* Performs a General Authenticate.
* Sends an General Authenticate APDU. (Protocol step 2)
* See BSI-TR-03110, version 2.10, part 3, B.11.2.
*
* @param key Ephemeral Public Key
* @return Response APDU
* @throws ProtocolException
*/
public byte[] generalAuthenticate(byte[] key) throws ProtocolException {
try {
if (key[0] != (byte) 0x04) {
key = ByteUtils.concatenate((byte) 0x04, key);
}
CardCommandAPDU generalAuthenticate = new GeneralAuthenticate((byte) 0x80, key);
CardResponseAPDU response = generalAuthenticate.transmit(dispatcher, slotHandle);
return response.getData();
} catch (APDUException e) {
throw new ProtocolException(e.getResult());
}
}
use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.
the class CardCommandAPDU method transmit.
/**
* Transmit the APDU.
*
* @param dispatcher Dispatcher
* @param slotHandle Slot handle
* @param responses List of positive responses
* @return Response APDU
* @throws APDUException
*/
public CardResponseAPDU transmit(Dispatcher dispatcher, byte[] slotHandle, List<byte[]> responses) throws APDUException {
Transmit t;
TransmitResponse tr = null;
try {
if (responses != null) {
t = makeTransmit(slotHandle, responses);
} else {
t = makeTransmit(slotHandle);
}
tr = (TransmitResponse) dispatcher.safeDeliver(t);
WSHelper.checkResult(tr);
CardResponseAPDU responseAPDU = new CardResponseAPDU(tr);
return responseAPDU;
} catch (WSException ex) {
throw new APDUException(ex, tr);
} catch (Exception ex) {
throw new APDUException(ex);
}
}
use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.
the class CardUtils method readFile.
/**
* Reads a file.
*
* @param dispatcher Dispatcher
* @param slotHandle Slot handle
* @param fcp File Control Parameters
* @return File content
* @throws APDUException
*/
public static byte[] readFile(FCP fcp, Dispatcher dispatcher, byte[] slotHandle) throws APDUException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Read 255 bytes per APDU
byte length = (byte) 0xFF;
// -1 indicates I don't know
short numToRead = -1;
if (fcp != null) {
Long fcpNumBytes = fcp.getNumBytes();
if (fcpNumBytes != null) {
// more than short is not possible and besides that very unrealistic
numToRead = fcpNumBytes.shortValue();
// reduce readout size
if (numToRead < 255) {
length = (byte) numToRead;
}
}
}
boolean isRecord = isRecordEF(fcp);
// records start at index 1
byte i = (byte) (isRecord ? 1 : 0);
short numRead = 0;
try {
CardResponseAPDU response;
byte[] trailer;
int lastNumRead = 0;
boolean goAgain;
do {
if (!isRecord) {
CardCommandAPDU readBinary = new ReadBinary(numRead, length);
// 0x6A84 code for the estonian identity card. The card returns this code
// after the last read process.
response = readBinary.transmit(dispatcher, slotHandle, CardCommandStatus.response(0x9000, 0x6282, 0x6A84, 0x6A83, 0x6A86, 0x6B00));
} else {
CardCommandAPDU readRecord = new ReadRecord((byte) i);
response = readRecord.transmit(dispatcher, slotHandle, CardCommandStatus.response(0x9000, 0x6282, 0x6A84, 0x6A83));
}
trailer = response.getTrailer();
if (!Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x84 }) && !Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x83 }) && !Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x86 })) {
byte[] data = response.getData();
// some cards are just pure shit and return 9000 when no bytes have been read
baos.write(data);
lastNumRead = data.length;
numRead += lastNumRead;
}
i++;
// update length value
goAgain = response.isNormalProcessed() && lastNumRead != 0 || (Arrays.equals(trailer, new byte[] { (byte) 0x62, (byte) 0x82 }) && isRecord);
if (goAgain && numToRead != -1) {
// we have a limit, enforce it
short remainingBytes = (short) (numToRead - numRead);
if (remainingBytes <= 0) {
goAgain = false;
} else if (remainingBytes < 255) {
// update length when we reached the area below 255
length = (byte) remainingBytes;
}
}
} while (goAgain);
baos.close();
} catch (IOException e) {
throw new APDUException(e);
}
return baos.toByteArray();
}
Aggregations