crypt.c 64.1 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
#define CRYPT_Free(buffer) (LocalFree(buffer))
58

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 void CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
186
{
187
	if (phWnd) *phWnd = crypt_hWindow;
188 189 190 191 192 193
}

#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)
194
static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
195 196 197 198 199 200 201
{
	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;
202
	if ( !(provider->hModule = LoadLibraryW(pImage)) )
203 204
	{
		errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
205
		FIXME("Failed to load dll %s\n", debugstr_w(pImage));
206 207
		goto error;
	}
208
	provider->dwMagic = MAGIC_CRYPTPROV;
209
	provider->refcount = 1;
210 211 212 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

	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);

238 239 240
	/* FIXME: Not sure what the pbContextInfo field is for.
	 *        Does it need memory allocation?
         */
241
	provider->pVTable->Version = 3;
242 243
	provider->pVTable->FuncVerifyImage = CRYPT_VerifyImage;
	provider->pVTable->FuncReturnhWnd = CRYPT_ReturnhWnd;
244 245 246 247 248 249 250 251 252 253
	provider->pVTable->dwProvType = 0;
	provider->pVTable->pbContextInfo = NULL;
	provider->pVTable->cbContextInfo = 0;
	provider->pVTable->pszProvName = NULL;
	return provider;

error:
	SetLastError(errorcode);
	if (provider)
	{
254
		provider->dwMagic = 0;
255 256 257 258 259 260 261 262 263 264 265
		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

266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
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)
		{
287 288 289 290 291 292 293 294 295 296
                    UUID uuid;
                    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 };
297

298 299 300 301 302 303 304 305 306 307 308 309 310
                    rs = UuidCreate(&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));
                    }
311 312 313 314 315
		}
		RegCloseKey(key);
	}
}

316
/******************************************************************************
317
 * CryptAcquireContextW (ADVAPI32.@)
318
 *
319
 * Acquire a crypto provider context handle.
320
 *
321
 * PARAMS
322 323 324 325 326 327 328 329
 *  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.
330
 */
331 332
BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
		LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
333
{
334 335
	PCRYPTPROV pProv = NULL;
	HKEY key;
336 337
	PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
	PSTR provnameA = NULL, pszContainerA = NULL;
338
	DWORD keytype, type, len;
339
	ULONG r;
340 341 342
	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};
343

344
	TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
345
		debugstr_w(pszProvider), dwProvType, dwFlags);
346

347
	if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
348
	{
349
		SetLastError(NTE_BAD_PROV_TYPE);
350 351
		return FALSE;
	}
352 353
	
	if (!phProv)
354
	{
355
		SetLastError(ERROR_INVALID_PARAMETER);
356 357 358
		return FALSE;
	}

359 360 361
	/* Make sure the MachineGuid value exists */
	CRYPT_CreateMachineGuid();

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

	keyname = CRYPT_GetProvKeyName(provname);
423
	r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
424
	CRYPT_Free(keyname);
425 426 427 428 429
	if (r != ERROR_SUCCESS)
	{
		SetLastError(NTE_KEYSET_NOT_DEF);
		goto error;
	}
430
	len = sizeof(DWORD);
431
	r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
432
	if (r != ERROR_SUCCESS)
433
	{
434 435 436 437 438
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
	if (type != dwProvType)
	{
439
		TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
440
		SetLastError(NTE_PROV_TYPE_NO_MATCH);
441 442 443
		goto error;
	}

444
	r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
445
	if ( r != ERROR_SUCCESS || keytype != REG_SZ)
446
	{
447
		TRACE("error %d reading size of 'Image Path' from registry\n", r );
448 449 450 451 452 453 454 455 456 457
		RegCloseKey(key);
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
	if (!(temp = CRYPT_Alloc(len)))
	{
		RegCloseKey(key);
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		goto error;
	}
458
	r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
459 460
	if( r != ERROR_SUCCESS )
	{
461
		TRACE("error %d reading 'Image Path' from registry\n", r );
462 463 464 465
		RegCloseKey(key);
		SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
		goto error;
	}
466
	RegCloseKey(key);
467 468
	len = ExpandEnvironmentStringsW(temp, NULL, 0);
	if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
469 470 471 472
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		goto error;
	}
473
	if (!ExpandEnvironmentStringsW(temp, imagepath, len))
474 475 476 477
	{
		/* ExpandEnvironmentStrings will call SetLastError */
		goto error;
	}
478
	pProv = CRYPT_LoadProvider(imagepath);
479
	if (!pProv) {
480
		/* CRYPT_LoadProvider calls SetLastError */
481 482
		goto error;
	}
483
	pProv->pVTable->dwProvType = dwProvType;
484 485 486 487 488 489 490 491 492 493 494 495
	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))
