crypt.c 63.6 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright 1999 Ian Schmidt
 * Copyright 2001 Travis Michielsen
 *
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18
 */
19

20 21 22 23 24 25
/***********************************************************************
 *
 *  TODO:
 *  - Reference counting
 *  - Thread-safing
 */
26

27
#include "config.h"
28
#include "wine/port.h"
29

30 31
#include <time.h>
#include <stdlib.h>
32
#include <stdio.h>
33
#include <sys/types.h>
34 35 36
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
37
#include <fcntl.h>
38 39 40
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
41

42 43
#include "ntstatus.h"
#define WIN32_NO_STATUS
44 45 46
#include "crypt.h"
#include "winnls.h"
#include "winreg.h"
47
#include "rpc.h"
48
#include "wine/debug.h"
49
#include "wine/unicode.h"
50
#include "winternl.h"
51

52
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
53

54
static HWND crypt_hWindow;
55

56
#define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
57 58
#define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))

59
static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
60
{
61 62 63 64 65 66 67 68
	static const WCHAR KEYSTR[] = {
                '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','\\',0
	};
	PWSTR keyname;
69

70
	keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
71 72
	if (keyname)
	{
73 74
		strcpyW(keyname, KEYSTR);
		strcpyW(keyname + strlenW(KEYSTR), pProvName);
75 76 77 78 79
	} else
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
	return keyname;
}

80
static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
81
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	static const WCHAR MACHINESTR[] = {
                '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',' ','X','X','X',0
	};
	static const WCHAR USERSTR[] = {
                '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','\\',
                'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
	};
	PWSTR keyname;
	PWSTR ptr;
98

99
	keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
100 101
	if (keyname)
	{
102 103
		user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
		ptr = keyname + strlenW(keyname);
104
		*(--ptr) = (dwType % 10) + '0';
105
		*(--ptr) = ((dwType / 10) % 10) + '0';
106 107 108 109 110 111 112 113 114 115 116
		*(--ptr) = (dwType / 100) + '0';
	} else
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
	return keyname;
}

/* CRYPT_UnicodeTOANSI
 * wstr - unicode string
 * str - pointer to ANSI string
 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
 *
117
 * returns TRUE if unsuccessful, FALSE otherwise.
118 119 120 121 122 123 124 125 126 127 128 129 130 131
 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
 */
static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
{
	int count;

	if (!wstr)
	{
		*str = NULL;
		return TRUE;
	}
	count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	if (strsize == -1)
		*str = CRYPT_Alloc(count * sizeof(CHAR));
132 133
	else
		count = min( count, strsize );
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	if (*str)
	{
		WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
		return TRUE;
	}
	SetLastError(ERROR_NOT_ENOUGH_MEMORY);
	return FALSE;
}

/* CRYPT_ANSITOUnicode
 * str - ANSI string
 * wstr - pointer to unicode string
 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
 */
static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
{
150
	unsigned int wcount;
151 152 153 154 155 156 157 158 159

	if (!str)
	{
		*wstr = NULL;
		return TRUE;
	}
	wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
	if (wstrsize == -1)
		*wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
160 161
	else
		wcount = min( wcount, wstrsize/sizeof(WCHAR) );
162 163 164 165 166 167 168 169 170
	if (*wstr)
	{
		MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
		return TRUE;
	}
	SetLastError(ERROR_NOT_ENOUGH_MEMORY);
	return FALSE;
}

171
/* These next 2 functions are used by the VTableProvStruc structure */
172
static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
173 174 175 176 177 178
{
	if (!lpszImage || !pData)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
179

180
	FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
181

182 183 184
	return TRUE;
}

185
static BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
186 187 188 189 190 191 192 193 194 195 196
{
	if (!phWnd)
		return FALSE;
	*phWnd = crypt_hWindow;
	return TRUE;
}

#define CRYPT_GetProvFunc(name) \
	if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
#define CRYPT_GetProvFuncOpt(name) \
	provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
197
static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
198 199 200 201 202 203 204
{
	PCRYPTPROV provider;
	DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;

	if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
	if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
	if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
205
	if ( !(provider->hModule = LoadLibraryW(pImage)) )
206 207
	{
		errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
208
		FIXME("Failed to load dll %s\n", debugstr_w(pImage));
209 210
		goto error;
	}
211
	provider->dwMagic = MAGIC_CRYPTPROV;
212
	provider->refcount = 1;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	errorcode = NTE_PROVIDER_DLL_FAIL;
	CRYPT_GetProvFunc(CPAcquireContext);
	CRYPT_GetProvFunc(CPCreateHash);
	CRYPT_GetProvFunc(CPDecrypt);
	CRYPT_GetProvFunc(CPDeriveKey);
	CRYPT_GetProvFunc(CPDestroyHash);
	CRYPT_GetProvFunc(CPDestroyKey);
	CRYPT_GetProvFuncOpt(CPDuplicateHash);
	CRYPT_GetProvFuncOpt(CPDuplicateKey);
	CRYPT_GetProvFunc(CPEncrypt);
	CRYPT_GetProvFunc(CPExportKey);
	CRYPT_GetProvFunc(CPGenKey);
	CRYPT_GetProvFunc(CPGenRandom);
	CRYPT_GetProvFunc(CPGetHashParam);
	CRYPT_GetProvFunc(CPGetKeyParam);
	CRYPT_GetProvFunc(CPGetProvParam);
	CRYPT_GetProvFunc(CPGetUserKey);
	CRYPT_GetProvFunc(CPHashData);
	CRYPT_GetProvFunc(CPHashSessionKey);
	CRYPT_GetProvFunc(CPImportKey);
	CRYPT_GetProvFunc(CPReleaseContext);
	CRYPT_GetProvFunc(CPSetHashParam);
	CRYPT_GetProvFunc(CPSetKeyParam);
	CRYPT_GetProvFunc(CPSetProvParam);
	CRYPT_GetProvFunc(CPSignHash);
	CRYPT_GetProvFunc(CPVerifySignature);

241 242 243
	/* FIXME: Not sure what the pbContextInfo field is for.
	 *        Does it need memory allocation?
         */
244
	provider->pVTable->Version = 3;
245 246
	provider->pVTable->pFuncVerifyImage = (FARPROC)CRYPT_VerifyImage;
	provider->pVTable->pFuncReturnhWnd = (FARPROC)CRYPT_ReturnhWnd;
247 248 249 250 251 252 253 254 255 256
	provider->pVTable->dwProvType = 0;
	provider->pVTable->pbContextInfo = NULL;
	provider->pVTable->cbContextInfo = 0;
	provider->pVTable->pszProvName = NULL;
	return provider;

error:
	SetLastError(errorcode);
	if (provider)
	{
257
		provider->dwMagic = 0;
258 259 260 261 262 263 264 265 266 267 268
		if (provider->hModule)
			FreeLibrary(provider->hModule);
		CRYPT_Free(provider->pVTable);
		CRYPT_Free(provider->pFuncs);
		CRYPT_Free(provider);
	}
	return NULL;
}
#undef CRYPT_GetProvFunc
#undef CRYPT_GetProvFuncOpt

