urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / crypt32 / tests / protectdata.c
1 /*
2  * Unit test suite for crypt32.dll's CryptProtectData/CryptUnprotectData
3  *
4  * Copyright 2005 Kees Cook <kees@outflux.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
27
28 #include "wine/test.h"
29
30 static BOOL (WINAPI *pCryptProtectData)(DATA_BLOB*,LPCWSTR,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);
31 static BOOL (WINAPI *pCryptUnprotectData)(DATA_BLOB*,LPWSTR*,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);
32
33 static char  secret[]     = "I am a super secret string that no one can see!";
34 static char  secret2[]    = "I am a super secret string indescribable string";
35 static char  key[]        = "Wibble wibble wibble";
36 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};
37 static BOOL protected = FALSE; /* if true, the unprotect tests can run */
38 static DATA_BLOB cipher;
39 static DATA_BLOB cipher_entropy;
40 static DATA_BLOB cipher_no_desc;
41
42 static void test_cryptprotectdata(void)
43 {
44     LONG r;
45     DATA_BLOB plain;
46     DATA_BLOB entropy;
47
48     plain.pbData=(void*)secret;
49     plain.cbData=strlen(secret)+1;
50
51     entropy.pbData=(void*)key;
52     entropy.cbData=strlen(key)+1;
53
54     SetLastError(0xDEADBEEF);
55     protected = pCryptProtectData(NULL,desc,NULL,NULL,NULL,0,&cipher);
56     ok(!protected, "Encrypting without plain data source.\n");
57     r = GetLastError();
58     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
59
60     SetLastError(0xDEADBEEF);
61     protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,NULL);
62     ok(!protected, "Encrypting without cipher destination.\n");
63     r = GetLastError();
64     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
65
66     cipher.pbData=NULL;
67     cipher.cbData=0;
68
69     /* without entropy */
70     SetLastError(0xDEADBEEF);
71     protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
72     ok(protected, "Encrypting without entropy.\n");
73     r = GetLastError();
74     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
75
76     cipher_entropy.pbData=NULL;
77     cipher_entropy.cbData=0;
78
79     /* with entropy */
80     SetLastError(0xDEADBEEF);
81     protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
82     ok(protected, "Encrypting with entropy.\n");
83     r = GetLastError();
84     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
85
86     cipher_no_desc.pbData=NULL;
87     cipher_no_desc.cbData=0;
88
89     /* with entropy but no description */
90     plain.pbData=(void*)secret2;
91     plain.cbData=strlen(secret2)+1;
92     SetLastError(0xDEADBEEF);
93     protected = pCryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
94     ok(protected, "Encrypting with entropy and no description.\n");
95     r = GetLastError();
96     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
97 }
98
99 static void test_cryptunprotectdata(void)
100 {
101     LONG r;
102     DATA_BLOB plain;
103     DATA_BLOB entropy;
104     BOOL okay;
105     WCHAR * data_desc;
106
107     entropy.pbData=(void*)key;
108     entropy.cbData=strlen(key)+1;
109
110     ok(protected, "CryptProtectData failed to run, so I can't test its output\n");
111     if (!protected) return;
112
113     plain.pbData=NULL;
114     plain.cbData=0;
115
116     SetLastError(0xDEADBEEF);
117     okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
118     ok(!okay,"Decrypting without destination\n");
119     r = GetLastError();
120     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
121
122     SetLastError(0xDEADBEEF);
123     okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
124     ok(!okay,"Decrypting without source\n");
125     r = GetLastError();
126     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
127
128     plain.pbData=NULL;
129     plain.cbData=0;
130
131     SetLastError(0xDEADBEEF);
132     okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
133     ok(!okay,"Decrypting without needed entropy\n");
134     r = GetLastError();
135     ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
136
137     plain.pbData=NULL;
138     plain.cbData=0;
139     data_desc=NULL;
140
141     /* without entropy */
142     SetLastError(0xDEADBEEF);
143     okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
144     ok(okay,"Decrypting without entropy\n");
145     r = GetLastError();
146     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
147
148     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
149     ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
150     ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
151     ok(data_desc!=NULL,"Description not allocated\n");
152     ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
153
154     LocalFree(plain.pbData);
155     LocalFree(data_desc);
156
157     plain.pbData=NULL;
158     plain.cbData=0;
159     data_desc=NULL;
160
161     /* with wrong entropy */
162     SetLastError(0xDEADBEEF);
163     okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
164     ok(!okay,"Decrypting with wrong entropy\n");
165     r = GetLastError();
166     ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
167
168     /* with entropy */
169     SetLastError(0xDEADBEEF);
170     okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
171     ok(okay,"Decrypting with entropy\n");
172     r = GetLastError();
173     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
174
175     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
176     ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
177     ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
178     ok(data_desc!=NULL,"Description not allocated\n");
179     ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
180
181     LocalFree(plain.pbData);
182     LocalFree(data_desc);
183
184     plain.pbData=NULL;
185     plain.cbData=0;
186     data_desc=NULL;
187
188     /* with entropy but no description */
189     SetLastError(0xDEADBEEF);
190     okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
191     ok(okay,"Decrypting with entropy and no description\n");
192     r = GetLastError();
193     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
194
195     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
196     ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
197     ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
198     ok(data_desc!=NULL,"Description not allocated\n");
199     ok(data_desc[0]=='\0',"Description not empty\n");
200
201     LocalFree(data_desc);
202     LocalFree(plain.pbData);
203
204     plain.pbData=NULL;
205     plain.cbData=0;
206 }
207
208 START_TEST(protectdata)
209 {
210     HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
211     hCrypt32 = GetModuleHandleA("crypt32.dll");
212     pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
213     pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
214     if (!pCryptProtectData || !pCryptUnprotectData)
215     {
216         skip("Crypt(Un)ProtectData() is not available\n");
217         return;
218     }
219
220     protected=FALSE;
221     test_cryptprotectdata();
222     test_cryptunprotectdata();
223
224     /* deinit globals here */
225     if (cipher.pbData) LocalFree(cipher.pbData);
226     if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
227     if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);
228 }