public override void ImportParameters(RSAParameters parameters)
        {
            if (this._safeKeyHandle != null && !this._safeKeyHandle.IsClosed)
            {
                this._safeKeyHandle.Dispose();
                this._safeKeyHandle = (SafeKeyHandle)null;
            }
            RSACspObject @object = RSACryptoServiceProvider.RSAStructToObject(parameters);

            this._safeKeyHandle = SafeKeyHandle.InvalidHandle;
            if (RSACryptoServiceProvider.IsPublic(parameters))
            {
                Utils._ImportKey(Utils.StaticProvHandle, 41984, CspProviderFlags.NoFlags, (object)@object, ref this._safeKeyHandle);
            }
            else
            {
                if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
                {
                    KeyContainerPermission            containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
                    KeyContainerPermissionAccessEntry accessEntry         = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Import);
                    containerPermission.AccessEntries.Add(accessEntry);
                    containerPermission.Demand();
                }
                if (this._safeProvHandle == null)
                {
                    this._safeProvHandle = Utils.CreateProvHandle(this._parameters, this._randomKeyContainer);
                }
                Utils._ImportKey(this._safeProvHandle, 41984, this._parameters.Flags, (object)@object, ref this._safeKeyHandle);
            }
        }
예제 #2
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override void ImportParameters(RSAParameters parameters)
        {
            // Free the current key handle
            if (_safeKeyHandle != null && !_safeKeyHandle.IsClosed)
            {
                _safeKeyHandle.Dispose();
                _safeKeyHandle = null;
            }

            RSACspObject rsaCspObject = RSAStructToObject(parameters);

            _safeKeyHandle = SafeKeyHandle.InvalidHandle;

            if (IsPublic(parameters))
            {
                // Use our CRYPT_VERIFYCONTEXT handle, CRYPT_EXPORTABLE is not applicable to public only keys, so pass false
                Utils._ImportKey(Utils.StaticProvHandle, Constants.CALG_RSA_KEYX, (CspProviderFlags)0, rsaCspObject, ref _safeKeyHandle);
            }
            else
            {
                if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
                {
                    KeyContainerPermission            kp    = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
                    KeyContainerPermissionAccessEntry entry = new KeyContainerPermissionAccessEntry(_parameters, KeyContainerPermissionFlags.Import);
                    kp.AccessEntries.Add(entry);
                    kp.Demand();
                }
                if (_safeProvHandle == null)
                {
                    _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer);
                }
                // Now, import the key into the CSP; _ImportKey will check for failures.
                Utils._ImportKey(_safeProvHandle, Constants.CALG_RSA_KEYX, _parameters.Flags, rsaCspObject, ref _safeKeyHandle);
            }
        }
 private static RSAParameters RSAObjectToStruct(RSACspObject rsaCspObject)
 {
     return(new RSAParameters()
     {
         Exponent = rsaCspObject.Exponent, Modulus = rsaCspObject.Modulus, P = rsaCspObject.P, Q = rsaCspObject.Q, DP = rsaCspObject.DP, DQ = rsaCspObject.DQ, InverseQ = rsaCspObject.InverseQ, D = rsaCspObject.D
     });
 }
 private void ReverseRSACspObject(RSACspObject obj)
 {
     if (obj.Modulus != null)
     {
         Array.Reverse(obj.Modulus);
     }
     if (obj.P != null)
     {
         Array.Reverse(obj.P);
     }
     if (obj.Q != null)
     {
         Array.Reverse(obj.Q);
     }
     if (obj.dp != null)
     {
         Array.Reverse(obj.dp);
     }
     if (obj.dq != null)
     {
         Array.Reverse(obj.dq);
     }
     if (obj.InverseQ != null)
     {
         Array.Reverse(obj.InverseQ);
     }
     if (obj.d != null)
     {
         Array.Reverse(obj.d);
     }
 }
 public override void ImportParameters(RSAParameters parameters)
 {
     if ((this._safeKeyHandle != null) && !this._safeKeyHandle.IsClosed)
     {
         this._safeKeyHandle.Dispose();
         this._safeKeyHandle = null;
     }
     RSACspObject cspObject = RSAStructToObject(parameters);
     this._safeKeyHandle = SafeKeyHandle.InvalidHandle;
     if (IsPublic(parameters))
     {
         Utils._ImportKey(Utils.StaticProvHandle, 0xa400, CspProviderFlags.NoFlags, cspObject, ref this._safeKeyHandle);
     }
     else
     {
         KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
         KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Import);
         permission.AccessEntries.Add(accessEntry);
         permission.Demand();
         if (this._safeProvHandle == null)
         {
             this._safeProvHandle = Utils.CreateProvHandle(this._parameters, this._randomKeyContainer);
         }
         Utils._ImportKey(this._safeProvHandle, 0xa400, this._parameters.Flags, cspObject, ref this._safeKeyHandle);
     }
 }
