Пример #1
0
        internal static IntPtr CertEnumCertificatesInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] IntPtr pPrevCertContext)
        {
            if (hCertStore == null)
            {
                throw new ArgumentNullException("hCertStore");
            }
            if (hCertStore.IsInvalid)
            {
                throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
            }
            if (pPrevCertContext == IntPtr.Zero)
            {
                new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand();
            }
            IntPtr pCertContext = CAPIUnsafe.CertEnumCertificatesInStore(hCertStore, pPrevCertContext);

            if (pCertContext == IntPtr.Zero)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr != -2146885628)
                {
                    CAPISafe.CertFreeCertificateContext(pCertContext);
                    throw new CryptographicException(hr);
                }
            }
            return(pCertContext);
        }
Пример #2
0
 internal static System.Security.Cryptography.SafeCertContextHandle CertDuplicateCertificateContext([In] System.Security.Cryptography.SafeCertContextHandle pCertContext)
 {
     if ((pCertContext != null) && !pCertContext.IsInvalid)
     {
         return(CAPISafe.CertDuplicateCertificateContext(pCertContext));
     }
     return(System.Security.Cryptography.SafeCertContextHandle.InvalidHandle);
 }
Пример #3
0
 internal static System.Security.Cryptography.SafeCertContextHandle CertDuplicateCertificateContext([In] IntPtr pCertContext)
 {
     if (pCertContext == IntPtr.Zero)
     {
         return(System.Security.Cryptography.SafeCertContextHandle.InvalidHandle);
     }
     return(CAPISafe.CertDuplicateCertificateContext(pCertContext));
 }
Пример #4
0
        internal static SafeLocalAllocHandle LocalAlloc(uint uFlags, IntPtr sizetdwBytes)
        {
            SafeLocalAllocHandle handle = CAPISafe.LocalAlloc(uFlags, sizetdwBytes);

            if ((handle == null) || handle.IsInvalid)
            {
                throw new OutOfMemoryException();
            }
            return(handle);
        }
Пример #5
0
        internal static CAPIBase.CRYPT_OID_INFO CryptFindOIDInfo([In] uint dwKeyType, [In] IntPtr pvKey, [In] System.Security.Cryptography.OidGroup dwGroupId)
        {
            if (pvKey == IntPtr.Zero)
            {
                throw new ArgumentNullException("pvKey");
            }
            CAPIBase.CRYPT_OID_INFO crypt_oid_info = new CAPIBase.CRYPT_OID_INFO(Marshal.SizeOf(typeof(CAPIBase.CRYPT_OID_INFO)));
            IntPtr ptr = CAPISafe.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId);

            if (ptr != IntPtr.Zero)
            {
                crypt_oid_info = (CAPIBase.CRYPT_OID_INFO)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CRYPT_OID_INFO));
            }
            return(crypt_oid_info);
        }
Пример #6
0
        internal static CAPIBase.CRYPT_OID_INFO CryptFindOIDInfo([In] uint dwKeyType, [In] SafeLocalAllocHandle pvKey, [In] System.Security.Cryptography.OidGroup dwGroupId)
        {
            if (pvKey == null)
            {
                throw new ArgumentNullException("pvKey");
            }
            if (pvKey.IsInvalid)
            {
                throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "pvKey");
            }
            CAPIBase.CRYPT_OID_INFO crypt_oid_info = new CAPIBase.CRYPT_OID_INFO(Marshal.SizeOf(typeof(CAPIBase.CRYPT_OID_INFO)));
            IntPtr ptr = CAPISafe.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId);

            if (ptr != IntPtr.Zero)
            {
                crypt_oid_info = (CAPIBase.CRYPT_OID_INFO)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CRYPT_OID_INFO));
            }
            return(crypt_oid_info);
        }
