Esempio n. 1
0
        public APDUResponse Transmit(APDUCommand apduCmd)
        {
            try
            {
                Core.Smartcard.APDUCommand apduCommand = new Core.Smartcard.APDUCommand(
                    apduCmd.Class,
                    apduCmd.Ins,
                    apduCmd.P1,
                    apduCmd.P2,
                    (apduCmd.Data == null || apduCmd.Data.Length == 0) ? null : apduCmd.Data,
                    apduCmd.Le);

                Core.Smartcard.APDUResponse apduResponse = card.Transmit(apduCommand);

                return(new APDUResponse()
                {
                    Data = apduResponse.Data,
                    SW1 = apduResponse.SW1,
                    SW2 = apduResponse.SW2
                });
            }
            catch (SmartCardException scEx)
            {
                SmartcardFault scFault = new SmartcardFault(scEx);
                throw new FaultException <SmartcardFault>(scFault);
            }
            catch (Exception ex)
            {
                GeneralFault genFault = new GeneralFault(ex);
                throw new FaultException <GeneralFault>(genFault);
            }
        }
        /// <summary>
        /// Process an APDU sequence and execute each of its commands in the sequence order
        /// </summary>
        /// <param name="apduSequenceName">Name of the sequence to play</param>
        /// <param name="seqParam">An array of SequenceParam object used as parameters for the sequence</param>
        /// <returns>APDUResponse object of the last command executed</returns>
        //public APDUResponse ProcessSequence(string apduSequenceName,  Dictionary<string, string> seqParam)
        public APDUResponse ProcessSequence(string apduSequenceName, SequenceParameter seqParam)
        {
            APDUResponse apduResp = null;
            //Dictionary<string, string> l_seqParam = null;
            SequenceParameter l_seqParam = null;

            // Get the sequence
            XmlNode apduSeq = SequenceByName(apduSequenceName);

            if (apduSeq == null)
            {
                throw new ApduCommandException(ApduCommandException.NoSuchSequence);
            }

            // Process the params of the sequence
            l_seqParam = ProcessParams(seqParam, apduSeq.Attributes);

            // Get the list of commands to execute
            XmlNodeList xmlCmdList = apduSeq.ChildNodes;

            for (int nI = 0; nI < xmlCmdList.Count; nI++)
            {
                apduResp = ProcessSeqCmd(xmlCmdList.Item(nI), l_seqParam);
            }

            return(apduResp);
        }
        /// <summary>
        /// Executes an APDU command
        /// </summary>
        /// <param name="apduCmd">APDUCommand object to execute</param>
        /// <returns>APDUResponse object of the response</returns>
        public APDUResponse ExecuteApduCommand(APDUCommand apduCmd)
        {
            byte bLe = 0;

            // Send the command
            m_apduResp = Card.Transmit(apduCmd);
            AddLog(new APDULog(apduCmd, m_apduResp));

            // Check if SW2 can be used as Le for the next call
            if (m_apduResp.SW1 == 0x9F)
            {
                m_bLeSW2 = true;
            }
            else
            {
                m_bLeSW2 = false;
            }

            if (m_bReplay)
            {
                if (m_bCheckSW1 && (m_apduResp.SW1 == m_bSW1Cond))
                {
                    // Replay the command with Le = SW2 of response
                    bLe         = m_apduResp.SW2;
                    m_bCheckSW1 = false;
                }
                else if (m_bLeData)
                {
                    // Replay the command with Le = Le + Data[m_nDataId - 1] of response
                    bLe       = (byte)(m_apduResp.Data[m_nDataId - 1] + apduCmd.Le);
                    m_bLeData = false;
                }

                // Replay the command
                apduCmd = new APDUCommand(
                    apduCmd.Class,
                    apduCmd.Ins,
                    apduCmd.P1,
                    apduCmd.P2,
                    apduCmd.Data,
                    bLe);

                m_apduResp = Card.Transmit(apduCmd);
                AddLog(new APDULog(apduCmd, m_apduResp));

                m_bReplay = false;
            }

            return(m_apduResp);
        }
        /// <summary>
        /// Process a command of a sequence of APDU
        /// </summary>
        /// <param name="xmlCmd">XML node representing the command</param>
        /// <param name="seqParam">List of parameters of the sequence</param>
        /// <returns>APDUResponse object of command executed</returns>
//        private APDUResponse ProcessSeqCmd(XmlNode xmlCmd, Dictionary<string, string> seqParam)
        private APDUResponse ProcessSeqCmd(XmlNode xmlCmd, SequenceParameter seqParam)
        {
            bool         bApdu     = false;
            bool         bSeq      = false;
            string       sApduName = null;
            string       sSeqName  = null;
            APDUResponse apduResp  = null;

            // Get the APDU or Sequence name
            try
            {
                // Get the Apdu name to run
                sApduName = xmlCmd.Attributes[xmlAttrApdu].Value;
                bApdu     = true;
            }
            catch
            {
            }
            finally
            {
                try
                {
                    sSeqName = xmlCmd.Attributes[xmlAttrSequence].Value;
                    bSeq     = true;
                }
                catch
                {
                }

                if ((bSeq | bApdu) == false)
                {
                    throw new ApduCommandException(ApduCommandException.MissingApduOrCommand);
                }
            }

            if (bApdu)
            {
                APDUParam apduParams = BuildCommandParam(xmlCmd.Attributes, seqParam);
                apduResp = ProcessCommand(sApduName, apduParams);
            }

            if (bSeq)
            {
                // Process a sub sequence
                apduResp = ProcessSequence(sSeqName, seqParam);
            }

            return(apduResp);
        }
Esempio n. 5
0
 public APDULog(APDUCommand apduCmd, APDUResponse apduResp)
 {
     m_apduCmd  = apduCmd;
     m_apduResp = apduResp;
 }