Beispiel #1
0
        //public static DeviceManager deviceManager = new DeviceManager(false);

        public SmartCardTest()
        {
            List <CardInfo> lst        = SmartCardUtils.GetReaderNames();
            String          readerName = lst[0].ReaderName;

            smartCard = new SmartCard(readerName, "5304");
        }
Beispiel #2
0
        private string FindSigningCertificateId(string tokenPin, string certificateLabel)
        {
            var signingSlot = SmartCardUtils.SaferFindSlot(this.pkcsLibPath, this.tokenLabel);

            if (signingSlot == null)
            {
                throw new InvalidOperationException("No Smart Card was found.");
            }
            using (var session = PkcsSession.StartNewSession(signingSlot, tokenPin))
            {
                return(SmartCardUtils.FindSigningCertificateId(session, certificateLabel));
            }
        }
        public void FindSigningCertificate()
        {
            var slot = SmartCardUtils.SaferFindSlot(@"C:\Program Files (x86)\EAC MW klient\pkcs11_x86.dll", "Sig_ZEP");

            using (var session = PkcsSession.StartNewSession(slot, "200860"))
            {
                var signingCertificate = SmartCardUtils.FindSigningCertificate(session, "Certifikat k podpisovemu klucu");
                Assert.IsNotNull(signingCertificate);

                var certificateChain = SmartCardUtils.GetCertificateChain(signingCertificate);
                Assert.IsNotEmpty(certificateChain);
            }
        }
Beispiel #4
0
        public BlobStoreTest()
        {
            LoggerUtils.setupLoggers();
            List <CardInfo> lst        = SmartCardUtils.GetReaderNames();
            String          readerName = lst[0].ReaderName;

            smartCard = new SmartCard(readerName, pin);
            CardMode mode = this.smartCard.GetCardMode();

            if (mode != CardMode.ROOT)
            {
                this.smartCard.SetCardInRootMode();
            }
            BigInteger.TryParse(pString, out p);
            BigInteger.TryParse(qString, out q);
            KeyPair pq  = new KeyPair(p, q);
            String  puk = this.smartCard.InitDevice(pq, pin);
        }
Beispiel #5
0
        /// <summary>
        /// Constructs a new SampleDevice instance.
        /// </summary>
        /// <param name="gq">The group construction.</param>
        /// <param name="gd">The device generator.</param>
        public SmartCardDevice(GroupDescription gq, GroupElement gd, SmartCardParams smartCardParam)
        {
            pin      = smartCardParam.pin;
            credID   = smartCardParam.credID;
            groupID  = smartCardParam.groupID;
            proverID = smartCardParam.proverID;

            // As SnartCardDevice do not provide a way to lookup card readr names
            // we provide a small potion of logic to lookup a card and cardreader
            List <CardInfo> cardInfoList = SmartCardUtils.GetReaderNames();
            // loop until we find a card with the status of "working mode". if none found
            // throw
            String readerName = null;

            foreach (CardInfo i in cardInfoList)
            {
                if (i.CardMode == (int)CardMode.WORKING)
                {
                    readerName = i.ReaderName;
                    break;
                }
            }
            if (readerName == null)
            {
                // TODO create a better exception
                throw new Exception("No card founds in working mode");
            }
            bool doTimeProfile = ParseConfigManager.DoTimeProfile();

            this.device = new SmartCard(readerName, pin, doTimeProfile);
            // As the group and generator is set from the java init service we will only verify
            // TODO fix to see that group 0 is set on the hw smartcard.
            //if (!this.device.IsGeneratorSet(groupID))
            //{
            // TODO Find better exception
            // throw new Exception("No generator is set on the card to use this group");
            //}

            this.Gq = gq;
            this.Gd = gd;
        }
        public HardwareSmartCardTest()
        {
            List <CardInfo> lst = SmartCardUtils.GetReaderNames();

            Assert.IsNotNull(lst);
            Assert.IsTrue(lst.Count > 0);
            String readerName = lst[0].ReaderName;

            BigInteger.TryParse(this.pString, out p);
            BigInteger.TryParse(this.qString, out q);

            smartCard = new SmartCard(readerName, "1234");
            try
            {
                this.smartCard.SetVirginMode();
            }
            catch (Exception)
            {
                Assert.Fail("Set the card into virgin mode failed");
            }
        }
Beispiel #7
0
        public void SignPdf(string inputPdfPath, string signedPdfPath, string tokenPin)
        {
            // Pkcs11RsaSignature can't find a private key by certificate label, only by certificate id.
            var signingCertificateId = this.FindSigningCertificateId(tokenPin, this.ckaLabel);

            var pkcs11RsaSignature = SmartCardUtils.SaferCreateSignature(this.pkcsLibPath, this.tokenLabel, tokenPin, signingCertificateId);

            if (pkcs11RsaSignature == null)
            {
                throw new InvalidOperationException("Smart card read error.");
            }
            try
            {
                var rawSigningCertificate = pkcs11RsaSignature.SaferGetSigningCertificate();
                var signingCertificate    = SmartCardUtils.ParseCertificate(rawSigningCertificate);
                var signatureAuthor       = GetCertificateCn(signingCertificate.Subject);
                var certificateChain      = SmartCardUtils.GetCertificateChain(signingCertificate);

                var certPath = CertUtils.BuildCertPath(rawSigningCertificate, certificateChain.Select(v => v.RawData).ToList());

                using (var pdfReader = new PdfReader(inputPdfPath))
                {
                    using (var outputStream = new FileStream(signedPdfPath, FileMode.Create))
                    {
                        // Create PdfStamper that applies extra content to the PDF document
                        using (var pdfStamper = PdfStamper.CreateSignature(pdfReader, outputStream, '\0', Path.GetTempFileName(), true))
                        {
                            pdfStamper.SignatureAppearance.SignatureCreator = signatureAuthor;
                            pdfStamper.SignatureAppearance.SignDate         = DateTime.Now;
                            // Sign PDF document
                            MakeSignature.SignDetached(pdfStamper.SignatureAppearance, pkcs11RsaSignature, certPath, null, null, null, 0, CryptoStandard.CADES);
                        }
                    }
                }
            }
            finally
            {
                pkcs11RsaSignature.Dispose();
            }
        }
        public void SelectAllSlotsTest()
        {
            var slot = SmartCardUtils.SaferFindSlot(@"C:\Program Files (x86)\EAC MW klient\pkcs11_x86.dll", "Sig_ZEP");

            Assert.IsNotNull(slot);
        }