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"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
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-06-28 23:45:29 +00:00
|
|
|
typedef struct _CryptMsgBase
|
|
|
|
{
|
2007-06-28 23:49:55 +00:00
|
|
|
LONG ref;
|
|
|
|
DWORD open_flags;
|
2007-06-28 23:52:37 +00:00
|
|
|
PCMSG_STREAM_INFO stream_info;
|
2007-06-28 23:53:37 +00:00
|
|
|
BOOL finalized;
|
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,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
2007-06-28 23:45:29 +00:00
|
|
|
{
|
|
|
|
msg->ref = 1;
|
|
|
|
msg->open_flags = dwFlags;
|
2007-06-28 23:52:37 +00:00
|
|
|
msg->stream_info = pStreamInfo;
|
2007-06-28 23:53:37 +00:00
|
|
|
msg->finalized = FALSE;
|
2007-06-28 23:45:29 +00:00
|
|
|
}
|
|
|
|
|
2007-06-28 23:49:06 +00:00
|
|
|
typedef struct _CDataEncodeMsg
|
|
|
|
{
|
|
|
|
CryptMsgBase base;
|
|
|
|
} CDataEncodeMsg;
|
|
|
|
|
|
|
|
static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
|
|
|
|
LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
|
|
|
CDataEncodeMsg *msg;
|
|
|
|
|
|
|
|
if (pvMsgEncodeInfo)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
FIXME("semi-stub\n");
|
|
|
|
msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
|
|
|
|
if (msg)
|
|
|
|
{
|
2007-06-28 23:52:37 +00:00
|
|
|
CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo);
|
2007-06-28 23:49:06 +00:00
|
|
|
}
|
|
|
|
return (HCRYPTMSG)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-06-28 23:44:33 +00: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 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
|
|
|
|
DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
|
|
|
|
PCMSG_STREAM_INFO pStreamInfo)
|
|
|
|
{
|
|
|
|
FIXME("(%08x, %08x, %08x, %08lx, %p, %p): stub\n", dwMsgEncodingType,
|
|
|
|
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-06-28 23:41:43 +00:00
|
|
|
return NULL;
|
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;
|
|
|
|
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 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;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
|
2007-06-08 16:20:49 +00:00
|
|
|
pvData, pcbData);
|
2007-06-28 23:49:55 +00:00
|
|
|
if (msg && msg->get_param)
|
|
|
|
ret = msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
|
|
|
|
return ret;
|
2007-06-08 16:20:49 +00:00
|
|
|
}
|