Пример #1
0
        public static bool IsJPKICardExist()
        {
            logger.Debug("IsJPKICardExist");
            bool ret = false;

            try {
                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // SELECT AP
                    if (reader.SendandResponse(APDU_SELECT_AP).IsSuccess == false)
                    {
                        throw (new Exception("SELECT AP Error"));
                    }
                }
                ret = true;
            } catch (Exception ex) {
                logger.Error(ex);
            }
            return(ret);
        }
Пример #2
0
        private static byte[] getEF(byte[] apduSelectMF)
        {
            logger.Debug("getEF");
            byte[] certDER = null;

            try {
                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // SELECT AP
                    if (reader.SendandResponse(APDU_SELECT_AP).IsSuccess == false)
                    {
                        throw (new Exception("SELECT AP Error"));
                    }

                    // select MF
                    if (reader.SendandResponse(apduSelectMF).IsSuccess == false)
                    {
                        throw (new Exception("SELECT MF Error"));
                    }

                    // READ Cert
                    certDER = readCert(reader);
                }
            } catch (Exception ex) {
                logger.Error(ex);
            }
            return(certDER);
        }
Пример #3
0
        private static byte[] getEFwidhPIN(byte[] apduSelectMF, byte[] apduSelectPIN, string pin)
        {
            logger.Debug("getEFwidhPIN");
            byte[] certDER = null;

            try {
                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // SELECT AP
                    if (reader.SendandResponse(APDU_SELECT_AP).IsSuccess == false)
                    {
                        throw (new Exception("SELECT AP Error"));
                    }

                    // SELECT PIN IDF
                    if (reader.SendandResponse(apduSelectPIN).IsSuccess == false)
                    {
                        throw (new Exception("SELECT PIN IDF Error"));
                    }

                    // VERIFY PIN
                    {
                        byte[] pinbyte = System.Text.Encoding.ASCII.GetBytes(pin);

                        var apdu = new List <byte>();
                        apdu.AddRange(new List <byte> {
                            0x00, 0x20, 0x00, 0x80
                        });
                        apdu.Add((byte)pinbyte.Length);
                        apdu.AddRange(pinbyte.ToList());

                        // send
                        if (reader.SendandResponse(apdu.ToArray()).IsSuccess == false)
                        {
                            throw (new Exception("VERIFY PIN Error"));
                        }
                    }

                    // select MF
                    if (reader.SendandResponse(apduSelectMF).IsSuccess == false)
                    {
                        throw (new Exception("SELECT MF Error"));
                    }

                    // READ Cert
                    certDER = readCert(reader);
                }
            } catch (Exception ex) {
                logger.Error(ex);
            }
            return(certDER);
        }
Пример #4
0
        private static int getPINRetryCount(byte[] apduSelectMF)
        {
            logger.Debug("<<<getPINRetryCount>>>");
            int retrycount = -1;

            try {
                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // SELECT AP
                    if (reader.SendandResponse(APDU_SELECT_AP).IsSuccess == false)
                    {
                        throw (new Exception("SELECT AP Error"));
                    }

                    // SELECT MF
                    if (reader.SendandResponse(apduSelectMF).IsSuccess == false)
                    {
                        throw (new Exception("SELECT MF Error"));
                    }

                    // VERIFY
                    var res = reader.SendandResponse(new byte[] { 0x00, 0x20, 0x00, 0x80 });
                    if (res.Sw1 == 0x63)
                    {
                        retrycount = res.Sw2 & 0xF;
                    }
                }
            } catch (Exception ex) {
                logger.Error(ex);
                return(-9);
            }
            return(retrycount);
        }
Пример #5
0
        public static byte[] GetCardUID()
        {
            logger.Debug("<<<GetCardUID>>>");
            byte[] uid = null;
            try {
                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // get UID
                    var response = reader.SendandResponse(new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 });
                    if (response.IsSuccess)
                    {
                        uid = response.Data;
                    }
                }
            } catch (Exception ex) {
                logger.Debug(ex);
            }
            return(uid);
        }
