private void CounterSign(CmsSigner signer) { CspParameters parameters = new CspParameters(); if (!X509Utils.GetPrivateKeyInfo(X509Utils.GetCertContext(signer.Certificate), ref parameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } KeyContainerPermission containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Open | KeyContainerPermissionFlags.Sign); containerPermission.AccessEntries.Add(accessEntry); containerPermission.Demand(); uint dwIndex = (uint)PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this, 0); SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)))); CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = PkcsUtils.CreateSignerEncodeInfo(signer); try { Marshal.StructureToPtr((object)signerEncodeInfo, localAllocHandle.DangerousGetHandle(), false); if (!CAPI.CryptMsgCountersign(this.m_signedCms.GetCryptMsgHandle(), dwIndex, 1U, localAllocHandle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } this.m_signedCms.ReopenToDecode(); } finally { Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)); localAllocHandle.Dispose(); signerEncodeInfo.Dispose(); } int num = (int)PkcsUtils.AddCertsToMessage(this.m_signedCms.GetCryptMsgHandle(), this.m_signedCms.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
internal static unsafe X509Certificate2 CreateDummyCertificate(CspParameters parameters) { System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle; SafeCryptProvHandle hCryptProv = SafeCryptProvHandle.InvalidHandle; uint dwFlags = 0U; if ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags) { dwFlags |= 32U; } if ((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != CspProviderFlags.NoFlags) { dwFlags |= 4026531840U; } if ((parameters.Flags & CspProviderFlags.NoPrompt) != CspProviderFlags.NoFlags) { dwFlags |= 64U; } if (!CAPI.CryptAcquireContext(ref hCryptProv, parameters.KeyContainerName, parameters.ProviderName, (uint)parameters.ProviderType, dwFlags)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPT_KEY_PROV_INFO cryptKeyProvInfo = new CAPI.CRYPT_KEY_PROV_INFO(); cryptKeyProvInfo.pwszProvName = parameters.ProviderName; cryptKeyProvInfo.pwszContainerName = parameters.KeyContainerName; cryptKeyProvInfo.dwProvType = (uint)parameters.ProviderType; cryptKeyProvInfo.dwKeySpec = (uint)parameters.KeyNumber; cryptKeyProvInfo.dwFlags = (parameters.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore ? 32U : 0U; SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_KEY_PROV_INFO)))); Marshal.StructureToPtr((object)cryptKeyProvInfo, localAllocHandle1.DangerousGetHandle(), false); CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier = new CAPI.CRYPT_ALGORITHM_IDENTIFIER(); algorithmIdentifier.pszObjId = "1.3.14.3.2.29"; SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER)))); Marshal.StructureToPtr((object)algorithmIdentifier, localAllocHandle2.DangerousGetHandle(), false); X500DistinguishedName distinguishedName = new X500DistinguishedName("cn=CMS Signer Dummy Certificate"); System.Security.Cryptography.SafeCertContextHandle selfSignCertificate; fixed(byte *numPtr = distinguishedName.RawData) selfSignCertificate = CAPI.CAPIUnsafe.CertCreateSelfSignCertificate(hCryptProv, new IntPtr((void *)&new CAPI.CRYPTOAPI_BLOB() { cbData = (uint)distinguishedName.RawData.Length, pbData = new IntPtr((void *)numPtr) }), 1U, localAllocHandle1.DangerousGetHandle(), localAllocHandle2.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); Marshal.DestroyStructure(localAllocHandle1.DangerousGetHandle(), typeof(CAPI.CRYPT_KEY_PROV_INFO)); localAllocHandle1.Dispose(); Marshal.DestroyStructure(localAllocHandle2.DangerousGetHandle(), typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER)); localAllocHandle2.Dispose(); if (selfSignCertificate == null || selfSignCertificate.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509Certificate2 x509Certificate2 = new X509Certificate2(selfSignCertificate.DangerousGetHandle()); selfSignCertificate.Dispose(); return(x509Certificate2); }
private void CoSign(CmsSigner signer, bool silent) { CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = PkcsUtils.CreateSignerEncodeInfo(signer, silent); try { SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr((object)signerEncodeInfo, localAllocHandle.DangerousGetHandle(), false); if (!CAPI.CryptMsgControl(this.m_safeCryptMsgHandle, 0U, 6U, localAllocHandle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)); localAllocHandle.Dispose(); } } finally { signerEncodeInfo.Dispose(); } int num = (int)PkcsUtils.AddCertsToMessage(this.m_safeCryptMsgHandle, this.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
internal static unsafe int GetSignerIndex(SafeCryptMsgHandle safeCrytpMsgHandle, SignerInfo signerInfo, int startIndex) { uint num1 = 0U; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 5U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } for (int index = startIndex; index < (int)num1; ++index) { uint num3 = 0U; if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6U, (uint)index, IntPtr.Zero, new IntPtr((void *)&num3))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } if (num3 > 0U) { SafeLocalAllocHandle pvData = CAPI.LocalAlloc(0U, new IntPtr((long)num3)); if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6U, (uint)index, pvData, new IntPtr((void *)&num3))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } CAPI.CMSG_SIGNER_INFO cmsgSignerInfo1 = signerInfo.GetCmsgSignerInfo(); CAPI.CMSG_SIGNER_INFO cmsgSignerInfo2 = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); if (X509Utils.MemEqual((byte *)(void *)cmsgSignerInfo1.Issuer.pbData, cmsgSignerInfo1.Issuer.cbData, (byte *)(void *)cmsgSignerInfo2.Issuer.pbData, cmsgSignerInfo2.Issuer.cbData) && X509Utils.MemEqual((byte *)(void *)cmsgSignerInfo1.SerialNumber.pbData, cmsgSignerInfo1.SerialNumber.cbData, (byte *)(void *)cmsgSignerInfo2.SerialNumber.pbData, cmsgSignerInfo2.SerialNumber.cbData)) { return(index); } pvData.Dispose(); } } throw new CryptographicException(-2146889714); }
private unsafe void Sign(CmsSigner signer, bool silent) { CAPI.CMSG_SIGNED_ENCODE_INFO signedEncodeInfo = new CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNED_ENCODE_INFO))); CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = PkcsUtils.CreateSignerEncodeInfo(signer, silent); byte[] encodedMessage = (byte[])null; try { SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(0U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr((object)signerEncodeInfo, localAllocHandle.DangerousGetHandle(), false); X509Certificate2Collection bagOfCertificates = PkcsUtils.CreateBagOfCertificates(signer); SafeLocalAllocHandle encodedCertBlob = PkcsUtils.CreateEncodedCertBlob(bagOfCertificates); signedEncodeInfo.cSigners = 1U; signedEncodeInfo.rgSigners = localAllocHandle.DangerousGetHandle(); signedEncodeInfo.cCertEncoded = (uint)bagOfCertificates.Count; if (bagOfCertificates.Count > 0) { signedEncodeInfo.rgCertEncoded = encodedCertBlob.DangerousGetHandle(); } SafeCryptMsgHandle safeCryptMsgHandle = string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) != 0 ? CAPI.CryptMsgOpenToEncode(65537U, this.Detached ? 4U : 0U, 2U, new IntPtr((void *)&signedEncodeInfo), this.ContentInfo.ContentType.Value, IntPtr.Zero) : CAPI.CryptMsgOpenToEncode(65537U, this.Detached ? 4U : 0U, 2U, new IntPtr((void *)&signedEncodeInfo), IntPtr.Zero, IntPtr.Zero); if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (this.ContentInfo.Content.Length > 0 && !CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, this.ContentInfo.pContent, (uint)this.ContentInfo.Content.Length, true)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } encodedMessage = PkcsUtils.GetContent(safeCryptMsgHandle); safeCryptMsgHandle.Dispose(); encodedCertBlob.Dispose(); } finally { Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)); localAllocHandle.Dispose(); } } finally { signerEncodeInfo.Dispose(); } SafeCryptMsgHandle safeCryptMsgHandle1 = SignedCms.OpenToDecode(encodedMessage, this.ContentInfo, this.Detached); if (this.m_safeCryptMsgHandle != null && !this.m_safeCryptMsgHandle.IsInvalid) { this.m_safeCryptMsgHandle.Dispose(); } this.m_safeCryptMsgHandle = safeCryptMsgHandle1; GC.KeepAlive((object)signer); }
private static byte[] Encode(DateTime signingTime) { long val = signingTime.ToFileTimeUtc(); SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(long)))); Marshal.WriteInt64(localAllocHandle.DangerousGetHandle(), val); byte[] encodedData = new byte[0]; if (!CAPI.EncodeObject("1.2.840.113549.1.9.5", localAllocHandle.DangerousGetHandle(), out encodedData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } localAllocHandle.Dispose(); return(encodedData); }
private void Decode() { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null; if (!CAPI.DecodeObject(new IntPtr(17L), this.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(CAPI.CERT_PUBLIC_KEY_INFO keyInfo) { SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO)))); Marshal.StructureToPtr((object)keyInfo, localAllocHandle.DangerousGetHandle(), false); int keyLength = (int)CAPI.CAPISafe.CertGetPublicKeyLength(65537U, localAllocHandle.DangerousGetHandle()); byte[] numArray = new byte[(IntPtr)keyInfo.Algorithm.Parameters.cbData]; if (numArray.Length > 0) { Marshal.Copy(keyInfo.Algorithm.Parameters.pbData, numArray, 0, numArray.Length); } Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CERT_PUBLIC_KEY_INFO)); localAllocHandle.Dispose(); this.Reset(new Oid(keyInfo.Algorithm.pszObjId), keyLength, numArray); }
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 SafeCertChainHandle ppChainContext) { if (pCertContext == null || pCertContext.IsInvalid) { throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_InvalidContextHandle"), "pCertContext"); } SafeCertStoreHandle hAdditionalStore = SafeCertStoreHandle.InvalidHandle; if (extraStore != null && extraStore.Count > 0) { hAdditionalStore = X509Utils.ExportToMemoryStore(extraStore); } CAPI.CERT_CHAIN_PARA pChainPara = new CAPI.CERT_CHAIN_PARA(); pChainPara.cbSize = (uint)Marshal.SizeOf((object)pChainPara); SafeLocalAllocHandle localAllocHandle1 = SafeLocalAllocHandle.InvalidHandle; if (applicationPolicy != null && applicationPolicy.Count > 0) { pChainPara.RequestedUsage.dwType = 0U; pChainPara.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count; localAllocHandle1 = X509Utils.CopyOidsToUnmanagedMemory(applicationPolicy); pChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = localAllocHandle1.DangerousGetHandle(); } SafeLocalAllocHandle localAllocHandle2 = SafeLocalAllocHandle.InvalidHandle; if (certificatePolicy != null && certificatePolicy.Count > 0) { pChainPara.RequestedIssuancePolicy.dwType = 0U; pChainPara.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count; localAllocHandle2 = X509Utils.CopyOidsToUnmanagedMemory(certificatePolicy); pChainPara.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = localAllocHandle2.DangerousGetHandle(); } pChainPara.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds; System.Runtime.InteropServices.ComTypes.FILETIME pTime = new System.Runtime.InteropServices.ComTypes.FILETIME(); *(long *)&pTime = verificationTime.ToFileTime(); uint dwFlags = X509Utils.MapRevocationFlags(revocationMode, revocationFlag); if (!CAPI.CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, hAdditionalStore, ref pChainPara, dwFlags, IntPtr.Zero, out ppChainContext)) { return(Marshal.GetHRForLastWin32Error()); } localAllocHandle1.Dispose(); localAllocHandle2.Dispose(); return(0); }
internal static bool GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters) { SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; uint pcbData = 0U; if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2U, invalidHandle, out pcbData)) { if (Marshal.GetLastWin32Error() == -2146885628) { return(false); } else { throw new CryptographicException(Marshal.GetLastWin32Error()); } } else { SafeLocalAllocHandle pvData = CAPI.LocalAlloc(0U, new IntPtr((long)pcbData)); if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2U, pvData, out pcbData)) { if (Marshal.GetLastWin32Error() == -2146885628) { return(false); } else { throw new CryptographicException(Marshal.GetLastWin32Error()); } } else { CAPI.CRYPT_KEY_PROV_INFO cryptKeyProvInfo = (CAPI.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CRYPT_KEY_PROV_INFO)); parameters.ProviderName = cryptKeyProvInfo.pwszProvName; parameters.KeyContainerName = cryptKeyProvInfo.pwszContainerName; parameters.ProviderType = (int)cryptKeyProvInfo.dwProvType; parameters.KeyNumber = (int)cryptKeyProvInfo.dwKeySpec; parameters.Flags = ((int)cryptKeyProvInfo.dwFlags & 32) == 32 ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags; pvData.Dispose(); return(true); } } }
internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier(SafeCryptMsgHandle safeCryptMsgHandle) { AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(); uint num = 0U; if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15U, 0U, IntPtr.Zero, new IntPtr((void *)&num))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } if (num > 0U) { SafeLocalAllocHandle pvData = CAPI.LocalAlloc(0U, new IntPtr((long)num)); if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15U, 0U, pvData, new IntPtr((void *)&num))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } algorithmIdentifier = new AlgorithmIdentifier((CAPI.CRYPT_ALGORITHM_IDENTIFIER)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER))); pvData.Dispose(); } return(algorithmIdentifier); }
internal static string DecodeOctetString(byte[] encodedOctetString) { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null; if (!CAPI.DecodeObject(new IntPtr(25L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if ((int)cbDecodedValue == 0) { return(string.Empty); } CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if ((int)cryptoapiBlob.cbData == 0) { return(string.Empty); } string str = Marshal.PtrToStringUni(cryptoapiBlob.pbData); decodedValue.Dispose(); return(str); }
internal static unsafe X509IssuerSerial DecodeIssuerSerial(CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial) { SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; uint csz = CAPI.CAPISafe.CertNameToStrW(65537U, new IntPtr((void *)&pIssuerAndSerial.Issuer), 33554435U, invalidHandle, 0U); if (csz <= 1U) { throw new CryptographicException(Marshal.GetLastWin32Error()); } SafeLocalAllocHandle psz = CAPI.LocalAlloc(0U, new IntPtr((long)(2U * csz))); if (CAPI.CAPISafe.CertNameToStrW(65537U, new IntPtr((void *)&pIssuerAndSerial.Issuer), 33554435U, psz, csz) <= 1U) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509IssuerSerial x509IssuerSerial = new X509IssuerSerial(); x509IssuerSerial.IssuerName = Marshal.PtrToStringUni(psz.DangerousGetHandle()); byte[] numArray = new byte[(IntPtr)pIssuerAndSerial.SerialNumber.cbData]; Marshal.Copy(pIssuerAndSerial.SerialNumber.pbData, numArray, 0, numArray.Length); x509IssuerSerial.SerialNumber = X509Utils.EncodeHexStringFromInt(numArray); psz.Dispose(); return(x509IssuerSerial); }
private unsafe void RemoveCounterSignature(int parentIndex, int childIndex) { if (parentIndex < 0) { throw new ArgumentOutOfRangeException("parentIndex"); } if (childIndex < 0) { throw new ArgumentOutOfRangeException("childIndex"); } uint cbData1 = 0U; SafeLocalAllocHandle pvData1 = SafeLocalAllocHandle.InvalidHandle; uint cbData2 = 0U; SafeLocalAllocHandle pvData2 = SafeLocalAllocHandle.InvalidHandle; IntPtr num1 = IntPtr.Zero; SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle(); uint num2; if (PkcsUtils.CmsSupported()) { PkcsUtils.GetParam(cryptMsgHandle, 39U, (uint)parentIndex, out pvData1, out cbData1); CAPI.CMSG_CMS_SIGNER_INFO cmsgCmsSignerInfo = (CAPI.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(pvData1.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_SIGNER_INFO)); num2 = cmsgCmsSignerInfo.UnauthAttrs.cAttr; num1 = new IntPtr((long)cmsgCmsSignerInfo.UnauthAttrs.rgAttr); } else { PkcsUtils.GetParam(cryptMsgHandle, 6U, (uint)parentIndex, out pvData2, out cbData2); CAPI.CMSG_SIGNER_INFO cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData2.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); num2 = cmsgSignerInfo.UnauthAttrs.cAttr; num1 = new IntPtr((long)cmsgSignerInfo.UnauthAttrs.rgAttr); } for (uint index = 0U; index < num2; ++index) { CAPI.CRYPT_ATTRIBUTE cryptAttribute1 = (CAPI.CRYPT_ATTRIBUTE)Marshal.PtrToStructure(num1, typeof(CAPI.CRYPT_ATTRIBUTE)); if (string.Compare(cryptAttribute1.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0 && cryptAttribute1.cValue > 0U) { if (childIndex < (int)cryptAttribute1.cValue) { if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 9U, new IntPtr((void *)&new CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex, dwUnauthAttrIndex = index }))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cryptAttribute1.cValue <= 1U) { return; } try { SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr((long)(uint)((ulong)(cryptAttribute1.cValue - 1U) * (ulong)Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB))))); CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr1 = (CAPI.CRYPTOAPI_BLOB *)(void *) cryptAttribute1.rgValue; CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr2 = (CAPI.CRYPTOAPI_BLOB *)(void *) localAllocHandle1.DangerousGetHandle(); int num3 = 0; while (num3 < (int)cryptAttribute1.cValue) { if (num3 != childIndex) { *cryptoapiBlobPtr2 = *cryptoapiBlobPtr1; } ++num3; ++cryptoapiBlobPtr1; ++cryptoapiBlobPtr2; } CAPI.CRYPT_ATTRIBUTE cryptAttribute2 = new CAPI.CRYPT_ATTRIBUTE(); cryptAttribute2.pszObjId = cryptAttribute1.pszObjId; cryptAttribute2.cValue = cryptAttribute1.cValue - 1U; cryptAttribute2.rgValue = localAllocHandle1.DangerousGetHandle(); SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE)))); Marshal.StructureToPtr((object)cryptAttribute2, localAllocHandle2.DangerousGetHandle(), false); byte[] encodedData; try { if (!CAPI.EncodeObject(new IntPtr(22L), localAllocHandle2.DangerousGetHandle(), out encodedData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { Marshal.DestroyStructure(localAllocHandle2.DangerousGetHandle(), typeof(CAPI.CRYPT_ATTRIBUTE)); localAllocHandle2.Dispose(); } fixed(byte *numPtr = &encodedData[0]) { if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex, blob = { cbData = (uint)encodedData.Length, pbData = new IntPtr((void *)numPtr) } }))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } localAllocHandle1.Dispose(); return; } catch (CryptographicException ex) { byte[] encodedData; if (CAPI.EncodeObject(new IntPtr(22L), num1, out encodedData)) { fixed(byte *numPtr = &encodedData[0]) CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) { dwSignerIndex = (uint)parentIndex, blob = { cbData = (uint)encodedData.Length, pbData = new IntPtr((void *)numPtr) } })); } throw; } } else { childIndex -= (int)cryptAttribute1.cValue; } } num1 = new IntPtr((long)num1 + (long)Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE))); } if (pvData1 != null && !pvData1.IsInvalid) { pvData1.Dispose(); } if (pvData2 != null && !pvData2.IsInvalid) { pvData2.Dispose(); } throw new CryptographicException(-2146885618); }
private unsafe void Verify(X509Certificate2Collection extraStore, X509Certificate2 certificate, bool verifySignatureOnly) { SafeLocalAllocHandle pvData1 = SafeLocalAllocHandle.InvalidHandle; CAPI.CERT_CONTEXT certContext = (CAPI.CERT_CONTEXT)Marshal.PtrToStructure(X509Utils.GetCertContext(certificate).DangerousGetHandle(), typeof(CAPI.CERT_CONTEXT)); IntPtr ptr1 = new IntPtr((long)new IntPtr((long)certContext.pCertInfo + (long)Marshal.OffsetOf(typeof(CAPI.CERT_INFO), "SubjectPublicKeyInfo")) + (long)Marshal.OffsetOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO), "Algorithm")); IntPtr num1 = new IntPtr((long)ptr1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER), "Parameters")); if ((int)CAPI.CryptFindOIDInfo(1U, Marshal.ReadIntPtr(ptr1), 3U).Algid == 8704) { bool flag = false; IntPtr ptr2 = new IntPtr((long)num1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPTOAPI_BLOB), "cbData")); IntPtr ptr3 = new IntPtr((long)num1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPTOAPI_BLOB), "pbData")); if (Marshal.ReadInt32(ptr2) == 0) { flag = true; } else if (Marshal.ReadIntPtr(ptr3) == IntPtr.Zero) { flag = true; } else if (Marshal.ReadInt32(Marshal.ReadIntPtr(ptr3)) == 5) { flag = true; } if (flag) { SafeCertChainHandle invalidHandle = SafeCertChainHandle.InvalidHandle; X509Utils.BuildChain(new IntPtr(0L), X509Utils.GetCertContext(certificate), (X509Certificate2Collection)null, (OidCollection)null, (OidCollection)null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, DateTime.Now, new TimeSpan(0, 0, 0), ref invalidHandle); invalidHandle.Dispose(); uint pcbData = 0U; if (!CAPI.CAPISafe.CertGetCertificateContextProperty(X509Utils.GetCertContext(certificate), 22U, pvData1, out pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (pcbData > 0U) { pvData1 = CAPI.LocalAlloc(64U, new IntPtr((long)pcbData)); if (!CAPI.CAPISafe.CertGetCertificateContextProperty(X509Utils.GetCertContext(certificate), 22U, pvData1, out pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } Marshal.WriteInt32(ptr2, (int)pcbData); Marshal.WriteIntPtr(ptr3, pvData1.DangerousGetHandle()); } } } if (this.m_parentSignerInfo == null) { if (!CAPI.CryptMsgControl(this.m_signedCms.GetCryptMsgHandle(), 0U, 1U, certContext.pCertInfo)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } else { int num2 = -1; int hr = 0; SafeLocalAllocHandle pvData2; while (true) { try { num2 = PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this.m_parentSignerInfo, num2 + 1); } catch (CryptographicException ex) { if (hr != 0) { throw new CryptographicException(hr); } throw; } uint cbData = 0U; pvData2 = SafeLocalAllocHandle.InvalidHandle; PkcsUtils.GetParam(this.m_signedCms.GetCryptMsgHandle(), 28U, (uint)num2, out pvData2, out cbData); if ((int)cbData == 0) { hr = -2146885618; } else { fixed(byte *numPtr = this.m_encodedSignerInfo) { if (!CAPI.CAPISafe.CryptMsgVerifyCountersignatureEncoded(IntPtr.Zero, 65537U, pvData2.DangerousGetHandle(), cbData, new IntPtr((void *)numPtr), (uint)this.m_encodedSignerInfo.Length, certContext.pCertInfo)) { hr = Marshal.GetLastWin32Error(); } else { break; } } } } // ISSUE: fixed variable is out of scope // ISSUE: __unpin statement __unpin(numPtr); pvData2.Dispose(); } if (!verifySignatureOnly) { int hr = SignerInfo.VerifyCertificate(certificate, extraStore); if (hr != 0) { throw new CryptographicException(hr); } } pvData1.Dispose(); }
internal AlgorithmIdentifier(CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle; byte[] numArray = new byte[0]; uint num = X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); switch (num) { case 26114U: if (algorithmIdentifier.Parameters.cbData > 0U) { if (!CAPI.DecodeObject(new IntPtr(41L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPT_RC2_CBC_PARAMETERS rc2CbcParameters = (CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (rc2CbcParameters.dwVersion) { case 52U: keyLength = 56; break; case 58U: keyLength = 128; break; case 160U: keyLength = 40; break; } if (rc2CbcParameters.fIV) { numArray = (byte[])rc2CbcParameters.rgbIV.Clone(); break; } else { break; } } else { break; } case 26625U: case 26113U: case 26115U: if (algorithmIdentifier.Parameters.cbData > 0U) { if (!CAPI.DecodeObject(new IntPtr(25L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue > 0U) { if ((int)num == 26625) { CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if (cryptoapiBlob.cbData > 0U) { numArray = new byte[(IntPtr)cryptoapiBlob.cbData]; Marshal.Copy(cryptoapiBlob.pbData, numArray, 0, numArray.Length); } } else { numArray = new byte[(IntPtr)cbDecodedValue]; Marshal.Copy(decodedValue.DangerousGetHandle(), numArray, 0, numArray.Length); } } } keyLength = (int)num != 26625 ? ((int)num != 26113 ? 192 : 64) : 128 - numArray.Length * 8; break; default: if (algorithmIdentifier.Parameters.cbData > 0U) { numArray = new byte[(IntPtr)algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, numArray, 0, numArray.Length); break; } else { break; } } this.Reset(new Oid(algorithmIdentifier.pszObjId), keyLength, numArray); decodedValue.Dispose(); }