Ejemplo n.º 1
0
        private void LoadCertificateFromFile(string fileName, object password, X509KeyStorageFlags keyStorageFlags)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPathInternal(fileName)).Demand();
            if (X509Utils.MapContentType(X509Utils._QueryCertFileType(fileName)) == X509ContentType.Pfx && (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == X509KeyStorageFlags.PersistKeySet)
            {
                new KeyContainerPermission(KeyContainerPermissionFlags.Create).Demand();
            }
            uint   dwFlags = X509Utils.MapKeyStorageFlags(keyStorageFlags);
            IntPtr num     = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                num = X509Utils.PasswordToHGlobalUni(password);
                X509Utils._LoadCertFromFile(fileName, num, dwFlags, (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) != X509KeyStorageFlags.DefaultKeySet, ref this.m_safeCertContext);
            }
            finally
            {
                if (num != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(num);
                }
            }
        }
Ejemplo n.º 2
0
        private void LoadCertificateFromBlob(byte[] rawData, object password, X509KeyStorageFlags keyStorageFlags)
        {
            if (rawData == null || rawData.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyOrNullArray"), "rawData");
            }
            if (X509Utils.MapContentType(X509Utils._QueryCertBlobType(rawData)) == X509ContentType.Pfx && (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == X509KeyStorageFlags.PersistKeySet)
            {
                new KeyContainerPermission(KeyContainerPermissionFlags.Create).Demand();
            }
            uint   dwFlags = X509Utils.MapKeyStorageFlags(keyStorageFlags);
            IntPtr num     = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                num = X509Utils.PasswordToHGlobalUni(password);
                X509Utils._LoadCertFromBlob(rawData, num, dwFlags, (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) != X509KeyStorageFlags.DefaultKeySet, ref this.m_safeCertContext);
            }
            finally
            {
                if (num != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(num);
                }
            }
        }
Ejemplo n.º 3
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
        }
Ejemplo n.º 4
0
        [System.Security.SecurityCritical]  // auto-generated
        private void LoadCertificateFromFile(string fileName, object password, X509KeyStorageFlags keyStorageFlags)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            Contract.EndContractBlock();

            string fullPath = Path.GetFullPathInternal(fileName);

            new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand();
            X509ContentType contentType = X509Utils.MapContentType(X509Utils._QueryCertFileType(fileName));

#if !FEATURE_CORECLR
            if (contentType == X509ContentType.Pkcs12 &&
                (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == X509KeyStorageFlags.PersistKeySet)
            {
                KeyContainerPermission kp = new KeyContainerPermission(KeyContainerPermissionFlags.Create);
                kp.Demand();
            }
#endif // !FEATURE_CORECLR
            uint   dwFlags    = X509Utils.MapKeyStorageFlags(keyStorageFlags);
            IntPtr szPassword = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                szPassword = X509Utils.PasswordToHGlobalUni(password);
                X509Utils._LoadCertFromFile(fileName,
                                            szPassword,
                                            dwFlags,
#if FEATURE_CORECLR
                                            false,
#else // FEATURE_CORECLR
                                            (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == 0 ? false : true,
#endif // FEATURE_CORECLR else
                                            ref m_safeCertContext);
            }
            finally {
                if (szPassword != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(szPassword);
                }
            }
        }
Ejemplo n.º 5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        private void LoadCertificateFromBlob(byte[] rawData, object password, X509KeyStorageFlags keyStorageFlags)
        {
            if (rawData == null || rawData.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyOrNullArray"), "rawData");
            }
            Contract.EndContractBlock();

            X509ContentType contentType = X509Utils.MapContentType(X509Utils._QueryCertBlobType(rawData));

#if !FEATURE_CORECLR
            if (contentType == X509ContentType.Pkcs12 &&
                (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == X509KeyStorageFlags.PersistKeySet)
            {
                KeyContainerPermission kp = new KeyContainerPermission(KeyContainerPermissionFlags.Create);
                kp.Demand();
            }
#endif // !FEATURE_CORECLR
            uint   dwFlags    = X509Utils.MapKeyStorageFlags(keyStorageFlags);
            IntPtr szPassword = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                szPassword = X509Utils.PasswordToHGlobalUni(password);
                X509Utils._LoadCertFromBlob(rawData,
                                            szPassword,
                                            dwFlags,
#if FEATURE_CORECLR
                                            false,
#else // FEATURE_CORECLR
                                            (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) == 0 ? false : true,
#endif // FEATURE_CORECLR else
                                            ref m_safeCertContext);
            }
            finally {
                if (szPassword != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(szPassword);
                }
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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"));
            }
        }