Exemplo n.º 1
0
        [System.Security.SecurityCritical]  // auto-generated
        private byte[] ExportHelper(X509ContentType contentType, object password)
        {
            switch (contentType)
            {
            case X509ContentType.Cert:
                break;

#if FEATURE_CORECLR
            case (X509ContentType)0x02 /* X509ContentType.SerializedCert */:
            case (X509ContentType)0x03 /* X509ContentType.Pkcs12 */:
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_InvalidContentType"),
                                                 new NotSupportedException());
#else // FEATURE_CORECLR
            case X509ContentType.SerializedCert:
                break;

#if !FEATURE_PAL
            case X509ContentType.Pkcs12:
                KeyContainerPermission kp = new KeyContainerPermission(KeyContainerPermissionFlags.Open | KeyContainerPermissionFlags.Export);
                kp.Demand();
                break;
#endif // !FEATURE_PAL
#endif // FEATURE_CORECLR else
            default:
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_InvalidContentType"));
            }

#if !FEATURE_CORECLR
            IntPtr szPassword     = IntPtr.Zero;
            byte[] encodedRawData = null;
            SafeCertStoreHandle safeCertStoreHandle = X509Utils.ExportCertToMemoryStore(this);

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                szPassword     = X509Utils.PasswordToHGlobalUni(password);
                encodedRawData = X509Utils._ExportCertificatesToBlob(safeCertStoreHandle, contentType, szPassword);
            }
            finally {
                if (szPassword != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(szPassword);
                }
                safeCertStoreHandle.Dispose();
            }
            if (encodedRawData == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_ExportFailed"));
            }
            return(encodedRawData);
#else // !FEATURE_CORECLR
            return(RawData);
#endif // !FEATURE_CORECLR
        }
Exemplo n.º 2
0
        private byte[] ExportHelper(X509ContentType contentType, object password)
        {
            switch (contentType)
            {
            case X509ContentType.Cert:
            case X509ContentType.SerializedCert:
                break;

            case X509ContentType.Pfx:
                new KeyContainerPermission(KeyContainerPermissionFlags.Export | KeyContainerPermissionFlags.Open).Demand();
                break;

            default:
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_InvalidContentType"));
            }
            IntPtr zero = IntPtr.Zero;

            byte[] buffer = null;
            SafeCertStoreHandle safeCertStoreHandle = X509Utils.ExportCertToMemoryStore(this);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                zero   = X509Utils.PasswordToHGlobalUni(password);
                buffer = X509Utils._ExportCertificatesToBlob(safeCertStoreHandle, contentType, zero);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(zero);
                }
                safeCertStoreHandle.Dispose();
            }
            if (buffer == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_ExportFailed"));
            }
            return(buffer);
        }
Exemplo n.º 3
0
        private byte[] ExportHelper(X509ContentType contentType, object password)
        {
            switch (contentType)
            {
            case X509ContentType.Cert:
            case X509ContentType.SerializedCert:
                IntPtr num      = IntPtr.Zero;
                byte[] numArray = (byte[])null;
                SafeCertStoreHandle memoryStore = X509Utils.ExportCertToMemoryStore(this);
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    num      = X509Utils.PasswordToHGlobalUni(password);
                    numArray = X509Utils._ExportCertificatesToBlob(memoryStore, contentType, num);
                }
                finally
                {
                    if (num != IntPtr.Zero)
                    {
                        Marshal.ZeroFreeGlobalAllocUnicode(num);
                    }
                    memoryStore.Dispose();
                }
                if (numArray == null)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_ExportFailed"));
                }
                return(numArray);

            case X509ContentType.Pfx:
                new KeyContainerPermission(KeyContainerPermissionFlags.Open | KeyContainerPermissionFlags.Export).Demand();
                goto case X509ContentType.Cert;

            default:
                throw new CryptographicException(Environment.GetResourceString("Cryptography_X509_InvalidContentType"));
            }
        }