2007-06-08 18:20:49 +02: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"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|
|
|
|
2007-06-29 01:45:29 +02:00
|
|
|
/* Called when a message's ref count reaches zero. Free any message-specific
|
|
|
|
* data here.
|
|
|
|
*/
|
|
|
|
typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
|
|
|
|
|
2007-06-29 01:49:55 +02:00
|
|
|
typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData);
|
|
|
|
|
2007-06-29 01:51:47 +02:00
|
|
|
typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal);
|
|
|
|
|
2007-06-29 01:45:29 +02:00
|
|
|
typedef struct _CryptMsgBase
|
|
|
|
{
|
2007-06-29 01:49:55 +02:00
|
|
|
LONG ref;
|
|
|
|
DWORD open_flags;
|
2007-07-09 19:55:42 +02:00
|
|
|
BOOL streamed;
|
|
|
|
CMSG_STREAM_INFO stream_info;
|
2007-06-29 01:53:37 +02:00
|
|
|
BOOL finalized;
|
2007-06-29 01:49:55 +02:00
|
|
|
CryptMsgCloseFunc close;
|
2007-06-29 01:51:47 +02:00
|
|
|
CryptMsgUpdateFunc update;
|
2007-06-29 01:49:55 +02:00
|
|
|
CryptMsgGetParamFunc get_param;
|
2007-06-29 01:45:29 +02:00
|
|
|
} CryptMsgBase;
|
|
|
|
|
2007-06-29 01:52:37 +02:00
|
|
|
static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
|
2007-07-09 19:50:48 +02:00
|
|
|
PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
|
|
|
|
CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
|
2007-06-29 01:45:29 +02:00
|
|
|
{
|
|
|
|
msg->ref = 1;
|
|
|
|
msg->open_flags = dwFlags;
|
2007-07-09 19:55:42 +02: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 19:50:48 +02:00
|
|
|
msg->close = close;
|
|
|
|
msg->get_param = get_param;
|
|
|
|
msg->update = update;
|
2007-06-29 01:53:37 +02:00
|
|
|
msg->finalized = FALSE;
|
2007-06-29 01:45:29 +02:00
|
|
|
}
|
|
|
|
|
2007-06-29 01:49:06 +02:00
|
|
|
typedef struct _CDataEncodeMsg
|
|
|
|
{
|
|
|
|
CryptMsgBase base;
|
2007-06-29 02:14:02 +02:00
|
|
|
DWORD bare_content_len;
|
|
|
|
LPBYTE bare_content;
|
2007-06-29 01:49:06 +02:00
|
|
|
} CDataEncodeMsg;
|
|
|
|
|
2007-06-29 02:14:02 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
|
|
|
CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (msg->base.finalized)
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
else if (!fFinal)
|
|
|
|
{
|
|
|
|
if (msg->base.open_flags & CMSG_DETACHED_FLAG)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_MSG_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->base.finalized = TRUE;
|
|
|
|
if (!cbData)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
|
|
|
|
|
|
|
|
/* 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-07-09 19:55:42 +02:00
|
|
|
if (ret && msg->base.streamed)
|
2007-06-29 02:14:02 +02:00
|
|
|
FIXME("stream info unimplemented\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-29 02:16:15 +02:00
|
|
|
static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-06-29 02:19:04 +02:00
|
|
|
CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
|
2007-06-29 02:16:15 +02:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
switch (dwParamType)
|
|
|
|
{
|
|
|
|
case CMSG_CONTENT_PARAM:
|
2007-07-09 20:19:44 +02: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 02:16:15 +02:00
|
|
|
break;
|
2007-06-29 02:19:04 +02:00
|
|
|
case CMSG_BARE_CONTENT_PARAM:
|
2007-07-09 20:19:44 +02:00
|
|
|
if (msg->base.streamed)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else if (!pvData)
|
2007-06-29 02:19:04 +02:00
|
|
|
{
|
|
|
|
*pcbData = msg->bare_content_len;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else if (*pcbData < msg->bare_content_len)
|
|
|
|
{
|
|
|
|
*pcbData = msg->bare_content_len;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbData = msg->bare_content_len;
|
|
|
|
memcpy(pvData, msg->bare_content, msg->bare_content_len);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2007-06-29 02:16:15 +02:00
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-29 01:49:06 +02: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 19:50:48 +02:00
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update);
|
2007-06-29 02:14:02 +02:00
|
|
|
msg->bare_content_len = sizeof(empty_data_content);
|
|
|
|
msg->bare_content = (LPBYTE)empty_data_content;
|
2007-06-29 01:49:06 +02:00
|
|
|
}
|
|
|
|
return (HCRYPTMSG)msg;
|
|
|
|
}
|
|
|
|
|
2007-06-29 01:44:33 +02: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 18:20:49 +02:00
|
|
|
HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
|
|
|
|
DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
2007-06-29 01:44:33 +02:00
|
|
|
HCRYPTMSG msg = NULL;
|
|
|
|
|
|
|
|
TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
|
2007-06-08 18:20:49 +02:00
|
|
|
dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
|
2007-06-29 01:44:33 +02:00
|
|
|
|
|
|
|
if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
switch (dwMsgType)
|
|
|
|
{
|
|
|
|
case CMSG_DATA:
|
2007-06-29 01:49:06 +02:00
|
|
|
msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
|
|
|
|
pszInnerContentObjID, pStreamInfo);
|
|
|
|
break;
|
2007-06-29 01:44:33 +02:00
|
|
|
case CMSG_SIGNED:
|
|
|
|
case CMSG_ENVELOPED:
|
|
|
|
case CMSG_HASHED:
|
|
|
|
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 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 20:20:34 +02:00
|
|
|
typedef struct _CDecodeMsg
|
|
|
|
{
|
|
|
|
CryptMsgBase base;
|
|
|
|
DWORD type;
|
|
|
|
HCRYPTPROV crypt_prov;
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
|
|
|
FIXME("(%p, %p, %d, %d): stub\n", hCryptMsg, pbData, cbData, fFinal);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-07-09 20:22:08 +02:00
|
|
|
CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
switch (dwParamType)
|
|
|
|
{
|
|
|
|
case CMSG_TYPE_PARAM:
|
|
|
|
if (!pvData)
|
|
|
|
{
|
|
|
|
*pcbData = sizeof(DWORD);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else if (*pcbData < sizeof(DWORD))
|
|
|
|
{
|
|
|
|
*pcbData = sizeof(DWORD);
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbData = sizeof(DWORD);
|
|
|
|
*(DWORD *)pvData = msg->type;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented for parameter %d\n", dwParamType);
|
|
|
|
SetLastError(CRYPT_E_INVALID_MSG_TYPE);
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-09 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
2007-06-08 18:20:49 +02:00
|
|
|
HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
|
|
|
|
DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
2007-07-09 20:20:34 +02:00
|
|
|
CDecodeMsg *msg;
|
|
|
|
|
|
|
|
TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
|
2007-06-08 18:20:49 +02:00
|
|
|
dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
|
2007-06-29 01:44:33 +02:00
|
|
|
|
|
|
|
if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-07-09 20:20:34 +02:00
|
|
|
msg = CryptMemAlloc(sizeof(CDecodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
|
|
|
|
CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
|
|
|
|
msg->type = dwMsgType;
|
|
|
|
}
|
|
|
|
return msg;
|
2007-06-08 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
2007-06-29 01:45:29 +02:00
|
|
|
TRACE("(%p)\n", hCryptMsg);
|
|
|
|
|
|
|
|
if (hCryptMsg)
|
|
|
|
{
|
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
|
|
|
|
InterlockedIncrement(&msg->ref);
|
|
|
|
}
|
2007-06-08 18:20:49 +02:00
|
|
|
return hCryptMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
|
|
|
|
{
|
2007-06-29 01:45:29 +02: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 18:20:49 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
|
|
|
|
DWORD cbData, BOOL fFinal)
|
|
|
|
{
|
2007-06-29 01:51:47 +02:00
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
|
|
|
|
if (msg && msg->update)
|
|
|
|
ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
|
|
|
|
return ret;
|
2007-06-08 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
|
|
|
|
DWORD dwIndex, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2007-06-29 01:49:55 +02:00
|
|
|
CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
|
2007-06-08 18:20:49 +02:00
|
|
|
pvData, pcbData);
|
2007-06-29 01:49:55 +02:00
|
|
|
if (msg && msg->get_param)
|
|
|
|
ret = msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
|
|
|
|
return ret;
|
2007-06-08 18:20:49 +02:00
|
|
|
}
|