private static int ExportCertificate(ExportCertificateOptions opts)
        {
            string          pkcs11LibPath = opts.LibPath ?? FindEidLibrary();
            IBokPinProvider pinProvider   = CreatePinpProvider(opts.UseConsolePin);

            using EidRsaCryptoAccessor eidRsaCryptoAccessor = new EidRsaCryptoAccessor(pkcs11LibPath, pinProvider);

            X509Certificate2 certificate = eidRsaCryptoAccessor.ExtractPublicCertificate().GetAwaiter().GetResult();

            string savePath = opts.ExportCertificatePath;

            if (string.IsNullOrEmpty(savePath))
            {
                savePath = string.Concat(certificate.Thumbprint, ".cer");
            }

            File.WriteAllBytes(savePath, certificate.RawData);
            return(0);
        }
        private static int DecryptFile(DecryptFileOptions opts)
        {
            string          pkcs11LibPath = opts.LibPath ?? FindEidLibrary();
            IBokPinProvider pinProvider   = CreatePinpProvider(opts.UseConsolePin);

            using EidRsaCryptoAccessor eidRsaCryptoAccessor = new EidRsaCryptoAccessor(pkcs11LibPath, pinProvider);

            using FileStream inputFiletream = new FileStream(opts.EncryptedFile, FileMode.Open, FileAccess.Read);

            using ContainerReader reader = new ContainerReader(inputFiletream, eidRsaCryptoAccessor);

            string fileName       = reader.ReadFileName().GetAwaiter().GetResult();
            string outputFilePath = Path.Combine(Path.GetDirectoryName(opts.EncryptedFile), fileName);

            using FileStream outputFiletream = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite);

            using Stream contentSrream = reader.GetContentStream().GetAwaiter().GetResult();
            contentSrream.CopyTo(outputFiletream);

            return(0);
        }
Example #3
0
        public EidRsaCryptoAccessor(string pkcs11Libpath, IBokPinProvider bokPinProvider, string tokenLabel = null)
        {
            if (pkcs11Libpath == null)
            {
                throw new ArgumentNullException(nameof(pkcs11Libpath));
            }

            if (bokPinProvider == null)
            {
                throw new ArgumentNullException(nameof(bokPinProvider));
            }

            if (tokenLabel == null)
            {
                tokenLabel = "SIG_EP";
            }

            this.bokPinProvider = bokPinProvider;
            this.pkcs11         = new Pkcs11(pkcs11Libpath, AppType.MultiThreaded);
            try
            {
                List <Slot> slots = this.pkcs11.GetSlotList(SlotsType.WithTokenPresent);
                this.slot = slots.SingleOrDefault(t => string.IsNullOrEmpty(tokenLabel) || string.Equals(t.GetTokenInfo().Label, tokenLabel, StringComparison.Ordinal));
                if (this.slot == null)
                {
                    throw new ArgumentException($"PKCS#11 lib '{pkcs11Libpath}' can not contains slot with label '{tokenLabel}'.");
                }

                this.loginSession = null;
            }
            catch (Exception)
            {
                this.loginSession?.Dispose();
                this.pkcs11.Dispose();
                throw;
            }
        }