private static unsafe int FindCertificatePolicyCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { string strA = (string)pvCallbackData; if (strA.Length != 0) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); IntPtr ptr = CAPISafe.CertFindExtension("2.5.29.32", cert_info.cExtension, cert_info.rgExtension); if (ptr == IntPtr.Zero) { return(1); } CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (CAPI.DecodeObject(new IntPtr(0x10L), destination, out decodedValue, out cbDecodedValue)) { CAPIBase.CERT_POLICIES_INFO cert_policies_info = (CAPIBase.CERT_POLICIES_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_POLICIES_INFO)); for (int i = 0; i < cert_policies_info.cPolicyInfo; i++) { IntPtr ptr2 = new IntPtr(((long)cert_policies_info.rgPolicyInfo) + (i * Marshal.SizeOf(typeof(CAPIBase.CERT_POLICY_INFO)))); CAPIBase.CERT_POLICY_INFO cert_policy_info = (CAPIBase.CERT_POLICY_INFO)Marshal.PtrToStructure(ptr2, typeof(CAPIBase.CERT_POLICY_INFO)); if (string.Compare(strA, cert_policy_info.pszPolicyIdentifier, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } } return(1); }
private static unsafe int FindTimeValidCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { System.Runtime.InteropServices.ComTypes.FILETIME pTimeToVerify = (System.Runtime.InteropServices.ComTypes.FILETIME)pvCallbackData; CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); if (CAPISafe.CertVerifyTimeValidity(ref pTimeToVerify, cert_context.pCertInfo) == 0) { return(0); } return(1); }
private static unsafe int FindExtensionCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); if (CAPISafe.CertFindExtension((string)pvCallbackData, cert_info.cExtension, cert_info.rgExtension) == IntPtr.Zero) { return(1); } return(0); }
private static unsafe int FindTemplateNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { IntPtr zero = IntPtr.Zero; IntPtr ptr = IntPtr.Zero; CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); zero = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.20.2", cert_info.cExtension, cert_info.rgExtension); ptr = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.21.7", cert_info.cExtension, cert_info.rgExtension); if ((zero != IntPtr.Zero) || (ptr != IntPtr.Zero)) { if (zero != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(zero, typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (CAPI.DecodeObject(new IntPtr(0x18L), destination, out decodedValue, out cbDecodedValue)) { CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE)); if (string.Compare(Marshal.PtrToStringUni(cert_name_value.Value.pbData), (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } if (ptr != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension2 = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_EXTENSION)); byte[] buffer2 = new byte[cert_extension2.Value.cbData]; Marshal.Copy(cert_extension2.Value.pbData, buffer2, 0, buffer2.Length); uint num2 = 0; SafeLocalAllocHandle handle2 = null; if (CAPI.DecodeObject(new IntPtr(0x40L), buffer2, out handle2, out num2)) { CAPIBase.CERT_TEMPLATE_EXT cert_template_ext = (CAPIBase.CERT_TEMPLATE_EXT)Marshal.PtrToStructure(handle2.DangerousGetHandle(), typeof(CAPIBase.CERT_TEMPLATE_EXT)); string strB = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)pvCallbackData, System.Security.Cryptography.OidGroup.Template); if (strB == null) { strB = (string)pvCallbackData; } if (string.Compare(cert_template_ext.pszObjId, strB, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } } return(1); }
private static unsafe int FindKeyUsageCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); uint num = 0; if (!CAPISafe.CertGetIntendedKeyUsage(0x10001, cert_context.pCertInfo, new IntPtr((void *)&num), 4)) { return(0); } uint num2 = Convert.ToUInt32(pvCallbackData, null); if ((num & num2) == num2) { return(0); } return(1); }
internal unsafe X509ExtensionCollection(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle) { this.m_list = new ArrayList(); using (System.Security.Cryptography.SafeCertContextHandle handle = CAPI.CertDuplicateCertificateContext(safeCertContextHandle)) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)handle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); uint cExtension = cert_info.cExtension; IntPtr rgExtension = cert_info.rgExtension; for (uint i = 0; i < cExtension; i++) { X509Extension asnEncodedData = new X509Extension(new IntPtr(((long)rgExtension) + (i * Marshal.SizeOf(typeof(CAPIBase.CERT_EXTENSION))))); X509Extension extension2 = CryptoConfig.CreateFromName(asnEncodedData.Oid.Value) as X509Extension; if (extension2 != null) { extension2.CopyFrom(asnEncodedData); asnEncodedData = extension2; } this.Add(asnEncodedData); } } }
private static unsafe int FindSerialNumberCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); byte[] destination = new byte[cert_info.SerialNumber.cbData]; Marshal.Copy(cert_info.SerialNumber.pbData, destination, 0, destination.Length); int hexArraySize = System.Security.Cryptography.X509Certificates.X509Utils.GetHexArraySize(destination); byte[] buffer2 = (byte[])pvCallbackData; if (buffer2.Length != hexArraySize) { return(1); } for (int i = 0; i < buffer2.Length; i++) { if (buffer2[i] != destination[i]) { return(1); } } return(0); }
private static unsafe uint GetVersion(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); return(cert_info.dwVersion + 1); }
private static unsafe Oid GetSignatureAlgorithm(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); return(new Oid(cert_info.SignatureAlgorithm.pszObjId, System.Security.Cryptography.OidGroup.SignatureAlgorithm, false)); }
public unsafe string GetNameInfo(X509NameType nameType, bool forIssuer) { uint dwFlags = forIssuer ? 1 : 0; uint dwDisplayType = System.Security.Cryptography.X509Certificates.X509Utils.MapNameType(nameType); switch (dwDisplayType) { case 1: return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType)); case 4: return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType)); } string str = string.Empty; CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *) this.m_safeCertContext.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); IntPtr[] ptrArray = new IntPtr[] { CAPISafe.CertFindExtension(forIssuer ? "2.5.29.8" : "2.5.29.7", cert_info.cExtension, cert_info.rgExtension), CAPISafe.CertFindExtension(forIssuer ? "2.5.29.18" : "2.5.29.17", cert_info.cExtension, cert_info.rgExtension) }; for (int i = 0; i < ptrArray.Length; i++) { if (ptrArray[i] != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptrArray[i], typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; SafeLocalAllocHandle handle2 = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr(cert_extension.pszObjId); bool flag = CAPI.DecodeObject(handle2.DangerousGetHandle(), destination, out decodedValue, out cbDecodedValue); handle2.Dispose(); if (flag) { CAPIBase.CERT_ALT_NAME_INFO cert_alt_name_info = (CAPIBase.CERT_ALT_NAME_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_ALT_NAME_INFO)); for (int j = 0; j < cert_alt_name_info.cAltEntry; j++) { IntPtr ptr = new IntPtr(((long)cert_alt_name_info.rgAltEntry) + (j * Marshal.SizeOf(typeof(CAPIBase.CERT_ALT_NAME_ENTRY)))); CAPIBase.CERT_ALT_NAME_ENTRY cert_alt_name_entry = (CAPIBase.CERT_ALT_NAME_ENTRY)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_ALT_NAME_ENTRY)); switch (dwDisplayType) { case 6: if (cert_alt_name_entry.dwAltNameChoice == 3) { str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszDNSName); } break; case 7: if (cert_alt_name_entry.dwAltNameChoice == 7) { str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszURL); } break; case 8: if (cert_alt_name_entry.dwAltNameChoice == 1) { CAPIBase.CERT_OTHER_NAME cert_other_name = (CAPIBase.CERT_OTHER_NAME)Marshal.PtrToStructure(cert_alt_name_entry.Value.pOtherName, typeof(CAPIBase.CERT_OTHER_NAME)); if (cert_other_name.pszObjId == "1.3.6.1.4.1.311.20.2.3") { uint num6 = 0; SafeLocalAllocHandle handle3 = null; if (CAPI.DecodeObject(new IntPtr(0x18L), System.Security.Cryptography.X509Certificates.X509Utils.PtrToByte(cert_other_name.Value.pbData, cert_other_name.Value.cbData), out handle3, out num6)) { CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(handle3.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE)); if (System.Security.Cryptography.X509Certificates.X509Utils.IsCertRdnCharString(cert_name_value.dwValueType)) { str = Marshal.PtrToStringUni(cert_name_value.Value.pbData); } handle3.Dispose(); } } } break; } } decodedValue.Dispose(); } } } if ((nameType != X509NameType.DnsName) || ((str != null) && (str.Length != 0))) { return(str); } return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, 3)); }
private static unsafe byte[] ExportCertificatesToBlob(System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle, X509ContentType contentType, string password) { System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle; uint dwSaveAs = 2; byte[] destination = null; CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = new CAPIBase.CRYPTOAPI_BLOB(); SafeLocalAllocHandle pbElement = SafeLocalAllocHandle.InvalidHandle; switch (contentType) { case X509ContentType.Cert: invalidHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, invalidHandle); if ((invalidHandle != null) && !invalidHandle.IsInvalid) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)invalidHandle.DangerousGetHandle()); destination = new byte[cert_context.cbCertEncoded]; Marshal.Copy(cert_context.pbCertEncoded, destination, 0, destination.Length); } break; case X509ContentType.SerializedCert: { invalidHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, invalidHandle); uint num2 = 0; if ((invalidHandle != null) && !invalidHandle.IsInvalid) { if (!CAPISafe.CertSerializeCertificateStoreElement(invalidHandle, 0, pbElement, new IntPtr((void *)&num2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } pbElement = CAPI.LocalAlloc(0, new IntPtr((long)num2)); if (!CAPISafe.CertSerializeCertificateStoreElement(invalidHandle, 0, pbElement, new IntPtr((void *)&num2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } destination = new byte[num2]; Marshal.Copy(pbElement.DangerousGetHandle(), destination, 0, destination.Length); break; } break; } case X509ContentType.Pfx: if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr((void *)&cryptoapi_blob), password, 6)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } cryptoapi_blob.pbData = CAPI.LocalAlloc(0, new IntPtr((long)cryptoapi_blob.cbData)).DangerousGetHandle(); if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr((void *)&cryptoapi_blob), password, 6)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } destination = new byte[cryptoapi_blob.cbData]; Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length); break; case X509ContentType.SerializedStore: case X509ContentType.Pkcs7: if (contentType == X509ContentType.SerializedStore) { dwSaveAs = 1; } if (!CAPI.CertSaveStore(safeCertStoreHandle, 0x10001, dwSaveAs, 2, new IntPtr((void *)&cryptoapi_blob), 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } pbElement = CAPI.LocalAlloc(0, new IntPtr((long)cryptoapi_blob.cbData)); cryptoapi_blob.pbData = pbElement.DangerousGetHandle(); if (!CAPI.CertSaveStore(safeCertStoreHandle, 0x10001, dwSaveAs, 2, new IntPtr((void *)&cryptoapi_blob), 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } destination = new byte[cryptoapi_blob.cbData]; Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length); break; default: throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidContentType")); } pbElement.Dispose(); invalidHandle.Dispose(); return(destination); }