예제 #6
0
        private static RSACspObject RSAStructToObject(RSAParameters rsaParams)
        {
            RSACspObject rsaCspObject = new RSACspObject();

            rsaCspObject.Exponent = rsaParams.Exponent;
            rsaCspObject.Modulus  = rsaParams.Modulus;
            rsaCspObject.P        = rsaParams.P;
            rsaCspObject.Q        = rsaParams.Q;
            rsaCspObject.DP       = rsaParams.DP;
            rsaCspObject.DQ       = rsaParams.DQ;
            rsaCspObject.InverseQ = rsaParams.InverseQ;
            rsaCspObject.D        = rsaParams.D;
            return(rsaCspObject);
        }
        public override RSAParameters ExportParameters(bool includePrivateParameters)
        {
            this.GetKeyPair();
            if (includePrivateParameters && !CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
            {
                KeyContainerPermission            containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
                KeyContainerPermissionAccessEntry accessEntry         = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Export);
                containerPermission.AccessEntries.Add(accessEntry);
                containerPermission.Demand();
            }
            RSACspObject rsaCspObject = new RSACspObject();

            Utils._ExportKey(this._safeKeyHandle, includePrivateParameters ? 7 : 6, (object)rsaCspObject);
            return(RSACryptoServiceProvider.RSAObjectToStruct(rsaCspObject));
        }
 public override RSAParameters ExportParameters(bool includePrivateParameters)
 {
     this.GetKeyPair();
     if (includePrivateParameters)
     {
         KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
         KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Export);
         permission.AccessEntries.Add(accessEntry);
         permission.Demand();
     }
     RSACspObject cspObject = new RSACspObject();
     int blobType = includePrivateParameters ? 7 : 6;
     Utils._ExportKey(this._safeKeyHandle, blobType, cspObject);
     return RSAObjectToStruct(cspObject);
 }
