Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

xamarin - SW=6985 error in middle of the reading ePassport (DG5,DG7)

I'm developing Xamarin.Android app for reading Epassport data and faced to problem: Often when i`m reading with secure messaging big DataGroup (DG5,DG7) in middle of reading Epassport gives RAPDU = 6985 (0x6985, 0x69 0x85), but sometimes all works (~65% cases).

I'm tried write isoDep.SetTimeout(2000); but this didn't help, SSC always right increment in VerificationMessage and SendSecureMessageAPDU, KS_Enc and KS_MAC don't changes, I don't know why Epassport give me 6985, help me

    // Read first 4 bytes for FileLength
     
    byte[] Header4, Header5;
            DO97 = new byte[] { 0x97, 0x01, 0x80 }; // 0x80 = 126
            byte i = 0;



            //RAPDU length of data Epassport = 255 but need 256, so 256 = 128  * 2;
            // Problem arises here in WHILE
            while (i != FileLength[0] && FileLength.Length >= 2) // Need for big files (DG5 photo /DG7 signature) 
            {
                //Read first 128 bytes
               Header4 = new byte[] { 0x0C, 0xB0, i, 0x04, 0x80, 0x00, 0x00, 0x00 }; // offset 0x04 because first 4 bytes for lenght 
                var result4 = SendSecureMessageAPDU(Header4, DO97);  // ERROR HERE 
                VerificationRAPDU(result4);
                Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result4, KS_Enc)).SkipLast(24).ToArray();

                //Read last 128 bytes
               Header5 = new byte[] { 0x0C, 0xB0, i, 0x84, 0x80, 0x00, 0x00, 0x00 };             
                var result5 = SendSecureMessageAPDU(Header5, DO97); // OR ERROR HERE !!!!!
                VerificationRAPDU(result5);
                Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result5, KS_Enc)).SkipLast(24).ToArray();

                i++;
            }

            // For short file (Length <255 bytes) and last block bigfile ( where length also <255 bytes) 
            byte[] Header6 = new byte[] { 0x0C, 0xB0, i, 0x04, 0x80, 0x00, 0x00, 0x00 };

            // Header6 = new byte[] { 0x0C, 0xB0, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00 };

            if (FileLength.Length > 1)
            {
                DO97 = new byte[] { 0x97, 0x01, FileLength[1] };
            }
            else
            {
                DO97 = new byte[] { 0x97, 0x01, FileLength[0] };
            }

            var result6 = SendSecureMessageAPDU(Header6, DO97);
            VerificationRAPDU(result6);

            Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result6, KS_Enc)).SkipLast(16).ToArray();
            string testDG = BitConverter.ToString(Data_DG).Replace("-", "");

            var asddas = Encoding.UTF8.GetString(Data_DG);

            return Data_DG;
        }
 public byte[] SendSecureMessageAPDU(byte[] Header, byte[] buildDO)
        {
            byte[] M = Header.Concat(buildDO).ToArray();

            // Вычисление MAC от M:
            string sM = BitConverter.ToString(M);

            if (SSC[SSC.Length - 2] == 255)
            {
                SSC[SSC.Length - 3]++;
                SSC[SSC.Length - 2]++;
                SSC[SSC.Length - 1]++;
            }
            else if (SSC[SSC.Length - 1] == 255)
            {
                SSC[SSC.Length - 2]++;
                SSC[SSC.Length - 1]++;
            }
            else
                SSC[SSC.Length - 1]++;

            byte[] N = SSC.Concat(Header)
                        .Concat(buildDO)  
                        .ToArray();

            byte[] CC = MAC(N, KS_MAC);
           
            byte[] DO8E = new byte[] { 0x8E, 0x08, };
            DO8E = DO8E.Concat(CC).ToArray();

            string sD08E = BitConverter.ToString(DO8E);

            // build secure command
            var command = Header.Take(4).ToArray()
                           .Concat(new byte[] { (byte)(buildDO.Length + DO8E.Length) }) // Length
                           .Concat(buildDO)
                           .Concat(DO8E)
                           .Concat(new byte[] { 0x00 })
                           .ToArray();

            var result = isoDep.Transceive(command).ToArray(); // Error RAPDU = 6985

            if (result.Length == 2)  
            {               
                throw new RAPDU_Exception("Error RAPDU ePassport");
            }

            return result;
        }
question from:https://stackoverflow.com/questions/66049076/sw-6985-error-in-middle-of-the-reading-epassport-dg5-dg7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...