Java Code Examples for javacard.framework.ISO7816#SW_NO_ERROR

The following examples show how to use javacard.framework.ISO7816#SW_NO_ERROR . 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: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * Exports selected parameters from a given keyPairs key.
 * Raw parameter data is always prepended by its length as a
 * short value. The order of parameters is the usual one from
 * EC_Consts: field,a,b,g,r,k,w,s.
 *
 * @param keypair keyPair to export from
 * @param key     key to export from (KEY_PUBLIC || KEY_PRIVATE)
 * @param params  params to export (EC_Consts.PARAMETER_* | ...)
 * @param buffer  buffer to export to
 * @param offset  offset to start writing in buffer
 * @return length of data written
 */
public short exportParameters(KeyPair keypair, byte key, short params, byte[] buffer, short offset) {
    sw = ISO7816.SW_NO_ERROR;
    if (params == EC_Consts.PARAMETERS_NONE) {
        return sw;
    }

    short length = 0;
    short paramMask = EC_Consts.PARAMETER_FP;
    while (paramMask <= EC_Consts.PARAMETER_S) {
        short masked = (short) (paramMask & params);
        if (masked != 0) {
            short len = exportParameter(keypair, key, masked, buffer, (short) (offset + 2));
            if (len == 0) {
                paramMask = (short) (paramMask << 1);
                continue;
            }
            Util.setShort(buffer, offset, len);
            offset += len + 2;
            length += len + 2;
        }
        paramMask = (short) (paramMask << 1);
    }
    return length;
}
 
Example 2
Source File: JCMathLibTests.java    From JCMathLib with MIT License 6 votes vote down vote up
void testAPDU(CardManager cardMngr, String input, String expectedOutput) {
    try {
        ResponseAPDU response = cardMngr.transmit(new CommandAPDU(hexStringToByteArray(input)));
        if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
            if (!expectedOutput.isEmpty()) {
                byte[] data = response.getData();
                String output = Util.bytesToHex(data);
                assertTrue(expectedOutput.equalsIgnoreCase(output), "Result provided by card mismatch expected");
            }
        }
        else {
            assertTrue(false, String.format("Card failed with 0x%x", response.getSW()));
        }                
    }
    catch (Exception e) {
        e.printStackTrace();
        assertTrue(false, "Card transmit failed with execption");
    }
}
 
Example 3
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keypair
 * @param key
 * @param params
 * @param inBuffer
 * @param inOffset
 * @return
 */
public short setExternalCurve(KeyPair keypair, byte key, short params, byte[] inBuffer, short inOffset) {
    sw = ISO7816.SW_NO_ERROR;
    if (params == EC_Consts.PARAMETERS_NONE) {
        return sw;
    }

    short paramMask = EC_Consts.PARAMETER_FP;
    while (paramMask <= EC_Consts.PARAMETER_S) {
        short masked = (short) (paramMask & params);
        if (masked != 0) {
            short paramLength = Util.getShort(inBuffer, inOffset);
            inOffset += 2;
            sw = setParameter(keypair, key, masked, inBuffer, inOffset, paramLength);
            inOffset += paramLength;
            if (sw != ISO7816.SW_NO_ERROR) break;
        }
        paramMask = (short) (paramMask << 1);
    }
    return sw;
}
 
Example 4
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keypair
 * @param key
 * @param params
 * @param transformation
 * @param buffer
 * @param offset
 * @return
 */
public short transformCurve(KeyPair keypair, byte key, short params, short transformation, byte[] buffer, short offset) {
    sw = ISO7816.SW_NO_ERROR;
    if (params == EC_Consts.PARAMETERS_NONE) {
        return sw;
    }

    //go through param bit by bit, and invalidate all selected params
    short paramMask = EC_Consts.PARAMETER_FP;
    while (paramMask <= EC_Consts.PARAMETER_S) {
        short masked = (short) (paramMask & params);
        if (masked != 0) {
            short length = exportParameter(keypair, key, masked, buffer, offset);
            length = EC_Consts.transformParameter(transformation, buffer, offset, length);
            sw = setParameter(keypair, key, masked, buffer, offset, length);
            if (sw != ISO7816.SW_NO_ERROR) break;
        }
        paramMask = (short) (paramMask << 1);
    }
    return sw;
}
 
Example 5
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keyClass
 * @param keyLength
 * @return
 */
