本文整理汇总了C#中SafeCertContextHandle类的典型用法代码示例。如果您正苦于以下问题:C# SafeCertContextHandle类的具体用法?C# SafeCertContextHandle怎么用?C# SafeCertContextHandle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SafeCertContextHandle类属于命名空间,在下文中一共展示了SafeCertContextHandle类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportPublicKeyInfo
private static SafeBCryptKeyHandle ImportPublicKeyInfo(SafeCertContextHandle certContext)
{
#if NETNATIVE
// CryptImportPublicKeyInfoEx2() not in the UWP api list.
throw new PlatformNotSupportedException();
#else
unsafe
{
SafeBCryptKeyHandle bCryptKeyHandle;
bool mustRelease = false;
certContext.DangerousAddRef(ref mustRelease);
try
{
unsafe
{
bool success = Interop.crypt32.CryptImportPublicKeyInfoEx2(CertEncodingType.X509_ASN_ENCODING, &(certContext.CertContext->pCertInfo->SubjectPublicKeyInfo), 0, null, out bCryptKeyHandle);
if (!success)
throw Marshal.GetHRForLastWin32Error().ToCryptographicException();
return bCryptKeyHandle;
}
}
finally
{
if (mustRelease)
certContext.DangerousRelease();
}
}
#endif //NETNATIVE
}
示例2: CryptQueryObject
public static unsafe extern bool CryptQueryObject(
CertQueryObjectType dwObjectType,
void* pvObject,
ExpectedContentTypeFlags dwExpectedContentTypeFlags,
ExpectedFormatTypeFlags dwExpectedFormatTypeFlags,
int dwFlags, // reserved - always pass 0
out CertEncodingType pdwMsgAndCertEncodingType,
out ContentType pdwContentType,
out FormatType pdwFormatType,
out SafeCertStoreHandle phCertStore,
out SafeCryptMsgHandle phMsg,
out SafeCertContextHandle ppvContext
);
示例3: DisplayX509Certificate
private static void DisplayX509Certificate (SafeCertContextHandle safeCertContext, IntPtr hwndParent) {
if (safeCertContext.IsInvalid)
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "safeCertContext");
int dwErrorCode = CAPI.ERROR_SUCCESS;
// Initialize view structure.
CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo = new CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW();
ViewInfo.dwSize = (uint) Marshal.SizeOf(ViewInfo);
ViewInfo.hwndParent = hwndParent;
ViewInfo.dwFlags = 0;
ViewInfo.szTitle = null;
ViewInfo.pCertContext = safeCertContext.DangerousGetHandle();
ViewInfo.rgszPurposes = IntPtr.Zero;
ViewInfo.cPurposes = 0;
ViewInfo.pCryptProviderData = IntPtr.Zero;
ViewInfo.fpCryptProviderDataTrustedUsage = false;
ViewInfo.idxSigner = 0;
ViewInfo.idxCert = 0;
ViewInfo.fCounterSigner = false;
ViewInfo.idxCounterSigner = 0;
ViewInfo.cStores = 0;
ViewInfo.rghStores = IntPtr.Zero;
ViewInfo.cPropSheetPages = 0;
ViewInfo.rgPropSheetPages = IntPtr.Zero;
ViewInfo.nStartPage = 0;
// View the certificate
if (!CAPI.CryptUIDlgViewCertificateW(ViewInfo, IntPtr.Zero))
dwErrorCode = Marshal.GetLastWin32Error();
// CryptUIDlgViewCertificateW returns ERROR_CANCELLED if the user closes
// the window through the x button or by pressing CANCEL, so ignore this error code
if (dwErrorCode != CAPI.ERROR_SUCCESS && dwErrorCode != CAPI.ERROR_CANCELLED)
throw new CryptographicException(Marshal.GetLastWin32Error());
}
示例4: CertGetCertificateContextProperty
public static extern bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, [Out] out CRYPTOAPI_BLOB pvData, [In, Out] ref int pcbData);
示例5: CertSerializeCertificateStoreElement
public static extern bool CertSerializeCertificateStoreElement(SafeCertContextHandle pCertContext, int dwFlags, [Out] byte[] pbElement, [In, Out] ref int pcbElement);
示例6: CertGetCertificateContextPropertyString
public static extern bool CertGetCertificateContextPropertyString(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, [Out] StringBuilder pvData, [In, Out] ref int pcbData);
示例7: FindApplicationPolicyCallback
//
// Callback method to find certificates by application policy (also known as EKU)
// An example of application policy can be: "Encrypting File System"
//
private static unsafe int FindApplicationPolicyCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
string eku = (string) pvCallbackData;
if (eku.Length == 0)
return CAPI.S_FALSE;
IntPtr pCertContext = safeCertContextHandle.DangerousGetHandle();
int cNumOIDs = 0;
uint cbOIDs = 0;
SafeLocalAllocHandle rghOIDs = SafeLocalAllocHandle.InvalidHandle;
if (!CAPI.CertGetValidUsages(1, new IntPtr(&pCertContext), new IntPtr(&cNumOIDs), rghOIDs, new IntPtr(&cbOIDs)))
return CAPI.S_FALSE;
rghOIDs = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(cbOIDs));
if (!CAPI.CertGetValidUsages(1, new IntPtr(&pCertContext), new IntPtr(&cNumOIDs), rghOIDs, new IntPtr(&cbOIDs)))
return CAPI.S_FALSE;
// -1 means the certificate is good for all usages.
if (cNumOIDs == -1)
return CAPI.S_OK;
for (int index = 0; index < cNumOIDs; index++) {
IntPtr pszOid = Marshal.ReadIntPtr(new IntPtr((long) rghOIDs.DangerousGetHandle() + index * Marshal.SizeOf(typeof(IntPtr))));
string oidValue = Marshal.PtrToStringAnsi(pszOid);
if (String.Compare(eku, oidValue, StringComparison.OrdinalIgnoreCase) == 0)
return CAPI.S_OK;
}
return CAPI.S_FALSE;
}
示例8: FindSerialNumberCallback
//
// Callback method to find certificates by serial number.
// This can be useful when using XML Digital Signature and X509Data.
//
private static unsafe int FindSerialNumberCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
CAPI.CERT_INFO pCertInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pCertContext.pCertInfo, typeof(CAPI.CERT_INFO));
byte[] hex = new byte[pCertInfo.SerialNumber.cbData];
Marshal.Copy(pCertInfo.SerialNumber.pbData, hex, 0, hex.Length);
int size = X509Utils.GetHexArraySize(hex);
byte[] serialNumber = (byte[]) pvCallbackData;
if (serialNumber.Length != size)
return CAPI.S_FALSE;
for (int index = 0; index < serialNumber.Length; index++) {
if (serialNumber[index] != hex[index])
return CAPI.S_FALSE;
}
return CAPI.S_OK;
}
示例9: GetPrivateKeyInfo
internal static bool GetPrivateKeyInfo (SafeCertContextHandle safeCertContext, ref CspParameters parameters) {
SafeLocalAllocHandle ptr = SafeLocalAllocHandle.InvalidHandle;
uint cbData = 0;
if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext,
CAPI.CERT_KEY_PROV_INFO_PROP_ID,
ptr,
ref cbData)) {
int dwErrorCode = Marshal.GetLastWin32Error();
if (dwErrorCode == CAPI.CRYPT_E_NOT_FOUND)
return false;
else
throw new CryptographicException(Marshal.GetLastWin32Error());
}
ptr = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(cbData));
if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext,
CAPI.CERT_KEY_PROV_INFO_PROP_ID,
ptr,
ref cbData)) {
int dwErrorCode = Marshal.GetLastWin32Error();
if (dwErrorCode == CAPI.CRYPT_E_NOT_FOUND)
return false;
else
throw new CryptographicException(Marshal.GetLastWin32Error());
}
CAPI.CRYPT_KEY_PROV_INFO pKeyProvInfo = (CAPI.CRYPT_KEY_PROV_INFO) Marshal.PtrToStructure(ptr.DangerousGetHandle(), typeof(CAPI.CRYPT_KEY_PROV_INFO));
parameters.ProviderName = pKeyProvInfo.pwszProvName;
parameters.KeyContainerName = pKeyProvInfo.pwszContainerName;
parameters.ProviderType = (int) pKeyProvInfo.dwProvType;
parameters.KeyNumber = (int) pKeyProvInfo.dwKeySpec;
parameters.Flags = (CspProviderFlags) ((pKeyProvInfo.dwFlags & CAPI.CRYPT_MACHINE_KEYSET) == CAPI.CRYPT_MACHINE_KEYSET ? CspProviderFlags.UseMachineKeyStore : 0);
ptr.Dispose();
return true;
}
示例10: VerifyCertificate
internal static unsafe int VerifyCertificate (SafeCertContextHandle pCertContext,
OidCollection applicationPolicy,
OidCollection certificatePolicy,
X509RevocationMode revocationMode,
X509RevocationFlag revocationFlag,
DateTime verificationTime,
TimeSpan timeout,
X509Certificate2Collection extraStore,
IntPtr pszPolicy,
IntPtr pdwErrorStatus) {
if (pCertContext == null || pCertContext.IsInvalid)
throw new ArgumentException("pCertContext");
CAPI.CERT_CHAIN_POLICY_PARA PolicyPara = new CAPI.CERT_CHAIN_POLICY_PARA(Marshal.SizeOf(typeof(CAPI.CERT_CHAIN_POLICY_PARA)));
CAPI.CERT_CHAIN_POLICY_STATUS PolicyStatus = new CAPI.CERT_CHAIN_POLICY_STATUS(Marshal.SizeOf(typeof(CAPI.CERT_CHAIN_POLICY_STATUS)));
// Build the chain.
SafeCertChainHandle pChainContext = SafeCertChainHandle.InvalidHandle;
int hr = X509Utils.BuildChain(new IntPtr(CAPI.HCCE_CURRENT_USER),
pCertContext,
extraStore,
applicationPolicy,
certificatePolicy,
revocationMode,
revocationFlag,
verificationTime,
timeout,
ref pChainContext);
if (hr != CAPI.S_OK)
return hr;
// Verify the chain using the specified policy.
if (CAPI.CAPISafe.CertVerifyCertificateChainPolicy(pszPolicy, pChainContext, ref PolicyPara, ref PolicyStatus)) {
if (pdwErrorStatus != IntPtr.Zero)
*(uint*) pdwErrorStatus = PolicyStatus.dwError;
if (PolicyStatus.dwError != 0)
return CAPI.S_FALSE;
} else {
// The API failed.
return Marshal.GetHRForLastWin32Error();
}
return CAPI.S_OK;
}
示例11: CertAddCertificateContextToStore
public static extern bool CertAddCertificateContextToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, CertStoreAddDisposition dwAddDisposition, IntPtr ppStoreContext);
示例12: FindSubjectKeyIdentifierCallback
//
// Callback method to find certificates by subject key identifier.
// This can be useful when using XML Digital Signature and X509Data.
//
private static unsafe int FindSubjectKeyIdentifierCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
SafeLocalAllocHandle ptr = SafeLocalAllocHandle.InvalidHandle;
// We look for the Key Id extended property
// this will first look if there is a V3 SKI extension
// and then if that fails, It will return the Key Id extended property.
uint cbData = 0;
if (!CAPI.CertGetCertificateContextProperty(safeCertContextHandle,
CAPI.CERT_KEY_IDENTIFIER_PROP_ID,
ptr,
ref cbData))
return CAPI.S_FALSE;
ptr = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(cbData));
if (!CAPI.CertGetCertificateContextProperty(safeCertContextHandle,
CAPI.CERT_KEY_IDENTIFIER_PROP_ID,
ptr,
ref cbData))
return CAPI.S_FALSE;
byte[] subjectKeyIdentifier = (byte[]) pvCallbackData;
if (subjectKeyIdentifier.Length != cbData)
return CAPI.S_FALSE;
byte[] hex = new byte[cbData];
Marshal.Copy(ptr.DangerousGetHandle(), hex, 0, hex.Length);
ptr.Dispose();
for (uint index = 0; index < cbData; index++) {
if (subjectKeyIdentifier[index] != hex[index])
return CAPI.S_FALSE;
}
return CAPI.S_OK;
}
示例13: CertEnumCertificatesInStore
/// <summary>
/// A less error-prone wrapper for CertEnumCertificatesInStore().
///
/// To begin the enumeration, set pCertContext to null. Each iteration replaces pCertContext with
/// the next certificate in the iteration. The final call sets pCertContext to an invalid SafeCertStoreHandle
/// and returns "false" to indicate the the end of the store has been reached.
/// </summary>
public static bool CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, ref SafeCertContextHandle pCertContext)
{
unsafe
{
CERT_CONTEXT* pPrevCertContext = pCertContext == null ? null : pCertContext.Disconnect();
pCertContext = CertEnumCertificatesInStore(hCertStore, pPrevCertContext);
return !pCertContext.IsInvalid;
}
}
示例14: FindKeyUsageCallback
//
// Callback method to find certificates that have a particular Key Usage.
// The callback data can be either a string (example: "KeyEncipherment") or a DWORD which can have multiple bits set in it.
// If the callback data is a string, we can achieve the effect of a bit union by calling it multiple times, each time
// further restricting the set of selected certificates.
//
private static unsafe int FindKeyUsageCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
uint dwUsages = 0;
if (!CAPI.CertGetIntendedKeyUsage(CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
pCertContext.pCertInfo,
new IntPtr(&dwUsages),
4 /* sizeof(DWORD) */))
return CAPI.S_OK; // no key usage means it is valid for all key usages.
uint dwCheckUsage = Convert.ToUInt32(pvCallbackData, null);
if ((dwUsages & dwCheckUsage) == dwCheckUsage)
return CAPI.S_OK;
return CAPI.S_FALSE;
}
示例15: FindExtensionCallback
//
// Callback method to find certificates that have a particular extension.
// The callback data can be either an OID friendly name or value (all should be ANSI strings).
//
private static unsafe int FindExtensionCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
CAPI.CERT_INFO pCertInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pCertContext.pCertInfo, typeof(CAPI.CERT_INFO));
IntPtr pExtension = CAPI.CertFindExtension((string) pvCallbackData,
pCertInfo.cExtension,
pCertInfo.rgExtension);
if (pExtension == IntPtr.Zero)
return CAPI.S_FALSE;
return CAPI.S_OK;
}