2005-05-24 11:58:26 +02:00
|
|
|
/*
|
|
|
|
* Unit test suite for crypt32.dll's CryptProtectData/CryptUnprotectData
|
|
|
|
*
|
|
|
|
* Copyright 2005 Kees Cook <kees@outflux.net>
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-05-24 11:58:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <winerror.h>
|
|
|
|
#include <wincrypt.h>
|
|
|
|
|
|
|
|
#include "wine/test.h"
|
|
|
|
|
2007-11-20 13:48:48 +01:00
|
|
|
static BOOL (WINAPI *pCryptProtectData)(DATA_BLOB*,LPCWSTR,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);
|
|
|
|
static BOOL (WINAPI *pCryptUnprotectData)(DATA_BLOB*,LPWSTR*,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);
|
|
|
|
|
2006-10-05 17:57:26 +02:00
|
|
|
static char secret[] = "I am a super secret string that no one can see!";
|
|
|
|
static char secret2[] = "I am a super secret string indescribable string";
|
|
|
|
static char key[] = "Wibble wibble wibble";
|
2005-05-24 11:58:26 +02:00
|
|
|
static const WCHAR desc[] = {'U','l','t','r','a',' ','s','e','c','r','e','t',' ','t','e','s','t',' ','m','e','s','s','a','g','e',0};
|
|
|
|
static BOOL protected = FALSE; /* if true, the unprotect tests can run */
|
|
|
|
static DATA_BLOB cipher;
|
|
|
|
static DATA_BLOB cipher_entropy;
|
|
|
|
static DATA_BLOB cipher_no_desc;
|
|
|
|
|
|
|
|
static void test_cryptprotectdata(void)
|
|
|
|
{
|
|
|
|
LONG r;
|
|
|
|
DATA_BLOB plain;
|
|
|
|
DATA_BLOB entropy;
|
|
|
|
|
|
|
|
plain.pbData=(void*)secret;
|
|
|
|
plain.cbData=strlen(secret)+1;
|
|
|
|
|
|
|
|
entropy.pbData=(void*)key;
|
|
|
|
entropy.cbData=strlen(key)+1;
|
|
|
|
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
protected = pCryptProtectData(NULL,desc,NULL,NULL,NULL,0,&cipher);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!protected, "Encrypting without plain data source.\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%lu) GetLastError seen\n",r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,NULL);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!protected, "Encrypting without cipher destination.\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%lu) GetLastError seen\n",r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
cipher.pbData=NULL;
|
|
|
|
cipher.cbData=0;
|
|
|
|
|
|
|
|
/* without entropy */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
|
2009-01-30 02:01:14 +01:00
|
|
|
ok(protected ||
|
|
|
|
broken(!protected), /* Win9x/NT4 */
|
|
|
|
"Encrypting without entropy.\n");
|
|
|
|
if (protected)
|
|
|
|
{
|
|
|
|
r = GetLastError();
|
|
|
|
ok(r == ERROR_SUCCESS ||
|
|
|
|
r == ERROR_IO_PENDING, /* win2k */
|
2022-02-23 09:12:05 +01:00
|
|
|
"Expected ERROR_SUCCESS or ERROR_IO_PENDING, got %ld\n",r);
|
2009-01-30 02:01:14 +01:00
|
|
|
}
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
cipher_entropy.pbData=NULL;
|
|
|
|
cipher_entropy.cbData=0;
|
|
|
|
|
|
|
|
/* with entropy */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
|
2009-01-30 02:01:14 +01:00
|
|
|
ok(protected ||
|
|
|
|
broken(!protected), /* Win9x/NT4 */
|
|
|
|
"Encrypting with entropy.\n");
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
cipher_no_desc.pbData=NULL;
|
|
|
|
cipher_no_desc.cbData=0;
|
|
|
|
|
|
|
|
/* with entropy but no description */
|
|
|
|
plain.pbData=(void*)secret2;
|
|
|
|
plain.cbData=strlen(secret2)+1;
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
protected = pCryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
|
2008-07-24 20:52:19 +02:00
|
|
|
if (!protected)
|
2008-04-11 01:06:29 +02:00
|
|
|
{
|
|
|
|
/* fails in win2k */
|
2008-07-24 20:52:19 +02:00
|
|
|
ok(GetLastError() == ERROR_INVALID_PARAMETER,
|
2022-02-23 09:12:05 +01:00
|
|
|
"Expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
|
2008-04-11 01:06:29 +02:00
|
|
|
}
|
2005-05-24 11:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cryptunprotectdata(void)
|
|
|
|
{
|
|
|
|
LONG r;
|
|
|
|
DATA_BLOB plain;
|
|
|
|
DATA_BLOB entropy;
|
|
|
|
BOOL okay;
|
|
|
|
WCHAR * data_desc;
|
|
|
|
|
|
|
|
entropy.pbData=(void*)key;
|
|
|
|
entropy.cbData=strlen(key)+1;
|
|
|
|
|
2008-04-11 01:06:29 +02:00
|
|
|
/* fails in win2k */
|
|
|
|
if (!protected)
|
|
|
|
{
|
2008-06-04 12:33:03 +02:00
|
|
|
skip("CryptProtectData failed to run\n");
|
2008-04-11 01:06:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!okay,"Decrypting without destination\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%lu) GetLastError seen\n",r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!okay,"Decrypting without source\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%lu) GetLastError seen\n",r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!okay,"Decrypting without needed entropy\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_DATA, "Wrong (%lu) GetLastError seen\n", r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
data_desc=NULL;
|
|
|
|
|
|
|
|
/* without entropy */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(okay,"Decrypting without entropy\n");
|
|
|
|
|
|
|
|
ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
|
|
|
|
ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
|
2005-08-29 11:38:19 +02:00
|
|
|
ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(data_desc!=NULL,"Description not allocated\n");
|
|
|
|
ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
|
|
|
|
|
|
|
|
LocalFree(plain.pbData);
|
|
|
|
LocalFree(data_desc);
|
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
data_desc=NULL;
|
|
|
|
|
|
|
|
/* with wrong entropy */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(!okay,"Decrypting with wrong entropy\n");
|
|
|
|
r = GetLastError();
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(r == ERROR_INVALID_DATA, "Wrong (%lu) GetLastError seen\n",r);
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
/* with entropy */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(okay,"Decrypting with entropy\n");
|
|
|
|
|
|
|
|
ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
|
|
|
|
ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
|
2005-08-29 11:38:19 +02:00
|
|
|
ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(data_desc!=NULL,"Description not allocated\n");
|
|
|
|
ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
|
|
|
|
|
|
|
|
LocalFree(plain.pbData);
|
|
|
|
LocalFree(data_desc);
|
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
data_desc=NULL;
|
|
|
|
|
|
|
|
/* with entropy but no description */
|
|
|
|
SetLastError(0xDEADBEEF);
|
2007-11-20 13:48:48 +01:00
|
|
|
okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(okay,"Decrypting with entropy and no description\n");
|
|
|
|
|
|
|
|
ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
|
|
|
|
ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
|
2005-08-29 11:38:19 +02:00
|
|
|
ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
|
2005-05-24 11:58:26 +02:00
|
|
|
ok(data_desc!=NULL,"Description not allocated\n");
|
|
|
|
ok(data_desc[0]=='\0',"Description not empty\n");
|
|
|
|
|
2007-11-07 18:24:55 +01:00
|
|
|
LocalFree(data_desc);
|
2005-05-24 11:58:26 +02:00
|
|
|
LocalFree(plain.pbData);
|
|
|
|
|
|
|
|
plain.pbData=NULL;
|
|
|
|
plain.cbData=0;
|
|
|
|
}
|
|
|
|
|
2009-12-02 14:59:56 +01:00
|
|
|
static void test_simpleroundtrip(const char *plaintext)
|
2009-10-28 02:40:31 +01:00
|
|
|
{
|
|
|
|
DATA_BLOB input;
|
|
|
|
DATA_BLOB encrypted;
|
|
|
|
DATA_BLOB output;
|
|
|
|
int res;
|
|
|
|
WCHAR emptyW[1];
|
|
|
|
|
|
|
|
emptyW[0] = 0;
|
|
|
|
input.pbData = (unsigned char *)plaintext;
|
|
|
|
input.cbData = strlen(plaintext);
|
|
|
|
res = pCryptProtectData(&input, emptyW, NULL, NULL, NULL, 0, &encrypted);
|
2009-11-06 22:08:38 +01:00
|
|
|
ok(res != 0 || broken(!res), "can't protect\n");
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
/* Fails on Win9x, NT4 */
|
|
|
|
win_skip("CryptProtectData failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-10-28 02:40:31 +01:00
|
|
|
|
|
|
|
res = pCryptUnprotectData(&encrypted, NULL, NULL, NULL, NULL, 0, &output);
|
2022-02-23 09:12:05 +01:00
|
|
|
ok(res != 0, "can't unprotect; last error %lu\n", GetLastError());
|
|
|
|
ok(output.cbData == strlen(plaintext), "output wrong length %ld for input '%s', wanted %d\n", output.cbData, plaintext, lstrlenA(plaintext));
|
2009-12-02 14:59:56 +01:00
|
|
|
ok(!memcmp(plaintext, (char *)output.pbData, output.cbData), "output wrong contents for input '%s'\n", plaintext);
|
2009-12-06 01:21:21 +01:00
|
|
|
LocalFree(output.pbData);
|
|
|
|
LocalFree(encrypted.pbData);
|
2009-10-28 02:40:31 +01:00
|
|
|
}
|
|
|
|
|
2005-05-24 11:58:26 +02:00
|
|
|
START_TEST(protectdata)
|
|
|
|
{
|
2007-11-20 13:48:48 +01:00
|
|
|
HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
|
|
|
|
pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
|
|
|
|
pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
|
|
|
|
if (!pCryptProtectData || !pCryptUnprotectData)
|
|
|
|
{
|
2009-02-24 00:04:36 +01:00
|
|
|
win_skip("Crypt(Un)ProtectData() is not available\n");
|
2007-11-20 13:48:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-05-24 11:58:26 +02:00
|
|
|
|
2007-11-20 13:48:48 +01:00
|
|
|
protected=FALSE;
|
2005-05-24 11:58:26 +02:00
|
|
|
test_cryptprotectdata();
|
|
|
|
test_cryptunprotectdata();
|
2009-12-02 14:59:56 +01:00
|
|
|
test_simpleroundtrip("");
|
|
|
|
test_simpleroundtrip("hello");
|
2005-05-24 11:58:26 +02:00
|
|
|
|
|
|
|
/* deinit globals here */
|
|
|
|
if (cipher.pbData) LocalFree(cipher.pbData);
|
|
|
|
if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
|
|
|
|
if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);
|
|
|
|
}
|