496
	{
497 498 499
		/* 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.
500
		 */
501 502
		if (dwFlags & CRYPT_DELETEKEYSET)
		{
503
			pProv->dwMagic = 0;
504
			FreeLibrary(pProv->hModule);
505 506
			CRYPT_Free(provnameA);
			CRYPT_Free(pProv->pVTable);
507 508 509 510 511
			CRYPT_Free(pProv->pFuncs);
			CRYPT_Free(pProv);
		} else {
			*phProv = (HCRYPTPROV)pProv;
		}
512 513
		CRYPT_Free(pszContainerA);
		CRYPT_Free(provname);
514 515
		CRYPT_Free(temp);
		CRYPT_Free(imagepath);
516 517
		return TRUE;
	}
518
	/* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
519 520 521
error:
	if (pProv)
	{
522
		pProv->dwMagic = 0;
523 524
		if (pProv->hModule)
			FreeLibrary(pProv->hModule);
525 526
		CRYPT_Free(pProv->pVTable);
		CRYPT_Free(pProv->pFuncs);
527 528
		CRYPT_Free(pProv);
	}
529 530 531 532 533
	CRYPT_Free(pszContainerA);
	CRYPT_Free(provnameA);
	CRYPT_Free(provname);
	CRYPT_Free(temp);
	CRYPT_Free(imagepath);
534 535
	return FALSE;
}
536

537
/******************************************************************************
538
 * CryptAcquireContextA (ADVAPI32.@)
539
 *
540
 * See CryptAcquireContextW.
541
 */
542 543
BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
		LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
544
{
545
	PWSTR pProvider = NULL, pContainer = NULL;
546 547
	BOOL ret = FALSE;

548 549
	TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_a(pszContainer),
              debugstr_a(pszProvider), dwProvType, dwFlags);
550

551
	if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
552 553 554 555
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
556
	if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
557 558
	{
		CRYPT_Free(pContainer);
559 560
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
561 562
	}

563
	ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
564

565 566
	CRYPT_Free(pContainer);
	CRYPT_Free(pProvider);
567 568 569 570 571 572

	return ret;
}

/******************************************************************************
 * CryptContextAddRef (ADVAPI32.@)
573 574 575 576 577 578 579 580 581 582 583 584
 *
 * 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
585 586 587
 */
BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
{
588 589
	PCRYPTPROV pProv = (PCRYPTPROV)hProv;	

590
	TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
591 592 593 594 595 596 597

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

598 599 600 601 602 603
	if (pProv->dwMagic != MAGIC_CRYPTPROV)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

604 605
	pProv->refcount++;
	return TRUE;
606 607
}

Patrik Stridvall's avatar
Patrik Stridvall committed
608
/******************************************************************************
609
 * CryptReleaseContext (ADVAPI32.@)
610 611 612 613 614 615 616 617 618 619
 *
 * 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
620
 */
621
BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
622
{
623
	PCRYPTPROV pProv = (PCRYPTPROV)hProv;
624
	BOOL ret = TRUE;
625

626
	TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
627

628
	if (!pProv)
629
	{
630 631
		SetLastError(NTE_BAD_UID);
		return FALSE;
632
	}
633

634 635 636 637 638 639
	if (pProv->dwMagic != MAGIC_CRYPTPROV)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

640 641 642 643
	pProv->refcount--;
	if (pProv->refcount <= 0) 
	{
		ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
644
		pProv->dwMagic = 0;
645
		FreeLibrary(pProv->hModule);
646
#if 0
647
		CRYPT_Free(pProv->pVTable->pContextInfo);
648
#endif
649 650 651 652 653
		CRYPT_Free(pProv->pVTable->pszProvName);
		CRYPT_Free(pProv->pVTable);
		CRYPT_Free(pProv->pFuncs);
		CRYPT_Free(pProv);
	}
654
	return ret;
655
}
656

657
/******************************************************************************
658
 * CryptGenRandom (ADVAPI32.@)
659 660 661 662 663 664 665 666 667 668 669 670 671 672
 *
 * 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.
673
 */
674
BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
675
{
676 677
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

678
	TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
679 680

	if (!hProv)
681 682 683 684
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
685

686
	if (prov->dwMagic != MAGIC_CRYPTPROV)
687 688 689 690
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
691

692
	return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
693
}
694 695

/******************************************************************************
696
 * CryptCreateHash (ADVAPI32.@)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
 *
 * 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.
713 714 715 716
 */
BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
		DWORD dwFlags, HCRYPTHASH *phHash)
{
717 718 719 720
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash;

721
	TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
722

723
	if (!prov || !phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
724
		(key && key->dwMagic != MAGIC_CRYPTKEY))
725 726 727 728
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
729
	if (dwFlags)
730 731 732 733
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
734
	if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
735 736 737 738
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
739 740

	hash->pProvider = prov;
741
	hash->dwMagic = MAGIC_CRYPTHASH;
742 743
	if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
			key ? key->hPrivate : 0, 0, &hash->hPrivate))
744 745 746 747
        {
            *phHash = (HCRYPTHASH)hash;
            return TRUE;
        }
748

749
	/* CSP error! */
750
	hash->dwMagic = 0;
751
	CRYPT_Free(hash);
752
	*phHash = 0;
753 754 755 756 757
	return FALSE;
}

/******************************************************************************
 * CryptDecrypt (ADVAPI32.@)
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
 *
 * 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
773 774 775 776
 */
BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
777 778 779 780
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

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

783 784 785
	if (!key || !pbData || !pdwDataLen ||
		!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
		key->pProvider->dwMagic != MAGIC_CRYPTPROV)
786 787 788 789
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
790 791 792 793

	prov = key->pProvider;
	return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
			Final, dwFlags, pbData, pdwDataLen);
794 795 796 797
}

/******************************************************************************
 * CryptDeriveKey (ADVAPI32.@)
798 799 800 801 802 803 804 805 806 807 808 809 810
 *
 * 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
811 812 813 814
 */
BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
		DWORD dwFlags, HCRYPTKEY *phKey)
{
815 816 817 818
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
	PCRYPTKEY key;

819
	TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
820 821

	if (!prov || !hash)
822 823 824 825
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
826
	if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
827 828 829 830
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
831
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
832 833 834 835
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
836 837

	key->pProvider = prov;
838
	key->dwMagic = MAGIC_CRYPTKEY;
839
	if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
840 841 842 843
        {
            *phKey = (HCRYPTKEY)key;
            return TRUE;
        }
844

845
	/* CSP error! */
846
	key->dwMagic = 0;
847
	CRYPT_Free(key);
848
	*phKey = 0;
849 850 851 852 853
	return FALSE;
}

/******************************************************************************
 * CryptDestroyHash (ADVAPI32.@)
854 855 856 857 858 859 860 861 862
 *
 * Destroys the hash object referenced by hHash.
 *
 * PARAMS
 *  hHash [I] Handle of the hash object to be destroyed.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
863 864 865
 */
BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
{
866 867 868 869 870 871 872
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;
	BOOL ret;

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

	if (!hash)
873 874 875 876
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
877

878 879
	if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
		hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
880 881 882 883 884
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

885 886
	prov = hash->pProvider;
	ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
887
	hash->dwMagic = 0;
888 889
	CRYPT_Free(hash);
	return ret;
890 891 892
}

/******************************************************************************
893 894 895 896 897 898 899 900 901 902
 * CryptDestroyKey (ADVAPI32.@)
 *
 * Releases the handle referenced by hKey.
 *
 * PARAMS
 *  hKey [I] Handle of the key to be destroyed.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
903 904 905
 */
BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
{
906 907 908 909 910 911 912
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTPROV prov;
	BOOL ret;

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

	if (!key)
913 914 915 916
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
917

918 919
	if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
		key->pProvider->dwMagic != MAGIC_CRYPTPROV)
920 921 922 923
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
924

925 926
	prov = key->pProvider;
	ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
927
	key->dwMagic = 0;
928 929
	CRYPT_Free(key);
	return ret;
930 931 932 933
}

/******************************************************************************
 * CryptDuplicateHash (ADVAPI32.@)
934 935 936 937 938 939 940 941 942 943 944 945
 *
 * 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
946 947 948 949
 */
BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
		DWORD dwFlags, HCRYPTHASH *phHash)
{
950 951 952
	PCRYPTPROV prov;
	PCRYPTHASH orghash, newhash;

953
	TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
954 955

	orghash = (PCRYPTHASH)hHash;
956 957
	if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
		orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
958
	{
959 960
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
961
	}
962 963 964

	prov = orghash->pProvider;
	if (!prov->pFuncs->pCPDuplicateHash)
965 966 967 968
	{
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
		return FALSE;
	}
969 970

	if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
971 972 973 974
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
975 976

	newhash->pProvider = prov;
977
	newhash->dwMagic = MAGIC_CRYPTHASH;
978 979 980 981 982
	if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
	{
		*phHash = (HCRYPTHASH)newhash;
		return TRUE;
	}
983
	newhash->dwMagic = 0;
984
	CRYPT_Free(newhash);
985 986 987 988 989
	return FALSE;
}

