2 * Unit tests for crypt functions
4 * Copyright (c) 2004 Michael Jung
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/test.h"
30 static const char szRsaBaseProv
[] = MS_DEF_PROV_A
;
31 static const char szNonExistentProv
[] = "Wine Non Existent Cryptographic Provider v11.2";
32 static const char szKeySet
[] = "wine_test_keyset";
33 static const char szBadKeySet
[] = "wine_test_bad_keyset";
34 #define NON_DEF_PROV_TYPE 999
36 static void init_environment(void)
40 /* Ensure that container "wine_test_keyset" does exist */
41 if (!CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
43 CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
45 CryptReleaseContext(hProv
, 0);
47 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
48 if (!CryptAcquireContext(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, 0))
50 CryptAcquireContext(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
52 CryptReleaseContext(hProv
, 0);
54 /* Ensure that container "wine_test_bad_keyset" does not exist. */
55 if (CryptAcquireContext(&hProv
, szBadKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
57 CryptReleaseContext(hProv
, 0);
58 CryptAcquireContext(&hProv
, szBadKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
62 static void clean_up_environment(void)
66 /* Remove container "wine_test_keyset" */
67 if (CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
69 CryptReleaseContext(hProv
, 0);
70 CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
73 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
74 if (CryptAcquireContext(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, 0))
76 CryptReleaseContext(hProv
, 0);
77 CryptAcquireContext(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
81 static void test_acquire_context(void)
86 /* Provoke all kinds of error conditions (which are easy to provoke).
87 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
88 * but since this is likely to change between CSP versions, we don't check
89 * this. Please don't change the order of tests. */
90 result
= CryptAcquireContext(&hProv
, NULL
, NULL
, 0, 0);
91 ok(!result
&& GetLastError()==NTE_BAD_PROV_TYPE
, "%08x\n", (unsigned int)GetLastError());
93 result
= CryptAcquireContext(&hProv
, NULL
, NULL
, 1000, 0);
94 ok(!result
&& GetLastError()==NTE_BAD_PROV_TYPE
, "%08x\n", (unsigned int)GetLastError());
96 result
= CryptAcquireContext(&hProv
, NULL
, NULL
, NON_DEF_PROV_TYPE
, 0);
97 ok(!result
&& GetLastError()==NTE_PROV_TYPE_NOT_DEF
, "%08x\n", (unsigned int)GetLastError());
99 result
= CryptAcquireContext(&hProv
, szKeySet
, szNonExistentProv
, PROV_RSA_FULL
, 0);
100 ok(!result
&& GetLastError()==NTE_KEYSET_NOT_DEF
, "%08x\n", (unsigned int)GetLastError());
102 result
= CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, NON_DEF_PROV_TYPE
, 0);
103 ok(!result
&& GetLastError()==NTE_PROV_TYPE_NO_MATCH
, "%08x\n", (unsigned int)GetLastError());
105 result
= CryptAcquireContext(NULL
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0);
106 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "%08x\n", (unsigned int)GetLastError());
108 /* Last not least, try to really acquire a context. */
109 result
= CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0);
110 ok(result
, "%08x\n", (unsigned int)GetLastError());
112 if (GetLastError() == ERROR_SUCCESS
)
113 CryptReleaseContext(hProv
, 0);
115 /* Try again, witch an empty ("\0") szProvider parameter */
116 result
= CryptAcquireContext(&hProv
, szKeySet
, "", PROV_RSA_FULL
, 0);
117 ok(result
, "%08x\n", (unsigned int)GetLastError());
119 if (GetLastError() == ERROR_SUCCESS
)
120 CryptReleaseContext(hProv
, 0);
123 static BOOL
FindProvRegVals(DWORD dwIndex
, DWORD
*pdwProvType
, LPSTR
*pszProvName
,
124 DWORD
*pcbProvName
, DWORD
*pdwProvCount
)
128 DWORD size
= sizeof(DWORD
);
130 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey
))
133 RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, pdwProvCount
, pcbProvName
,
134 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
136 if (!(*pszProvName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbProvName
))))
139 RegEnumKeyEx(hKey
, dwIndex
, *pszProvName
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
142 RegOpenKey(hKey
, *pszProvName
, &subkey
);
143 RegQueryValueEx(subkey
, "Type", NULL
, NULL
, (BYTE
*)pdwProvType
, &size
);
151 static void test_enum_providers(void)
153 /* expected results */
154 CHAR
*pszProvName
= NULL
;
161 CHAR
*provider
= NULL
;
167 DWORD notZeroFlags
= 5;
169 if (!FindProvRegVals(dwIndex
, &dwType
, &pszProvName
, &cbName
, &provCount
))
172 /* check pdwReserved flag for NULL */
173 result
= CryptEnumProviders(dwIndex
, ¬Null
, 0, &type
, NULL
, &providerLen
);
174 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "%08x\n", (unsigned int)GetLastError());
176 /* check dwFlags == 0 */
177 result
= CryptEnumProviders(dwIndex
, NULL
, notZeroFlags
, &type
, NULL
, &providerLen
);
178 ok(!result
&& GetLastError()==NTE_BAD_FLAGS
, "%08x\n", (unsigned int)GetLastError());
180 /* alloc provider to half the size required
181 * cbName holds the size required */
184 providerLen
= cbName
/ 2;
185 if (!(provider
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, providerLen
))))
188 result
= CryptEnumProviders(dwIndex
, NULL
, 0, &type
, provider
, &providerLen
);
189 ok(!result
&& GetLastError()==ERROR_MORE_DATA
, "expected %08x, got %08x\n",
190 ERROR_MORE_DATA
, (unsigned int)GetLastError());
195 /* loop through the providers to get the number of providers
196 * after loop ends, count should be provCount + 1 so subtract 1
197 * to get actual number of providers */
199 while(CryptEnumProviders(count
++, NULL
, 0, &dwType
, NULL
, &providerLen
))
202 ok(count
==provCount
, "expected %i, got %i\n", (int)provCount
, (int)count
);
204 /* loop past the actual number of providers to get the error
205 * ERROR_NO_MORE_ITEMS */
206 for (count
= 0; count
< provCount
+ 1; count
++)
207 result
= CryptEnumProviders(count
, NULL
, 0, &dwType
, NULL
, &providerLen
);
208 ok(!result
&& GetLastError()==ERROR_NO_MORE_ITEMS
, "expected %08x, got %08x\n",
209 ERROR_NO_MORE_ITEMS
, (unsigned int)GetLastError());
211 /* check expected versus actual values returned */
212 result
= CryptEnumProviders(dwIndex
, NULL
, 0, &type
, NULL
, &providerLen
);
213 ok(result
&& providerLen
==cbName
, "expected %i, got %i\n", (int)cbName
, (int)providerLen
);
214 if (!(provider
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, providerLen
))))
217 result
= CryptEnumProviders(dwIndex
, NULL
, 0, &type
, provider
, &providerLen
);
218 ok(result
&& type
==dwType
, "expected %i, got %i\n",
219 (unsigned int)dwType
, (unsigned int)type
);
220 ok(result
&& !strcmp(pszProvName
, provider
), "expected %s, got %s\n", pszProvName
, provider
);
221 ok(result
&& cbName
==providerLen
, "expected %i, got %i\n",
222 (unsigned int)cbName
, (unsigned int)providerLen
);
228 test_acquire_context();
229 clean_up_environment();
231 test_enum_providers();