javacard.security.CryptoException Java Examples

The following examples show how to use javacard.security.CryptoException. 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: HmacSha512.java    From SatochipApplet with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void init(byte[] tmp){
	data= tmp;
	try {
		sha512 = MessageDigest.getInstance(MessageDigest.ALG_SHA_512, false); 
	} catch (CryptoException e) {
		ISOException.throwIt(CardEdge.SW_UNSUPPORTED_FEATURE); // unsupported feature => use a more recent card!
	}
}
 
Example #2
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getR(byte[] bytes, short s) throws CryptoException {
    return thePoint.getR(bytes, s);
}
 
Example #3
Source File: IsoApplet.java    From IsoApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Decipher the data from the apdu using the private key referenced by
 * 			an earlier MANAGE SECURITY ENVIRONMENT apdu.
 *
 * \param apdu The PERFORM SECURITY OPERATION apdu with P1=80 and P2=86.
 *
 * \throw ISOException SW_CONDITIONS_NOT_SATISFIED, SW_WRONG_LENGTH and
 *						SW_WRONG_DATA
 */
private void decipher(APDU apdu) {
    short offset_cdata;
    short lc;
    short decLen = -1;

    lc = doChainingOrExtAPDU(apdu);
    offset_cdata = 0;

    // Padding indicator should be "No further indication".
    if(ram_buf[offset_cdata] != (byte) 0x00) {
        ISOException.throwIt(ISO7816.SW_WRONG_DATA);
    }

    switch(currentAlgorithmRef[0]) {

    case ALG_RSA_PAD_PKCS1:
        // Get the key - it must be an RSA private key,
        // checks have been done in MANAGE SECURITY ENVIRONMENT.
        RSAPrivateCrtKey theKey = (RSAPrivateCrtKey) keys[currentPrivateKeyRef[0]];

        // Check the length of the cipher.
        // Note: The first byte of the data field is the padding indicator
        //		 and therefor not part of the ciphertext.
        if((short)(lc-1) !=  (short)(theKey.getSize() / 8)) {
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
        }

        rsaPkcs1Cipher.init(theKey, Cipher.MODE_DECRYPT);
        try {
            decLen = rsaPkcs1Cipher.doFinal(ram_buf, (short)(offset_cdata+1), (short)(lc-1),
                                            apdu.getBuffer(), (short) 0);
        } catch(CryptoException e) {
            ISOException.throwIt(ISO7816.SW_WRONG_DATA);
        }

        // We have to send at most 256 bytes. A short APDU can handle that - only one send operation neccessary.
        apdu.setOutgoingAndSend((short)0, decLen);
        break;

    default:
        ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
    }
}
 
Example #4
Source File: Empty.java    From ant-javacard with MIT License 4 votes vote down vote up
public void process(APDU arg0) throws ISOException {
	CryptoException.throwIt((short) 0x6666);
}
 
Example #5
Source File: Second.java    From ant-javacard with MIT License 4 votes vote down vote up
public void process(APDU arg0) throws ISOException {
	CryptoException.throwIt((short) 0x6666);
}
 
Example #6
Source File: First.java    From ant-javacard with MIT License 4 votes vote down vote up
public void process(APDU arg0) throws ISOException {
	CryptoException.throwIt((short) 0x6666);
}
 
Example #7
Source File: Empty.java    From ant-javacard with MIT License 4 votes vote down vote up
public void process(APDU arg0) throws ISOException {
	CryptoException.throwIt((short) 0x6666);
}
 
