internal static unsafe bool CryptQueryObject([In] uint dwObjectType, [In] object pvObject, [In] uint dwExpectedContentTypeFlags, [In] uint dwExpectedFormatTypeFlags, [In] uint dwFlags, [Out] IntPtr pdwMsgAndCertEncodingType, [Out] IntPtr pdwContentType, [Out] IntPtr pdwFormatType, [In, Out] ref System.Security.Cryptography.SafeCertStoreHandle phCertStore, [In, Out] IntPtr phMsg, [In, Out] IntPtr ppvContext) { bool flag = false; GCHandle handle = GCHandle.Alloc(pvObject, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); try { if (pvObject == null) { throw new ArgumentNullException("pvObject"); } if (dwObjectType == 1) { string fullPath = Path.GetFullPath((string)pvObject); new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand(); } else { CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob; cryptoapi_blob.cbData = (uint)((byte[])pvObject).Length; cryptoapi_blob.pbData = ptr; ptr = new IntPtr((void *)&cryptoapi_blob); } flag = CAPIUnsafe.CryptQueryObject(dwObjectType, ptr, dwExpectedContentTypeFlags, dwExpectedFormatTypeFlags, dwFlags, pdwMsgAndCertEncodingType, pdwContentType, pdwFormatType, ref phCertStore, phMsg, ppvContext); } finally { if (handle.IsAllocated) { handle.Free(); } } return(flag); }
internal static System.Security.Cryptography.SafeCertStoreHandle CertOpenStore([In] IntPtr lpszStoreProvider, [In] uint dwMsgAndCertEncodingType, [In] IntPtr hCryptProv, [In] uint dwFlags, [In] string pvPara) { if ((lpszStoreProvider != new IntPtr(2L)) && (lpszStoreProvider != new IntPtr(10L))) { throw new ArgumentException(SR.GetString("Security_InvalidValue"), "lpszStoreProvider"); } if (((((dwFlags & 0x20000) == 0x20000) || ((dwFlags & 0x80000) == 0x80000)) || ((dwFlags & 0x90000) == 0x90000)) && ((pvPara != null) && pvPara.StartsWith(@"\\", StringComparison.Ordinal))) { new PermissionSet(PermissionState.Unrestricted).Demand(); } if ((dwFlags & 0x10) == 0x10) { new StorePermission(StorePermissionFlags.DeleteStore).Demand(); } else { new StorePermission(StorePermissionFlags.OpenStore).Demand(); } if ((dwFlags & 0x2000) == 0x2000) { new StorePermission(StorePermissionFlags.CreateStore).Demand(); } if ((dwFlags & 0x4000) == 0) { new StorePermission(StorePermissionFlags.CreateStore).Demand(); } return(CAPIUnsafe.CertOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType, hCryptProv, dwFlags | 4, pvPara)); }
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); }
internal static System.Security.Cryptography.SafeCertContextHandle CertFindCertificateInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] uint dwCertEncodingType, [In] uint dwFindFlags, [In] uint dwFindType, [In] IntPtr pvFindPara, [In] System.Security.Cryptography.SafeCertContextHandle pPrevCertContext) { if (hCertStore == null) { throw new ArgumentNullException("hCertStore"); } if (hCertStore.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore"); } return(CAPIUnsafe.CertFindCertificateInStore(hCertStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCertContext)); }
internal static bool CertDeleteCertificateFromStore([In] System.Security.Cryptography.SafeCertContextHandle pCertContext) { if (pCertContext == null) { throw new ArgumentNullException("pCertContext"); } if (pCertContext.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "pCertContext"); } new StorePermission(StorePermissionFlags.RemoveFromStore).Demand(); return(CAPIUnsafe.CertDeleteCertificateFromStore(pCertContext)); }
internal static bool PFXExportCertStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In, Out] IntPtr pPFX, [In] string szPassword, [In] uint dwFlags) { if (hCertStore == null) { throw new ArgumentNullException("hCertStore"); } if (hCertStore.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore"); } new KeyContainerPermission(KeyContainerPermissionFlags.Export | KeyContainerPermissionFlags.Open).Demand(); return(CAPIUnsafe.PFXExportCertStore(hCertStore, pPFX, szPassword, dwFlags)); }
internal static unsafe System.Security.Cryptography.SafeCertStoreHandle PFXImportCertStore([In] uint dwObjectType, [In] object pvObject, [In] string szPassword, [In] uint dwFlags, [In] bool persistKeyContainers) { if (pvObject == null) { throw new ArgumentNullException("pvObject"); } byte[] buffer = null; if (dwObjectType == 1) { buffer = File.ReadAllBytes((string)pvObject); } else { buffer = (byte[])pvObject; } if (persistKeyContainers) { new KeyContainerPermission(KeyContainerPermissionFlags.Create).Demand(); } System.Security.Cryptography.SafeCertStoreHandle invalidHandle = System.Security.Cryptography.SafeCertStoreHandle.InvalidHandle; GCHandle handle2 = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr ptr = handle2.AddrOfPinnedObject(); try { CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob; cryptoapi_blob.cbData = (uint)buffer.Length; cryptoapi_blob.pbData = ptr; invalidHandle = CAPIUnsafe.PFXImportCertStore(new IntPtr((void *)&cryptoapi_blob), szPassword, dwFlags); } finally { if (handle2.IsAllocated) { handle2.Free(); } } if (!invalidHandle.IsInvalid && !persistKeyContainers) { for (IntPtr ptr2 = CertEnumCertificatesInStore(invalidHandle, IntPtr.Zero); ptr2 != IntPtr.Zero; ptr2 = CertEnumCertificatesInStore(invalidHandle, ptr2)) { CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob2 = new CAPIBase.CRYPTOAPI_BLOB(); if (!CertSetCertificateContextProperty(ptr2, 0x65, 0x40000000, new IntPtr((void *)&cryptoapi_blob2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } } return(invalidHandle); }
internal static bool CertSetCertificateContextProperty([In] System.Security.Cryptography.SafeCertContextHandle pCertContext, [In] uint dwPropId, [In] uint dwFlags, [In] IntPtr pvData) { if (pvData == IntPtr.Zero) { throw new ArgumentNullException("pvData"); } if (((dwPropId != 0x13) && (dwPropId != 11)) && ((dwPropId != 0x65) && (dwPropId != 2))) { throw new ArgumentException(SR.GetString("Security_InvalidValue"), "dwFlags"); } if (((dwPropId == 0x13) || (dwPropId == 11)) || (dwPropId == 2)) { new PermissionSet(PermissionState.Unrestricted).Demand(); } return(CAPIUnsafe.CertSetCertificateContextProperty(pCertContext, dwPropId, dwFlags, pvData)); }
internal static bool CertSaveStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] uint dwMsgAndCertEncodingType, [In] uint dwSaveAs, [In] uint dwSaveTo, [In, Out] IntPtr pvSaveToPara, [In] uint dwFlags) { if (hCertStore == null) { throw new ArgumentNullException("hCertStore"); } if (hCertStore.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore"); } new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand(); if ((dwSaveTo == 3) || (dwSaveTo == 4)) { throw new ArgumentException(SR.GetString("Security_InvalidValue"), "pvSaveToPara"); } return(CAPIUnsafe.CertSaveStore(hCertStore, dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags)); }
internal static bool CertSetCertificateContextProperty([In] System.Security.Cryptography.SafeCertContextHandle pCertContext, [In] uint dwPropId, [In] uint dwFlags, [In] SafeLocalAllocHandle safeLocalAllocHandle) { if (pCertContext == null) { throw new ArgumentNullException("pCertContext"); } if (pCertContext.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "pCertContext"); } if (((dwPropId != 0x13) && (dwPropId != 11)) && ((dwPropId != 0x65) && (dwPropId != 2))) { throw new ArgumentException(SR.GetString("Security_InvalidValue"), "dwFlags"); } if (((dwPropId == 0x13) || (dwPropId == 11)) || (dwPropId == 2)) { new PermissionSet(PermissionState.Unrestricted).Demand(); } return(CAPIUnsafe.CertSetCertificateContextProperty(pCertContext, dwPropId, dwFlags, safeLocalAllocHandle)); }
internal static bool CertAddCertificateLinkToStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] System.Security.Cryptography.SafeCertContextHandle pCertContext, [In] uint dwAddDisposition, [In, Out] System.Security.Cryptography.SafeCertContextHandle ppStoreContext) { if (hCertStore == null) { throw new ArgumentNullException("hCertStore"); } if (hCertStore.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore"); } if (pCertContext == null) { throw new ArgumentNullException("pCertContext"); } if (pCertContext.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "pCertContext"); } new StorePermission(StorePermissionFlags.AddToStore).Demand(); return(CAPIUnsafe.CertAddCertificateLinkToStore(hCertStore, pCertContext, dwAddDisposition, ppStoreContext)); }
internal static System.Security.Cryptography.SafeCertContextHandle CertEnumCertificatesInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] System.Security.Cryptography.SafeCertContextHandle pPrevCertContext) { if (hCertStore == null) { throw new ArgumentNullException("hCertStore"); } if (hCertStore.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore"); } if (pPrevCertContext.IsInvalid) { new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand(); } System.Security.Cryptography.SafeCertContextHandle handle = CAPIUnsafe.CertEnumCertificatesInStore(hCertStore, pPrevCertContext); if (((handle == null) || handle.IsInvalid) && (Marshal.GetLastWin32Error() != -2146885628)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(handle); }
internal static bool CryptAcquireContext([In, Out] ref SafeCryptProvHandle hCryptProv, [In, MarshalAs(UnmanagedType.LPStr)] string pwszContainer, [In, MarshalAs(UnmanagedType.LPStr)] string pwszProvider, [In] uint dwProvType, [In] uint dwFlags) { CspParameters parameters = new CspParameters { ProviderName = pwszProvider, KeyContainerName = pwszContainer, ProviderType = (int)dwProvType, KeyNumber = -1, Flags = ((dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags }; KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Open); permission.AccessEntries.Add(accessEntry); permission.Demand(); bool flag = CAPIUnsafe.CryptAcquireContext(ref hCryptProv, pwszContainer, pwszProvider, dwProvType, dwFlags); if (!flag && (Marshal.GetLastWin32Error() == -2146893802)) { flag = CAPIUnsafe.CryptAcquireContext(ref hCryptProv, pwszContainer, pwszProvider, dwProvType, dwFlags | 8); } return(flag); }
internal static SafeCryptMsgHandle CryptMsgOpenToEncode([In] uint dwMsgEncodingType, [In] uint dwFlags, [In] uint dwMsgType, [In] IntPtr pvMsgEncodeInfo, [In] string pszInnerContentObjID, [In] IntPtr pStreamInfo) { return(CAPIUnsafe.CryptMsgOpenToEncode(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, pStreamInfo)); }
internal static bool CryptMsgCountersign([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwIndex, [In] uint cCountersigners, [In] IntPtr rgCountersigners) { return(CAPIUnsafe.CryptMsgCountersign(hCryptMsg, dwIndex, cCountersigners, rgCountersigners)); }
internal static bool CryptMsgControl([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwFlags, [In] uint dwCtrlType, [In] IntPtr pvCtrlPara) { return(CAPIUnsafe.CryptMsgControl(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara)); }