/******************************************************************************
 * CryptDuplicateKey (ADVAPI32.@)
990 991 992 993 994 995 996 997 998 999 1000 1001
 *
 * 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
1002 1003 1004
 */
BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
{
1005 1006 1007
	PCRYPTPROV prov;
	PCRYPTKEY orgkey, newkey;

1008
	TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1009 1010

	orgkey = (PCRYPTKEY)hKey;
1011 1012
	if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
		orgkey->dwMagic != MAGIC_CRYPTKEY ||
1013 1014
		orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
	{
1015 1016
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
1017
	}
1018 1019 1020

	prov = orgkey->pProvider;
	if (!prov->pFuncs->pCPDuplicateKey)
1021 1022 1023 1024
	{
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
		return FALSE;
	}
1025 1026

	if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1027 1028 1029 1030
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1031 1032

	newkey->pProvider = prov;
1033
	newkey->dwMagic = MAGIC_CRYPTKEY;
1034 1035 1036 1037 1038
	if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
	{
		*phKey = (HCRYPTKEY)newkey;
		return TRUE;
	}
1039
	newkey->dwMagic = 0;
1040
	CRYPT_Free(newkey);
1041 1042 1043 1044 1045
	return FALSE;
}

/******************************************************************************
 * CryptEncrypt (ADVAPI32.@)
1046 1047 1048 1049
 *
 * Encrypts data.
 *
 * PARAMS
Lei Zhang's avatar
Lei Zhang committed
1050
 *  hKey       [I] Handle to the encryption key.
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
 *  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.
1066 1067 1068 1069
 */
BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
{
1070 1071 1072 1073
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

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

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

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

/******************************************************************************
1089
 * CryptEnumProvidersW (ADVAPI32.@)
1090
 *
Lei Zhang's avatar
Lei Zhang committed
1091
 * Returns the next available CSP.
1092 1093 1094 1095 1096 1097 1098 1099
 *
 * 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
1100
 *                    of bytes stored in the buffer on return.
1101 1102 1103 1104 1105 1106
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
 *
 *  NOTES
1107
 *   If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1108
 *   for memory allocation purposes.
1109
 */
1110 1111
BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1112
{
1113
	HKEY hKey;
1114 1115 1116 1117 1118 1119 1120 1121
	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};
1122
	BOOL ret;
1123

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

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

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

1144
	ret = TRUE;
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
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1156
			RegCloseKey(hKey);
1157 1158 1159
			return FALSE;
		}

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

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

1188
		if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1189 1190
			ret = FALSE;

1191 1192 1193
		RegCloseKey(subkey);
	}
	RegCloseKey(hKey);
1194
	return ret;
1195 1196 1197
}

/******************************************************************************
1198
 * CryptEnumProvidersA (ADVAPI32.@)
1199
 *
1200
 * See CryptEnumProvidersW.
1201
 */
1202 1203
BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1204
{
1205
	PWSTR str = NULL;
1206
	DWORD bufsize;
1207
	BOOL ret;
1208

1209
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1210
			pdwProvType, pszProvName, pcbProvName);
1211

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

/******************************************************************************
1236
 * CryptEnumProviderTypesW (ADVAPI32.@)
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
 *
 * 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
1247
 *                    of bytes stored in the buffer on return.
1248 1249 1250 1251 1252 1253
 *
 *  RETURNS
 *   Success: TRUE
 *   Failure: FALSE
 *
 *  NOTES
1254
 *   If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1255
 *   for memory allocation purposes.
1256
 */
1257 1258
BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1259
{
1260
	HKEY hKey, hSubkey;
1261
	DWORD keylen, numkeys, dwType;
1262
	PWSTR keyname, ch;
1263
	DWORD result;
1264 1265 1266 1267 1268 1269 1270 1271
	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};
1272

1273
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1274 1275 1276
		dwFlags, pdwProvType, pszTypeName, pcbTypeName);

	if (pdwReserved || !pdwProvType || !pcbTypeName)
1277 1278 1279 1280 1281 1282 1283 1284 1285
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if (dwFlags)
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}
1286

1287
	if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1288 1289
		return FALSE;

1290
	RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1291
	if (dwIndex >= numkeys)
1292 1293
	{
		SetLastError(ERROR_NO_MORE_ITEMS);
1294
		RegCloseKey(hKey);
1295 1296
		return FALSE;
	}
1297
	keylen++;
1298
	if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1299 1300
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1301
		RegCloseKey(hKey);
