Java Code Examples for javacard.framework.ISO7816#OFFSET_INS

The following examples show how to use javacard.framework.ISO7816#OFFSET_INS . 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: 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 2
Source File: TransitApplet.java    From JCMathLib with MIT License 5 votes vote down vote up
public void process(APDU apdu) {

        // C-APDU: [CLA, INS, P1, P2, LC, ...]

        byte[] buffer = apdu.getBuffer();

        // Dispatch C-APDU for processing
        if (!apdu.isISOInterindustryCLA()) {
            switch (buffer[ISO7816.OFFSET_INS]) {
            case INITIALIZE_SESSION:
                initializeSession(apdu);
                return;
            case PROCESS_REQUEST:
                processRequest(apdu);
                return;
            default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        } else {
            if (buffer[ISO7816.OFFSET_INS] == (byte)(0xA4)) {
                return;
            } else if (buffer[ISO7816.OFFSET_INS] == VERIFY) {
                verify(apdu);
            } else {
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        }
    }
 
Example 3
Source File: NdefApplet.java    From openjavacard-ndef with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Process an APDU
 *
 * This is the outer layer of our APDU dispatch.
 *
 * It deals with the CLA and INS of the APDU,
 * leaving the rest to an INS-specific function.
 *
 * @param apdu to be processed
 * @throws ISOException on error
 */
public final void process(APDU apdu) throws ISOException {
    byte[] buffer = apdu.getBuffer();
    byte ins = buffer[ISO7816.OFFSET_INS];

    // handle selection of the applet
    if(selectingApplet()) {
        vars[VAR_SELECTED_FILE] = FILEID_NONE;
        return;
    }

    // secure messaging is not supported
    if(apdu.isSecureMessagingCLA()) {
        ISOException.throwIt(ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED);
    }

    // process commands to the applet
    if(apdu.isISOInterindustryCLA()) {
        if (ins == INS_SELECT) {
            processSelect(apdu);
        } else if (ins == INS_READ_BINARY) {
            processReadBinary(apdu);
        } else if (ins == INS_UPDATE_BINARY) {
            if(FEATURE_WRITING) {
                processUpdateBinary(apdu);
            } else {
                ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
            }
        } else {
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    } else {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
}
 
Example 4
Source File: NdefApplet.java    From openjavacard-ndef with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Process an APDU
 *
 * This is the outer layer of our APDU dispatch.
 *
 * It deals with the CLA and INS of the APDU,
 * leaving the rest to an INS-specific function.
 *
 * @param apdu to be processed
 * @throws ISOException on error
 */
public final void process(APDU apdu) throws ISOException {
    byte[] buffer = apdu.getBuffer();
    byte ins = buffer[ISO7816.OFFSET_INS];

    // handle selection of the applet
    if(selectingApplet()) {
        vars[VAR_SELECTED_FILE] = FILEID_NONE;
        return;
    }

    // secure messaging is not supported
    if(apdu.isSecureMessagingCLA()) {
        ISOException.throwIt(ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED);
    }

    // process commands to the applet
    if(apdu.isISOInterindustryCLA()) {
        if (ins == INS_SELECT) {
            processSelect(apdu);
        } else if (ins == INS_READ_BINARY) {
            processReadBinary(apdu);
        } else if (ins == INS_UPDATE_BINARY) {
            ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
        } else {
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    } else {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
}
 
Example 5
Source File: NdefApplet.java    From openjavacard-ndef with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Process an APDU
 *
 * This is the outer layer of our APDU dispatch.
 *
 * It deals with the CLA and INS of the APDU,
 * leaving the rest to an INS-specific function.
 *
 * @param apdu to be processed
 * @throws ISOException on error
 */
public final void process(APDU apdu) throws ISOException {
    byte[] buffer = apdu.getBuffer();
    byte ins = buffer[ISO7816.OFFSET_INS];

    // handle selection of the applet
    if(selectingApplet()) {
        vars[VAR_SELECTED_FILE] = FILEID_NONE;
        connectService();
        return;
    }

    // if we are not connected then fail
    if(!isConnected()) {
        ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }

    // secure messaging is not supported
    if(apdu.isSecureMessagingCLA()) {
        ISOException.throwIt(ISO7816.SW_SECURE_MESSAGING_NOT_SUPPORTED);
    }

    // process commands to the applet
    if(apdu.isISOInterindustryCLA()) {
        if (ins == INS_SELECT) {
            processSelect(apdu);
        } else if (ins == INS_READ_BINARY) {
            processReadBinary(apdu);
        } else if (ins == INS_UPDATE_BINARY) {
            ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
        } else {
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    } else {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
}
 
Example 6
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 7
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 8
Source File: TransmitManager.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
public void processChainInitialization(APDU apdu) {
    byte buffer[] = apdu.getBuffer();
    byte ins = buffer[ISO7816.OFFSET_INS];
    // Command chaining checks & initialization
    if(chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_INS] != 0 || isCommandChainingCLA(apdu)) {
        short p1p2 = Util.getShort(buffer, ISO7816.OFFSET_P1);
        /*
         * Command chaining only for:
         * 	- PERFORM SECURITY OPERATION
         * 	- GENERATE ASYMMETRIC KEYKAIR
         * 	- PUT DATA
         * when not using extended APDUs.
         */
        if( (ins != GidsApplet.INS_PERFORM_SECURITY_OPERATION
                && ins != GidsApplet.INS_GENERATE_ASYMMETRIC_KEYPAIR
                && ins != GidsApplet.INS_PUT_DATA)) {
            ISOException.throwIt(ErrorCode.SW_COMMAND_CHAINING_NOT_SUPPORTED);
        }

        if(chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_INS] == 0
                && chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_P1P2] == 0) {
            /* A new chain is starting - set the current INS and P1P2. */
            if(ins == 0) {
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
            chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_INS] = ins;
            chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_P1P2] = p1p2;
        } else if(chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_INS] != ins
                  || chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_P1P2] != p1p2) {
            /* The current chain is not yet completed,
             * but an apdu not part of the chain had been received. */
            ISOException.throwIt(ErrorCode.SW_COMMAND_NOT_ALLOWED_GENERAL);
        } else if(!isCommandChainingCLA(apdu)) {
            /* A chain is ending, set the current INS and P1P2 to zero to indicate that. */
            chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_INS] = 0;
            chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_P1P2] = 0;
        }
    }

    // If the card expects a GET RESPONSE, no other operation should be requested.
    if(chaining_cache[RAM_CHAINING_CACHE_OFFSET_BYTES_REMAINING] > 0 && ins != GidsApplet.INS_GET_RESPONSE) {
        // clear the buffer
        Clear(true);
    }
    if (ins != GidsApplet.INS_PUT_DATA) {
        clearCachedRecord();
    }
}
 