Example #8
Source File: CardUtil.java    From ECTester with MIT License 4 votes vote down vote up
public static String getSW(short sw) {
    int upper = (sw & 0xff00) >> 8;
    int lower = (sw & 0xff);
    switch (upper) {
        case 0xf1:
            return String.format("CryptoException(%d)", lower);
        case 0xf2:
            return String.format("SystemException(%d)", lower);
        case 0xf3:
            return String.format("PINException(%d)", lower);
        case 0xf4:
            return String.format("TransactionException(%d)", lower);
        case 0xf5:
            return String.format("CardRuntimeException(%d)", lower);
        default:
            switch (sw) {
                case ISO7816.SW_APPLET_SELECT_FAILED:
                    return "APPLET_SELECT_FAILED";
                case ISO7816.SW_BYTES_REMAINING_00:
                    return "BYTES_REMAINING";
                case ISO7816.SW_CLA_NOT_SUPPORTED:
                    return "CLA_NOT_SUPPORTED";
                case ISO7816.SW_COMMAND_NOT_ALLOWED:
                    return "COMMAND_NOT_ALLOWED";
                case ISO7816.SW_CONDITIONS_NOT_SATISFIED:
                    return "CONDITIONS_NOT_SATISFIED";
                case ISO7816.SW_CORRECT_LENGTH_00:
                    return "CORRECT_LENGTH";
                case ISO7816.SW_DATA_INVALID:
                    return "DATA_INVALID";
                case ISO7816.SW_FILE_FULL:
                    return "FILE_FULL";
                case ISO7816.SW_FILE_INVALID:
                    return "FILE_INVALID";
                case ISO7816.SW_FILE_NOT_FOUND:
                    return "FILE_NOT_FOUND";
                case ISO7816.SW_FUNC_NOT_SUPPORTED:
                    return "FUNC_NOT_SUPPORTED";
                case ISO7816.SW_INCORRECT_P1P2:
                    return "INCORRECT_P1P2";
                case ISO7816.SW_INS_NOT_SUPPORTED:
                    return "INS_NOT_SUPPORTED";
                case ISO7816.SW_LOGICAL_CHANNEL_NOT_SUPPORTED:
                    return "LOGICAL_CHANNEL_NOT_SUPPORTED";
                case ISO7816.SW_RECORD_NOT_FOUND:
                    return "RECORD_NOT_FOUND";
                case ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED:
                    return "SECURE_MESSAGING_NOT_SUPPORTED";
                case ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED:
                    return "SECURITY_STATUS_NOT_SATISFIED";
                case ISO7816.SW_UNKNOWN:
                    return "UNKNOWN";
                case ISO7816.SW_WARNING_STATE_UNCHANGED:
                    return "WARNING_STATE_UNCHANGED";
                case ISO7816.SW_WRONG_DATA:
                    return "WRONG_DATA";
                case ISO7816.SW_WRONG_LENGTH:
                    return "WRONG_LENGTH";
                case ISO7816.SW_WRONG_P1P2:
                    return "WRONG_P1P2";
                case CryptoException.ILLEGAL_VALUE:
                    return "ILLEGAL_VALUE";
                case CryptoException.UNINITIALIZED_KEY:
                    return "UNINITIALIZED_KEY";
                case CryptoException.NO_SUCH_ALGORITHM:
                    return "NO_SUCH_ALG";
                case CryptoException.INVALID_INIT:
                    return "INVALID_INIT";
                case CryptoException.ILLEGAL_USE:
                    return "ILLEGAL_USE";
                case ECTesterApplet.SW_SIG_VERIFY_FAIL:
                    return "SIG_VERIFY_FAIL";
                case ECTesterApplet.SW_DH_DHC_MISMATCH:
                    return "DH_DHC_MISMATCH";
                case ECTesterApplet.SW_KEYPAIR_NULL:
                    return "KEYPAIR_NULL";
                case ECTesterApplet.SW_KA_NULL:
                    return "KA_NULL";
                case ECTesterApplet.SW_SIGNATURE_NULL:
                    return "SIGNATURE_NULL";
                case ECTesterApplet.SW_OBJECT_NULL:
                    return "OBJECT_NULL";
                case ECTesterApplet.SW_Exception:
                    return "Exception";
                case ECTesterApplet.SW_ArrayIndexOutOfBoundsException:
                    return "ArrayIndexOutOfBoundsException";
                case ECTesterApplet.SW_ArithmeticException:
                    return "ArithmeticException";
                case ECTesterApplet.SW_ArrayStoreException:
                    return "ArrayStoreException";
                case ECTesterApplet.SW_NullPointerException:
                    return "NullPointerException";
                case ECTesterApplet.SW_NegativeArraySizeException:
                    return "NegativeArraySizeException";
                default:
                    return "unknown";
            }
    }
}
 
