1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winerror.h>
#include <wincrypt.h>
#include "wine/test.h"
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*);
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";
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);
protected = pCryptProtectData(NULL,desc,NULL,NULL,NULL,0,&cipher);
ok(!protected, "Encrypting without plain data source.\n");
r = GetLastError();
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
SetLastError(0xDEADBEEF);
protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,NULL);
ok(!protected, "Encrypting without cipher destination.\n");
r = GetLastError();
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
cipher.pbData=NULL;
cipher.cbData=0;
/* without entropy */
SetLastError(0xDEADBEEF);
protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
ok(protected ||
broken(!protected), /* Win9x/NT4 */
"Encrypting without entropy.\n");
if (protected)
{
r = GetLastError();
ok(r == ERROR_SUCCESS ||
r == ERROR_IO_PENDING, /* win2k */
"Expected ERROR_SUCCESS or ERROR_IO_PENDING, got %d\n",r);
}
cipher_entropy.pbData=NULL;
cipher_entropy.cbData=0;
/* with entropy */
SetLastError(0xDEADBEEF);
protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
ok(protected ||
broken(!protected), /* Win9x/NT4 */
"Encrypting with entropy.\n");
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);
protected = pCryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
if (!protected)
{
/* fails in win2k */
ok(GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
}
}
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;
/* fails in win2k */
if (!protected)
{
skip("CryptProtectData failed to run\n");
return;
}
plain.pbData=NULL;
plain.cbData=0;
SetLastError(0xDEADBEEF);
okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
ok(!okay,"Decrypting without destination\n");
r = GetLastError();
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
SetLastError(0xDEADBEEF);
okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
ok(!okay,"Decrypting without source\n");
r = GetLastError();
ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
plain.pbData=NULL;
plain.cbData=0;
SetLastError(0xDEADBEEF);
okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
ok(!okay,"Decrypting without needed entropy\n");
r = GetLastError();
ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
plain.pbData=NULL;
plain.cbData=0;
data_desc=NULL;
/* without entropy */
SetLastError(0xDEADBEEF);
okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
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");
ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
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);
okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
ok(!okay,"Decrypting with wrong entropy\n");
r = GetLastError();
ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
/* with entropy */
SetLastError(0xDEADBEEF);
okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
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");
ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
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);
okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
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");
ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
ok(data_desc!=NULL,"Description not allocated\n");
ok(data_desc[0]=='\0',"Description not empty\n");
LocalFree(data_desc);
LocalFree(plain.pbData);
plain.pbData=NULL;
plain.cbData=0;
}
static void test_simpleroundtrip(const char *plaintext)
{
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);
ok(res != 0 || broken(!res), "can't protect\n");
if (!res)
{
/* Fails on Win9x, NT4 */
win_skip("CryptProtectData failed\n");
return;
}
res = pCryptUnprotectData(&encrypted, NULL, NULL, NULL, NULL, 0, &output);
ok(res != 0, "can't unprotect; last error %u\n", GetLastError());
ok(output.cbData == strlen(plaintext), "output wrong length %d for input '%s', wanted %d\n", output.cbData, plaintext, lstrlenA(plaintext));
ok(!memcmp(plaintext, (char *)output.pbData, output.cbData), "output wrong contents for input '%s'\n", plaintext);
LocalFree(output.pbData);
LocalFree(encrypted.pbData);
}
START_TEST(protectdata)
{
HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
if (!pCryptProtectData || !pCryptUnprotectData)
{
win_skip("Crypt(Un)ProtectData() is not available\n");
return;
}
protected=FALSE;
test_cryptprotectdata();
test_cryptunprotectdata();
test_simpleroundtrip("");
test_simpleroundtrip("hello");
/* 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);
}
|