simplify PIN verification
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package im.status.keycard.applet;
|
||||
|
||||
import im.status.keycard.io.APDUCommand;
|
||||
import im.status.keycard.io.APDUException;
|
||||
import im.status.keycard.io.APDUResponse;
|
||||
import im.status.keycard.io.CardChannel;
|
||||
import im.status.keycard.io.*;
|
||||
import org.bouncycastle.jce.interfaces.ECPrivateKey;
|
||||
import org.bouncycastle.jce.interfaces.ECPublicKey;
|
||||
|
||||
@@ -74,11 +71,19 @@ public class KeycardCommandSet {
|
||||
private final CardChannel apduChannel;
|
||||
private SecureChannelSession secureChannel;
|
||||
|
||||
/**
|
||||
* Creates a KeycardCommandSet using the given APDU Channel
|
||||
* @param apduChannel APDU channel
|
||||
*/
|
||||
public KeycardCommandSet(CardChannel apduChannel) {
|
||||
this.apduChannel = apduChannel;
|
||||
this.secureChannel = new SecureChannelSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SecureChannel object
|
||||
* @param secureChannel secure channel
|
||||
*/
|
||||
protected void setSecureChannel(SecureChannelSession secureChannel) {
|
||||
this.secureChannel = secureChannel;
|
||||
}
|
||||
@@ -257,7 +262,7 @@ public class KeycardCommandSet {
|
||||
* Sends a VERIFY PIN APDU. The raw bytes of the given string are encrypted using the secure channel and used as APDU
|
||||
* data.
|
||||
*
|
||||
* @param pin the pin
|
||||
* @param pin the PIN
|
||||
* @return the raw card response
|
||||
* @throws IOException communication error
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@ public class APDUResponse {
|
||||
public static final int SW_CARD_LOCKED = 0x6283;
|
||||
public static final int SW_REFERENCED_DATA_NOT_FOUND = 0x6A88;
|
||||
public static final int SW_CONDITIONS_OF_USE_NOT_SATISFIED = 0x6985; // applet may be already installed
|
||||
public static final int SW_WRONG_PIN_MASK = 0x63C0;
|
||||
|
||||
private byte[] apdu;
|
||||
private byte[] data;
|
||||
@@ -87,6 +88,20 @@ public class APDUResponse {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks response from an authentication command (VERIFY PIN, UNBLOCK PUK)
|
||||
*
|
||||
* @throws WrongPINException wrong PIN
|
||||
* @throws APDUException unexpected response
|
||||
*/
|
||||
public APDUResponse checkAuthOK() throws WrongPINException, APDUException {
|
||||
if ((this.sw & SW_WRONG_PIN_MASK) == SW_WRONG_PIN_MASK) {
|
||||
throw new WrongPINException(sw2 & 0x0F);
|
||||
} else {
|
||||
return checkOK();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data field of this APDU.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package im.status.keycard.io;
|
||||
|
||||
/**
|
||||
* Exception thrown when checking PIN/PUK
|
||||
*/
|
||||
public class WrongPINException extends APDUException {
|
||||
private int retryAttempts;
|
||||
|
||||
/**
|
||||
* Construct an exception with the given number of retry attempts.
|
||||
*
|
||||
* @param retryAttempts the number of retry attempts
|
||||
*/
|
||||
public WrongPINException(int retryAttempts) {
|
||||
super("Wrong PIN");
|
||||
this.retryAttempts = retryAttempts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of available retry attempts.
|
||||
*
|
||||
* @return the number of retry attempts
|
||||
*/
|
||||
public int getRetryAttempts() {
|
||||
return retryAttempts;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user