Пример #7
0
        internal static unsafe bool EncodeObject(string lpszStructType, IntPtr pvStructInfo, out byte[] encodedData)
        {
            encodedData = new byte[0];
            uint num = 0;
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (!CAPISafe.CryptEncodeObject(0x10001, lpszStructType, pvStructInfo, invalidHandle, new IntPtr((void *)&num)))
            {
                return(false);
            }
            invalidHandle = LocalAlloc(0, new IntPtr((long)num));
            if (!CAPISafe.CryptEncodeObject(0x10001, lpszStructType, pvStructInfo, invalidHandle, new IntPtr((void *)&num)))
            {
                return(false);
            }
            encodedData = new byte[num];
            Marshal.Copy(invalidHandle.DangerousGetHandle(), encodedData, 0, (int)num);
            invalidHandle.Dispose();
            return(true);
        }
Пример #8
0
        internal static unsafe bool DecodeObject(IntPtr pszStructType, IntPtr pbEncoded, uint cbEncoded, out SafeLocalAllocHandle decodedValue, out uint cbDecodedValue)
        {
            decodedValue   = SafeLocalAllocHandle.InvalidHandle;
            cbDecodedValue = 0;
            uint num = 0;
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (!CAPISafe.CryptDecodeObject(0x10001, pszStructType, pbEncoded, cbEncoded, 0, invalidHandle, new IntPtr((void *)&num)))
            {
                return(false);
            }
            invalidHandle = LocalAlloc(0, new IntPtr((long)num));
            if (!CAPISafe.CryptDecodeObject(0x10001, pszStructType, pbEncoded, cbEncoded, 0, invalidHandle, new IntPtr((void *)&num)))
            {
                return(false);
            }
            decodedValue   = invalidHandle;
            cbDecodedValue = num;
            return(true);
        }
Пример #9
0
        internal static unsafe string CryptFormatObject([In] uint dwCertEncodingType, [In] uint dwFormatStrType, [In] string lpszStructType, [In] byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException("rawData");
            }
            uint num = 0;
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (!CAPISafe.CryptFormatObject(dwCertEncodingType, 0, dwFormatStrType, IntPtr.Zero, lpszStructType, rawData, (uint)rawData.Length, invalidHandle, new IntPtr((void *)&num)))
            {
                return(System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(rawData));
            }
            invalidHandle = LocalAlloc(0, new IntPtr((long)num));
            if (!CAPISafe.CryptFormatObject(dwCertEncodingType, 0, dwFormatStrType, IntPtr.Zero, lpszStructType, rawData, (uint)rawData.Length, invalidHandle, new IntPtr((void *)&num)))
            {
                return(System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(rawData));
            }
            string str = Marshal.PtrToStringUni(invalidHandle.DangerousGetHandle());

            invalidHandle.Dispose();
            return(str);
        }
Пример #10
0
        internal static unsafe string GetCertNameInfo([In] System.Security.Cryptography.SafeCertContextHandle safeCertContext, [In] uint dwFlags, [In] uint dwDisplayType)
        {
            if (safeCertContext == null)
            {
                throw new ArgumentNullException("pCertContext");
            }
            if (safeCertContext.IsInvalid)
            {
                throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "safeCertContext");
            }
            uint num = 0x2000003;
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (dwDisplayType == 3)
            {
                invalidHandle = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr("2.5.4.3");
            }
            uint cchNameString = 0;
            SafeLocalAllocHandle pszNameString = SafeLocalAllocHandle.InvalidHandle;

            cchNameString = CAPISafe.CertGetNameStringW(safeCertContext, dwDisplayType, dwFlags, (dwDisplayType == 3) ? invalidHandle.DangerousGetHandle() : new IntPtr((void *)&num), pszNameString, 0);
            if (cchNameString == 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            pszNameString = LocalAlloc(0, new IntPtr((long)(2 * cchNameString)));
            if (CAPISafe.CertGetNameStringW(safeCertContext, dwDisplayType, dwFlags, (dwDisplayType == 3) ? invalidHandle.DangerousGetHandle() : new IntPtr((void *)&num), pszNameString, cchNameString) == 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            string str = Marshal.PtrToStringUni(pszNameString.DangerousGetHandle());

            pszNameString.Dispose();
            invalidHandle.Dispose();
            return(str);
        }