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); }
internal static Oid GetContentType(SafeCryptMsgHandle safeCryptMsgHandle) { uint cbData = 0U; byte[] pvData = new byte[0]; PkcsUtils.GetParam(safeCryptMsgHandle, 4U, 0U, out pvData, out cbData); if (pvData.Length > 0 && (int)pvData[pvData.Length - 1] == 0) { byte[] numArray = new byte[pvData.Length - 1]; Array.Copy((Array)pvData, 0, (Array)numArray, 0, numArray.Length); pvData = numArray; } return(new Oid(Encoding.ASCII.GetString(pvData))); }
public static Oid GetContentType(byte[] encodedMessage) { if (encodedMessage == null) { throw new ArgumentNullException("encodedMessage"); } SafeCryptMsgHandle safeCryptMsgHandle = CAPI.CAPISafe.CryptMsgOpenToDecode(65537U, 0U, 0U, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (!CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, encodedMessage, (uint)encodedMessage.Length, true)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } Oid oid; switch (PkcsUtils.GetMessageType(safeCryptMsgHandle)) { case 1U: oid = new Oid("1.2.840.113549.1.7.1"); break; case 2U: oid = new Oid("1.2.840.113549.1.7.2"); break; case 3U: oid = new Oid("1.2.840.113549.1.7.3"); break; case 4U: oid = new Oid("1.2.840.113549.1.7.4"); break; case 5U: oid = new Oid("1.2.840.113549.1.7.5"); break; case 6U: oid = new Oid("1.2.840.113549.1.7.6"); break; default: throw new CryptographicException(-2146889724); } safeCryptMsgHandle.Dispose(); return(oid); }
public void Decode(byte[] encodedMessage) { if (encodedMessage == null) { throw new ArgumentNullException("encodedMessage"); } if (this.m_safeCryptMsgHandle != null && !this.m_safeCryptMsgHandle.IsInvalid) { this.m_safeCryptMsgHandle.Dispose(); } this.m_safeCryptMsgHandle = SignedCms.OpenToDecode(encodedMessage, this.ContentInfo, this.Detached); if (this.Detached) { return; } this.m_contentInfo = new ContentInfo(PkcsUtils.GetContentType(this.m_safeCryptMsgHandle), PkcsUtils.GetContent(this.m_safeCryptMsgHandle)); }
public SignedCms(SubjectIdentifierType signerIdentifierType, ContentInfo contentInfo, bool detached) { if (contentInfo == null) { throw new ArgumentNullException("contentInfo"); } if (contentInfo.Content == null) { throw new ArgumentNullException("contentInfo.Content"); } if (signerIdentifierType != SubjectIdentifierType.SubjectKeyIdentifier && signerIdentifierType != SubjectIdentifierType.IssuerAndSerialNumber && signerIdentifierType != SubjectIdentifierType.NoSignature) { signerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber; } this.m_safeCryptMsgHandle = SafeCryptMsgHandle.InvalidHandle; this.m_signerIdentifierType = signerIdentifierType; this.m_version = 0; this.m_contentInfo = contentInfo; this.m_detached = detached; }
internal static unsafe void GetParam(SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out SafeLocalAllocHandle pvData, out uint cbData) { cbData = 0U; pvData = SafeLocalAllocHandle.InvalidHandle; fixed(uint *numPtr = &cbData) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numPtr))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } if (cbData > 0U) { pvData = CAPI.LocalAlloc(64U, new IntPtr((long)cbData)); if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numPtr))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } } } }
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 unsafe uint AddCertsToMessage(SafeCryptMsgHandle safeCryptMsgHandle, X509Certificate2Collection bagOfCerts, X509Certificate2Collection chainOfCerts) { uint num = 0U; foreach (X509Certificate2 certificate in chainOfCerts) { if (bagOfCerts.Find(X509FindType.FindByThumbprint, (object)certificate.Thumbprint, false).Count == 0) { CAPI.CERT_CONTEXT certContext = *(CAPI.CERT_CONTEXT *)(void *) X509Utils.GetCertContext(certificate).DangerousGetHandle(); if (!CAPI.CryptMsgControl(safeCryptMsgHandle, 0U, 10U, new IntPtr((long)&new CAPI.CRYPTOAPI_BLOB() { cbData = certContext.cbCertEncoded, pbData = certContext.pbCertEncoded }))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } ++num; } } return(num); }
internal static unsafe void GetParam(SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out byte[] pvData, out uint cbData) { cbData = 0U; pvData = new byte[0]; fixed(uint *numPtr1 = &cbData) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, IntPtr.Zero, new IntPtr((void *)numPtr1))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } if (cbData > 0U) { pvData = new byte[(IntPtr)cbData]; fixed(byte *numPtr2 = &pvData[0]) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, new IntPtr((void *)numPtr2), new IntPtr((void *)numPtr1))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } } } } }
internal RecipientInfoCollection() { this.m_safeCryptMsgHandle = SafeCryptMsgHandle.InvalidHandle; this.m_recipientInfos = new ArrayList(); }
internal RecipientInfoCollection(SafeCryptMsgHandle safeCryptMsgHandle) { bool flag = PkcsUtils.CmsSupported(); uint num1 = 0U; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); if (flag) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 33U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } else if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 17U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } this.m_recipientInfos = new ArrayList(); for (uint index = 0U; index < num1; ++index) { if (flag) { SafeLocalAllocHandle pvData; uint cbData; PkcsUtils.GetParam(safeCryptMsgHandle, 36U, index, out pvData, out cbData); CAPI.CMSG_CMS_RECIPIENT_INFO cmsRecipientInfo = (CAPI.CMSG_CMS_RECIPIENT_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_RECIPIENT_INFO)); switch (cmsRecipientInfo.dwRecipientChoice) { case 1U: CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans = (CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO)); this.m_recipientInfos.Add((object)new KeyTransRecipientInfo(pvData, keyTrans, index)); continue; case 2U: CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO agreeRecipientInfo = (CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO)); switch (agreeRecipientInfo.dwOriginatorChoice) { case 1U: CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO certIdRecipient = (CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO)); for (uint subIndex = 0U; subIndex < certIdRecipient.cRecipientEncryptedKeys; ++subIndex) { this.m_recipientInfos.Add((object)new KeyAgreeRecipientInfo(pvData, certIdRecipient, index, subIndex)); } continue; case 2U: CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO publicKeyRecipient = (CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO)); for (uint subIndex = 0U; subIndex < publicKeyRecipient.cRecipientEncryptedKeys; ++subIndex) { this.m_recipientInfos.Add((object)new KeyAgreeRecipientInfo(pvData, publicKeyRecipient, index, subIndex)); } continue; default: throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Originator_Identifier_Choice"), agreeRecipientInfo.dwOriginatorChoice.ToString((IFormatProvider)CultureInfo.CurrentCulture)); } default: throw new CryptographicException(-2147483647); } } else { SafeLocalAllocHandle pvData; uint cbData; PkcsUtils.GetParam(safeCryptMsgHandle, 19U, index, out pvData, out cbData); CAPI.CERT_INFO certInfo = (CAPI.CERT_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CERT_INFO)); this.m_recipientInfos.Add((object)new KeyTransRecipientInfo(pvData, certInfo, index)); } } this.m_safeCryptMsgHandle = safeCryptMsgHandle; }
internal RecipientInfoCollection(RecipientInfo recipientInfo) { this.m_safeCryptMsgHandle = SafeCryptMsgHandle.InvalidHandle; this.m_recipientInfos = new ArrayList(1); this.m_recipientInfos.Add((object)recipientInfo); }
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); }
protected override bool ReleaseHandle() { return(SafeCryptMsgHandle.CryptMsgClose(this.handle)); }
internal static unsafe CryptographicAttributeObjectCollection GetUnprotectedAttributes(SafeCryptMsgHandle safeCryptMsgHandle) { uint num = 0U; CryptographicAttributeObjectCollection objectCollection = new CryptographicAttributeObjectCollection(); SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 37U, 0U, invalidHandle, new IntPtr((void *)&num)) && Marshal.GetLastWin32Error() != -2146889713) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } if (num > 0U) { SafeLocalAllocHandle localAllocHandle; using (localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr((long)num))) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 37U, 0U, localAllocHandle, new IntPtr((void *)&num))) { PkcsUtils.checkErr(Marshal.GetLastWin32Error()); } objectCollection = new CryptographicAttributeObjectCollection(localAllocHandle); } } return(objectCollection); }