269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static void CRYPT_CreateMachineGuid(void)
{
	static const WCHAR cryptographyW[] = {
                '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',0 };
	static const WCHAR machineGuidW[] = {
		'M','a','c','h','i','n','e','G','u','i','d',0 };
	LONG r;
	HKEY key;

	r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, cryptographyW, 0, KEY_ALL_ACCESS,
			  &key);
	if (!r)
	{
		DWORD size;

		r = RegQueryValueExW(key, machineGuidW, NULL, NULL, NULL, &size);
		if (r == ERROR_FILE_NOT_FOUND)
		{
			static const WCHAR rpcrt4[] = {
				'r','p','c','r','t','4',0 };
			HMODULE lib = LoadLibraryW(rpcrt4);

			if (lib)
			{
				RPC_STATUS (RPC_ENTRY *pUuidCreate)(UUID *);
				UUID uuid;
298 299 300 301 302 303 304 305 306
                                WCHAR buf[37];
                                RPC_STATUS rs;
                                static const WCHAR uuidFmt[] = {
                                    '%','0','8','x','-','%','0','4','x','-',
                                    '%','0','4','x','-','%','0','2','x',
                                    '%','0','2','x','-','%','0','2','x',
                                    '%','0','2','x','%','0','2','x',
                                    '%','0','2','x','%','0','2','x',
                                    '%','0','2','x',0 };
307 308

				pUuidCreate = GetProcAddress(lib, "UuidCreate");
309 310 311 312 313 314 315 316 317 318 319 320 321
                                rs = pUuidCreate(&uuid);
                                if (rs == S_OK)
                                {
                                    sprintfW(buf, uuidFmt,
                                             uuid.Data1, uuid.Data2, uuid.Data3,
                                             uuid.Data4[0], uuid.Data4[1],
                                             uuid.Data4[2], uuid.Data4[3],
                                             uuid.Data4[4], uuid.Data4[5],
                                             uuid.Data4[6], uuid.Data4[7] );
                                    RegSetValueExW(key, machineGuidW, 0, REG_SZ,
                                                   (const BYTE *)buf,
                                                   (lstrlenW(buf)+1)*sizeof(WCHAR));
                                }
322 323 324 325 326 327 328
				FreeLibrary(lib);
			}
		}
		RegCloseKey(key);
	}
}

329
/******************************************************************************
330
 * CryptAcquireContextW (ADVAPI32.@)
331
 *
332
 * Acquire a crypto provider context handle.
333
 *
334
 * PARAMS
335 336 337 338 339 340 341 342
 *  phProv       [O] Pointer to HCRYPTPROV for the output.
 *  pszContainer [I] Key Container Name
 *  pszProvider  [I] Cryptographic Service Provider Name
 *  dwProvType   [I] Crypto provider type to get a handle.
 *  dwFlags      [I] flags for the operation
 *
 * RETURNS 
 *  TRUE on success, FALSE on failure.
343
 */
344 345
BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
		LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
346
{
347 348
	PCRYPTPROV pProv = NULL;
	HKEY key;
349 350
	PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
	PSTR provnameA = NULL, pszContainerA = NULL;
351
	DWORD keytype, type, len;
352
	ULONG r;
353 354 355
	static const WCHAR nameW[] = {'N','a','m','e',0};
	static const WCHAR typeW[] = {'T','y','p','e',0};
	static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
356

357
	TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
358
		debugstr_w(pszProvider), dwProvType, dwFlags);
359

360
	if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
361
	{
362
		SetLastError(NTE_BAD_PROV_TYPE);
363 364
		return FALSE;
	}
365 366
	
	if (!phProv)
367
	{
368
		SetLastError(ERROR_INVALID_PARAMETER);
369 370 371
		return FALSE;
	}

372 373 374
	/* Make sure the MachineGuid value exists */
	CRYPT_CreateMachineGuid();

375
	if (!pszProvider || !*pszProvider)
376
	{
377 378 379
		/* No CSP name specified so try the user default CSP first
		 * then try the machine default CSP
		 */
380
		if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
381
			TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
382 383
			SetLastError(NTE_PROV_TYPE_NOT_DEF);
			return FALSE;
384
		}
385
		if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
386 387
		{
			CRYPT_Free(keyname);
388
			if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
389
				TRACE("No type registered for crypto provider type %d.\n", dwProvType);
390 391 392
				RegCloseKey(key);
				SetLastError(NTE_PROV_TYPE_NOT_DEF);
				goto error;
393
			}
394 395
			if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
				TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
396
				CRYPT_Free(keyname);
397 398
				RegCloseKey(key);
				SetLastError(NTE_PROV_TYPE_NOT_DEF);
399 400
				goto error;
			}
401 402
		}
		CRYPT_Free(keyname);
403
		r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
404
		if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
405
		{
406
			TRACE("error %d reading size of 'Name' from registry\n", r );
407 408 409 410 411 412 413 414 415 416
			RegCloseKey(key);
			SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
			goto error;
		}
		if(!(provname = CRYPT_Alloc(len)))
		{
			RegCloseKey(key);
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			goto error;
		}
417
		r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
418 419
		if( r != ERROR_SUCCESS )
		{
420
			TRACE("error %d reading 'Name' from registry\n", r );
421 422 423 424
			RegCloseKey(key);
			SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
			goto error;
		}
425 426
		RegCloseKey(key);
	} else {
427
		if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
428
		{
429
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
430 431
			goto error;
		}
432
		strcpyW(provname, pszProvider);
433 434 435
	}

	keyname = CRYPT_GetProvKeyName(provname);
436
	r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
437
	CRYPT_Free(keyname);
438 439 440 441 442
	if (r != ERROR_SUCCESS)
	{
		SetLastError(NTE_KEYSET_NOT_DEF);
		goto error;
	}
443
	len = sizeof(DWORD);
444
	r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
445
	if (r != ERROR_SUCCESS)
