From 64dce8a579f2894d39087dc51e329dd7407b2190 Mon Sep 17 00:00:00 2001 From: Michael Jung <mjung@iss.tu-darmstadt.de> Date: Thu, 4 Nov 2004 21:15:32 +0000 Subject: [PATCH] Implemented a substantial part of rsaenh.dll. --- configure | 14 +- configure.ac | 7 + dlls/Makefile.in | 13 + dlls/rsaenh/.cvsignore | 3 + dlls/rsaenh/Makefile.in | 18 + dlls/rsaenh/handle.c | 454 ++++++ dlls/rsaenh/handle.h | 75 + dlls/rsaenh/implossl.c | 683 +++++++++ dlls/rsaenh/implossl.h | 129 ++ dlls/rsaenh/rsaenh.c | 2658 +++++++++++++++++++++++++++++++++ dlls/rsaenh/rsaenh.spec | 27 + dlls/rsaenh/tests/.cvsignore | 3 + dlls/rsaenh/tests/Makefile.in | 13 + dlls/rsaenh/tests/rsaenh.c | 843 +++++++++++ include/config.h.in | 15 + tools/wine.inf | 2 +- 16 files changed, 4955 insertions(+), 2 deletions(-) create mode 100644 dlls/rsaenh/.cvsignore create mode 100644 dlls/rsaenh/Makefile.in create mode 100644 dlls/rsaenh/handle.c create mode 100644 dlls/rsaenh/handle.h create mode 100644 dlls/rsaenh/implossl.c create mode 100644 dlls/rsaenh/implossl.h create mode 100644 dlls/rsaenh/rsaenh.c create mode 100644 dlls/rsaenh/rsaenh.spec create mode 100644 dlls/rsaenh/tests/.cvsignore create mode 100644 dlls/rsaenh/tests/Makefile.in create mode 100644 dlls/rsaenh/tests/rsaenh.c diff --git a/configure b/configure index 723fa2dd660..fa067a99a6e 100755 --- a/configure +++ b/configure @@ -16473,6 +16473,11 @@ done + + + + + @@ -16514,6 +16519,11 @@ for ac_header in \ netinet/in_systm.h \ netinet/tcp.h \ netinet/tcp_fsm.h \ + openssl/des.h \ + openssl/md2.h \ + openssl/rc2.h \ + openssl/rc4.h \ + openssl/rsa.h \ openssl/ssl.h \ process.h \ pthread.h \ @@ -20228,7 +20238,7 @@ MAKE_LIB_RULES=libs/Makelib.rules MAKE_PROG_RULES=programs/Makeprog.rules - ac_config_files="$ac_config_files Make.rules dlls/Makedll.rules dlls/Maketest.rules libs/Makelib.rules programs/Makeprog.rules Makefile dlls/Makefile dlls/advapi32/Makefile dlls/advapi32/tests/Makefile dlls/advpack/Makefile dlls/amstream/Makefile dlls/atl/Makefile dlls/avicap32/Makefile dlls/avifil32/Makefile dlls/cabinet/Makefile dlls/capi2032/Makefile dlls/cards/Makefile dlls/cfgmgr32/Makefile dlls/comcat/Makefile dlls/comctl32/Makefile dlls/comctl32/tests/Makefile dlls/commdlg/Makefile dlls/crtdll/Makefile dlls/crypt32/Makefile dlls/ctl3d/Makefile dlls/d3d8/Makefile dlls/d3d9/Makefile dlls/d3dim/Makefile dlls/d3drm/Makefile dlls/d3dx8/Makefile dlls/d3dxof/Makefile dlls/dbghelp/Makefile dlls/dciman32/Makefile dlls/ddraw/Makefile dlls/ddraw/tests/Makefile dlls/devenum/Makefile dlls/dinput/Makefile dlls/dinput8/Makefile dlls/dmband/Makefile dlls/dmcompos/Makefile dlls/dmime/Makefile dlls/dmloader/Makefile dlls/dmscript/Makefile dlls/dmstyle/Makefile dlls/dmsynth/Makefile dlls/dmusic/Makefile dlls/dmusic32/Makefile dlls/dplay/Makefile dlls/dplayx/Makefile dlls/dpnet/Makefile dlls/dpnhpast/Makefile dlls/dsound/Makefile dlls/dsound/tests/Makefile dlls/dswave/Makefile dlls/dxdiagn/Makefile dlls/dxerr8/Makefile dlls/dxerr9/Makefile dlls/dxguid/Makefile dlls/gdi/Makefile dlls/gdi/tests/Makefile dlls/glu32/Makefile dlls/glut32/Makefile dlls/hhctrl.ocx/Makefile dlls/iccvid/Makefile dlls/icmp/Makefile dlls/ifsmgr.vxd/Makefile dlls/imagehlp/Makefile dlls/imm32/Makefile dlls/iphlpapi/Makefile dlls/iphlpapi/tests/Makefile dlls/itss/Makefile dlls/kernel/Makefile dlls/kernel/tests/Makefile dlls/lzexpand/Makefile dlls/mapi32/Makefile dlls/mapi32/tests/Makefile dlls/mlang/Makefile dlls/mlang/tests/Makefile dlls/mmdevldr.vxd/Makefile dlls/monodebg.vxd/Makefile dlls/mpr/Makefile dlls/msacm/Makefile dlls/msacm/imaadp32/Makefile dlls/msacm/msadp32/Makefile dlls/msacm/msg711/Makefile dlls/msacm/winemp3/Makefile dlls/msacm/tests/Makefile dlls/mscms/Makefile dlls/mscms/tests/Makefile dlls/msdmo/Makefile dlls/mshtml/Makefile dlls/msi/Makefile dlls/msimg32/Makefile dlls/msisys/Makefile dlls/msnet32/Makefile dlls/msrle32/Makefile dlls/msvcrt/Makefile dlls/msvcrt/tests/Makefile dlls/msvcrt20/Makefile dlls/msvcrt40/Makefile dlls/msvcrtd/Makefile dlls/msvcrtd/tests/Makefile dlls/msvidc32/Makefile dlls/msvideo/Makefile dlls/mswsock/Makefile dlls/netapi32/Makefile dlls/netapi32/tests/Makefile dlls/newdev/Makefile dlls/ntdll/Makefile dlls/ntdll/tests/Makefile dlls/odbc32/Makefile dlls/ole32/Makefile dlls/ole32/tests/Makefile dlls/oleacc/Makefile dlls/oleaut32/Makefile dlls/oleaut32/tests/Makefile dlls/olecli/Makefile dlls/oledlg/Makefile dlls/olepro32/Makefile dlls/olesvr/Makefile dlls/opengl32/Makefile dlls/psapi/Makefile dlls/psapi/tests/Makefile dlls/qcap/Makefile dlls/quartz/Makefile dlls/quartz/tests/Makefile dlls/rasapi32/Makefile dlls/richedit/Makefile dlls/rpcrt4/Makefile dlls/rpcrt4/tests/Makefile dlls/rsabase/Makefile dlls/rsabase/tests/Makefile dlls/secur32/Makefile dlls/serialui/Makefile dlls/setupapi/Makefile dlls/shdocvw/Makefile dlls/shell32/Makefile dlls/shell32/tests/Makefile dlls/shfolder/Makefile dlls/shlwapi/Makefile dlls/shlwapi/tests/Makefile dlls/snmpapi/Makefile dlls/sti/Makefile dlls/strmiids/Makefile dlls/tapi32/Makefile dlls/ttydrv/Makefile dlls/twain/Makefile dlls/unicows/Makefile dlls/url/Makefile dlls/urlmon/Makefile dlls/urlmon/tests/Makefile dlls/user/Makefile dlls/user/tests/Makefile dlls/uuid/Makefile dlls/uxtheme/Makefile dlls/vdhcp.vxd/Makefile dlls/vdmdbg/Makefile dlls/version/Makefile dlls/version/tests/Makefile dlls/vmm.vxd/Makefile dlls/vnbt.vxd/Makefile dlls/vnetbios.vxd/Makefile dlls/vtdapi.vxd/Makefile dlls/vwin32.vxd/Makefile dlls/win32s/Makefile dlls/winaspi/Makefile dlls/wined3d/Makefile dlls/winedos/Makefile dlls/wineps/Makefile dlls/wininet/Makefile dlls/wininet/tests/Makefile dlls/winmm/Makefile dlls/winmm/joystick/Makefile dlls/winmm/mcianim/Makefile dlls/winmm/mciavi/Makefile dlls/winmm/mcicda/Makefile dlls/winmm/mciseq/Makefile dlls/winmm/mciwave/Makefile dlls/winmm/midimap/Makefile dlls/winmm/tests/Makefile dlls/winmm/wavemap/Makefile dlls/winmm/winealsa/Makefile dlls/winmm/winearts/Makefile dlls/winmm/wineaudioio/Makefile dlls/winmm/winejack/Makefile dlls/winmm/winenas/Makefile dlls/winmm/wineoss/Makefile dlls/winnls/Makefile dlls/winsock/Makefile dlls/winsock/tests/Makefile dlls/winspool/Makefile dlls/winspool/tests/Makefile dlls/wintab32/Makefile dlls/wintrust/Makefile dlls/wow32/Makefile dlls/wsock32/Makefile dlls/x11drv/Makefile documentation/Makefile fonts/Makefile include/Makefile libs/Makefile libs/port/Makefile libs/unicode/Makefile libs/wine/Makefile libs/wpp/Makefile loader/Makefile programs/Makefile programs/avitools/Makefile programs/clock/Makefile programs/cmdlgtst/Makefile programs/control/Makefile programs/expand/Makefile programs/msiexec/Makefile programs/notepad/Makefile programs/progman/Makefile programs/regedit/Makefile programs/regsvr32/Makefile programs/rpcss/Makefile programs/rundll32/Makefile programs/start/Makefile programs/taskmgr/Makefile programs/uninstaller/Makefile programs/view/Makefile programs/wcmd/Makefile programs/wineboot/Makefile programs/winebrowser/Makefile programs/winecfg/Makefile programs/wineconsole/Makefile programs/winedbg/Makefile programs/winefile/Makefile programs/winemenubuilder/Makefile programs/winemine/Makefile programs/winepath/Makefile programs/winetest/Makefile programs/winevdm/Makefile programs/winhelp/Makefile programs/winver/Makefile server/Makefile tools/Makefile tools/widl/Makefile tools/winapi/Makefile tools/winebuild/Makefile tools/winedump/Makefile tools/winegcc/Makefile tools/wmc/Makefile tools/wrc/Makefile" + ac_config_files="$ac_config_files Make.rules dlls/Makedll.rules dlls/Maketest.rules libs/Makelib.rules programs/Makeprog.rules Makefile dlls/Makefile dlls/advapi32/Makefile dlls/advapi32/tests/Makefile dlls/advpack/Makefile dlls/amstream/Makefile dlls/atl/Makefile dlls/avicap32/Makefile dlls/avifil32/Makefile dlls/cabinet/Makefile dlls/capi2032/Makefile dlls/cards/Makefile dlls/cfgmgr32/Makefile dlls/comcat/Makefile dlls/comctl32/Makefile dlls/comctl32/tests/Makefile dlls/commdlg/Makefile dlls/crtdll/Makefile dlls/crypt32/Makefile dlls/ctl3d/Makefile dlls/d3d8/Makefile dlls/d3d9/Makefile dlls/d3dim/Makefile dlls/d3drm/Makefile dlls/d3dx8/Makefile dlls/d3dxof/Makefile dlls/dbghelp/Makefile dlls/dciman32/Makefile dlls/ddraw/Makefile dlls/ddraw/tests/Makefile dlls/devenum/Makefile dlls/dinput/Makefile dlls/dinput8/Makefile dlls/dmband/Makefile dlls/dmcompos/Makefile dlls/dmime/Makefile dlls/dmloader/Makefile dlls/dmscript/Makefile dlls/dmstyle/Makefile dlls/dmsynth/Makefile dlls/dmusic/Makefile dlls/dmusic32/Makefile dlls/dplay/Makefile dlls/dplayx/Makefile dlls/dpnet/Makefile dlls/dpnhpast/Makefile dlls/dsound/Makefile dlls/dsound/tests/Makefile dlls/dswave/Makefile dlls/dxdiagn/Makefile dlls/dxerr8/Makefile dlls/dxerr9/Makefile dlls/dxguid/Makefile dlls/gdi/Makefile dlls/gdi/tests/Makefile dlls/glu32/Makefile dlls/glut32/Makefile dlls/hhctrl.ocx/Makefile dlls/iccvid/Makefile dlls/icmp/Makefile dlls/ifsmgr.vxd/Makefile dlls/imagehlp/Makefile dlls/imm32/Makefile dlls/iphlpapi/Makefile dlls/iphlpapi/tests/Makefile dlls/itss/Makefile dlls/kernel/Makefile dlls/kernel/tests/Makefile dlls/lzexpand/Makefile dlls/mapi32/Makefile dlls/mapi32/tests/Makefile dlls/mlang/Makefile dlls/mlang/tests/Makefile dlls/mmdevldr.vxd/Makefile dlls/monodebg.vxd/Makefile dlls/mpr/Makefile dlls/msacm/Makefile dlls/msacm/imaadp32/Makefile dlls/msacm/msadp32/Makefile dlls/msacm/msg711/Makefile dlls/msacm/winemp3/Makefile dlls/msacm/tests/Makefile dlls/mscms/Makefile dlls/mscms/tests/Makefile dlls/msdmo/Makefile dlls/mshtml/Makefile dlls/msi/Makefile dlls/msimg32/Makefile dlls/msisys/Makefile dlls/msnet32/Makefile dlls/msrle32/Makefile dlls/msvcrt/Makefile dlls/msvcrt/tests/Makefile dlls/msvcrt20/Makefile dlls/msvcrt40/Makefile dlls/msvcrtd/Makefile dlls/msvcrtd/tests/Makefile dlls/msvidc32/Makefile dlls/msvideo/Makefile dlls/mswsock/Makefile dlls/netapi32/Makefile dlls/netapi32/tests/Makefile dlls/newdev/Makefile dlls/ntdll/Makefile dlls/ntdll/tests/Makefile dlls/odbc32/Makefile dlls/ole32/Makefile dlls/ole32/tests/Makefile dlls/oleacc/Makefile dlls/oleaut32/Makefile dlls/oleaut32/tests/Makefile dlls/olecli/Makefile dlls/oledlg/Makefile dlls/olepro32/Makefile dlls/olesvr/Makefile dlls/opengl32/Makefile dlls/psapi/Makefile dlls/psapi/tests/Makefile dlls/qcap/Makefile dlls/quartz/Makefile dlls/quartz/tests/Makefile dlls/rasapi32/Makefile dlls/richedit/Makefile dlls/rpcrt4/Makefile dlls/rpcrt4/tests/Makefile dlls/rsabase/Makefile dlls/rsabase/tests/Makefile dlls/rsaenh/Makefile dlls/rsaenh/tests/Makefile dlls/secur32/Makefile dlls/serialui/Makefile dlls/setupapi/Makefile dlls/shdocvw/Makefile dlls/shell32/Makefile dlls/shell32/tests/Makefile dlls/shfolder/Makefile dlls/shlwapi/Makefile dlls/shlwapi/tests/Makefile dlls/snmpapi/Makefile dlls/sti/Makefile dlls/strmiids/Makefile dlls/tapi32/Makefile dlls/ttydrv/Makefile dlls/twain/Makefile dlls/unicows/Makefile dlls/url/Makefile dlls/urlmon/Makefile dlls/urlmon/tests/Makefile dlls/user/Makefile dlls/user/tests/Makefile dlls/uuid/Makefile dlls/uxtheme/Makefile dlls/vdhcp.vxd/Makefile dlls/vdmdbg/Makefile dlls/version/Makefile dlls/version/tests/Makefile dlls/vmm.vxd/Makefile dlls/vnbt.vxd/Makefile dlls/vnetbios.vxd/Makefile dlls/vtdapi.vxd/Makefile dlls/vwin32.vxd/Makefile dlls/win32s/Makefile dlls/winaspi/Makefile dlls/wined3d/Makefile dlls/winedos/Makefile dlls/wineps/Makefile dlls/wininet/Makefile dlls/wininet/tests/Makefile dlls/winmm/Makefile dlls/winmm/joystick/Makefile dlls/winmm/mcianim/Makefile dlls/winmm/mciavi/Makefile dlls/winmm/mcicda/Makefile dlls/winmm/mciseq/Makefile dlls/winmm/mciwave/Makefile dlls/winmm/midimap/Makefile dlls/winmm/tests/Makefile dlls/winmm/wavemap/Makefile dlls/winmm/winealsa/Makefile dlls/winmm/winearts/Makefile dlls/winmm/wineaudioio/Makefile dlls/winmm/winejack/Makefile dlls/winmm/winenas/Makefile dlls/winmm/wineoss/Makefile dlls/winnls/Makefile dlls/winsock/Makefile dlls/winsock/tests/Makefile dlls/winspool/Makefile dlls/winspool/tests/Makefile dlls/wintab32/Makefile dlls/wintrust/Makefile dlls/wow32/Makefile dlls/wsock32/Makefile dlls/x11drv/Makefile documentation/Makefile fonts/Makefile include/Makefile libs/Makefile libs/port/Makefile libs/unicode/Makefile libs/wine/Makefile libs/wpp/Makefile loader/Makefile programs/Makefile programs/avitools/Makefile programs/clock/Makefile programs/cmdlgtst/Makefile programs/control/Makefile programs/expand/Makefile programs/msiexec/Makefile programs/notepad/Makefile programs/progman/Makefile programs/regedit/Makefile programs/regsvr32/Makefile programs/rpcss/Makefile programs/rundll32/Makefile programs/start/Makefile programs/taskmgr/Makefile programs/uninstaller/Makefile programs/view/Makefile programs/wcmd/Makefile programs/wineboot/Makefile programs/winebrowser/Makefile programs/winecfg/Makefile programs/wineconsole/Makefile programs/winedbg/Makefile programs/winefile/Makefile programs/winemenubuilder/Makefile programs/winemine/Makefile programs/winepath/Makefile programs/winetest/Makefile programs/winevdm/Makefile programs/winhelp/Makefile programs/winver/Makefile server/Makefile tools/Makefile tools/widl/Makefile tools/winapi/Makefile tools/winebuild/Makefile tools/winedump/Makefile tools/winegcc/Makefile tools/wmc/Makefile tools/wrc/Makefile" cat >confcache <<\_ACEOF @@ -20891,6 +20901,8 @@ do "dlls/rpcrt4/tests/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/rpcrt4/tests/Makefile" ;; "dlls/rsabase/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/rsabase/Makefile" ;; "dlls/rsabase/tests/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/rsabase/tests/Makefile" ;; + "dlls/rsaenh/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/rsaenh/Makefile" ;; + "dlls/rsaenh/tests/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/rsaenh/tests/Makefile" ;; "dlls/secur32/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/secur32/Makefile" ;; "dlls/serialui/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/serialui/Makefile" ;; "dlls/setupapi/Makefile" ) CONFIG_FILES="$CONFIG_FILES dlls/setupapi/Makefile" ;; diff --git a/configure.ac b/configure.ac index 6f462357d98..6fb04fa1638 100644 --- a/configure.ac +++ b/configure.ac @@ -1174,6 +1174,11 @@ AC_CHECK_HEADERS(\ netinet/in_systm.h \ netinet/tcp.h \ netinet/tcp_fsm.h \ + openssl/des.h \ + openssl/md2.h \ + openssl/rc2.h \ + openssl/rc4.h \ + openssl/rsa.h \ openssl/ssl.h \ process.h \ pthread.h \ @@ -1686,6 +1691,8 @@ dlls/rpcrt4/Makefile dlls/rpcrt4/tests/Makefile dlls/rsabase/Makefile dlls/rsabase/tests/Makefile +dlls/rsaenh/Makefile +dlls/rsaenh/tests/Makefile dlls/secur32/Makefile dlls/serialui/Makefile dlls/setupapi/Makefile diff --git a/dlls/Makefile.in b/dlls/Makefile.in index ab0b82cdea7..6efa253bb33 100644 --- a/dlls/Makefile.in +++ b/dlls/Makefile.in @@ -110,6 +110,7 @@ BASEDIRS = \ richedit \ rpcrt4 \ rsabase \ + rsaenh \ secur32 \ serialui \ setupapi \ @@ -348,6 +349,7 @@ SYMLINKS_SO = \ riched32.dll.so \ rpcrt4.dll.so \ rsabase.dll.so \ + rsaenh.dll.so \ secur32.dll.so \ serialui.dll.so \ setupapi.dll.so \ @@ -777,6 +779,9 @@ rpcrt4.dll.so: rpcrt4/rpcrt4.dll.so rsabase.dll.so: rsabase/rsabase.dll.so $(RM) $@ && $(LN_S) rsabase/rsabase.dll.so $@ +rsaenh.dll.so: rsaenh/rsaenh.dll.so + $(RM) $@ && $(LN_S) rsaenh/rsaenh.dll.so $@ + secur32.dll.so: secur32/secur32.dll.so $(RM) $@ && $(LN_S) secur32/secur32.dll.so $@ @@ -1069,6 +1074,7 @@ IMPORT_LIBS = \ libriched32.$(IMPLIBEXT) \ librpcrt4.$(IMPLIBEXT) \ librsabase.$(IMPLIBEXT) \ + librsaenh.$(IMPLIBEXT) \ libsecur32.$(IMPLIBEXT) \ libserialui.$(IMPLIBEXT) \ libsetupapi.$(IMPLIBEXT) \ @@ -1571,6 +1577,11 @@ librsabase.def: rsabase/rsabase.spec.def librsabase.a: rsabase/rsabase.spec.def $(DLLTOOL) -k -l $@ -d rsabase/rsabase.spec.def +librsaenh.def: rsaenh/rsaenh.spec.def + $(RM) $@ && $(LN_S) rsaenh/rsaenh.spec.def $@ +librsaenh.a: rsaenh/rsaenh.spec.def + $(DLLTOOL) -k -l $@ -d rsaenh/rsaenh.spec.def + libsecur32.def: secur32/secur32.spec.def $(RM) $@ && $(LN_S) secur32/secur32.spec.def $@ libsecur32.a: secur32/secur32.spec.def @@ -1833,6 +1844,7 @@ rasapi32/rasapi32.spec.def: $(WINEBUILD) richedit/riched32.spec.def: $(WINEBUILD) rpcrt4/rpcrt4.spec.def: $(WINEBUILD) rsabase/rsabase.spec.def: $(WINEBUILD) +rsaenh/rsaenh.spec.def: $(WINEBUILD) secur32/secur32.spec.def: $(WINEBUILD) serialui/serialui.spec.def: $(WINEBUILD) setupapi/setupapi.spec.def: $(WINEBUILD) @@ -1981,6 +1993,7 @@ rasapi32/rasapi32.dll.so: rasapi32 richedit/riched32.dll.so: richedit rpcrt4/rpcrt4.dll.so: rpcrt4 rsabase/rsabase.dll.so: rsabase +rsaenh/rsaenh.dll.so: rsaenh secur32/secur32.dll.so: secur32 serialui/serialui.dll.so: serialui setupapi/setupapi.dll.so: setupapi diff --git a/dlls/rsaenh/.cvsignore b/dlls/rsaenh/.cvsignore new file mode 100644 index 00000000000..d41d0d6399d --- /dev/null +++ b/dlls/rsaenh/.cvsignore @@ -0,0 +1,3 @@ +Makefile +rsaenh.dll.dbg.c +rsaenh.spec.def diff --git a/dlls/rsaenh/Makefile.in b/dlls/rsaenh/Makefile.in new file mode 100644 index 00000000000..cea30329d5f --- /dev/null +++ b/dlls/rsaenh/Makefile.in @@ -0,0 +1,18 @@ +EXTRADEFS = -DCOM_NO_WINDOWS_H +TOPSRCDIR = @top_srcdir@ +TOPOBJDIR = ../.. +SRCDIR = @srcdir@ +VPATH = @srcdir@ +MODULE = rsaenh.dll +IMPORTS = advapi32 kernel32 + +C_SRCS = \ + handle.c \ + implossl.c \ + rsaenh.c + +SUBDIRS = tests + +@MAKE_DLL_RULES@ + +### Dependencies: diff --git a/dlls/rsaenh/handle.c b/dlls/rsaenh/handle.c new file mode 100644 index 00000000000..7b2b8177774 --- /dev/null +++ b/dlls/rsaenh/handle.c @@ -0,0 +1,454 @@ +/* + * dlls/rsaenh/handle.c + * Support code to manage HANDLE tables. + * + * Copyright 1998 Alexandre Julliard + * Copyright 2002-2004 Mike McCormack for CodeWeavers + * Copyright 2004 Michael Jung + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <string.h> +#include <stdarg.h> + +#include "windef.h" +#include "winbase.h" +#include "handle.h" + +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(handle); + +#define HANDLE2INDEX(h) ((h)-1) +#define INDEX2HANDLE(i) ((i)+1) + +/****************************************************************************** + * init_handle_table + * + * Initializes the HANDLETABLE structure pointed to by lpTable + * + * PARAMS + * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initalized. + * + * NOTES + * Note that alloc_handle_table calls init_handle_table on it's own, which + * means that you only have to call init_handle_table, if you use a global + * variable of type HANDLETABLE for your handle table. However, in this + * case you have to call destroy_handle_table when you don't need the table + * any more. + */ +void init_handle_table(HANDLETABLE *lpTable) +{ + TRACE("(lpTable=%p)\n", lpTable); + + lpTable->paEntries = NULL; + lpTable->iEntries = 0; + lpTable->iFirstFree = 0; + InitializeCriticalSection(&lpTable->mutex); +} + +/****************************************************************************** + * destroy_handle_table + * + * Destroys the handle table. + * + * PARAMS + * lpTable [I] Pointer to the handle table, which is to be destroyed. + * + * NOTES + * Note that release_handle_table takes care of this. + */ +void destroy_handle_table(HANDLETABLE *lpTable) +{ + TRACE("(lpTable=%p)\n", lpTable); + + if (lpTable->paEntries) + HeapFree(GetProcessHeap(), 0, lpTable->paEntries); + DeleteCriticalSection(&lpTable->mutex); +} + +/****************************************************************************** + * is_valid_handle + * + * Tests if handle is valid given the specified handle table + * + * PARAMS + * lpTable [I] Pointer to the handle table, with respect to which the handle's + * validness is tested. + * handle [I] The handle tested for validness. + * dwType [I] A magic value that identifies the referenced object's type. + * + * RETURNS + * non zero, if handle is valid. + * zero, if handle is not valid. + */ +int is_valid_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType) +{ + unsigned int index = HANDLE2INDEX(handle); + int ret = 0; + + TRACE("(lpTable=%p, handle=%d)\n", lpTable, handle); + + EnterCriticalSection(&lpTable->mutex); + + /* We don't use zero handle values */ + if (!handle) goto exit; + + /* Check for index out of table bounds */ + if (index >= lpTable->iEntries) goto exit; + + /* Check if this handle is currently allocated */ + if (!lpTable->paEntries[index].pObject) goto exit; + + /* Check if this handle references an object of the correct type. */ + if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit; + + ret = 1; +exit: + LeaveCriticalSection(&lpTable->mutex); + return ret; +} + +/****************************************************************************** + * alloc_handle_table + * + * Allocates a new handle table + * + * PARAMS + * lplpTable [O] Pointer to the variable, to which the pointer to the newly + * allocated handle table is written. + * RETURNS + * non zero, if successfull + * zero, if not successfull (out of process heap memory) + * + * NOTES + * If all you need is a single handle table, you may as well declare a global + * variable of type HANDLETABLE and call init_handle_table on your own. + */ +int alloc_handle_table(HANDLETABLE **lplpTable) +{ + TRACE("(lplpTable=%p)\n", lplpTable); + + *lplpTable = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLETABLE)); + if (*lplpTable) + { + init_handle_table(*lplpTable); + return 1; + } + else + return 0; +} + +/****************************************************************************** + * release_handle_table + * + * Releases a handle table and frees the resources occupied by it. + * + * PARAMS + * lpTable [I] Pointer to the handle table, which is to be released. + * + * RETURNS + * non zero, if successfull + * zero, if not successfull + * + * NOTES + * All valid handles still in the table are released also. + */ +int release_handle_table(HANDLETABLE *lpTable) +{ + TRACE("(lpTable=%p)\n", lpTable); + + release_all_handles(lpTable); + destroy_handle_table(lpTable); + return (int)HeapFree(GetProcessHeap(), 0, lpTable); +} + +/****************************************************************************** + * grow_handle_table [Internal] + * + * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT + * + * PARAMS + * lpTable [I] Pointer to the table, which is to be grown + * + * RETURNS + * non zero, if successfull + * zero, if not successfull (out of memory on process heap) + * + * NOTES + * This is a support function for alloc_handle. Do not call! + */ +static int grow_handle_table(HANDLETABLE *lpTable) +{ + HANDLETABLEENTRY *newEntries; + unsigned int i, newIEntries; + + newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT; + + newEntries = (HANDLETABLEENTRY*)HeapAlloc(GetProcessHeap(), 0, + sizeof(HANDLETABLEENTRY)*newIEntries); + if (!newEntries) + return 0; + + if (lpTable->paEntries) + { + memcpy(newEntries, lpTable->paEntries, sizeof(HANDLETABLEENTRY)*lpTable->iEntries); + HeapFree(GetProcessHeap(), 0, lpTable->paEntries); + } + + for (i=lpTable->iEntries; i<newIEntries; i++) + { + newEntries[i].pObject = NULL; + newEntries[i].iNextFree = i+1; + } + + lpTable->paEntries = newEntries; + lpTable->iEntries = newIEntries; + + return 1; +} + +/****************************************************************************** + * alloc_handle + * + * Allocates a new handle to the specified object in a given handle table. + * + * PARAMS + * lpTable [I] Pointer to the handle table, from which the new handle is + * allocated. + * lpObject [I] Pointer to the object, for which a handle shall be allocated. + * lpHandle [O] Pointer to a handle variable, into which the handle value will + * be stored. If not successfull, this will be + * INVALID_HANDLE_VALUE + * RETURNS + * non zero, if successfull + * zero, if not successfull (no free handle) + */ +int alloc_handle(HANDLETABLE *lpTable, OBJECTHDR *lpObject, unsigned int *lpHandle) +{ + int ret = 0; + + TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle); + + EnterCriticalSection(&lpTable->mutex); + if (lpTable->iFirstFree >= lpTable->iEntries) + if (!grow_handle_table(lpTable)) + { + *lpHandle = (unsigned int)INVALID_HANDLE_VALUE; + goto exit; + } + + *lpHandle = INDEX2HANDLE(lpTable->iFirstFree); + + lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject; + lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree; + lpObject->refcount++; + + ret = 1; +exit: + LeaveCriticalSection(&lpTable->mutex); + return ret; +} + +/****************************************************************************** + * release_handle + * + * Releases resources occupied by the specified handle in the given table. + * The reference count of the handled object is decremented. If it becomes + * zero and if the 'destructor' function pointer member is non NULL, the + * destructor function will be called. Note that release_handle does not + * release resources other than the handle itself. If this is wanted, do it + * in the destructor function. + * + * PARAMS + * lpTable [I] Pointer to the handle table, from which a handle is to be + * released. + * handle [I] The handle, which is to be released + * dwType [I] Identifier for the type of the object, for which a handle is + * to be released. + * + * RETURNS + * non zero, if successfull + * zero, if not successfull (invalid handle) + */ +int release_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType) +{ + unsigned int index = HANDLE2INDEX(handle); + OBJECTHDR *pObject; + int ret = 0; + + TRACE("(lpTable=%p, hande=%d)\n", lpTable, handle); + + EnterCriticalSection(&lpTable->mutex); + + if (!is_valid_handle(lpTable, handle, dwType)) + goto exit; + + pObject = lpTable->paEntries[index].pObject; + pObject->refcount--; + if (pObject->refcount == 0) + if (pObject->destructor) + pObject->destructor(pObject); + + lpTable->paEntries[index].pObject = NULL; + lpTable->paEntries[index].iNextFree = lpTable->iFirstFree; + lpTable->iFirstFree = index; + + ret = 1; +exit: + LeaveCriticalSection(&lpTable->mutex); + return ret; +} + +/****************************************************************************** + * release_all_handles + * + * Releases all valid handles in the given handle table and shrinks the table + * to zero size. + * + * PARAMS + * lpTable [I] The table of which all valid handles shall be released. + */ +void release_all_handles(HANDLETABLE *lpTable) +{ + unsigned int i; + + TRACE("(lpTable=%p)\n", lpTable); + + EnterCriticalSection(&lpTable->mutex); + for (i=0; i<lpTable->iEntries; i++) + if (lpTable->paEntries[i].pObject) + release_handle(lpTable, lpTable->paEntries[i].pObject->dwType, INDEX2HANDLE(i)); + LeaveCriticalSection(&lpTable->mutex); +} + +/****************************************************************************** + * lookup_handle + * + * Returns the object identified by the handle in the given handle table + * + * PARAMS + * lpTable [I] Pointer to the handle table, in which the handle is looked up. + * handle [I] The handle, which is to be looked up + * lplpObject [O] Pointer to the variable, into which the pointer to the + * object looked up is copied. + * RETURNS + * non zero, if successfull + * zero, if not successfull (invalid handle) + */ +int lookup_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, OBJECTHDR **lplpObject) +{ + int ret = 0; + + TRACE("(lpTable=%p, handle=%d, lplpObject=%p)\n", lpTable, handle, lplpObject); + + EnterCriticalSection(&lpTable->mutex); + if (!is_valid_handle(lpTable, handle, dwType)) + { + *lplpObject = NULL; + goto exit; + } + *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject; + + ret = 1; +exit: + LeaveCriticalSection(&lpTable->mutex); + return ret; +} + +/****************************************************************************** + * copy_handle + * + * Copies a handle. Increments reference count in the object referenced by the + * handle + * + * PARAMS + * lpTable [I] Pointer to the handle table, which holds the handle to be copied. + * handle [I] The handle to be copied. + * copy [O] Pointer to a handle variable, where the copied handle is put. + * + * RETURNS + * non zero, if successfull + * zero, if not successfull (invalid handle or out of memory) + */ +int copy_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, unsigned int *copy) +{ + OBJECTHDR *pObject; + int ret; + + TRACE("(lpTable=%p, handle=%d, copy=%p)\n", lpTable, handle, copy); + + EnterCriticalSection(&lpTable->mutex); + if (!lookup_handle(lpTable, handle, dwType, &pObject)) + { + *copy = (unsigned int)INVALID_HANDLE_VALUE; + LeaveCriticalSection(&lpTable->mutex); + return 0; + } + + ret = alloc_handle(lpTable, pObject, copy); + LeaveCriticalSection(&lpTable->mutex); + return ret; +} + +/****************************************************************************** + * new_object + * + * Allocates a new object of size cbSize on the current process's heap. + * Initializes the object header using the destructor and dwType params. + * Allocates a handle to the object in the handle table pointed to by lpTable. + * Returns a pointer to the created object in ppObject. + * Returns a handle to the created object. + * + * PARAMS + * lpTable [I] Pointer to the handle table, from which a handle is to be + * allocated. + * cbSize [I] Size of the object to be allocated in bytes. + * dwType [I] Object type; will be copied to the object header. + * destructor [I] Function pointer to a destructor function. Will be called + * once the object's reference count gets zero. + * ppObject [O] Pointer to a pointer variable, where a pointer to the newly + * created object will be stored. You may set this to NULL. + * + * RETURNS + * INVALID_HANDLE_VALUE, if something went wrong. + * a handle to the new object, if successfull. + */ +unsigned int new_object(HANDLETABLE *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor, + OBJECTHDR **ppObject) +{ + OBJECTHDR *pObject; + unsigned int hObject; + + if (ppObject) + *ppObject = NULL; + + pObject = (OBJECTHDR*)HeapAlloc(GetProcessHeap(), 0, cbSize); + if (!pObject) + return (unsigned int)INVALID_HANDLE_VALUE; + + pObject->dwType = dwType; + pObject->refcount = 0; + pObject->destructor = destructor; + + if (!alloc_handle(lpTable, pObject, &hObject)) + HeapFree(GetProcessHeap(), 0, pObject); + else + if (ppObject) + *ppObject = pObject; + + return hObject; +} diff --git a/dlls/rsaenh/handle.h b/dlls/rsaenh/handle.h new file mode 100644 index 00000000000..77df2a83f81 --- /dev/null +++ b/dlls/rsaenh/handle.h @@ -0,0 +1,75 @@ +/* + * dlls/rsaenh/handle.h + * Support code to manage HANDLE tables. + * + * Copyright 1998 Alexandre Julliard + * Copyright 2002-2004 Mike McCormack for CodeWeavers + * Copyright 2004 Michael Jung + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __WINE_HANDLE_H +#define __WINE_HANDLE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define TABLE_SIZE_INCREMENT 32 + +struct tagOBJECTHDR; +typedef struct tagOBJECTHDR OBJECTHDR; +typedef void (*DESTRUCTOR)(OBJECTHDR *object); +struct tagOBJECTHDR +{ + DWORD dwType; + UINT refcount; + DESTRUCTOR destructor; +}; + +typedef struct tagHANDLETABLEENTRY +{ + OBJECTHDR *pObject; + unsigned int iNextFree; +} HANDLETABLEENTRY; + +typedef struct tagHANDLETABLE +{ + unsigned int iEntries; + unsigned int iFirstFree; + HANDLETABLEENTRY *paEntries; + CRITICAL_SECTION mutex; +} HANDLETABLE; + +int alloc_handle_table (HANDLETABLE **lplpTable); +void init_handle_table (HANDLETABLE *lpTable); +void release_all_handles (HANDLETABLE *lpTable); +int release_handle_table(HANDLETABLE *lpTable); +void destroy_handle_table(HANDLETABLE *lpTable); +int alloc_handle (HANDLETABLE *lpTable, OBJECTHDR *lpObject, unsigned int *lpHandle); +int release_handle (HANDLETABLE *lpTable, unsigned int handle, DWORD dwType); +int copy_handle (HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, unsigned int *copy); +int lookup_handle (HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, OBJECTHDR **lplpObject); +int is_valid_handle (HANDLETABLE *lpTable, unsigned int handle, DWORD dwType); + +unsigned int new_object (HANDLETABLE *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor, + OBJECTHDR **ppObject); + +#ifdef __cplusplus +} +#endif + +#endif /* __WINE_HANDLE_H */ diff --git a/dlls/rsaenh/implossl.c b/dlls/rsaenh/implossl.c new file mode 100644 index 00000000000..fc02bd109ff --- /dev/null +++ b/dlls/rsaenh/implossl.c @@ -0,0 +1,683 @@ +/* + * dlls/rsaenh/implossl.c + * Encapsulating the OpenSSL dependend parts of RSAENH + * + * Copyright (c) 2004 Michael Jung + * + * based on code by Mike McCormack and David Hammerton + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "config.h" + +#include "wine/port.h" +#include "wine/library.h" +#include "wine/debug.h" + +#include "windef.h" +#include "wincrypt.h" + +#include "implossl.h" + +#include <stdio.h> + +WINE_DEFAULT_DEBUG_CHANNEL(crypt); + +#ifndef SONAME_LIBCRYPTO +#define SONAME_LIBCRYPTO "libcrypto.so" +#endif + +static void *libcrypto; + +#define MAKE_FUNCPTR(f) static typeof(f) * p##f + +/* OpenSSL funtions that we use */ +#ifdef HAVE_OPENSSL_MD2_H +MAKE_FUNCPTR(MD2_Init); +MAKE_FUNCPTR(MD2_Update); +MAKE_FUNCPTR(MD2_Final); +#endif +#ifdef HAVE_OPENSSL_RC2_H +MAKE_FUNCPTR(RC2_set_key); +MAKE_FUNCPTR(RC2_ecb_encrypt); +#endif +#ifdef HAVE_OPENSSL_RC4_H +MAKE_FUNCPTR(RC4_set_key); +MAKE_FUNCPTR(RC4); +#endif +#ifdef HAVE_OPENSSL_DES_H +MAKE_FUNCPTR(DES_set_odd_parity); +MAKE_FUNCPTR(DES_set_key_unchecked); +MAKE_FUNCPTR(DES_ecb_encrypt); +MAKE_FUNCPTR(DES_ecb3_encrypt); +#endif +#ifdef HAVE_OPENSSL_RSA_H +MAKE_FUNCPTR(RSA_generate_key); +MAKE_FUNCPTR(RSA_free); +MAKE_FUNCPTR(RSA_size); +MAKE_FUNCPTR(RSA_check_key); +MAKE_FUNCPTR(RSA_public_encrypt); +MAKE_FUNCPTR(RSA_private_encrypt); +MAKE_FUNCPTR(RSAPrivateKey_dup); +MAKE_FUNCPTR(BN_bn2bin); +MAKE_FUNCPTR(BN_bin2bn); +MAKE_FUNCPTR(BN_get_word); +MAKE_FUNCPTR(BN_set_word); +MAKE_FUNCPTR(BN_num_bits); +#endif + +/* Function prototypes copied from dlls/advapi32/crypt_md4.c */ +VOID WINAPI MD4Init( MD4_CTX *ctx ); +VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len ); +VOID WINAPI MD4Final( MD4_CTX *ctx ); +/* Function prototypes copied from dlls/advapi32/crypt_md5.c */ +VOID WINAPI MD5Init( MD5_CTX *ctx ); +VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len ); +VOID WINAPI MD5Final( MD5_CTX *ctx ); +/* Function prototypes copied from dlls/advapi32/crypt_sha.c */ +VOID WINAPI A_SHAInit(PSHA_CTX Context); +VOID WINAPI A_SHAUpdate(PSHA_CTX Context, PCHAR Buffer, UINT BufferSize); +VOID WINAPI A_SHAFinal(PSHA_CTX Context, PULONG Result); + + +BOOL load_lib( void ) +{ +/* FIXME: Is this portable? */ +#if defined HAVE_OPENSSL_MD2_H || defined HAVE_OPENSSL_RC2_H || defined HAVE_OPENSSL_RC4_H || \ + defined HAVE_OPENSSL_DES_H || defined HAVE_OPENSSL_RSA_H + libcrypto = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0); + if (!libcrypto) + { + MESSAGE("Couldn't load %s, RSA encryption not available.\n", SONAME_LIBCRYPTO); + MESSAGE("Install the openssl package if you're have problems.\n"); + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + +#define GETFUNC(x) p##x = wine_dlsym(libcrypto, #x, NULL, 0); + +#ifdef HAVE_OPENSSL_MD2_H + GETFUNC(MD2_Init); + GETFUNC(MD2_Update); + GETFUNC(MD2_Final); +#endif +#ifdef HAVE_OPENSSL_RC2_H + GETFUNC(RC2_set_key); + GETFUNC(RC2_ecb_encrypt); +#endif +#ifdef HAVE_OPENSSL_RC4_H + GETFUNC(RC4_set_key); + GETFUNC(RC4); +#endif +#ifdef HAVE_OPENSSL_DES_H + GETFUNC(DES_set_odd_parity); + GETFUNC(DES_set_key_unchecked); + GETFUNC(DES_ecb_encrypt); + GETFUNC(DES_ecb3_encrypt); +#endif +#ifdef HAVE_OPENSSL_RSA_H + GETFUNC(RSA_generate_key); + GETFUNC(RSA_free); + GETFUNC(RSA_size); + GETFUNC(RSA_check_key); + GETFUNC(RSA_public_encrypt); + GETFUNC(RSA_private_encrypt); + GETFUNC(RSAPrivateKey_dup); + GETFUNC(BN_bn2bin); + GETFUNC(BN_bin2bn); + GETFUNC(BN_get_word); + GETFUNC(BN_set_word); + GETFUNC(BN_num_bits); +#endif + +#endif /* ifdef have any openssl header */ + return TRUE; +} + +BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_MD2_H + case CALG_MD2: + if (!pMD2_Init) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pMD2_Init(&pHashContext->md2); + break; +#endif + case CALG_MD4: + MD4Init(&pHashContext->md4); + break; + + case CALG_MD5: + MD5Init(&pHashContext->md5); + break; + + case CALG_SHA: + A_SHAInit(&pHashContext->sha); + break; + + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData, + DWORD dwDataLen) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_MD2_H + case CALG_MD2: + if (!pMD2_Update) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pMD2_Update(&pHashContext->md2, pbData, dwDataLen); + break; +#endif + case CALG_MD4: + MD4Update(&pHashContext->md4, pbData, dwDataLen); + break; + + case CALG_MD5: + MD5Update(&pHashContext->md5, pbData, dwDataLen); + break; + + case CALG_SHA: + A_SHAUpdate(&pHashContext->sha, (PCHAR)pbData, dwDataLen); + break; + + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_MD2_H + case CALG_MD2: + if (!pMD2_Final) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pMD2_Final(pbHashValue, &pHashContext->md2); + break; +#endif + case CALG_MD4: + MD4Final(&pHashContext->md4); + memcpy(pbHashValue, pHashContext->md4.digest, 16); + break; + + case CALG_MD5: + MD5Final(&pHashContext->md5); + memcpy(pbHashValue, pHashContext->md5.digest, 16); + break; + + case CALG_SHA: + A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue); + break; + + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext, + HASH_CONTEXT *pDestHashContext) +{ + memcpy(pDestHashContext, pSrcHashContext, sizeof(HASH_CONTEXT)); + + return TRUE; +} + +BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_RSA_H + case CALG_RSA_KEYX: + case CALG_RSA_SIGN: + if (!pRSA_generate_key) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pKeyContext->rsa = pRSA_generate_key((int)dwKeyLen*8, 65537, NULL, NULL); + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_RSA_H + case CALG_RSA_KEYX: + case CALG_RSA_SIGN: + if (!pRSA_free) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + if (pKeyContext->rsa) pRSA_free(pKeyContext->rsa); + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, DWORD dwSaltLen, + BYTE *abKeyValue) +{ + switch (aiAlgid) + { +#ifdef HAVE_OPENSSL_RC4_H + case CALG_RC4: + if (!pRC4_set_key) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pRC4_set_key(&pKeyContext->rc4, dwKeyLen + dwSaltLen, abKeyValue); + break; +#endif +#ifdef HAVE_OPENSSL_RC2_H + case CALG_RC2: + if (!pRC2_set_key) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pRC2_set_key(&pKeyContext->rc2, dwKeyLen + dwSaltLen, abKeyValue, dwKeyLen * 8); + break; +#endif +#ifdef HAVE_OPENSSL_DES_H + case CALG_3DES: + if (!pDES_set_odd_parity || !pDES_set_key_unchecked) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_set_odd_parity(&((DES_cblock*)abKeyValue)[2]); + pDES_set_key_unchecked(&((DES_cblock*)abKeyValue)[2], &pKeyContext->des[2]); + + case CALG_3DES_112: + if (!pDES_set_odd_parity || !pDES_set_key_unchecked) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_set_odd_parity(&((DES_cblock*)abKeyValue)[1]); + pDES_set_key_unchecked(&((DES_cblock*)abKeyValue)[1], &pKeyContext->des[1]); + + case CALG_DES: + if (!pDES_set_odd_parity || !pDES_set_key_unchecked) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_set_odd_parity((DES_cblock*)abKeyValue); + pDES_set_key_unchecked((DES_cblock*)abKeyValue, &pKeyContext->des[0]); + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext, + KEY_CONTEXT *pDestKeyContext) +{ + switch (aiAlgid) + { + case CALG_RC4: + case CALG_RC2: + case CALG_3DES: + case CALG_3DES_112: + case CALG_DES: + memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT)); + break; +#ifdef HAVE_OPENSSL_RSA_H + case CALG_RSA_KEYX: + case CALG_RSA_SIGN: + if (!pRSAPrivateKey_dup) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDestKeyContext->rsa = pRSAPrivateKey_dup(pSrcKeyContext->rsa); + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +#ifdef HAVE_OPENSSL_RSA_H +static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) { + BYTE swap; + DWORD i; + + for (i=0; i<dwLen/2; i++) { + swap = pbData[i]; + pbData[i] = pbData[dwLen-i-1]; + pbData[dwLen-i-1] = swap; + } +} +#endif + +BOOL encrypt_block_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out, + DWORD enc) +{ +#ifdef HAVE_OPENSSL_RSA_H + int cLen; +#endif + + switch (aiAlgid) { +#ifdef HAVE_OPENSSL_RC2_H + case CALG_RC2: + if (!pRC2_ecb_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pRC2_ecb_encrypt(in, out, &pKeyContext->rc2, enc ? RC2_ENCRYPT : RC2_DECRYPT); + break; +#endif +#ifdef HAVE_OPENSSL_DES_H + case CALG_DES: + if (!pDES_ecb_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_ecb_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &pKeyContext->des[0], + enc ? DES_ENCRYPT : DES_DECRYPT); + break; + + case CALG_3DES_112: + if (!pDES_ecb3_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, + &pKeyContext->des[0], &pKeyContext->des[1], &pKeyContext->des[0], + enc ? DES_ENCRYPT : DES_DECRYPT); + break; + + case CALG_3DES: + if (!pDES_ecb3_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pDES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, + &pKeyContext->des[0], &pKeyContext->des[1], &pKeyContext->des[2], + enc ? DES_ENCRYPT : DES_DECRYPT); + break; +#endif +#ifdef HAVE_OPENSSL_RSA_H + case CALG_RSA_KEYX: + if (!pBN_num_bits || !pRSA_public_encrypt || !pRSA_private_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + cLen = pBN_num_bits(pKeyContext->rsa->n)/8; + if (enc) { + pRSA_public_encrypt(cLen, in, out, pKeyContext->rsa, RSA_NO_PADDING); + reverse_bytes((BYTE*)in, cLen); + } else { + reverse_bytes((BYTE*)in, cLen); + pRSA_private_encrypt(cLen, in, out, pKeyContext->rsa, RSA_NO_PADDING); + } + break; + + case CALG_RSA_SIGN: + if (!pBN_num_bits || !pRSA_public_encrypt || !pRSA_private_encrypt) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + cLen = pBN_num_bits(pKeyContext->rsa->n)/8; + if (enc) { + pRSA_private_encrypt(cLen, in, out, pKeyContext->rsa, RSA_NO_PADDING); + reverse_bytes((BYTE*)in, cLen); + } else { + reverse_bytes((BYTE*)in, cLen); + pRSA_public_encrypt(cLen, in, out, pKeyContext->rsa, RSA_NO_PADDING); + } + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen) +{ + switch (aiAlgid) { +#ifdef HAVE_OPENSSL_RC4_H + case CALG_RC4: + if (!pRC4) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + pRC4(&pKeyContext->rc4, (unsigned long)dwLen, stream, stream); + break; +#endif + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return TRUE; +} + +BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen) +{ + FILE *dev_random; + + /* FIXME: /dev/urandom does not provide random numbers of a sufficient + * quality for cryptographic applications. /dev/random is much better, + * but it blocks if the kernel has not yet collected enough entropy for + * the request, which will suspend the calling thread for an indefinite + * amount of time. */ + dev_random = fopen("/dev/urandom", "r"); + if (dev_random) + { + if (fread(pbBuffer, (size_t)dwLen, 1, dev_random) == 1) + { + fclose(dev_random); + return TRUE; + } + fclose(dev_random); + } + SetLastError(NTE_FAIL); + return FALSE; +} + +BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp) +{ +#ifdef HAVE_OPENSSL_RSA_H + if (!pBN_bn2bin || !pBN_get_word) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + + pBN_bn2bin(pKeyContext->rsa->n, pbDest); + reverse_bytes(pbDest, dwKeyLen); + *pdwPubExp = (DWORD)pBN_get_word(pKeyContext->rsa->e); + + return TRUE; +#else + SetLastError(NTE_FAIL); + return FALSE; +#endif +} + +BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD dwPubExp) +{ +#ifdef HAVE_OPENSSL_RSA_H + BYTE *pbTemp; + + if (!pBN_bin2bn || !pBN_set_word) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + + pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwKeyLen); + if (!pbTemp) return FALSE; + memcpy(pbTemp, pbSrc, dwKeyLen); + reverse_bytes(pbTemp, dwKeyLen); + pBN_bin2bn(pbTemp, dwKeyLen, pKeyContext->rsa->n); + HeapFree(GetProcessHeap(), 0, pbTemp); + + pBN_set_word(pKeyContext->rsa->e, (BN_ULONG)dwPubExp); + + return TRUE; +#else + SetLastError(NTE_FAIL); + return FALSE; +#endif +} + +BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD *pdwPubExp) +{ +#ifdef HAVE_OPENSSL_RSA_H + if (!pBN_bn2bin || !pBN_get_word) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + + pBN_bn2bin(pKeyContext->rsa->n, pbDest); + reverse_bytes(pbDest, dwKeyLen); + pbDest += dwKeyLen; + pBN_bn2bin(pKeyContext->rsa->p, pbDest); + reverse_bytes(pbDest, (dwKeyLen+1)>>1); + pbDest += (dwKeyLen+1)>>1; + pBN_bn2bin(pKeyContext->rsa->q, pbDest); + reverse_bytes(pbDest, (dwKeyLen+1)>>1); + pbDest += (dwKeyLen+1)>>1; + pBN_bn2bin(pKeyContext->rsa->dmp1, pbDest); + reverse_bytes(pbDest, (dwKeyLen+1)>>1); + pbDest += (dwKeyLen+1)>>1; + pBN_bn2bin(pKeyContext->rsa->dmq1, pbDest); + reverse_bytes(pbDest, (dwKeyLen+1)>>1); + pbDest += (dwKeyLen+1)>>1; + pBN_bn2bin(pKeyContext->rsa->iqmp, pbDest); + reverse_bytes(pbDest, (dwKeyLen+1)>>1); + pbDest += (dwKeyLen+1)>>1; + pBN_bn2bin(pKeyContext->rsa->d, pbDest); + reverse_bytes(pbDest, dwKeyLen); + *pdwPubExp = (DWORD)pBN_get_word(pKeyContext->rsa->e); + + return TRUE; +#else + SetLastError(NTE_FAIL); + return FALSE; +#endif +} + +BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD dwPubExp) +{ +#ifdef HAVE_OPENSSL_RSA_H + BYTE *pbTemp, *pbBigNum; + + if (!pBN_bin2bn || !pBN_set_word) + { + SetLastError(NTE_PROVIDER_DLL_FAIL); + return FALSE; + } + + pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1)); + if (!pbTemp) return FALSE; + memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1)); + pbBigNum = pbTemp; + + reverse_bytes(pbBigNum, dwKeyLen); + pBN_bin2bn(pbBigNum, dwKeyLen, pKeyContext->rsa->n); + pbBigNum += dwKeyLen; + reverse_bytes(pbBigNum, (dwKeyLen+1)>>1); + pBN_bin2bn(pbBigNum, (dwKeyLen+1)>>1, pKeyContext->rsa->p); + pbBigNum += (dwKeyLen+1)>>1; + reverse_bytes(pbBigNum, (dwKeyLen+1)>>1); + pBN_bin2bn(pbBigNum, (dwKeyLen+1)>>1, pKeyContext->rsa->q); + pbBigNum += (dwKeyLen+1)>>1; + reverse_bytes(pbBigNum, (dwKeyLen+1)>>1); + pBN_bin2bn(pbBigNum, (dwKeyLen+1)>>1, pKeyContext->rsa->dmp1); + pbBigNum += (dwKeyLen+1)>>1; + reverse_bytes(pbBigNum, (dwKeyLen+1)>>1); + pBN_bin2bn(pbBigNum, (dwKeyLen+1)>>1, pKeyContext->rsa->dmq1); + pbBigNum += (dwKeyLen+1)>>1; + reverse_bytes(pbBigNum, (dwKeyLen+1)>>1); + pBN_bin2bn(pbBigNum, (dwKeyLen+1)>>1, pKeyContext->rsa->iqmp); + pbBigNum += (dwKeyLen+1)>>1; + reverse_bytes(pbBigNum, dwKeyLen); + pBN_bin2bn(pbBigNum, dwKeyLen, pKeyContext->rsa->d); + pBN_set_word(pKeyContext->rsa->e, (BN_ULONG)dwPubExp); + + HeapFree(GetProcessHeap(), 0, pbTemp); + + return TRUE; +#else + SetLastError(NTE_FAIL); + return FALSE; +#endif +} diff --git a/dlls/rsaenh/implossl.h b/dlls/rsaenh/implossl.h new file mode 100644 index 00000000000..0f63b63ac7d --- /dev/null +++ b/dlls/rsaenh/implossl.h @@ -0,0 +1,129 @@ +/* + * dlls/rsaenh/implossl.h + * Encapsulating the OpenSSL dependend parts of RSABASE + * + * Copyright (c) 2004 Michael Jung + * + * based on code by Mike McCormack and David Hammerton + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __WINE_IMPLOSSL_H +#define __WINE_IMPLOSSL_H + +#ifdef HAVE_OPENSSL_MD2_H +#include <openssl/md2.h> +#endif + +#ifdef HAVE_OPENSSL_RC2_H +#include <openssl/rc2.h> +#endif + +#ifdef HAVE_OPENSSL_RC4_H +#include <openssl/rc4.h> +#endif + +#ifdef HAVE_OPENSSL_DES_H +#include <openssl/des.h> +#endif + +#ifdef HAVE_OPENSSL_RSA_H +#include <openssl/rsa.h> +#endif + +/* Next typedef copied from dlls/advapi32/crypt_md4.c */ +typedef struct tagMD4_CTX { + unsigned int buf[4]; + unsigned int i[2]; + unsigned char in[64]; + unsigned char digest[16]; +} MD4_CTX; + +/* Next typedef copied from dlls/advapi32/crypt_md5.c */ +typedef struct tagMD5_CTX +{ + unsigned int i[2]; + unsigned int buf[4]; + unsigned char in[64]; + unsigned char digest[16]; +} MD5_CTX; + +/* Next typedef copied form dlls/advapi32/crypt_sha.c */ +typedef struct tagSHA_CTX +{ + ULONG Unknown[6]; + ULONG State[5]; + ULONG Count[2]; + UCHAR Buffer[64]; +} SHA_CTX, *PSHA_CTX; + +typedef union tagHASH_CONTEXT { +#ifdef HAVE_OPENSSL_MD2_H + MD2_CTX md2; +#endif + MD4_CTX md4; + MD5_CTX md5; + SHA_CTX sha; +} HASH_CONTEXT; + +typedef union tagKEY_CONTEXT { +#ifdef HAVE_OPENSSL_RC2_H + RC2_KEY rc2; +#endif +#ifdef HAVE_OPENSSL_RC4_H + RC4_KEY rc4; +#endif +#ifdef HAVE_OPENSSL_DES_H + DES_key_schedule des[3]; +#endif +#ifdef HAVE_OPENSSL_RSA_H + RSA *rsa; +#endif + DWORD dwDummy; +} KEY_CONTEXT; + +BOOL load_lib(void); + +BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext); +BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData, + DWORD dwDataLen); +BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue); +BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext, + HASH_CONTEXT *pDestHashContext); + +BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen); +BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext); +BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, DWORD dwSaltLen, + BYTE *abKeyValue); +BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext, + KEY_CONTEXT *pDestKeyContext); + +BOOL encrypt_block_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, CONST BYTE *pbIn, BYTE *pbOut, + DWORD enc); +BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *pbInOut, DWORD dwLen); + +BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD *pdwPubExp); +BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD dwPubExp); +BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD *pdwPubExp); +BOOL import_private_key_impl(CONST BYTE* pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, + DWORD dwPubExp); + +BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen); + +#endif /* __WINE_IMPLOSSL_H */ diff --git a/dlls/rsaenh/rsaenh.c b/dlls/rsaenh/rsaenh.c new file mode 100644 index 00000000000..a45cf4ea216 --- /dev/null +++ b/dlls/rsaenh/rsaenh.c @@ -0,0 +1,2658 @@ +/* + * dlls/rsaenh/rsaenh.c + * RSAENH - RSA encryption for Wine + * + * Copyright 2002 TransGaming Technologies (David Hammerton) + * Copyright 2004 Mike McCormack for CodeWeavers + * Copyright 2004 Michael Jung + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" +#include "wine/port.h" +#include "wine/library.h" +#include "wine/debug.h" + +#include <stdarg.h> +#include <stdio.h> + +#include "windef.h" +#include "winbase.h" +#include "winreg.h" +#include "wincrypt.h" +#include "lmcons.h" +#include "handle.h" +#include "implossl.h" + +WINE_DEFAULT_DEBUG_CHANNEL(crypt); + +/****************************************************************************** + * CRYPTHASH - hash objects + */ +#define RSAENH_MAGIC_HASH 0x85938417u +#define RSAENH_MAX_HASH_SIZE 36 +#define RSAENH_HASHSTATE_IDLE 0 +#define RSAENH_HASHSTATE_HASHING 1 +#define RSAENH_HASHSTATE_FINISHED 2 +typedef struct tagCRYPTHASH +{ + OBJECTHDR header; + ALG_ID aiAlgid; + HCRYPTKEY hKey; + HCRYPTPROV hProv; + DWORD dwHashSize; + DWORD dwState; + HASH_CONTEXT context; + BYTE abHashValue[RSAENH_MAX_HASH_SIZE]; + PHMAC_INFO pHMACInfo; +} CRYPTHASH; + +/****************************************************************************** + * CRYPTKEY - key objects + */ +#define RSAENH_MAGIC_KEY 0x73620457u +#define RSAENH_MAX_KEY_SIZE 24 +#define RSAENH_MAX_BLOCK_SIZE 24 +#define RSAENH_KEYSTATE_IDLE 0 +#define RSAENH_KEYSTATE_ENCRYPTING 1 +#define RSAENH_KEYSTATE_DECRYPTING 2 +typedef struct tagCRYPTKEY +{ + OBJECTHDR header; + ALG_ID aiAlgid; + HCRYPTPROV hProv; + DWORD dwMode; + DWORD dwModeBits; + DWORD dwPermissions; + DWORD dwKeyLen; + DWORD dwSaltLen; + DWORD dwBlockLen; + DWORD dwState; + KEY_CONTEXT context; + BYTE abKeyValue[RSAENH_MAX_KEY_SIZE]; + BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE]; + BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE]; +} CRYPTKEY; + +/****************************************************************************** + * KEYCONTAINER - key containers + */ +#define RSAENH_PERSONALITY_BASE 0u +#define RSAENH_PERSONALITY_STRONG 1u +#define RSAENH_PERSONALITY_ENHANCED 2u + +#define RSAENH_MAGIC_CONTAINER 0x26384993u +typedef struct tagKEYCONTAINER +{ + OBJECTHDR header; + DWORD dwMode; + DWORD dwPersonality; + DWORD dwEnumAlgsCtr; + CHAR szName[MAX_PATH]; + CHAR szProvName[MAX_PATH]; + HCRYPTKEY hKeyExchangeKeyPair; + HCRYPTKEY hSignatureKeyPair; +} KEYCONTAINER; + +/****************************************************************************** + * Some magic constants + */ +#define RSAENH_ENCRYPT 1 +#define RSAENH_DECRYPT 0 +#define RSAENH_HMAC_DEF_IPAD_CHAR 0x36 +#define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c +#define RSAENH_HMAC_DEF_PAD_LEN 64 +#define RSAENH_DES_EFFECTIVE_KEYLEN 56 +#define RSAENH_DES_STORAGE_KEYLEN 64 +#define RSAENH_3DES112_EFFECTIVE_KEYLEN 112 +#define RSAENH_3DES112_STORAGE_KEYLEN 128 +#define RSAENH_3DES_EFFECTIVE_KEYLEN 168 +#define RSAENH_3DES_STORAGE_KEYLEN 192 +#define RSAENH_MAGIC_RSA2 0x32415352 +#define RSAENH_MAGIC_RSA1 0x31415352 +#define RSAENH_PKC_BLOCKTYPE 0x02 + +#define RSAENH_MIN(a,b) ((a)<(b)?(a):(b)) +/****************************************************************************** + * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities. + */ +#define RSAENH_MAX_ENUMALGS 14 +PROV_ENUMALGS_EX aProvEnumAlgsEx[3][RSAENH_MAX_ENUMALGS+1] = +{ + { + {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"}, + {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"}, + {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"}, + {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"}, + {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"}, + {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"}, + {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"}, + {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"}, + {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"}, + {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"}, + {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"}, + {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"}, + {0, 0, 0, 0,0, 1,"", 1,""} + }, + { + {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"}, + {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"}, + {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"}, + {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"}, + {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"}, + {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"}, + {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"}, + {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"}, + {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"}, + {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"}, + {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"}, + {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"}, + {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"}, + {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"}, + {0, 0, 0, 0,0, 1,"", 1,""} + }, + { + {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"}, + {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"}, + {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"}, + {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"}, + {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"}, + {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"}, + {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"}, + {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"}, + {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"}, + {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"}, + {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"}, + {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"}, + {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"}, + {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"}, + {0, 0, 0, 0,0, 1,"", 1,""} + } +}; + +/****************************************************************************** + * API forward declarations + */ +BOOL WINAPI +RSAENH_CPGetKeyParam( + HCRYPTPROV hProv, + HCRYPTKEY hKey, + DWORD dwParam, + BYTE *pbData, + DWORD *pdwDataLen, + DWORD dwFlags +); + +BOOL WINAPI +RSAENH_CPEncrypt( + HCRYPTPROV hProv, + HCRYPTKEY hKey, + HCRYPTHASH hHash, + BOOL Final, + DWORD dwFlags, + BYTE *pbData, + DWORD *pdwDataLen, + DWORD dwBufLen +); + +BOOL WINAPI +RSAENH_CPGetHashParam( + HCRYPTPROV hProv, + HCRYPTHASH hHash, + DWORD dwParam, + BYTE *pbData, + DWORD *pdwDataLen, + DWORD dwFlags +); + +BOOL WINAPI +RSAENH_CPExportKey( + HCRYPTPROV hProv, + HCRYPTKEY hKey, + HCRYPTKEY hPubKey, + DWORD dwBlobType, + DWORD dwFlags, + BYTE *pbData, + DWORD *pdwDataLen +); + +BOOL WINAPI +RSAENH_CPImportKey( + HCRYPTPROV hProv, + CONST BYTE *pbData, + DWORD dwDataLen, + HCRYPTKEY hPubKey, + DWORD dwFlags, + HCRYPTKEY *phKey +); + +/****************************************************************************** + * CSP's handle table (used by all acquired key containers) + */ +static HANDLETABLE handle_table; + +/****************************************************************************** + * DllMain (RSAENH.@) + * + * Initializes and destroys the handle table for the CSP's handles. + */ +int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) +{ + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + init_handle_table(&handle_table); + break; + + case DLL_PROCESS_DETACH: + destroy_handle_table(&handle_table); + break; + } + return 1; +} + +/****************************************************************************** + * copy_param [Internal] + * + * Helper function that supports the standard WINAPI protocol for querying data + * of dynamic size. + * + * PARAMS + * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough. + * May be NUL if the required buffer size is to be queried only. + * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer + * Out: Size of parameter pbParam + * pbParam [I] Parameter value. + * dwParamSize [I] Size of pbParam + * + * RETURN + * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL) + * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA + */ +static inline BOOL copy_param( + BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize) +{ + if (pbBuffer) + { + if (dwParamSize > *pdwBufferSize) + { + SetLastError(ERROR_MORE_DATA); + *pdwBufferSize = dwParamSize; + return FALSE; + } + memcpy(pbBuffer, pbParam, dwParamSize); + } + *pdwBufferSize = dwParamSize; + return TRUE; +} + +/****************************************************************************** + * get_algid_info [Internal] + * + * Query CSP capabilities for a given crypto algorithm. + * + * PARAMS + * pKeyContainer [I] Pointer to a key container of the CSP whose capabilities are to be queried. + * algid [I] Identifier of the crypto algorithm about which information is requested. + * + * RETURNS + * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm. + * Failure: NULL (algid not supported) + */ +static inline const PROV_ENUMALGS_EX* get_algid_info(KEYCONTAINER *pKeyContainer, ALG_ID algid) { + PROV_ENUMALGS_EX *iterator; + + for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) { + if (iterator->aiAlgid == algid) return iterator; + } + + return NULL; +} + +/****************************************************************************** + * free_hmac_info [Internal] + * + * Deeply free an HMAC_INFO struct. + * + * PARAMS + * hmac_info [I] Pointer to the HMAC_INFO struct to be freed. + * + * NOTES + * See Internet RFC 2104 for details on the HMAC algorithm. + */ +static inline void free_hmac_info(PHMAC_INFO hmac_info) { + if (!hmac_info) return; + if (hmac_info->pbInnerString) HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString); + if (hmac_info->pbOuterString) HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString); + HeapFree(GetProcessHeap(), 0, hmac_info); +} + +/****************************************************************************** + * copy_hmac_info [Internal] + * + * Deeply copy an HMAC_INFO struct + * + * PARAMS + * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored. + * src [I] Pointer to the HMAC_INFO struct to be copied. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * See Internet RFC 2104 for details on the HMAC algorithm. + */ +static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) { + if (!src) return FALSE; + *dst = (PHMAC_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO)); + if (!*dst) return FALSE; + memcpy(*dst, src, sizeof(HMAC_INFO)); + (*dst)->pbInnerString = NULL; + (*dst)->pbOuterString = NULL; + if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN; + (*dst)->pbInnerString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString); + if (!(*dst)->pbInnerString) { + free_hmac_info(*dst); + return FALSE; + } + if (src->cbInnerString) + memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString); + else + memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN); + if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN; + (*dst)->pbOuterString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString); + if (!(*dst)->pbOuterString) { + free_hmac_info(*dst); + return FALSE; + } + if (src->cbOuterString) + memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString); + else + memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN); + return TRUE; +} + +/****************************************************************************** + * destroy_hash [Internal] + * + * Destructor for hash objects + * + * PARAMS + * pCryptHash [I] Pointer to the hash object to be destroyed. + * Will be invalid after function returns! + */ +static void destroy_hash(OBJECTHDR *pCryptHash) +{ + free_hmac_info(((CRYPTHASH*)pCryptHash)->pHMACInfo); + HeapFree(GetProcessHeap(), 0, pCryptHash); +} + +/****************************************************************************** + * init_hash [Internal] + * + * Initialize (or reset) a hash object + * + * PARAMS + * pKeyContainer [I] Pointer to the key container the hash object belongs to. + * pCryptHash [I] The hash object to be initialized. + */ +static inline BOOL init_hash(KEYCONTAINER *pKeyContainer, CRYPTHASH *pCryptHash) { + DWORD dwLen; + const PROV_ENUMALGS_EX *pAlgInfo; + + switch (pCryptHash->aiAlgid) + { + case CALG_HMAC: + if (pCryptHash->pHMACInfo) { + pAlgInfo = get_algid_info(pKeyContainer, pCryptHash->pHMACInfo->HashAlgid); + if (pAlgInfo) pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3; + return init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context); + } + return TRUE; + + case CALG_MAC: + dwLen = sizeof(DWORD); + RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN, + (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0); + pCryptHash->dwHashSize >>= 3; + return TRUE; + + default: + return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context); + } +} + +/****************************************************************************** + * update_hash [Internal] + * + * Hashes the given data and updates the hash object's state accordingly + * + * PARAMS + * pCryptHash [I] Hash object to be updated. + * pbData [I] Pointer to data stream to be hashed. + * dwDataLen [I] Length of data stream. + */ +static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) { + BYTE *pbTemp; + + switch (pCryptHash->aiAlgid) + { + case CALG_HMAC: + if (pCryptHash->pHMACInfo) + update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context, + pbData, dwDataLen); + break; + + case CALG_MAC: + pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwDataLen); + if (!pbTemp) return; + memcpy(pbTemp, pbData, dwDataLen); + RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0, + pbTemp, &dwDataLen, dwDataLen); + HeapFree(GetProcessHeap(), 0, pbTemp); + break; + + default: + update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen); + } +} + +/****************************************************************************** + * finalize_hash [Internal] + * + * Finalizes the hash, after all data has been hashed with update_hash. + * No additional data can be hashed afterwards until the hash gets initialized again. + * + * PARAMS + * pCryptHash [I] Hash object to be finalized. + */ +static inline void finalize_hash(CRYPTHASH *pCryptHash) { + DWORD dwDataLen; + + switch (pCryptHash->aiAlgid) + { + case CALG_HMAC: + if (pCryptHash->pHMACInfo) + finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context, + pCryptHash->abHashValue); + break; + + case CALG_MAC: + dwDataLen = 0; + RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0, + pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize); + break; + + default: + finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue); + } +} + +/****************************************************************************** + * destroy_key [Internal] + * + * Destructor for key objects + * + * PARAMS + * pCryptKey [I] Pointer to the key object to be destroyed. + * Will be invalid after function returns! + */ +static void destroy_key(OBJECTHDR *pCryptKey) +{ + free_key_impl(((CRYPTKEY*)pCryptKey)->aiAlgid, &((CRYPTKEY*)pCryptKey)->context); + HeapFree(GetProcessHeap(), 0, pCryptKey); +} + +/****************************************************************************** + * setup_key [Internal] + * + * Initialize (or reset) a key object + * + * PARAMS + * pCryptKey [I] The key object to be initialized. + */ +static inline void setup_key(CRYPTKEY *pCryptKey) { + pCryptKey->dwState = RSAENH_KEYSTATE_IDLE; + memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector)); + setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen, + pCryptKey->dwSaltLen, pCryptKey->abKeyValue); +} + +/****************************************************************************** + * new_key [Internal] + * + * Creates a new key object. If neither pbKey nor hHash is given a random key + * will be generated. + * + * PARAMS + * hProv [I] Handle to the provider to which the created key will belong. + * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid. + * dwFlags [I] Upper 16 bits give the key length. + * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT + * pbKey [I] Byte stream to be used as key material. May be NULL. + * hHash [I] Handle to a hash object whose value will be used as key material. May be zero. + * + * RETURNS + * Success: Handle to the created key. + * Failure: INVALID_HANDLE_VALUE + */ +static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, BYTE *pbKey, + HCRYPTHASH hHash) +{ + KEYCONTAINER *pKeyContainer; + HCRYPTKEY hCryptKey; + CRYPTKEY *pCryptKey; + CRYPTHASH *pCryptHash; + DWORD dwKeyLen = HIWORD(dwFlags), i; + const PROV_ENUMALGS_EX *peaAlgidInfo; + BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2]; + + if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + + /* + * Retrieve the CSP's capabilities for the given ALG_ID value + */ + peaAlgidInfo = get_algid_info(pKeyContainer, aiAlgid); + if (!peaAlgidInfo) { + SetLastError(NTE_BAD_ALGID); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + + /* + * Assume the default key length, if none is specified explicitly + */ + if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen; + + /* + * Check if the requested key length is supported by the current CSP. + * Adjust key length's for DES algorithms. + */ + switch (aiAlgid) { + case CALG_DES: + if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) { + dwKeyLen = RSAENH_DES_STORAGE_KEYLEN; + } + if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) { + SetLastError(NTE_BAD_FLAGS); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + break; + + case CALG_3DES_112: + if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) { + dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN; + } + if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) { + SetLastError(NTE_BAD_FLAGS); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + break; + + case CALG_3DES: + if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) { + dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN; + } + if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) { + SetLastError(NTE_BAD_FLAGS); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + break; + + default: + if (dwKeyLen % 8 || + dwKeyLen > peaAlgidInfo->dwMaxLen || + dwKeyLen < peaAlgidInfo->dwMinLen) + { + SetLastError(NTE_BAD_FLAGS); + return (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + } + + /* + * If a valid hash handle is supplied, we derive the key material from the hash. + * If the hash value is not large enough for the claimed key, we have to construct + * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey. + */ + if (lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pCryptHash)) { + DWORD dwLen = RSAENH_MAX_HASH_SIZE; + + RSAENH_CPGetHashParam(pCryptHash->hProv, hHash, HP_HASHVAL, abHashValue, &dwLen, 0); + + if (dwLen < (dwKeyLen >> 3)) { + BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN], old_hashval[RSAENH_MAX_HASH_SIZE]; + + memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE); + + for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) { + pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0); + pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0); + } + + init_hash(pKeyContainer, pCryptHash); + update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN); + finalize_hash(pCryptHash); + memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize); + + init_hash(pKeyContainer, pCryptHash); + update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN); + finalize_hash(pCryptHash); + memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue, + pCryptHash->dwHashSize); + + memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE); + } + + pbKey = abHashValue; + } + + hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, + destroy_key, (OBJECTHDR**)&pCryptKey); + if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE) + { + pCryptKey->aiAlgid = aiAlgid; + pCryptKey->hProv = hProv; + pCryptKey->dwModeBits = 0; + pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE | + CRYPT_MAC; + pCryptKey->dwKeyLen = dwKeyLen >> 3; + if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT))) + pCryptKey->dwSaltLen = 16 - pCryptKey->dwKeyLen; + else + pCryptKey->dwSaltLen = 0; + memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue)); + if (pbKey) memcpy(pCryptKey->abKeyValue, pbKey, RSAENH_MIN(pCryptKey->dwKeyLen, + sizeof(pCryptKey->abKeyValue))); + memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector)); + + switch(aiAlgid) + { + case CALG_RC4: + pCryptKey->dwBlockLen = 0; + pCryptKey->dwMode = 0; + break; + + case CALG_RC2: + case CALG_DES: + case CALG_3DES_112: + case CALG_3DES: + pCryptKey->dwBlockLen = 8; + pCryptKey->dwMode = CRYPT_MODE_CBC; + break; + + case CALG_RSA_KEYX: + case CALG_RSA_SIGN: + pCryptKey->dwBlockLen = dwKeyLen >> 3; + pCryptKey->dwMode = 0; + break; + } + + new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen); + setup_key(pCryptKey); + } + + return hCryptKey; +} + +/****************************************************************************** + * destroy_key_container [Internal] + * + * Destructor for key containers. The user's signature and key exchange private + * keys are stored in the registry _IN_PLAINTEXT_. + * + * PARAMS + * pObjectHdr [I] Pointer to the key container to be destroyed. + */ +static void destroy_key_container(OBJECTHDR *pObjectHdr) +{ + KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr; + CRYPTKEY *pKey; + CHAR szRSABase[MAX_PATH]; + HKEY hKey; + DWORD dwLen; + BYTE *pbKey; + + /* On WinXP, persistent keys are stored in a file located at: + * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string + */ + sprintf(szRSABase, "Software\\Wine\\Crypto\\RSA\\%s", pKeyContainer->szName); + + if (RegCreateKeyExA(HKEY_CURRENT_USER, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE, + KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) + { + if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY, + (OBJECTHDR**)&pKey)) + { + if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0, + PRIVATEKEYBLOB, 0, 0, &dwLen)) + { + pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen); + if (pbKey) + { + if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0, + PRIVATEKEYBLOB, 0, pbKey, &dwLen)) + { + RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY, pbKey, dwLen); + } + HeapFree(GetProcessHeap(), 0, pbKey); + } + } + release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair, + RSAENH_MAGIC_KEY); + } + + if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY, + (OBJECTHDR**)&pKey)) + { + if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0, PRIVATEKEYBLOB, + 0, 0, &dwLen)) + { + pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen); + if (pbKey) + { + if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0, + PRIVATEKEYBLOB, 0, pbKey, &dwLen)) + { + RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY, pbKey, dwLen); + } + HeapFree(GetProcessHeap(), 0, pbKey); + } + } + release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair, + RSAENH_MAGIC_KEY); + } + + RegCloseKey(hKey); + } + + HeapFree( GetProcessHeap(), 0, pKeyContainer ); +} + +/****************************************************************************** + * new_key_container [Internal] + * + * Create a new key container. The personality (RSA Base, Strong or Enhanced CP) + * of the CSP is determined via the pVTable->pszProvName string. + * + * PARAMS + * pszContainerName [I] Name of the key container. + * pVTable [I] Callback functions and context info provided by the OS + * + * RETURNS + * Success: Handle to the new key container. + * Failure: INVALID_HANDLE_VALUE + */ +static HCRYPTPROV new_key_container(PCHAR pszContainerName, PVTableProvStruc pVTable) +{ + KEYCONTAINER *pKeyContainer; + HCRYPTPROV hKeyContainer; + + hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER, + destroy_key_container, (OBJECTHDR**)&pKeyContainer); + if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE) + { + strncpy(pKeyContainer->szName, pszContainerName, MAX_PATH); + pKeyContainer->szName[MAX_PATH-1] = '\0'; + pKeyContainer->dwMode = 0; + pKeyContainer->dwEnumAlgsCtr = 0; + pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE; + pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE; + if (pVTable && pVTable->pszProvName) { + strncpy(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH); + pKeyContainer->szProvName[MAX_PATH-1] = '\0'; + if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) { + pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE; + } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) { + pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED; + } else { + pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG; + } + } + } + + return hKeyContainer; +} + +/****************************************************************************** + * read_key_container [Internal] + * + * Tries to read the persistent state of the key container (mainly the signature + * and key exchange private keys) given by pszContainerName. + * + * PARAMS + * pszContainerName [I] Name of the key container to read from the registry + * pVTable [I] Pointer to context data provided by the operating system + * + * RETURNS + * Success: Handle to the key container read from the registry + * Failure: INVALID_HANDLE_VALUE + */ +static HCRYPTPROV read_key_container(PCHAR pszContainerName, PVTableProvStruc pVTable) +{ + CHAR szRSABase[MAX_PATH]; + BYTE *pbKey; + HKEY hKey; + DWORD dwValueType, dwLen; + KEYCONTAINER *pKeyContainer; + HCRYPTPROV hKeyContainer; + + sprintf(szRSABase, "Software\\Wine\\Crypto\\RSA\\%s", pszContainerName); + + if (RegOpenKeyExA(HKEY_CURRENT_USER, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS) + { + SetLastError(NTE_BAD_KEYSET); + return (HCRYPTPROV)INVALID_HANDLE_VALUE; + } + + hKeyContainer = new_key_container(pszContainerName, pVTable); + if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE) + { + if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + return (HCRYPTPROV)INVALID_HANDLE_VALUE; + + if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) == + ERROR_SUCCESS) + { + pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen); + if (pbKey) + { + if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) == + ERROR_SUCCESS) + { + RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0, + &pKeyContainer->hKeyExchangeKeyPair); + } + HeapFree(GetProcessHeap(), 0, pbKey); + } + } + + if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) == + ERROR_SUCCESS) + { + pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen); + if (pbKey) + { + if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) == + ERROR_SUCCESS) + { + RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0, + &pKeyContainer->hSignatureKeyPair); + } + HeapFree(GetProcessHeap(), 0, pbKey); + } + } + } + + return hKeyContainer; +} + +/****************************************************************************** + * CPAcquireContext (RSAENH.@) + * + * Acquire a handle to the key container specified by pszContainer + * + * PARAMS + * phProv [O] Pointer to the location the acquired handle will be written to. + * pszContainer [I] Name of the desired key container. See Notes + * dwFlags [I] Flags. See Notes. + * pVTable [I] Pointer to a PVTableProvStruct containing callbacks. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * If pszContainer is NULL or points to a zero length string the user's login + * name will be used as the key container name. + * + * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created. + * If a keyset with the given name already exists, the function fails and sets + * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified + * key container does not exist, function fails and sets last error to + * NTE_BAD_KEYSET. + */ +BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer, + DWORD dwFlags, PVTableProvStruc pVTable) +{ + DWORD dwLen; + CHAR szKeyContainerName[MAX_PATH] = ""; + + TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv, + debugstr_a(pszContainer), dwFlags, pVTable); + + if (!load_lib()) return FALSE; + + if (pszContainer ? strlen(pszContainer) : 0) + { + strncpy(szKeyContainerName, pszContainer, MAX_PATH); + szKeyContainerName[MAX_PATH-1] = '\0'; + } + else + { + dwLen = MAX_PATH; + if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE; + } + + switch (dwFlags) + { + case 0: + *phProv = read_key_container(szKeyContainerName, pVTable); + break; + + case CRYPT_NEWKEYSET: + *phProv = read_key_container(szKeyContainerName, pVTable); + if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) + { + release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER); + SetLastError(NTE_EXISTS); + return FALSE; + } + *phProv = new_key_container(szKeyContainerName, pVTable); + break; + + default: + *phProv = (unsigned int)INVALID_HANDLE_VALUE; + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + return *phProv != (unsigned int)INVALID_HANDLE_VALUE; +} + +/****************************************************************************** + * CPCreateHash (RSAENH.@) + * + * CPCreateHash creates and initalizes a new hash object. + * + * PARAMS + * hProv [I] Handle to the key container to which the new hash will belong. + * Algid [I] Identifies the hash algorithm, which will be used for the hash. + * hKey [I] Handle to a session key applied for keyed hashes. + * dwFlags [I] Currently no flags defined. Must be zero. + * phHash [O] Points to the location where a handle to the new hash will be stored. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC. + * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero. + */ +BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, + HCRYPTHASH *phHash) +{ + KEYCONTAINER *pKeyContainer; + CRYPTHASH *pCryptHash; + const PROV_ENUMALGS_EX *peaAlgidInfo; + + TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey, + dwFlags, phHash); + + if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + peaAlgidInfo = get_algid_info(pKeyContainer, Algid); + if (!peaAlgidInfo) + { + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + if (dwFlags) + { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if ((Algid == CALG_MAC || Algid == CALG_HMAC)) { + CRYPTKEY *pCryptKey; + + if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + } + + *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH, + destroy_hash, (OBJECTHDR**)&pCryptHash); + if (!pCryptHash) return FALSE; + + pCryptHash->aiAlgid = Algid; + pCryptHash->hKey = hKey; + pCryptHash->hProv = hProv; + pCryptHash->dwState = RSAENH_HASHSTATE_IDLE; + pCryptHash->pHMACInfo = (PHMAC_INFO)NULL; + pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3; + + return init_hash(pKeyContainer, pCryptHash); +} + +/****************************************************************************** + * CPDestroyHash (RSAENH.@) + * + * Releases the handle to a hash object. The object is destroyed if it's reference + * count reaches zero. + * + * PARAMS + * hProv [I] Handle to the key container to which the hash object belongs. + * hHash [I] Handle to the hash object to be released. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash) +{ + TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash); + + if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + return TRUE; +} + +/****************************************************************************** + * CPDestroyKey (RSAENH.@) + * + * Releases the handle to a key object. The object is destroyed if it's reference + * count reaches zero. + * + * PARAMS + * hProv [I] Handle to the key container to which the key object belongs. + * hKey [I] Handle to the key object to be released. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey) +{ + TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey); + + if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + return TRUE; +} + +/****************************************************************************** + * CPDuplicateHash (RSAENH.@) + * + * Clones a hash object including it's current state. + * + * PARAMS + * hUID [I] Handle to the key container the hash belongs to. + * hHash [I] Handle to the hash object to be cloned. + * pdwReserved [I] Reserved. Must be NULL. + * dwFlags [I] No flags are currently defined. Must be 0. + * phHash [O] Handle to the cloned hash object. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + */ +BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved, + DWORD dwFlags, HCRYPTHASH *phHash) +{ + CRYPTHASH *pSrcHash, *pDestHash; + + TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash, + pdwReserved, dwFlags, phHash); + + if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + if (!phHash || pdwReserved || dwFlags) + { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + + *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH, + destroy_hash, (OBJECTHDR**)&pDestHash); + if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE) + { + memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH)); + duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context); + copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo); + } + + return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE; +} + +/****************************************************************************** + * CPDuplicateKey (RSAENH.@) + * + * Clones a key object including it's current state. + * + * PARAMS + * hUID [I] Handle to the key container the hash belongs to. + * hKey [I] Handle to the key object to be cloned. + * pdwReserved [I] Reserved. Must be NULL. + * dwFlags [I] No flags are currently defined. Must be 0. + * phHash [O] Handle to the cloned key object. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + */ +BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved, + DWORD dwFlags, HCRYPTKEY *phKey) +{ + CRYPTKEY *pSrcKey, *pDestKey; + + TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey, + pdwReserved, dwFlags, phKey); + + if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + if (!phKey || pdwReserved || dwFlags) + { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + + *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key, + (OBJECTHDR**)&pDestKey); + if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE) + { + memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY)); + duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context); + return TRUE; + } + else + { + return FALSE; + } +} + +/****************************************************************************** + * CPEncrypt (RSAENH.@) + * + * Encrypt data. + * + * PARAMS + * hProv [I] The key container hKey and hHash belong to. + * hKey [I] The key used to encrypt the data. + * hHash [I] An optional hash object for parallel hashing. See notes. + * Final [I] Indicates if this is the last block of data to encrypt. + * dwFlags [I] Currently no flags defined. Must be zero. + * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there. + * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data. + * dwBufLen [I] Size of the buffer at pbData. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTES + * If a hash object handle is provided in hHash, it will be updated with the plaintext. + * This is useful for message signatures. + * + * This function uses the standard WINAPI protocol for querying data of dynamic length. + * + * FIXME + * Parallel hashing not yet implemented. + */ +BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, + DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen) +{ + CRYPTKEY *pCryptKey; + BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE]; + DWORD dwEncryptedLen, i, j, k; + + TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, " + "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen, + dwBufLen); + + if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) + { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE) + pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING; + + if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING) + { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) { + if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen; + for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen; + *pdwDataLen = dwEncryptedLen; + + if (*pdwDataLen > dwBufLen) + { + SetLastError(ERROR_MORE_DATA); + return FALSE; + } + + for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) { + switch (pCryptKey->dwMode) { + case CRYPT_MODE_ECB: + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out, + RSAENH_ENCRYPT); + break; + + case CRYPT_MODE_CBC: + for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j]; + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out, + RSAENH_ENCRYPT); + memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen); + break; + + case CRYPT_MODE_CFB: + for (j=0; j<pCryptKey->dwBlockLen; j++) { + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, + pCryptKey->abChainVector, o, RSAENH_ENCRYPT); + out[j] = in[j] ^ o[0]; + for (k=0; k<pCryptKey->dwBlockLen-1; k++) + pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1]; + pCryptKey->abChainVector[k] = out[j]; + } + break; + + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + memcpy(in, out, pCryptKey->dwBlockLen); + } + } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) { + encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen); + } + + if (Final) setup_key(pCryptKey); + + return TRUE; +} + +/****************************************************************************** + * CPDecrypt (RSAENH.@) + * + * Decrypt data. + * + * PARAMS + * hProv [I] The key container hKey and hHash belong to. + * hKey [I] The key used to decrypt the data. + * hHash [I] An optional hash object for parallel hashing. See notes. + * Final [I] Indicates if this is the last block of data to decrypt. + * dwFlags [I] Currently no flags defined. Must be zero. + * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there. + * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTES + * If a hash object handle is provided in hHash, it will be updated with the plaintext. + * This is useful for message signatures. + * + * This function uses the standard WINAPI protocol for querying data of dynamic length. + * + * FIXME + * Parallel hashing not yet implemented. + */ +BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, + DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen) +{ + CRYPTKEY *pCryptKey; + BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE]; + DWORD i, j, k; + + TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, " + "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen); + + if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) + { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE) + pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING; + + if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING) + { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) { + for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) { + switch (pCryptKey->dwMode) { + case CRYPT_MODE_ECB: + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out, + RSAENH_DECRYPT); + break; + + case CRYPT_MODE_CBC: + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out, + RSAENH_DECRYPT); + for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j]; + memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen); + break; + + case CRYPT_MODE_CFB: + for (j=0; j<pCryptKey->dwBlockLen; j++) { + encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, + pCryptKey->abChainVector, o, RSAENH_ENCRYPT); + out[j] = in[j] ^ o[0]; + for (k=0; k<pCryptKey->dwBlockLen-1; k++) + pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1]; + pCryptKey->abChainVector[k] = in[j]; + } + break; + + default: + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + memcpy(in, out, pCryptKey->dwBlockLen); + } + if (Final) *pdwDataLen -= pbData[*pdwDataLen-1]; + + } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) { + encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen); + } + + if (Final) setup_key(pCryptKey); + + return TRUE; +} + +/****************************************************************************** + * CPExportKey (RSAENH.@) + * + * Export a key into a binary large object (BLOB). + * + * PARAMS + * hProv [I] Key container from which a key is to be exported. + * hKey [I] Key to be exported. + * hPubKey [I] Key used to encrypt sensitive BLOB data. + * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB. + * dwFlags [I] Currently none defined. + * pbData [O] Pointer to a buffer where the BLOB will be written to. + * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + */ +BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey, + DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen) +{ + CRYPTKEY *pCryptKey, *pPubKey; + BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData; + RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1); + ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1); + DWORD dwDataLen, i; + BYTE *pbRawData; + + TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p," + "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen); + + if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + switch ((BYTE)dwBlobType) + { + case SIMPLEBLOB: + if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){ + SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */ + return FALSE; + } + + if (GET_ALG_CLASS(pCryptKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT) { + SetLastError(NTE_BAD_KEY); /* FIXME: error code? */ + return FALSE; + } + + dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen; + if (pbData) { + if (*pdwDataLen < dwDataLen) { + SetLastError(ERROR_MORE_DATA); + *pdwDataLen = dwDataLen; + return FALSE; + } + + pBlobHeader->bType = SIMPLEBLOB; + pBlobHeader->bVersion = CUR_BLOB_VERSION; + pBlobHeader->reserved = 0; + pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid; + + *pAlgid = pPubKey->aiAlgid; + + pbRawData = (BYTE*)(pAlgid+1); + pbRawData[0] = 0x00; + pbRawData[1] = RSAENH_PKC_BLOCKTYPE; + for (i=2; i < pPubKey->dwBlockLen - pCryptKey->dwKeyLen - 1; i++) + do gen_rand_impl(&pbRawData[i], 1); while (!pbRawData[i]); + pbRawData[i] = 0x00; + for (i=0; i<pCryptKey->dwKeyLen; i++) + pbRawData[pPubKey->dwBlockLen - pCryptKey->dwKeyLen + i] = + pCryptKey->abKeyValue[i]; + + encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbRawData, pbRawData, + RSAENH_ENCRYPT); + } + *pdwDataLen = dwDataLen; + return TRUE; + + case PUBLICKEYBLOB: + if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) { + SetLastError(NTE_BAD_KEY); /* FIXME: error code? */ + return FALSE; + } + + if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen; + if (pbData) { + if (*pdwDataLen < dwDataLen) { + SetLastError(ERROR_MORE_DATA); + *pdwDataLen = dwDataLen; + return FALSE; + } + + pBlobHeader->bType = PUBLICKEYBLOB; + pBlobHeader->bVersion = CUR_BLOB_VERSION; + pBlobHeader->reserved = 0; + pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid; + + pRSAPubKey->magic = RSAENH_MAGIC_RSA1; + pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3; + + export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context, + pCryptKey->dwKeyLen, &pRSAPubKey->pubexp); + } + *pdwDataLen = dwDataLen; + return TRUE; + + case PRIVATEKEYBLOB: + if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + + 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1); + if (pbData) { + if (*pdwDataLen < dwDataLen) { + SetLastError(ERROR_MORE_DATA); + *pdwDataLen = dwDataLen; + return FALSE; + } + + pBlobHeader->bType = PRIVATEKEYBLOB; + pBlobHeader->bVersion = CUR_BLOB_VERSION; + pBlobHeader->reserved = 0; + pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid; + + pRSAPubKey->magic = RSAENH_MAGIC_RSA2; + pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3; + + export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context, + pCryptKey->dwKeyLen, &pRSAPubKey->pubexp); + } + *pdwDataLen = dwDataLen; + return TRUE; + + default: + SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */ + return FALSE; + } +} + +/****************************************************************************** + * CPImportKey (RSAENH.@) + * + * Import a BLOB'ed key into a key container. + * + * PARAMS + * hProv [I] Key container into which the key is to be imported. + * pbData [I] Pointer to a buffer which holds the BLOB. + * dwDataLen [I] Length of data in buffer at pbData. + * hPubKey [I] Key used to decrypt sensitive BLOB data. + * dwFlags [I] Currently none defined. + * phKey [O] Handle to the imported key. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + */ +BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen, + HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey) +{ + KEYCONTAINER *pKeyContainer; + CRYPTKEY *pCryptKey, *pPubKey; + CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData; + CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1); + CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1); + CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1); + BYTE *pbDecrypted, abKeyValue[RSAENH_MAX_KEY_SIZE]; + DWORD dwKeyLen, i; + + TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n", + hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey); + + if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwDataLen < sizeof(BLOBHEADER) || + pBlobHeader->bVersion != CUR_BLOB_VERSION || + pBlobHeader->reserved != 0) + { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + switch (pBlobHeader->bType) + { + case PRIVATEKEYBLOB: + if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) || + (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) || + (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + + (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4)))) + { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), NULL, + (HCRYPTHASH)0); + if (!lookup_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) { + SetLastError(NTE_FAIL); + return FALSE; + } + + return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context, + pRSAPubKey->bitlen/8, pRSAPubKey->pubexp); + + case PUBLICKEYBLOB: + if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) || + (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) || + (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3))) + { + SetLastError(NTE_BAD_DATA); + return FALSE; + } + + *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), NULL, + (HCRYPTHASH)0); + if (!lookup_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) { + SetLastError(NTE_FAIL); + return FALSE; + } + + return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context, + pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp); + + case SIMPLEBLOB: + if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) || + pPubKey->aiAlgid != CALG_RSA_KEYX) + { + SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */ + return FALSE; + } + + if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen) + { + SetLastError(NTE_BAD_DATA); /* FIXME: error code */ + return FALSE; + } + + pbDecrypted = (BYTE*)HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen); + if (!pbDecrypted) return FALSE; + encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted, + RSAENH_DECRYPT); + + for (i=2; i<pPubKey->dwBlockLen && pbDecrypted[i]; i++); + if ((i==pPubKey->dwBlockLen) || + (pbDecrypted[0] != 0x00) || + (pbDecrypted[1] != RSAENH_PKC_BLOCKTYPE)) + { + HeapFree(GetProcessHeap(), 0, pbDecrypted); + SetLastError(NTE_BAD_DATA); /* FIXME: error code */ + return FALSE; + } + + dwKeyLen = pPubKey->dwBlockLen-i-1; + memcpy(abKeyValue, pbDecrypted+i+1, dwKeyLen); + HeapFree(GetProcessHeap(), 0, pbDecrypted); + + *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, abKeyValue, 0); + return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE; + + default: + SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */ + return FALSE; + } +} + +/****************************************************************************** + * CPGenKey (RSAENH.@) + * + * Generate a key in the key container + * + * PARAMS + * hProv [I] Key container for which a key is to be generated. + * Algid [I] Crypto algorithm identifier for the key to be generated. + * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes + * phKey [O] Handle to the generated key. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * FIXME + * Flags currently not considered. + * + * NOTES + * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE + * and AT_SIGNATURE values. + */ +BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey) +{ + KEYCONTAINER *pKeyContainer; + BYTE abKeyValue[2048]; + + TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + /* MSDN: hProv not containing valid context handle */ + SetLastError(NTE_BAD_UID); + return FALSE; + } + + switch (Algid) + { + case AT_SIGNATURE: + RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair); + pKeyContainer->hSignatureKeyPair = + new_key(hProv, CALG_RSA_SIGN, dwFlags, abKeyValue, 0); + copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY, + (unsigned int*)phKey); + break; + + case AT_KEYEXCHANGE: + RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair); + pKeyContainer->hKeyExchangeKeyPair = new_key(hProv, CALG_RSA_KEYX, dwFlags, abKeyValue, 0); + copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY, + (unsigned int*)phKey); + break; + + case CALG_RC2: + case CALG_RC4: + case CALG_DES: + gen_rand_impl(abKeyValue, 2048); + *phKey = new_key(hProv, Algid, dwFlags, abKeyValue, 0); + break; + + default: + /* MSDN: Algorithm not supported specified by Algid */ + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + return *phKey != (unsigned int)INVALID_HANDLE_VALUE; +} + +/****************************************************************************** + * CPGenRandom (RSAENH.@) + * + * Generate a random byte stream. + * + * PARAMS + * hProv [I] Key container that is used to generate random bytes. + * dwLen [I] Specifies the number of requested random data bytes. + * pbBuffer [O] Random bytes will be stored here. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer) +{ + KEYCONTAINER *pKeyContainer; + + TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + /* MSDN: hProv not containing valid context handle */ + SetLastError(NTE_BAD_UID); + return FALSE; + } + + return gen_rand_impl(pbBuffer, dwLen); +} + +/****************************************************************************** + * CPGetHashParam (RSAENH.@) + * + * Query parameters of an hash object. + * + * PARAMS + * hProv [I] The kea container, which the hash belongs to. + * hHash [I] The hash object that is to be queried. + * dwParam [I] Specifies the parameter that is to be queried. + * pbData [I] Pointer to the buffer where the parameter value will be stored. + * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value. + * dwFlags [I] None currently defined. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be + * finalized if HP_HASHVALUE is queried. + */ +BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, + DWORD *pdwDataLen, DWORD dwFlags) +{ + CRYPTHASH *pCryptHash; + KEYCONTAINER *pKeyContainer; + BYTE abHashValue[RSAENH_MAX_HASH_SIZE]; + + TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n", + hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) + { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH, + (OBJECTHDR**)&pCryptHash)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + if (!pdwDataLen) + { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + + switch (dwParam) + { + case HP_ALGID: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid, + sizeof(ALG_ID)); + + case HP_HASHSIZE: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize, + sizeof(DWORD)); + + case HP_HASHVAL: + if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) { + SetLastError(NTE_BAD_HASH_STATE); + return FALSE; + } + + if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED)) + { + finalize_hash(pCryptHash); + if (pCryptHash->aiAlgid == CALG_HMAC) { + memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize); + init_hash(pKeyContainer, pCryptHash); + update_hash(pCryptHash, pCryptHash->pHMACInfo->pbOuterString, + pCryptHash->pHMACInfo->cbOuterString); + update_hash(pCryptHash, abHashValue, pCryptHash->dwHashSize); + finalize_hash(pCryptHash); + } + + pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED; + } + + return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue, + pCryptHash->dwHashSize); + + default: + SetLastError(NTE_BAD_TYPE); + return FALSE; + } +} + +/****************************************************************************** + * CPSetKeyParam (RSAENH.@) + * + * Set a parameter of a key object + * + * PARAMS + * hProv [I] The key container to which the key belongs. + * hKey [I] The key for which a parameter is to be set. + * dwParam [I] Parameter type. See Notes. + * pbData [I] Pointer to the parameter value. + * dwFlags [I] Currently none defined. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTES: + * Defined dwParam types are: + * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB. + * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's) + * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT, + * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC + * - KP_IV: Initialization vector + */ +BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, + DWORD dwFlags) +{ + CRYPTKEY *pCryptKey; + + TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey, + dwParam, pbData, dwFlags); + + if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + switch (dwParam) { + case KP_MODE: + pCryptKey->dwMode = *(DWORD*)pbData; + return TRUE; + + case KP_MODE_BITS: + pCryptKey->dwModeBits = *(DWORD*)pbData; + return TRUE; + + case KP_PERMISSIONS: + pCryptKey->dwPermissions = *(DWORD*)pbData; + return TRUE; + + case KP_IV: + memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen); + return TRUE; + + default: + SetLastError(NTE_BAD_TYPE); + return FALSE; + } +} + +/****************************************************************************** + * CPGetKeyParam (RSAENH.@) + * + * Query a key parameter. + * + * PARAMS + * hProv [I] The key container, which the key belongs to. + * hHash [I] The key object that is to be queried. + * dwParam [I] Specifies the parameter that is to be queried. + * pbData [I] Pointer to the buffer where the parameter value will be stored. + * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value. + * dwFlags [I] None currently defined. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * Defined dwParam types are: + * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB. + * - KP_MODE_BITS: Shift width for cipher feedback mode. + * (Currently ignored by MS CSP's - always eight) + * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT, + * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC + * - KP_IV: Initialization vector. + * - KP_KEYLEN: Bitwidth of the key. + * - KP_BLOCKLEN: Size of a block cipher block. + * - KP_SALT: Salt value. + */ +BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, + DWORD *pdwDataLen, DWORD dwFlags) +{ + CRYPTKEY *pCryptKey; + DWORD dwBitLen; + + TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n", + hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags); + + if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_BAD_KEY); + return FALSE; + } + + switch (dwParam) + { + case KP_IV: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector, + pCryptKey->dwBlockLen); + + case KP_SALT: + return copy_param(pbData, pdwDataLen, + (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen); + + case KP_KEYLEN: + dwBitLen = pCryptKey->dwKeyLen << 3; + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD)); + + case KP_BLOCKLEN: + dwBitLen = pCryptKey->dwBlockLen << 3; + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD)); + + case KP_MODE: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD)); + + case KP_MODE_BITS: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits, + sizeof(DWORD)); + + case KP_PERMISSIONS: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions, + sizeof(DWORD)); + + default: + SetLastError(NTE_BAD_TYPE); + return FALSE; + } +} + +/****************************************************************************** + * CPGetProvParam (RSAENH.@) + * + * Query a CSP parameter. + * + * PARAMS + * hProv [I] The key container that is to be queried. + * dwParam [I] Specifies the parameter that is to be queried. + * pbData [I] Pointer to the buffer where the parameter value will be stored. + * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value. + * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}). + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * NOTES: + * Defined dwParam types: + * - PP_CONTAINER: Name of the key container. + * - PP_NAME: Name of the cryptographic service provider. + * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits. + * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits. + * - PP_ENUMALGS{_EX}: Query provider capabilities. + */ +BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, + DWORD *pdwDataLen, DWORD dwFlags) +{ + KEYCONTAINER *pKeyContainer; + PROV_ENUMALGS provEnumalgs; + DWORD dwTemp; + + TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n", + hProv, dwParam, pbData, pdwDataLen, dwFlags); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + /* MSDN: hProv not containing valid context handle */ + SetLastError(NTE_BAD_UID); + return FALSE; + } + + switch (dwParam) + { + case PP_CONTAINER: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName, + strlen(pKeyContainer->szName)+1); + + case PP_NAME: + return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName, + strlen(pKeyContainer->szProvName)+1); + + case PP_SIG_KEYSIZE_INC: + case PP_KEYX_KEYSIZE_INC: + dwTemp = 8; + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp)); + + case PP_ENUMALGS: + case PP_ENUMALGS_EX: + if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) || + (!aProvEnumAlgsEx[pKeyContainer->dwPersonality] + [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) && + ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST)) + { + SetLastError(ERROR_NO_MORE_ITEMS); + return FALSE; + } + + if (dwParam == PP_ENUMALGS) { + if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS))) + pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ? + 0 : pKeyContainer->dwEnumAlgsCtr+1; + + provEnumalgs.aiAlgid = aProvEnumAlgsEx + [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid; + provEnumalgs.dwBitLen = aProvEnumAlgsEx + [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen; + provEnumalgs.dwNameLen = aProvEnumAlgsEx + [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen; + memcpy(provEnumalgs.szName, aProvEnumAlgsEx + [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName, + 20*sizeof(CHAR)); + + return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs, + sizeof(PROV_ENUMALGS)); + } else { + if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX))) + pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ? + 0 : pKeyContainer->dwEnumAlgsCtr+1; + + return copy_param(pbData, pdwDataLen, + (CONST BYTE*)&aProvEnumAlgsEx + [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr], + sizeof(PROV_ENUMALGS_EX)); + } + + default: + /* MSDN: Unknown parameter number in dwParam */ + SetLastError(NTE_BAD_TYPE); + return FALSE; + } + return FALSE; +} + +/****************************************************************************** + * CPDeriveKey (RSAENH.@) + * + * Derives a key from a hash value. + * + * PARAMS + * hProv [I] Key container for which a key is to be generated. + * Algid [I] Crypto algorithm identifier for the key to be generated. + * hBaseData [I] Hash from whose value the key will be derived. + * dwFlags [I] See Notes. + * phKey [O] The generated key. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + * + * NOTES + * Defined flags: + * - CRYPT_EXPORTABLE: Key can be exported. + * - CRYPT_NO_SALT: No salt is used for 40 bit keys. + * - CRYPT_CREATE_SALT: Use remaining bits as salt value. + */ +BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, + DWORD dwFlags, HCRYPTKEY *phKey) +{ + KEYCONTAINER *pKeyContainer; + + TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid, + hBaseData, dwFlags, phKey); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (!is_valid_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + if (!phKey) + { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + + *phKey = new_key(hProv, Algid, dwFlags, NULL, hBaseData); + return *phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE ? FALSE : TRUE; +} + +/****************************************************************************** + * CPGetUserKey (RSAENH.@) + * + * Returns a handle to the user's private key-exchange- or signature-key. + * + * PARAMS + * hProv [I] The key container from which a user key is requested. + * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE + * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTE + * A newly created key container does not contain private user key. Create them with CPGenKey. + */ +BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey) +{ + KEYCONTAINER *pKeyContainer; + + TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + /* MSDN: hProv not containing valid context handle */ + SetLastError(NTE_BAD_UID); + return FALSE; + } + + switch (dwKeySpec) + { + case AT_KEYEXCHANGE: + copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY, + (unsigned int*)phUserKey); + break; + + case AT_SIGNATURE: + copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY, + (unsigned int*)phUserKey); + break; + + default: + *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE; + } + + if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) + { + /* MSDN: dwKeySpec parameter specifies non existent key */ + SetLastError(NTE_NO_KEY); + return FALSE; + } + + return TRUE; +} + +/****************************************************************************** + * CPHashData (RSAENH.@) + * + * Updates a hash object with the given data. + * + * PARAMS + * hProv [I] Key container to which the hash object belongs. + * hHash [I] Hash object which is to be updated. + * pbData [I] Pointer to data with which the hash object is to be updated. + * dwDataLen [I] Length of the data. + * dwFlags [I] Currently none defined. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTES + * The actual hash value is queried with CPGetHashParam, which will finalize + * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE. + */ +BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData, + DWORD dwDataLen, DWORD dwFlags) +{ + CRYPTHASH *pCryptHash; + KEYCONTAINER *pKeyContainer; + + TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n", + hProv, hHash, pbData, dwDataLen, dwFlags); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) + { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH, + (OBJECTHDR**)&pCryptHash)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + if (!get_algid_info(pKeyContainer, pCryptHash->aiAlgid) || + pCryptHash->aiAlgid == CALG_SSL3_SHAMD5) + { + SetLastError(NTE_BAD_ALGID); + return FALSE; + } + + if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) + pCryptHash->dwState = RSAENH_HASHSTATE_HASHING; + + if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING) + { + SetLastError(NTE_BAD_HASH_STATE); + return FALSE; + } + + update_hash(pCryptHash, pbData, dwDataLen); + return TRUE; +} + +/****************************************************************************** + * CPHashSessionKey (RSAENH.@) + */ +BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey, + DWORD dwFlags) +{ + FIXME("(stub)\n"); + return FALSE; +} + +/****************************************************************************** + * CPReleaseContext (RSAENH.@) + * + * Release a key container. + * + * PARAMS + * hProv [I] Key container to be released. + * dwFlags [I] Currently none defined. + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags) +{ + TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags); + + if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER)) + { + /* MSDN: hProv not containing valid context handle */ + SetLastError(NTE_BAD_UID); + return FALSE; + } + + return TRUE; +} + +/****************************************************************************** + * CPSetHashParam (RSAENH.@) + * + * Set a parameter of a hash object + * + * PARAMS + * hProv [I] The key container to which the key belongs. + * hHash [I] The hash object for which a parameter is to be set. + * dwParam [I] Parameter type. See Notes. + * pbData [I] Pointer to the parameter value. + * dwFlags [I] Currently none defined. + * + * RETURNS + * Success: TRUE. + * Failure: FALSE. + * + * NOTES: + * Currently only the HP_HMAC_INFO dwParam type is defined. + * The HMAC_INFO struct will be deep copied into the hash object. + * See Internet RFC 2104 for details on the HMAC algorithm. + */ +BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, + BYTE *pbData, DWORD dwFlags) +{ + CRYPTHASH *pCryptHash; + CRYPTKEY *pCryptKey; + KEYCONTAINER *pKeyContainer; + int i; + + TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", + hProv, hHash, dwParam, pbData, dwFlags); + + if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER, + (OBJECTHDR**)&pKeyContainer)) + { + SetLastError(NTE_BAD_UID); + return FALSE; + } + + if (dwFlags) { + SetLastError(NTE_BAD_FLAGS); + return FALSE; + } + + if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH, + (OBJECTHDR**)&pCryptHash)) + { + SetLastError(NTE_BAD_HASH); + return FALSE; + } + + switch (dwParam) { + case HP_HMAC_INFO: + free_hmac_info(pCryptHash->pHMACInfo); + if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE; + init_hash(pKeyContainer, pCryptHash); + + if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY, + (OBJECTHDR**)&pCryptKey)) + { + SetLastError(NTE_FAIL); /* FIXME: correct error code? */ + return FALSE; + } + + for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) { + pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i]; + } + for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) { + pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i]; + } + + return RSAENH_CPHashData(hProv, hHash, pCryptHash->pHMACInfo->pbInnerString, + pCryptHash->pHMACInfo->cbInnerString, 0); + + default: + SetLastError(NTE_BAD_TYPE); + return FALSE; + } +} + +/****************************************************************************** + * CPSetProvParam (RSAENH.@) + */ +BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags) +{ + FIXME("(stub)\n"); + return FALSE; +} + +/****************************************************************************** + * CPSignHash (RSAENH.@) + */ +BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec, + LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature, + DWORD *pdwSigLen) +{ + FIXME("(stub)\n"); + return FALSE; +} + +/****************************************************************************** + * CPVerifySignature (RSAENH.@) + */ +BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature, + DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription, + DWORD dwFlags) +{ + FIXME("(stub)\n"); + return FALSE; +} + +static const WCHAR szProviderKeys[3][97] = { + { 'S','o','f','t','w','a','r','e','\\', + 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r', + 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v', + 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s', + 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r', + 'o','v','i','d','e','r',' ','v','1','.','0',0 }, + { 'S','o','f','t','w','a','r','e','\\', + 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r', + 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v', + 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ', + 'E','n','h','a','n','c','e','d', + ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r', + 'o','v','i','d','e','r',' ','v','1','.','0',0 }, + { 'S','o','f','t','w','a','r','e','\\', + 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r', + 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v', + 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g', + ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r', + 'o','v','i','d','e','r',0 } +}; +static const WCHAR szDefaultKey[] = { 'S','o','f','t','w','a','r','e','\\', + 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r', + 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v', + 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0}; + +/****************************************************************************** + * DllRegisterServer (RSAENH.@) + * + * Dll self registration. + * + * PARAMS + * + * RETURNS + * Success: S_OK. + * Failure: != S_OK + * + * NOTES + * Registers the following keys: + * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\ + * Microsoft Base Cryptographic Provider v1.0 + * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\ + * Microsoft Enhanced Cryptographic Provider + * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\ + * Microsoft Strong Cryptographpic Provider + * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001 + */ +HRESULT WINAPI RSAENH_DllRegisterServer() +{ + HKEY key; + DWORD dp; + long apiRet; + int i; + + for (i=0; i<3; i++) { + apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL, + REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp); + + if (apiRet == ERROR_SUCCESS) + { + if (dp == REG_CREATED_NEW_KEY) + { + static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 }; + static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 }; + static const WCHAR szType[] = { 'T','y','p','e',0 }; + static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 }; + DWORD type = 1; + DWORD sign = 0xdeadbeef; + RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase, + (lstrlenW(szRSABase) + 1) * sizeof(WCHAR)); + RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type)); + RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign)); + } + RegCloseKey(key); + } + } + if (apiRet == ERROR_SUCCESS) + apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKey, 0, NULL, REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, NULL, &key, &dp); + if (apiRet == ERROR_SUCCESS) + { + if (dp == REG_CREATED_NEW_KEY) + { + static const WCHAR szName[] = { 'N','a','m','e',0 }; + static const WCHAR szRSAName[] = { + 'M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',' ', + 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ', + 'P','r','o','v','i','d','e','r',0 }; + static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 }; + static const WCHAR szRSATypeName[] = { + 'R','S','A',' ','F','u','l','l',' ', + '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ', + 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 }; + + RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName, sizeof(szRSAName)); + RegSetValueExW(key, szTypeName, 0, REG_SZ, (LPBYTE)szRSATypeName,sizeof(szRSATypeName)); + } + RegCloseKey(key); + } + return HRESULT_FROM_WIN32(apiRet); +} + +/****************************************************************************** + * DllUnregisterServer (RSAENH.@) + * + * Dll self unregistration. + * + * PARAMS + * + * RETURNS + * Success: S_OK + * + * NOTES + * For the relevant keys see DllRegisterServer. + */ +HRESULT WINAPI RSAENH_DllUnregisterServer() +{ + RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]); + RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]); + RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]); + RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKey); + return S_OK; +} diff --git a/dlls/rsaenh/rsaenh.spec b/dlls/rsaenh/rsaenh.spec new file mode 100644 index 00000000000..8f41dc3bd5a --- /dev/null +++ b/dlls/rsaenh/rsaenh.spec @@ -0,0 +1,27 @@ +@ stdcall CPAcquireContext(ptr str long ptr) RSAENH_CPAcquireContext +@ stdcall CPCreateHash(long long ptr long ptr) RSAENH_CPCreateHash +@ stdcall CPDecrypt(long long long long long ptr ptr) RSAENH_CPDecrypt +@ stdcall CPDeriveKey(long long long long ptr) RSAENH_CPDeriveKey +@ stdcall CPDestroyHash(long long) RSAENH_CPDestroyHash +@ stdcall CPDestroyKey(long long) RSAENH_CPDestroyKey +@ stdcall CPDuplicateHash(long long ptr long ptr) RSAENH_CPDuplicateHash +@ stdcall CPDuplicateKey(long long ptr long ptr) RSAENH_CPDuplicateKey +@ stdcall CPEncrypt(long long long long long ptr ptr long) RSAENH_CPEncrypt +@ stdcall CPExportKey(long long long long long ptr ptr) RSAENH_CPExportKey +@ stdcall CPGenKey(long long long ptr) RSAENH_CPGenKey +@ stdcall CPGenRandom(long long ptr) RSAENH_CPGenRandom +@ stdcall CPGetHashParam(long long long ptr ptr long) RSAENH_CPGetHashParam +@ stdcall CPGetKeyParam(long long long ptr ptr long) RSAENH_CPGetKeyParam +@ stdcall CPGetProvParam(long long ptr ptr long) RSAENH_CPGetProvParam +@ stdcall CPGetUserKey(long long ptr) RSAENH_CPGetUserKey +@ stdcall CPHashData(long long ptr long long) RSAENH_CPHashData +@ stdcall CPHashSessionKey(long long long long) RSAENH_CPHashSessionKey +@ stdcall CPImportKey(long ptr long long long ptr) RSAENH_CPImportKey +@ stdcall CPReleaseContext(long long) RSAENH_CPReleaseContext +@ stdcall CPSetHashParam(long long long ptr long) RSAENH_CPSetHashParam +@ stdcall CPSetKeyParam(long long long ptr long) RSAENH_CPSetKeyParam +@ stdcall CPSetProvParam(long long ptr long) RSAENH_CPSetProvParam +@ stdcall CPSignHash(long long long wstr long ptr ptr) RSAENH_CPSignHash +@ stdcall CPVerifySignature(long long ptr long long wstr long) RSAENH_CPVerifySignature +@ stdcall -private DllRegisterServer() RSAENH_DllRegisterServer +@ stdcall -private DllUnregisterServer() RSAENH_DllUnregisterServer diff --git a/dlls/rsaenh/tests/.cvsignore b/dlls/rsaenh/tests/.cvsignore new file mode 100644 index 00000000000..95f60a57d35 --- /dev/null +++ b/dlls/rsaenh/tests/.cvsignore @@ -0,0 +1,3 @@ +Makefile +rsaenh.ok +testlist.c diff --git a/dlls/rsaenh/tests/Makefile.in b/dlls/rsaenh/tests/Makefile.in new file mode 100644 index 00000000000..1aa2cfdc98c --- /dev/null +++ b/dlls/rsaenh/tests/Makefile.in @@ -0,0 +1,13 @@ +TOPSRCDIR = @top_srcdir@ +TOPOBJDIR = ../../.. +SRCDIR = @srcdir@ +VPATH = @srcdir@ +TESTDLL = rsaenh.dll +IMPORTS = advapi32 kernel32 + +CTESTS = \ + rsaenh.c + +@MAKE_TEST_RULES@ + +### Dependencies: diff --git a/dlls/rsaenh/tests/rsaenh.c b/dlls/rsaenh/tests/rsaenh.c new file mode 100644 index 00000000000..8e1ea13dd95 --- /dev/null +++ b/dlls/rsaenh/tests/rsaenh.c @@ -0,0 +1,843 @@ +/* + * Unit tests for rsaenh functions + * + * Copyright (c) 2004 Michael Jung + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <string.h> +#include <stdio.h> +#include "wine/test.h" +#include "windef.h" +#include "winbase.h" +#include "winerror.h" +#include "wincrypt.h" + +static HCRYPTPROV hProv; +static const char szContainer[] = "winetest"; +static const unsigned char pbData[] = "Wine rocks totally!"; +static const char szProvider[] = MS_ENHANCED_PROV_A; + +/* +static void trace_hex(BYTE *pbData, DWORD dwLen) { + char szTemp[256]; + DWORD i, j; + + for (i = 0; i < dwLen-7; i+=8) { + sprintf(szTemp, "0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", + pbData[i], pbData[i+1], pbData[i+2], pbData[i+3], pbData[i+4], pbData[i+5], + pbData[i+6], pbData[i+7]); + trace(szTemp); + } + for (j=0; i<dwLen; j++,i++) { + sprintf(szTemp+6*j, "0x%02x, \n", pbData[i]); + } + trace(szTemp); +} +*/ + +static int init_environment(void) +{ + HCRYPTKEY hKey; + BOOL result; + + hProv = (HCRYPTPROV)INVALID_HANDLE_VALUE; + + if (!CryptAcquireContext(&hProv, szContainer, szProvider, PROV_RSA_FULL, 0)) + { + ok(GetLastError()==NTE_BAD_KEYSET, "%08lx\n", GetLastError()); + if (GetLastError()!=NTE_BAD_KEYSET) return 0; + result = CryptAcquireContext(&hProv, szContainer, szProvider, PROV_RSA_FULL, + CRYPT_NEWKEYSET); + ok(result, "%08lx\n", GetLastError()); + if (!result) return 0; + result = CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hKey); + ok(result, "%08lx\n", GetLastError()); + if (result) CryptDestroyKey(hKey); + result = CryptGenKey(hProv, AT_SIGNATURE, 0, &hKey); + ok(result, "%08lx\n", GetLastError()); + if (result) CryptDestroyKey(hKey); + } + return 1; +} + +static void clean_up_environment(void) +{ + CryptAcquireContext(&hProv, szContainer, szProvider, PROV_RSA_FULL, CRYPT_DELETEKEYSET); +} + +static void test_prov() +{ + BOOL result; + DWORD dwLen, dwInc; + + dwLen = (DWORD)sizeof(DWORD); + result = CryptGetProvParam(hProv, PP_SIG_KEYSIZE_INC, (BYTE*)&dwInc, &dwLen, 0); + ok(result && dwInc==8, "%08lx, %ld\n", GetLastError(), dwInc); + + dwLen = (DWORD)sizeof(DWORD); + result = CryptGetProvParam(hProv, PP_KEYX_KEYSIZE_INC, (BYTE*)&dwInc, &dwLen, 0); + ok(result && dwInc==8, "%08lx, %ld\n", GetLastError(), dwInc); +} + +static void test_gen_random() +{ + BOOL result; + BYTE rnd1[16], rnd2[16]; + + memset(rnd1, 0, sizeof(rnd1)); + memset(rnd2, 0, sizeof(rnd2)); + + result = CryptGenRandom(hProv, sizeof(rnd1), rnd1); + if (!result && GetLastError() == NTE_FAIL) { + /* rsaenh compiled without OpenSSL */ + return; + } + + ok(result, "%08lx\n", GetLastError()); + + result = CryptGenRandom(hProv, sizeof(rnd2), rnd2); + ok(result, "%08lx\n", GetLastError()); + + ok(memcmp(rnd1, rnd2, sizeof(rnd1)), "CryptGenRandom generates non random data\n"); +} + +static BOOL derive_key(ALG_ID aiAlgid, HCRYPTKEY *phKey, DWORD len) +{ + HCRYPTHASH hHash; + BOOL result; + unsigned char pbData[2000]; + int i; + + *phKey = (HCRYPTKEY)NULL; + for (i=0; i<2000; i++) pbData[i] = (unsigned char)i; + result = CryptCreateHash(hProv, CALG_MD2, 0, 0, &hHash); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError()==NTE_BAD_ALGID, "%08lx", GetLastError()); + return FALSE; + } + ok(result, "%08lx\n", GetLastError()); + if (!result) return FALSE; + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + if (!result) return FALSE; + result = CryptDeriveKey(hProv, aiAlgid, hHash, (len << 16) | CRYPT_EXPORTABLE, phKey); + ok(result, "%08lx\n", GetLastError()); + if (!result) return FALSE; + len = 2000; + result = CryptGetHashParam(hHash, HP_HASHVAL, pbData, &len, 0); + ok(result, "%08lx\n", GetLastError()); + CryptDestroyHash(hHash); + return TRUE; +} + +static void test_hashes(void) +{ + static const unsigned char md2hash[16] = { + 0x12, 0xcb, 0x1b, 0x08, 0xc8, 0x48, 0xa4, 0xa9, + 0xaa, 0xf3, 0xf1, 0x9f, 0xfc, 0x29, 0x28, 0x68 }; + static const unsigned char md4hash[16] = { + 0x8e, 0x2a, 0x58, 0xbf, 0xf2, 0xf5, 0x26, 0x23, + 0x79, 0xd2, 0x92, 0x36, 0x1b, 0x23, 0xe3, 0x81 }; + static const unsigned char md5hash[16] = { + 0x15, 0x76, 0xa9, 0x4d, 0x6c, 0xb3, 0x34, 0xdd, + 0x12, 0x6c, 0xb1, 0xc2, 0x7f, 0x19, 0xe0, 0xf2 }; + static const unsigned char sha1hash[20] = { + 0xf1, 0x0c, 0xcf, 0xde, 0x60, 0xc1, 0x7d, 0xb2, 0x6e, 0x7d, + 0x85, 0xd3, 0x56, 0x65, 0xc7, 0x66, 0x1d, 0xbb, 0xeb, 0x2c }; + unsigned char pbData[2048]; + BOOL result; + HCRYPTHASH hHash, hHashClone; + BYTE pbHashValue[36]; + DWORD hashlen, len; + int i; + + for (i=0; i<2048; i++) pbData[i] = (unsigned char)i; + + /* MD2 Hashing */ + result = CryptCreateHash(hProv, CALG_MD2, 0, 0, &hHash); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError() == NTE_BAD_ALGID, "%08lx\n", GetLastError()); + } else { + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + + len = sizeof(DWORD); + result = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&hashlen, &len, 0); + ok(result && (hashlen == 16), "%08lx, hashlen: %ld\n", GetLastError(), hashlen); + + len = 16; + result = CryptGetHashParam(hHash, HP_HASHVAL, pbHashValue, &len, 0); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbHashValue, md2hash, 16), "Wrong MD2 hash!\n"); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + } + + /* MD4 Hashing */ + result = CryptCreateHash(hProv, CALG_MD4, 0, 0, &hHash); + ok(result, "%08lx\n", GetLastError()); + + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + + len = sizeof(DWORD); + result = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&hashlen, &len, 0); + ok(result && (hashlen == 16), "%08lx, hashlen: %ld\n", GetLastError(), hashlen); + + len = 16; + result = CryptGetHashParam(hHash, HP_HASHVAL, pbHashValue, &len, 0); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbHashValue, md4hash, 16), "Wrong MD4 hash!\n"); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + /* MD5 Hashing */ + result = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash); + ok(result, "%08lx\n", GetLastError()); + + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + + len = sizeof(DWORD); + result = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&hashlen, &len, 0); + ok(result && (hashlen == 16), "%08lx, hashlen: %ld\n", GetLastError(), hashlen); + + len = 16; + result = CryptGetHashParam(hHash, HP_HASHVAL, pbHashValue, &len, 0); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbHashValue, md5hash, 16), "Wrong MD5 hash!\n"); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + /* SHA1 Hashing */ + result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash); + ok(result, "%08lx\n", GetLastError()); + + result = CryptHashData(hHash, (BYTE*)pbData, 5, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDuplicateHash(hHash, 0, 0, &hHashClone); + ok(result, "%08lx\n", GetLastError()); + + result = CryptHashData(hHashClone, (BYTE*)pbData+5, sizeof(pbData)-5, 0); + ok(result, "%08lx\n", GetLastError()); + + len = sizeof(DWORD); + result = CryptGetHashParam(hHashClone, HP_HASHSIZE, (BYTE*)&hashlen, &len, 0); + ok(result && (hashlen == 20), "%08lx, hashlen: %ld\n", GetLastError(), hashlen); + + len = 20; + result = CryptGetHashParam(hHashClone, HP_HASHVAL, pbHashValue, &len, 0); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbHashValue, sha1hash, 20), "Wrong SHA1 hash!\n"); + + result = CryptDestroyHash(hHashClone); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); +} + +static void test_block_cipher_modes() +{ + static const BYTE plain[23] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, + 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; + static const BYTE ecb[24] = { + 0xc0, 0x9a, 0xe4, 0x2f, 0x0a, 0x47, 0x67, 0x11, 0xf2, 0xb2, 0x5d, 0x5f, + 0x08, 0xff, 0x49, 0xa4, 0x45, 0x3a, 0x68, 0x14, 0xca, 0x18, 0xe5, 0xf4 }; + static const BYTE cbc[24] = { + 0xc0, 0x9a, 0xe4, 0x2f, 0x0a, 0x47, 0x67, 0x11, 0x10, 0xf5, 0xda, 0x61, + 0x4e, 0x3d, 0xab, 0xc0, 0x97, 0x85, 0x01, 0x12, 0x97, 0xa4, 0xf7, 0xd3 }; + static const BYTE cfb[24] = { + 0x29, 0xb5, 0x67, 0x85, 0x0b, 0x1b, 0xec, 0x07, 0x67, 0x2d, 0xa1, 0xa4, + 0x1a, 0x47, 0x24, 0x6a, 0x54, 0xe1, 0xe0, 0x92, 0xf9, 0x0e, 0xf6, 0xeb }; + HCRYPTKEY hKey; + BOOL result; + BYTE abData[24]; + DWORD dwMode, dwLen; + + result = derive_key(CALG_RC2, &hKey, 40); + if (!result) return; + + memcpy(abData, plain, sizeof(abData)); + + dwMode = CRYPT_MODE_ECB; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + SetLastError(ERROR_SUCCESS); + dwLen = 23; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData, &dwLen, 24); + ok(result && dwLen == 24 && !memcmp(ecb, abData, sizeof(ecb)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData, &dwLen); + ok(result && dwLen == 23 && !memcmp(plain, abData, sizeof(plain)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwMode = CRYPT_MODE_CBC; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = 23; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData, &dwLen, 24); + ok(result && dwLen == 24 && !memcmp(cbc, abData, sizeof(cbc)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData, &dwLen); + ok(result && dwLen == 23 && !memcmp(plain, abData, sizeof(plain)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwMode = CRYPT_MODE_CFB; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = 16; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, FALSE, 0, abData, &dwLen, 24); + ok(result && dwLen == 16, "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwLen = 7; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData+16, &dwLen, 8); + ok(result && dwLen == 8 && !memcmp(cfb, abData, sizeof(cfb)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwLen = 8; + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, FALSE, 0, abData, &dwLen); + ok(result && dwLen == 8, "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwLen = 16; + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData+8, &dwLen); + ok(result && dwLen == 15 && !memcmp(plain, abData, sizeof(plain)), + "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + dwMode = CRYPT_MODE_OFB; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = 23; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abData, &dwLen, 24); + ok(!result && GetLastError() == NTE_BAD_ALGID, "%08lx\n", GetLastError()); +} + +static void test_3des112() +{ + HCRYPTKEY hKey; + BOOL result; + DWORD dwLen; + unsigned char pbData[16]; + int i; + + result = derive_key(CALG_3DES_112, &hKey, 0); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError() == NTE_BAD_ALGID, "%08lx\n", GetLastError()); + return; + } + + for (i=0; i<sizeof(pbData); i++) pbData[i] = (unsigned char)i; + + dwLen = 13; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen, 16); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); +} + +static void test_des() +{ + HCRYPTKEY hKey; + BOOL result; + DWORD dwLen, dwMode; + unsigned char pbData[16]; + int i; + + result = derive_key(CALG_DES, &hKey, 56); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError()==NTE_BAD_ALGID, "%08lx\n", GetLastError()); + return; + } + + dwMode = CRYPT_MODE_ECB; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + for (i=0; i<sizeof(pbData); i++) pbData[i] = (unsigned char)i; + + dwLen = 13; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen, 16); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); +} + +static void test_3des() +{ + HCRYPTKEY hKey; + BOOL result; + DWORD dwLen; + unsigned char pbData[16]; + static const BYTE des3[16] = { + 0x7b, 0xba, 0xdd, 0xa2, 0x39, 0xd3, 0x7b, 0xb3, + 0xc7, 0x51, 0x81, 0x41, 0x53, 0xe8, 0xcf, 0xeb }; + int i; + + result = derive_key(CALG_3DES, &hKey, 0); + if (!result) return; + + for (i=0; i<sizeof(pbData); i++) pbData[i] = (unsigned char)i; + + dwLen = 13; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen, 16); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbData, des3, sizeof(des3)), "3DES encryption failed!\n"); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwLen); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); +} + +static void test_rc2() +{ + static const BYTE rc2encrypted[16] = { + 0x02, 0x34, 0x7d, 0xf6, 0x1d, 0xc5, 0x9b, 0x8b, + 0x2e, 0x0d, 0x63, 0x80, 0x72, 0xc1, 0xc2, 0xb1 }; + HCRYPTHASH hHash; + HCRYPTKEY hKey; + BOOL result; + DWORD dwLen, dwKeyLen, dwDataLen, dwMode, dwModeBits; + BYTE *pbTemp; + unsigned char pbData[2000], pbHashValue[16]; + int i; + + for (i=0; i<2000; i++) pbData[i] = (unsigned char)i; + + /* MD2 Hashing */ + result = CryptCreateHash(hProv, CALG_MD2, 0, 0, &hHash); + if (!result) { + ok(GetLastError()==NTE_BAD_ALGID, "%08lx\n", GetLastError()); + } else { + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = 16; + result = CryptGetHashParam(hHash, HP_HASHVAL, pbHashValue, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDeriveKey(hProv, CALG_RC2, hHash, 56 << 16, &hKey); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_KEYLEN, (BYTE*)&dwKeyLen, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + dwMode = CRYPT_MODE_CBC; + result = CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_MODE_BITS, (BYTE*)&dwModeBits, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_PERMISSIONS, (BYTE*)&dwModeBits, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE*)&dwModeBits, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptGetKeyParam(hKey, KP_IV, NULL, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + pbTemp = HeapAlloc(GetProcessHeap(), 0, dwLen); + CryptGetKeyParam(hKey, KP_IV, pbTemp, &dwLen, 0); + HeapFree(GetProcessHeap(), 0, pbTemp); + + result = CryptGetKeyParam(hKey, KP_SALT, NULL, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + pbTemp = HeapAlloc(GetProcessHeap(), 0, dwLen); + CryptGetKeyParam(hKey, KP_SALT, pbTemp, &dwLen, 0); + HeapFree(GetProcessHeap(), 0, pbTemp); + + dwLen = sizeof(DWORD); + CryptGetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, &dwLen, 0); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + dwDataLen = 13; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwDataLen, 24); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbData, rc2encrypted, 8), "RC2 encryption failed!\n"); + + result = CryptGetKeyParam(hKey, KP_IV, NULL, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + pbTemp = HeapAlloc(GetProcessHeap(), 0, dwLen); + CryptGetKeyParam(hKey, KP_IV, pbTemp, &dwLen, 0); + HeapFree(GetProcessHeap(), 0, pbTemp); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwDataLen); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); + } +} + +static void test_rc4() +{ + static const BYTE rc4[16] = { + 0x17, 0x0c, 0x44, 0x8e, 0xae, 0x90, 0xcd, 0xb0, + 0x7f, 0x87, 0xf5, 0x7a, 0xec, 0xb2, 0x2e, 0x35 }; + BOOL result; + HCRYPTHASH hHash; + HCRYPTKEY hKey; + DWORD dwDataLen = 5, dwKeyLen, dwLen = sizeof(DWORD), dwMode; + unsigned char pbData[2000], *pbTemp; + unsigned char pszBuffer[256]; + int i; + + for (i=0; i<2000; i++) pbData[i] = (unsigned char)i; + + /* MD2 Hashing */ + result = CryptCreateHash(hProv, CALG_MD2, 0, 0, &hHash); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError() == NTE_BAD_ALGID, "%08lx\n", GetLastError()); + } else { + result = CryptHashData(hHash, (BYTE*)pbData, sizeof(pbData), 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = 16; + result = CryptGetHashParam(hHash, HP_HASHVAL, pszBuffer, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDeriveKey(hProv, CALG_RC4, hHash, 56 << 16, &hKey); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_KEYLEN, (BYTE*)&dwKeyLen, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(DWORD); + result = CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE*)&dwKeyLen, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptGetKeyParam(hKey, KP_IV, NULL, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + pbTemp = HeapAlloc(GetProcessHeap(), 0, dwLen); + CryptGetKeyParam(hKey, KP_IV, pbTemp, &dwLen, 0); + HeapFree(GetProcessHeap(), 0, pbTemp); + + result = CryptGetKeyParam(hKey, KP_SALT, NULL, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + pbTemp = HeapAlloc(GetProcessHeap(), 0, dwLen); + CryptGetKeyParam(hKey, KP_SALT, pbTemp, &dwLen, 0); + HeapFree(GetProcessHeap(), 0, pbTemp); + + dwLen = sizeof(DWORD); + CryptGetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, &dwLen, 0); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + dwDataLen = 16; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwDataLen, 24); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(pbData, rc4, dwDataLen), "RC4 encryption failed!\n"); + + result = CryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, pbData, &dwDataLen); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); + } +} + +static void test_hmac() { + HCRYPTKEY hKey; + HCRYPTHASH hHash; + BOOL result; + HMAC_INFO hmacInfo = { CALG_MD2, NULL, 0, NULL, 0 }; + DWORD dwLen; + BYTE abData[256]; + static const BYTE hmac[16] = { + 0xfd, 0x16, 0xb5, 0xb6, 0x13, 0x1c, 0x2b, 0xd6, + 0x0a, 0xc7, 0xae, 0x92, 0x76, 0xa3, 0x05, 0x71 }; + int i; + + for (i=0; i<sizeof(abData)/sizeof(BYTE); i++) abData[i] = (BYTE)i; + + if (!derive_key(CALG_RC2, &hKey, 56)) return; + + result = CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHash); + ok(result, "%08lx\n", GetLastError()); + if (!result) return; + + result = CryptSetHashParam(hHash, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0); + ok(result, "%08lx\n", GetLastError()); + + result = CryptHashData(hHash, (BYTE*)abData, sizeof(abData), 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(abData)/sizeof(BYTE); + result = CryptGetHashParam(hHash, HP_HASHVAL, abData, &dwLen, 0); + ok(result, "%08lx\n", GetLastError()); + + ok(!memcmp(abData, hmac, sizeof(hmac)), "HMAC failed!\n"); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); + + /* Provoke errors */ + result = CryptCreateHash(hProv, CALG_HMAC, 0, 0, &hHash); + ok(!result && GetLastError() == NTE_BAD_KEY, "%08lx\n", GetLastError()); +} + +static void test_mac() { + HCRYPTKEY hKey; + HCRYPTHASH hHash; + BOOL result; + DWORD dwLen; + BYTE abData[256], abEnc[264]; + static const BYTE mac[8] = { 0x0d, 0x3e, 0x15, 0x6b, 0x85, 0x63, 0x5c, 0x11 }; + int i; + + for (i=0; i<sizeof(abData)/sizeof(BYTE); i++) abData[i] = (BYTE)i; + for (i=0; i<sizeof(abData)/sizeof(BYTE); i++) abEnc[i] = (BYTE)i; + + if (!derive_key(CALG_RC2, &hKey, 56)) return; + + dwLen = 256; + result = CryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, abEnc, &dwLen, 264); + ok (result && dwLen == 264, "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + result = CryptCreateHash(hProv, CALG_MAC, hKey, 0, &hHash); + ok(result, "%08lx\n", GetLastError()); + if (!result) return; + + result = CryptHashData(hHash, (BYTE*)abData, sizeof(abData), 0); + ok(result, "%08lx\n", GetLastError()); + + dwLen = sizeof(abData)/sizeof(BYTE); + result = CryptGetHashParam(hHash, HP_HASHVAL, abData, &dwLen, 0); + ok(result && dwLen == 8, "%08lx, dwLen: %ld\n", GetLastError(), dwLen); + + ok(!memcmp(abData, mac, sizeof(mac)), "MAC failed!\n"); + + result = CryptDestroyHash(hHash); + ok(result, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); + + /* Provoke errors */ + if (!derive_key(CALG_RC4, &hKey, 56)) return; + + result = CryptCreateHash(hProv, CALG_MAC, hKey, 0, &hHash); + ok(!result && GetLastError() == NTE_BAD_KEY, "%08lx\n", GetLastError()); + + result = CryptDestroyKey(hKey); + ok(result, "%08lx\n", GetLastError()); +} + +static void test_import_private() +{ + DWORD dwLen; + HCRYPTKEY hKeyExchangeKey, hSessionKey; + BOOL result; + BYTE abPlainPrivateKey[596] = { + 0x07, 0x02, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, + 0x52, 0x53, 0x41, 0x32, 0x00, 0x04, 0x00, 0x00, + 0x01, 0x00, 0x01, 0x00, 0x9b, 0x64, 0xef, 0xce, + 0x31, 0x7c, 0xad, 0x56, 0xe2, 0x1e, 0x9b, 0x96, + 0xb3, 0xf0, 0x29, 0x88, 0x6e, 0xa8, 0xc2, 0x11, + 0x33, 0xd6, 0xcc, 0x8c, 0x69, 0xb2, 0x1a, 0xfd, + 0xfc, 0x23, 0x21, 0x30, 0x4d, 0x29, 0x45, 0xb6, + 0x3a, 0x67, 0x11, 0x80, 0x1a, 0x91, 0xf2, 0x9f, + 0x01, 0xac, 0xc0, 0x11, 0x50, 0x5f, 0xcd, 0xb9, + 0xad, 0x76, 0x9f, 0x6e, 0x91, 0x55, 0x71, 0xda, + 0x97, 0x96, 0x96, 0x22, 0x75, 0xb4, 0x83, 0x44, + 0x89, 0x9e, 0xf8, 0x44, 0x40, 0x7c, 0xd6, 0xcd, + 0x9d, 0x88, 0xd6, 0x88, 0xbc, 0x56, 0xb7, 0x64, + 0xe9, 0x2c, 0x24, 0x2f, 0x0d, 0x78, 0x55, 0x1c, + 0xb2, 0x67, 0xb1, 0x5e, 0xbc, 0x0c, 0xcf, 0x1c, + 0xe9, 0xd3, 0x9e, 0xa2, 0x15, 0x24, 0x73, 0xd6, + 0xdb, 0x6f, 0x83, 0xb2, 0xf8, 0xbc, 0xe7, 0x47, + 0x3b, 0x01, 0xef, 0x49, 0x08, 0x98, 0xd6, 0xa3, + 0xf9, 0x25, 0x57, 0xe9, 0x39, 0x3c, 0x53, 0x30, + 0x1b, 0xf2, 0xc9, 0x62, 0x31, 0x43, 0x5d, 0x84, + 0x24, 0x30, 0x21, 0x9a, 0xad, 0xdb, 0x62, 0x91, + 0xc8, 0x07, 0xd9, 0x2f, 0xd6, 0xb5, 0x37, 0x6f, + 0xfe, 0x7a, 0x12, 0xbc, 0xd9, 0xd2, 0x2b, 0xbf, + 0xd7, 0xb1, 0xfa, 0x7d, 0xc0, 0x48, 0xdd, 0x74, + 0xdd, 0x55, 0x04, 0xa1, 0x8b, 0xc1, 0x0a, 0xc4, + 0xa5, 0x57, 0x62, 0xee, 0x08, 0x8b, 0xf9, 0x19, + 0x6c, 0x52, 0x06, 0xf8, 0x73, 0x0f, 0x24, 0xc9, + 0x71, 0x9f, 0xc5, 0x45, 0x17, 0x3e, 0xae, 0x06, + 0x81, 0xa2, 0x96, 0x40, 0x06, 0xbf, 0xeb, 0x9e, + 0x80, 0x2b, 0x27, 0x20, 0x8f, 0x38, 0xcf, 0xeb, + 0xff, 0x3b, 0x38, 0x41, 0x35, 0x69, 0x66, 0x13, + 0x1d, 0x3c, 0x01, 0x3b, 0xf6, 0x37, 0xca, 0x9c, + 0x61, 0x74, 0x98, 0xcf, 0xc9, 0x6e, 0xe8, 0x90, + 0xc7, 0xb7, 0x33, 0xc0, 0x07, 0x3c, 0xf8, 0xc8, + 0xf6, 0xf2, 0xd7, 0xf0, 0x21, 0x62, 0x58, 0x8a, + 0x55, 0xbf, 0xa1, 0x2d, 0x3d, 0xa6, 0x69, 0xc5, + 0x02, 0x19, 0x31, 0xf0, 0x94, 0x0f, 0x45, 0x5c, + 0x95, 0x1b, 0x53, 0xbc, 0xf5, 0xb0, 0x1a, 0x8f, + 0xbf, 0x40, 0xe0, 0xc7, 0x73, 0xe7, 0x72, 0x6e, + 0xeb, 0xb1, 0x0f, 0x38, 0xc5, 0xf8, 0xee, 0x04, + 0xed, 0x34, 0x1a, 0x10, 0xf9, 0x53, 0x34, 0xf3, + 0x3e, 0xe6, 0x5c, 0xd1, 0x47, 0x65, 0xcd, 0xbd, + 0xf1, 0x06, 0xcb, 0xb4, 0xb1, 0x26, 0x39, 0x9f, + 0x71, 0xfe, 0x3d, 0xf8, 0x62, 0xab, 0x22, 0x8b, + 0x0e, 0xdc, 0xb9, 0xe8, 0x74, 0x06, 0xfc, 0x8c, + 0x25, 0xa1, 0xa9, 0xcf, 0x07, 0xf9, 0xac, 0x21, + 0x01, 0x7b, 0x1c, 0xdc, 0x94, 0xbd, 0x47, 0xe1, + 0xa0, 0x86, 0x59, 0x35, 0x6a, 0x6f, 0xb9, 0x70, + 0x26, 0x7c, 0x3c, 0xfd, 0xbd, 0x81, 0x39, 0x36, + 0x42, 0xc2, 0xbd, 0xbe, 0x84, 0x27, 0x9a, 0x69, + 0x81, 0xda, 0x99, 0x27, 0xc2, 0x4f, 0x62, 0x33, + 0xf4, 0x79, 0x30, 0xc5, 0x63, 0x54, 0x71, 0xf1, + 0x47, 0x22, 0x25, 0x9b, 0x6c, 0x00, 0x2f, 0x1c, + 0xf4, 0x1f, 0x85, 0xbc, 0xf6, 0x67, 0x6a, 0xe3, + 0xf6, 0x55, 0x8a, 0xef, 0xd0, 0x0b, 0xd3, 0xa2, + 0xc5, 0x51, 0x70, 0x15, 0x0a, 0xf0, 0x98, 0x4c, + 0xb7, 0x19, 0x62, 0x0e, 0x2d, 0x2a, 0x4a, 0x7d, + 0x7a, 0x0a, 0xc4, 0x17, 0xe3, 0x5d, 0x20, 0x52, + 0xa9, 0x98, 0xc3, 0xaa, 0x11, 0xf6, 0xbf, 0x4c, + 0x94, 0x99, 0x81, 0x89, 0xf0, 0x7f, 0x66, 0xaa, + 0xc8, 0x88, 0xd7, 0x31, 0x84, 0x71, 0xb6, 0x64, + 0x09, 0x76, 0x0b, 0x7f, 0x1a, 0x1f, 0x2e, 0xfe, + 0xcd, 0x59, 0x2a, 0x54, 0x11, 0x84, 0xd4, 0x6a, + 0x61, 0xdf, 0xaa, 0x76, 0x66, 0x9d, 0x82, 0x11, + 0x56, 0x3d, 0xd2, 0x52, 0xe6, 0x42, 0x5a, 0x77, + 0x92, 0x98, 0x34, 0xf3, 0x56, 0x6c, 0x96, 0x10, + 0x40, 0x59, 0x16, 0xcb, 0x77, 0x61, 0xe3, 0xbf, + 0x4b, 0xd4, 0x39, 0xfb, 0xb1, 0x4e, 0xc1, 0x74, + 0xec, 0x7a, 0xea, 0x3d, 0x68, 0xbb, 0x0b, 0xe6, + 0xc6, 0x06, 0xbf, 0xdd, 0x7f, 0x94, 0x42, 0xc0, + 0x0f, 0xe4, 0x92, 0x33, 0x6c, 0x6e, 0x1b, 0xba, + 0x73, 0xf9, 0x79, 0x84, 0xdf, 0x45, 0x00, 0xe4, + 0x94, 0x88, 0x9d, 0x08, 0x89, 0xcf, 0xf2, 0xa4, + 0xc5, 0x47, 0x45, 0x85, 0x86, 0xa5, 0xcc, 0xa8, + 0xf2, 0x5d, 0x58, 0x07 + }; + BYTE abSessionKey[148] = { + 0x01, 0x02, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, + 0x00, 0xa4, 0x00, 0x00, 0xb8, 0xa4, 0xdf, 0x5e, + 0x9e, 0xb1, 0xbf, 0x85, 0x3d, 0x24, 0x2d, 0x1e, + 0x69, 0xb7, 0x67, 0x13, 0x8e, 0x78, 0xf2, 0xdf, + 0xc6, 0x69, 0xce, 0x46, 0x7e, 0xf2, 0xf2, 0x33, + 0x20, 0x6f, 0xa1, 0xa5, 0x59, 0x83, 0x25, 0xcb, + 0x3a, 0xb1, 0x8a, 0x12, 0x63, 0x02, 0x3c, 0xfb, + 0x4a, 0xfa, 0xef, 0x8e, 0xf7, 0x29, 0x57, 0xb1, + 0x9e, 0xa7, 0xf3, 0x02, 0xfd, 0xca, 0xdf, 0x5a, + 0x1f, 0x71, 0xb6, 0x26, 0x09, 0x24, 0x39, 0xda, + 0xc0, 0xde, 0x2a, 0x0e, 0xcd, 0x1f, 0xe5, 0xb6, + 0x4f, 0x82, 0xa0, 0xa9, 0x90, 0xd3, 0xd9, 0x6a, + 0x43, 0x14, 0x2a, 0xf7, 0xac, 0xd5, 0xa0, 0x54, + 0x93, 0xc4, 0xb9, 0xe7, 0x24, 0x84, 0x4d, 0x69, + 0x5e, 0xcc, 0x2a, 0x32, 0xb5, 0xfb, 0xe4, 0xb4, + 0x08, 0xd5, 0x36, 0x58, 0x59, 0x40, 0xfb, 0x29, + 0x7f, 0xa7, 0x17, 0x25, 0xc4, 0x0d, 0x78, 0x37, + 0x04, 0x8c, 0x49, 0x92 + }; + BYTE abEncryptedMessage[12] = { + 0x40, 0x64, 0x28, 0xe8, 0x8a, 0xe7, 0xa4, 0xd4, + 0x1c, 0xfd, 0xde, 0x71 + }; + + dwLen = (DWORD)sizeof(abPlainPrivateKey); + result = CryptImportKey(hProv, abPlainPrivateKey, dwLen, 0, 0, &hKeyExchangeKey); + if (!result) { + /* rsaenh compiled without OpenSSL */ + ok(GetLastError() == NTE_FAIL, "%08lx\n", GetLastError()); + return; + } + + dwLen = (DWORD)sizeof(abSessionKey); + result = CryptImportKey(hProv, abSessionKey, dwLen, hKeyExchangeKey, 0, &hSessionKey); + ok(result, "%08lx\n", GetLastError()); + if (!result) return; + + dwLen = (DWORD)sizeof(abEncryptedMessage); + result = CryptDecrypt(hSessionKey, 0, TRUE, 0, abEncryptedMessage, &dwLen); + ok(result && dwLen == 12 && !strcmp(abEncryptedMessage, "Wine rocks!"), + "%08lx, len: %ld\n", GetLastError(), dwLen); + + if (!derive_key(CALG_RC4, &hSessionKey, 56)) return; + + dwLen = (DWORD)sizeof(abSessionKey); + result = CryptExportKey(hSessionKey, hKeyExchangeKey, SIMPLEBLOB, 0, abSessionKey, &dwLen); + ok(result, "%08lx\n", GetLastError()); + if (!result) return; + + dwLen = (DWORD)sizeof(abSessionKey); + result = CryptImportKey(hProv, abSessionKey, dwLen, hKeyExchangeKey, 0, &hSessionKey); + ok(result, "%08lx\n", GetLastError()); + if (!result) return; +} + +START_TEST(rsaenh) +{ + if (!init_environment()) + return; + test_prov(); + test_gen_random(); + test_hashes(); + test_rc4(); + test_rc2(); + test_des(); + test_3des112(); + test_3des(); + test_hmac(); + test_mac(); + test_block_cipher_modes(); + test_import_private(); + clean_up_environment(); +} diff --git a/include/config.h.in b/include/config.h.in index a7867911863..5e9cf62e011 100644 --- a/include/config.h.in +++ b/include/config.h.in @@ -431,6 +431,21 @@ /* Define if OpenGL is present on the system */ #undef HAVE_OPENGL +/* Define to 1 if you have the <openssl/des.h> header file. */ +#undef HAVE_OPENSSL_DES_H + +/* Define to 1 if you have the <openssl/md2.h> header file. */ +#undef HAVE_OPENSSL_MD2_H + +/* Define to 1 if you have the <openssl/rc2.h> header file. */ +#undef HAVE_OPENSSL_RC2_H + +/* Define to 1 if you have the <openssl/rc4.h> header file. */ +#undef HAVE_OPENSSL_RC4_H + +/* Define to 1 if you have the <openssl/rsa.h> header file. */ +#undef HAVE_OPENSSL_RSA_H + /* Define to 1 if you have the <openssl/ssl.h> header file. */ #undef HAVE_OPENSSL_SSL_H diff --git a/tools/wine.inf b/tools/wine.inf index 4f93e2c804f..cbf58248a34 100644 --- a/tools/wine.inf +++ b/tools/wine.inf @@ -2005,7 +2005,7 @@ HKLM,%CurrentVersion%\Telephony\Country List\998,"SameAreaRule",,"G" 11,,ole32.dll,1 11,,oleaut32.dll,1 11,,quartz.dll,1 -11,,rsabase.dll,1 +11,,rsaenh.dll,1 11,,shdocvw.dll,1 11,,shell32.dll,1 11,,urlmon.dll,1 -- GitLab