Пример #6
0
        private static byte[] signature(string pin, byte[] digestSHA1, byte[] apduSelectPIN, byte[] apduSelectKey)
        {
            byte[] signature = null;

            try {
                if (pin.Length <= 0)
                {
                    throw new Exception("Error PIN_REQUIRED");
                }

                logger.Debug("DIGEST SHA1 ---");
                logger.Debug(Common.BytesToHexString(digestSHA1));
                logger.Debug("--- DIGEST SHA1");

                var digestInfo = createDigestInfo(digestSHA1);

                logger.Debug("DIGESTINFO ---");
                logger.Debug(Common.BytesToHexString(digestInfo));
                logger.Debug("--- DIGESTINFO");

                using (var reader = new ICReader()) {
                    // CONNECT
                    if (reader.Connect() == false)
                    {
                        throw (new Exception("Connect Error"));
                    }

                    // SELECT AP
                    if (reader.SendandResponse(APDU_SELECT_AP).IsSuccess == false)
                    {
                        throw (new Exception("SELECT AP Error"));
                    }

                    // SELECT PIN IDF
                    if (reader.SendandResponse(apduSelectPIN).IsSuccess == false)
                    {
                        throw (new Exception("SELECT PIN IDF Error"));
                    }

                    // VERIFY PIN
                    {
                        byte[] pinbyte = System.Text.Encoding.ASCII.GetBytes(pin);

                        var apdu = new List <byte>();
                        apdu.AddRange(new List <byte> {
                            0x00, 0x20, 0x00, 0x80
                        });
                        apdu.Add((byte)pinbyte.Length);
                        apdu.AddRange(pinbyte.ToList());

                        // send
                        if (reader.SendandResponse(apdu.ToArray()).IsSuccess == false)
                        {
                            throw (new Exception("VERIFY PIN Error"));
                        }
                    }

                    // SELECT 秘密鍵IEF
                    if (reader.SendandResponse(apduSelectKey).IsSuccess == false)
                    {
                        throw (new Exception("SELECT MF Error"));
                    }

                    // COMPUTE DIGITAL SIGNATURE
                    // < 80 2A 00 80 [DigestInfo]
                    // > [SIGNATURE]
                    {
                        var apdu = new List <byte>();
                        apdu.AddRange(new List <byte> {
                            0x80, 0x2A, 0x00, 0x80
                        });
                        apdu.Add((byte)digestInfo.Length);
                        apdu.AddRange(digestInfo.ToList());
                        apdu.Add((byte)0x00);

                        var res = reader.SendandResponse(apdu.ToArray());
                        if (res.IsSuccess == false)
                        {
                            throw (new Exception("SIGNATURE Error"));
                        }
                        signature = res.Data;
                    }
                }
            } catch (Exception ex) {
                logger.Debug(ex);
            }
            return(signature);
        }
Пример #7
0
        private static byte[] readCert(ICReader reader)
        {
            var certDER = new List <byte>();

            // READ BINARY
            int datasize = 0;
            {
                // http://www.geocities.co.jp/SiliconValley-SanJose/3377/asn1Body.html
                // ブロックの最初の4byteを読む
                // ⇒30:82:06:2B
                // 30 = タグ
                //  ‭0011-0000
                //  00          b8-b7:クラス        00   = 汎用
                //    1         b6   :構造化フラグ  1    = 構造型
                //     1-0000   b5-b1:タグ番号      0x10 = SEQUENCE(ASN.1 オブジェクトの集合を表記するための型)

                // 82 = 値の長さ1(レングス)
                //  ‭1000-0010
                //‬  1           b8   :              1    = 128オクテット(byte)以上
                //   000-0010   b7-b1:              0x02 = 長さ部の長さ = 2byte
                //                                          ※この後2byteが値の部分の長さという意味

                // 06:2B = 値の長さ2(レングス)
                //  dec = 1579                      値の長さは1579byte
                // ※DERデータが1579byte、という意味(この4byteは含まれない)

                var response = reader.SendandResponse(new byte[] { 0x00, 0xB0, 0x00, 0x00, 0x04 });
                if (response.IsSuccess == false)
                {
                    throw (new Exception("READ BINARY Error"));
                }

                // blockData-4byte + status-2byte
                datasize = ChangeEndian.Reverse(BitConverter.ToUInt16(response.Data, 2));

                // add header-4byte
                datasize = datasize + 4;
            }

            // get block num
            int blocksize = 256;            // 決めうち!
            int blocknum  = (int)Math.Ceiling(datasize / (double)blocksize);

            {
                var apdu = new byte[] { 0x00, 0xB0, 0x00, 0x00, 0x00 };
                for (int intIc = 0; intIc < blocknum; intIc++)
                {
                    apdu[2] = (byte)intIc;
                    var response = reader.SendandResponse(apdu);
                    if (response.IsSuccess == false)
                    {
                        throw (new Exception("READ BINARY Error"));
                    }
                    // blockdata(256byte)
                    certDER.AddRange(response.Data.ToList());
                }
            }
            certDER = certDER.Take(datasize).ToList();

            // log
            //ParseCert(certDER.ToArray());

            return(certDER.ToArray());
        }