/// <include file='doc\DSACryptoServiceProvider.uex' path='docs/doc[@for="DSACryptoServiceProvider.ImportParameters"]/*' /> public override void ImportParameters(DSAParameters parameters) { if (_CSPHandleProtector.IsClosed || _KeyHandleProtector.IsClosed) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1")); } DSACspObject dsaKey = new DSACspObject(); // P, Q, G are required if (parameters.P == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_MissingField")); } dsaKey.P = (byte[])parameters.P.Clone(); if (parameters.Q == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_MissingField")); } dsaKey.Q = (byte[])parameters.Q.Clone(); if (parameters.G == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_MissingField")); } dsaKey.G = (byte[])parameters.G.Clone(); // Y is not required dsaKey.Y = (parameters.Y == null ? null : ((byte[])parameters.Y.Clone())); // J is not required dsaKey.J = (parameters.J == null ? null : ((byte[])parameters.J.Clone())); // seed is not required dsaKey.seed = (parameters.Seed == null ? null : ((byte[])parameters.Seed.Clone())); // counter is not required dsaKey.counter = parameters.Counter; // X is not required -- private component dsaKey.X = (parameters.X == null ? null : ((byte[])parameters.X.Clone())); // NOTE: We must reverse the dsaKey before importing! ReverseDSACspObject(dsaKey); // Free the current key handle _KeyHandleProtector.Close(); // Now, import the key into the CSP bool incremented = false; try { if (_CSPHandleProtector.TryAddRef(ref incremented)) { _hKey = _ImportKey(_CSPHandleProtector.Handle, CALG_DSA_SIGN, dsaKey); } else { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1")); } } finally { if (incremented) { _CSPHandleProtector.Release(); } } _KeyHandleProtector = new __KeyHandleProtector(_hKey); _parameters.KeyNumber = AT_SIGNATURE; if (dsaKey.X == null) { // If no X, then only have the public _containerContents = KeyContainerContents.PublicOnly; } else { // Our key pairs are always exportable _containerContents = KeyContainerContents.PublicAndExportablePrivate; } // zeroize private key material if (dsaKey.X != null) { Array.Clear(dsaKey.X, 0, dsaKey.X.Length); } }
/// <include file='doc\RSACryptoServiceProvider.uex' path='docs/doc[@for="RSACryptoServiceProvider.ImportParameters"]/*' /> public override void ImportParameters(RSAParameters parameters) { if (_CSPHandleProtector.IsClosed || _KeyHandleProtector.IsClosed) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1")); } RSACspObject rsaKey = new RSACspObject(); // Modulus is required for both public & private keys if (parameters.Modulus == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_MissingField")); } rsaKey.Modulus = (byte[])parameters.Modulus.Clone(); // Exponent is required byte[] rgbExponent = parameters.Exponent; if (rgbExponent == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_MissingField")); } rsaKey.Exponent = ConvertByteArrayToInt(rgbExponent); // p is optional rsaKey.P = (parameters.P == null ? null : ((byte[])parameters.P.Clone())); // q is optional rsaKey.Q = (parameters.Q == null ? null : ((byte[])parameters.Q.Clone())); // dp is optional rsaKey.dp = (parameters.DP == null ? null : ((byte[])parameters.DP.Clone())); // dq is optional rsaKey.dq = (parameters.DQ == null ? null : ((byte[])parameters.DQ.Clone())); // InverseQ is optional rsaKey.InverseQ = (parameters.InverseQ == null ? null : ((byte[])parameters.InverseQ.Clone())); // d is optional rsaKey.d = (parameters.D == null ? null : ((byte[])parameters.D.Clone())); // NOTE: We must reverse the rsaKey before importing! ReverseRSACspObject(rsaKey); // Free the current key handle _KeyHandleProtector.Close(); // Now, import the key into the CSP bool incremented = false; try { if (_CSPHandleProtector.TryAddRef(ref incremented)) { _hKey = _ImportKey(_CSPHandleProtector.Handle, CALG_RSA_KEYX, rsaKey); } else { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1")); } } finally { if (incremented) { _CSPHandleProtector.Release(); } } _KeyHandleProtector = new __KeyHandleProtector(_hKey); _parameters.KeyNumber = AT_KEYEXCHANGE; // reset _containerContents if (rsaKey.P == null) { _containerContents = KeyContainerContents.PublicOnly; } else { // Our key pairs are always exportable _containerContents = KeyContainerContents.PublicAndExportablePrivate; } // zeroize private info if (rsaKey.d != null) { Array.Clear(rsaKey.d, 0, rsaKey.d.Length); } if (rsaKey.P != null) { Array.Clear(rsaKey.P, 0, rsaKey.P.Length); } if (rsaKey.Q != null) { Array.Clear(rsaKey.Q, 0, rsaKey.Q.Length); } if (rsaKey.dp != null) { Array.Clear(rsaKey.dp, 0, rsaKey.dp.Length); } if (rsaKey.dq != null) { Array.Clear(rsaKey.dq, 0, rsaKey.dq.Length); } if (rsaKey.InverseQ != null) { Array.Clear(rsaKey.InverseQ, 0, rsaKey.InverseQ.Length); } }