Example #9
Source File: CardUtil.java    From ECTester with MIT License 4 votes vote down vote up
public static String getSWSource(short sw) {
    switch (sw) {
        case ISO7816.SW_NO_ERROR:
        case ISO7816.SW_APPLET_SELECT_FAILED:
        case ISO7816.SW_BYTES_REMAINING_00:
        case ISO7816.SW_CLA_NOT_SUPPORTED:
        case ISO7816.SW_COMMAND_NOT_ALLOWED:
        case ISO7816.SW_CONDITIONS_NOT_SATISFIED:
        case ISO7816.SW_CORRECT_LENGTH_00:
        case ISO7816.SW_DATA_INVALID:
        case ISO7816.SW_FILE_FULL:
        case ISO7816.SW_FILE_INVALID:
        case ISO7816.SW_FILE_NOT_FOUND:
        case ISO7816.SW_FUNC_NOT_SUPPORTED:
        case ISO7816.SW_INCORRECT_P1P2:
        case ISO7816.SW_INS_NOT_SUPPORTED:
        case ISO7816.SW_LOGICAL_CHANNEL_NOT_SUPPORTED:
        case ISO7816.SW_RECORD_NOT_FOUND:
        case ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED:
        case ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED:
        case ISO7816.SW_UNKNOWN:
        case ISO7816.SW_WARNING_STATE_UNCHANGED:
        case ISO7816.SW_WRONG_DATA:
        case ISO7816.SW_WRONG_LENGTH:
        case ISO7816.SW_WRONG_P1P2:
            return "ISO";
        case CryptoException.ILLEGAL_VALUE:
        case CryptoException.UNINITIALIZED_KEY:
        case CryptoException.NO_SUCH_ALGORITHM:
        case CryptoException.INVALID_INIT:
        case CryptoException.ILLEGAL_USE:
            return "CryptoException";
        case ECTesterApplet.SW_SIG_VERIFY_FAIL:
        case ECTesterApplet.SW_DH_DHC_MISMATCH:
        case ECTesterApplet.SW_KEYPAIR_NULL:
        case ECTesterApplet.SW_KA_NULL:
        case ECTesterApplet.SW_SIGNATURE_NULL:
        case ECTesterApplet.SW_OBJECT_NULL:
            return "ECTesterApplet";
        default:
            return "?";
    }
}
 
Example #10
Source File: GidsApplet.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Decipher the data from the apdu using the private key referenced by
 * 			an earlier MANAGE SECURITY ENVIRONMENT apdu.
 *
 * \param apdu The PERFORM SECURITY OPERATION apdu with P1=80 and P2=86.
 *
 * \throw ISOException SW_CONDITIONS_NOT_SATISFIED, SW_WRONG_LENGTH and
 *						SW_WRONG_DATA
 */
private void decipher(APDU apdu) {
    byte[] buf = apdu.getBuffer();
    short offset_cdata;
    short lc;
    short decLen = -1;
    byte[] ram_buf = transmitManager.GetRamBuffer();
    Cipher cipher = null;

    lc = transmitManager.doChainingOrExtAPDU(apdu);
    offset_cdata = 0;

    // Padding indicator should be "No further indication".
    if(buf[offset_cdata] != (byte) 0x00) {
        ISOException.throwIt(ISO7816.SW_WRONG_DATA);
    }

    switch((byte) (currentAlgorithmRef[0] & 0xF0)) {

    case (byte) 0x80:
        cipher = rsaOaepCipher;
        break;
    case (byte) 0x40:
        cipher = rsaPkcs1Cipher;
        break;
    case (byte) 0x00:
        cipher = rsaRawCipher;
        break;
    default:
        ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
    }
    // Get the key - it must be an RSA private key,
    // checks have been done in MANAGE SECURITY ENVIRONMENT.
    CRTKeyFile key = (CRTKeyFile) currentKey[0];
    PrivateKey theKey = key.GetKey().getPrivate();

    // Check the length of the cipher.
    // Note: The first byte of the data field is the padding indicator
    //		 and therefor not part of the ciphertext.
    if(lc !=  (short)(theKey.getSize() / 8)) {
        ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }

    cipher.init(theKey, Cipher.MODE_DECRYPT);

    try {
        decLen = cipher.doFinal(ram_buf, (short) 0, lc,
                                buf, (short) 0);
    } catch(CryptoException e) {
        ISOException.throwIt(ISO7816.SW_WRONG_DATA);
    }

    // We have to send at most 256 bytes. A short APDU can handle that - only one send operation neccessary.
    apdu.setOutgoingAndSend((short)0, decLen);
}
 
