internal static unsafe int GetSignerIndex(System.Security.Cryptography.SafeCryptMsgHandle safeCrytpMsgHandle, SignerInfo signerInfo, int startIndex) { uint num = 0; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 5, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2))) { checkErr(Marshal.GetLastWin32Error()); } for (int i = startIndex; i < num; i++) { uint num4 = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, IntPtr.Zero, new IntPtr((void *)&num4))) { checkErr(Marshal.GetLastWin32Error()); } if (num4 > 0) { System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num4)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, pvData, new IntPtr((void *)&num4))) { checkErr(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsgSignerInfo = signerInfo.GetCmsgSignerInfo(); System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info2 = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); if (System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.Issuer.pbData, cmsgSignerInfo.Issuer.cbData, (byte *)cmsg_signer_info2.Issuer.pbData, cmsg_signer_info2.Issuer.cbData) && System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.SerialNumber.pbData, cmsgSignerInfo.SerialNumber.cbData, (byte *)cmsg_signer_info2.SerialNumber.pbData, cmsg_signer_info2.SerialNumber.cbData)) { return(i); } pvData.Dispose(); } } throw new CryptographicException(-2146889714); }
internal static unsafe X509Certificate2 CreateDummyCertificate(CspParameters parameters) { System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle; System.Security.Cryptography.SafeCryptProvHandle hCryptProv = System.Security.Cryptography.SafeCryptProvHandle.InvalidHandle; uint dwFlags = 0; if ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags) { dwFlags |= 0x20; } if ((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != CspProviderFlags.NoFlags) { dwFlags |= 0xf0000000; } if ((parameters.Flags & CspProviderFlags.NoPrompt) != CspProviderFlags.NoFlags) { dwFlags |= 0x40; } if (!System.Security.Cryptography.CAPI.CryptAcquireContext(ref hCryptProv, parameters.KeyContainerName, parameters.ProviderName, (uint)parameters.ProviderType, dwFlags)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO structure = new System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO { pwszProvName = parameters.ProviderName, pwszContainerName = parameters.KeyContainerName, dwProvType = (uint)parameters.ProviderType, dwKeySpec = (uint)parameters.KeyNumber, dwFlags = ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) ? 0x20 : 0 }; System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)))); Marshal.StructureToPtr(structure, handle3.DangerousGetHandle(), false); System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER crypt_algorithm_identifier = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER { pszObjId = "1.3.14.3.2.29" }; System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)))); Marshal.StructureToPtr(crypt_algorithm_identifier, handle4.DangerousGetHandle(), false); X500DistinguishedName name = new X500DistinguishedName("cn=CMS Signer Dummy Certificate"); fixed(byte *numRef = name.RawData) { System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = new System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB { cbData = (uint)name.RawData.Length, pbData = new IntPtr((void *)numRef) }; invalidHandle = System.Security.Cryptography.CAPI.CAPIUnsafe.CertCreateSelfSignCertificate(hCryptProv, new IntPtr((void *)&cryptoapi_blob), 1, handle3.DangerousGetHandle(), handle4.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } Marshal.DestroyStructure(handle3.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)); handle3.Dispose(); Marshal.DestroyStructure(handle4.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)); handle4.Dispose(); if ((invalidHandle == null) || invalidHandle.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509Certificate2 certificate = new X509Certificate2(invalidHandle.DangerousGetHandle()); invalidHandle.Dispose(); return(certificate); }
internal static bool GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint pcbData = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)pcbData)); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO crypt_key_prov_info = (System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)); parameters.ProviderName = crypt_key_prov_info.pwszProvName; parameters.KeyContainerName = crypt_key_prov_info.pwszContainerName; parameters.ProviderType = (int)crypt_key_prov_info.dwProvType; parameters.KeyNumber = (int)crypt_key_prov_info.dwKeySpec; parameters.Flags = ((crypt_key_prov_info.dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags; invalidHandle.Dispose(); return(true); }
private void CounterSign(CmsSigner signer) { CspParameters parameters = new CspParameters(); if (!System.Security.Cryptography.X509Certificates.X509Utils.GetPrivateKeyInfo(System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(signer.Certificate), ref parameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Sign | KeyContainerPermissionFlags.Open); permission.AccessEntries.Add(accessEntry); permission.Demand(); uint dwIndex = (uint)PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this, 0); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer); try { Marshal.StructureToPtr(structure, handle.DangerousGetHandle(), false); if (!System.Security.Cryptography.CAPI.CryptMsgCountersign(this.m_signedCms.GetCryptMsgHandle(), dwIndex, 1, handle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } this.m_signedCms.ReopenToDecode(); } finally { Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle.Dispose(); structure.Dispose(); } PkcsUtils.AddCertsToMessage(this.m_signedCms.GetCryptMsgHandle(), this.m_signedCms.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
private unsafe void Sign(CmsSigner signer, bool silent) { System.Security.Cryptography.SafeCryptMsgHandle hCryptMsg = null; System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO cmsg_signed_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO))); System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer, silent); byte[] encodedMessage = null; try { System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr(structure, handle2.DangerousGetHandle(), false); X509Certificate2Collection certificates = PkcsUtils.CreateBagOfCertificates(signer); System.Security.Cryptography.SafeLocalAllocHandle handle3 = PkcsUtils.CreateEncodedCertBlob(certificates); cmsg_signed_encode_info.cSigners = 1; cmsg_signed_encode_info.rgSigners = handle2.DangerousGetHandle(); cmsg_signed_encode_info.cCertEncoded = (uint)certificates.Count; if (certificates.Count > 0) { cmsg_signed_encode_info.rgCertEncoded = handle3.DangerousGetHandle(); } if (string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) == 0) { hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), IntPtr.Zero, IntPtr.Zero); } else { hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), this.ContentInfo.ContentType.Value, IntPtr.Zero); } if ((hCryptMsg == null) || hCryptMsg.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if ((this.ContentInfo.Content.Length > 0) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, this.ContentInfo.pContent, (uint)this.ContentInfo.Content.Length, true)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } encodedMessage = PkcsUtils.GetContent(hCryptMsg); hCryptMsg.Dispose(); handle3.Dispose(); } finally { Marshal.DestroyStructure(handle2.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle2.Dispose(); } } finally { structure.Dispose(); } hCryptMsg = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached); if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid) { this.m_safeCryptMsgHandle.Dispose(); } this.m_safeCryptMsgHandle = hCryptMsg; GC.KeepAlive(signer); }
private static byte[] Encode(DateTime signingTime) { long val = signingTime.ToFileTimeUtc(); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(long)))); Marshal.WriteInt64(handle.DangerousGetHandle(), val); byte[] encodedData = new byte[0]; if (!System.Security.Cryptography.CAPI.EncodeObject("1.2.840.113549.1.9.5", handle.DangerousGetHandle(), out encodedData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } handle.Dispose(); return(encodedData); }
private void Decode() { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle decodedValue = null; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x11L), base.RawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } long fileTime = Marshal.ReadInt64(decodedValue.DangerousGetHandle()); decodedValue.Dispose(); this.m_signingTime = DateTime.FromFileTimeUtc(fileTime); this.m_decoded = true; }
internal AlgorithmIdentifier(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO keyInfo) { System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO)))); Marshal.StructureToPtr(keyInfo, handle.DangerousGetHandle(), false); int keyLength = (int)System.Security.Cryptography.CAPI.CAPISafe.CertGetPublicKeyLength(0x10001, handle.DangerousGetHandle()); byte[] destination = new byte[keyInfo.Algorithm.Parameters.cbData]; if (destination.Length > 0) { Marshal.Copy(keyInfo.Algorithm.Parameters.pbData, destination, 0, destination.Length); } Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO)); handle.Dispose(); this.Reset(new System.Security.Cryptography.Oid(keyInfo.Algorithm.pszObjId), keyLength, destination); }
internal static string FindOidInfo(uint keyType, string keyValue, Cryptography.OidGroup oidGroup) { if (keyValue == null) { throw new ArgumentNullException("keyValue"); } if (keyValue.Length == 0) { return(null); } SafeLocalAllocHandle pvKey = SafeLocalAllocHandle.InvalidHandle; try { switch (keyType) { case CAPI.CRYPT_OID_INFO_OID_KEY: pvKey = StringToAnsiPtr(keyValue); break; case CAPI.CRYPT_OID_INFO_NAME_KEY: pvKey = StringToUniPtr(keyValue); break; default: Debug.Assert(false); break; } CAPI.CRYPT_OID_INFO pOidInfo = CAPI.CryptFindOIDInfo(keyType, pvKey, oidGroup); if (keyType == CAPI.CRYPT_OID_INFO_OID_KEY) { return(pOidInfo.pwszName); } else { return(pOidInfo.pszOID); } } finally { pvKey.Dispose(); } }
internal static unsafe int BuildChain(IntPtr hChainEngine, System.Security.Cryptography.SafeCertContextHandle pCertContext, X509Certificate2Collection extraStore, OidCollection applicationPolicy, OidCollection certificatePolicy, X509RevocationMode revocationMode, X509RevocationFlag revocationFlag, DateTime verificationTime, TimeSpan timeout, ref System.Security.Cryptography.SafeCertChainHandle ppChainContext) { System.Security.Cryptography.CAPI.CERT_CHAIN_PARA cert_chain_para; if ((pCertContext == null) || pCertContext.IsInvalid) { throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_InvalidContextHandle"), "pCertContext"); } System.Security.Cryptography.SafeCertStoreHandle invalidHandle = System.Security.Cryptography.SafeCertStoreHandle.InvalidHandle; if ((extraStore != null) && (extraStore.Count > 0)) { invalidHandle = ExportToMemoryStore(extraStore); } cert_chain_para = new System.Security.Cryptography.CAPI.CERT_CHAIN_PARA { cbSize = (uint)Marshal.SizeOf(cert_chain_para) }; System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if ((applicationPolicy != null) && (applicationPolicy.Count > 0)) { cert_chain_para.RequestedUsage.dwType = 0; cert_chain_para.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count; handle2 = CopyOidsToUnmanagedMemory(applicationPolicy); cert_chain_para.RequestedUsage.Usage.rgpszUsageIdentifier = handle2.DangerousGetHandle(); } System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if ((certificatePolicy != null) && (certificatePolicy.Count > 0)) { cert_chain_para.RequestedIssuancePolicy.dwType = 0; cert_chain_para.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count; handle3 = CopyOidsToUnmanagedMemory(certificatePolicy); cert_chain_para.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = handle3.DangerousGetHandle(); } cert_chain_para.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds; System.Runtime.InteropServices.ComTypes.FILETIME pTime = new System.Runtime.InteropServices.ComTypes.FILETIME(); *((long *)&pTime) = verificationTime.ToFileTime(); uint dwFlags = MapRevocationFlags(revocationMode, revocationFlag); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, invalidHandle, ref cert_chain_para, dwFlags, IntPtr.Zero, ref ppChainContext)) { return(Marshal.GetHRForLastWin32Error()); } handle2.Dispose(); handle3.Dispose(); return(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); }
private void CoSign(CmsSigner signer, bool silent) { using (System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO cmsg_signer_encode_info = PkcsUtils.CreateSignerEncodeInfo(signer, silent)) { System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr(cmsg_signer_encode_info, handle.DangerousGetHandle(), false); if (!System.Security.Cryptography.CAPI.CryptMsgControl(this.m_safeCryptMsgHandle, 0, 6, handle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle.Dispose(); } } PkcsUtils.AddCertsToMessage(this.m_safeCryptMsgHandle, this.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle) { AlgorithmIdentifier identifier = new AlgorithmIdentifier(); uint num = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, IntPtr.Zero, new IntPtr((void *)&num))) { checkErr(Marshal.GetLastWin32Error()); } if (num > 0) { System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, pvData, new IntPtr((void *)&num))) { checkErr(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier = (System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)); identifier = new AlgorithmIdentifier(algorithmIdentifier); pvData.Dispose(); } return(identifier); }
internal static string DecodeOctetString(byte[] encodedOctetString) { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle decodedValue = null; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x19L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue == 0) { return(string.Empty); } System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)); if (cryptoapi_blob.cbData == 0) { return(string.Empty); } string str = Marshal.PtrToStringUni(cryptoapi_blob.pbData); decodedValue.Dispose(); return(str); }
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); }
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); }
internal static unsafe X509IssuerSerial DecodeIssuerSerial(System.Security.Cryptography.CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint csz = System.Security.Cryptography.CAPI.CAPISafe.CertNameToStrW(0x10001, new IntPtr((void *)&pIssuerAndSerial.Issuer), 0x2000003, invalidHandle, 0); if (csz <= 1) { throw new CryptographicException(Marshal.GetLastWin32Error()); } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)(2 * csz))); if (System.Security.Cryptography.CAPI.CAPISafe.CertNameToStrW(0x10001, new IntPtr((void *)&pIssuerAndSerial.Issuer), 0x2000003, invalidHandle, csz) <= 1) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509IssuerSerial serial = new X509IssuerSerial { IssuerName = Marshal.PtrToStringUni(invalidHandle.DangerousGetHandle()) }; byte[] destination = new byte[pIssuerAndSerial.SerialNumber.cbData]; Marshal.Copy(pIssuerAndSerial.SerialNumber.pbData, destination, 0, destination.Length); serial.SerialNumber = System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexStringFromInt(destination); invalidHandle.Dispose(); return(serial); }
private unsafe void Verify(X509Certificate2Collection extraStore, X509Certificate2 certificate, bool verifySignatureOnly) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; System.Security.Cryptography.CAPI.CERT_CONTEXT cert_context = (System.Security.Cryptography.CAPI.CERT_CONTEXT)Marshal.PtrToStructure(System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(certificate).DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CERT_CONTEXT)); IntPtr ptr = new IntPtr(((long)cert_context.pCertInfo) + ((long)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CERT_INFO), "SubjectPublicKeyInfo"))); IntPtr ptr2 = new IntPtr(((long)ptr) + ((long)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO), "Algorithm"))); IntPtr ptr3 = new IntPtr(((long)ptr2) + ((long)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER), "Parameters"))); IntPtr pvKey = Marshal.ReadIntPtr(ptr2); if (System.Security.Cryptography.CAPI.CryptFindOIDInfo(1, pvKey, 3).Algid == 0x2200) { bool flag = false; IntPtr ptr5 = new IntPtr(((long)ptr3) + ((long)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB), "cbData"))); IntPtr ptr6 = new IntPtr(((long)ptr3) + ((long)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB), "pbData"))); if (Marshal.ReadInt32(ptr5) == 0) { flag = true; } else if (Marshal.ReadIntPtr(ptr6) == IntPtr.Zero) { flag = true; } else if (Marshal.ReadInt32(Marshal.ReadIntPtr(ptr6)) == 5) { flag = true; } if (flag) { System.Security.Cryptography.SafeCertChainHandle ppChainContext = System.Security.Cryptography.SafeCertChainHandle.InvalidHandle; System.Security.Cryptography.X509Certificates.X509Utils.BuildChain(new IntPtr(0L), System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(certificate), null, null, null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, DateTime.Now, new TimeSpan(0, 0, 0), ref ppChainContext); ppChainContext.Dispose(); uint pcbData = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(certificate), 0x16, invalidHandle, ref pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (pcbData > 0) { invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)pcbData)); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(certificate), 0x16, invalidHandle, ref pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } Marshal.WriteInt32(ptr5, (int)pcbData); Marshal.WriteIntPtr(ptr6, invalidHandle.DangerousGetHandle()); } } } if (this.m_parentSignerInfo == null) { if (!System.Security.Cryptography.CAPI.CryptMsgControl(this.m_signedCms.GetCryptMsgHandle(), 0, 1, cert_context.pCertInfo)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } goto Label_02F4; } int num2 = -1; int hr = 0; Label_022F: try { num2 = PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this.m_parentSignerInfo, num2 + 1); } catch (CryptographicException) { if (hr == 0) { throw; } throw new CryptographicException(hr); } uint cbData = 0; System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; PkcsUtils.GetParam(this.m_signedCms.GetCryptMsgHandle(), 0x1c, (uint)num2, out pvData, out cbData); if (cbData == 0) { hr = -2146885618; goto Label_022F; } fixed(byte *numRef = this.m_encodedSignerInfo) { if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgVerifyCountersignatureEncoded(IntPtr.Zero, 0x10001, pvData.DangerousGetHandle(), cbData, new IntPtr((void *)numRef), (uint)this.m_encodedSignerInfo.Length, cert_context.pCertInfo)) { hr = Marshal.GetLastWin32Error(); goto Label_022F; } } pvData.Dispose(); Label_02F4: if (!verifySignatureOnly) { int num5 = VerifyCertificate(certificate, extraStore); if (num5 != 0) { throw new CryptographicException(num5); } } invalidHandle.Dispose(); }
private unsafe void RemoveCounterSignature(int parentIndex, int childIndex) { if (parentIndex < 0) { throw new ArgumentOutOfRangeException("parentIndex"); } if (childIndex < 0) { throw new ArgumentOutOfRangeException("childIndex"); } uint cbData = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint num2 = 0; System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint num3 = 0; uint cAttr = 0; IntPtr zero = IntPtr.Zero; System.Security.Cryptography.SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle(); if (PkcsUtils.CmsSupported()) { PkcsUtils.GetParam(cryptMsgHandle, 0x27, (uint)parentIndex, out invalidHandle, out cbData); System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO cmsg_cms_signer_info = (System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO)); cAttr = cmsg_cms_signer_info.UnauthAttrs.cAttr; zero = new IntPtr((long)cmsg_cms_signer_info.UnauthAttrs.rgAttr); } else { PkcsUtils.GetParam(cryptMsgHandle, 6, (uint)parentIndex, out pvData, out num2); System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); cAttr = cmsg_signer_info.UnauthAttrs.cAttr; zero = new IntPtr((long)cmsg_signer_info.UnauthAttrs.rgAttr); } for (num3 = 0; num3 < cAttr; num3++) { System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE crypt_attribute = (System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)Marshal.PtrToStructure(zero, typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)); if ((string.Compare(crypt_attribute.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0) && (crypt_attribute.cValue > 0)) { if (childIndex < crypt_attribute.cValue) { System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_del_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex, dwUnauthAttrIndex = num3 }; if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 9, new IntPtr((void *)&cmsg_ctrl_del_signer_unauth_attr_para))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (crypt_attribute.cValue > 1) { try { byte[] buffer; uint num5 = (uint)((crypt_attribute.cValue - 1) * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB))); System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num5)); System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *rgValue = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)crypt_attribute.rgValue; System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *handle = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)handle4.DangerousGetHandle(); int num6 = 0; while (num6 < crypt_attribute.cValue) { if (num6 != childIndex) { handle[0] = rgValue[0]; } num6++; rgValue++; handle++; } System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE structure = new System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE { pszObjId = crypt_attribute.pszObjId, cValue = crypt_attribute.cValue - 1, rgValue = handle4.DangerousGetHandle() }; System.Security.Cryptography.SafeLocalAllocHandle handle5 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)))); Marshal.StructureToPtr(structure, handle5.DangerousGetHandle(), false); try { if (!System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), handle5.DangerousGetHandle(), out buffer)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { Marshal.DestroyStructure(handle5.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)); handle5.Dispose(); } fixed(byte *numRef = buffer) { System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex }; cmsg_ctrl_add_signer_unauth_attr_para.blob.cbData = (uint)buffer.Length; cmsg_ctrl_add_signer_unauth_attr_para.blob.pbData = new IntPtr((void *)numRef); if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void *)&cmsg_ctrl_add_signer_unauth_attr_para))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } handle4.Dispose(); } catch (CryptographicException) { byte[] buffer2; if (System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), zero, out buffer2)) { fixed(byte *numRef2 = buffer2) { System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para2 = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex }; cmsg_ctrl_add_signer_unauth_attr_para2.blob.cbData = (uint)buffer2.Length; cmsg_ctrl_add_signer_unauth_attr_para2.blob.pbData = new IntPtr((void *)numRef2); System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void *)&cmsg_ctrl_add_signer_unauth_attr_para2)); } } throw; } } return; } childIndex -= (int)crypt_attribute.cValue; } zero = new IntPtr(((long)zero) + Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE))); } if ((invalidHandle != null) && !invalidHandle.IsInvalid) { invalidHandle.Dispose(); } if ((pvData != null) && !pvData.IsInvalid) { pvData.Dispose(); } throw new CryptographicException(-2146885618); }
internal AlgorithmIdentifier(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; byte[] destination = new byte[0]; uint num3 = System.Security.Cryptography.X509Certificates.X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); if (num3 == 0x6602) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x29L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS crypt_rc_cbc_parameters = (System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (crypt_rc_cbc_parameters.dwVersion) { case 0x34: keyLength = 0x38; break; case 0x3a: keyLength = 0x80; break; case 160: keyLength = 40; break; } if (crypt_rc_cbc_parameters.fIV) { destination = (byte[])crypt_rc_cbc_parameters.rgbIV.Clone(); } } } else if (((num3 == 0x6801) || (num3 == 0x6601)) || (num3 == 0x6603)) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x19L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue > 0) { if (num3 == 0x6801) { System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)); if (cryptoapi_blob.cbData > 0) { destination = new byte[cryptoapi_blob.cbData]; Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length); } } else { destination = new byte[cbDecodedValue]; Marshal.Copy(invalidHandle.DangerousGetHandle(), destination, 0, destination.Length); } } } if (num3 == 0x6801) { keyLength = 0x80 - (destination.Length * 8); } else if (num3 == 0x6601) { keyLength = 0x40; } else { keyLength = 0xc0; } } else if (algorithmIdentifier.Parameters.cbData > 0) { destination = new byte[algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, destination, 0, destination.Length); } this.Reset(new System.Security.Cryptography.Oid(algorithmIdentifier.pszObjId), keyLength, destination); invalidHandle.Dispose(); }