internal static extern unsafe SafeCryptMsgHandle CryptMsgOpenToEncode( MsgEncodingType dwMsgEncodingType, int dwFlags, CryptMsgType dwMsgType, CMSG_ENVELOPED_ENCODE_INFO *pvMsgEncodeInfo, [MarshalAs(UnmanagedType.LPStr)] string pszInnerContentObjID, IntPtr pStreamInfo);
internal static extern bool CryptMsgGetParam( #endif SafeCryptMsgHandle hCryptMsg, CryptMsgParamType dwParamType, int dwIndex, out CryptMsgType pvData, ref int pcbData);
internal static DecryptorPalWindows Decode( ReadOnlySpan <byte> encodedMessage, out int version, out ContentInfo contentInfo, out AlgorithmIdentifier contentEncryptionAlgorithm, out X509Certificate2Collection originatorCerts, out CryptographicAttributeObjectCollection unprotectedAttributes ) { SafeCryptMsgHandle hCryptMsg = Interop.Crypt32.CryptMsgOpenToDecode(MsgEncodingType.All, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (hCryptMsg == null || hCryptMsg.IsInvalid) { throw Marshal.GetLastWin32Error().ToCryptographicException(); } if (!Interop.Crypt32.CryptMsgUpdate( hCryptMsg, ref MemoryMarshal.GetReference(encodedMessage), encodedMessage.Length, fFinal: true)) { throw Marshal.GetLastWin32Error().ToCryptographicException(); } CryptMsgType cryptMsgType = hCryptMsg.GetMessageType(); if (cryptMsgType != CryptMsgType.CMSG_ENVELOPED) { throw ErrorCode.CRYPT_E_INVALID_MSG_TYPE.ToCryptographicException(); } version = hCryptMsg.GetVersion(); contentInfo = hCryptMsg.GetContentInfo(); AlgorithmIdentifierAsn contentEncryptionAlgorithmAsn; using (SafeHandle sh = hCryptMsg.GetMsgParamAsMemory(CryptMsgParamType.CMSG_ENVELOPE_ALGORITHM_PARAM)) { unsafe { CRYPT_ALGORITHM_IDENTIFIER *pCryptAlgorithmIdentifier = (CRYPT_ALGORITHM_IDENTIFIER *)(sh.DangerousGetHandle()); contentEncryptionAlgorithm = (*pCryptAlgorithmIdentifier).ToAlgorithmIdentifier(); contentEncryptionAlgorithmAsn.Algorithm = contentEncryptionAlgorithm.Oid.Value !; contentEncryptionAlgorithmAsn.Parameters = (*pCryptAlgorithmIdentifier).Parameters.ToByteArray(); } } originatorCerts = hCryptMsg.GetOriginatorCerts(); unprotectedAttributes = hCryptMsg.GetUnprotectedAttributes(); RecipientInfoCollection recipientInfos = CreateRecipientInfos(hCryptMsg); return(new DecryptorPalWindows(hCryptMsg, recipientInfos, contentEncryptionAlgorithmAsn)); }
public sealed override Oid GetEncodedMessageType(byte[] encodedMessage) { using (SafeCryptMsgHandle hCryptMsg = Interop.Crypt32.CryptMsgOpenToDecode(MsgEncodingType.All, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)) { if (hCryptMsg == null || hCryptMsg.IsInvalid) { throw Marshal.GetLastWin32Error().ToCryptographicException(); } if (!Interop.Crypt32.CryptMsgUpdate(hCryptMsg, encodedMessage, encodedMessage.Length, fFinal: true)) { throw Marshal.GetLastWin32Error().ToCryptographicException(); } int msgTypeAsInt; int cbSize = sizeof(int); if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_TYPE_PARAM, 0, out msgTypeAsInt, ref cbSize)) { throw Marshal.GetLastWin32Error().ToCryptographicException(); } CryptMsgType msgType = (CryptMsgType)msgTypeAsInt; switch (msgType) { case CryptMsgType.CMSG_DATA: return(Oid.FromOidValue(Oids.Pkcs7Data, OidGroup.ExtensionOrAttribute)); case CryptMsgType.CMSG_SIGNED: return(Oid.FromOidValue(Oids.Pkcs7Signed, OidGroup.ExtensionOrAttribute)); case CryptMsgType.CMSG_ENVELOPED: return(Oid.FromOidValue(Oids.Pkcs7Enveloped, OidGroup.ExtensionOrAttribute)); case CryptMsgType.CMSG_SIGNED_AND_ENVELOPED: return(Oid.FromOidValue(Oids.Pkcs7SignedEnveloped, OidGroup.ExtensionOrAttribute)); case CryptMsgType.CMSG_HASHED: return(Oid.FromOidValue(Oids.Pkcs7Hashed, OidGroup.ExtensionOrAttribute)); case CryptMsgType.CMSG_ENCRYPTED: return(Oid.FromOidValue(Oids.Pkcs7Encrypted, OidGroup.ExtensionOrAttribute)); default: throw ErrorCode.CRYPT_E_INVALID_MSG_TYPE.ToCryptographicException(); } } }
internal static extern bool CryptMsgGetParam(SafeCryptMsgHandle hCryptMsg, CryptMsgParamType dwParamType, int dwIndex, out CryptMsgType pvData, [In, Out] ref int pcbData);
internal static unsafe extern SafeCryptMsgHandle CryptMsgOpenToEncode(MsgEncodingType dwMsgEncodingType, int dwFlags, CryptMsgType dwMsgType, CMSG_ENVELOPED_ENCODE_INFO* pvMsgEncodeInfo, [MarshalAs(UnmanagedType.LPStr)] string pszInnerContentObjID, IntPtr pStreamInfo);
internal static partial bool CryptMsgGetParam( SafeCryptMsgHandle hCryptMsg, CryptMsgParamType dwParamType, int dwIndex, out CryptMsgType pvData, ref int pcbData);