Example 9
Source File: TransmitManager.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Send the data from ram_buf, using either extended APDUs or GET RESPONSE.
 *
 * \param apdu The APDU object, in STATE_OUTGOING state.
 *
 * \param pos The position in ram_buf at where the data begins
 *
 * \param len The length of the data to be sent. If zero, 9000 will be
 *            returned
 */
private void sendData(APDU apdu) {
    short le;
    short remaininglen = 0;
    byte data[] = null;
    short pos = chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_POS];

    le = apdu.setOutgoing();
    // le has not been set
    if(le == 0) {
        // we get here when called from the Shared VMWare reader
        byte ins = apdu.getBuffer()[ISO7816.OFFSET_INS];
        if ( ins != GidsApplet.INS_GENERATE_ASYMMETRIC_KEYPAIR) {
            le = 256;
        } else {
            le = 0;
        }
    }

    if (chaining_object[CHAINING_OBJECT] == null) {
        data = ram_buf;
        remaininglen = chaining_cache[RAM_CHAINING_CACHE_OFFSET_BYTES_REMAINING];
    } else if (chaining_object[CHAINING_OBJECT] instanceof Record) {
        Record record = (Record) (chaining_object[CHAINING_OBJECT]);
        data = record.GetData();
        remaininglen = (short) (((short) data.length) - pos);
    } else if (chaining_object[CHAINING_OBJECT] instanceof Record[]) {
        data = ram_buf;
        remaininglen = copyRecordsToRamBuf(le);
        pos = 0;
    }

    // We have 256 Bytes send-capacity per APDU.
    short sendLen = remaininglen > le ? le : remaininglen;
    apdu.setOutgoingLength(sendLen);
    apdu.sendBytesLong(data, pos, sendLen);
    // the position when using Record[] is maintened by copyRecordsToRamBuf
    if (chaining_object[CHAINING_OBJECT] == null || !(chaining_object[CHAINING_OBJECT] instanceof Record[])) {
        chaining_cache[RAM_CHAINING_CACHE_OFFSET_CURRENT_POS]+= sendLen;
    }

    if (chaining_object[CHAINING_OBJECT] == null) {
        chaining_cache[RAM_CHAINING_CACHE_OFFSET_BYTES_REMAINING] -= sendLen;
    }
    remaininglen -= sendLen;
    if(remaininglen > 0) {
        short nextRespLen = remaininglen > 256 ? 256 : remaininglen;
        ISOException.throwIt( (short)(ISO7816.SW_BYTES_REMAINING_00 | nextRespLen) );
    } else {
        Clear(true);
        return;
    }
}
 
