Пример #1
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 = ExtractBignum(rsaStructure->n, modulusSize),
                    Exponent = ExtractBignum(rsaStructure->e, 0),
                };

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

            return rsaParameters;
        }
Пример #2
0
        internal static SafeRsaHandle DuplicateHandle(IntPtr handle)
        {
            Debug.Assert(handle != IntPtr.Zero);

            // Reliability: Allocate the SafeHandle before calling RSA_up_ref so
            // that we don't lose a tracked reference in low-memory situations.
            SafeRsaHandle safeHandle = new SafeRsaHandle();

            if (!Interop.Crypto.RsaUpRef(handle))
            {
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }

            safeHandle.SetHandle(handle);
            return safeHandle;
        }
Пример #3
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();
            }

            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;
        }
Пример #4
0
 internal static extern int RSA_generate_key_ex(SafeRsaHandle rsa, int bits, SafeBignumHandle e, IntPtr zero);
Пример #5
0
 internal static extern int RSA_size(SafeRsaHandle rsa);
Пример #6
0
 internal extern static int RSA_private_decrypt(int flen, byte[] from, byte[] to, SafeRsaHandle rsa, OpenSslRsaPadding padding);
Пример #7
0
 internal static extern bool EVP_PKEY_set1_RSA(SafeEvpPKeyHandle pkey, SafeRsaHandle rsa);
Пример #8
0
 private static extern bool GetRsaParameters(
     SafeRsaHandle key,
     out IntPtr n,
     out IntPtr e,
     out IntPtr d,
     out IntPtr p,
     out IntPtr dmp1,
     out IntPtr q,
     out IntPtr dmq1,
     out IntPtr iqmp);
Пример #9
0
 internal static extern bool RSA_sign(int type, byte[] m, int m_len, byte[] sigret, out int siglen, SafeRsaHandle rsa);
Пример #10
0
 internal static extern int RsaGenerateKeyEx(SafeRsaHandle rsa, int bits, SafeBignumHandle e);
Пример #11
0
 internal static extern int RsaSize(SafeRsaHandle rsa);
Пример #12
0
 internal extern static int RsaPrivateDecrypt(
     int flen,
     byte[] from,
     byte[] to,
     SafeRsaHandle rsa,
     RsaPadding padding);
Пример #13
0
 internal static extern void SetRsaParameters(
     SafeRsaHandle key,
     byte[] n,
     int nLength,
     byte[] e,
     int eLength,
     byte[] d,
     int dLength,
     byte[] p,
     int pLength,
     byte[] dmp1,
     int dmp1Length,
     byte[] q,
     int qLength,
     byte[] dmq1,
     int dmq1Length,
     byte[] iqmp,
     int iqmpLength);
Пример #14
0
 internal static extern bool EvpPkeySetRsa(SafeEvpPKeyHandle pkey, SafeRsaHandle rsa);
Пример #15
0
 internal static extern bool RSA_verify(int type, byte[] m, int m_len, byte[] sigbuf, int siglen, SafeRsaHandle rsa);
Пример #16
0
 private static void CheckInvalidKey(SafeRsaHandle key)
 {
     if (key == null || key.IsInvalid)
     {
         throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
     }
 }
Пример #17
0
 private static void CheckInvalidNewKey(SafeRsaHandle key)
 {
     if (key == null || key.IsInvalid)
     {
         throw CreateOpenSslException();
     }
 }