예제 #1
0
        private static X509Certificate2 SetEcDsaPrivateKey(X509Certificate2 cert, string privateKey)
        {
            string[] keyParts = privateKey.Split('-', StringSplitOptions.RemoveEmptyEntries);
            byte[]   keyBytes = Convert.FromBase64String(keyParts[1]);

            ECDsa eccPrivateKey = ECDsa.Create();

            if (eccPrivateKey == null)
            {
                throw new PlatformNotSupportedException("Unable to create ECDsa");
            }

            switch (keyParts[0])
            {
            case "BEGIN PRIVATE KEY":
                eccPrivateKey.ImportPkcs8PrivateKey(keyBytes, out _);
                break;

            case "BEGIN EC PRIVATE KEY":
                eccPrivateKey.ImportECPrivateKey(keyBytes, out _);
                break;

            default:
                throw new ArgumentException("Invalid PrivateKey String", nameof(privateKey));
            }

            return(cert.CopyWithPrivateKey(eccPrivateKey).FixForWindows());
        }
예제 #2
0
        public TemporaryExposureKeySignService(
            IConfiguration config,
            ILogger <TemporaryExposureKeySignService> logger)
        {
            Logger = logger;
            Logger.LogInformation($"{nameof(TemporaryExposureKeySignService)} constructor");
            VerificationKeyId      = config.VerificationKeyId();
            VerificationKeyVersion = config.VerificationKeyVersion();
            VerificationKeySecret  = config.VerificationKeySecret();

            var key = Convert.FromBase64String(VerificationKeySecret);

            VerificationKey = ECDsa.Create();
            int read;

            VerificationKey.ImportECPrivateKey(key, out read);
            VerificationKeyGenerator = System.Security.Cryptography.X509Certificates.X509SignatureGenerator.CreateForECDsa(VerificationKey);
        }
예제 #3
0
        public static ECDsa LoadKey(this byte[] priv)
        {
            ECCurve curve = ECCurve.NamedCurves.nistP256;

            ECDsa key = ECDsa.Create(curve);

            key.ImportECPrivateKey(priv, out _);
            //new ECParameters
            //{
            //    Curve = curve,
            //    D = priv,
            //    Q = new ECPoint
            //    {
            //        X = null,
            //        Y = null,
            //    },
            //});

            //key.ImportECPrivateKey(priv, out _);

            return(key);
        }
        public void ExportRSAPrivateKey(AsnFormat format)
        {
            using ECDsa ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256);
            byte[] exported = ecdsa.ExportECPrivateKey(format);

            if (format == AsnFormat.Der)
            {
                using ECDsa ecdsaImport = ECDsa.Create();
                ecdsaImport.ImportECPrivateKey(exported, out _);
            }

            if (format == AsnFormat.Pem)
            {
                using MemoryStream ms = new MemoryStream(exported);
                using TextReader tr   = new StreamReader(ms, Encoding.ASCII);
                PemReader pemReader = new PemReader(tr);
                object    obj       = pemReader.ReadObject();
                Assert.IsNotNull(obj);
            }

            this.CheckFormat(format, exported);
        }
예제 #5
0
        public void Initialize_key_Pair()
        {
            if (File.Exists("Keys/PUBkey.txt"))
            {
                Console.WriteLine("retreiving keys");
                using (FileStream fspub = File.OpenRead("Keys/PUBkey.txt"))
                    using (FileStream fsprv = File.OpenRead("Keys/PRVkey.txt"))
                    {
                        PublicKey   = new byte[fspub.Length];
                        private_key = new byte[fsprv.Length];

                        fspub.Read(PublicKey, 0, PublicKey.Length);
                        fsprv.Read(PrivateKey, 0, PrivateKey.Length);

                        //import the keys to the current ECDSA object
                        int         bytes, bytes2 = 0;
                        Span <byte> imported_prv_key = new Span <byte>(PrivateKey);
                        Span <byte> imported_pub_key = new Span <byte>(PublicKey);

                        Ecc.ImportECPrivateKey(imported_prv_key, out bytes);
                        Ecc.ImportSubjectPublicKeyInfo(imported_pub_key, out bytes2);
                    }
            }
            else
            {
                Console.WriteLine("creating new keys");

                using (FileStream fspub = File.Create("Keys/PUBkey.txt"))
                    using (FileStream fsprv = File.Create("Keys/PRVkey.txt"))
                    {
                        PublicKey   = Ecc.ExportSubjectPublicKeyInfo();
                        private_key = Ecc.ExportECPrivateKey();
                        fspub.Write(PublicKey, 0, PublicKey.Length);
                        fsprv.Write(PrivateKey, 0, PrivateKey.Length);
                    }
            }
        }