Java Code Examples for javax.smartcardio.ResponseAPDU#getSW1()

The following examples show how to use javax.smartcardio.ResponseAPDU#getSW1() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void verifyPin(char[] pin) throws TechnicalConnectorException {
   try {
      ResponseAPDU responseApdu = verifyPIN(pin);
      if (36864 != responseApdu.getSW()) {
         LOG.debug("VERIFY_PIN error");
         LOG.debug("SW: " + Integer.toHexString(responseApdu.getSW()));
         if (27011 == responseApdu.getSW()) {
            throw new BeIDPinCodeException(new ResponseAPDUException("eID card blocked!", responseApdu));
         } else if (99 != responseApdu.getSW1()) {
            LOG.debug("PIN verification error.");
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         } else {
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         }
      }
   } catch (CardNotPresentException var2) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_EID_NULL, var2, new Object[0]);
   } catch (CardException var3) {
      throw new BeIDPinCodeException(var3);
   }
}
 
Example 2
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void verifyPin(char[] pin) throws TechnicalConnectorException {
   try {
      ResponseAPDU responseApdu = verifyPIN(pin);
      if (36864 != responseApdu.getSW()) {
         LOG.debug("VERIFY_PIN error");
         LOG.debug("SW: " + Integer.toHexString(responseApdu.getSW()));
         if (27011 == responseApdu.getSW()) {
            throw new BeIDPinCodeException(new ResponseAPDUException("eID card blocked!", responseApdu));
         } else if (99 != responseApdu.getSW1()) {
            LOG.debug("PIN verification error.");
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         } else {
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         }
      }
   } catch (CardNotPresentException var2) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_EID_NULL, var2, new Object[0]);
   } catch (CardException var3) {
      throw new BeIDPinCodeException(var3);
   }
}
 
Example 3
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void verifyPin(char[] pin) throws TechnicalConnectorException {
   try {
      ResponseAPDU responseApdu = verifyPIN(pin);
      if (36864 != responseApdu.getSW()) {
         LOG.debug("VERIFY_PIN error");
         LOG.debug("SW: " + Integer.toHexString(responseApdu.getSW()));
         if (27011 == responseApdu.getSW()) {
            throw new BeIDPinCodeException(new ResponseAPDUException("eID card blocked!", responseApdu));
         } else if (99 != responseApdu.getSW1()) {
            LOG.debug("PIN verification error.");
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         } else {
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         }
      }
   } catch (CardNotPresentException var2) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_EID_NULL, var2, new Object[0]);
   } catch (CardException var3) {
      throw new BeIDPinCodeException(var3);
   }
}
 
Example 4
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void verifyPin(char[] pin) throws TechnicalConnectorException {
   try {
      ResponseAPDU responseApdu = verifyPIN(pin);
      if (36864 != responseApdu.getSW()) {
         LOG.debug("VERIFY_PIN error");
         LOG.debug("SW: " + Integer.toHexString(responseApdu.getSW()));
         if (27011 == responseApdu.getSW()) {
            throw new BeIDPinCodeException(new ResponseAPDUException("eID card blocked!", responseApdu));
         } else if (99 != responseApdu.getSW1()) {
            LOG.debug("PIN verification error.");
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         } else {
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         }
      }
   } catch (CardNotPresentException var2) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_EID_NULL, var2, new Object[0]);
   } catch (CardException var3) {
      throw new BeIDPinCodeException(var3);
   }
}
 
Example 5
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void verifyPin(char[] pin) throws TechnicalConnectorException {
   try {
      ResponseAPDU responseApdu = verifyPIN(pin);
      if (36864 != responseApdu.getSW()) {
         LOG.debug("VERIFY_PIN error");
         LOG.debug("SW: " + Integer.toHexString(responseApdu.getSW()));
         if (27011 == responseApdu.getSW()) {
            throw new BeIDPinCodeException(new ResponseAPDUException("eID card blocked!", responseApdu));
         } else if (99 != responseApdu.getSW1()) {
            LOG.debug("PIN verification error.");
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         } else {
            throw new BeIDPinCodeException(new ResponseAPDUException("PIN Verification Error", responseApdu));
         }
      }
   } catch (CardNotPresentException var2) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_EID_NULL, var2, new Object[0]);
   } catch (CardException var3) {
      throw new BeIDPinCodeException(var3);
   }
}
 
Example 6
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 7
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 8
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 9
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 10
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new org.taktik.connector.technical.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 11
Source File: SCP03Wrapper.java    From openjavacard-tools with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ResponseAPDU unwrap(ResponseAPDU response) throws CardException {
    // get fields
    byte[] data = response.getData();
    int sw1 = response.getSW1();
    int sw2 = response.getSW2();

    // perform RMAC
    if (mRMAC) {
        // get the right key
        GPKey rmacKey = mKeys.getKeyByUsage(GPKeyUsage.RMAC);

        // check for sufficient length
        if (data.length < 16) {
            throw new CardException("Can not unwrap: response too short");
        }

        // extract the MAC value
        byte[] mac = Arrays.copyOfRange(data, data.length - 8, data.length);

        // complete MAC context with response
        ByteArrayOutputStream rmac = new ByteArrayOutputStream();
        rmac.write(mICV, 0, mICV.length);
        rmac.write(data, 0, data.length - 8);
        rmac.write(sw1);
        rmac.write(sw2);

        // perform MAC computation
        byte[] macResult = GPBouncy.scp03_mac(rmacKey, rmac.toByteArray(), 128);
        byte[] myMAC = Arrays.copyOfRange(macResult, 0, 8);

        // compare MAC values
        if (!Arrays.equals(mac, myMAC)) {
            throw new CardException("Can not unwrap: bad response MAC");
        }

        data = Arrays.copyOfRange(data, 0, data.length - 8);
    }

    // perform RENC
    if(mRENC && data.length > 0) {
        // RENC uses the ENC key
        GPKey encKey = mKeys.getKeyByUsage(GPKeyUsage.ENC);
        // get counter and modify it for RENC
        byte[] ctr = getEncryptionCounter();
        ctr[0] = (byte)0x80;
        // derive the ICV
        byte[] icv = GPCrypto.enc_aes_ecb(encKey, ctr);
        // perform decryption
        byte[] decrypted = GPCrypto.dec_aes_cbc(encKey, data, icv);
        // remove padding
        data = GPCrypto.unpad80(decrypted);
    }

    // assemble response APDU
    ByteArrayOutputStream myData = new ByteArrayOutputStream();
    myData.write(data, 0, data.length);
    myData.write(sw1);
    myData.write(sw2);

    // return response object
    return new ResponseAPDU(myData.toByteArray());
}