Example #11
Source File: GidsApplet.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Only this class's install method should create the applet object.
 */
protected GidsApplet() {

    // by default the pin manager is in "initialization mode"
    pinManager = new GidsPINManager();

    transmitManager = new TransmitManager();

    currentAlgorithmRef = JCSystem.makeTransientByteArray((short)1, JCSystem.CLEAR_ON_DESELECT);
    currentKey = JCSystem.makeTransientObjectArray((short)1, JCSystem.CLEAR_ON_DESELECT);

    rsaPkcs1Cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
    try {
        rsaOaepCipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1_OAEP, false);
    } catch (CryptoException e) {
        if(e.getReason() == CryptoException.NO_SUCH_ALGORITHM) {
            rsaOaepCipher = null;
        } else {
            throw e;
        }
    }
    rsaRawCipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);

    byte mechanisms =  (byte) 0xC0;
    fs = new GidsFileSystem(pinManager, transmitManager, (short) 0x3F00,
                            // FCP
                            new byte[]	{
                                (byte)0x62, (byte)0x08,
                                (byte)0x82, (byte)0x01, (byte)0x38, // File descriptor byte.
                                (byte)0x8C, (byte)0x03, (byte)0x03, (byte)0x30, (byte)0x30,// security attribute
                            },
                            // FCI
                            new byte[]	{
                                0x61, 0X12,
                                0x4F, 0x0B, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x97, (byte) 0x42, (byte) 0x54, (byte) 0x46, (byte) 0x59, 0x02, 0x01, // AID
                                0x73, 0x03,
                                0x40, 0x01, mechanisms, // cryptographic mechanism
                            },
                            // FMD
                            new byte[]	{
                                (byte)0x64, (byte)0x09,
                                (byte)0x5F, (byte)0x2F, (byte) 0x01, (byte) 0x60, // pin usage policy
                                (byte)0x7F, (byte)0x65, 0x02, (byte) 0x80, 0x00
                            }
                           );

    // FCI / FMD / FCP are hard coded
    register();
}
 
Example #12
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getK() throws CryptoException {
    return thePoint.getK();
}
 
Example #13
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setFieldFP(byte[] bytes, short s, short s1) throws CryptoException {
    thePoint.setFieldFP(bytes, s, s1);
}
 
Example #14
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getG(byte[] bytes, short s) throws CryptoException {
    return thePoint.getG(bytes, s);
}
 
Example #15
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getB(byte[] bytes, short s) throws CryptoException {
    return thePoint.getB(bytes, s);
}
 
Example #16
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getA(byte[] bytes, short s) throws CryptoException {
    return thePoint.getA(bytes, s);
}
 
Example #17
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public short getField(byte[] bytes, short s) throws CryptoException {
    return thePoint.getField(bytes, s);
}
 
Example #18
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setR(byte[] bytes, short s, short s1) throws CryptoException {
    thePoint.setR(bytes, s, s1);
}
 
Example #19
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setG(byte[] bytes, short s, short s1) throws CryptoException {
    thePoint.setG(bytes, s, s1);
}
 
Example #20
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setB(byte[] bytes, short s, short s1) throws CryptoException {
    thePoint.setB(bytes, s, s1);
}
 
Example #21
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setA(byte[] bytes, short s, short s1) throws CryptoException {
    thePoint.setA(bytes, s, s1);
}
 
Example #22
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setFieldF2M(short s, short s1, short s2) throws CryptoException {
    thePoint.setFieldF2M(s, s1, s2);
}
 
Example #23
Source File: ECPoint.java    From JCMathLib with MIT License 4 votes vote down vote up
public void setFieldF2M(short s) throws CryptoException {
    thePoint.setFieldF2M(s);
}