Example 10
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 11
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;
            
    }       
}
 
Example 12
Source File: Gpg.java    From OpenPGP-Card with GNU General Public License v3.0 4 votes vote down vote up
private void decrypt(APDU apdu) {
  byte[] buffer = apdu.getBuffer();
  // PW1 with 0x82
  if (!pins[PIN_INDEX_PW1].isValidated() || !pinSubmitted[1]) {
    ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
  }
  if (!confidentialityKey.getPrivate().isInitialized()) {
    ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
  }
  boolean firstCommand = (commandChainingBuffer[TEMP_INS] != buffer[ISO7816.OFFSET_INS]);
  // Mark the command chain as bad so it stays in this state in case of exception.
  short len = apdu.setIncomingAndReceive();
  if (len < 1) {
    ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
  }
  if (firstCommand) {
    Util.arrayCopyNonAtomic(buffer, (short) (ISO7816.OFFSET_CDATA + 1), commandChainingBuffer,
                            TEMP_GET_RESPONSE_DATA, (short) (len - 1));
    len = (short) (len - 1);
  } else {
    short existing = Util.getShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH);
    if ((short) (len + existing) > RSA_KEY_LENGTH_BYTES) {
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }
    Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, commandChainingBuffer,
                            (short) (TEMP_GET_RESPONSE_DATA + existing), len);
    len += existing;
  }
  if (len < RSA_KEY_LENGTH_BYTES) {
    commandChainingBuffer[TEMP_INS] = CMD_COMPUTE_PSO;
    Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH, len);
    return;  // For compatibily with GPG
  }
  // We have enough bytes to decrypt.
  cipherRSA.init(confidentialityKey.getPrivate(), Cipher.MODE_DECRYPT);
  len = cipherRSA.doFinal(commandChainingBuffer, TEMP_GET_RESPONSE_DATA, RSA_KEY_LENGTH_BYTES,
                          buffer, (short) 0);
  // Clear command chaining buffer to make ready for next operation.
  Util.arrayFillNonAtomic(commandChainingBuffer, (short) 0, (short) commandChainingBuffer.length, (byte) 0);
  apdu.setOutgoingAndSend((short) 0, len);
}
 
Example 13
Source File: Gpg.java    From OpenPGP-Card with GNU General Public License v3.0 4 votes vote down vote up
/**
 * GENERATE KEY APDU implementation.
 */