1302 1303
		return FALSE;
	}
1304
	if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1305
                CRYPT_Free(keyname);
1306
		RegCloseKey(hKey);
1307
		return FALSE;
1308
        }
1309
	RegOpenKeyW(hKey, keyname, &hSubkey);
1310 1311
	RegCloseKey(hKey);

1312
	ch = keyname + strlenW(keyname);
1313
	/* Convert "Type 000" to 0, etc/ */
1314 1315 1316 1317
	*pdwProvType = *(--ch) - '0';
	*pdwProvType += (*(--ch) - '0') * 10;
	*pdwProvType += (*(--ch) - '0') * 100;
	CRYPT_Free(keyname);
1318
	
1319
	result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1320
	if (result)
1321 1322
	{
		SetLastError(result);
1323
		RegCloseKey(hSubkey);
1324 1325
		return FALSE;
	}
1326

1327 1328
	RegCloseKey(hSubkey);
	return TRUE;
1329 1330 1331
}

/******************************************************************************
1332
 * CryptEnumProviderTypesA (ADVAPI32.@)
1333
 *
1334
 * See CryptEnumProviderTypesW.
1335
 */
1336 1337
BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
		DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1338
{
1339
	PWSTR str = NULL;
1340
	DWORD bufsize;
1341 1342
	BOOL ret;

1343
	TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1344
			pdwProvType, pszTypeName, pcbTypeName);
1345

1346
	if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1347
		return FALSE;
1348
	if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1349 1350 1351 1352
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1353
	ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1354 1355
	if (str)
		CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1356
	*pcbTypeName = bufsize / sizeof(WCHAR);
1357 1358 1359
	if (str)
	{
		CRYPT_Free(str);
1360
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1361 1362 1363 1364
		{
			SetLastError(ERROR_MORE_DATA);
			return FALSE;
		}
1365 1366
	}
	return ret;
1367 1368 1369 1370
}

/******************************************************************************
 * CryptExportKey (ADVAPI32.@)
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
 * 
 * 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.
1389 1390 1391 1392
 */
BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
		DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
1393 1394 1395
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;

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

1398 1399
	if (!key || !pdwDataLen || !key->pProvider ||
		key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1400 1401 1402 1403
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1404 1405 1406 1407

	prov = key->pProvider;
	return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
			dwBlobType, dwFlags, pbData, pdwDataLen);
1408 1409 1410 1411
}

/******************************************************************************
 * CryptGenKey (ADVAPI32.@)
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
 *
 * 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
1424 1425 1426
 */
BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
{
1427 1428 1429
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key;

1430
	TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1431

1432
	if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1433 1434 1435 1436
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1437
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1438 1439 1440 1441
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1442 1443

	key->pProvider = prov;
1444
	key->dwMagic = MAGIC_CRYPTKEY;
1445
	if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1446 1447 1448 1449
        {
            *phKey = (HCRYPTKEY)key;
            return TRUE;
        }
1450

1451
	/* CSP error! */
1452
	key->dwMagic = 0;
1453
	CRYPT_Free(key);
1454 1455 1456 1457
	return FALSE;
}

/******************************************************************************
1458
 * CryptGetDefaultProviderW (ADVAPI32.@)
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
 *
 * 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.
1476
 */
1477 1478
BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
		DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1479
{
1480
	HKEY hKey;
1481
	PWSTR keyname;
1482
	DWORD result;
1483
	static const WCHAR nameW[] = {'N','a','m','e',0};
1484 1485

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

1516
	if (result)
1517
	{
1518
		if (result != ERROR_MORE_DATA)
1519
			SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1520 1521 1522
		else
			SetLastError(result);
		
1523 1524
		return FALSE;
	}
1525
	
1526
	return TRUE;
1527 1528 1529
}

/******************************************************************************
1530
 * CryptGetDefaultProviderA (ADVAPI32.@)
1531
 *
1532
 * See CryptGetDefaultProviderW.
1533
 */
1534 1535
BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
		DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1536
{
1537
	PWSTR str = NULL;
1538
	DWORD bufsize;
1539
	BOOL ret;
1540

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

1543 1544
	CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
	if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1545 1546 1547 1548
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1549
	ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1550 1551
	if (str)
		CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1552
	*pcbProvName = bufsize / sizeof(WCHAR);
1553 1554 1555
	if (str)
	{
		CRYPT_Free(str);
1556
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1557 1558 1559 1560
		{
			SetLastError(ERROR_MORE_DATA);
			return FALSE;
		}
1561 1562
	}
	return ret;
1563
}
1564