예제 #9
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override RSAParameters ExportParameters(bool includePrivateParameters)
        {
            GetKeyPair();
            if (includePrivateParameters)
            {
                if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
                {
                    KeyContainerPermission            kp    = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
                    KeyContainerPermissionAccessEntry entry = new KeyContainerPermissionAccessEntry(_parameters, KeyContainerPermissionFlags.Export);
                    kp.AccessEntries.Add(entry);
                    kp.Demand();
                }
            }
            RSACspObject rsaCspObject = new RSACspObject();
            int          blobType     = includePrivateParameters ? Constants.PRIVATEKEYBLOB : Constants.PUBLICKEYBLOB;

            // _ExportKey will check for failures and throw an exception
            Utils._ExportKey(_safeKeyHandle, blobType, rsaCspObject);
            return(RSAObjectToStruct(rsaCspObject));
        }
 private static RSACspObject RSAStructToObject (RSAParameters rsaParams) {
     RSACspObject rsaCspObject = new RSACspObject();
     rsaCspObject.Exponent = rsaParams.Exponent;
     rsaCspObject.Modulus = rsaParams.Modulus;
     rsaCspObject.P = rsaParams.P;
     rsaCspObject.Q = rsaParams.Q;
     rsaCspObject.DP = rsaParams.DP;
     rsaCspObject.DQ = rsaParams.DQ;
     rsaCspObject.InverseQ = rsaParams.InverseQ;
     rsaCspObject.D = rsaParams.D;
     return rsaCspObject;
 }
 [System.Security.SecuritySafeCritical]  // auto-generated
 public override RSAParameters ExportParameters (bool includePrivateParameters) {
     GetKeyPair();
     if (includePrivateParameters) {
         if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
             KeyContainerPermission kp = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
             KeyContainerPermissionAccessEntry entry = new KeyContainerPermissionAccessEntry(_parameters, KeyContainerPermissionFlags.Export);
             kp.AccessEntries.Add(entry);
             kp.Demand();
         }
     }
     RSACspObject rsaCspObject = new RSACspObject();
     int blobType = includePrivateParameters ? Constants.PRIVATEKEYBLOB : Constants.PUBLICKEYBLOB;
     // _ExportKey will check for failures and throw an exception
     Utils._ExportKey(_safeKeyHandle, blobType, rsaCspObject);
     return RSAObjectToStruct(rsaCspObject);
 }
 private static RSAParameters RSAObjectToStruct(RSACspObject rsaCspObject)
 {
     return new RSAParameters { Exponent = rsaCspObject.Exponent, Modulus = rsaCspObject.Modulus, P = rsaCspObject.P, Q = rsaCspObject.Q, DP = rsaCspObject.DP, DQ = rsaCspObject.DQ, InverseQ = rsaCspObject.InverseQ, D = rsaCspObject.D };
 }
        /// <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);
            }
        }
        /************************* PUBLIC METHODS ************************/

        /// <include file='doc\RSACryptoServiceProvider.uex' path='docs/doc[@for="RSACryptoServiceProvider.ExportParameters"]/*' />
        public override RSAParameters ExportParameters(bool includePrivateParameters)
        {
            int hr;

            if (_CSPHandleProtector.IsClosed || _KeyHandleProtector.IsClosed)
            {
                throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1"));
            }

            RSACspObject  rsaKey    = new RSACspObject();
            RSAParameters rsaParams = new RSAParameters();

            if (includePrivateParameters)
            {
                bool incremented = false;
                try {
                    if (_KeyHandleProtector.TryAddRef(ref incremented))
                    {
                        hr = _ExportKey(_KeyHandleProtector.Handle, PRIVATEKEYBLOB, rsaKey);
                    }
                    else
                    {
                        throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1"));
                    }
                }
                finally {
                    if (incremented)
                    {
                        _KeyHandleProtector.Release();
                    }
                }
                if (hr != 0)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_ExportKey"));
                }
                // Must reverse after export!
                ReverseRSACspObject(rsaKey);
                rsaParams.Modulus  = rsaKey.Modulus;
                rsaParams.Exponent = ConvertIntToByteArray(rsaKey.Exponent);
                rsaParams.P        = rsaKey.P;
                rsaParams.Q        = rsaKey.Q;
                rsaParams.DP       = rsaKey.dp;
                rsaParams.DQ       = rsaKey.dq;
                rsaParams.InverseQ = rsaKey.InverseQ;
                rsaParams.D        = rsaKey.d;
            }
            else
            {
                bool incremented = false;
                try {
                    if (_KeyHandleProtector.TryAddRef(ref incremented))
                    {
                        hr = _ExportKey(_KeyHandleProtector.Handle, PUBLICKEYBLOB, rsaKey);
                    }
                    else
                    {
                        throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic_ObjectName1"));
                    }
                }
                finally {
                    if (incremented)
                    {
                        _KeyHandleProtector.Release();
                    }
                }
                if (hr != 0)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_ExportKey"));
                }
                // Must reverse after export!
                ReverseRSACspObject(rsaKey);
                rsaParams.Modulus  = rsaKey.Modulus;
                rsaParams.Exponent = ConvertIntToByteArray(rsaKey.Exponent);
                // 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);
                }
            }
            return(rsaParams);
        }
 private extern IntPtr  _ImportKey(IntPtr unknown1, int unknown2, RSACspObject unknown3);
 private extern int     _ExportKey(IntPtr unknown1, int unknown2, RSACspObject unknown3);