private void generateAsymetricKey(APDU apdu) {
  byte[] buffer = apdu.getBuffer();
  if (apdu.setIncomingAndReceive() != 2) {
    ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
  }
  KeyPair key = getKey(buffer[ISO7816.OFFSET_CDATA]);
  if (buffer[ISO7816.OFFSET_P1] == (byte) 0x81) {
    if (!(key.getPublic()).isInitialized()) {
      ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
    }
  } else {
    if (!pins[PIN_INDEX_PW3].isValidated()) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    JCSystem.beginTransaction();
    key.genKeyPair();
    if (buffer[ISO7816.OFFSET_CDATA] == (byte)0xB6) {
      signatureCounter[0] = 0;
      signatureCounter[1] = 0;
      signatureCounter[2] = 0;
    }
    JCSystem.commitTransaction();
  }
  // Send the TLV data and public exponent using the APDU buffer.
  buffer[ISO7816.OFFSET_CDATA] = 0x7F;
  buffer[(short) (ISO7816.OFFSET_CDATA + 1)] = 0x49;
  buffer[(short) (ISO7816.OFFSET_CDATA + 2)] = (byte) 0x82;
  buffer[(short) (ISO7816.OFFSET_CDATA + 5)] = (byte) 0x82;
  short length = ((RSAPublicKey) key.getPublic()).getExponent(
      buffer, (short) (ISO7816.OFFSET_CDATA + 7));
  buffer[(short) (ISO7816.OFFSET_CDATA + 6)] = (byte) length;
  short pos = (short) (ISO7816.OFFSET_CDATA + 7 + length);
  buffer[pos] = (byte) 0x81;
  buffer[(short) (pos + 1)] = (byte) 0x82;
  Util.setShort(buffer, (short) (pos + 2), RSA_KEY_LENGTH_BYTES);
  Util.setShort(buffer, (short) (ISO7816.OFFSET_CDATA + 3),
                (short) (pos + RSA_KEY_LENGTH_BYTES - ISO7816.OFFSET_CDATA - 1));
  apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short) (length + 11));

  // And the modulus using get response.
  Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH, RSA_KEY_LENGTH_BYTES);
  ((RSAPublicKey) key.getPublic()).getModulus(commandChainingBuffer, TEMP_GET_RESPONSE_DATA);
  // Skip leading zero byte.
  if (commandChainingBuffer[TEMP_GET_RESPONSE_DATA] == 0) {
    Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET,
                  (short) (TEMP_GET_RESPONSE_DATA + 1));
  } else {
    Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET, TEMP_GET_RESPONSE_DATA);
  }
  commandChainingBuffer[TEMP_INS] = buffer[ISO7816.OFFSET_INS];
  ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00);
}
 
Example 14
Source File: Ppse2Pay.java    From CardExamples with The Unlicense 4 votes vote down vote up
@Override
public void process(APDU apdu) throws ISOException {
    byte[] buf = apdu.getBuffer();

    if (selectingApplet()) {
        //check that LC is 0x0E
        if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != 0x0E)
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        //get the rest of the apdu and check length
        if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        if(FCI_TEMPLATE==null)
        {
            FCI_TEMPLATE = new byte[12 + ADF.length];
            FCI_TEMPLATE[0]=(byte)0xA5; //FCI Proprietary Template
            FCI_TEMPLATE[1]=(byte)(10 + ADF.length);   //length
            FCI_TEMPLATE[2]=(byte)0xBF; //FCI Issuer Discretionary Data
            FCI_TEMPLATE[3]=(byte)0x0C;
            FCI_TEMPLATE[4]=(byte)(7 + ADF.length);   //length

            FCI_TEMPLATE[5]=(byte)0x61; //Directory Entry
            FCI_TEMPLATE[6]=(byte)(ADF.length + 5);   //length
            FCI_TEMPLATE[7]=(byte)0x4F; //ADF Name
            FCI_TEMPLATE[8]=(byte)(ADF.length);    //length
            for(short i=0;i<ADF.length;i++)
                FCI_TEMPLATE[9+i] = ADF[i];
            FCI_TEMPLATE[9 + ADF.length]=(byte)0x87; //Application Priority Indicator
            FCI_TEMPLATE[10 + ADF.length]=(byte)1;    //length
            FCI_TEMPLATE[11 + ADF.length]=(byte)0x01;
        }

        //return FCI upon successful select
        apdu.setOutgoing();

        buf[0]=(byte)0x6F; //FCI Template
        buf[1]=(byte)(2 + DF.length + FCI_TEMPLATE.length);   //length
        buf[2]=(byte)0x84; //DF Name
        buf[3]=(byte)DF.length;   //length
        for(short i=0;i<DF.length;i++)
            buf[4+i] = DF[i];
        for(short i=0;i<FCI_TEMPLATE.length;i++)
            buf[4 + DF.length + i] = FCI_TEMPLATE[i];
        apdu.setOutgoingLength((short)(4 + DF.length + FCI_TEMPLATE.length));
        apdu.sendBytes((short)0,(short)(4 + DF.length + FCI_TEMPLATE.length));
        return;
    }

    switch (buf[ISO7816.OFFSET_INS]) {

        case (byte) 0xA4: //select PPSE
            //check that P1 & P2 are correct
            if(buf[ISO7816.OFFSET_P1] != (byte) 0x04 || buf[ISO7816.OFFSET_P2] != (byte) 0x00)
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);

            //check that LC is 0x0E
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != 0x0E)
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            //get the rest of the apdu and check length
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
                //otherwise, the file name was wrong for this select
            else ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

        case (byte) 0xEE: //loopback
            //check that P1 & P2 are correct
            if(buf[ISO7816.OFFSET_P1] != (byte) 0x00 || buf[ISO7816.OFFSET_P2] != (byte) 0x00)
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);

            //check that the length byte is within the spec (1-250)
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) < 1 || (short)(buf[ISO7816.OFFSET_LC] & 0xFF) > 250)
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            //get the rest of the apdu and check length
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            short len = buf[ISO7816.OFFSET_LC];
            for(short i=0;i<len;i++)
                buf[i] = buf[i+5];
            apdu.setOutgoingLength(len);
            apdu.sendBytes((short)0,len);
            break;

        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }


}
 
