Exemplo n.º 1
0
        /*
         *
         * @throws SmartCardException
         */

        /**
         * BaseSigner interface for the requested certificate. Do not forget to logout after your crypto
         * operation finished
         * @param aCardPIN
         * @param aCert
         * @return
         * @throws SmartCardException
         */

        public SmartCardManager()
        {
            try
            {
                LOGGER.Debug("New SmartCardManager will be created");
                String terminal;

                int      index     = 0;
                String[] terminals = SmartOp.getCardTerminals();

                if (terminals == null || terminals.Length == 0)
                {
                    MesajiIsle("Kart takılı kart okuyucu bulunamadı (SmartCardManager)", 1);
                    Program.KartOkuyucuYok = 1;
                    return;
                    // throw new SmartCardException("Kart takılı kart okuyucu bulunamadı");
                }

                LOGGER.Debug("Kart okuyucu sayısı : " + terminals.Length);
                if (terminals.Length != Program.TerminalSayisi && Program.TerminalSayisi != 0)
                {
                    MesajiIsle("Kart seçildikten sonra imzalama aşamasında yeni kart okuyucu takıldı.", 1);
                    Program.KartOkuyucuYok = 1;
                    return;
                }

                // MesajiIsle("Bilgi 1 - Terminal: " + terminal, 0);
                try
                {  // karttipi bastan parametre ile gelmisse
                    if (Program.ParamCardType != "")
                    {
                        Program.ParamSlotID = SmartOp.findSlotNumber(CardTypeConverter.AsCardType(Program.ParamCardType)).ToString();
                        bsc           = new P11SmartCard(CardTypeConverter.AsCardType(Program.ParamCardType));
                        mSerialNumber = StringUtil.ToString(bsc.getSerial(Convert.ToInt64(Program.ParamSlotID)));
                        bsc.openSession(Convert.ToInt64(Program.ParamSlotID));

                        Program.CardType = Program.ParamCardType;
                    }
                    else
                    {
                        if (terminals.Length == 1)
                        {
                            terminal = terminals[index];
                        }
                        else
                        {
                            index    = askOption(null, null, terminals, "Okuyucu Listesi", new String[] { "Tamam" });
                            terminal = terminals[index];
                        }
                        // burada try catch gerek olmadan kart tipi ve slot id tesbit ediliyor...
                        // ama sadece akis icin calisiyor, safesign da calismadi
                        Pair <long, CardType> slotAndCardType = SmartOp.getSlotAndCardType(terminal);
                        //  MesajiIsle("Bilgi 2 - Terminal: " + terminal + " SmartCard Type: " + slotAndCardType.getmObj2().ToString() + " SlotID: " + slotAndCardType.getmObj1().ToString(), 0);
                        // bulunan kart type kullanilarak kart yapisi olusturuluyor
                        bsc = new P11SmartCard(slotAndCardType.getmObj2());
                        // olusturulan kart yapisi bulunan slotid kullanilarak aciliyor
                        bsc.openSession(slotAndCardType.getmObj1());
                        Program.ParamSlotID = slotAndCardType.getmObj1().ToString();
                        Program.CardType    = slotAndCardType.getmObj2().ToString();
                        Program.Terminal    = terminal;
                    }
                }
                catch
                {
                    // etugra
                    //bsc = new P11SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.SAFESIGN);
                    //bsc.openSession(52481);
                    //MessageBox.Show(index.ToString() + " nolu terminal serino");
                    //MessageBox.Show(StringUtil.ToString(bsc.getSerial()));
                    //MessageBox.Show("Serino gösterdi");
                    // continue;
                    // bu slot id belirleme ve open session kismini, manuel imzalamada signerhelp icerisine aldim, yoksa
                    // burada acilan sessioni gormuyordu bir sekilde. bu kisim sertifika okuma ozelligi cozulebilirse iptal edilebilir belki...

                    long[] PresentSlots;
                    // long[] PresentSerials;
                    try
                    {
                        sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.AKIS);
                        if (Program.ParamSlotID == "")
                        {
                            FindSlotID();
                        }

                        string s = new string(sc.getSlotInfo(Convert.ToInt64(Program.ParamSlotID)).slotDescription);
                        s = new string(sc.getSlotInfo(Convert.ToInt64(Program.ParamSlotID)).manufacturerID);
                        s = sc.getSlotInfo(Convert.ToInt64(Program.ParamSlotID)).ToString();
                        // MesajiIsle("slotDescription (SlotID(" +Program.ParamSlotID+"): "+ s, 0);
                        //Program.ParamSlotIndex = index.ToString();
                        Program.CardType = sc.getCardType().ToString();
                        bsc = new P11SmartCard(sc.getCardType());
                        // MesajiIsle("Bilgi 3 - SmartCard Type: " + sc.getCardType().ToString() + " SlotID: " + Program.ParamSlotID, 0);
                        bsc.openSession(Convert.ToInt64(Program.ParamSlotID));
                    }
                    catch
                    {
                        try
                        {
                            sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.SAFESIGN);
                            if (Program.ParamSlotID == "")
                            {
                                FindSlotID();
                            }

                            string s = new string(sc.getSlotInfo(Convert.ToInt64(Program.ParamSlotID)).slotDescription);
                            // MesajiIsle("slotDescription (SlotID(" +Program.ParamSlotID+"): "+ s, 0);
                            //Program.ParamSlotIndex = index.ToString();
                            Program.CardType = sc.getCardType().ToString();
                            bsc = new P11SmartCard(sc.getCardType());
                            // MesajiIsle("Bilgi 3 - SmartCard Type: " + sc.getCardType().ToString() + " SlotID: " + Program.ParamSlotID, 0);
                            bsc.openSession(Convert.ToInt64(Program.ParamSlotID));
                        }
                        catch
                        {
                            try
                            {
                                sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.GEMPLUS);
                            }
                            catch
                            {
                                try
                                {
                                    sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.TKART);
                                }
                                catch
                                {
                                    try
                                    {
                                        sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.ALADDIN);
                                    }
                                    catch
                                    {
                                        try
                                        {
                                            sc = new SmartCard(tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType.SEFIROT);
                                            //PresentSlots = sc.getTokenPresentSlotList(); // tokenli slot listesini al
                                            //for (m = 0; m < PresentSlots.Length; m++)
                                            //{
                                            //    ListSmartCard.Add(new P11SmartCard(sc.getCardType()));
                                            //    ListSmartCard[index].openSession(PresentSlots[m]); // etugra icin 52481
                                            //    tekkartSerialNumber = StringUtil.ToString(ListSmartCard[index].getSerial());
                                            //    // sertifika getirme islemi
                                            //    ImzaSertifikasiGetirTek(true, false, index);
                                            //    PresentSlots[m].ToString();
                                            //    index = index + 1;
                                            //}
                                        }
                                        catch
                                        { }
                                    }
                                }
                            }
                        }
                    }
                    if (sc != null)
                    {
                        PresentSlots = sc.getTokenPresentSlotList(); // tokenli slot listesini al
                        // PresentSerials = sc.getTokenSerialNo();
                        // secim kutusu haline getirerek slotid al
                        index = 0;
                        // long SlotID = 0;
                        if (PresentSlots.Length == 1)
                        {
                            Program.ParamSlotID = PresentSlots[index].ToString();
                        }
                        else
                        {
                            Program.ParamSlotID = askOptionValue(null, null, PresentSlots, "Slot Listesi", new String[] { "Tamam" });
                        }
                        // sc.getSlotInfo(slots[0]).slotDescription;
                        string s = new string(sc.getSlotInfo(Convert.ToInt64(Program.ParamSlotID)).slotDescription);
                        // MesajiIsle("slotDescription (SlotID(" +Program.ParamSlotID+"): "+ s, 0);
                        //Program.ParamSlotIndex = index.ToString();
                        Program.CardType = sc.getCardType().ToString();
                        bsc = new P11SmartCard(sc.getCardType());
                        // MesajiIsle("Bilgi 3 - SmartCard Type: " + sc.getCardType().ToString() + " SlotID: " + Program.ParamSlotID, 0);
                        bsc.openSession(Convert.ToInt64(Program.ParamSlotID));

                        //bsc.openSession(SlotID);
                        //MessageBox.Show("bsc.login(5255)");
                        //bsc.login("5255");
                        //MessageBox.Show("login ok");
                    }
                    else
                    {
                        MesajiIsle("Kart tipi belirlenemedi", 0);
                    }
                }

                mSerialNumber = StringUtil.ToString(bsc.getSerial());
                mSlotCount    = terminals.Length;
            }
            catch (SmartCardException e)
            {
                throw e;
            }
            catch (PKCS11Exception e)
            {
                throw new SmartCardException("Pkcs11 exception", e);
            }
            catch (IOException e)
            {
                throw new SmartCardException("Smart Card IO exception - Detay bilgilerine bakınız", e);
            }
        }