Java Code Examples for javacard.framework.ISO7816#OFFSET_CLA

The following examples show how to use javacard.framework.ISO7816#OFFSET_CLA . 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: PayPass.java    From CardExamples with The Unlicense 6 votes vote down vote up
public void get_data(APDU apdu, byte[] buf) {
    // verify that the class for this instruction is correct
    if ((short) (buf[ISO7816.OFFSET_CLA] & 0xFF) != 0x80)
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    // check state - this command only works in the PERSO state
    if (PROFILE.STATE != PERSO)
        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    // check that P1 & P2 are correct
    if (buf[ISO7816.OFFSET_P1] != (byte) 0x00 || (byte) buf[ISO7816.OFFSET_P2] != (byte) 0xCF)
        ISOException.throwIt((short) 0x6A88); //referenced data not found
    // build response message
    apdu.setOutgoing();
    apdu.setOutgoingLength((short) 13);

    buf[0] = (byte) 0xCF; //Key Data Tag
    buf[1] = (byte) 11;   //length
    buf[2] = PROFILE.VER_KMC;
    Util.arrayCopyNonAtomic(PROFILE.KMC_ID, (short) 0, buf, (short) 3, (short) 6);
    Util.arrayCopyNonAtomic(PROFILE.CSN, (short) 0, buf, (short) 9, (short) 4);

    apdu.sendBytes((short) 0, (short) 13);
}
 
Example 2
Source File: GaussKeyCard.java    From gauss-key-card with Apache License 2.0 5 votes vote down vote up
public void
process(APDU apdu)
{
	final byte[] buffer = apdu.getBuffer();

	if (selectingApplet()) {
		return;
	}

	// We only support the proprietary class.
	if ((buffer[ISO7816.OFFSET_CLA] & (byte)0x80) != (byte)0x80) {
		ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
		return;
	}

	switch (buffer[ISO7816.OFFSET_INS]) {
	case INS_GET_PUBLIC_KEY:
		processGetPublicKey(apdu);
		break;

	case INS_AUTHENTICATE:
		processAuthenticate(apdu);
		break;

	case INS_GET_CARD_INFO:
		processGetCardInfo(apdu);
		break;

	default:
		ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
	}
}
 
Example 3
Source File: PasswordManagerApplet.java    From sim-password-manager with Apache License 2.0 5 votes vote down vote up
public void process(APDU apdu) throws ISOException {
    byte[] buff = apdu.getBuffer();

    if (selectingApplet()) {
        return;
    }

    // account for logical channels
    if (((byte) (buff[ISO7816.OFFSET_CLA] & (byte) 0xFC)) != CLA) {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }

    switch (buff[ISO7816.OFFSET_INS]) {
    case INS_GET_STATUS:
        getInitStatus(apdu);
        break;
    case INS_GEN_RANDOM:
        prng(apdu);
        break;
    case INS_GEN_KEY:
        generateKeys(apdu);
        break;
    case INS_ENCRYPT:
        encrypt(apdu);
        break;
    case INS_DECRYPT:
        decrypt(apdu);
        break;
    case INS_CLEAR:
        clear(apdu);
        break;

    default:
        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }
}
 