Example 15
Source File: Ppse2Pay.java    From CardExamples with The Unlicense 4 votes vote down vote up
@Override
public void process(APDU apdu) throws ISOException {
    byte[] buf = apdu.getBuffer();

    if (selectingApplet()) {
        //check that LC is 0x0E
        if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != 0x0E)
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        //get the rest of the apdu and check length
        if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        if(FCI_TEMPLATE==null)
        {
            FCI_TEMPLATE = new byte[12 + ADF.length];
            FCI_TEMPLATE[0]=(byte)0xA5; //FCI Proprietary Template
            FCI_TEMPLATE[1]=(byte)(10 + ADF.length);   //length
            FCI_TEMPLATE[2]=(byte)0xBF; //FCI Issuer Discretionary Data
            FCI_TEMPLATE[3]=(byte)0x0C;
            FCI_TEMPLATE[4]=(byte)(7 + ADF.length);   //length

            FCI_TEMPLATE[5]=(byte)0x61; //Directory Entry
            FCI_TEMPLATE[6]=(byte)(ADF.length + 5);   //length
            FCI_TEMPLATE[7]=(byte)0x4F; //ADF Name
            FCI_TEMPLATE[8]=(byte)(ADF.length);    //length
            for(short i=0;i<ADF.length;i++)
                FCI_TEMPLATE[9+i] = ADF[i];
            FCI_TEMPLATE[9 + ADF.length]=(byte)0x87; //Application Priority Indicator
            FCI_TEMPLATE[10 + ADF.length]=(byte)1;    //length
            FCI_TEMPLATE[11 + ADF.length]=(byte)0x01;
        }

        //return FCI upon successful select
        apdu.setOutgoing();

        buf[0]=(byte)0x6F; //FCI Template
        buf[1]=(byte)(2 + DF.length + FCI_TEMPLATE.length);   //length
        buf[2]=(byte)0x84; //DF Name
        buf[3]=(byte)DF.length;   //length
        for(short i=0;i<DF.length;i++)
            buf[4+i] = DF[i];
        for(short i=0;i<FCI_TEMPLATE.length;i++)
            buf[4 + DF.length + i] = FCI_TEMPLATE[i];
        apdu.setOutgoingLength((short)(4 + DF.length + FCI_TEMPLATE.length));
        apdu.sendBytes((short)0,(short)(4 + DF.length + FCI_TEMPLATE.length));
        return;
    }

    switch (buf[ISO7816.OFFSET_INS]) {

        case (byte) 0xA4: //select PPSE
            //check that P1 & P2 are correct
            if(buf[ISO7816.OFFSET_P1] != (byte) 0x04 || buf[ISO7816.OFFSET_P2] != (byte) 0x00)
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);

            //check that LC is 0x0E
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != 0x0E)
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            //get the rest of the apdu and check length
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
                //otherwise, the file name was wrong for this select
            else ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

        case (byte) 0xEE: //loopback
            //check that P1 & P2 are correct
            if(buf[ISO7816.OFFSET_P1] != (byte) 0x00 || buf[ISO7816.OFFSET_P2] != (byte) 0x00)
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);

            //check that the length byte is within the spec (1-250)
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) < 1 || (short)(buf[ISO7816.OFFSET_LC] & 0xFF) > 250)
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            //get the rest of the apdu and check length
            if((short)(buf[ISO7816.OFFSET_LC] & 0xFF) != apdu.setIncomingAndReceive())
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

            short len = buf[ISO7816.OFFSET_LC];
            for(short i=0;i<len;i++)
                buf[i] = buf[i+5];
            apdu.setOutgoingLength(len);
            apdu.sendBytes((short)0,len);
            break;

        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }


}