/// <summary>
        /// Parses the certificate to get access to the underlying ECDsaCng implementation
        /// Requires the private key so that the resulting ECDsaCng can sign
        /// </summary>
        /// <param name="certificate">A certificate from a file or store</param>
        /// <returns>ECDsaCng that can sign AND verify data</returns>
        public static ECDsaCng ParsePrivateCertificate(X509Certificate2 certificate)
        {
            // Get the ECDSA private key (needs CngKey lib)
            var privateKey = certificate.GetCngPrivateKey();
            if (privateKey == null)
            {
                throw new InvalidOperationException("Certificate does not contain a private key, or is not in the right format");
            }

            return new ECDsaCng(privateKey);
        }
Example #2
0
        public int Execute()
        {
            if (false == File.Exists(Path))
            {
                Console.WriteLine("The file " + Path + " could not be located");
                WriteHelp(Console.Error);
                return 1;
            }
            var cert = new X509Certificate2();
            cert.Import(ReadFully(File.OpenRead(Path)), Password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

            var privateKey = cert.GetCngPrivateKey();
            if (null == privateKey)
            {
                Console.WriteLine("No private key found in pfx");
                return 2;
            }

            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);

            store.Add(cert);
            store.Close();

            GrantAccess(privateKey);
            Console.WriteLine("Imported key successfully");
            Console.WriteLine("\t algorithm: " + privateKey.Algorithm);
            Console.WriteLine("\t keysize: " + privateKey.KeySize);
            Console.WriteLine("\t usages: " + privateKey.KeyUsage);
            Console.WriteLine("\t uniquename: " + privateKey.UniqueName);
            return 0;
        }