446
	{
447 448 449 450 451
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
	if (type != dwProvType)
	{
452
		TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
453
		SetLastError(NTE_PROV_TYPE_NO_MATCH);
454 455 456
		goto error;
	}

457
	r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
458
	if ( r != ERROR_SUCCESS || keytype != REG_SZ)
459
	{
460
		TRACE("error %d reading size of 'Image Path' from registry\n", r );
461 462 463 464 465 466 467 468 469 470
		RegCloseKey(key);
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
	if (!(temp = CRYPT_Alloc(len)))
	{
		RegCloseKey(key);
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		goto error;
	}
471
	r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
472 473
	if( r != ERROR_SUCCESS )
	{
474
		TRACE("error %d reading 'Image Path' from registry\n", r );
475 476 477 478
		RegCloseKey(key);
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
479
	RegCloseKey(key);
480 481
	len = ExpandEnvironmentStringsW(temp, NULL, 0);
	if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
482 483 484 485
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		goto error;
	}
486
	if (!ExpandEnvironmentStringsW(temp, imagepath, len))
487 488 489 490
	{
		/* ExpandEnvironmentStrings will call SetLastError */
		goto error;
	}
491
	pProv = CRYPT_LoadProvider(imagepath);
492
	if (!pProv) {
493
		/* CRYPT_LoadProvider calls SetLastError */
494 495
		goto error;
	}
496
	pProv->pVTable->dwProvType = dwProvType;
497 498 499 500 501 502 503 504 505 506 507 508
	if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
	{
		/* CRYPT_UnicodeToANSI calls SetLastError */
		goto error;
	}
	pProv->pVTable->pszProvName = provnameA;
	if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
	{
		/* CRYPT_UnicodeToANSI calls SetLastError */
		goto error;
	}
	if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
509
	{
510 511 512
		/* MSDN: When this flag is set, the value returned in phProv is undefined,
		 *       and thus, the CryptReleaseContext function need not be called afterwards.
		 *       Therefore, we must clean up everything now.
513
		 */
514 515
		if (dwFlags & CRYPT_DELETEKEYSET)
		{
516
			pProv->dwMagic = 0;
517
			FreeLibrary(pProv->hModule);
518 519
			CRYPT_Free(provnameA);
			CRYPT_Free(pProv->pVTable);
520 521 522 523 524
			CRYPT_Free(pProv->pFuncs);
			CRYPT_Free(pProv);
		} else {
			*phProv = (HCRYPTPROV)pProv;
		}
525 526
		CRYPT_Free(pszContainerA);
		CRYPT_Free(provname);
527 528
		CRYPT_Free(temp);
		CRYPT_Free(imagepath);
529 530
		return TRUE;
	}
531
	/* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
532 533 534
error:
	if (pProv)
	{
535
		pProv->dwMagic = 0;
536 537
		if (pProv->hModule)
			FreeLibrary(pProv->hModule);
538 539
		CRYPT_Free(pProv->pVTable);
		CRYPT_Free(pProv->pFuncs);
540 541
		CRYPT_Free(pProv);
	}
542 543 544 545 546
	CRYPT_Free(pszContainerA);
	CRYPT_Free(provnameA);
	CRYPT_Free(provname);
	CRYPT_Free(temp);
	CRYPT_Free(imagepath);
547 548
	return FALSE;
}
549

550
/******************************************************************************
551
 * CryptAcquireContextA (ADVAPI32.@)
552
 *
553
 * See CryptAcquireContextW.
554
 */
555 556
BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
		LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
557
{
558
	PWSTR pProvider = NULL, pContainer = NULL;
559 560
	BOOL ret = FALSE;

561
	TRACE("(%p, %s, %s, %d, %08x)\n", phProv, pszContainer,
562
		pszProvider, dwProvType, dwFlags);
563

564
	if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
565 566 567 568
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
569
	if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
570 571
	{
		CRYPT_Free(pContainer);
572 573
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
574 575
	}

576
	ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
577

578 579
	CRYPT_Free(pContainer);
	CRYPT_Free(pProvider);
580 581 582 583 584 585

	return ret;
}

/******************************************************************************
 * CryptContextAddRef (ADVAPI32.@)
586 587 588 589 590 591 592 593 594 595 596 597
 *
 * Increases reference count of a cryptographic service provider handle
 * by one.
 *
 * PARAMS
 *  hProv       [I] Handle to the CSP whose reference is being incremented.
 *  pdwReserved [IN] Reserved for future use and must be NULL.
 *  dwFlags     [I] Reserved for future use and must be NULL.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
598 599 600
 */
BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
{
601 602
	PCRYPTPROV pProv = (PCRYPTPROV)hProv;	

603
	TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
604 605 606 607 608 609 610

	if (!pProv)
	{
		SetLastError(NTE_BAD_UID);
		return FALSE;
	}

611 612 613 614 615 616
	if (pProv->dwMagic != MAGIC_CRYPTPROV)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

617 618
	pProv->refcount++;
	return TRUE;
619 620
}

Patrik Stridvall's avatar
Patrik Stridvall committed
621
/******************************************************************************
622
 * CryptReleaseContext (ADVAPI32.@)
623 624 625 626 627 628 629 630 631 632
 *
 * Releases the handle of a CSP.  Reference count is decreased.
 *
 * PARAMS
 *  hProv   [I] Handle of a CSP.
 *  dwFlags [I] Reserved for future use and must be NULL.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
Patrik Stridvall's avatar
Patrik Stridvall committed
633
 */
634
BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
635
{
636
	PCRYPTPROV pProv = (PCRYPTPROV)hProv;
637
	BOOL ret = TRUE;
638

639
	TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
640

641
	if (!pProv)
642
	{
643 644
		SetLastError(NTE_BAD_UID);
		return FALSE;
645
	}
646

647 648 649 650 651 652
	if (pProv->dwMagic != MAGIC_CRYPTPROV)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

653 654 655 656
	pProv->refcount--;
	if (pProv->refcount <= 0) 
	{
		ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
657
		pProv->dwMagic = 0;
658
		FreeLibrary(pProv->hModule);
659
#if 0
660
		CRYPT_Free(pProv->pVTable->pContextInfo);
661
#endif
662 663 664 665 666
		CRYPT_Free(pProv->pVTable->pszProvName);
		CRYPT_Free(pProv->pVTable);
		CRYPT_Free(pProv->pFuncs);
		CRYPT_Free(pProv);
	}
667
	return ret;
668
}
669

670
/******************************************************************************
671
 * CryptGenRandom (ADVAPI32.@)
672 673 674 675 676 677 678 679 680 681 682 683 684 685
 *
 * Fills a buffer with cryptographically random bytes.
 *
 * PARAMS
 *  hProv    [I] Handle of a CSP.
 *  dwLen    [I] Number of bytes to generate.
 *  pbBuffer [I/O] Buffer to contain random bytes.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  pdBuffer must be at least dwLen bytes long.
686
 */
687
BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
688
{
689 690
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

691
	TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
692 693

	if (!hProv)
694 695 696 697
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
698

699
	if (prov->dwMagic != MAGIC_CRYPTPROV)
700 701 702 703
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
704

705
	return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
706
}
707 708

/******************************************************************************
709
 * CryptCreateHash (ADVAPI32.@)
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
 *
 * Initiates the hashing of a stream of data.
 *
 * PARAMS
 *  hProv   [I] Handle of a CSP.
 *  Algid   [I] Identifies the hash algorithm to use.
 *  hKey    [I] Key for the hash (if required).
 *  dwFlags [I] Reserved for future use and must be NULL.
 *  phHash  [O] Address of the future handle to the new hash object.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  If the algorithm is a keyed hash, hKey is the key.
726 727 728 729
 */
BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
		DWORD dwFlags, HCRYPTHASH *phHash)
{
730 731 732 733
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash;

734
	TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
735 736

	if (!prov)
737 738 739 740
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
741
	if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
742 743 744 745
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
746
	if (dwFlags)
747 748 749 750
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
751
	if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
752 753 754 755
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
756 757 758 759 760

	hash->pProvider = prov;

	if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
			key ? key->hPrivate : 0, 0, &hash->hPrivate))
761 762 763 764
        {
            *phHash = (HCRYPTHASH)hash;
            return TRUE;
        }
765

766
	/* CSP error! */
767
	CRYPT_Free(hash);
768
	*phHash = 0;
769 770 771 772 773
	return FALSE;
}

/******************************************************************************
 * CryptDecrypt (ADVAPI32.@)
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
 *
 * Decrypts data encrypted by CryptEncrypt.
 *
 * PARAMS
 *  hKey       [I] Handle to the decryption key.
 *  hHash      [I] Handle to a hash object.
 *  Final      [I] TRUE if this is the last section to be decrypted.
 *  dwFlags    [I] Reserved for future use. Can be CRYPT_OAEP.
 *  pbData     [I/O] Buffer that holds the encrypted data. Holds decrypted
 *                   data on return
 *  pdwDataLen [I/O] Length of pbData before and after the call.
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
789 790 791 792
 */
BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
793 794 795 796
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

797
	TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
798

799
	if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
800 801 802 803
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
804 805 806 807

	prov = key->pProvider;
	return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
			Final, dwFlags, pbData, pdwDataLen);
808 809 810 811
}

/******************************************************************************
 * CryptDeriveKey (ADVAPI32.@)
812 813 814 815 816 817 818 819 820 821 822 823 824
 *
 * Generates session keys derived from a base data value.
 *
 * PARAMS
 *  hProv     [I] Handle to a CSP.
 *  Algid     [I] Identifies the symmetric encryption algorithm to use.
 *  hBaseData [I] Handle to a hash object.
 *  dwFlags   [I] Type of key to generate.
 *  phKey     [I/O] Address of the newly generated key.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
825 826 827 828
 */
BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
		DWORD dwFlags, HCRYPTKEY *phKey)
{
829 830 831 832
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
	PCRYPTKEY key;

833
	TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
834 835

	if (!prov || !hash)
836 837 838 839
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
840
	if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
841 842 843 844
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
845
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
846 847 848 849
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
850 851 852

	key->pProvider = prov;
	if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
853 854 855 856
        {
            *phKey = (HCRYPTKEY)key;
            return TRUE;
        }
857

858
	/* CSP error! */
859
	CRYPT_Free(key);
860
	*phKey = 0;
861 862 863 864 865
	return FALSE;
}

/******************************************************************************
 * CryptDestroyHash (ADVAPI32.@)
866 867 868 869 870 871 872 873 874
 *
 * Destroys the hash object referenced by hHash.
 *
 * PARAMS
 *  hHash [I] Handle of the hash object to be destroyed.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
875 876 877
 */
BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
{
878 879 880 881 882 883 884
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;
	BOOL ret;

	TRACE("(0x%lx)\n", hHash);

	if (!hash)
885 886 887 888
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
889

890
	if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
891 892 893 894 895
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

896 897 898 899
	prov = hash->pProvider;
	ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
	CRYPT_Free(hash);
	return ret;
900 901 902
}

/******************************************************************************
903 904 905 906 907 908 909 910 911 912
 * CryptDestroyKey (ADVAPI32.@)
 *
 * Releases the handle referenced by hKey.
 *
 * PARAMS
 *  hKey [I] Handle of the key to be destroyed.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
913 914 915
 */
BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
{
916 917 918 919 920 921 922
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTPROV prov;
	BOOL ret;

	TRACE("(0x%lx)\n", hKey);

	if (!key)
923 924 925 926
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
927

928
	if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
929 930 931 932
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
933

934 935 936 937
	prov = key->pProvider;
	ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
	CRYPT_Free(key);
	return ret;
938 939 940 941
}

/******************************************************************************
 * CryptDuplicateHash (ADVAPI32.@)
942 943 944 945 946 947 948 949 950 951 952 953
 *
 * Duplicates a hash.
 *
 * PARAMS
 *  hHash       [I] Handle to the hash to be copied.
 *  pdwReserved [I] Reserved for future use and must be zero.
 *  dwFlags     [I] Reserved for future use and must be zero.
 *  phHash      [O] Address of the handle to receive the copy.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
954 955 956 957
 */
BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
		DWORD dwFlags, HCRYPTHASH *phHash)
{
958 959 960
	PCRYPTPROV prov;
	PCRYPTHASH orghash, newhash;

961
	TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
962 963

	orghash = (PCRYPTHASH)hHash;
964 965 966
	if (!orghash || pdwReserved || !phHash || !orghash->pProvider || 
		orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
	{
967 968
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
969
	}
970 971 972

	prov = orghash->pProvider;
	if (!prov->pFuncs->pCPDuplicateHash)
973 974 975 976
	{
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
		return FALSE;
	}
977 978

	if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
979 980 981 982
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
983 984 985 986 987 988 989 990

	newhash->pProvider = prov;
	if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
	{
		*phHash = (HCRYPTHASH)newhash;
		return TRUE;
	}
	CRYPT_Free(newhash);
991 992 993 994 995
	return FALSE;
}

/******************************************************************************
 * CryptDuplicateKey (ADVAPI32.@)
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
 *
 * Duplicate a key and the key's state.
 *
 * PARAMS
 *  hKey        [I] Handle of the key to copy.
 *  pdwReserved [I] Reserved for future use and must be NULL.
 *  dwFlags     [I] Reserved for future use and must be zero.
 *  phKey       [I] Address of the handle to the duplicated key.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1008 1009 1010
 */
BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
{
1011 1012 1013
	PCRYPTPROV prov;
	PCRYPTKEY orgkey, newkey;

1014
	TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1015 1016

	orgkey = (PCRYPTKEY)hKey;
1017 1018 1019
	if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider || 
		orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
	{
1020 1021
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
1022
	}
1023 1024 1025

	prov = orgkey->pProvider;
	if (!prov->pFuncs->pCPDuplicateKey)
1026 1027 1028 1029
	{
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
		return FALSE;
	}
1030 1031

	if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1032 1033 1034 1035
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1036 1037 1038 1039 1040 1041 1042 1043

	newkey->pProvider = prov;
	if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
	{
		*phKey = (HCRYPTKEY)newkey;
		return TRUE;
	}
	CRYPT_Free(newkey);
1044 1045 1046 1047 1048
	return FALSE;
}

/******************************************************************************
 * CryptEncrypt (ADVAPI32.@)
1049 1050 1051 1052
 *
 * Encrypts data.
 *
 * PARAMS
Lei Zhang's avatar
Lei Zhang committed
1053
 *  hKey       [I] Handle to the encryption key.
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
 *  hHash      [I] Handle to a hash object.
 *  Final      [I] TRUE if this is the last section to encrypt.
 *  dwFlags    [I] Can be CRYPT_OAEP.
 *  pbData     [I/O] Data to be encrypted. Contains encrypted data after call.
 *  pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
 *                   encrypted data after call.
 *  dwBufLen   [I] Length of the input pbData buffer.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 *  NOTES
 *   If pbData is NULL, CryptEncrypt determines stores the number of bytes
 *   required for the returned data in pdwDataLen.
1069 1070 1071 1072
 */
BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
{
1073 1074 1075 1076
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

1077
	TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1078

1079
	if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1080 1081 1082 1083
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1084 1085 1086 1087

	prov = key->pProvider;
	return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
			Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1088 1089 1090
}

/******************************************************************************
1091
 * CryptEnumProvidersW (ADVAPI32.@)
1092
 *
Lei Zhang's avatar
Lei Zhang committed
1093
 * Returns the next available CSP.
1094 1095 1096 1097 1098 1099 1100 1101
 *
 * PARAMS
 *  dwIndex     [I] Index of the next provider to be enumerated.
 *  pdwReserved [I] Reserved for future use and must be NULL.
 *  dwFlags     [I] Reserved for future use and must be zero.
 *  pdwProvType [O] DWORD designating the type of the provider.
 *  pszProvName [O] Buffer that receives data from the provider.
 *  pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1102
 *                    of bytes stored in the buffer on return.
1103 1104 1105 1106 1107 1108
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
 *
 *  NOTES
1109
 *   If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1110
 *   for memory allocation purposes.
1111
 */
1112 1113
BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1114
{
1115
	HKEY hKey;
1116 1117 1118 1119 1120 1121 1122 1123
	static const WCHAR providerW[] = {
                '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',0
        };
	static const WCHAR typeW[] = {'T','y','p','e',0};
1124

1125
	TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1126
			pdwProvType, pszProvName, pcbProvName);
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	if (pdwReserved || !pcbProvName)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if (dwFlags)
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
1138

1139
	if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1140 1141 1142 1143
	{
		SetLastError(NTE_FAIL);
		return FALSE;
	}
1144 1145 1146 1147

	if (!pszProvName)
	{
		DWORD numkeys;
1148
		WCHAR *provNameW;
1149
		
1150 1151
		RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
				 NULL, NULL, NULL, NULL, NULL, NULL);
1152
		
1153
		if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1154 1155 1156 1157 1158
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}