1565 1566
/******************************************************************************
 * CryptGetHashParam (ADVAPI32.@)
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
 *
 * 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.
1583 1584 1585 1586
 */
BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1587 1588 1589
	PCRYPTPROV prov;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

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

1592 1593
	if (!hash || !pdwDataLen || !hash->pProvider ||
		hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1594 1595 1596 1597
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1598 1599 1600 1601

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

1604 1605
/******************************************************************************
 * CryptGetKeyParam (ADVAPI32.@)
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
 *
 * 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.
1622 1623 1624 1625
 */
BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1626 1627 1628
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;

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

1631 1632
	if (!key || !pdwDataLen || !key->pProvider ||
		key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1633 1634 1635 1636
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1637 1638 1639 1640

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

1643 1644
/******************************************************************************
 * CryptGetProvParam (ADVAPI32.@)
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
 *
 * 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.
1661 1662 1663 1664
 */
BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
		DWORD *pdwDataLen, DWORD dwFlags)
{
1665 1666
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

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

1669
	if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1670 1671 1672 1673
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1674

1675
	return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1676
}
1677

1678 1679
/******************************************************************************
 * CryptGetUserKey (ADVAPI32.@)
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
 *
 * 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
1691 1692 1693
 */
BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
{
1694 1695 1696
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY key;

1697
	TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1698 1699

	if (!prov)
1700 1701 1702 1703
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1704
	if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1705 1706 1707 1708
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1709
	if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1710 1711 1712 1713
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1714 1715

	key->pProvider = prov;
1716
	key->dwMagic = MAGIC_CRYPTKEY;
1717
	if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1718 1719 1720 1721
        {
            *phUserKey = (HCRYPTKEY)key;
            return TRUE;
        }
1722

1723
	/* CSP Error */
1724
	key->dwMagic = 0;
1725
	CRYPT_Free(key);
1726
	*phUserKey = 0;
1727
	return FALSE;
1728 1729
}

1730 1731
/******************************************************************************
 * CryptHashData (ADVAPI32.@)
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
 *
 * 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
1744
 */
1745
BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1746
{
1747 1748 1749
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;

1750
	TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1751 1752

	if (!hash)
1753 1754 1755 1756
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1757 1758
	if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
		hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1759 1760 1761 1762
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1763 1764 1765

	prov = hash->pProvider;
	return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1766 1767 1768 1769
}

/******************************************************************************
 * CryptHashSessionKey (ADVAPI32.@)
1770
 *
1771 1772
 * Compute the cryptographic hash of a session key object.
 *
1773 1774 1775 1776 1777 1778 1779 1780
 * 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
1781 1782 1783
 */
BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
{
1784 1785 1786 1787
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTKEY key = (PCRYPTKEY)hKey;
	PCRYPTPROV prov;

1788
	TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1789 1790

	if (!hash || !key)
1791 1792 1793 1794
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1795

1796 1797
	if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
		hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
1798 1799 1800 1801
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1802

1803 1804
	prov = hash->pProvider;
	return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1805 1806 1807 1808
}

/******************************************************************************
 * CryptImportKey (ADVAPI32.@)
1809
 *
1810 1811
 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
 *
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
 * 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
1823
 */
1824
BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1825 1826
		HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
{
1827 1828
	PCRYPTPROV prov = (PCRYPTPROV)hProv;
	PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1829

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

1832 1833 1834
	if (!prov || !pbData || !dwDataLen || !phKey ||
		prov->dwMagic != MAGIC_CRYPTPROV ||
		(pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
1835 1836 1837 1838
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1839 1840

	if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1841 1842 1843 1844
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
1845 1846

	importkey->pProvider = prov;
1847
	importkey->dwMagic = MAGIC_CRYPTKEY;
1848 1849 1850 1851 1852 1853 1854
	if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
			pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
	{
		*phKey = (HCRYPTKEY)importkey;
		return TRUE;
	}

1855
	importkey->dwMagic = 0;
1856
	CRYPT_Free(importkey);
1857 1858 1859 1860
	return FALSE;
}

/******************************************************************************
1861
 * CryptSignHashW (ADVAPI32.@)
1862 1863 1864 1865 1866 1867
 *
 * Signs data.
 *
 * PARAMS
 *  hHash        [I] Handle of the hash object to be signed.
 *  dwKeySpec    [I] Private key to use.
1868
 *  sDescription [I] Should be NULL.
1869 1870 1871 1872 1873 1874 1875
 *  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
1876 1877 1878 1879 1880
 *
 * 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.
1881
 */
1882
BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1883 1884
		DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
{
1885 1886
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTPROV prov;
1887

1888
	TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", 
1889
		hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1890 1891

	if (!hash)
1892 1893 1894 1895
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
1896 1897
	if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
		 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1898 1899 1900 1901
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1902 1903

	prov = hash->pProvider;
1904
	return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1905
		dwFlags, pbSignature, pdwSigLen);
1906 1907
}

1908 1909 1910
/******************************************************************************
 * CryptSignHashA (ADVAPI32.@)
 *
1911
 * See CryptSignHashW.
1912 1913 1914 1915 1916 1917 1918
 */
BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
		DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
{
	LPWSTR wsDescription;
	BOOL result;

1919
	TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", 
1920 1921 1922 1923
		hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);

	CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
	result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1924
	CRYPT_Free(wsDescription);
1925 1926 1927 1928

	return result;
}

1929 1930
/******************************************************************************
 * CryptSetHashParam (ADVAPI32.@)
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
 *
 * 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
1943
 */
1944
BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1945
{
1946 1947 1948
	PCRYPTPROV prov;
	PCRYPTHASH hash = (PCRYPTHASH)hHash;

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

1951 1952
	if (!hash || !pbData || !hash->pProvider ||
		hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1953 1954 1955 1956
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1957 1958 1959 1960

	prov = hash->pProvider;
	return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
			dwParam, pbData, dwFlags);
1961 1962 1963
}

/******************************************************************************
1964
 * CryptSetKeyParam (ADVAPI32.@)
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
 *
 * 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
1977
 */
1978
BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1979
{
1980 1981 1982
	PCRYPTPROV prov;
	PCRYPTKEY key = (PCRYPTKEY)hKey;

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

1985 1986
	if (!key || !pbData || !key->pProvider ||
		key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1987 1988 1989 1990
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
1991 1992 1993 1994

	prov = key->pProvider;
	return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
			dwParam, pbData, dwFlags);
1995 1996 1997 1998
}

/******************************************************************************
 * CryptSetProviderA (ADVAPI32.@)
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
 *
 * 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
2009 2010 2011
 */
BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
{
2012
	TRACE("(%s, %d)\n", pszProvName, dwProvType);
2013
	return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2014 2015 2016 2017
}

/******************************************************************************
 * CryptSetProviderW (ADVAPI32.@)
2018
 *
2019
 * See CryptSetProviderA.
2020 2021 2022
 */
BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
{
2023
	TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2024
	return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2025 2026 2027
}

/******************************************************************************
2028
 * CryptSetProviderExW (ADVAPI32.@)
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
 *
 * 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
2041
 */
2042
BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2043
{
2044
	HKEY hProvKey, hTypeKey;
2045 2046
	PWSTR keyname;
	static const WCHAR nameW[] = {'N','a','m','e',0};
2047

2048
	TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2049 2050

	if (!pszProvName || pdwReserved)
2051 2052 2053 2054
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
2055
	if (dwProvType > MAXPROVTYPES)
2056 2057 2058 2059
	{
		SetLastError(NTE_BAD_PROV_TYPE);
		return FALSE;
	}
2060 2061
	if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
			|| dwFlags == CRYPT_DELETE_DEFAULT)
2062 2063 2064 2065 2066
	{
		SetLastError(NTE_BAD_FLAGS);
		return FALSE;
	}

2067
	if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2068 2069 2070 2071
	{
		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
		return FALSE;
	}
2072
	if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2073
		keyname, &hTypeKey))
