public ICertificatePal CopyWithPrivateKey(ECDsa privateKey)
        {
            var typedKey = privateKey as ECDsaImplementation.ECDsaSecurityTransforms;

            byte[] ecPrivateKey;

            if (typedKey != null)
            {
                ECParameters ecParameters = default;

                if (typedKey.TryExportDataKeyParameters(includePrivateParameters: true, ref ecParameters))
                {
                    using (PinAndClear.Track(ecParameters.D !))
                    {
                        AsnWriter writer = EccKeyFormatHelper.WriteECPrivateKey(ecParameters);
                        ecPrivateKey = writer.Encode();
                        writer.Reset();
                    }
                }
                else
                {
                    return(CopyWithPrivateKey(typedKey.GetKeys().PrivateKey));
                }
            }
            else
            {
                ecPrivateKey = privateKey.ExportECPrivateKey();
            }

            using (PinAndClear.Track(ecPrivateKey))
                using (SafeSecKeyRefHandle privateSecKey = Interop.AppleCrypto.ImportEphemeralKey(ecPrivateKey, true))
                {
                    return(CopyWithPrivateKey(privateSecKey));
                }
        }
 // There is no internal support for PEM format
 public static string ToPem(this ECDsa privateKey)
 {
     return("-----BEGIN EC PRIVATE KEY-----\n" +
            FormatB64(
                Convert.ToBase64String(
                    privateKey.ExportECPrivateKey()
                    )
                ) +
            "\n-----END EC PRIVATE KEY-----\n");
 }
示例#3
0
 /// <summary>
 /// Returns a byte array containing the ECDsa private key in PEM format.
 /// </summary>
 public static byte[] ExportECDsaPrivateKeyAsPEM(
     X509Certificate2 certificate)
 {
     byte[] exportedECPrivateKey = null;
     using (ECDsa ecdsaPrivateKey = certificate.GetECDsaPrivateKey())
     {
         // write private key as PKCS#1
         exportedECPrivateKey = ecdsaPrivateKey.ExportECPrivateKey();
     }
     return(EncodeAsPEM(exportedECPrivateKey, "EC PRIVATE KEY"));
 }
示例#4
0
        public ICertificatePal CopyWithPrivateKey(ECDsa privateKey)
        {
            var typedKey = privateKey as ECDsaImplementation.ECDsaSecurityTransforms;

            if (typedKey != null)
            {
                return(CopyWithPrivateKey(typedKey.GetKeys().PrivateKey));
            }

            byte[] ecPrivateKey = privateKey.ExportECPrivateKey();

            using (PinAndClear.Track(ecPrivateKey))
                using (SafeSecKeyRefHandle privateSecKey = Interop.AppleCrypto.ImportEphemeralKey(ecPrivateKey, true))
                {
                    return(CopyWithPrivateKey(privateSecKey));
                }
        }
        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);
        }
示例#6
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);
                    }
            }
        }