1159
		RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1160
		CRYPT_Free(provNameW);
1161
		(*pcbProvName)++;
1162
		*pcbProvName *= sizeof(WCHAR);
1163

1164
		if (dwIndex >= numkeys)
1165 1166 1167 1168
		{
			SetLastError(ERROR_NO_MORE_ITEMS);
			return FALSE;
		}
1169 1170
	} else {
		DWORD size = sizeof(DWORD);
1171
		DWORD result;
1172
		HKEY subkey;
1173 1174 1175
		
		result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
		if (result)
1176 1177 1178 1179
		{
			SetLastError(result);
			return FALSE;
		}
1180
		if (RegOpenKeyW(hKey, pszProvName, &subkey))
1181
			return FALSE;
1182
		if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1183 1184 1185 1186 1187
			return FALSE;
		RegCloseKey(subkey);
	}
	RegCloseKey(hKey);
	return TRUE;
1188 1189 1190
}

/******************************************************************************
1191
 * CryptEnumProvidersA (ADVAPI32.@)
1192
 *
1193
 * See CryptEnumProvidersW.
1194
 */
1195 1196
BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1197
{
1198
	PWSTR str = NULL;
1199
	DWORD bufsize;
1200
	BOOL ret; /* = FALSE; */
1201

1202
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1203
			pdwProvType, pszProvName, pcbProvName);
1204

1205
	if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1206
		return FALSE;
1207
	if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1208 1209 1210 1211
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1212
	ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1213
	if (str)
1214
		CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1215
	*pcbProvName = bufsize / sizeof(WCHAR);  /* FIXME: not correct */
1216 1217
	if (str)
	{
1218
		CRYPT_Free(str);
1219
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1220 1221 1222 1223
		{
			SetLastError(ERROR_MORE_DATA);
			return FALSE;
		}
1224 1225
	}
	return ret;
1226 1227 1228
}

/******************************************************************************
1229
 * CryptEnumProviderTypesW (ADVAPI32.@)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
 *
 * Retrieves the next type of CSP supported.
 *
 * PARAMS
 *  dwIndex     [I] Index of the next provider to be enumerated.
 *  pdwReserved [I] Reserved for future use and must be NULL.
 *  dwFlags     [I] Reserved for future use and must be zero.
 *  pdwProvType [O] DWORD designating the type of the provider.
 *  pszTypeName [O] Buffer that receives data from the provider type.
 *  pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1240
 *                    of bytes stored in the buffer on return.
1241 1242 1243 1244 1245 1246
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
 *
 *  NOTES
1247
 *   If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1248
 *   for memory allocation purposes.
1249
 */
1250 1251
BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1252
{
1253
	HKEY hKey, hSubkey;
1254
	DWORD keylen, numkeys, dwType;
1255
	PWSTR keyname, ch;
1256
	DWORD result;
1257 1258 1259 1260 1261 1262 1263 1264
	static const WCHAR KEYSTR[] = {
                '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',0
	};
	static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1265

1266
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1267 1268 1269
		dwFlags, pdwProvType, pszTypeName, pcbTypeName);

	if (pdwReserved || !pdwProvType || !pcbTypeName)
1270 1271 1272 1273 1274 1275 1276 1277 1278
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if (dwFlags)
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
1279

1280
	if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1281 1282
		return FALSE;

1283
	RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1284
	if (dwIndex >= numkeys)
1285 1286 1287 1288
	{
		SetLastError(ERROR_NO_MORE_ITEMS);
		return FALSE;
	}
1289
	keylen++;
1290
	if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1291 1292 1293 1294
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1295
	if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1296
                CRYPT_Free(keyname);
1297
		return FALSE;
1298
        }
1299 1300
	RegOpenKeyW(hKey, keyname, &hSubkey);
	ch = keyname + strlenW(keyname);
1301
	/* Convert "Type 000" to 0, etc/ */
1302 1303 1304 1305
	*pdwProvType = *(--ch) - '0';
	*pdwProvType += (*(--ch) - '0') * 10;
	*pdwProvType += (*(--ch) - '0') * 100;
	CRYPT_Free(keyname);
1306
	
1307
	result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1308
	if (result)
1309 1310 1311 1312
	{
		SetLastError(result);
		return FALSE;
	}
1313

1314 1315 1316
	RegCloseKey(hSubkey);
	RegCloseKey(hKey);
	return TRUE;
1317 1318 1319
}

/******************************************************************************
1320
 * CryptEnumProviderTypesA (ADVAPI32.@)
1321
 *
1322
 * See CryptEnumProviderTypesW.
1323
 */
1324 1325
BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1326
{
1327
	PWSTR str = NULL;
1328
	DWORD bufsize;
1329 1330
	BOOL ret;

1331
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1332
			pdwProvType, pszTypeName, pcbTypeName);
1333

1334
	if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1335
		return FALSE;
1336
	if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1337 1338 1339 1340
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1341
	ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1342 1343
	if (str)
		CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1344
	*pcbTypeName = bufsize / sizeof(WCHAR);
1345 1346 1347
	if (str)
	{
		CRYPT_Free(str);
1348
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1349 1350 1351 1352
		{
			SetLastError(ERROR_MORE_DATA);
			return FALSE;
		}
1353 1354
	}
	return ret;
1355 1356 1357 1358
}

/******************************************************************************
 * CryptExportKey (ADVAPI32.@)
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
 * 
 * Exports a cryptographic key from a CSP.
 *
 * PARAMS
 *  hKey       [I] Handle to the key to export.
 *  hExpKey    [I] Handle to a cryptographic key of the end user.
 *  dwBlobType [I] Type of BLOB to be exported.
 *  dwFlags    [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
 *  pbData     [O] Buffer to receive BLOB data.
 *  pdwDataLen [I/O] Specifies the size of pbData.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
 *  buffer needed to hold the BLOB.
1377 1378 1379 1380
 */
BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
1381 1382 1383
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;

1384
	TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1385

1386
	if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1387 1388 1389 1390
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1391 1392 1393 1394

	prov = key->pProvider;
	return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
			dwBlobType, dwFlags, pbData, pdwDataLen);
1395 1396 1397 1398
}

/******************************************************************************
 * CryptGenKey (ADVAPI32.@)
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
 *
 * Generates a random cryptographic session key or a pub/priv key pair.
 *
 * PARAMS
 *  hProv   [I] Handle to a CSP.
 *  Algid   [I] Algorithm to use to make key.
 *  dwFlags [I] Specifies type of key to make.
 *  phKey   [I] Address of the handle to which the new key is copied.
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
1411 1412 1413
 */
BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
{
1414 1415 1416
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key;

1417
	TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1418 1419

	if (!prov)
1420 1421 1422 1423
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1424
	if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1425 1426 1427 1428
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1429
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1430 1431 1432 1433
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1434 1435 1436 1437

	key->pProvider = prov;

	if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1438 1439 1440 1441
        {
            *phKey = (HCRYPTKEY)key;
            return TRUE;
        }
1442

1443
	/* CSP error! */
1444
	CRYPT_Free(key);
1445 1446 1447 1448
	return FALSE;
}

/******************************************************************************
1449
 * CryptGetDefaultProviderW (ADVAPI32.@)
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
 *
 * Finds the default CSP of a certain provider type.
 *
 * PARAMS
 *  dwProvType  [I] Provider type to look for.
 *  pdwReserved [I] Reserved for future use and must be NULL.
 *  dwFlags     [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
 *  pszProvName [O] Name of the default CSP.
 *  pcbProvName [I/O] Size of pszProvName
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  If pszProvName is NULL, pcbProvName will hold the size of the buffer for
 *  memory allocation purposes on return.
1467
 */
1468 1469
BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
		DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1470
{
1471
	HKEY hKey;
1472
	PWSTR keyname;
1473
	DWORD result;
1474
	static const WCHAR nameW[] = {'N','a','m','e',0};
1475 1476

	if (pdwReserved || !pcbProvName)
1477 1478 1479 1480
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1481
	if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1482 1483 1484 1485
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
1486
	if (dwProvType > 999)
1487 1488 1489 1490
	{
		SetLastError(NTE_BAD_PROV_TYPE);
		return FALSE;
	}
1491
	if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1492 1493 1494 1495
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1496
	if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ?  HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1497 1498
	{
		CRYPT_Free(keyname);
1499 1500
		SetLastError(NTE_PROV_TYPE_NOT_DEF);
		return FALSE;
1501 1502
	}
	CRYPT_Free(keyname);
1503
	
1504
	result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName); 
1505
	if (result)
1506
	{
1507
		if (result != ERROR_MORE_DATA)
1508
			SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1509 1510 1511
		else
			SetLastError(result);
		
1512 1513
		return FALSE;
	}
1514
	
1515 1516
	RegCloseKey(hKey);
	return TRUE;
1517 1518 1519
}

/******************************************************************************
1520
 * CryptGetDefaultProviderA (ADVAPI32.@)
1521
 *
1522
 * See CryptGetDefaultProviderW.
1523
 */
1524 1525
BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
		DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1526
{
1527
	PWSTR str = NULL;
1528
	DWORD bufsize;
1529 1530
	BOOL ret = FALSE;

1531
	TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1532

1533 1534
	CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
	if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1535 1536 1537 1538
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1539
	ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1540 1541
	if (str)
		CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1542
	*pcbProvName = bufsize / sizeof(WCHAR);
1543 1544 1545
	if (str)
	{
		CRYPT_Free(str);
1546
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1547 1548 1549 1550
		{
			SetLastError(ERROR_MORE_DATA);
			return FALSE;
		}
1551 1552
	}
	return ret;
1553
}
1554

1555 1556
/******************************************************************************
 * CryptGetHashParam (ADVAPI32.@)
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
 *
 * Retrieves data that controls the operations of a hash object.
 *
 * PARAMS
 *  hHash      [I] Handle of the hash object to question.
 *  dwParam    [I] Query type.
 *  pbData     [O] Buffer that receives the value data.
 *  pdwDataLen [I/O] Size of the pbData buffer.
 *  dwFlags    [I] Reserved for future use and must be zero.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  If pbData is NULL, pdwDataLen will contain the length required.
1573 1574 1575 1576
 */
BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1577 1578 1579
	PCRYPTPROV prov;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

1580
	TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1581

1582
	if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1583 1584 1585 1586
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1587 1588 1589 1590

	prov = hash->pProvider;
	return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
			pbData, pdwDataLen, dwFlags);
1591
}
1592

1593 1594
/******************************************************************************
 * CryptGetKeyParam (ADVAPI32.@)
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
 *
 * Retrieves data that controls the operations of a key.
 *
 * PARAMS
 *  hKey       [I] Handle to they key in question.
 *  dwParam    [I] Specifies query type.
 *  pbData     [O] Sequence of bytes to receive data.
 *  pdwDataLen [I/O] Size of pbData.
 *  dwFlags    [I] Reserved for future use and must be zero.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1611 1612 1613 1614
 */
BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1615 1616 1617
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;

1618
	TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1619

1620
	if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1621 1622 1623 1624
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1625 1626 1627 1628

	prov = key->pProvider;
	return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
			pbData, pdwDataLen, dwFlags);
1629
}
1630

1631 1632
/******************************************************************************
 * CryptGetProvParam (ADVAPI32.@)
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
 *
 * Retrieves parameters that control the operations of a CSP.
 *
 * PARAMS
 *  hProv      [I] Handle of the CSP in question.
 *  dwParam    [I] Specifies query type.
 *  pbData     [O] Buffer to receive the data.
 *  pdwDataLen [I/O] Size of pbData.
 *  dwFlags    [I] see MSDN Docs.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 *
 * NOTES
 *  If pbData is NULL, pdwDataLen is set to the needed buffer length.
1649 1650 1651 1652
 */
BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1653 1654
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

1655
	TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1656

1657
	if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1658 1659 1660 1661
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1662

1663
	return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1664
}
1665

1666 1667
/******************************************************************************
 * CryptGetUserKey (ADVAPI32.@)
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
 *
 * Gets a handle of one of a user's two public/private key pairs.
 *
 * PARAMS
 *  hProv     [I] Handle of a CSP.
 *  dwKeySpec [I] Private key to use.
 *  phUserKey [O] Pointer to the handle of the retrieved keys.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1679 1680 1681
 */
BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
{
1682 1683 1684
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key;

1685
	TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1686 1687

	if (!prov)
1688 1689 1690 1691
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1692
	if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1693 1694 1695 1696
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1697
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1698 1699 1700 1701
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1702 1703 1704 1705

	key->pProvider = prov;

	if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1706 1707 1708 1709
        {
            *phUserKey = (HCRYPTKEY)key;
            return TRUE;
        }
1710

1711
	/* CSP Error */
1712
	CRYPT_Free(key);
1713
	*phUserKey = 0;
1714
	return FALSE;
1715 1716
}

1717 1718
/******************************************************************************
 * CryptHashData (ADVAPI32.@)
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
 *
 * Adds data to a hash object.
 *
 * PARAMS
 *  hHash     [I] Handle of the hash object.
 *  pbData    [I] Buffer of data to be hashed.
 *  dwDataLen [I] Number of bytes to add.
 *  dwFlags   [I] Can be CRYPT_USERDATA
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1731
 */
1732
BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1733
{
1734 1735 1736
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;

1737
	TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1738 1739

	if (!hash)
1740 1741 1742 1743
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1744
	if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1745 1746 1747 1748
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1749 1750 1751

	prov = hash->pProvider;
	return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1752 1753 1754 1755
}

/******************************************************************************
 * CryptHashSessionKey (ADVAPI32.@)
1756
 *
1757 1758
 * Compute the cryptographic hash of a session key object.
 *
1759 1760 1761 1762 1763 1764 1765 1766
 * PARAMS 
 *  hHash   [I] Handle to the hash object.
 *  hKey    [I] Handle to the key to be hashed.
 *  dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1767 1768 1769
 */
BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
{
1770 1771 1772 1773
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTPROV prov;

1774
	TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1775 1776

	if (!hash || !key)
1777 1778 1779 1780
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1781

1782
	if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1783 1784 1785 1786
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1787

1788 1789
	prov = hash->pProvider;
	return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1790 1791 1792 1793
}

/******************************************************************************
 * CryptImportKey (ADVAPI32.@)
1794
 *
1795 1796
 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
 *
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
 * PARAMS
 *  hProv     [I] Handle of a CSP.
 *  pbData    [I] Contains the key to be imported.
 *  dwDataLen [I] Length of the key.
 *  hPubKey   [I] Cryptographic key that decrypts pdData
 *  dwFlags   [I] Used only with a public/private key pair.
 *  phKey     [O] Imported key.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1808
 */
1809
BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1810 1811
		HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
{
1812 1813
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1814

1815
	TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1816

1817
	if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1818 1819 1820 1821
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1822 1823

	if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1824 1825 1826 1827
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837

	importkey->pProvider = prov;
	if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
			pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
	{
		*phKey = (HCRYPTKEY)importkey;
		return TRUE;
	}

	CRYPT_Free(importkey);
1838 1839 1840 1841
	return FALSE;
}

/******************************************************************************
1842
 * CryptSignHashW (ADVAPI32.@)
1843 1844 1845 1846 1847 1848
 *
 * Signs data.
 *
 * PARAMS
 *  hHash        [I] Handle of the hash object to be signed.
 *  dwKeySpec    [I] Private key to use.
1849
 *  sDescription [I] Should be NULL.
1850 1851 1852 1853 1854 1855 1856
 *  dwFlags      [I] CRYPT_NOHASHOID/X931_FORMAT.
 *  pbSignature  [O] Buffer of the signature data.
 *  pdwSigLen    [I/O] Size of the pbSignature buffer.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1857 1858 1859 1860 1861
 *
 * NOTES
 *  Because of security flaws sDescription should not be used and should thus be
 *  NULL. It is supported only for compatibility with Microsoft's Cryptographic
 *  Providers.
1862
 */
1863
BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1864 1865
		DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
{
1866 1867
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;
1868

1869
	TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", 
1870
		hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1871 1872

	if (!hash)
1873 1874 1875 1876
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1877
	if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1878 1879 1880 1881
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1882 1883

	prov = hash->pProvider;
1884
	return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1885
		dwFlags, pbSignature, pdwSigLen);
1886 1887
}

1888 1889 1890
/******************************************************************************
 * CryptSignHashA (ADVAPI32.@)
 *
1891
 * See CryptSignHashW.
1892 1893 1894 1895 1896 1897 1898
 */
BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
		DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
{
	LPWSTR wsDescription;
	BOOL result;

1899
	TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", 
1900 1901 1902 1903
		hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);

	CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
	result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1904
	CRYPT_Free(wsDescription);
1905 1906 1907 1908

	return result;
}

1909 1910
/******************************************************************************
 * CryptSetHashParam (ADVAPI32.@)
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
 *
 * Customizes the operations of a hash object.
 *
 * PARAMS
 *  hHash   [I] Handle of the hash object to set parameters.
 *  dwParam [I] HP_HMAC_INFO/HASHVAL.
 *  pbData  [I] Value data buffer.
 *  dwFlags [I] Reserved for future use and must be zero.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1923
 */
1924
BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1925
{
1926 1927 1928
	PCRYPTPROV prov;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

1929
	TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1930

1931
	if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1932 1933 1934 1935
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1936 1937 1938 1939

	prov = hash->pProvider;
	return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
			dwParam, pbData, dwFlags);
1940 1941 1942
}

/******************************************************************************
1943
 * CryptSetKeyParam (ADVAPI32.@)
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
 *
 * Customizes a session key's operations.
 *
 * PARAMS
 *  hKey    [I] Handle to the key to set values.
 *  dwParam [I] See MSDN Doc.
 *  pbData  [I] Buffer of values to set.
 *  dwFlags [I] Only used when dwParam == KP_ALGID.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1956
 */
1957
BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1958
{
1959 1960 1961
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;

1962
	TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1963

1964
	if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1965 1966 1967 1968
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1969 1970 1971 1972

	prov = key->pProvider;
	return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
			dwParam, pbData, dwFlags);
1973 1974 1975 1976
}

/******************************************************************************
 * CryptSetProviderA (ADVAPI32.@)
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
 *
 * Specifies the current user's default CSP.
 *
 * PARAMS
 *  pszProvName [I] Name of the new default CSP.
 *  dwProvType  [I] Provider type of the CSP.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
1987 1988 1989
 */
BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
{
1990
	TRACE("(%s, %d)\n", pszProvName, dwProvType);
1991
	return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1992 1993 1994 1995
}

/******************************************************************************
 * CryptSetProviderW (ADVAPI32.@)
1996
 *
1997
 * See CryptSetProviderA.
1998 1999 2000
 */
BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
{
2001
	TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2002
	return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2003 2004 2005
}

/******************************************************************************
2006
 * CryptSetProviderExW (ADVAPI32.@)
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
 *
 * Specifies the default CSP.
 *
 * PARAMS
 *  pszProvName [I] Name of the new default CSP.
 *  dwProvType  [I] Provider type of the CSP.
 *  pdwReserved [I] Reserved for future use and must be NULL.
 *  dwFlags     [I] See MSDN Doc.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2019
 */
2020
BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2021
{
2022
	HKEY hProvKey, hTypeKey;
2023 2024
	PWSTR keyname;
	static const WCHAR nameW[] = {'N','a','m','e',0};
2025

2026
	TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2027 2028

	if (!pszProvName || pdwReserved)
2029 2030 2031 2032
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
2033
	if (dwProvType > MAXPROVTYPES)
2034 2035 2036 2037
	{
		SetLastError(NTE_BAD_PROV_TYPE);
		return FALSE;
	}
2038 2039
	if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
			|| dwFlags == CRYPT_DELETE_DEFAULT)
2040 2041 2042 2043 2044
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}

2045
	if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2046 2047 2048 2049
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
2050
	if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2051
		keyname, &hTypeKey))
2052 2053
	{
		CRYPT_Free(keyname);
2054 2055
		SetLastError(NTE_BAD_PROVIDER);
		return FALSE;
2056 2057
	}
	CRYPT_Free(keyname);
2058 2059 2060
	
	if (dwFlags & CRYPT_DELETE_DEFAULT)
	{
2061
		RegDeleteValueW(hTypeKey, nameW);
2062 2063 2064 2065 2066 2067
	}
	else
	{
		if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
		{
			RegCloseKey(hTypeKey);
2068 2069
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
2070
		}
2071
		if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2072 2073 2074 2075
			keyname, &hProvKey))
		{
			CRYPT_Free(keyname);
			RegCloseKey(hTypeKey);
2076 2077
			SetLastError(NTE_BAD_PROVIDER);
			return FALSE;
2078 2079 2080
		}
		CRYPT_Free(keyname);
		
2081 2082
		if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
			(strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
		{
			RegCloseKey(hTypeKey);
			RegCloseKey(hProvKey);
			return FALSE;
		}
		
		RegCloseKey(hProvKey);
	}
	RegCloseKey(hTypeKey);