public KeyPair constructPair(byte keyClass, short keyLength) {
    sw = ISO7816.SW_NO_ERROR;
    KeyPair ecKeyPair = null;
    byte privKeyType;
    byte pubKeyType;
    if (keyClass == KeyPair.ALG_EC_FP) {
        privKeyType = KeyBuilder.TYPE_EC_FP_PRIVATE;
        pubKeyType = KeyBuilder.TYPE_EC_FP_PUBLIC;
    } else {
        privKeyType = KeyBuilder.TYPE_EC_F2M_PRIVATE;
        pubKeyType = KeyBuilder.TYPE_EC_F2M_PUBLIC;
    }
    try {
        if (!dryRun) {
            ECPrivateKey privateKey = (ECPrivateKey) KeyBuilder.buildKey(privKeyType, keyLength, false);
            ECPublicKey publicKey = (ECPublicKey) KeyBuilder.buildKey(pubKeyType, keyLength, false);

            ecKeyPair = new KeyPair(publicKey, privateKey);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return ecKeyPair;
}
 
Example 6
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keyClass
 * @param keyLength
 * @return
 */
public KeyPair allocatePair(byte keyClass, short keyLength) {
    sw = ISO7816.SW_NO_ERROR;
    KeyPair ecKeyPair = null;
    try {
        if (!dryRun) {
            ecKeyPair = new KeyPair(keyClass, keyLength);

            if (ecKeyPair.getPublic() == null || ecKeyPair.getPrivate() == null) {
                try {
                    ecKeyPair.genKeyPair();
                } catch (Exception ignored) {
                }
            }
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return ecKeyPair;
}
 
Example 7
Source File: CardUtil.java    From ECTester with MIT License 5 votes vote down vote up
public static String getSWString(short sw) {
    if (sw == ISO7816.SW_NO_ERROR) {
        return "OK   (0x9000)";
    } else {
        String str = getSW(sw);
        return String.format("fail (%s, 0x%04x)", str, sw);
    }
}
 
Example 8
Source File: ECKeyTester.java    From ECTester with MIT License 5 votes vote down vote up
public short allocateSig(byte algorithm) {
    sw = ISO7816.SW_NO_ERROR;
    try {
        if (!dryRun) {
            ecdsaSignature = Signature.getInstance(algorithm, false);
            sigType = algorithm;
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example 9
Source File: ECKeyTester.java    From ECTester with MIT License 5 votes vote down vote up
public short allocateKA(byte algorithm) {
    sw = ISO7816.SW_NO_ERROR;
    try {
        if (!dryRun) {
            ecKeyAgreement = KeyAgreement.getInstance(algorithm, false);
            kaType = algorithm;
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example 10
Source File: ECKeyGenerator.java    From ECTester with MIT License 5 votes vote down vote up
/**
 * @param keypair
 * @param key
 * @param curve
 * @param params
 * @param buffer
 * @param offset
 * @return
 */
public short setCurve(KeyPair keypair, byte key, byte curve, short params, byte[] buffer, short offset) {
    byte alg = EC_Consts.getCurveType(curve);
    sw = ISO7816.SW_NO_ERROR;

    if (params == EC_Consts.PARAMETERS_NONE) {
        return sw;
    }

    short length;
    //handle fp and f2m differently, as a FP KeyPair doesnt contain a F2M field and vice versa.
    if (alg == KeyPair.ALG_EC_FP && (params & EC_Consts.PARAMETER_FP) != 0) {
        length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_FP, buffer, offset);
        sw = setParameter(keypair, key, EC_Consts.PARAMETER_FP, buffer, offset, length);
    } else if (alg == KeyPair.ALG_EC_F2M && (params & EC_Consts.PARAMETER_F2M) != 0) {
        length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_F2M, buffer, offset);
        sw = setParameter(keypair, key, EC_Consts.PARAMETER_F2M, buffer, offset, length);
    }
    if (sw != ISO7816.SW_NO_ERROR) return sw;

    //go through all params
    short paramMask = EC_Consts.PARAMETER_A;
    while (paramMask <= EC_Consts.PARAMETER_S) {
        short masked = (short) (paramMask & params);
        if (masked != 0) {
            length = EC_Consts.getCurveParameter(curve, masked, buffer, offset);
            sw = setParameter(keypair, key, masked, buffer, offset, length);
            if (sw != ISO7816.SW_NO_ERROR) break;
        }
        paramMask = (short) (paramMask << 1);
    }
    return sw;
}
 
Example 11
Source File: TestClient.java    From JCMathLib with MIT License 5 votes vote down vote up
public boolean compare(ResponseAPDU response) {
    boolean bResult = false;
    if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
        bResult = Arrays.equals(m_expected.getEncoded(), response.getData());
    } else {
        bResult = false;
        System.out.println(String.format("fail (0x%x)", response.getSW()));
    }
    return bResult;
}
 
Example 12
Source File: CardMngr.java    From ECTester with MIT License 5 votes vote down vote up
public byte[] fetchCPLC() throws CardException {
    // Try CPLC via GP
    ResponseAPDU resp = send(FETCH_GP_CPLC_APDU);
    // If GP CLA fails, try with ISO
    if (resp.getSW() == (ISO7816.SW_CLA_NOT_SUPPORTED & 0xffff)) {
        resp = send(FETCH_ISO_CPLC_APDU);
    }
    if (resp.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
        return resp.getData();
    }
    return null;
}
 
Example 13
Source File: Util.java    From JCMathLib with MIT License 5 votes vote down vote up
private static boolean checkSW(ResponseAPDU response) {
    if (response.getSW() != (ISO7816.SW_NO_ERROR & 0xffff)) {
        System.err.printf("Received error status: %02X.\n",
                response.getSW());
        return false;
    }
    return true;
}
 
Example 14
Source File: TestClient.java    From JCMathLib with MIT License 5 votes vote down vote up
static boolean verifyAndLogResponse(String operationName, ResponseAPDU response, Long lastTransmitTime, ECPoint expected, FileOutputStream perfFile, ArrayList<String> failedTestsList) throws IOException {
    boolean bResult = false;
    if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
        bResult = Arrays.equals(expected.getEncoded(false), response.getData());
        if (!bResult) {
            System.out.println(String.format("Expected: %s", Util.toHex(expected.getEncoded(false))));
            System.out.println(String.format("Obtained: %s", Util.toHex(response.getData())));
        }
    } else {
        bResult = false;
        System.out.println(String.format("fail (0x%x)", response.getSW()));
    }
    logResponse(operationName, bResult, lastTransmitTime, perfFile, failedTestsList);
    return bResult;
}
 
Example 15
Source File: TestClient.java    From JCMathLib with MIT License 5 votes vote down vote up
static boolean verifyAndLogResponse(String operationName, ResponseAPDU response, Long lastTransmitTime, int expected, FileOutputStream perfFile, ArrayList<String> failedTestsList) throws IOException {
    boolean bResult = false;
    if (response.getSW () == (ISO7816.SW_NO_ERROR & 0xffff)) {
        bResult = Util.BytesToInt(response.getData()) == expected;
    }
    else {
        System.out.println(String.format("fail (0x%x)", response.getSW()));
    }
    logResponse(operationName, bResult, lastTransmitTime, perfFile, failedTestsList);
    return bResult;
}
 
Example 16
Source File: AppletUtil.java    From ECTester with MIT License 4 votes vote down vote up
private static short nullCheck(Object obj, short sw) {
    if (obj == null)
        ISOException.throwIt(sw);
    return ISO7816.SW_NO_ERROR;
}
 
Example 17
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 18
Source File: Response.java    From ECTester with MIT License 4 votes vote down vote up
boolean parse(int numSW, int numParams) {
    this.numSW = numSW;
    this.sws = new short[numSW];

    byte[] data = resp.getData();
    int offset = 0;

    //parse SWs in response
    for (int i = 0; i < numSW; ++i) {
        if (getLength() >= (offset + 2)) {
            short sw = ByteUtil.getShort(data, offset);
            offset += 2;
            sws[i] = sw;
            if (sw != ISO7816.SW_NO_ERROR) {
                success = false;
            }
        } else {
            success = false;
            error = true;
        }
    }

    if ((short) resp.getSW() != ISO7816.SW_NO_ERROR) {
        success = false;
        error = true;
    }


    //try to parse numParams..
    params = new byte[numParams][];
    for (int i = 0; i < numParams; i++) {
        if (data.length - offset < 2) {
            success = false;
            error = true;
            break;
        }
        short paramLength = ByteUtil.getShort(data, offset);
        offset += 2;
        if (data.length < offset + paramLength) {
            error = true;
            success = false;
            break;
        }
        params[i] = new byte[paramLength];
        System.arraycopy(data, offset, params[i], 0, paramLength);
        offset += paramLength;
    }
    return success;
}
 
Example 19
Source File: CardManager.java    From JCMathLib with MIT License 4 votes vote down vote up
private CardChannel connectToCardByTerminalFactory(TerminalFactory factory, int targetReaderIndex) throws CardException {
    List<CardTerminal> terminals = new ArrayList<>();

    boolean card_found = false;
    CardTerminal terminal = null;
    Card card = null;
    try {
        for (CardTerminal t : factory.terminals().list()) {
            terminals.add(t);
            if (t.isCardPresent()) {
                card_found = true;
            }
        }
    } catch (Exception ignored) {
    }

    if (card_found) {
        System.out.println("Cards found: " + terminals);

        terminal = terminals.get(targetReaderIndex); 

        System.out.print("Connecting...");
        card = terminal.connect("*"); 

        System.out.println(" done.");

        System.out.print("Establishing channel...");
        m_channel = card.getBasicChannel();

        System.out.println(" done.");

        System.out.print("Selecting applet...");

        CommandAPDU cmd = new CommandAPDU(0x00, 0xa4, 0x04, 0x00, m_APPLET_AID);
        ResponseAPDU response = transmit(cmd);
        if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
            System.out.print(" done");
        }
        else {
            System.out.print(" failed.");
        }
        
    } else {
        System.out.print("Failed to find required card.");
    }

    if (card != null) {
        return card.getBasicChannel();
    } else {
        return null;
    }
}