本文整理汇总了C#中SafeCertContextHandle.DangerousGetHandle方法的典型用法代码示例。如果您正苦于以下问题:C# SafeCertContextHandle.DangerousGetHandle方法的具体用法?C# SafeCertContextHandle.DangerousGetHandle怎么用?C# SafeCertContextHandle.DangerousGetHandle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SafeCertContextHandle
的用法示例。
在下文中一共展示了SafeCertContextHandle.DangerousGetHandle方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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());
}
示例2: 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;
}
示例3: 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;
}
示例4: FindCertificatePolicyCallback
//
// Callback method to find certificates by certificate policy.
// This is only recognized in XP platforms. However, passing in an OID value should work on downlevel platforms as well.
//
private static unsafe int FindCertificatePolicyCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
string certPolicy = (string) pvCallbackData;
if (certPolicy.Length == 0)
return CAPI.S_FALSE;
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(CAPI.szOID_CERT_POLICIES,
pCertInfo.cExtension,
pCertInfo.rgExtension);
if (pExtension == IntPtr.Zero)
return CAPI.S_FALSE;
CAPI.CERT_EXTENSION extension = (CAPI.CERT_EXTENSION) Marshal.PtrToStructure(pExtension, typeof(CAPI.CERT_EXTENSION));
byte[] rawData = new byte[extension.Value.cbData];
Marshal.Copy(extension.Value.pbData, rawData, 0, rawData.Length);
uint cbDecoded = 0;
SafeLocalAllocHandle decoded = null;
// Decode the extension.
bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_CERT_POLICIES),
rawData,
out decoded,
out cbDecoded);
if (result) {
CAPI.CERT_POLICIES_INFO pInfo = (CAPI.CERT_POLICIES_INFO) Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_POLICIES_INFO));
for (int index = 0; index < pInfo.cPolicyInfo; index++) {
IntPtr pPolicyInfoPtr = new IntPtr((long) pInfo.rgPolicyInfo + index * Marshal.SizeOf(typeof(CAPI.CERT_POLICY_INFO)));
CAPI.CERT_POLICY_INFO pPolicyInfo = (CAPI.CERT_POLICY_INFO) Marshal.PtrToStructure(pPolicyInfoPtr, typeof(CAPI.CERT_POLICY_INFO));
if (String.Compare(certPolicy, pPolicyInfo.pszPolicyIdentifier, StringComparison.OrdinalIgnoreCase) == 0)
return CAPI.S_OK;
}
}
return CAPI.S_FALSE;
}
示例5: 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;
}
示例6: FindTemplateNameCallback
//
// Callback method to find certificates by template name.
// The template name can have 2 different formats: V1 format (<= Win2K) is just a string
// V2 format (XP only) can be a friendly name or an OID.
// An example of Template Name can be "ClientAuth".
//
private static unsafe int FindTemplateNameCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
IntPtr pV1Template = IntPtr.Zero;
IntPtr pV2Template = IntPtr.Zero;
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
CAPI.CERT_INFO pCertInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pCertContext.pCertInfo, typeof(CAPI.CERT_INFO));
pV1Template = CAPI.CertFindExtension(CAPI.szOID_ENROLL_CERTTYPE_EXTENSION,
pCertInfo.cExtension,
pCertInfo.rgExtension);
pV2Template = CAPI.CertFindExtension(CAPI.szOID_CERTIFICATE_TEMPLATE,
pCertInfo.cExtension,
pCertInfo.rgExtension);
if (pV1Template == IntPtr.Zero && pV2Template == IntPtr.Zero)
return CAPI.S_FALSE;
if (pV1Template != IntPtr.Zero) {
CAPI.CERT_EXTENSION extension = (CAPI.CERT_EXTENSION) Marshal.PtrToStructure(pV1Template, typeof(CAPI.CERT_EXTENSION));
byte[] rawData = new byte[extension.Value.cbData];
Marshal.Copy(extension.Value.pbData, rawData, 0, rawData.Length);
uint cbDecoded = 0;
SafeLocalAllocHandle decoded = null;
// Decode the extension.
bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_UNICODE_ANY_STRING),
rawData,
out decoded,
out cbDecoded);
if (result) {
CAPI.CERT_NAME_VALUE pNameValue = (CAPI.CERT_NAME_VALUE) Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_NAME_VALUE));
string s = Marshal.PtrToStringUni(pNameValue.Value.pbData);
if (String.Compare(s, (string) pvCallbackData, StringComparison.OrdinalIgnoreCase) == 0)
return CAPI.S_OK;
}
}
if (pV2Template != IntPtr.Zero) {
CAPI.CERT_EXTENSION extension = (CAPI.CERT_EXTENSION) Marshal.PtrToStructure(pV2Template, typeof(CAPI.CERT_EXTENSION));
byte[] rawData = new byte[extension.Value.cbData];
Marshal.Copy(extension.Value.pbData, rawData, 0, rawData.Length);
uint cbDecoded = 0;
SafeLocalAllocHandle decoded = null;
// Decode the extension.
bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_CERTIFICATE_TEMPLATE),
rawData,
out decoded,
out cbDecoded);
if (result) {
CAPI.CERT_TEMPLATE_EXT pTemplate = (CAPI.CERT_TEMPLATE_EXT) Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_TEMPLATE_EXT));
// If we were passed the friendly name, retrieve the value string.
string oidValue = X509Utils.FindOidInfoWithFallback(CAPI.CRYPT_OID_INFO_NAME_KEY, (string)pvCallbackData, OidGroup.Template);
if (oidValue == null)
oidValue = (string) pvCallbackData;
if (String.Compare(pTemplate.pszObjId, oidValue, StringComparison.OrdinalIgnoreCase) == 0)
return CAPI.S_OK;
}
}
return CAPI.S_FALSE;
}
示例7: FindTimeNotBeforeCallback
//
// Callback method to find certificates effective after a certain DateTime.
// The callback data has to be a UTC FILETEME.
//
private static unsafe int FindTimeNotBeforeCallback(SafeCertContextHandle safeCertContextHandle, object pvCallbackData) {
_FILETIME ft = (_FILETIME) pvCallbackData;
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
if (CAPI.CertVerifyTimeValidity(ref ft, pCertContext.pCertInfo) == -1)
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: GetVersion
private static unsafe uint GetVersion (SafeCertContextHandle safeCertContextHandle) {
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
CAPI.CERT_INFO pCertInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pCertContext.pCertInfo, typeof(CAPI.CERT_INFO));
return (pCertInfo.dwVersion + 1);
}
示例10: GetSignatureAlgorithm
private static unsafe Oid GetSignatureAlgorithm (SafeCertContextHandle safeCertContextHandle) {
CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());
CAPI.CERT_INFO pCertInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pCertContext.pCertInfo, typeof(CAPI.CERT_INFO));
return new Oid(pCertInfo.SignatureAlgorithm.pszObjId, OidGroup.SignatureAlgorithm, false);
}
示例11: RemoveCertificateFromStore
//
// private static methods
//
private static void RemoveCertificateFromStore(SafeCertStoreHandle safeCertStoreHandle, SafeCertContextHandle safeCertContext) {
if (safeCertContext == null || safeCertContext.IsInvalid)
return;
if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid || safeCertStoreHandle.IsClosed)
throw new CryptographicException(SR.GetString(SR.Cryptography_X509_StoreNotOpen));
// Find the certificate in the store.
SafeCertContextHandle safeCertContext2 = CAPI.CertFindCertificateInStore(safeCertStoreHandle,
CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
0,
CAPI.CERT_FIND_EXISTING,
safeCertContext.DangerousGetHandle(),
SafeCertContextHandle.InvalidHandle);
// The certificate is not present in the store, simply return.
if (safeCertContext2 == null || safeCertContext2.IsInvalid)
return;
// CertDeleteCertificateFromStore always releases the context regardless of success
// or failure so we don't need to manually release it
GC.SuppressFinalize(safeCertContext2);
// Remove from the store.
if (!CAPI.CertDeleteCertificateFromStore(safeCertContext2))
throw new CryptographicException(Marshal.GetLastWin32Error());
}
示例12: VerifyCertificateIgnoringErrors
private static bool VerifyCertificateIgnoringErrors(SafeCertContextHandle pCertContext)
{
// This needs to be kept in sync with IsCertValid in the
// Unix/OpenSSL PAL version (and potentially any other PALs that come about)
ChainPal chainPal = ChainPal.BuildChain(
true,
CertificatePal.FromHandle(pCertContext.DangerousGetHandle()),
null, //extraStore
null, //applicationPolicy
null, //certificatePolicy
X509RevocationMode.NoCheck,
X509RevocationFlag.ExcludeRoot,
DateTime.Now,
new TimeSpan(0, 0, 0));
if (chainPal == null)
return false;
using (chainPal)
{
Exception verificationException;
bool? verified = chainPal.Verify(X509VerificationFlags.NoFlag, out verificationException);
if (!verified.GetValueOrDefault())
return false;
}
return true;
}