2093
	return TRUE;
2094 2095 2096
}

/******************************************************************************
2097
 * CryptSetProviderExA (ADVAPI32.@)
2098
 *
2099
 * See CryptSetProviderExW.
2100
 */
2101
BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2102
{
2103
	BOOL ret = FALSE;
2104
	PWSTR str = NULL;
2105

2106
	TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2107

2108
	if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2109
	{
2110
		ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2111 2112 2113
		CRYPT_Free(str);
	}
	return ret;
2114 2115 2116 2117
}

/******************************************************************************
 * CryptSetProvParam (ADVAPI32.@)
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
 *
 * Customizes the operations of a CSP.
 *
 * PARAMS
 *  hProv   [I] Handle of a CSP.
 *  dwParam [I] See MSDN Doc.
 *  pbData  [I] Buffer that contains a value to set as a parameter.
 *  dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2130
 */
2131
BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2132
{
2133 2134
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

2135
	TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2136 2137

	if (!prov)
2138 2139 2140 2141
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
2142
	if (prov->dwMagic != MAGIC_CRYPTPROV)
2143 2144 2145 2146
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
2147 2148 2149 2150 2151 2152 2153
	if (dwFlags & PP_USE_HARDWARE_RNG)
	{
		FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
		FIXME("\tLetting the CSP decide.\n");
	}
	if (dwFlags & PP_CLIENT_HWND)
	{
2154 2155
		/* FIXME: Should verify the parameter */
		if (pbData /* && IsWindow((HWND)pbData) */)
2156 2157 2158 2159 2160 2161 2162 2163
		{
			crypt_hWindow = (HWND)(pbData);
			return TRUE;
		} else {
			SetLastError(ERROR_INVALID_PARAMETER);
			return FALSE;
		}
	}
2164
	/* All other flags go to the CSP */
2165
	return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2166 2167 2168
}

/******************************************************************************
2169
 * CryptVerifySignatureW (ADVAPI32.@)
2170 2171 2172 2173 2174 2175 2176 2177
 *
 * Verifies the signature of a hash object.
 *
 * PARAMS
 *  hHash        [I] Handle of the hash object to verify.
 *  pbSignature  [I] Signature data to verify.
 *  dwSigLen     [I] Size of pbSignature.
 *  hPubKey      [I] Handle to the public key to authenticate signature.
2178
 *  sDescription [I] Should be NULL.
2179 2180 2181 2182 2183
 *  dwFlags      [I] See MSDN doc.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2184 2185 2186 2187 2188
 * 
 * NOTES
 *  Because of security flaws sDescription should not be used and should thus be
 *  NULL. It is supported only for compatibility with Microsoft's Cryptographic
 *  Providers.
2189
 */
2190
BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2191
		HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2192
{
2193 2194 2195
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTKEY key = (PCRYPTKEY)hPubKey;
	PCRYPTPROV prov;
2196

2197
	TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2198
			dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2199

2200
	if (!hash || !key ||
2201 2202 2203
	    !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
	    !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
	{
2204 2205
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
2206 2207
	}
		
2208 2209
	prov = hash->pProvider;
	return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2210
		key->hPrivate, sDescription, dwFlags);
2211
}
2212

2213 2214 2215
/******************************************************************************
 * CryptVerifySignatureA (ADVAPI32.@)
 *
2216
 * See CryptVerifySignatureW.
2217
 */
2218
BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2219 2220 2221 2222 2223
		HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
{
	LPWSTR wsDescription;
	BOOL result;

2224
	TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2225 2226 2227 2228
			dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);

	CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
	result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2229
	CRYPT_Free(wsDescription);
2230 2231 2232

	return result;
}
2233

2234
/******************************************************************************
2235
 * SystemFunction030   (ADVAPI32.@)
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
 *
 * Tests if two blocks of 16 bytes are equal
 *
 * PARAMS
 *  b1,b2   [I] block of 16 bytes
 *
 * RETURNS
 *  TRUE  if blocks are the same
 *  FALSE if blocks are different
 */
BOOL WINAPI SystemFunction030(PVOID b1, PVOID b2)
{
    return !memcmp(b1, b2, 0x10);
}

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
/******************************************************************************
 * SystemFunction035   (ADVAPI32.@)
 *
 * Described here:
http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
 *
 * NOTES
 *  Stub, always return TRUE.
 */
BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
{
    FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
    return TRUE;
}

2266 2267 2268 2269 2270 2271 2272 2273
/******************************************************************************
 * SystemFunction036   (ADVAPI32.@)
 *
 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
 *
 * PARAMS
 *  pbBufer [O] Pointer to memory to receive random bytes.
 *  dwLen   [I] Number of random bytes to fetch.
2274 2275 2276 2277
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
 */

BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
{
    int 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 = open("/dev/urandom", O_RDONLY);
    if (dev_random != -1)
    {
        if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
        {
            close(dev_random);
            return TRUE;
        }
        close(dev_random);
    }
    SetLastError(NTE_FAIL);
    return FALSE;
}    
    
2303 2304 2305 2306 2307 2308 2309 2310
/*
   These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
   in crypt32.dll.
 */

/******************************************************************************
 * SystemFunction040   (ADVAPI32.@)
 *
2311 2312
 * MSDN documents this function as RtlEncryptMemory.
 *
2313 2314 2315 2316 2317 2318 2319 2320
 * PARAMS
 *  memory [I/O] Pointer to memory to encrypt.
 *  length [I] Length of region to encrypt in bytes.
 *  flags  [I] Control whether other processes are able to decrypt the memory.
 *    RTL_ENCRYPT_OPTION_SAME_PROCESS 
 *    RTL_ENCRYPT_OPTION_CROSS_PROCESS 
 *    RTL_ENCRYPT_OPTION_SAME_LOGON
 *    
2321 2322 2323 2324
 * RETURNS
 *  Success: STATUS_SUCCESS
 *  Failure: NTSTATUS error code
 *
2325 2326 2327 2328
 * NOTES
 *  length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
 *  If flags are specified when encrypting, the same flag value must be given
 *  when decrypting the memory.
2329
 */
2330
NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2331
{
2332
	FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2333 2334 2335 2336 2337 2338
	return STATUS_SUCCESS;
}

/******************************************************************************
 * SystemFunction041  (ADVAPI32.@)
 *
2339 2340
 * MSDN documents this function as RtlDecryptMemory.
 *
2341 2342 2343 2344 2345 2346 2347 2348
 * PARAMS
 *  memory [I/O] Pointer to memory to decrypt.
 *  length [I] Length of region to decrypt in bytes.
 *  flags  [I] Control whether other processes are able to decrypt the memory.
 *    RTL_ENCRYPT_OPTION_SAME_PROCESS
 *    RTL_ENCRYPT_OPTION_CROSS_PROCESS
 *    RTL_ENCRYPT_OPTION_SAME_LOGON
 *
2349 2350 2351 2352
 * RETURNS
 *  Success: STATUS_SUCCESS
 *  Failure: NTSTATUS error code
 *
2353 2354 2355 2356
 * NOTES
 *  length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
 *  If flags are specified when encrypting, the same flag value must be given
 *  when decrypting the memory.
2357
 */
2358
NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2359
{
2360
	FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2361 2362
	return STATUS_SUCCESS;
}