Example 4
Source File: U2FApplet.java    From CCU2F with Apache License 2.0 4 votes vote down vote up
public void process(APDU apdu) throws ISOException {
    byte[] buffer = apdu.getBuffer();
    if (selectingApplet()) {
        if (attestationCertificateSet) {
            Util.arrayCopyNonAtomic(VERSION, (short)0, buffer, (short)0, (short)VERSION.length);
            apdu.setOutgoingAndSend((short)0, (short)VERSION.length);
        }
        return;
    }
    if (buffer[ISO7816.OFFSET_CLA] == PROPRIETARY_CLA) {
        if (attestationCertificateSet) {
            ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
        }
        switch(buffer[ISO7816.OFFSET_INS]) {
        case FIDO_ADM_SET_ATTESTATION_CERT:
            handleSetAttestationCert(apdu);
            break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }
    else if (buffer[ISO7816.OFFSET_CLA] == FIDO_CLA) {
        if (!attestationCertificateSet) {
            ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
        }
        switch(buffer[ISO7816.OFFSET_INS]) {
        case FIDO_INS_ENROLL:
            handleEnroll(apdu);
            break;
        case FIDO_INS_SIGN:
            handleSign(apdu);
            break;
        case FIDO_INS_VERSION:
            handleVersion(apdu);
            break;
        case ISO_INS_GET_DATA:
            handleGetData(apdu);
            break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }
    else {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
}
 
Example 5
Source File: GidsApplet.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Processes an incoming APDU.
 *
 * \see APDU.
 *
 * \param apdu The incoming APDU.
 */
public void process(APDU apdu) {
    byte buffer[] = apdu.getBuffer();
    byte ins = buffer[ISO7816.OFFSET_INS];

    // No secure messaging at the moment
    if((buffer[ISO7816.OFFSET_CLA] & 0x0C) != 0) {
        ISOException.throwIt(ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED);
    }

    transmitManager.processChainInitialization(apdu);

    if((buffer[ISO7816.OFFSET_CLA] & 0xE0) == 0) {
        switch (ins) {
        case INS_ACTIVATE_FILE:
            fs.processActivateFile(apdu);
            break;
        case INS_CREATE_FILE:
            fs.processCreateFile(apdu);
            break;
        case INS_CHANGE_REFERENCE_DATA:
            pinManager.processChangeReferenceData(apdu);
            break;
        case INS_DELETE_FILE:
            fs.processDeleteFile(apdu);
            break;
        case INS_GENERAL_AUTHENTICATE:
            pinManager.processGeneralAuthenticate(apdu);
            break;
        case INS_GENERATE_ASYMMETRIC_KEYPAIR:
            processGenerateAsymmetricKeypair(apdu);
            break;
        case INS_GET_DATA:
            processGetData(apdu);
            break;
        case INS_GET_RESPONSE:
            transmitManager.processGetResponse(apdu);
            break;
        case INS_MANAGE_SECURITY_ENVIRONMENT:
            processManageSecurityEnvironment(apdu);
            break;
        case INS_PERFORM_SECURITY_OPERATION:
            processPerformSecurityOperation(apdu);
            break;
        case INS_PUT_DATA:
            processPutData(apdu);
            break;
        case INS_RESET_RETRY_COUNTER:
            pinManager.processResetRetryCounter(apdu);
            break;
        case ISO7816.INS_SELECT:
            fs.processSelectFile(apdu, selectingApplet());
            break;
        case INS_TERMINATE_DF:
            processTerminateDF(apdu);
            break;
        case INS_VERIFY:
            pinManager.processVerify(apdu);
            break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        } // switch
    } else {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
}
 
Example 6
Source File: LWNFCForumApplet.java    From ledger-javacard with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void process(APDU apdu) throws ISOException {
    if (selectingApplet()) {
        return;
    }
    byte[] buffer = apdu.getBuffer();
    if (buffer[ISO7816.OFFSET_CLA] != NFCFORUM_CLA) {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
    switch(buffer[ISO7816.OFFSET_INS]) {
        case INS_SELECT: {
            apdu.setIncomingAndReceive();
            short selectedFile = Util.getShort(buffer, ISO7816.OFFSET_CDATA);
            switch(selectedFile) {
                case EF_CONTAINER:
                    scratch[OFFSET_SELECTED_FILE] = SELECTED_FILE_CONTAINER;
                    break;
                case EF_NDEF:
                    scratch[OFFSET_SELECTED_FILE] = SELECTED_FILE_NDEF;
                    break;
                default:
                    ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
            }
        }
        break;
        
        case INS_READ: {
            short offset = Util.makeShort(buffer[ISO7816.OFFSET_P1], buffer[ISO7816.OFFSET_P2]);
            if (scratch[OFFSET_SELECTED_FILE] == SELECTED_FILE_NONE) {
                ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
            }
            byte[] fileData = null;
            switch(scratch[OFFSET_SELECTED_FILE]) {
                case SELECTED_FILE_CONTAINER:
                    fileData = CONTAINER_DATA;
                    break;
                case SELECTED_FILE_NDEF:
                    fileData = FILE_DATA;
                    break;
            }
            if (offset >= (short)fileData.length) {
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
            }
            short sizeRead = (short)(buffer[ISO7816.OFFSET_LC] & 0xff);
            short blockLength = (((short)(offset + sizeRead) > (short)fileData.length) ? (short)(fileData.length - offset) : sizeRead);
            Util.arrayCopyNonAtomic(fileData, offset, buffer, (short)0, blockLength);
            apdu.setOutgoingAndSend((short)0, blockLength);
        }
        break;
            
    }       
}