Ejemplo n.º 1
0
 /**
  * Generate an assymetric RSA key pair according to ISO7816-8, Section 5.1. We only support RSA
  * 1024 bit at the moment, and return data in simple TLV data objects, tags 81 and 82.
  *
  * <p>Successful MSE command has to be performed prior to this one.
  */
 private void processGenerateAssymetricKeyPair(APDU apdu) {
   // This is only valid in state initial (at the moment)
   if (state != STATE_INITIAL) {
     ISOException.throwIt(SW_INS_NOT_SUPPORTED);
   }
   byte[] buf = apdu.getBuffer();
   byte p1 = buf[OFFSET_P1];
   byte p2 = buf[OFFSET_P2];
   if (p1 != (byte) 0x80 || p2 != (byte) 0x00) {
     ISOException.throwIt(SW_INCORRECT_P1P2);
   }
   if (currentPrivateKey[0] == null) {
     ISOException.throwIt(SW_CONDITIONS_NOT_SATISFIED);
   }
   KeyPair pair = new KeyPair(tempKeyPublic, (RSAPrivateCrtKey) currentPrivateKey[0]);
   pair.genKeyPair();
   // Sanity check, the KeyPair class should regenerate the keys "in place".
   if (pair.getPrivate() != currentPrivateKey[0] || pair.getPublic() != tempKeyPublic) {
     ISOException.throwIt(SW_DATA_INVALID);
   }
   apdu.setOutgoing();
   short len = (short) 0;
   short offset = 0;
   buf[offset++] = (byte) 0x81;
   len = tempKeyPublic.getModulus(buf, (short) (offset + 2));
   buf[offset++] = (byte) 0x81;
   buf[offset++] = (byte) len;
   offset += len;
   buf[offset++] = (byte) 0x82;
   len = tempKeyPublic.getExponent(buf, (short) (offset + 1));
   buf[offset++] = (byte) len;
   offset += len;
   apdu.setOutgoingLength(offset);
   apdu.sendBytes((short) 0, offset);
 }
Ejemplo n.º 2
0
  private cardTest() {

    // Instantiate all object the applet will ever need
    // pin= new OwnerPIN(MAX_LENGTH, MAX_ATTEMPTS);
    // if(bArray==null){//check
    //			If no pin is passed as parameter at installation time use default 0000
    // pin.update(new byte[] {0x00,0x00,0x00,0x00}, (short) 0, (byte) 0x04);
    //	}
    // else {
    // pin.update(bArray, bOffset,  bLength);
    // }

    try {
      // Set signature algorithm
      sig = Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1, false);
      // Generate the card keys
      keys.genKeyPair();
      // Get the public key
      k = (RSAPublicKey) keys.getPublic();
      // Get the private key
      k2 = (RSAPrivateKey) keys.getPrivate();
      // Initialize the signature object with card private key
      sig.init(k2, Signature.MODE_SIGN);
    } catch (CryptoException ex) {
      ISOException.throwIt((short) (ex.getReason()));
    } catch (SecurityException ex) {
      ISOException.throwIt((short) (0x6F10));
    } catch (Exception ex) {
      ISOException.throwIt((short) (0x6F20));
    }
  }