Exemple #1
0
        public MFTestResults CryptokiTest_SlotList()
        {
            // this test will throw an exception if the slot list or slot info is incorrect.
            MFTestResults res = MFTestResults.Pass;

            try
            {
                Slot[] list = Cryptoki.Cryptoki.SlotList;

                int cnt = list.Length;

                for (int i = 0; i < cnt; i++)
                {
                    Slot slot = list[i];
                    bool fDigestExists = false;

                    slot.SlotEvent += new Slot.OnSlotEvent(slot_SlotEvent);

                    Debug.Print("Slot " + i.ToString());
                    Debug.Print("  Desc: " + slot.Info.Description);
                    Debug.Print("  Manu: " + slot.Info.ManufactureID);
                    Debug.Print("  Flgs: " + slot.Info.Flags);
                    Debug.Print("  HVer: " + slot.Info.HardwareVersion.Major.ToString() + "." + list[i].Info.HardwareVersion.Minor.ToString());
                    Debug.Print("  FVer: " + slot.Info.FirmwareVersion.Major.ToString() + "." + list[i].Info.FirmwareVersion.Minor.ToString());

                    MechanismType[] mechs = slot.SupportedMechanisms;

                    Debug.Print("  Mechs: " + slot.Info.Description);
                    for (int j = 0; j < mechs.Length; j++)
                    {
                        Debug.Print("    " + GetMechString(mechs[j]));

                        if (mechs[j] == MechanismType.SHA256)
                            fDigestExists = true;
                    }

                    if(0 != (slot.Info.Flags & Slot.SlotFlag.TokenPresent))
                    {
                        TokenInfo info = new TokenInfo();

                        slot.GetTokenInfo(ref info);


                        Debug.Print("Token Info: ");
                        Debug.Print("  Labl  : " + info.Label);
                        Debug.Print("  Model : " + info.Model);
                        Debug.Print("  S/N   : " + info.SerialNumber);
                        Debug.Print("  Manu  : " + info.Manufacturer);
                        Debug.Print("  Flgs  : " + info.Flags);
                        Debug.Print("  PriMem: " + info.FreePrivateMemory);
                        Debug.Print("  PubMem: " + info.FreePublicMemory);
                        Debug.Print("  MaxPin: " + info.MaxPinLen);
                        Debug.Print("  MinPin: " + info.MinPinLen);
                        Debug.Print("  MaxRW : " + info.MaxRwSessionCount);
                        Debug.Print("  MaxRO : " + info.MaxSessionCount);
                        Debug.Print("  SesCnt: " + info.SessionCount);
                        Debug.Print("  TPrMem: " + info.TotalPrivateMemory);
                        Debug.Print("  TPuMem: " + info.TotalPublicMemory);
                        Debug.Print("  Utc   : " + info.UtcTime.ToString());
                        Debug.Print("  HVer  : " + info.HardwareVersion.Major.ToString() + "." + info.HardwareVersion.Minor.ToString());
                        Debug.Print("  FVer  : " + info.FirmwareVersion.Major.ToString() + "." + info.FirmwareVersion.Minor.ToString());

                        try
                        {
                            slot.InitializeToken("TokenPin", "label");
                        }
                        catch
                        {
                            Debug.Print("Warning: InitializeToken Failed");
                        }

                    }

                    using (Session sess = slot.OpenSession(Session.SessionFlag.ReadOnly))
                    {
                        if (fDigestExists)
                        {
                            HashAlgorithm sha = new HashAlgorithm(HashAlgorithmType.SHA256, sess);

                            byte[] data1 = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes("Hello World"));
                            byte[] data2 = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes("Hello World "));

                            bool success = false;
                            for (int k = 0; k < data1.Length; k++)
                            {
                                if (data1[k] != data2[k])
                                {
                                    success = true;
                                    break;
                                }
                            }
                            if (!success) res = MFTestResults.Fail;
                        }
                    }

                    slot.SlotEvent -= new Slot.OnSlotEvent(slot_SlotEvent);
                }
            }
            catch(Exception ex)
            {
                Log.Exception("Unexpected", ex);
                res = MFTestResults.Fail;
            }

            return res;
        }
 public extern void GetTokenInfo(ref TokenInfo info);