2074 2075
	{
		CRYPT_Free(keyname);
2076 2077
		SetLastError(NTE_BAD_PROVIDER);
		return FALSE;
2078 2079
	}
	CRYPT_Free(keyname);
2080 2081 2082
	
	if (dwFlags & CRYPT_DELETE_DEFAULT)
	{
2083
		RegDeleteValueW(hTypeKey, nameW);
2084 2085 2086 2087 2088 2089
	}
	else
	{
		if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
		{
			RegCloseKey(hTypeKey);
2090 2091
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
2092
		}
2093
		if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2094 2095 2096 2097
			keyname, &hProvKey))
		{
			CRYPT_Free(keyname);
			RegCloseKey(hTypeKey);
2098 2099
			SetLastError(NTE_BAD_PROVIDER);
			return FALSE;
2100 2101 2102
		}
		CRYPT_Free(keyname);
		
2103 2104
		if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
			(strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
		{
			RegCloseKey(hTypeKey);
			RegCloseKey(hProvKey);
			return FALSE;
		}
		
		RegCloseKey(hProvKey);
	}
	RegCloseKey(hTypeKey);

2115
	return TRUE;
2116 2117 2118
}

/******************************************************************************
2119
 * CryptSetProviderExA (ADVAPI32.@)
2120
 *
2121
 * See CryptSetProviderExW.
2122
 */
