2007-06-08 16:20:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Juan Lang
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wincrypt.h"
|
2007-07-12 21:20:20 +00:00
|
|
|
#include "snmp.h"
|
2007-06-08 16:20:49 +00:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
2007-07-12 21:20:20 +00:00
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "crypt32_private.h"
|
2007-06-08 16:20:49 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|
|
|
|
2007-06-28 23:45:29 +00:00
|
|
|
/* Called when a message's ref count reaches zero. Free any message-specific
|
|
|
|
* data here.
|
|
|
|
*/
|
|
|
|
typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
|
|
|
|
|
2007-06-28 23:49:55 +00:00
|
|
|
typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData);
|
|
|
|
|
2007-06-28 23:51:47 +00:00
|
|
|
typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal);
|
|
|
|
|
2007-07-12 22:05:02 +00:00
|
|
|
typedef enum _CryptMsgState {
|
|
|
|
MsgStateInit,
|
2007-07-12 22:05:45 +00:00
|
|
|
MsgStateUpdated,
|
2007-07-12 22:05:02 +00:00
|
|
|
MsgStateFinalized
|
|
|
|
} CryptMsgState;
|
|
|
|
|
2007-06-28 23:45:29 +00:00
|
|
|
typedef struct _CryptMsgBase
|
|
|
|
{
|
2007-06-28 23:49:55 +00:00
|
|
|
LONG ref;
|
|
|
|
DWORD open_flags;
|
2007-07-09 17:55:42 +00:00
|
|
|
BOOL streamed;
|
|
|
|
CMSG_STREAM_INFO stream_info;
|
2007-07-12 22:05:02 +00:00
|
|
|
CryptMsgState state;
|
2007-06-28 23:49:55 +00:00
|
|
|
CryptMsgCloseFunc close;
|
2007-06-28 23:51:47 +00:00
|
|
|
CryptMsgUpdateFunc update;
|
2007-06-28 23:49:55 +00:00
|
|
|
CryptMsgGetParamFunc get_param;
|
2007-06-28 23:45:29 +00:00
|
|
|
} CryptMsgBase;
|
|
|
|
|
2007-06-28 23:52:37 +00:00
|
|
|
static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
|
2007-07-09 17:50:48 +00:00
|
|
|
PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
|
|
|
|
CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
|
2007-06-28 23:45:29 +00:00
|
|
|
{
|
|
|
|
msg->ref = 1;
|
|
|
|
msg->open_flags = dwFlags;
|
2007-07-09 17:55:42 +00:00
|
|
|
if (pStreamInfo)
|
|
|
|
{
|
|
|
|
msg->streamed = TRUE;
|
|
|
|
memcpy(&msg->stream_info, pStreamInfo, sizeof(msg->stream_info));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->streamed = FALSE;
|
|
|
|
memset(&msg->stream_info, 0, sizeof(msg->stream_info));
|
|
|
|
}
|
2007-07-09 17:50:48 +00:00
|
|
|
msg->close = close;
|
|
|
|
msg->get_param = get_param;
|
|
|
|
msg->update = update;
|
2007-07-12 22:05:02 +00:00
|
|
|
msg->state = MsgStateInit;
|
2007-06-28 23:45:29 +00:00
|
|
|
}
|
|
|
|
|
2007-06-28 23:49:06 +00:00
|
|
|
typedef struct _CDataEncodeMsg
|
|
|
|
{
|
|
|
|
CryptMsgBase base;
|
2007-06-29 00:14:02 +00:00
|
|
|
DWORD bare_content_len;
|
|
|
|
LPBYTE bare_content;
|
2007-06-28 23:49:06 +00:00
|
|
|
} CDataEncodeMsg;
|
|
|
|
|
2007-06-29 00:14:02 +00:00
|
|
|
static const BYTE empty_data_content[] = { 0x04,0x00 };
|
|
|
|
|
|
|
|
static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
|
|
|
CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
|
|
|
|
|
|
|
|
if (msg->bare_content != empty_data_content)
|
|
|
|
LocalFree(msg->bare_content);
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:20:20 +00:00
|
|
|
static WINAPI BOOL CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
const CDataEncodeMsg *msg = (const CDataEncodeMsg *)pvStructInfo;
|
|
|
|
DWORD lenBytes;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
/* Trick: report bytes needed based on total message length, even though
|
|
|
|
* the message isn't available yet. The caller will use the length
|
|
|
|
* reported here to encode its length.
|
|
|
|
*/
|
|
|
|
CRYPT_EncodeLen(msg->base.stream_info.cbContent, NULL, &lenBytes);
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = 1 + lenBytes + msg->base.stream_info.cbContent;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, 1 + lenBytes)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_OCTETSTRING;
|
|
|
|
CRYPT_EncodeLen(msg->base.stream_info.cbContent, pbEncoded,
|
|
|
|
&lenBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
|
|
|
|
CRYPT_DATA_BLOB *header)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
|
|
|
|
{
|
|
|
|
FIXME("unimplemented for indefinite-length encoding\n");
|
|
|
|
header->cbData = 0;
|
|
|
|
header->pbData = NULL;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnConstructedItem constructed = { 0, msg,
|
|
|
|
CRYPT_EncodeContentLength };
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &constructed, CRYPT_AsnEncodeConstructed, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
|
|
|
|
sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
|
|
|
(LPBYTE)&header->pbData, &header->cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* Trick: subtract the content length from the reported length,
|
|
|
|
* as the actual content hasn't come yet.
|
|
|
|
*/
|
|
|
|
header->cbData -= msg->base.stream_info.cbContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-29 00:14:02 +00:00
|
|
|
static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
|
|
|
CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2007-07-12 22:05:02 +00:00
|
|
|
if (msg->base.streamed)
|
2007-06-29 00:14:02 +00:00
|
|
|
{
|
2007-07-12 21:20:20 +00:00
|
|
|
__TRY
|
|
|
|
{
|
2007-07-12 22:05:45 +00:00
|
|
|
if (msg->base.state != MsgStateUpdated)
|
2007-07-12 21:20:20 +00:00
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB header;
|
|
|
|
|
|
|
|
ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = msg->base.stream_info.pfnStreamOutput(
|
|
|
|
msg->base.stream_info.pvArg, header.pbData, header.cbData,
|
|
|
|
FALSE);
|
|
|
|
LocalFree(header.pbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fFinal)
|
|
|
|
ret = msg->base.stream_info.pfnStreamOutput(
|
|
|
|
msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
|
|
|
|
FALSE);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (msg->base.stream_info.cbContent == 0xffffffff)
|
|
|
|
{
|
|
|
|
BYTE indefinite_trailer[6] = { 0 };
|
|
|
|
|
|
|
|
ret = msg->base.stream_info.pfnStreamOutput(
|
|
|
|
msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
|
|
|
|
FALSE);
|
|
|
|
if (ret)
|
|
|
|
ret = msg->base.stream_info.pfnStreamOutput(
|
|
|
|
msg->base.stream_info.pvArg, indefinite_trailer,
|
|
|
|
sizeof(indefinite_trailer), TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = msg->base.stream_info.pfnStreamOutput(
|
|
|
|
msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY;
|
2007-06-29 00:14:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-12 21:20:20 +00:00
|
|
|
if (!fFinal)
|
|
|
|
{
|
|
|
|
if (msg->base.open_flags & CMSG_DETACHED_FLAG)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
}
|
2007-06-29 00:14:02 +00:00
|
|
|
else
|
|
|
|
{
|
2007-07-12 21:20:20 +00:00
|
|
|
if (!cbData)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
|
2007-06-29 00:14:02 +00:00
|
|
|
|
2007-07-12 21:20:20 +00:00
|
|
|
/* non-streamed data messages don't allow non-final updates,
|
|
|
|
* don't bother checking whether data already exist, they can't.
|
|
|
|
*/
|
|
|
|
ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
|
|
|
|
&blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
|
|
|
|
&msg->bare_content_len);
|
|
|
|
}
|
2007-06-29 00:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:54:56 +00:00
|
|
|
static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const BYTE *src,
|
|
|
|
DWORD len)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (!pvData)
|
|
|
|
*pcbData = len;
|
|
|
|
else if (*pcbData < len)
|
|
|
|
{
|
|
|
|
*pcbData = len;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbData = len;
|
|
|
|
memcpy(pvData, src, len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-29 00:16:15 +00:00
|
|
|
static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-06-29 00:19:04 +00:00
|
|
|
CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
|
2007-06-29 00:16:15 +00:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
switch (dwParamType)
|
|
|
|
{
|
|
|
|
case CMSG_CONTENT_PARAM:
|
2007-07-09 18:19:44 +00:00
|
|
|
if (msg->base.streamed)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_CONTENT_INFO info;
|
|
|
|
char rsa_data[] = "1.2.840.113549.1.7.1";
|
|
|
|
|
|
|
|
info.pszObjId = rsa_data;
|
|
|
|
info.Content.cbData = msg->bare_content_len;
|
|
|
|
info.Content.pbData = msg->bare_content;
|
|
|
|
ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
|
|
|
|
pvData, pcbData);
|
|
|
|
}
|
2007-06-29 00:16:15 +00:00
|
|
|
break;
|
2007-06-29 00:19:04 +00:00
|
|
|
case CMSG_BARE_CONTENT_PARAM:
|
2007-07-09 18:19:44 +00:00
|
|
|
if (msg->base.streamed)
|
|
|
|
SetLastError(E_INVALIDARG);
|
2007-06-29 00:19:04 +00:00
|
|
|
else
|
2007-07-12 21:54:56 +00:00
|
|
|
ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
|
|
|
|
msg->bare_content_len);
|
2007-06-29 00:19:04 +00:00
|
|
|
break;
|
2007-06-29 00:16:15 +00:00
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-28 23:49:06 +00:00
|
|
|
static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
|
|
|
|
LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
|
|
|
CDataEncodeMsg *msg;
|
|
|
|
|
|
|
|
if (pvMsgEncodeInfo)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
2007-07-09 17:50:48 +00:00
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update);
|
2007-06-29 00:14:02 +00:00
|
|
|
msg->bare_content_len = sizeof(empty_data_content);
|
|
|
|
msg->bare_content = (LPBYTE)empty_data_content;
|
2007-06-28 23:49:06 +00:00
|
|
|
}
|
|
|
|
return (HCRYPTMSG)msg;
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:32:00 +00:00
|
|
|
typedef struct _CHashEncodeMsg
|
|
|
|
{
|
2007-07-12 21:52:41 +00:00
|
|
|
CryptMsgBase base;
|
|
|
|
HCRYPTPROV prov;
|
|
|
|
HCRYPTHASH hash;
|
|
|
|
CRYPT_DATA_BLOB data;
|
2007-07-12 21:32:00 +00:00
|
|
|
} CHashEncodeMsg;
|
|
|
|
|
|
|
|
static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
|
|
|
CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
|
|
|
|
|
2007-07-12 21:52:41 +00:00
|
|
|
CryptMemFree(msg->data.pbData);
|
2007-07-12 21:32:00 +00:00
|
|
|
CryptDestroyHash(msg->hash);
|
|
|
|
if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
|
|
|
|
CryptReleaseContext(msg->prov, 0);
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:59:27 +00:00
|
|
|
static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
|
|
|
|
DWORD *pcbData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
ALG_ID algID;
|
|
|
|
DWORD size = sizeof(algID);
|
|
|
|
|
|
|
|
ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
|
|
|
|
if (ret)
|
|
|
|
{
|
2007-07-17 14:22:50 +00:00
|
|
|
CRYPT_DIGESTED_DATA digestedData = { 0 };
|
2007-07-12 21:59:27 +00:00
|
|
|
char oid_rsa_data[] = szOID_RSA_data;
|
|
|
|
|
2007-07-17 14:22:50 +00:00
|
|
|
digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
|
|
|
|
digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
|
|
|
|
/* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
|
2007-07-12 21:59:27 +00:00
|
|
|
/* Quirk: OID is only encoded messages if an update has happened */
|
2007-07-12 22:05:45 +00:00
|
|
|
if (msg->base.state != MsgStateInit)
|
2007-07-17 14:22:50 +00:00
|
|
|
digestedData.ContentInfo.pszObjId = oid_rsa_data;
|
2007-07-12 21:59:27 +00:00
|
|
|
if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
|
|
|
|
CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
2007-07-17 14:22:50 +00:00
|
|
|
(LPBYTE)&digestedData.ContentInfo.Content.pbData,
|
|
|
|
&digestedData.ContentInfo.Content.cbData);
|
2007-07-12 21:59:27 +00:00
|
|
|
}
|
2007-07-12 22:05:02 +00:00
|
|
|
if (msg->base.state == MsgStateFinalized)
|
2007-07-12 21:59:27 +00:00
|
|
|
{
|
|
|
|
size = sizeof(DWORD);
|
|
|
|
ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
|
2007-07-17 14:22:50 +00:00
|
|
|
(LPBYTE)&digestedData.hash.cbData, &size, 0);
|
2007-07-12 21:59:27 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-07-17 14:22:50 +00:00
|
|
|
digestedData.hash.pbData = CryptMemAlloc(
|
|
|
|
digestedData.hash.cbData);
|
|
|
|
ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
|
|
|
|
digestedData.hash.pbData, &digestedData.hash.cbData, 0);
|
2007-07-12 21:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
2007-07-17 14:22:50 +00:00
|
|
|
ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
|
|
|
|
pcbData);
|
|
|
|
CryptMemFree(digestedData.hash.pbData);
|
|
|
|
LocalFree(digestedData.ContentInfo.Content.pbData);
|
2007-07-12 21:59:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:32:00 +00:00
|
|
|
static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-07-12 21:44:59 +00:00
|
|
|
CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
|
2007-07-12 21:32:00 +00:00
|
|
|
pvData, pcbData);
|
2007-07-12 21:44:59 +00:00
|
|
|
|
|
|
|
switch (dwParamType)
|
|
|
|
{
|
2007-07-12 21:59:27 +00:00
|
|
|
case CMSG_BARE_CONTENT_PARAM:
|
|
|
|
if (msg->base.streamed)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
|
|
|
|
break;
|
|
|
|
case CMSG_CONTENT_PARAM:
|
|
|
|
{
|
|
|
|
CRYPT_CONTENT_INFO info;
|
|
|
|
|
|
|
|
ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
|
|
|
|
&info.Content.cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
info.Content.pbData = CryptMemAlloc(info.Content.cbData);
|
|
|
|
if (info.Content.pbData)
|
|
|
|
{
|
|
|
|
ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
|
|
|
|
info.Content.pbData, &info.Content.cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
char oid_rsa_hashed[] = szOID_RSA_hashedData;
|
|
|
|
|
|
|
|
info.pszObjId = oid_rsa_hashed;
|
|
|
|
ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
|
|
|
|
PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
|
|
|
|
}
|
|
|
|
CryptMemFree(info.Content.pbData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-07-12 21:44:59 +00:00
|
|
|
case CMSG_COMPUTED_HASH_PARAM:
|
|
|
|
ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
|
|
|
|
0);
|
|
|
|
break;
|
2007-07-12 21:51:03 +00:00
|
|
|
case CMSG_VERSION_PARAM:
|
2007-07-12 22:05:02 +00:00
|
|
|
if (msg->base.state != MsgStateFinalized)
|
2007-07-12 21:51:03 +00:00
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
else
|
|
|
|
{
|
2007-07-12 21:54:56 +00:00
|
|
|
DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
|
|
|
|
|
2007-07-12 22:06:21 +00:00
|
|
|
/* Since the data are always encoded as octets, the version is
|
|
|
|
* always 0 (see rfc3852, section 7)
|
|
|
|
*/
|
2007-07-12 21:54:56 +00:00
|
|
|
ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&version,
|
|
|
|
sizeof(version));
|
2007-07-12 21:51:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-07-12 21:44:59 +00:00
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-12 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
2007-07-12 21:49:23 +00:00
|
|
|
CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
|
|
|
|
|
2007-07-12 22:05:02 +00:00
|
|
|
if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
|
|
|
|
{
|
|
|
|
/* Doesn't do much, as stream output is never called, and you
|
|
|
|
* can't get the content.
|
|
|
|
*/
|
|
|
|
ret = CryptHashData(msg->hash, pbData, cbData, 0);
|
|
|
|
}
|
2007-07-12 21:49:23 +00:00
|
|
|
else
|
|
|
|
{
|
2007-07-12 22:05:02 +00:00
|
|
|
if (!fFinal)
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
2007-07-12 21:49:23 +00:00
|
|
|
else
|
|
|
|
{
|
2007-07-12 22:05:02 +00:00
|
|
|
ret = CryptHashData(msg->hash, pbData, cbData, 0);
|
|
|
|
if (ret)
|
2007-07-12 21:49:23 +00:00
|
|
|
{
|
2007-07-12 22:05:02 +00:00
|
|
|
msg->data.pbData = CryptMemAlloc(cbData);
|
|
|
|
if (msg->data.pbData)
|
2007-07-12 21:52:41 +00:00
|
|
|
{
|
2007-07-12 22:05:02 +00:00
|
|
|
memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
|
|
|
|
msg->data.cbData += cbData;
|
2007-07-12 21:52:41 +00:00
|
|
|
}
|
2007-07-12 22:05:02 +00:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2007-07-12 21:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-12 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
|
|
|
|
LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
|
|
|
CHashEncodeMsg *msg;
|
|
|
|
const CMSG_HASHED_ENCODE_INFO *info =
|
|
|
|
(const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
|
|
|
|
HCRYPTPROV prov;
|
|
|
|
ALG_ID algID;
|
|
|
|
|
|
|
|
if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_UNKNOWN_ALGO);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (info->hCryptProv)
|
|
|
|
prov = info->hCryptProv;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prov = CRYPT_GetDefaultProvider();
|
|
|
|
dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
|
|
|
|
}
|
|
|
|
msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update);
|
|
|
|
msg->prov = prov;
|
2007-07-12 21:52:41 +00:00
|
|
|
msg->data.cbData = 0;
|
|
|
|
msg->data.pbData = NULL;
|
2007-07-12 21:32:00 +00:00
|
|
|
if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
|
|
|
|
{
|
|
|
|
CryptMsgClose(msg);
|
|
|
|
msg = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (HCRYPTMSG)msg;
|
|
|
|
}
|
|
|
|
|
2007-07-21 00:31:11 +00:00
|
|
|
typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
|
|
|
|
{
|
|
|
|
DWORD cbSize;
|
|
|
|
PCERT_INFO pCertInfo;
|
|
|
|
HCRYPTPROV hCryptProv;
|
|
|
|
DWORD dwKeySpec;
|
|
|
|
CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
|
|
|
|
void *pvHashAuxInfo;
|
|
|
|
DWORD cAuthAttr;
|
|
|
|
PCRYPT_ATTRIBUTE rgAuthAttr;
|
|
|
|
DWORD cUnauthAttr;
|
|
|
|
PCRYPT_ATTRIBUTE rgUnauthAttr;
|
|
|
|
CERT_ID SignerId;
|
|
|
|
CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
|
|
|
|
void *pvHashEncryptionAuxInfo;
|
|
|
|
} CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
|
|
|
|
|
|
|
|
typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
|
|
|
|
{
|
|
|
|
DWORD cbSize;
|
|
|
|
DWORD cSigners;
|
|
|
|
PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
|
|
|
|
DWORD cCertEncoded;
|
|
|
|
PCERT_BLOB rgCertEncoded;
|
|
|
|
DWORD cCrlEncoded;
|
|
|
|
PCRL_BLOB rgCrlEncoded;
|
|
|
|
DWORD cAttrCertEncoded;
|
|
|
|
PCERT_BLOB rgAttrCertEncoded;
|
|
|
|
} CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
|
|
|
|
|
|
|
|
static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
|
|
|
|
{
|
2007-07-24 00:35:17 +00:00
|
|
|
if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
|
|
|
|
signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
|
|
|
|
{
|
|
|
|
FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-07-21 00:31:11 +00:00
|
|
|
if (!signer->pCertInfo->SerialNumber.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!signer->pCertInfo->Issuer.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!signer->hCryptProv)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_UNKNOWN_ALGO);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-07-24 00:56:12 +00:00
|
|
|
typedef struct _CSignerInfo
|
|
|
|
{
|
|
|
|
HCRYPTPROV prov;
|
|
|
|
HCRYPTHASH hash;
|
|
|
|
HCRYPTKEY key;
|
|
|
|
CMSG_SIGNER_INFO info;
|
|
|
|
} CSignerInfo;
|
|
|
|
|
|
|
|
static BOOL CRYPT_CopyBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
out->cbData = in->cbData;
|
|
|
|
if (out->cbData)
|
|
|
|
{
|
|
|
|
out->pbData = CryptMemAlloc(out->cbData);
|
|
|
|
if (out->pbData)
|
|
|
|
memcpy(out->pbData, in->pbData, out->cbData);
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out->pbData = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_CopyAttribute(CRYPT_ATTRIBUTE *out, const CRYPT_ATTRIBUTE *in)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
/* Assumption: algorithm IDs will point to static strings, not stack-based
|
|
|
|
* ones, so copying the pointer values is safe.
|
|
|
|
*/
|
|
|
|
out->pszObjId = in->pszObjId;
|
|
|
|
out->cValue = in->cValue;
|
|
|
|
if (out->cValue)
|
|
|
|
{
|
|
|
|
out->rgValue = CryptMemAlloc(out->cValue * sizeof(CRYPT_DATA_BLOB));
|
|
|
|
if (out->rgValue)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
memset(out->rgValue, 0, out->cValue * sizeof(CRYPT_DATA_BLOB));
|
|
|
|
for (i = 0; ret && i < out->cValue; i++)
|
|
|
|
ret = CRYPT_CopyBlob(&out->rgValue[i], &in->rgValue[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
|
|
|
|
const CRYPT_ATTRIBUTES *in)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
out->cAttr = in->cAttr;
|
|
|
|
if (out->cAttr)
|
|
|
|
{
|
|
|
|
out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
|
|
|
|
if (out->rgAttr)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
|
|
|
|
for (i = 0; ret && i < out->cAttr; i++)
|
|
|
|
ret = CRYPT_CopyAttribute(&out->rgAttr[i], &in->rgAttr[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out->rgAttr = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CSignerInfo_Construct(CSignerInfo *out,
|
|
|
|
CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in, DWORD open_flags)
|
|
|
|
{
|
|
|
|
ALG_ID algID;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
out->prov = in->hCryptProv;
|
|
|
|
if (!(open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
|
|
|
|
CryptContextAddRef(out->prov, NULL, 0);
|
|
|
|
algID = CertOIDToAlgId(in->HashAlgorithm.pszObjId);
|
|
|
|
ret = CryptCreateHash(out->prov, algID, 0, 0, &out->hash);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* Note: needs to change if CMS fields are supported */
|
|
|
|
out->info.dwVersion = CMSG_SIGNER_INFO_V1;
|
|
|
|
ret = CRYPT_CopyBlob(&out->info.Issuer, &in->pCertInfo->Issuer);
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_CopyBlob(&out->info.SerialNumber,
|
|
|
|
&in->pCertInfo->SerialNumber);
|
|
|
|
/* Assumption: algorithm IDs will point to static strings, not
|
|
|
|
* stack-based ones, so copying the pointer values is safe.
|
|
|
|
*/
|
|
|
|
out->info.HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_CopyBlob(&out->info.HashAlgorithm.Parameters,
|
|
|
|
&in->HashAlgorithm.Parameters);
|
|
|
|
memset(&out->info.HashEncryptionAlgorithm, 0,
|
|
|
|
sizeof(out->info.HashEncryptionAlgorithm));
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_CopyAttributes(&out->info.AuthAttrs,
|
|
|
|
(CRYPT_ATTRIBUTES *)&in->cAuthAttr);
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_CopyAttributes(&out->info.UnauthAttrs,
|
|
|
|
(CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CSignerInfo_Free(CSignerInfo *signer)
|
|
|
|
{
|
|
|
|
DWORD i, j;
|
|
|
|
|
|
|
|
CryptDestroyKey(signer->key);
|
|
|
|
CryptDestroyHash(signer->hash);
|
|
|
|
CryptReleaseContext(signer->prov, 0);
|
|
|
|
CryptMemFree(signer->info.Issuer.pbData);
|
|
|
|
CryptMemFree(signer->info.SerialNumber.pbData);
|
|
|
|
CryptMemFree(signer->info.HashAlgorithm.Parameters.pbData);
|
|
|
|
CryptMemFree(signer->info.EncryptedHash.pbData);
|
|
|
|
for (i = 0; i < signer->info.AuthAttrs.cAttr; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < signer->info.AuthAttrs.rgAttr[i].cValue; j++)
|
|
|
|
CryptMemFree(signer->info.AuthAttrs.rgAttr[i].rgValue[j].pbData);
|
|
|
|
CryptMemFree(signer->info.AuthAttrs.rgAttr[i].rgValue);
|
|
|
|
}
|
|
|
|
CryptMemFree(signer->info.AuthAttrs.rgAttr);
|
|
|
|
for (i = 0; i < signer->info.UnauthAttrs.cAttr; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < signer->info.UnauthAttrs.rgAttr[i].cValue; j++)
|
|
|
|
CryptMemFree(signer->info.UnauthAttrs.rgAttr[i].rgValue[j].pbData);
|
|
|
|
CryptMemFree(signer->info.UnauthAttrs.rgAttr[i].rgValue);
|
|
|
|
}
|
|
|
|
CryptMemFree(signer->info.UnauthAttrs.rgAttr);
|
|
|
|
}
|
|
|
|
|
2007-07-21 00:31:11 +00:00
|
|
|
typedef struct _CSignedEncodeMsg
|
|
|
|
{
|
2007-07-24 00:33:09 +00:00
|
|
|
CryptMsgBase base;
|
|
|
|
CRYPT_DATA_BLOB data;
|
2007-07-24 00:56:12 +00:00
|
|
|
DWORD cSigners;
|
|
|
|
CSignerInfo *signers;
|
2007-07-21 00:31:11 +00:00
|
|
|
} CSignedEncodeMsg;
|
|
|
|
|
|
|
|
static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
2007-07-24 00:33:09 +00:00
|
|
|
CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
|
2007-07-24 00:56:12 +00:00
|
|
|
DWORD i;
|
2007-07-24 00:33:09 +00:00
|
|
|
|
|
|
|
CryptMemFree(msg->data.pbData);
|
2007-07-24 00:56:12 +00:00
|
|
|
for (i = 0; i < msg->cSigners; i++)
|
|
|
|
CSignerInfo_Free(&msg->signers[i]);
|
|
|
|
CryptMemFree(msg->signers);
|
2007-07-21 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
|
|
|
FIXME("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex, pvData,
|
|
|
|
pcbData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-07-24 00:56:12 +00:00
|
|
|
static BOOL CSignedEncodeMsg_UpdateHash(CSignedEncodeMsg *msg,
|
|
|
|
const BYTE *pbData, DWORD cbData)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %d)\n", msg, pbData, cbData);
|
|
|
|
|
|
|
|
for (i = 0; ret && i < msg->cSigners; i++)
|
|
|
|
ret = CryptHashData(msg->signers[i].hash, pbData, cbData, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BYTE tmp;
|
|
|
|
|
|
|
|
for (i = 0; i < hash->cbData / 2; i++)
|
|
|
|
{
|
|
|
|
tmp = hash->pbData[hash->cbData - i - 1];
|
|
|
|
hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
|
|
|
|
hash->pbData[i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CSignedEncodeMsg_Sign(CSignedEncodeMsg *msg)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", msg);
|
|
|
|
|
|
|
|
for (i = 0; ret && i < msg->cSigners; i++)
|
|
|
|
{
|
|
|
|
ret = CryptSignHashW(msg->signers[i].hash, AT_SIGNATURE, NULL, 0, NULL,
|
|
|
|
&msg->signers[i].info.EncryptedHash.cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
msg->signers[i].info.EncryptedHash.pbData =
|
|
|
|
CryptMemAlloc(msg->signers[i].info.EncryptedHash.cbData);
|
|
|
|
if (msg->signers[i].info.EncryptedHash.pbData)
|
|
|
|
{
|
|
|
|
ret = CryptSignHashW(msg->signers[i].hash, AT_SIGNATURE, NULL,
|
|
|
|
0, msg->signers[i].info.EncryptedHash.pbData,
|
|
|
|
&msg->signers[i].info.EncryptedHash.cbData);
|
|
|
|
if (ret)
|
|
|
|
CRYPT_ReverseBytes(&msg->signers[i].info.EncryptedHash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-21 00:31:11 +00:00
|
|
|
static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
2007-07-24 00:33:09 +00:00
|
|
|
CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
|
|
|
|
{
|
2007-07-24 00:56:12 +00:00
|
|
|
ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
|
|
|
|
/* FIXME: hash authenticated attributes on final update */
|
|
|
|
if (ret && fFinal)
|
|
|
|
ret = CSignedEncodeMsg_Sign(msg);
|
|
|
|
if (msg->base.streamed)
|
|
|
|
FIXME("streamed partial stub\n");
|
2007-07-24 00:33:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!fFinal)
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cbData)
|
|
|
|
{
|
|
|
|
msg->data.pbData = CryptMemAlloc(cbData);
|
|
|
|
if (msg->data.pbData)
|
|
|
|
{
|
|
|
|
memcpy(msg->data.pbData, pbData, cbData);
|
|
|
|
msg->data.cbData = cbData;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
if (ret)
|
2007-07-24 00:56:12 +00:00
|
|
|
ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
|
|
|
|
/* FIXME: hash authenticated attributes */
|
|
|
|
if (ret)
|
|
|
|
ret = CSignedEncodeMsg_Sign(msg);
|
2007-07-24 00:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-21 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
|
|
|
|
const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
|
|
|
const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
|
|
|
|
(const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
|
|
|
|
DWORD i;
|
|
|
|
CSignedEncodeMsg *msg;
|
|
|
|
|
|
|
|
if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
|
|
|
|
info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
|
|
|
|
{
|
|
|
|
FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < info->cSigners; i++)
|
|
|
|
if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
|
|
|
|
return NULL;
|
|
|
|
msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
2007-07-24 00:56:12 +00:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2007-07-21 00:31:11 +00:00
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
|
|
|
|
CSignedEncodeMsg_Update);
|
2007-07-24 00:33:09 +00:00
|
|
|
msg->data.cbData = 0;
|
|
|
|
msg->data.pbData = NULL;
|
2007-07-24 00:56:12 +00:00
|
|
|
msg->cSigners = 0;
|
|
|
|
if (info->cSigners)
|
|
|
|
{
|
|
|
|
msg->signers = CryptMemAlloc(info->cSigners * sizeof(CSignerInfo));
|
|
|
|
if (msg->signers)
|
|
|
|
{
|
|
|
|
msg->cSigners = info->cSigners;
|
|
|
|
memset(msg->signers, 0, msg->cSigners * sizeof(CSignerInfo));
|
|
|
|
for (i = 0; ret && i < msg->cSigners; i++)
|
|
|
|
ret = CSignerInfo_Construct(&msg->signers[i],
|
|
|
|
&info->rgSigners[i], dwFlags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
CSignedEncodeMsg_Close(msg);
|
|
|
|
msg = NULL;
|
|
|
|
}
|
2007-07-21 00:31:11 +00:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2007-06-28 23:44:33 +00:00
|
|
|
static inline const char *MSG_TYPE_STR(DWORD type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
#define _x(x) case (x): return #x
|
|
|
|
_x(CMSG_DATA);
|
|
|
|
_x(CMSG_SIGNED);
|
|
|
|
_x(CMSG_ENVELOPED);
|
|
|
|
_x(CMSG_SIGNED_AND_ENVELOPED);
|
|
|
|
_x(CMSG_HASHED);
|
|
|
|
_x(CMSG_ENCRYPTED);
|
|
|
|
#undef _x
|
|
|
|
default:
|
|
|
|
return wine_dbg_sprintf("unknown (%d)", type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 16:20:49 +00:00
|
|
|
HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
|
|
|
|
DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
2007-06-28 23:44:33 +00:00
|
|
|
HCRYPTMSG msg = NULL;
|
|
|
|
|
|
|
|
TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
|
2007-06-08 16:20:49 +00:00
|
|
|
dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
|
2007-06-28 23:44:33 +00:00
|
|
|
|
|
|
|
if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
switch (dwMsgType)
|
|
|
|
{
|
|
|
|
case CMSG_DATA:
|
2007-06-28 23:49:06 +00:00
|
|
|
msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
|
|
|
|
pszInnerContentObjID, pStreamInfo);
|
|
|
|
break;
|
2007-07-12 21:32:00 +00:00
|
|
|
case CMSG_HASHED:
|
|
|
|
msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
|
|
|
|
pszInnerContentObjID, pStreamInfo);
|
|
|
|
break;
|
2007-06-28 23:44:33 +00:00
|
|
|
case CMSG_SIGNED:
|
2007-07-21 00:31:11 +00:00
|
|
|
msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
|
|
|
|
pszInnerContentObjID, pStreamInfo);
|
|
|
|
break;
|
2007-06-28 23:44:33 +00:00
|
|
|
case CMSG_ENVELOPED:
|
|
|
|
FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
|
|
|
|
break;
|
|
|
|
case CMSG_SIGNED_AND_ENVELOPED:
|
|
|
|
case CMSG_ENCRYPTED:
|
|
|
|
/* defined but invalid, fall through */
|
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
}
|
|
|
|
return msg;
|
2007-06-08 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 18:20:34 +00:00
|
|
|
typedef struct _CDecodeMsg
|
|
|
|
{
|
2007-07-17 14:36:24 +00:00
|
|
|
CryptMsgBase base;
|
|
|
|
DWORD type;
|
|
|
|
HCRYPTPROV crypt_prov;
|
2007-07-19 14:32:10 +00:00
|
|
|
HCRYPTHASH hash;
|
2007-07-17 14:36:24 +00:00
|
|
|
CRYPT_DATA_BLOB msg_data;
|
|
|
|
PCONTEXT_PROPERTY_LIST properties;
|
2007-07-09 18:20:34 +00:00
|
|
|
} CDecodeMsg;
|
|
|
|
|
|
|
|
static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
|
|
|
CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
|
|
|
|
|
|
|
|
if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
|
|
|
|
CryptReleaseContext(msg->crypt_prov, 0);
|
2007-07-19 14:32:10 +00:00
|
|
|
CryptDestroyHash(msg->hash);
|
2007-07-13 18:45:03 +00:00
|
|
|
CryptMemFree(msg->msg_data.pbData);
|
2007-07-17 14:36:24 +00:00
|
|
|
ContextPropertyList_Free(msg->properties);
|
2007-07-13 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
|
|
|
|
DWORD cbData)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (cbData)
|
|
|
|
{
|
|
|
|
if (msg->msg_data.cbData)
|
|
|
|
msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
|
|
|
|
msg->msg_data.cbData + cbData);
|
|
|
|
else
|
|
|
|
msg->msg_data.pbData = CryptMemAlloc(cbData);
|
|
|
|
if (msg->msg_data.pbData)
|
|
|
|
{
|
|
|
|
memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
|
|
|
|
msg->msg_data.cbData += cbData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-19 14:30:44 +00:00
|
|
|
static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CRYPT_DATA_BLOB *data;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
|
|
|
|
blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
|
|
|
|
&size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = ContextPropertyList_SetProperty(msg->properties,
|
|
|
|
CMSG_CONTENT_PARAM, data->pbData, data->cbData);
|
|
|
|
LocalFree(data);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-19 14:31:34 +00:00
|
|
|
static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
|
|
|
|
const CRYPT_ALGORITHM_IDENTIFIER *id)
|
|
|
|
{
|
|
|
|
static const BYTE nullParams[] = { ASN_NULL, 0 };
|
|
|
|
CRYPT_ALGORITHM_IDENTIFIER *copy;
|
|
|
|
DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
|
|
|
|
|
|
|
|
/* Linearize algorithm id */
|
|
|
|
len += strlen(id->pszObjId) + 1;
|
|
|
|
len += id->Parameters.cbData;
|
|
|
|
copy = CryptMemAlloc(len);
|
|
|
|
if (copy)
|
|
|
|
{
|
|
|
|
copy->pszObjId =
|
|
|
|
(LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
|
|
|
|
strcpy(copy->pszObjId, id->pszObjId);
|
|
|
|
copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
|
|
|
|
+ 1;
|
|
|
|
/* Trick: omit NULL parameters */
|
|
|
|
if (id->Parameters.cbData == sizeof(nullParams) &&
|
|
|
|
!memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
|
|
|
|
{
|
|
|
|
copy->Parameters.cbData = 0;
|
|
|
|
len -= sizeof(nullParams);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
copy->Parameters.cbData = id->Parameters.cbData;
|
|
|
|
if (copy->Parameters.cbData)
|
|
|
|
memcpy(copy->Parameters.pbData, id->Parameters.pbData,
|
|
|
|
id->Parameters.cbData);
|
|
|
|
ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
|
|
|
|
len);
|
|
|
|
CryptMemFree(copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
|
|
|
|
{
|
|
|
|
id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
|
|
|
|
id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
|
|
|
|
}
|
|
|
|
|
2007-07-20 21:43:12 +00:00
|
|
|
static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
|
|
|
|
CRYPT_DER_BLOB *blob)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CRYPT_DIGESTED_DATA *digestedData;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
|
|
|
|
&size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
msg->type = CMSG_HASHED;
|
|
|
|
ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
|
|
|
|
(const BYTE *)&digestedData->version, sizeof(digestedData->version));
|
|
|
|
CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
|
|
|
|
&digestedData->DigestAlgorithm);
|
|
|
|
ContextPropertyList_SetProperty(msg->properties,
|
|
|
|
CMSG_INNER_CONTENT_TYPE_PARAM,
|
|
|
|
(const BYTE *)digestedData->ContentInfo.pszObjId,
|
|
|
|
digestedData->ContentInfo.pszObjId ?
|
|
|
|
strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
|
|
|
|
if (digestedData->ContentInfo.Content.cbData)
|
|
|
|
CDecodeMsg_DecodeDataContent(msg,
|
|
|
|
&digestedData->ContentInfo.Content);
|
|
|
|
else
|
|
|
|
ContextPropertyList_SetProperty(msg->properties,
|
|
|
|
CMSG_CONTENT_PARAM, NULL, 0);
|
|
|
|
ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
|
|
|
|
digestedData->hash.pbData, digestedData->hash.cbData);
|
|
|
|
LocalFree(digestedData);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-13 18:45:03 +00:00
|
|
|
/* Decodes the content in blob as the type given, and updates the value
|
|
|
|
* (type, parameters, etc.) of msg based on what blob contains.
|
|
|
|
* It doesn't just use msg's type, to allow a recursive call from an implicitly
|
|
|
|
* typed message once the outer content info has been decoded.
|
|
|
|
*/
|
|
|
|
static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
|
|
|
|
DWORD type)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CMSG_DATA:
|
2007-07-19 14:30:44 +00:00
|
|
|
if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
|
2007-07-13 18:47:46 +00:00
|
|
|
msg->type = CMSG_DATA;
|
|
|
|
break;
|
2007-07-13 18:45:03 +00:00
|
|
|
case CMSG_HASHED:
|
2007-07-20 21:43:12 +00:00
|
|
|
if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
|
2007-07-19 14:30:44 +00:00
|
|
|
msg->type = CMSG_HASHED;
|
2007-07-17 14:25:31 +00:00
|
|
|
break;
|
2007-07-13 18:45:03 +00:00
|
|
|
case CMSG_ENVELOPED:
|
|
|
|
case CMSG_SIGNED:
|
|
|
|
FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
CRYPT_CONTENT_INFO *info;
|
2007-07-20 21:43:12 +00:00
|
|
|
DWORD size;
|
2007-07-13 18:45:03 +00:00
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
|
|
|
|
msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
|
|
|
|
NULL, (LPBYTE)&info, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!strcmp(info->pszObjId, szOID_RSA_data))
|
|
|
|
ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
|
|
|
|
else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
|
|
|
|
ret = CDecodeMsg_DecodeContent(msg, &info->Content,
|
|
|
|
CMSG_HASHED);
|
|
|
|
else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
|
|
|
|
ret = CDecodeMsg_DecodeContent(msg, &info->Content,
|
|
|
|
CMSG_ENVELOPED);
|
|
|
|
else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
|
|
|
|
ret = CDecodeMsg_DecodeContent(msg, &info->Content,
|
|
|
|
CMSG_SIGNED);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-09 18:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
2007-07-13 18:45:03 +00:00
|
|
|
CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
|
|
|
|
|
|
|
|
if (msg->base.streamed)
|
|
|
|
{
|
|
|
|
ret = CDecodeMsg_CopyData(msg, pbData, cbData);
|
|
|
|
FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
|
|
|
|
cbData, fFinal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!fFinal)
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = CDecodeMsg_CopyData(msg, pbData, cbData);
|
|
|
|
if (ret)
|
|
|
|
ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-09 18:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-07-09 18:22:08 +00:00
|
|
|
CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
switch (dwParamType)
|
|
|
|
{
|
|
|
|
case CMSG_TYPE_PARAM:
|
2007-07-12 21:54:56 +00:00
|
|
|
ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&msg->type,
|
|
|
|
sizeof(msg->type));
|
2007-07-09 18:22:08 +00:00
|
|
|
break;
|
2007-07-19 14:31:34 +00:00
|
|
|
case CMSG_HASH_ALGORITHM_PARAM:
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob;
|
|
|
|
|
|
|
|
ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
|
|
|
|
&blob);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
|
|
|
|
if (ret && pvData)
|
|
|
|
CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-19 14:32:10 +00:00
|
|
|
case CMSG_COMPUTED_HASH_PARAM:
|
|
|
|
if (!msg->hash)
|
|
|
|
{
|
|
|
|
CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
|
|
|
|
DWORD size = 0;
|
|
|
|
ALG_ID algID = 0;
|
|
|
|
|
|
|
|
CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
|
|
|
|
hashAlgoID = CryptMemAlloc(size);
|
|
|
|
ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
|
|
|
|
hashAlgoID, &size);
|
|
|
|
if (ret)
|
|
|
|
algID = CertOIDToAlgId(hashAlgoID->pszObjId);
|
|
|
|
ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->hash);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB content;
|
|
|
|
|
|
|
|
ret = ContextPropertyList_FindProperty(msg->properties,
|
|
|
|
CMSG_CONTENT_PARAM, &content);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptHashData(msg->hash, content.pbData,
|
|
|
|
content.cbData, 0);
|
|
|
|
}
|
|
|
|
CryptMemFree(hashAlgoID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
if (ret)
|
|
|
|
ret = CryptGetHashParam(msg->hash, HP_HASHVAL, pvData, pcbData, 0);
|
|
|
|
break;
|
2007-07-17 14:36:24 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob;
|
|
|
|
|
|
|
|
ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
|
|
|
|
&blob);
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
|
2007-07-17 14:33:01 +00:00
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
2007-07-17 14:36:24 +00:00
|
|
|
}
|
2007-07-09 18:22:08 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2007-07-09 18:20:34 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 16:20:49 +00:00
|
|
|
HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
|
|
|
|
DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
2007-07-09 18:20:34 +00:00
|
|
|
CDecodeMsg *msg;
|
|
|
|
|
|
|
|
TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
|
2007-06-08 16:20:49 +00:00
|
|
|
dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
|
2007-06-28 23:44:33 +00:00
|
|
|
|
|
|
|
if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-07-09 18:20:34 +00:00
|
|
|
msg = CryptMemAlloc(sizeof(CDecodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
|
|
|
|
msg->type = dwMsgType;
|
2007-07-13 17:58:10 +00:00
|
|
|
if (hCryptProv)
|
|
|
|
msg->crypt_prov = hCryptProv;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->crypt_prov = CRYPT_GetDefaultProvider();
|
|
|
|
msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
|
|
|
|
}
|
2007-07-19 14:32:10 +00:00
|
|
|
msg->hash = 0;
|
2007-07-13 18:45:03 +00:00
|
|
|
msg->msg_data.cbData = 0;
|
|
|
|
msg->msg_data.pbData = NULL;
|
2007-07-17 14:36:24 +00:00
|
|
|
msg->properties = ContextPropertyList_Create();
|
2007-07-09 18:20:34 +00:00
|
|
|
}
|
|
|
|
return msg;
|
2007-06-08 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
2007-06-28 23:45:29 +00:00
|
|
|
TRACE("(%p)\n", hCryptMsg);
|
|
|
|
|
|
|
|
if (hCryptMsg)
|
|
|
|
{
|
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
|
|
|
|
InterlockedIncrement(&msg->ref);
|
|
|
|
}
|
2007-06-08 16:20:49 +00:00
|
|
|
return hCryptMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
2007-06-28 23:45:29 +00:00
|
|
|
TRACE("(%p)\n", hCryptMsg);
|
|
|
|
|
|
|
|
if (hCryptMsg)
|
|
|
|
{
|
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
|
|
|
|
if (InterlockedDecrement(&msg->ref) == 0)
|
|
|
|
{
|
|
|
|
TRACE("freeing %p\n", msg);
|
|
|
|
if (msg->close)
|
|
|
|
msg->close(msg);
|
|
|
|
CryptMemFree(msg);
|
|
|
|
}
|
|
|
|
}
|
2007-06-08 16:20:49 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
2007-06-28 23:51:47 +00:00
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
2007-07-12 22:05:02 +00:00
|
|
|
BOOL ret = FALSE;
|
2007-06-28 23:51:47 +00:00
|
|
|
|
|
|
|
TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
|
2007-07-12 22:05:02 +00:00
|
|
|
|
|
|
|
if (msg->state == MsgStateFinalized)
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
|
2007-07-12 22:05:45 +00:00
|
|
|
msg->state = MsgStateUpdated;
|
2007-07-12 22:05:02 +00:00
|
|
|
if (fFinal)
|
|
|
|
msg->state = MsgStateFinalized;
|
|
|
|
}
|
|
|
|
return ret;
|
2007-06-08 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-06-28 23:49:55 +00:00
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
|
2007-06-08 16:20:49 +00:00
|
|
|
pvData, pcbData);
|
2007-07-12 21:55:44 +00:00
|
|
|
return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
|
2007-06-08 16:20:49 +00:00
|
|
|
}
|