示例#1
0
        internal static RSAParameters ExportRsaParameters(SafeRsaHandle key, bool includePrivateParameters)
        {
            Debug.Assert(
                key != null && !key.IsInvalid,
                "Callers should check the key is invalid and throw an exception with a message");

            if (key == null || key.IsInvalid)
            {
                throw new CryptographicException();
            }

            bool addedRef = false;

            try
            {
                key.DangerousAddRef(ref addedRef);

                IntPtr n, e, d, p, dmp1, q, dmq1, iqmp;
                if (!GetRsaParameters(key, out n, out e, out d, out p, out dmp1, out q, out dmq1, out iqmp))
                {
                    throw new CryptographicException();
                }

                int modulusSize = Crypto.RsaSize(key);

                // RSACryptoServiceProvider expects P, DP, Q, DQ, and InverseQ to all
                // be padded up to half the modulus size.
                int halfModulus = modulusSize / 2;

                RSAParameters rsaParameters = new RSAParameters
                {
                    Modulus  = Crypto.ExtractBignum(n, modulusSize) !,
                    Exponent = Crypto.ExtractBignum(e, 0) !,
                };

                if (includePrivateParameters)
                {
                    rsaParameters.D        = Crypto.ExtractBignum(d, modulusSize);
                    rsaParameters.P        = Crypto.ExtractBignum(p, halfModulus);
                    rsaParameters.DP       = Crypto.ExtractBignum(dmp1, halfModulus);
                    rsaParameters.Q        = Crypto.ExtractBignum(q, halfModulus);
                    rsaParameters.DQ       = Crypto.ExtractBignum(dmq1, halfModulus);
                    rsaParameters.InverseQ = Crypto.ExtractBignum(iqmp, halfModulus);
                }

                return(rsaParameters);
            }
            finally
            {
                if (addedRef)
                {
                    key.DangerousRelease();
                }
            }
        }
示例#2
0
        internal static unsafe RSAParameters ExportRsaParameters(SafeRsaHandle key, bool includePrivateParameters)
        {
            Debug.Assert(
                key != null && !key.IsInvalid,
                "Callers should check the key is invalid and throw an exception with a message");

            if (key == null || key.IsInvalid)
            {
                throw new CryptographicException();
            }

            RSAParameters rsaParameters;
            bool          addedRef = false;

            try
            {
                key.DangerousAddRef(ref addedRef);
                RSA_ST *rsaStructure = (RSA_ST *)key.DangerousGetHandle();

                int modulusSize = RSA_size(key);

                // RSACryptoServiceProvider expects P, DP, Q, DQ, and InverseQ to all
                // be padded up to half the modulus size.
                int halfModulus = modulusSize / 2;

                rsaParameters = new RSAParameters
                {
                    Modulus  = Crypto.ExtractBignum(rsaStructure->n, modulusSize),
                    Exponent = Crypto.ExtractBignum(rsaStructure->e, 0),
                };

                if (includePrivateParameters)
                {
                    rsaParameters.D        = Crypto.ExtractBignum(rsaStructure->d, modulusSize);
                    rsaParameters.P        = Crypto.ExtractBignum(rsaStructure->p, halfModulus);
                    rsaParameters.DP       = Crypto.ExtractBignum(rsaStructure->dmp1, halfModulus);
                    rsaParameters.Q        = Crypto.ExtractBignum(rsaStructure->q, halfModulus);
                    rsaParameters.DQ       = Crypto.ExtractBignum(rsaStructure->dmq1, halfModulus);
                    rsaParameters.InverseQ = Crypto.ExtractBignum(rsaStructure->iqmp, halfModulus);
                }
            }
            finally
            {
                if (addedRef)
                {
                    key.DangerousRelease();
                }
            }

            return(rsaParameters);
        }