2123
BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2124
{
2125
	BOOL ret = FALSE;
2126
	PWSTR str = NULL;
2127

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

2130
	if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2131
	{
2132
		ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2133 2134 2135
		CRYPT_Free(str);
	}
	return ret;
2136 2137 2138 2139
}

/******************************************************************************
 * CryptSetProvParam (ADVAPI32.@)
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
 *
 * 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
2152
 */
2153
BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2154
{
2155 2156
	PCRYPTPROV prov = (PCRYPTPROV)hProv;

2157
	TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2158 2159

	if (!prov)
2160 2161 2162 2163
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
2164
	if (prov->dwMagic != MAGIC_CRYPTPROV)
2165 2166 2167 2168
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
2169 2170 2171 2172 2173 2174 2175
	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)
	{
2176 2177
		/* FIXME: Should verify the parameter */
		if (pbData /* && IsWindow((HWND)pbData) */)
2178 2179 2180 2181 2182 2183 2184 2185
		{
			crypt_hWindow = (HWND)(pbData);
			return TRUE;
		} else {
			SetLastError(ERROR_INVALID_PARAMETER);
			return FALSE;
		}
	}
2186
	/* All other flags go to the CSP */
2187
	return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2188 2189 2190
}

/******************************************************************************
2191
 * CryptVerifySignatureW (ADVAPI32.@)
2192 2193 2194 2195 2196 2197 2198 2199
 *
 * 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.
2200
 *  sDescription [I] Should be NULL.
2201 2202 2203 2204 2205
 *  dwFlags      [I] See MSDN doc.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2206 2207 2208 2209 2210
 * 
 * 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.
2211
 */
2212
BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2213
		HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2214
{
2215 2216 2217
	PCRYPTHASH hash = (PCRYPTHASH)hHash;
	PCRYPTKEY key = (PCRYPTKEY)hPubKey;
	PCRYPTPROV prov;
2218

2219
	TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2220
			dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2221

2222
	if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
2223 2224 2225
	    !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
	    !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
	{
2226 2227
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
2228 2229
	}
		
2230 2231
	prov = hash->pProvider;
	return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2232
		key->hPrivate, sDescription, dwFlags);
2233
}
2234

2235 2236 2237
/******************************************************************************
 * CryptVerifySignatureA (ADVAPI32.@)
 *
2238
 * See CryptVerifySignatureW.
2239
 */
2240
BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2241 2242 2243 2244 2245
		HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
{
	LPWSTR wsDescription;
	BOOL result;

2246
	TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2247 2248 2249 2250
			dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);

	CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
	result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2251
	CRYPT_Free(wsDescription);
2252 2253 2254

	return result;
}
2255

2256
/******************************************************************************
2257
 * SystemFunction030   (ADVAPI32.@)
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
 *
 * 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
 */
2268
BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2269 2270 2271 2272
{
    return !memcmp(b1, b2, 0x10);
}

2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
/******************************************************************************
 * 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;
}

2288 2289 2290 2291 2292 2293 2294 2295
/******************************************************************************
 * 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.
2296 2297 2298 2299
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
2300 2301
 */

2302
BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
{
    int dev_random;

    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);
    }
2316 2317
    else
        FIXME("couldn't open /dev/urandom\n");
2318 2319 2320 2321
    SetLastError(NTE_FAIL);
    return FALSE;
}    
    
2322 2323 2324 2325 2326 2327 2328 2329
/*
   These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
   in crypt32.dll.
 */

/******************************************************************************
 * SystemFunction040   (ADVAPI32.@)
 *
2330
 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2331
 *
2332 2333 2334 2335 2336 2337 2338 2339
 * 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
 *    
2340 2341 2342 2343
 * RETURNS
 *  Success: STATUS_SUCCESS
 *  Failure: NTSTATUS error code
 *
2344 2345 2346 2347
 * 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.
2348
 */
2349
NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2350
{
2351
	FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2352 2353 2354 2355 2356 2357
	return STATUS_SUCCESS;
}

/******************************************************************************
 * SystemFunction041  (ADVAPI32.@)
 *
2358
 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2359
 *
2360 2361 2362 2363 2364 2365 2366 2367
 * 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
 *
2368 2369 2370 2371
 * RETURNS
 *  Success: STATUS_SUCCESS
 *  Failure: NTSTATUS error code
 *
2372 2373 2374 2375
 * 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.
2376
 */
2377
NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2378
{
2379
	FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2380 2381
	return STATUS_SUCCESS;
}