1c2c66affSColin Finck /*
2c2c66affSColin Finck * Copyright 2006 Juan Lang
3c2c66affSColin Finck *
4c2c66affSColin Finck * This library is free software; you can redistribute it and/or
5c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
6c2c66affSColin Finck * License as published by the Free Software Foundation; either
7c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
8c2c66affSColin Finck *
9c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
10c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12c2c66affSColin Finck * Lesser General Public License for more details.
13c2c66affSColin Finck *
14c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
15c2c66affSColin Finck * License along with this library; if not, write to the Free Software
16c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17c2c66affSColin Finck *
18c2c66affSColin Finck */
19a6e10342SAmine Khaldi #include <stdarg.h>
20a6e10342SAmine Khaldi #define NONAMELESSUNION
21a6e10342SAmine Khaldi #include "windef.h"
22a6e10342SAmine Khaldi #include "winbase.h"
23a6e10342SAmine Khaldi #define CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
24a6e10342SAmine Khaldi #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
25a6e10342SAmine Khaldi #include "wincrypt.h"
26a6e10342SAmine Khaldi #include "wininet.h"
27a6e10342SAmine Khaldi #include "wine/debug.h"
28a6e10342SAmine Khaldi #include "wine/unicode.h"
29c2c66affSColin Finck #include "crypt32_private.h"
30c2c66affSColin Finck
31c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(chain);
33c2c66affSColin Finck
34c2c66affSColin Finck #define DEFAULT_CYCLE_MODULUS 7
35c2c66affSColin Finck
36c2c66affSColin Finck /* This represents a subset of a certificate chain engine: it doesn't include
37c2c66affSColin Finck * the "hOther" store described by MSDN, because I'm not sure how that's used.
38c2c66affSColin Finck * It also doesn't include the "hTrust" store, because I don't yet implement
39c2c66affSColin Finck * CTLs or complex certificate chains.
40c2c66affSColin Finck */
41c2c66affSColin Finck typedef struct _CertificateChainEngine
42c2c66affSColin Finck {
43c2c66affSColin Finck LONG ref;
44c2c66affSColin Finck HCERTSTORE hRoot;
45c2c66affSColin Finck HCERTSTORE hWorld;
46c2c66affSColin Finck DWORD dwFlags;
47c2c66affSColin Finck DWORD dwUrlRetrievalTimeout;
48c2c66affSColin Finck DWORD MaximumCachedCertificates;
49c2c66affSColin Finck DWORD CycleDetectionModulus;
50c2c66affSColin Finck } CertificateChainEngine;
51c2c66affSColin Finck
CRYPT_AddStoresToCollection(HCERTSTORE collection,DWORD cStores,HCERTSTORE * stores)52c2c66affSColin Finck static inline void CRYPT_AddStoresToCollection(HCERTSTORE collection,
53c2c66affSColin Finck DWORD cStores, HCERTSTORE *stores)
54c2c66affSColin Finck {
55c2c66affSColin Finck DWORD i;
56c2c66affSColin Finck
57c2c66affSColin Finck for (i = 0; i < cStores; i++)
58c2c66affSColin Finck CertAddStoreToCollection(collection, stores[i], 0, 0);
59c2c66affSColin Finck }
60c2c66affSColin Finck
CRYPT_CloseStores(DWORD cStores,HCERTSTORE * stores)61c2c66affSColin Finck static inline void CRYPT_CloseStores(DWORD cStores, HCERTSTORE *stores)
62c2c66affSColin Finck {
63c2c66affSColin Finck DWORD i;
64c2c66affSColin Finck
65c2c66affSColin Finck for (i = 0; i < cStores; i++)
66c2c66affSColin Finck CertCloseStore(stores[i], 0);
67c2c66affSColin Finck }
68c2c66affSColin Finck
69c2c66affSColin Finck static const WCHAR rootW[] = { 'R','o','o','t',0 };
70c2c66affSColin Finck
71c2c66affSColin Finck /* Finds cert in store by comparing the cert's hashes. */
CRYPT_FindCertInStore(HCERTSTORE store,PCCERT_CONTEXT cert)72c2c66affSColin Finck static PCCERT_CONTEXT CRYPT_FindCertInStore(HCERTSTORE store,
73c2c66affSColin Finck PCCERT_CONTEXT cert)
74c2c66affSColin Finck {
75c2c66affSColin Finck PCCERT_CONTEXT matching = NULL;
76c2c66affSColin Finck BYTE hash[20];
77c2c66affSColin Finck DWORD size = sizeof(hash);
78c2c66affSColin Finck
79c2c66affSColin Finck if (CertGetCertificateContextProperty(cert, CERT_HASH_PROP_ID, hash, &size))
80c2c66affSColin Finck {
81c2c66affSColin Finck CRYPT_HASH_BLOB blob = { sizeof(hash), hash };
82c2c66affSColin Finck
83c2c66affSColin Finck matching = CertFindCertificateInStore(store, cert->dwCertEncodingType,
84c2c66affSColin Finck 0, CERT_FIND_SHA1_HASH, &blob, NULL);
85c2c66affSColin Finck }
86c2c66affSColin Finck return matching;
87c2c66affSColin Finck }
88c2c66affSColin Finck
CRYPT_CheckRestrictedRoot(HCERTSTORE store)89c2c66affSColin Finck static BOOL CRYPT_CheckRestrictedRoot(HCERTSTORE store)
90c2c66affSColin Finck {
91c2c66affSColin Finck BOOL ret = TRUE;
92c2c66affSColin Finck
93c2c66affSColin Finck if (store)
94c2c66affSColin Finck {
95c2c66affSColin Finck HCERTSTORE rootStore = CertOpenSystemStoreW(0, rootW);
96c2c66affSColin Finck PCCERT_CONTEXT cert = NULL, check;
97c2c66affSColin Finck
98c2c66affSColin Finck do {
99c2c66affSColin Finck cert = CertEnumCertificatesInStore(store, cert);
100c2c66affSColin Finck if (cert)
101c2c66affSColin Finck {
102c2c66affSColin Finck if (!(check = CRYPT_FindCertInStore(rootStore, cert)))
103c2c66affSColin Finck ret = FALSE;
104c2c66affSColin Finck else
105c2c66affSColin Finck CertFreeCertificateContext(check);
106c2c66affSColin Finck }
107c2c66affSColin Finck } while (ret && cert);
108c2c66affSColin Finck if (cert)
109c2c66affSColin Finck CertFreeCertificateContext(cert);
110c2c66affSColin Finck CertCloseStore(rootStore, 0);
111c2c66affSColin Finck }
112c2c66affSColin Finck return ret;
113c2c66affSColin Finck }
114c2c66affSColin Finck
CRYPT_CreateChainEngine(HCERTSTORE root,DWORD system_store,const CERT_CHAIN_ENGINE_CONFIG * config)115c2c66affSColin Finck HCERTCHAINENGINE CRYPT_CreateChainEngine(HCERTSTORE root, DWORD system_store, const CERT_CHAIN_ENGINE_CONFIG *config)
116c2c66affSColin Finck {
117c2c66affSColin Finck CertificateChainEngine *engine;
118c2c66affSColin Finck HCERTSTORE worldStores[4];
119c2c66affSColin Finck
120c2c66affSColin Finck static const WCHAR caW[] = { 'C','A',0 };
121c2c66affSColin Finck static const WCHAR myW[] = { 'M','y',0 };
122c2c66affSColin Finck static const WCHAR trustW[] = { 'T','r','u','s','t',0 };
123c2c66affSColin Finck
124c2c66affSColin Finck if(!root) {
125c2c66affSColin Finck if(config->cbSize >= sizeof(CERT_CHAIN_ENGINE_CONFIG) && config->hExclusiveRoot)
126c2c66affSColin Finck root = CertDuplicateStore(config->hExclusiveRoot);
127c2c66affSColin Finck else if (config->hRestrictedRoot)
128c2c66affSColin Finck root = CertDuplicateStore(config->hRestrictedRoot);
129c2c66affSColin Finck else
130c2c66affSColin Finck root = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, system_store, rootW);
131c2c66affSColin Finck if(!root)
132c2c66affSColin Finck return NULL;
133c2c66affSColin Finck }
134c2c66affSColin Finck
135c2c66affSColin Finck engine = CryptMemAlloc(sizeof(CertificateChainEngine));
136c2c66affSColin Finck if(!engine) {
137c2c66affSColin Finck CertCloseStore(root, 0);
138c2c66affSColin Finck return NULL;
139c2c66affSColin Finck }
140c2c66affSColin Finck
141c2c66affSColin Finck engine->ref = 1;
142c2c66affSColin Finck engine->hRoot = root;
143c2c66affSColin Finck engine->hWorld = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
144c2c66affSColin Finck worldStores[0] = CertDuplicateStore(engine->hRoot);
145c2c66affSColin Finck worldStores[1] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, system_store, caW);
146c2c66affSColin Finck worldStores[2] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, system_store, myW);
147c2c66affSColin Finck worldStores[3] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, system_store, trustW);
148c2c66affSColin Finck
149*997d44c9SAmine Khaldi CRYPT_AddStoresToCollection(engine->hWorld, ARRAY_SIZE(worldStores), worldStores);
150c2c66affSColin Finck CRYPT_AddStoresToCollection(engine->hWorld, config->cAdditionalStore, config->rghAdditionalStore);
151*997d44c9SAmine Khaldi CRYPT_CloseStores(ARRAY_SIZE(worldStores), worldStores);
152c2c66affSColin Finck
153c2c66affSColin Finck engine->dwFlags = config->dwFlags;
154c2c66affSColin Finck engine->dwUrlRetrievalTimeout = config->dwUrlRetrievalTimeout;
155c2c66affSColin Finck engine->MaximumCachedCertificates = config->MaximumCachedCertificates;
156c2c66affSColin Finck if(config->CycleDetectionModulus)
157c2c66affSColin Finck engine->CycleDetectionModulus = config->CycleDetectionModulus;
158c2c66affSColin Finck else
159c2c66affSColin Finck engine->CycleDetectionModulus = DEFAULT_CYCLE_MODULUS;
160c2c66affSColin Finck
161c2c66affSColin Finck return engine;
162c2c66affSColin Finck }
163c2c66affSColin Finck
164c2c66affSColin Finck static CertificateChainEngine *default_cu_engine, *default_lm_engine;
165c2c66affSColin Finck
get_chain_engine(HCERTCHAINENGINE handle,BOOL allow_default)166c2c66affSColin Finck static CertificateChainEngine *get_chain_engine(HCERTCHAINENGINE handle, BOOL allow_default)
167c2c66affSColin Finck {
168c2c66affSColin Finck const CERT_CHAIN_ENGINE_CONFIG config = { sizeof(config) };
169c2c66affSColin Finck
170c2c66affSColin Finck if(handle == HCCE_CURRENT_USER) {
171c2c66affSColin Finck if(!allow_default)
172c2c66affSColin Finck return NULL;
173c2c66affSColin Finck
174c2c66affSColin Finck if(!default_cu_engine) {
175c2c66affSColin Finck handle = CRYPT_CreateChainEngine(NULL, CERT_SYSTEM_STORE_CURRENT_USER, &config);
176c2c66affSColin Finck InterlockedCompareExchangePointer((void**)&default_cu_engine, handle, NULL);
177c2c66affSColin Finck if(default_cu_engine != handle)
178c2c66affSColin Finck CertFreeCertificateChainEngine(handle);
179c2c66affSColin Finck }
180c2c66affSColin Finck
181c2c66affSColin Finck return default_cu_engine;
182c2c66affSColin Finck }
183c2c66affSColin Finck
184c2c66affSColin Finck if(handle == HCCE_LOCAL_MACHINE) {
185c2c66affSColin Finck if(!allow_default)
186c2c66affSColin Finck return NULL;
187c2c66affSColin Finck
188c2c66affSColin Finck if(!default_lm_engine) {
189c2c66affSColin Finck handle = CRYPT_CreateChainEngine(NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, &config);
190c2c66affSColin Finck InterlockedCompareExchangePointer((void**)&default_lm_engine, handle, NULL);
191c2c66affSColin Finck if(default_lm_engine != handle)
192c2c66affSColin Finck CertFreeCertificateChainEngine(handle);
193c2c66affSColin Finck }
194c2c66affSColin Finck
195c2c66affSColin Finck return default_lm_engine;
196c2c66affSColin Finck }
197c2c66affSColin Finck
198c2c66affSColin Finck return (CertificateChainEngine*)handle;
199c2c66affSColin Finck }
200c2c66affSColin Finck
free_chain_engine(CertificateChainEngine * engine)201c2c66affSColin Finck static void free_chain_engine(CertificateChainEngine *engine)
202c2c66affSColin Finck {
203c2c66affSColin Finck if(!engine || InterlockedDecrement(&engine->ref))
204c2c66affSColin Finck return;
205c2c66affSColin Finck
206c2c66affSColin Finck CertCloseStore(engine->hWorld, 0);
207c2c66affSColin Finck CertCloseStore(engine->hRoot, 0);
208c2c66affSColin Finck CryptMemFree(engine);
209c2c66affSColin Finck }
210c2c66affSColin Finck
211c2c66affSColin Finck typedef struct _CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT
212c2c66affSColin Finck {
213c2c66affSColin Finck DWORD cbSize;
214c2c66affSColin Finck HCERTSTORE hRestrictedRoot;
215c2c66affSColin Finck HCERTSTORE hRestrictedTrust;
216c2c66affSColin Finck HCERTSTORE hRestrictedOther;
217c2c66affSColin Finck DWORD cAdditionalStore;
218c2c66affSColin Finck HCERTSTORE *rghAdditionalStore;
219c2c66affSColin Finck DWORD dwFlags;
220c2c66affSColin Finck DWORD dwUrlRetrievalTimeout;
221c2c66affSColin Finck DWORD MaximumCachedCertificates;
222c2c66affSColin Finck DWORD CycleDetectionModulus;
223c2c66affSColin Finck } CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT;
224c2c66affSColin Finck
CertCreateCertificateChainEngine(PCERT_CHAIN_ENGINE_CONFIG pConfig,HCERTCHAINENGINE * phChainEngine)225c2c66affSColin Finck BOOL WINAPI CertCreateCertificateChainEngine(PCERT_CHAIN_ENGINE_CONFIG pConfig,
226c2c66affSColin Finck HCERTCHAINENGINE *phChainEngine)
227c2c66affSColin Finck {
228c2c66affSColin Finck BOOL ret;
229c2c66affSColin Finck
230c2c66affSColin Finck TRACE("(%p, %p)\n", pConfig, phChainEngine);
231c2c66affSColin Finck
232c2c66affSColin Finck if (pConfig->cbSize != sizeof(CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT)
233c2c66affSColin Finck && pConfig->cbSize != sizeof(CERT_CHAIN_ENGINE_CONFIG))
234c2c66affSColin Finck {
235c2c66affSColin Finck SetLastError(E_INVALIDARG);
236c2c66affSColin Finck return FALSE;
237c2c66affSColin Finck }
238c2c66affSColin Finck ret = CRYPT_CheckRestrictedRoot(pConfig->hRestrictedRoot);
239c2c66affSColin Finck if (!ret)
240c2c66affSColin Finck {
241c2c66affSColin Finck *phChainEngine = NULL;
242c2c66affSColin Finck return FALSE;
243c2c66affSColin Finck }
244c2c66affSColin Finck
245c2c66affSColin Finck *phChainEngine = CRYPT_CreateChainEngine(NULL, CERT_SYSTEM_STORE_CURRENT_USER, pConfig);
246c2c66affSColin Finck return *phChainEngine != NULL;
247c2c66affSColin Finck }
248c2c66affSColin Finck
CertFreeCertificateChainEngine(HCERTCHAINENGINE hChainEngine)249c2c66affSColin Finck void WINAPI CertFreeCertificateChainEngine(HCERTCHAINENGINE hChainEngine)
250c2c66affSColin Finck {
251c2c66affSColin Finck TRACE("(%p)\n", hChainEngine);
252c2c66affSColin Finck free_chain_engine(get_chain_engine(hChainEngine, FALSE));
253c2c66affSColin Finck }
254c2c66affSColin Finck
default_chain_engine_free(void)255c2c66affSColin Finck void default_chain_engine_free(void)
256c2c66affSColin Finck {
257c2c66affSColin Finck free_chain_engine(default_cu_engine);
258c2c66affSColin Finck free_chain_engine(default_lm_engine);
259c2c66affSColin Finck }
260c2c66affSColin Finck
261c2c66affSColin Finck typedef struct _CertificateChain
262c2c66affSColin Finck {
263c2c66affSColin Finck CERT_CHAIN_CONTEXT context;
264c2c66affSColin Finck HCERTSTORE world;
265c2c66affSColin Finck LONG ref;
266c2c66affSColin Finck } CertificateChain;
267c2c66affSColin Finck
CRYPT_IsCertificateSelfSigned(const CERT_CONTEXT * cert)268*997d44c9SAmine Khaldi DWORD CRYPT_IsCertificateSelfSigned(const CERT_CONTEXT *cert)
269c2c66affSColin Finck {
270*997d44c9SAmine Khaldi DWORD size, status = 0;
271c2c66affSColin Finck PCERT_EXTENSION ext;
272c2c66affSColin Finck BOOL ret;
273c2c66affSColin Finck
274c2c66affSColin Finck if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
275c2c66affSColin Finck cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
276c2c66affSColin Finck {
277c2c66affSColin Finck CERT_AUTHORITY_KEY_ID2_INFO *info;
278c2c66affSColin Finck
279c2c66affSColin Finck ret = CryptDecodeObjectEx(cert->dwCertEncodingType,
280c2c66affSColin Finck X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
281c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
282c2c66affSColin Finck &info, &size);
283c2c66affSColin Finck if (ret)
284c2c66affSColin Finck {
285c2c66affSColin Finck if (info->AuthorityCertIssuer.cAltEntry &&
286c2c66affSColin Finck info->AuthorityCertSerialNumber.cbData)
287c2c66affSColin Finck {
288c2c66affSColin Finck PCERT_ALT_NAME_ENTRY directoryName = NULL;
289c2c66affSColin Finck DWORD i;
290c2c66affSColin Finck
291c2c66affSColin Finck for (i = 0; !directoryName &&
292c2c66affSColin Finck i < info->AuthorityCertIssuer.cAltEntry; i++)
293c2c66affSColin Finck if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
294c2c66affSColin Finck == CERT_ALT_NAME_DIRECTORY_NAME)
295c2c66affSColin Finck directoryName =
296c2c66affSColin Finck &info->AuthorityCertIssuer.rgAltEntry[i];
297c2c66affSColin Finck if (directoryName)
298c2c66affSColin Finck {
299*997d44c9SAmine Khaldi if (CertCompareCertificateName(cert->dwCertEncodingType, &directoryName->u.DirectoryName, &cert->pCertInfo->Issuer)
300*997d44c9SAmine Khaldi && CertCompareIntegerBlob(&info->AuthorityCertSerialNumber, &cert->pCertInfo->SerialNumber))
301*997d44c9SAmine Khaldi status = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
302c2c66affSColin Finck }
303c2c66affSColin Finck else
304c2c66affSColin Finck {
305c2c66affSColin Finck FIXME("no supported name type in authority key id2\n");
306c2c66affSColin Finck ret = FALSE;
307c2c66affSColin Finck }
308c2c66affSColin Finck }
309c2c66affSColin Finck else if (info->KeyId.cbData)
310c2c66affSColin Finck {
311c2c66affSColin Finck ret = CertGetCertificateContextProperty(cert,
312c2c66affSColin Finck CERT_KEY_IDENTIFIER_PROP_ID, NULL, &size);
313c2c66affSColin Finck if (ret && size == info->KeyId.cbData)
314c2c66affSColin Finck {
315c2c66affSColin Finck LPBYTE buf = CryptMemAlloc(size);
316c2c66affSColin Finck
317c2c66affSColin Finck if (buf)
318c2c66affSColin Finck {
319*997d44c9SAmine Khaldi CertGetCertificateContextProperty(cert, CERT_KEY_IDENTIFIER_PROP_ID, buf, &size);
320*997d44c9SAmine Khaldi if (!memcmp(buf, info->KeyId.pbData, size))
321*997d44c9SAmine Khaldi status = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
322c2c66affSColin Finck CryptMemFree(buf);
323c2c66affSColin Finck }
324c2c66affSColin Finck }
325c2c66affSColin Finck }
326c2c66affSColin Finck LocalFree(info);
327c2c66affSColin Finck }
328c2c66affSColin Finck }
329c2c66affSColin Finck else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
330c2c66affSColin Finck cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
331c2c66affSColin Finck {
332c2c66affSColin Finck CERT_AUTHORITY_KEY_ID_INFO *info;
333c2c66affSColin Finck
334c2c66affSColin Finck ret = CryptDecodeObjectEx(cert->dwCertEncodingType,
335c2c66affSColin Finck X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
336c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
337c2c66affSColin Finck &info, &size);
338c2c66affSColin Finck if (ret)
339c2c66affSColin Finck {
340c2c66affSColin Finck if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
341c2c66affSColin Finck {
342*997d44c9SAmine Khaldi if (CertCompareCertificateName(cert->dwCertEncodingType, &info->CertIssuer, &cert->pCertInfo->Issuer)
343*997d44c9SAmine Khaldi && CertCompareIntegerBlob(&info->CertSerialNumber, &cert->pCertInfo->SerialNumber))
344*997d44c9SAmine Khaldi status = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
345c2c66affSColin Finck }
346c2c66affSColin Finck else if (info->KeyId.cbData)
347c2c66affSColin Finck {
348c2c66affSColin Finck ret = CertGetCertificateContextProperty(cert,
349c2c66affSColin Finck CERT_KEY_IDENTIFIER_PROP_ID, NULL, &size);
350c2c66affSColin Finck if (ret && size == info->KeyId.cbData)
351c2c66affSColin Finck {
352c2c66affSColin Finck LPBYTE buf = CryptMemAlloc(size);
353c2c66affSColin Finck
354c2c66affSColin Finck if (buf)
355c2c66affSColin Finck {
356c2c66affSColin Finck CertGetCertificateContextProperty(cert,
357c2c66affSColin Finck CERT_KEY_IDENTIFIER_PROP_ID, buf, &size);
358*997d44c9SAmine Khaldi if (!memcmp(buf, info->KeyId.pbData, size))
359*997d44c9SAmine Khaldi status = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
360c2c66affSColin Finck CryptMemFree(buf);
361c2c66affSColin Finck }
362c2c66affSColin Finck }
363c2c66affSColin Finck }
364c2c66affSColin Finck LocalFree(info);
365c2c66affSColin Finck }
366c2c66affSColin Finck }
367c2c66affSColin Finck else
368*997d44c9SAmine Khaldi if (CertCompareCertificateName(cert->dwCertEncodingType, &cert->pCertInfo->Subject, &cert->pCertInfo->Issuer))
369*997d44c9SAmine Khaldi status = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
370*997d44c9SAmine Khaldi
371*997d44c9SAmine Khaldi if (status)
372*997d44c9SAmine Khaldi status |= CERT_TRUST_IS_SELF_SIGNED;
373*997d44c9SAmine Khaldi
374*997d44c9SAmine Khaldi return status;
375c2c66affSColin Finck }
376c2c66affSColin Finck
CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element)377c2c66affSColin Finck static void CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element)
378c2c66affSColin Finck {
379c2c66affSColin Finck CertFreeCertificateContext(element->pCertContext);
380c2c66affSColin Finck CryptMemFree(element);
381c2c66affSColin Finck }
382c2c66affSColin Finck
CRYPT_CheckSimpleChainForCycles(PCERT_SIMPLE_CHAIN chain)383c2c66affSColin Finck static void CRYPT_CheckSimpleChainForCycles(PCERT_SIMPLE_CHAIN chain)
384c2c66affSColin Finck {
385c2c66affSColin Finck DWORD i, j, cyclicCertIndex = 0;
386c2c66affSColin Finck
387c2c66affSColin Finck /* O(n^2) - I don't think there's a faster way */
388c2c66affSColin Finck for (i = 0; !cyclicCertIndex && i < chain->cElement; i++)
389c2c66affSColin Finck for (j = i + 1; !cyclicCertIndex && j < chain->cElement; j++)
390c2c66affSColin Finck if (CertCompareCertificate(X509_ASN_ENCODING,
391c2c66affSColin Finck chain->rgpElement[i]->pCertContext->pCertInfo,
392c2c66affSColin Finck chain->rgpElement[j]->pCertContext->pCertInfo))
393c2c66affSColin Finck cyclicCertIndex = j;
394c2c66affSColin Finck if (cyclicCertIndex)
395c2c66affSColin Finck {
396c2c66affSColin Finck chain->rgpElement[cyclicCertIndex]->TrustStatus.dwErrorStatus
397c2c66affSColin Finck |= CERT_TRUST_IS_CYCLIC | CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
398c2c66affSColin Finck /* Release remaining certs */
399c2c66affSColin Finck for (i = cyclicCertIndex + 1; i < chain->cElement; i++)
400c2c66affSColin Finck CRYPT_FreeChainElement(chain->rgpElement[i]);
401c2c66affSColin Finck /* Truncate chain */
402c2c66affSColin Finck chain->cElement = cyclicCertIndex + 1;
403c2c66affSColin Finck }
404c2c66affSColin Finck }
405c2c66affSColin Finck
406c2c66affSColin Finck /* Checks whether the chain is cyclic by examining the last element's status */
CRYPT_IsSimpleChainCyclic(const CERT_SIMPLE_CHAIN * chain)407c2c66affSColin Finck static inline BOOL CRYPT_IsSimpleChainCyclic(const CERT_SIMPLE_CHAIN *chain)
408c2c66affSColin Finck {
409c2c66affSColin Finck if (chain->cElement)
410c2c66affSColin Finck return chain->rgpElement[chain->cElement - 1]->TrustStatus.dwErrorStatus
411c2c66affSColin Finck & CERT_TRUST_IS_CYCLIC;
412c2c66affSColin Finck else
413c2c66affSColin Finck return FALSE;
414c2c66affSColin Finck }
415c2c66affSColin Finck
CRYPT_CombineTrustStatus(CERT_TRUST_STATUS * chainStatus,const CERT_TRUST_STATUS * elementStatus)416c2c66affSColin Finck static inline void CRYPT_CombineTrustStatus(CERT_TRUST_STATUS *chainStatus,
417c2c66affSColin Finck const CERT_TRUST_STATUS *elementStatus)
418c2c66affSColin Finck {
419c2c66affSColin Finck /* Any error that applies to an element also applies to a chain.. */
420c2c66affSColin Finck chainStatus->dwErrorStatus |= elementStatus->dwErrorStatus;
421c2c66affSColin Finck /* but the bottom nibble of an element's info status doesn't apply to the
422c2c66affSColin Finck * chain.
423c2c66affSColin Finck */
424c2c66affSColin Finck chainStatus->dwInfoStatus |= (elementStatus->dwInfoStatus & 0xfffffff0);
425c2c66affSColin Finck }
426c2c66affSColin Finck
CRYPT_AddCertToSimpleChain(const CertificateChainEngine * engine,PCERT_SIMPLE_CHAIN chain,PCCERT_CONTEXT cert,DWORD subjectInfoStatus)427c2c66affSColin Finck static BOOL CRYPT_AddCertToSimpleChain(const CertificateChainEngine *engine,
428c2c66affSColin Finck PCERT_SIMPLE_CHAIN chain, PCCERT_CONTEXT cert, DWORD subjectInfoStatus)
429c2c66affSColin Finck {
430c2c66affSColin Finck BOOL ret = FALSE;
431c2c66affSColin Finck PCERT_CHAIN_ELEMENT element = CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT));
432c2c66affSColin Finck
433c2c66affSColin Finck if (element)
434c2c66affSColin Finck {
435c2c66affSColin Finck if (!chain->cElement)
436c2c66affSColin Finck chain->rgpElement = CryptMemAlloc(sizeof(PCERT_CHAIN_ELEMENT));
437c2c66affSColin Finck else
438c2c66affSColin Finck chain->rgpElement = CryptMemRealloc(chain->rgpElement,
439c2c66affSColin Finck (chain->cElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
440c2c66affSColin Finck if (chain->rgpElement)
441c2c66affSColin Finck {
442c2c66affSColin Finck chain->rgpElement[chain->cElement++] = element;
443c2c66affSColin Finck memset(element, 0, sizeof(CERT_CHAIN_ELEMENT));
444c2c66affSColin Finck element->cbSize = sizeof(CERT_CHAIN_ELEMENT);
445c2c66affSColin Finck element->pCertContext = CertDuplicateCertificateContext(cert);
446c2c66affSColin Finck if (chain->cElement > 1)
447c2c66affSColin Finck chain->rgpElement[chain->cElement - 2]->TrustStatus.dwInfoStatus
448c2c66affSColin Finck = subjectInfoStatus;
449c2c66affSColin Finck /* FIXME: initialize the rest of element */
450c2c66affSColin Finck if (!(chain->cElement % engine->CycleDetectionModulus))
451c2c66affSColin Finck {
452c2c66affSColin Finck CRYPT_CheckSimpleChainForCycles(chain);
453c2c66affSColin Finck /* Reinitialize the element pointer in case the chain is
454c2c66affSColin Finck * cyclic, in which case the chain is truncated.
455c2c66affSColin Finck */
456c2c66affSColin Finck element = chain->rgpElement[chain->cElement - 1];
457c2c66affSColin Finck }
458c2c66affSColin Finck CRYPT_CombineTrustStatus(&chain->TrustStatus,
459c2c66affSColin Finck &element->TrustStatus);
460c2c66affSColin Finck ret = TRUE;
461c2c66affSColin Finck }
462c2c66affSColin Finck else
463c2c66affSColin Finck CryptMemFree(element);
464c2c66affSColin Finck }
465c2c66affSColin Finck return ret;
466c2c66affSColin Finck }
467c2c66affSColin Finck
CRYPT_FreeSimpleChain(PCERT_SIMPLE_CHAIN chain)468c2c66affSColin Finck static void CRYPT_FreeSimpleChain(PCERT_SIMPLE_CHAIN chain)
469c2c66affSColin Finck {
470c2c66affSColin Finck DWORD i;
471c2c66affSColin Finck
472c2c66affSColin Finck for (i = 0; i < chain->cElement; i++)
473c2c66affSColin Finck CRYPT_FreeChainElement(chain->rgpElement[i]);
474c2c66affSColin Finck CryptMemFree(chain->rgpElement);
475c2c66affSColin Finck CryptMemFree(chain);
476c2c66affSColin Finck }
477c2c66affSColin Finck
CRYPT_CheckTrustedStatus(HCERTSTORE hRoot,PCERT_CHAIN_ELEMENT rootElement)478c2c66affSColin Finck static void CRYPT_CheckTrustedStatus(HCERTSTORE hRoot,
479c2c66affSColin Finck PCERT_CHAIN_ELEMENT rootElement)
480c2c66affSColin Finck {
481c2c66affSColin Finck PCCERT_CONTEXT trustedRoot = CRYPT_FindCertInStore(hRoot,
482c2c66affSColin Finck rootElement->pCertContext);
483c2c66affSColin Finck
484c2c66affSColin Finck if (!trustedRoot)
485c2c66affSColin Finck rootElement->TrustStatus.dwErrorStatus |=
486c2c66affSColin Finck CERT_TRUST_IS_UNTRUSTED_ROOT;
487c2c66affSColin Finck else
488c2c66affSColin Finck CertFreeCertificateContext(trustedRoot);
489c2c66affSColin Finck }
490c2c66affSColin Finck
CRYPT_CheckRootCert(HCERTSTORE hRoot,PCERT_CHAIN_ELEMENT rootElement)491c2c66affSColin Finck static void CRYPT_CheckRootCert(HCERTSTORE hRoot,
492c2c66affSColin Finck PCERT_CHAIN_ELEMENT rootElement)
493c2c66affSColin Finck {
494c2c66affSColin Finck PCCERT_CONTEXT root = rootElement->pCertContext;
495c2c66affSColin Finck
496c2c66affSColin Finck if (!CryptVerifyCertificateSignatureEx(0, root->dwCertEncodingType,
497c2c66affSColin Finck CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, (void *)root,
498c2c66affSColin Finck CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, (void *)root, 0, NULL))
499c2c66affSColin Finck {
500c2c66affSColin Finck TRACE_(chain)("Last certificate's signature is invalid\n");
501c2c66affSColin Finck rootElement->TrustStatus.dwErrorStatus |=
502c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID;
503c2c66affSColin Finck }
504c2c66affSColin Finck CRYPT_CheckTrustedStatus(hRoot, rootElement);
505c2c66affSColin Finck }
506c2c66affSColin Finck
507c2c66affSColin Finck /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
508c2c66affSColin Finck * or szOID_BASIC_CONSTRAINTS2, whichever is present) into a
509c2c66affSColin Finck * CERT_BASIC_CONSTRAINTS2_INFO. If it neither extension is present, sets
510c2c66affSColin Finck * constraints->fCA to defaultIfNotSpecified.
511c2c66affSColin Finck * Returns FALSE if the extension is present but couldn't be decoded.
512c2c66affSColin Finck */
CRYPT_DecodeBasicConstraints(PCCERT_CONTEXT cert,CERT_BASIC_CONSTRAINTS2_INFO * constraints,BOOL defaultIfNotSpecified)513c2c66affSColin Finck static BOOL CRYPT_DecodeBasicConstraints(PCCERT_CONTEXT cert,
514c2c66affSColin Finck CERT_BASIC_CONSTRAINTS2_INFO *constraints, BOOL defaultIfNotSpecified)
515c2c66affSColin Finck {
516c2c66affSColin Finck BOOL ret = TRUE;
517c2c66affSColin Finck PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
518c2c66affSColin Finck cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
519c2c66affSColin Finck
520c2c66affSColin Finck constraints->fPathLenConstraint = FALSE;
521c2c66affSColin Finck if (ext)
522c2c66affSColin Finck {
523c2c66affSColin Finck CERT_BASIC_CONSTRAINTS_INFO *info;
524c2c66affSColin Finck DWORD size = 0;
525c2c66affSColin Finck
526c2c66affSColin Finck ret = CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
527c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
528c2c66affSColin Finck NULL, &info, &size);
529c2c66affSColin Finck if (ret)
530c2c66affSColin Finck {
531c2c66affSColin Finck if (info->SubjectType.cbData == 1)
532c2c66affSColin Finck constraints->fCA =
533c2c66affSColin Finck info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
534c2c66affSColin Finck LocalFree(info);
535c2c66affSColin Finck }
536c2c66affSColin Finck }
537c2c66affSColin Finck else
538c2c66affSColin Finck {
539c2c66affSColin Finck ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
540c2c66affSColin Finck cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
541c2c66affSColin Finck if (ext)
542c2c66affSColin Finck {
543c2c66affSColin Finck DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
544c2c66affSColin Finck
545c2c66affSColin Finck ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
546c2c66affSColin Finck szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
547c2c66affSColin Finck 0, NULL, constraints, &size);
548c2c66affSColin Finck }
549c2c66affSColin Finck else
550c2c66affSColin Finck constraints->fCA = defaultIfNotSpecified;
551c2c66affSColin Finck }
552c2c66affSColin Finck return ret;
553c2c66affSColin Finck }
554c2c66affSColin Finck
555c2c66affSColin Finck /* Checks element's basic constraints to see if it can act as a CA, with
556c2c66affSColin Finck * remainingCAs CAs left in this chain. In general, a cert must include the
557c2c66affSColin Finck * basic constraints extension, with the CA flag asserted, in order to be
558c2c66affSColin Finck * allowed to be a CA. A V1 or V2 cert, which has no extensions, is also
559c2c66affSColin Finck * allowed to be a CA if it's installed locally (in the engine's world store.)
560c2c66affSColin Finck * This matches the expected usage in RFC 5280, section 4.2.1.9: a conforming
561c2c66affSColin Finck * CA MUST include the basic constraints extension in all certificates that are
562c2c66affSColin Finck * used to validate digital signatures on certificates. It also matches
563c2c66affSColin Finck * section 6.1.4(k): "If a certificate is a v1 or v2 certificate, then the
564c2c66affSColin Finck * application MUST either verify that the certificate is a CA certificate
565c2c66affSColin Finck * through out-of-band means or reject the certificate." Rejecting the
566c2c66affSColin Finck * certificate prohibits a large number of commonly used certificates, so
567c2c66affSColin Finck * accepting locally installed ones is a compromise.
568c2c66affSColin Finck * Root certificates are also allowed to be CAs even without a basic
569c2c66affSColin Finck * constraints extension. This is implied by RFC 5280, section 6.1: the
570c2c66affSColin Finck * root of a certificate chain's only requirement is that it was used to issue
571c2c66affSColin Finck * the next certificate in the chain.
572c2c66affSColin Finck * Updates chainConstraints with the element's constraints, if:
573c2c66affSColin Finck * 1. chainConstraints doesn't have a path length constraint, or
574c2c66affSColin Finck * 2. element's path length constraint is smaller than chainConstraints's
575c2c66affSColin Finck * Sets *pathLengthConstraintViolated to TRUE if a path length violation
576c2c66affSColin Finck * occurs.
577c2c66affSColin Finck * Returns TRUE if the element can be a CA, and the length of the remaining
578c2c66affSColin Finck * chain is valid.
579c2c66affSColin Finck */
CRYPT_CheckBasicConstraintsForCA(CertificateChainEngine * engine,PCCERT_CONTEXT cert,CERT_BASIC_CONSTRAINTS2_INFO * chainConstraints,DWORD remainingCAs,BOOL isRoot,BOOL * pathLengthConstraintViolated)580c2c66affSColin Finck static BOOL CRYPT_CheckBasicConstraintsForCA(CertificateChainEngine *engine,
581c2c66affSColin Finck PCCERT_CONTEXT cert, CERT_BASIC_CONSTRAINTS2_INFO *chainConstraints,
582c2c66affSColin Finck DWORD remainingCAs, BOOL isRoot, BOOL *pathLengthConstraintViolated)
583c2c66affSColin Finck {
584c2c66affSColin Finck BOOL validBasicConstraints, implicitCA = FALSE;
585c2c66affSColin Finck CERT_BASIC_CONSTRAINTS2_INFO constraints;
586c2c66affSColin Finck
587c2c66affSColin Finck if (isRoot)
588c2c66affSColin Finck implicitCA = TRUE;
589c2c66affSColin Finck else if (cert->pCertInfo->dwVersion == CERT_V1 ||
590c2c66affSColin Finck cert->pCertInfo->dwVersion == CERT_V2)
591c2c66affSColin Finck {
592c2c66affSColin Finck BYTE hash[20];
593c2c66affSColin Finck DWORD size = sizeof(hash);
594c2c66affSColin Finck
595c2c66affSColin Finck if (CertGetCertificateContextProperty(cert, CERT_HASH_PROP_ID,
596c2c66affSColin Finck hash, &size))
597c2c66affSColin Finck {
598c2c66affSColin Finck CRYPT_HASH_BLOB blob = { sizeof(hash), hash };
599c2c66affSColin Finck PCCERT_CONTEXT localCert = CertFindCertificateInStore(
600c2c66affSColin Finck engine->hWorld, cert->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH,
601c2c66affSColin Finck &blob, NULL);
602c2c66affSColin Finck
603c2c66affSColin Finck if (localCert)
604c2c66affSColin Finck {
605c2c66affSColin Finck CertFreeCertificateContext(localCert);
606c2c66affSColin Finck implicitCA = TRUE;
607c2c66affSColin Finck }
608c2c66affSColin Finck }
609c2c66affSColin Finck }
610c2c66affSColin Finck if ((validBasicConstraints = CRYPT_DecodeBasicConstraints(cert,
611c2c66affSColin Finck &constraints, implicitCA)))
612c2c66affSColin Finck {
613c2c66affSColin Finck chainConstraints->fCA = constraints.fCA;
614c2c66affSColin Finck if (!constraints.fCA)
615c2c66affSColin Finck {
616c2c66affSColin Finck TRACE_(chain)("chain element %d can't be a CA\n", remainingCAs + 1);
617c2c66affSColin Finck validBasicConstraints = FALSE;
618c2c66affSColin Finck }
619c2c66affSColin Finck else if (constraints.fPathLenConstraint)
620c2c66affSColin Finck {
621c2c66affSColin Finck /* If the element has path length constraints, they apply to the
622c2c66affSColin Finck * entire remaining chain.
623c2c66affSColin Finck */
624c2c66affSColin Finck if (!chainConstraints->fPathLenConstraint ||
625c2c66affSColin Finck constraints.dwPathLenConstraint <
626c2c66affSColin Finck chainConstraints->dwPathLenConstraint)
627c2c66affSColin Finck {
628c2c66affSColin Finck TRACE_(chain)("setting path length constraint to %d\n",
629c2c66affSColin Finck chainConstraints->dwPathLenConstraint);
630c2c66affSColin Finck chainConstraints->fPathLenConstraint = TRUE;
631c2c66affSColin Finck chainConstraints->dwPathLenConstraint =
632c2c66affSColin Finck constraints.dwPathLenConstraint;
633c2c66affSColin Finck }
634c2c66affSColin Finck }
635c2c66affSColin Finck }
636c2c66affSColin Finck if (chainConstraints->fPathLenConstraint &&
637c2c66affSColin Finck remainingCAs > chainConstraints->dwPathLenConstraint)
638c2c66affSColin Finck {
639c2c66affSColin Finck TRACE_(chain)("remaining CAs %d exceed max path length %d\n",
640c2c66affSColin Finck remainingCAs, chainConstraints->dwPathLenConstraint);
641c2c66affSColin Finck validBasicConstraints = FALSE;
642c2c66affSColin Finck *pathLengthConstraintViolated = TRUE;
643c2c66affSColin Finck }
644c2c66affSColin Finck return validBasicConstraints;
645c2c66affSColin Finck }
646c2c66affSColin Finck
domain_name_matches(LPCWSTR constraint,LPCWSTR name)647c2c66affSColin Finck static BOOL domain_name_matches(LPCWSTR constraint, LPCWSTR name)
648c2c66affSColin Finck {
649c2c66affSColin Finck BOOL match;
650c2c66affSColin Finck
651c2c66affSColin Finck /* RFC 5280, section 4.2.1.10:
652c2c66affSColin Finck * "For URIs, the constraint applies to the host part of the name...
653c2c66affSColin Finck * When the constraint begins with a period, it MAY be expanded with one
654c2c66affSColin Finck * or more labels. That is, the constraint ".example.com" is satisfied by
655c2c66affSColin Finck * both host.example.com and my.host.example.com. However, the constraint
656c2c66affSColin Finck * ".example.com" is not satisfied by "example.com". When the constraint
657c2c66affSColin Finck * does not begin with a period, it specifies a host."
658c2c66affSColin Finck * and for email addresses,
659c2c66affSColin Finck * "To indicate all Internet mail addresses on a particular host, the
660c2c66affSColin Finck * constraint is specified as the host name. For example, the constraint
661c2c66affSColin Finck * "example.com" is satisfied by any mail address at the host
662c2c66affSColin Finck * "example.com". To specify any address within a domain, the constraint
663c2c66affSColin Finck * is specified with a leading period (as with URIs)."
664c2c66affSColin Finck */
665c2c66affSColin Finck if (constraint[0] == '.')
666c2c66affSColin Finck {
667c2c66affSColin Finck /* Must be strictly greater than, a name can't begin with '.' */
668c2c66affSColin Finck if (lstrlenW(name) > lstrlenW(constraint))
669c2c66affSColin Finck match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
670c2c66affSColin Finck constraint);
671c2c66affSColin Finck else
672c2c66affSColin Finck {
673c2c66affSColin Finck /* name is too short, no match */
674c2c66affSColin Finck match = FALSE;
675c2c66affSColin Finck }
676c2c66affSColin Finck }
677c2c66affSColin Finck else
678c2c66affSColin Finck match = !lstrcmpiW(name, constraint);
679c2c66affSColin Finck return match;
680c2c66affSColin Finck }
681c2c66affSColin Finck
url_matches(LPCWSTR constraint,LPCWSTR name,DWORD * trustErrorStatus)682c2c66affSColin Finck static BOOL url_matches(LPCWSTR constraint, LPCWSTR name,
683c2c66affSColin Finck DWORD *trustErrorStatus)
684c2c66affSColin Finck {
685c2c66affSColin Finck BOOL match = FALSE;
686c2c66affSColin Finck
687c2c66affSColin Finck TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
688c2c66affSColin Finck
689c2c66affSColin Finck if (!constraint)
690c2c66affSColin Finck *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
691c2c66affSColin Finck else if (!name)
692c2c66affSColin Finck ; /* no match */
693c2c66affSColin Finck else
694c2c66affSColin Finck {
695c2c66affSColin Finck LPCWSTR colon, authority_end, at, hostname = NULL;
696c2c66affSColin Finck /* The maximum length for a hostname is 254 in the DNS, see RFC 1034 */
697c2c66affSColin Finck WCHAR hostname_buf[255];
698c2c66affSColin Finck
699c2c66affSColin Finck /* RFC 5280: only the hostname portion of the URL is compared. From
700c2c66affSColin Finck * section 4.2.1.10:
701c2c66affSColin Finck * "For URIs, the constraint applies to the host part of the name.
702c2c66affSColin Finck * The constraint MUST be specified as a fully qualified domain name
703c2c66affSColin Finck * and MAY specify a host or a domain."
704c2c66affSColin Finck * The format for URIs is in RFC 2396.
705c2c66affSColin Finck *
706c2c66affSColin Finck * First, remove any scheme that's present. */
707c2c66affSColin Finck colon = strchrW(name, ':');
708c2c66affSColin Finck if (colon && *(colon + 1) == '/' && *(colon + 2) == '/')
709c2c66affSColin Finck name = colon + 3;
710c2c66affSColin Finck /* Next, find the end of the authority component. (The authority is
711c2c66affSColin Finck * generally just the hostname, but it may contain a username or a port.
712c2c66affSColin Finck * Those are removed next.)
713c2c66affSColin Finck */
714c2c66affSColin Finck authority_end = strchrW(name, '/');
715c2c66affSColin Finck if (!authority_end)
716c2c66affSColin Finck authority_end = strchrW(name, '?');
717c2c66affSColin Finck if (!authority_end)
718c2c66affSColin Finck authority_end = name + strlenW(name);
719c2c66affSColin Finck /* Remove any port number from the authority. The userinfo portion
720c2c66affSColin Finck * of an authority may contain a colon, so stop if a userinfo portion
721c2c66affSColin Finck * is found (indicated by '@').
722c2c66affSColin Finck */
723c2c66affSColin Finck for (colon = authority_end; colon >= name && *colon != ':' &&
724c2c66affSColin Finck *colon != '@'; colon--)
725c2c66affSColin Finck ;
726c2c66affSColin Finck if (*colon == ':')
727c2c66affSColin Finck authority_end = colon;
728c2c66affSColin Finck /* Remove any username from the authority */
729c2c66affSColin Finck if ((at = strchrW(name, '@')))
730c2c66affSColin Finck name = at;
731c2c66affSColin Finck /* Ignore any path or query portion of the URL. */
732c2c66affSColin Finck if (*authority_end)
733c2c66affSColin Finck {
734*997d44c9SAmine Khaldi if (authority_end - name < ARRAY_SIZE(hostname_buf))
735c2c66affSColin Finck {
736c2c66affSColin Finck memcpy(hostname_buf, name,
737c2c66affSColin Finck (authority_end - name) * sizeof(WCHAR));
738c2c66affSColin Finck hostname_buf[authority_end - name] = 0;
739c2c66affSColin Finck hostname = hostname_buf;
740c2c66affSColin Finck }
741c2c66affSColin Finck /* else: Hostname is too long, not a match */
742c2c66affSColin Finck }
743c2c66affSColin Finck else
744c2c66affSColin Finck hostname = name;
745c2c66affSColin Finck if (hostname)
746c2c66affSColin Finck match = domain_name_matches(constraint, hostname);
747c2c66affSColin Finck }
748c2c66affSColin Finck return match;
749c2c66affSColin Finck }
750c2c66affSColin Finck
rfc822_name_matches(LPCWSTR constraint,LPCWSTR name,DWORD * trustErrorStatus)751c2c66affSColin Finck static BOOL rfc822_name_matches(LPCWSTR constraint, LPCWSTR name,
752c2c66affSColin Finck DWORD *trustErrorStatus)
753c2c66affSColin Finck {
754c2c66affSColin Finck BOOL match = FALSE;
755c2c66affSColin Finck LPCWSTR at;
756c2c66affSColin Finck
757c2c66affSColin Finck TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
758c2c66affSColin Finck
759c2c66affSColin Finck if (!constraint)
760c2c66affSColin Finck *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
761c2c66affSColin Finck else if (!name)
762c2c66affSColin Finck ; /* no match */
763c2c66affSColin Finck else if (strchrW(constraint, '@'))
764c2c66affSColin Finck match = !lstrcmpiW(constraint, name);
765c2c66affSColin Finck else
766c2c66affSColin Finck {
767c2c66affSColin Finck if ((at = strchrW(name, '@')))
768c2c66affSColin Finck match = domain_name_matches(constraint, at + 1);
769c2c66affSColin Finck else
770c2c66affSColin Finck match = !lstrcmpiW(constraint, name);
771c2c66affSColin Finck }
772c2c66affSColin Finck return match;
773c2c66affSColin Finck }
774c2c66affSColin Finck
dns_name_matches(LPCWSTR constraint,LPCWSTR name,DWORD * trustErrorStatus)775c2c66affSColin Finck static BOOL dns_name_matches(LPCWSTR constraint, LPCWSTR name,
776c2c66affSColin Finck DWORD *trustErrorStatus)
777c2c66affSColin Finck {
778c2c66affSColin Finck BOOL match = FALSE;
779c2c66affSColin Finck
780c2c66affSColin Finck TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
781c2c66affSColin Finck
782c2c66affSColin Finck if (!constraint)
783c2c66affSColin Finck *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
784c2c66affSColin Finck else if (!name)
785c2c66affSColin Finck ; /* no match */
786c2c66affSColin Finck /* RFC 5280, section 4.2.1.10:
787c2c66affSColin Finck * "DNS name restrictions are expressed as host.example.com. Any DNS name
788c2c66affSColin Finck * that can be constructed by simply adding zero or more labels to the
789c2c66affSColin Finck * left-hand side of the name satisfies the name constraint. For example,
790c2c66affSColin Finck * www.host.example.com would satisfy the constraint but host1.example.com
791c2c66affSColin Finck * would not."
792c2c66affSColin Finck */
793c2c66affSColin Finck else if (lstrlenW(name) == lstrlenW(constraint))
794c2c66affSColin Finck match = !lstrcmpiW(name, constraint);
795c2c66affSColin Finck else if (lstrlenW(name) > lstrlenW(constraint))
796c2c66affSColin Finck {
797c2c66affSColin Finck match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
798c2c66affSColin Finck constraint);
799c2c66affSColin Finck if (match)
800c2c66affSColin Finck {
801c2c66affSColin Finck BOOL dot = FALSE;
802c2c66affSColin Finck LPCWSTR ptr;
803c2c66affSColin Finck
804c2c66affSColin Finck /* This only matches if name is a subdomain of constraint, i.e.
805c2c66affSColin Finck * there's a '.' between the beginning of the name and the
806c2c66affSColin Finck * matching portion of the name.
807c2c66affSColin Finck */
808c2c66affSColin Finck for (ptr = name + lstrlenW(name) - lstrlenW(constraint);
809c2c66affSColin Finck !dot && ptr >= name; ptr--)
810c2c66affSColin Finck if (*ptr == '.')
811c2c66affSColin Finck dot = TRUE;
812c2c66affSColin Finck match = dot;
813c2c66affSColin Finck }
814c2c66affSColin Finck }
815c2c66affSColin Finck /* else: name is too short, no match */
816c2c66affSColin Finck
817c2c66affSColin Finck return match;
818c2c66affSColin Finck }
819c2c66affSColin Finck
ip_address_matches(const CRYPT_DATA_BLOB * constraint,const CRYPT_DATA_BLOB * name,DWORD * trustErrorStatus)820c2c66affSColin Finck static BOOL ip_address_matches(const CRYPT_DATA_BLOB *constraint,
821c2c66affSColin Finck const CRYPT_DATA_BLOB *name, DWORD *trustErrorStatus)
822c2c66affSColin Finck {
823c2c66affSColin Finck BOOL match = FALSE;
824c2c66affSColin Finck
825c2c66affSColin Finck TRACE("(%d, %p), (%d, %p)\n", constraint->cbData, constraint->pbData,
826c2c66affSColin Finck name->cbData, name->pbData);
827c2c66affSColin Finck
828c2c66affSColin Finck /* RFC5280, section 4.2.1.10, iPAddress syntax: either 8 or 32 bytes, for
829c2c66affSColin Finck * IPv4 or IPv6 addresses, respectively.
830c2c66affSColin Finck */
831c2c66affSColin Finck if (constraint->cbData != sizeof(DWORD) * 2 && constraint->cbData != 32)
832c2c66affSColin Finck *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
833c2c66affSColin Finck else if (name->cbData == sizeof(DWORD) &&
834c2c66affSColin Finck constraint->cbData == sizeof(DWORD) * 2)
835c2c66affSColin Finck {
836c2c66affSColin Finck DWORD subnet, mask, addr;
837c2c66affSColin Finck
838c2c66affSColin Finck memcpy(&subnet, constraint->pbData, sizeof(subnet));
839c2c66affSColin Finck memcpy(&mask, constraint->pbData + sizeof(subnet), sizeof(mask));
840c2c66affSColin Finck memcpy(&addr, name->pbData, sizeof(addr));
841c2c66affSColin Finck /* These are really in big-endian order, but for equality matching we
842c2c66affSColin Finck * don't need to swap to host order
843c2c66affSColin Finck */
844c2c66affSColin Finck match = (subnet & mask) == (addr & mask);
845c2c66affSColin Finck }
846c2c66affSColin Finck else if (name->cbData == 16 && constraint->cbData == 32)
847c2c66affSColin Finck {
848c2c66affSColin Finck const BYTE *subnet, *mask, *addr;
849c2c66affSColin Finck DWORD i;
850c2c66affSColin Finck
851c2c66affSColin Finck subnet = constraint->pbData;
852c2c66affSColin Finck mask = constraint->pbData + 16;
853c2c66affSColin Finck addr = name->pbData;
854c2c66affSColin Finck match = TRUE;
855c2c66affSColin Finck for (i = 0; match && i < 16; i++)
856c2c66affSColin Finck if ((subnet[i] & mask[i]) != (addr[i] & mask[i]))
857c2c66affSColin Finck match = FALSE;
858c2c66affSColin Finck }
859c2c66affSColin Finck /* else: name is wrong size, no match */
860c2c66affSColin Finck
861c2c66affSColin Finck return match;
862c2c66affSColin Finck }
863c2c66affSColin Finck
directory_name_matches(const CERT_NAME_BLOB * constraint,const CERT_NAME_BLOB * name)864c2c66affSColin Finck static BOOL directory_name_matches(const CERT_NAME_BLOB *constraint,
865c2c66affSColin Finck const CERT_NAME_BLOB *name)
866c2c66affSColin Finck {
867c2c66affSColin Finck CERT_NAME_INFO *constraintName;
868c2c66affSColin Finck DWORD size;
869c2c66affSColin Finck BOOL match = FALSE;
870c2c66affSColin Finck
871c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME, constraint->pbData,
872c2c66affSColin Finck constraint->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &constraintName, &size))
873c2c66affSColin Finck {
874c2c66affSColin Finck DWORD i;
875c2c66affSColin Finck
876c2c66affSColin Finck match = TRUE;
877c2c66affSColin Finck for (i = 0; match && i < constraintName->cRDN; i++)
878c2c66affSColin Finck match = CertIsRDNAttrsInCertificateName(X509_ASN_ENCODING,
879c2c66affSColin Finck CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG,
880c2c66affSColin Finck (CERT_NAME_BLOB *)name, &constraintName->rgRDN[i]);
881c2c66affSColin Finck LocalFree(constraintName);
882c2c66affSColin Finck }
883c2c66affSColin Finck return match;
884c2c66affSColin Finck }
885c2c66affSColin Finck
alt_name_matches(const CERT_ALT_NAME_ENTRY * name,const CERT_ALT_NAME_ENTRY * constraint,DWORD * trustErrorStatus,BOOL * present)886c2c66affSColin Finck static BOOL alt_name_matches(const CERT_ALT_NAME_ENTRY *name,
887c2c66affSColin Finck const CERT_ALT_NAME_ENTRY *constraint, DWORD *trustErrorStatus, BOOL *present)
888c2c66affSColin Finck {
889c2c66affSColin Finck BOOL match = FALSE;
890c2c66affSColin Finck
891c2c66affSColin Finck if (name->dwAltNameChoice == constraint->dwAltNameChoice)
892c2c66affSColin Finck {
893c2c66affSColin Finck if (present)
894c2c66affSColin Finck *present = TRUE;
895c2c66affSColin Finck switch (constraint->dwAltNameChoice)
896c2c66affSColin Finck {
897c2c66affSColin Finck case CERT_ALT_NAME_RFC822_NAME:
898c2c66affSColin Finck match = rfc822_name_matches(constraint->u.pwszURL,
899c2c66affSColin Finck name->u.pwszURL, trustErrorStatus);
900c2c66affSColin Finck break;
901c2c66affSColin Finck case CERT_ALT_NAME_DNS_NAME:
902c2c66affSColin Finck match = dns_name_matches(constraint->u.pwszURL,
903c2c66affSColin Finck name->u.pwszURL, trustErrorStatus);
904c2c66affSColin Finck break;
905c2c66affSColin Finck case CERT_ALT_NAME_URL:
906c2c66affSColin Finck match = url_matches(constraint->u.pwszURL,
907c2c66affSColin Finck name->u.pwszURL, trustErrorStatus);
908c2c66affSColin Finck break;
909c2c66affSColin Finck case CERT_ALT_NAME_IP_ADDRESS:
910c2c66affSColin Finck match = ip_address_matches(&constraint->u.IPAddress,
911c2c66affSColin Finck &name->u.IPAddress, trustErrorStatus);
912c2c66affSColin Finck break;
913c2c66affSColin Finck case CERT_ALT_NAME_DIRECTORY_NAME:
914c2c66affSColin Finck match = directory_name_matches(&constraint->u.DirectoryName,
915c2c66affSColin Finck &name->u.DirectoryName);
916c2c66affSColin Finck break;
917c2c66affSColin Finck default:
918c2c66affSColin Finck ERR("name choice %d unsupported in this context\n",
919c2c66affSColin Finck constraint->dwAltNameChoice);
920c2c66affSColin Finck *trustErrorStatus |=
921c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT;
922c2c66affSColin Finck }
923c2c66affSColin Finck }
924c2c66affSColin Finck else if (present)
925c2c66affSColin Finck *present = FALSE;
926c2c66affSColin Finck return match;
927c2c66affSColin Finck }
928c2c66affSColin Finck
alt_name_matches_excluded_name(const CERT_ALT_NAME_ENTRY * name,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus)929c2c66affSColin Finck static BOOL alt_name_matches_excluded_name(const CERT_ALT_NAME_ENTRY *name,
930c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus)
931c2c66affSColin Finck {
932c2c66affSColin Finck DWORD i;
933c2c66affSColin Finck BOOL match = FALSE;
934c2c66affSColin Finck
935c2c66affSColin Finck for (i = 0; !match && i < nameConstraints->cExcludedSubtree; i++)
936c2c66affSColin Finck match = alt_name_matches(name,
937c2c66affSColin Finck &nameConstraints->rgExcludedSubtree[i].Base, trustErrorStatus, NULL);
938c2c66affSColin Finck return match;
939c2c66affSColin Finck }
940c2c66affSColin Finck
alt_name_matches_permitted_name(const CERT_ALT_NAME_ENTRY * name,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus,BOOL * present)941c2c66affSColin Finck static BOOL alt_name_matches_permitted_name(const CERT_ALT_NAME_ENTRY *name,
942c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus,
943c2c66affSColin Finck BOOL *present)
944c2c66affSColin Finck {
945c2c66affSColin Finck DWORD i;
946c2c66affSColin Finck BOOL match = FALSE;
947c2c66affSColin Finck
948c2c66affSColin Finck for (i = 0; !match && i < nameConstraints->cPermittedSubtree; i++)
949c2c66affSColin Finck match = alt_name_matches(name,
950c2c66affSColin Finck &nameConstraints->rgPermittedSubtree[i].Base, trustErrorStatus,
951c2c66affSColin Finck present);
952c2c66affSColin Finck return match;
953c2c66affSColin Finck }
954c2c66affSColin Finck
get_subject_alt_name_ext(const CERT_INFO * cert)955c2c66affSColin Finck static inline PCERT_EXTENSION get_subject_alt_name_ext(const CERT_INFO *cert)
956c2c66affSColin Finck {
957c2c66affSColin Finck PCERT_EXTENSION ext;
958c2c66affSColin Finck
959c2c66affSColin Finck ext = CertFindExtension(szOID_SUBJECT_ALT_NAME2,
960c2c66affSColin Finck cert->cExtension, cert->rgExtension);
961c2c66affSColin Finck if (!ext)
962c2c66affSColin Finck ext = CertFindExtension(szOID_SUBJECT_ALT_NAME,
963c2c66affSColin Finck cert->cExtension, cert->rgExtension);
964c2c66affSColin Finck return ext;
965c2c66affSColin Finck }
966c2c66affSColin Finck
compare_alt_name_with_constraints(const CERT_EXTENSION * altNameExt,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus)967c2c66affSColin Finck static void compare_alt_name_with_constraints(const CERT_EXTENSION *altNameExt,
968c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus)
969c2c66affSColin Finck {
970c2c66affSColin Finck CERT_ALT_NAME_INFO *subjectAltName;
971c2c66affSColin Finck DWORD size;
972c2c66affSColin Finck
973c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME,
974c2c66affSColin Finck altNameExt->Value.pbData, altNameExt->Value.cbData,
975c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
976c2c66affSColin Finck &subjectAltName, &size))
977c2c66affSColin Finck {
978c2c66affSColin Finck DWORD i;
979c2c66affSColin Finck
980c2c66affSColin Finck for (i = 0; i < subjectAltName->cAltEntry; i++)
981c2c66affSColin Finck {
982c2c66affSColin Finck BOOL nameFormPresent;
983c2c66affSColin Finck
984c2c66affSColin Finck /* A name constraint only applies if the name form is present.
985c2c66affSColin Finck * From RFC 5280, section 4.2.1.10:
986c2c66affSColin Finck * "Restrictions apply only when the specified name form is
987c2c66affSColin Finck * present. If no name of the type is in the certificate,
988c2c66affSColin Finck * the certificate is acceptable."
989c2c66affSColin Finck */
990c2c66affSColin Finck if (alt_name_matches_excluded_name(
991c2c66affSColin Finck &subjectAltName->rgAltEntry[i], nameConstraints,
992c2c66affSColin Finck trustErrorStatus))
993c2c66affSColin Finck {
994c2c66affSColin Finck TRACE_(chain)("subject alternate name form %d excluded\n",
995c2c66affSColin Finck subjectAltName->rgAltEntry[i].dwAltNameChoice);
996c2c66affSColin Finck *trustErrorStatus |=
997c2c66affSColin Finck CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT;
998c2c66affSColin Finck }
999c2c66affSColin Finck nameFormPresent = FALSE;
1000c2c66affSColin Finck if (!alt_name_matches_permitted_name(
1001c2c66affSColin Finck &subjectAltName->rgAltEntry[i], nameConstraints,
1002c2c66affSColin Finck trustErrorStatus, &nameFormPresent) && nameFormPresent)
1003c2c66affSColin Finck {
1004c2c66affSColin Finck TRACE_(chain)("subject alternate name form %d not permitted\n",
1005c2c66affSColin Finck subjectAltName->rgAltEntry[i].dwAltNameChoice);
1006c2c66affSColin Finck *trustErrorStatus |=
1007c2c66affSColin Finck CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT;
1008c2c66affSColin Finck }
1009c2c66affSColin Finck }
1010c2c66affSColin Finck LocalFree(subjectAltName);
1011c2c66affSColin Finck }
1012c2c66affSColin Finck else
1013c2c66affSColin Finck *trustErrorStatus |=
1014c2c66affSColin Finck CERT_TRUST_INVALID_EXTENSION | CERT_TRUST_INVALID_NAME_CONSTRAINTS;
1015c2c66affSColin Finck }
1016c2c66affSColin Finck
rfc822_attr_matches_excluded_name(const CERT_RDN_ATTR * attr,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus)1017c2c66affSColin Finck static BOOL rfc822_attr_matches_excluded_name(const CERT_RDN_ATTR *attr,
1018c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus)
1019c2c66affSColin Finck {
1020c2c66affSColin Finck DWORD i;
1021c2c66affSColin Finck BOOL match = FALSE;
1022c2c66affSColin Finck
1023c2c66affSColin Finck for (i = 0; !match && i < nameConstraints->cExcludedSubtree; i++)
1024c2c66affSColin Finck {
1025c2c66affSColin Finck const CERT_ALT_NAME_ENTRY *constraint =
1026c2c66affSColin Finck &nameConstraints->rgExcludedSubtree[i].Base;
1027c2c66affSColin Finck
1028c2c66affSColin Finck if (constraint->dwAltNameChoice == CERT_ALT_NAME_RFC822_NAME)
1029c2c66affSColin Finck match = rfc822_name_matches(constraint->u.pwszRfc822Name,
1030c2c66affSColin Finck (LPCWSTR)attr->Value.pbData, trustErrorStatus);
1031c2c66affSColin Finck }
1032c2c66affSColin Finck return match;
1033c2c66affSColin Finck }
1034c2c66affSColin Finck
rfc822_attr_matches_permitted_name(const CERT_RDN_ATTR * attr,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus,BOOL * present)1035c2c66affSColin Finck static BOOL rfc822_attr_matches_permitted_name(const CERT_RDN_ATTR *attr,
1036c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus,
1037c2c66affSColin Finck BOOL *present)
1038c2c66affSColin Finck {
1039c2c66affSColin Finck DWORD i;
1040c2c66affSColin Finck BOOL match = FALSE;
1041c2c66affSColin Finck
1042c2c66affSColin Finck for (i = 0; !match && i < nameConstraints->cPermittedSubtree; i++)
1043c2c66affSColin Finck {
1044c2c66affSColin Finck const CERT_ALT_NAME_ENTRY *constraint =
1045c2c66affSColin Finck &nameConstraints->rgPermittedSubtree[i].Base;
1046c2c66affSColin Finck
1047c2c66affSColin Finck if (constraint->dwAltNameChoice == CERT_ALT_NAME_RFC822_NAME)
1048c2c66affSColin Finck {
1049c2c66affSColin Finck *present = TRUE;
1050c2c66affSColin Finck match = rfc822_name_matches(constraint->u.pwszRfc822Name,
1051c2c66affSColin Finck (LPCWSTR)attr->Value.pbData, trustErrorStatus);
1052c2c66affSColin Finck }
1053c2c66affSColin Finck }
1054c2c66affSColin Finck return match;
1055c2c66affSColin Finck }
1056c2c66affSColin Finck
compare_subject_with_email_constraints(const CERT_NAME_BLOB * subjectName,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus)1057c2c66affSColin Finck static void compare_subject_with_email_constraints(
1058c2c66affSColin Finck const CERT_NAME_BLOB *subjectName,
1059c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus)
1060c2c66affSColin Finck {
1061c2c66affSColin Finck CERT_NAME_INFO *name;
1062c2c66affSColin Finck DWORD size;
1063c2c66affSColin Finck
1064c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_UNICODE_NAME,
1065c2c66affSColin Finck subjectName->pbData, subjectName->cbData,
1066c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &name, &size))
1067c2c66affSColin Finck {
1068c2c66affSColin Finck DWORD i, j;
1069c2c66affSColin Finck
1070c2c66affSColin Finck for (i = 0; i < name->cRDN; i++)
1071c2c66affSColin Finck for (j = 0; j < name->rgRDN[i].cRDNAttr; j++)
1072c2c66affSColin Finck if (!strcmp(name->rgRDN[i].rgRDNAttr[j].pszObjId,
1073c2c66affSColin Finck szOID_RSA_emailAddr))
1074c2c66affSColin Finck {
1075c2c66affSColin Finck BOOL nameFormPresent;
1076c2c66affSColin Finck
1077c2c66affSColin Finck /* A name constraint only applies if the name form is
1078c2c66affSColin Finck * present. From RFC 5280, section 4.2.1.10:
1079c2c66affSColin Finck * "Restrictions apply only when the specified name form is
1080c2c66affSColin Finck * present. If no name of the type is in the certificate,
1081c2c66affSColin Finck * the certificate is acceptable."
1082c2c66affSColin Finck */
1083c2c66affSColin Finck if (rfc822_attr_matches_excluded_name(
1084c2c66affSColin Finck &name->rgRDN[i].rgRDNAttr[j], nameConstraints,
1085c2c66affSColin Finck trustErrorStatus))
1086c2c66affSColin Finck {
1087c2c66affSColin Finck TRACE_(chain)(
1088c2c66affSColin Finck "email address in subject name is excluded\n");
1089c2c66affSColin Finck *trustErrorStatus |=
1090c2c66affSColin Finck CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT;
1091c2c66affSColin Finck }
1092c2c66affSColin Finck nameFormPresent = FALSE;
1093c2c66affSColin Finck if (!rfc822_attr_matches_permitted_name(
1094c2c66affSColin Finck &name->rgRDN[i].rgRDNAttr[j], nameConstraints,
1095c2c66affSColin Finck trustErrorStatus, &nameFormPresent) && nameFormPresent)
1096c2c66affSColin Finck {
1097c2c66affSColin Finck TRACE_(chain)(
1098c2c66affSColin Finck "email address in subject name is not permitted\n");
1099c2c66affSColin Finck *trustErrorStatus |=
1100c2c66affSColin Finck CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT;
1101c2c66affSColin Finck }
1102c2c66affSColin Finck }
1103c2c66affSColin Finck LocalFree(name);
1104c2c66affSColin Finck }
1105c2c66affSColin Finck else
1106c2c66affSColin Finck *trustErrorStatus |=
1107c2c66affSColin Finck CERT_TRUST_INVALID_EXTENSION | CERT_TRUST_INVALID_NAME_CONSTRAINTS;
1108c2c66affSColin Finck }
1109c2c66affSColin Finck
CRYPT_IsEmptyName(const CERT_NAME_BLOB * name)1110c2c66affSColin Finck static BOOL CRYPT_IsEmptyName(const CERT_NAME_BLOB *name)
1111c2c66affSColin Finck {
1112c2c66affSColin Finck BOOL empty;
1113c2c66affSColin Finck
1114c2c66affSColin Finck if (!name->cbData)
1115c2c66affSColin Finck empty = TRUE;
1116c2c66affSColin Finck else if (name->cbData == 2 && name->pbData[1] == 0)
1117c2c66affSColin Finck {
1118c2c66affSColin Finck /* An empty sequence is also empty */
1119c2c66affSColin Finck empty = TRUE;
1120c2c66affSColin Finck }
1121c2c66affSColin Finck else
1122c2c66affSColin Finck empty = FALSE;
1123c2c66affSColin Finck return empty;
1124c2c66affSColin Finck }
1125c2c66affSColin Finck
compare_subject_with_constraints(const CERT_NAME_BLOB * subjectName,const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,DWORD * trustErrorStatus)1126c2c66affSColin Finck static void compare_subject_with_constraints(const CERT_NAME_BLOB *subjectName,
1127c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, DWORD *trustErrorStatus)
1128c2c66affSColin Finck {
1129c2c66affSColin Finck BOOL hasEmailConstraint = FALSE;
1130c2c66affSColin Finck DWORD i;
1131c2c66affSColin Finck
1132c2c66affSColin Finck /* In general, a subject distinguished name only matches a directory name
1133c2c66affSColin Finck * constraint. However, an exception exists for email addresses.
1134c2c66affSColin Finck * From RFC 5280, section 4.2.1.6:
1135c2c66affSColin Finck * "Legacy implementations exist where an electronic mail address is
1136c2c66affSColin Finck * embedded in the subject distinguished name as an emailAddress
1137c2c66affSColin Finck * attribute [RFC2985]."
1138c2c66affSColin Finck * If an email address constraint exists, check that constraint separately.
1139c2c66affSColin Finck */
1140c2c66affSColin Finck for (i = 0; !hasEmailConstraint && i < nameConstraints->cExcludedSubtree;
1141c2c66affSColin Finck i++)
1142c2c66affSColin Finck if (nameConstraints->rgExcludedSubtree[i].Base.dwAltNameChoice ==
1143c2c66affSColin Finck CERT_ALT_NAME_RFC822_NAME)
1144c2c66affSColin Finck hasEmailConstraint = TRUE;
1145c2c66affSColin Finck for (i = 0; !hasEmailConstraint && i < nameConstraints->cPermittedSubtree;
1146c2c66affSColin Finck i++)
1147c2c66affSColin Finck if (nameConstraints->rgPermittedSubtree[i].Base.dwAltNameChoice ==
1148c2c66affSColin Finck CERT_ALT_NAME_RFC822_NAME)
1149c2c66affSColin Finck hasEmailConstraint = TRUE;
1150c2c66affSColin Finck if (hasEmailConstraint)
1151c2c66affSColin Finck compare_subject_with_email_constraints(subjectName, nameConstraints,
1152c2c66affSColin Finck trustErrorStatus);
1153c2c66affSColin Finck for (i = 0; i < nameConstraints->cExcludedSubtree; i++)
1154c2c66affSColin Finck {
1155c2c66affSColin Finck CERT_ALT_NAME_ENTRY *constraint =
1156c2c66affSColin Finck &nameConstraints->rgExcludedSubtree[i].Base;
1157c2c66affSColin Finck
1158c2c66affSColin Finck if (constraint->dwAltNameChoice == CERT_ALT_NAME_DIRECTORY_NAME &&
1159c2c66affSColin Finck directory_name_matches(&constraint->u.DirectoryName, subjectName))
1160c2c66affSColin Finck {
1161c2c66affSColin Finck TRACE_(chain)("subject name is excluded\n");
1162c2c66affSColin Finck *trustErrorStatus |=
1163c2c66affSColin Finck CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT;
1164c2c66affSColin Finck }
1165c2c66affSColin Finck }
1166c2c66affSColin Finck /* RFC 5280, section 4.2.1.10:
1167c2c66affSColin Finck * "Restrictions apply only when the specified name form is present.
1168c2c66affSColin Finck * If no name of the type is in the certificate, the certificate is
1169c2c66affSColin Finck * acceptable."
1170c2c66affSColin Finck * An empty name can't have the name form present, so don't check it.
1171c2c66affSColin Finck */
1172c2c66affSColin Finck if (nameConstraints->cPermittedSubtree && !CRYPT_IsEmptyName(subjectName))
1173c2c66affSColin Finck {
1174c2c66affSColin Finck BOOL match = FALSE, hasDirectoryConstraint = FALSE;
1175c2c66affSColin Finck
1176c2c66affSColin Finck for (i = 0; !match && i < nameConstraints->cPermittedSubtree; i++)
1177c2c66affSColin Finck {
1178c2c66affSColin Finck CERT_ALT_NAME_ENTRY *constraint =
1179c2c66affSColin Finck &nameConstraints->rgPermittedSubtree[i].Base;
1180c2c66affSColin Finck
1181c2c66affSColin Finck if (constraint->dwAltNameChoice == CERT_ALT_NAME_DIRECTORY_NAME)
1182c2c66affSColin Finck {
1183c2c66affSColin Finck hasDirectoryConstraint = TRUE;
1184c2c66affSColin Finck match = directory_name_matches(&constraint->u.DirectoryName,
1185c2c66affSColin Finck subjectName);
1186c2c66affSColin Finck }
1187c2c66affSColin Finck }
1188c2c66affSColin Finck if (hasDirectoryConstraint && !match)
1189c2c66affSColin Finck {
1190c2c66affSColin Finck TRACE_(chain)("subject name is not permitted\n");
1191c2c66affSColin Finck *trustErrorStatus |= CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT;
1192c2c66affSColin Finck }
1193c2c66affSColin Finck }
1194c2c66affSColin Finck }
1195c2c66affSColin Finck
CRYPT_CheckNameConstraints(const CERT_NAME_CONSTRAINTS_INFO * nameConstraints,const CERT_INFO * cert,DWORD * trustErrorStatus)1196c2c66affSColin Finck static void CRYPT_CheckNameConstraints(
1197c2c66affSColin Finck const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, const CERT_INFO *cert,
1198c2c66affSColin Finck DWORD *trustErrorStatus)
1199c2c66affSColin Finck {
1200c2c66affSColin Finck CERT_EXTENSION *ext = get_subject_alt_name_ext(cert);
1201c2c66affSColin Finck
1202c2c66affSColin Finck if (ext)
1203c2c66affSColin Finck compare_alt_name_with_constraints(ext, nameConstraints,
1204c2c66affSColin Finck trustErrorStatus);
1205c2c66affSColin Finck /* Name constraints apply to the subject alternative name as well as the
1206c2c66affSColin Finck * subject name. From RFC 5280, section 4.2.1.10:
1207c2c66affSColin Finck * "Restrictions apply to the subject distinguished name and apply to
1208c2c66affSColin Finck * subject alternative names."
1209c2c66affSColin Finck */
1210c2c66affSColin Finck compare_subject_with_constraints(&cert->Subject, nameConstraints,
1211c2c66affSColin Finck trustErrorStatus);
1212c2c66affSColin Finck }
1213c2c66affSColin Finck
1214c2c66affSColin Finck /* Gets cert's name constraints, if any. Free with LocalFree. */
CRYPT_GetNameConstraints(CERT_INFO * cert)1215c2c66affSColin Finck static CERT_NAME_CONSTRAINTS_INFO *CRYPT_GetNameConstraints(CERT_INFO *cert)
1216c2c66affSColin Finck {
1217c2c66affSColin Finck CERT_NAME_CONSTRAINTS_INFO *info = NULL;
1218c2c66affSColin Finck
1219c2c66affSColin Finck CERT_EXTENSION *ext;
1220c2c66affSColin Finck
1221c2c66affSColin Finck if ((ext = CertFindExtension(szOID_NAME_CONSTRAINTS, cert->cExtension,
1222c2c66affSColin Finck cert->rgExtension)))
1223c2c66affSColin Finck {
1224c2c66affSColin Finck DWORD size;
1225c2c66affSColin Finck
1226c2c66affSColin Finck CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_CONSTRAINTS,
1227c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData,
1228c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &info,
1229c2c66affSColin Finck &size);
1230c2c66affSColin Finck }
1231c2c66affSColin Finck return info;
1232c2c66affSColin Finck }
1233c2c66affSColin Finck
CRYPT_IsValidNameConstraint(const CERT_NAME_CONSTRAINTS_INFO * info)1234c2c66affSColin Finck static BOOL CRYPT_IsValidNameConstraint(const CERT_NAME_CONSTRAINTS_INFO *info)
1235c2c66affSColin Finck {
1236c2c66affSColin Finck DWORD i;
1237c2c66affSColin Finck BOOL ret = TRUE;
1238c2c66affSColin Finck
1239c2c66affSColin Finck /* Make sure at least one permitted or excluded subtree is present. From
1240c2c66affSColin Finck * RFC 5280, section 4.2.1.10:
1241c2c66affSColin Finck * "Conforming CAs MUST NOT issue certificates where name constraints is an
1242c2c66affSColin Finck * empty sequence. That is, either the permittedSubtrees field or the
1243c2c66affSColin Finck * excludedSubtrees MUST be present."
1244c2c66affSColin Finck */
1245c2c66affSColin Finck if (!info->cPermittedSubtree && !info->cExcludedSubtree)
1246c2c66affSColin Finck {
1247c2c66affSColin Finck WARN_(chain)("constraints contain no permitted nor excluded subtree\n");
1248c2c66affSColin Finck ret = FALSE;
1249c2c66affSColin Finck }
1250c2c66affSColin Finck /* Check that none of the constraints specifies a minimum or a maximum.
1251c2c66affSColin Finck * See RFC 5280, section 4.2.1.10:
1252c2c66affSColin Finck * "Within this profile, the minimum and maximum fields are not used with
1253c2c66affSColin Finck * any name forms, thus, the minimum MUST be zero, and maximum MUST be
1254c2c66affSColin Finck * absent. However, if an application encounters a critical name
1255c2c66affSColin Finck * constraints extension that specifies other values for minimum or
1256c2c66affSColin Finck * maximum for a name form that appears in a subsequent certificate, the
1257c2c66affSColin Finck * application MUST either process these fields or reject the
1258c2c66affSColin Finck * certificate."
1259c2c66affSColin Finck * Since it gives no guidance as to how to process these fields, we
1260c2c66affSColin Finck * reject any name constraint that contains them.
1261c2c66affSColin Finck */
1262c2c66affSColin Finck for (i = 0; ret && i < info->cPermittedSubtree; i++)
1263c2c66affSColin Finck if (info->rgPermittedSubtree[i].dwMinimum ||
1264c2c66affSColin Finck info->rgPermittedSubtree[i].fMaximum)
1265c2c66affSColin Finck {
1266c2c66affSColin Finck TRACE_(chain)("found a minimum or maximum in permitted subtrees\n");
1267c2c66affSColin Finck ret = FALSE;
1268c2c66affSColin Finck }
1269c2c66affSColin Finck for (i = 0; ret && i < info->cExcludedSubtree; i++)
1270c2c66affSColin Finck if (info->rgExcludedSubtree[i].dwMinimum ||
1271c2c66affSColin Finck info->rgExcludedSubtree[i].fMaximum)
1272c2c66affSColin Finck {
1273c2c66affSColin Finck TRACE_(chain)("found a minimum or maximum in excluded subtrees\n");
1274c2c66affSColin Finck ret = FALSE;
1275c2c66affSColin Finck }
1276c2c66affSColin Finck return ret;
1277c2c66affSColin Finck }
1278c2c66affSColin Finck
CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain)1279c2c66affSColin Finck static void CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain)
1280c2c66affSColin Finck {
1281c2c66affSColin Finck int i, j;
1282c2c66affSColin Finck
1283c2c66affSColin Finck /* Microsoft's implementation appears to violate RFC 3280: according to
1284c2c66affSColin Finck * MSDN, the various CERT_TRUST_*_NAME_CONSTRAINT errors are set if a CA's
1285c2c66affSColin Finck * name constraint is violated in the end cert. According to RFC 3280,
1286c2c66affSColin Finck * the constraints should be checked against every subsequent certificate
1287c2c66affSColin Finck * in the chain, not just the end cert.
1288c2c66affSColin Finck * Microsoft's implementation also sets the name constraint errors on the
1289c2c66affSColin Finck * certs whose constraints were violated, not on the certs that violated
1290c2c66affSColin Finck * them.
1291c2c66affSColin Finck * In order to be error-compatible with Microsoft's implementation, while
1292c2c66affSColin Finck * still adhering to RFC 3280, I use a O(n ^ 2) algorithm to check name
1293c2c66affSColin Finck * constraints.
1294c2c66affSColin Finck */
1295c2c66affSColin Finck for (i = chain->cElement - 1; i > 0; i--)
1296c2c66affSColin Finck {
1297c2c66affSColin Finck CERT_NAME_CONSTRAINTS_INFO *nameConstraints;
1298c2c66affSColin Finck
1299c2c66affSColin Finck if ((nameConstraints = CRYPT_GetNameConstraints(
1300c2c66affSColin Finck chain->rgpElement[i]->pCertContext->pCertInfo)))
1301c2c66affSColin Finck {
1302c2c66affSColin Finck if (!CRYPT_IsValidNameConstraint(nameConstraints))
1303c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1304c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT;
1305c2c66affSColin Finck else
1306c2c66affSColin Finck {
1307c2c66affSColin Finck for (j = i - 1; j >= 0; j--)
1308c2c66affSColin Finck {
1309c2c66affSColin Finck DWORD errorStatus = 0;
1310c2c66affSColin Finck
1311c2c66affSColin Finck /* According to RFC 3280, self-signed certs don't have name
1312c2c66affSColin Finck * constraints checked unless they're the end cert.
1313c2c66affSColin Finck */
1314c2c66affSColin Finck if (j == 0 || !CRYPT_IsCertificateSelfSigned(
1315*997d44c9SAmine Khaldi chain->rgpElement[j]->pCertContext))
1316c2c66affSColin Finck {
1317c2c66affSColin Finck CRYPT_CheckNameConstraints(nameConstraints,
1318c2c66affSColin Finck chain->rgpElement[j]->pCertContext->pCertInfo,
1319c2c66affSColin Finck &errorStatus);
1320c2c66affSColin Finck if (errorStatus)
1321c2c66affSColin Finck {
1322c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1323c2c66affSColin Finck errorStatus;
1324c2c66affSColin Finck CRYPT_CombineTrustStatus(&chain->TrustStatus,
1325c2c66affSColin Finck &chain->rgpElement[i]->TrustStatus);
1326c2c66affSColin Finck }
1327c2c66affSColin Finck else
1328c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwInfoStatus |=
1329c2c66affSColin Finck CERT_TRUST_HAS_VALID_NAME_CONSTRAINTS;
1330c2c66affSColin Finck }
1331c2c66affSColin Finck }
1332c2c66affSColin Finck }
1333c2c66affSColin Finck LocalFree(nameConstraints);
1334c2c66affSColin Finck }
1335c2c66affSColin Finck }
1336c2c66affSColin Finck }
1337c2c66affSColin Finck
1338c2c66affSColin Finck /* Gets cert's policies info, if any. Free with LocalFree. */
CRYPT_GetPolicies(PCCERT_CONTEXT cert)1339c2c66affSColin Finck static CERT_POLICIES_INFO *CRYPT_GetPolicies(PCCERT_CONTEXT cert)
1340c2c66affSColin Finck {
1341c2c66affSColin Finck PCERT_EXTENSION ext;
1342c2c66affSColin Finck CERT_POLICIES_INFO *policies = NULL;
1343c2c66affSColin Finck
1344c2c66affSColin Finck ext = CertFindExtension(szOID_KEY_USAGE, cert->pCertInfo->cExtension,
1345c2c66affSColin Finck cert->pCertInfo->rgExtension);
1346c2c66affSColin Finck if (ext)
1347c2c66affSColin Finck {
1348c2c66affSColin Finck DWORD size;
1349c2c66affSColin Finck
1350c2c66affSColin Finck CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_POLICIES,
1351c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1352c2c66affSColin Finck &policies, &size);
1353c2c66affSColin Finck }
1354c2c66affSColin Finck return policies;
1355c2c66affSColin Finck }
1356c2c66affSColin Finck
CRYPT_CheckPolicies(const CERT_POLICIES_INFO * policies,CERT_INFO * cert,DWORD * errorStatus)1357c2c66affSColin Finck static void CRYPT_CheckPolicies(const CERT_POLICIES_INFO *policies, CERT_INFO *cert,
1358c2c66affSColin Finck DWORD *errorStatus)
1359c2c66affSColin Finck {
1360c2c66affSColin Finck DWORD i;
1361c2c66affSColin Finck
1362c2c66affSColin Finck for (i = 0; i < policies->cPolicyInfo; i++)
1363c2c66affSColin Finck {
1364c2c66affSColin Finck /* For now, the only accepted policy identifier is the anyPolicy
1365c2c66affSColin Finck * identifier.
1366c2c66affSColin Finck * FIXME: the policy identifiers should be compared against the
1367c2c66affSColin Finck * cert's certificate policies extension, subject to the policy
1368c2c66affSColin Finck * mappings extension, and the policy constraints extension.
1369c2c66affSColin Finck * See RFC 5280, sections 4.2.1.4, 4.2.1.5, and 4.2.1.11.
1370c2c66affSColin Finck */
1371c2c66affSColin Finck if (strcmp(policies->rgPolicyInfo[i].pszPolicyIdentifier,
1372c2c66affSColin Finck szOID_ANY_CERT_POLICY))
1373c2c66affSColin Finck {
1374c2c66affSColin Finck FIXME("unsupported policy %s\n",
1375c2c66affSColin Finck policies->rgPolicyInfo[i].pszPolicyIdentifier);
1376c2c66affSColin Finck *errorStatus |= CERT_TRUST_INVALID_POLICY_CONSTRAINTS;
1377c2c66affSColin Finck }
1378c2c66affSColin Finck }
1379c2c66affSColin Finck }
1380c2c66affSColin Finck
CRYPT_CheckChainPolicies(PCERT_SIMPLE_CHAIN chain)1381c2c66affSColin Finck static void CRYPT_CheckChainPolicies(PCERT_SIMPLE_CHAIN chain)
1382c2c66affSColin Finck {
1383c2c66affSColin Finck int i, j;
1384c2c66affSColin Finck
1385c2c66affSColin Finck for (i = chain->cElement - 1; i > 0; i--)
1386c2c66affSColin Finck {
1387c2c66affSColin Finck CERT_POLICIES_INFO *policies;
1388c2c66affSColin Finck
1389c2c66affSColin Finck if ((policies = CRYPT_GetPolicies(chain->rgpElement[i]->pCertContext)))
1390c2c66affSColin Finck {
1391c2c66affSColin Finck for (j = i - 1; j >= 0; j--)
1392c2c66affSColin Finck {
1393c2c66affSColin Finck DWORD errorStatus = 0;
1394c2c66affSColin Finck
1395c2c66affSColin Finck CRYPT_CheckPolicies(policies,
1396c2c66affSColin Finck chain->rgpElement[j]->pCertContext->pCertInfo, &errorStatus);
1397c2c66affSColin Finck if (errorStatus)
1398c2c66affSColin Finck {
1399c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1400c2c66affSColin Finck errorStatus;
1401c2c66affSColin Finck CRYPT_CombineTrustStatus(&chain->TrustStatus,
1402c2c66affSColin Finck &chain->rgpElement[i]->TrustStatus);
1403c2c66affSColin Finck }
1404c2c66affSColin Finck }
1405c2c66affSColin Finck LocalFree(policies);
1406c2c66affSColin Finck }
1407c2c66affSColin Finck }
1408c2c66affSColin Finck }
1409c2c66affSColin Finck
name_value_to_str(const CERT_NAME_BLOB * name)1410c2c66affSColin Finck static LPWSTR name_value_to_str(const CERT_NAME_BLOB *name)
1411c2c66affSColin Finck {
1412c2c66affSColin Finck DWORD len = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0, name,
1413c2c66affSColin Finck CERT_SIMPLE_NAME_STR, NULL, 0);
1414c2c66affSColin Finck LPWSTR str = NULL;
1415c2c66affSColin Finck
1416c2c66affSColin Finck if (len)
1417c2c66affSColin Finck {
1418c2c66affSColin Finck str = CryptMemAlloc(len * sizeof(WCHAR));
1419c2c66affSColin Finck if (str)
1420c2c66affSColin Finck cert_name_to_str_with_indent(X509_ASN_ENCODING, 0, name,
1421c2c66affSColin Finck CERT_SIMPLE_NAME_STR, str, len);
1422c2c66affSColin Finck }
1423c2c66affSColin Finck return str;
1424c2c66affSColin Finck }
1425c2c66affSColin Finck
dump_alt_name_entry(const CERT_ALT_NAME_ENTRY * entry)1426c2c66affSColin Finck static void dump_alt_name_entry(const CERT_ALT_NAME_ENTRY *entry)
1427c2c66affSColin Finck {
1428c2c66affSColin Finck LPWSTR str;
1429c2c66affSColin Finck
1430c2c66affSColin Finck switch (entry->dwAltNameChoice)
1431c2c66affSColin Finck {
1432c2c66affSColin Finck case CERT_ALT_NAME_OTHER_NAME:
1433c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_OTHER_NAME, oid = %s\n",
1434c2c66affSColin Finck debugstr_a(entry->u.pOtherName->pszObjId));
1435c2c66affSColin Finck break;
1436c2c66affSColin Finck case CERT_ALT_NAME_RFC822_NAME:
1437c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_RFC822_NAME: %s\n",
1438c2c66affSColin Finck debugstr_w(entry->u.pwszRfc822Name));
1439c2c66affSColin Finck break;
1440c2c66affSColin Finck case CERT_ALT_NAME_DNS_NAME:
1441c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_DNS_NAME: %s\n",
1442c2c66affSColin Finck debugstr_w(entry->u.pwszDNSName));
1443c2c66affSColin Finck break;
1444c2c66affSColin Finck case CERT_ALT_NAME_DIRECTORY_NAME:
1445c2c66affSColin Finck str = name_value_to_str(&entry->u.DirectoryName);
1446c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_DIRECTORY_NAME: %s\n", debugstr_w(str));
1447c2c66affSColin Finck CryptMemFree(str);
1448c2c66affSColin Finck break;
1449c2c66affSColin Finck case CERT_ALT_NAME_URL:
1450c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_URL: %s\n", debugstr_w(entry->u.pwszURL));
1451c2c66affSColin Finck break;
1452c2c66affSColin Finck case CERT_ALT_NAME_IP_ADDRESS:
1453c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_IP_ADDRESS: %d bytes\n",
1454c2c66affSColin Finck entry->u.IPAddress.cbData);
1455c2c66affSColin Finck break;
1456c2c66affSColin Finck case CERT_ALT_NAME_REGISTERED_ID:
1457c2c66affSColin Finck TRACE_(chain)("CERT_ALT_NAME_REGISTERED_ID: %s\n",
1458c2c66affSColin Finck debugstr_a(entry->u.pszRegisteredID));
1459c2c66affSColin Finck break;
1460c2c66affSColin Finck default:
1461c2c66affSColin Finck TRACE_(chain)("dwAltNameChoice = %d\n", entry->dwAltNameChoice);
1462c2c66affSColin Finck }
1463c2c66affSColin Finck }
1464c2c66affSColin Finck
dump_alt_name(LPCSTR type,const CERT_EXTENSION * ext)1465c2c66affSColin Finck static void dump_alt_name(LPCSTR type, const CERT_EXTENSION *ext)
1466c2c66affSColin Finck {
1467c2c66affSColin Finck CERT_ALT_NAME_INFO *name;
1468c2c66affSColin Finck DWORD size;
1469c2c66affSColin Finck
1470c2c66affSColin Finck TRACE_(chain)("%s:\n", type);
1471c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME,
1472c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData,
1473c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &name, &size))
1474c2c66affSColin Finck {
1475c2c66affSColin Finck DWORD i;
1476c2c66affSColin Finck
1477c2c66affSColin Finck TRACE_(chain)("%d alt name entries:\n", name->cAltEntry);
1478c2c66affSColin Finck for (i = 0; i < name->cAltEntry; i++)
1479c2c66affSColin Finck dump_alt_name_entry(&name->rgAltEntry[i]);
1480c2c66affSColin Finck LocalFree(name);
1481c2c66affSColin Finck }
1482c2c66affSColin Finck }
1483c2c66affSColin Finck
dump_basic_constraints(const CERT_EXTENSION * ext)1484c2c66affSColin Finck static void dump_basic_constraints(const CERT_EXTENSION *ext)
1485c2c66affSColin Finck {
1486c2c66affSColin Finck CERT_BASIC_CONSTRAINTS_INFO *info;
1487c2c66affSColin Finck DWORD size = 0;
1488c2c66affSColin Finck
1489c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
1490c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
1491c2c66affSColin Finck NULL, &info, &size))
1492c2c66affSColin Finck {
1493c2c66affSColin Finck TRACE_(chain)("SubjectType: %02x\n", info->SubjectType.pbData[0]);
1494c2c66affSColin Finck TRACE_(chain)("%s path length constraint\n",
1495c2c66affSColin Finck info->fPathLenConstraint ? "has" : "doesn't have");
1496c2c66affSColin Finck TRACE_(chain)("path length=%d\n", info->dwPathLenConstraint);
1497c2c66affSColin Finck LocalFree(info);
1498c2c66affSColin Finck }
1499c2c66affSColin Finck }
1500c2c66affSColin Finck
dump_basic_constraints2(const CERT_EXTENSION * ext)1501c2c66affSColin Finck static void dump_basic_constraints2(const CERT_EXTENSION *ext)
1502c2c66affSColin Finck {
1503c2c66affSColin Finck CERT_BASIC_CONSTRAINTS2_INFO constraints;
1504c2c66affSColin Finck DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
1505c2c66affSColin Finck
1506c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING,
1507c2c66affSColin Finck szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
1508c2c66affSColin Finck 0, NULL, &constraints, &size))
1509c2c66affSColin Finck {
1510c2c66affSColin Finck TRACE_(chain)("basic constraints:\n");
1511c2c66affSColin Finck TRACE_(chain)("can%s be a CA\n", constraints.fCA ? "" : "not");
1512c2c66affSColin Finck TRACE_(chain)("%s path length constraint\n",
1513c2c66affSColin Finck constraints.fPathLenConstraint ? "has" : "doesn't have");
1514c2c66affSColin Finck TRACE_(chain)("path length=%d\n", constraints.dwPathLenConstraint);
1515c2c66affSColin Finck }
1516c2c66affSColin Finck }
1517c2c66affSColin Finck
dump_key_usage(const CERT_EXTENSION * ext)1518c2c66affSColin Finck static void dump_key_usage(const CERT_EXTENSION *ext)
1519c2c66affSColin Finck {
1520c2c66affSColin Finck CRYPT_BIT_BLOB usage;
1521c2c66affSColin Finck DWORD size = sizeof(usage);
1522c2c66affSColin Finck
1523c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_BITS, ext->Value.pbData,
1524c2c66affSColin Finck ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL, &usage, &size))
1525c2c66affSColin Finck {
1526c2c66affSColin Finck #define trace_usage_bit(bits, bit) \
1527c2c66affSColin Finck if ((bits) & (bit)) TRACE_(chain)("%s\n", #bit)
1528c2c66affSColin Finck if (usage.cbData)
1529c2c66affSColin Finck {
1530c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_DIGITAL_SIGNATURE_KEY_USAGE);
1531c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_NON_REPUDIATION_KEY_USAGE);
1532c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_KEY_ENCIPHERMENT_KEY_USAGE);
1533c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_DATA_ENCIPHERMENT_KEY_USAGE);
1534c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_KEY_AGREEMENT_KEY_USAGE);
1535c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_KEY_CERT_SIGN_KEY_USAGE);
1536c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_CRL_SIGN_KEY_USAGE);
1537c2c66affSColin Finck trace_usage_bit(usage.pbData[0], CERT_ENCIPHER_ONLY_KEY_USAGE);
1538c2c66affSColin Finck }
1539c2c66affSColin Finck #undef trace_usage_bit
1540c2c66affSColin Finck if (usage.cbData > 1 && usage.pbData[1] & CERT_DECIPHER_ONLY_KEY_USAGE)
1541c2c66affSColin Finck TRACE_(chain)("CERT_DECIPHER_ONLY_KEY_USAGE\n");
1542c2c66affSColin Finck }
1543c2c66affSColin Finck }
1544c2c66affSColin Finck
dump_general_subtree(const CERT_GENERAL_SUBTREE * subtree)1545c2c66affSColin Finck static void dump_general_subtree(const CERT_GENERAL_SUBTREE *subtree)
1546c2c66affSColin Finck {
1547c2c66affSColin Finck dump_alt_name_entry(&subtree->Base);
1548c2c66affSColin Finck TRACE_(chain)("dwMinimum = %d, fMaximum = %d, dwMaximum = %d\n",
1549c2c66affSColin Finck subtree->dwMinimum, subtree->fMaximum, subtree->dwMaximum);
1550c2c66affSColin Finck }
1551c2c66affSColin Finck
dump_name_constraints(const CERT_EXTENSION * ext)1552c2c66affSColin Finck static void dump_name_constraints(const CERT_EXTENSION *ext)
1553c2c66affSColin Finck {
1554c2c66affSColin Finck CERT_NAME_CONSTRAINTS_INFO *nameConstraints;
1555c2c66affSColin Finck DWORD size;
1556c2c66affSColin Finck
1557c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_CONSTRAINTS,
1558c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData,
1559c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &nameConstraints,
1560c2c66affSColin Finck &size))
1561c2c66affSColin Finck {
1562c2c66affSColin Finck DWORD i;
1563c2c66affSColin Finck
1564c2c66affSColin Finck TRACE_(chain)("%d permitted subtrees:\n",
1565c2c66affSColin Finck nameConstraints->cPermittedSubtree);
1566c2c66affSColin Finck for (i = 0; i < nameConstraints->cPermittedSubtree; i++)
1567c2c66affSColin Finck dump_general_subtree(&nameConstraints->rgPermittedSubtree[i]);
1568c2c66affSColin Finck TRACE_(chain)("%d excluded subtrees:\n",
1569c2c66affSColin Finck nameConstraints->cExcludedSubtree);
1570c2c66affSColin Finck for (i = 0; i < nameConstraints->cExcludedSubtree; i++)
1571c2c66affSColin Finck dump_general_subtree(&nameConstraints->rgExcludedSubtree[i]);
1572c2c66affSColin Finck LocalFree(nameConstraints);
1573c2c66affSColin Finck }
1574c2c66affSColin Finck }
1575c2c66affSColin Finck
dump_cert_policies(const CERT_EXTENSION * ext)1576c2c66affSColin Finck static void dump_cert_policies(const CERT_EXTENSION *ext)
1577c2c66affSColin Finck {
1578c2c66affSColin Finck CERT_POLICIES_INFO *policies;
1579c2c66affSColin Finck DWORD size;
1580c2c66affSColin Finck
1581c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_POLICIES,
1582c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1583c2c66affSColin Finck &policies, &size))
1584c2c66affSColin Finck {
1585c2c66affSColin Finck DWORD i, j;
1586c2c66affSColin Finck
1587c2c66affSColin Finck TRACE_(chain)("%d policies:\n", policies->cPolicyInfo);
1588c2c66affSColin Finck for (i = 0; i < policies->cPolicyInfo; i++)
1589c2c66affSColin Finck {
1590c2c66affSColin Finck TRACE_(chain)("policy identifier: %s\n",
1591c2c66affSColin Finck debugstr_a(policies->rgPolicyInfo[i].pszPolicyIdentifier));
1592c2c66affSColin Finck TRACE_(chain)("%d policy qualifiers:\n",
1593c2c66affSColin Finck policies->rgPolicyInfo[i].cPolicyQualifier);
1594c2c66affSColin Finck for (j = 0; j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
1595c2c66affSColin Finck TRACE_(chain)("%s\n", debugstr_a(
1596c2c66affSColin Finck policies->rgPolicyInfo[i].rgPolicyQualifier[j].
1597c2c66affSColin Finck pszPolicyQualifierId));
1598c2c66affSColin Finck }
1599c2c66affSColin Finck LocalFree(policies);
1600c2c66affSColin Finck }
1601c2c66affSColin Finck }
1602c2c66affSColin Finck
dump_enhanced_key_usage(const CERT_EXTENSION * ext)1603c2c66affSColin Finck static void dump_enhanced_key_usage(const CERT_EXTENSION *ext)
1604c2c66affSColin Finck {
1605c2c66affSColin Finck CERT_ENHKEY_USAGE *usage;
1606c2c66affSColin Finck DWORD size;
1607c2c66affSColin Finck
1608c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ENHANCED_KEY_USAGE,
1609c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1610c2c66affSColin Finck &usage, &size))
1611c2c66affSColin Finck {
1612c2c66affSColin Finck DWORD i;
1613c2c66affSColin Finck
1614c2c66affSColin Finck TRACE_(chain)("%d usages:\n", usage->cUsageIdentifier);
1615c2c66affSColin Finck for (i = 0; i < usage->cUsageIdentifier; i++)
1616c2c66affSColin Finck TRACE_(chain)("%s\n", usage->rgpszUsageIdentifier[i]);
1617c2c66affSColin Finck LocalFree(usage);
1618c2c66affSColin Finck }
1619c2c66affSColin Finck }
1620c2c66affSColin Finck
dump_netscape_cert_type(const CERT_EXTENSION * ext)1621c2c66affSColin Finck static void dump_netscape_cert_type(const CERT_EXTENSION *ext)
1622c2c66affSColin Finck {
1623c2c66affSColin Finck CRYPT_BIT_BLOB usage;
1624c2c66affSColin Finck DWORD size = sizeof(usage);
1625c2c66affSColin Finck
1626c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_BITS, ext->Value.pbData,
1627c2c66affSColin Finck ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL, &usage, &size))
1628c2c66affSColin Finck {
1629c2c66affSColin Finck #define trace_cert_type_bit(bits, bit) \
1630c2c66affSColin Finck if ((bits) & (bit)) TRACE_(chain)("%s\n", #bit)
1631c2c66affSColin Finck if (usage.cbData)
1632c2c66affSColin Finck {
1633c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0],
1634c2c66affSColin Finck NETSCAPE_SSL_CLIENT_AUTH_CERT_TYPE);
1635c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0],
1636c2c66affSColin Finck NETSCAPE_SSL_SERVER_AUTH_CERT_TYPE);
1637c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0], NETSCAPE_SMIME_CERT_TYPE);
1638c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0], NETSCAPE_SIGN_CERT_TYPE);
1639c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0], NETSCAPE_SSL_CA_CERT_TYPE);
1640c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0], NETSCAPE_SMIME_CA_CERT_TYPE);
1641c2c66affSColin Finck trace_cert_type_bit(usage.pbData[0], NETSCAPE_SIGN_CA_CERT_TYPE);
1642c2c66affSColin Finck }
1643c2c66affSColin Finck #undef trace_cert_type_bit
1644c2c66affSColin Finck }
1645c2c66affSColin Finck }
1646c2c66affSColin Finck
dump_extension(const CERT_EXTENSION * ext)1647c2c66affSColin Finck static void dump_extension(const CERT_EXTENSION *ext)
1648c2c66affSColin Finck {
1649c2c66affSColin Finck TRACE_(chain)("%s (%scritical)\n", debugstr_a(ext->pszObjId),
1650c2c66affSColin Finck ext->fCritical ? "" : "not ");
1651c2c66affSColin Finck if (!strcmp(ext->pszObjId, szOID_SUBJECT_ALT_NAME))
1652c2c66affSColin Finck dump_alt_name("subject alt name", ext);
1653c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_ISSUER_ALT_NAME))
1654c2c66affSColin Finck dump_alt_name("issuer alt name", ext);
1655c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_BASIC_CONSTRAINTS))
1656c2c66affSColin Finck dump_basic_constraints(ext);
1657c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_KEY_USAGE))
1658c2c66affSColin Finck dump_key_usage(ext);
1659c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_SUBJECT_ALT_NAME2))
1660c2c66affSColin Finck dump_alt_name("subject alt name 2", ext);
1661c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_ISSUER_ALT_NAME2))
1662c2c66affSColin Finck dump_alt_name("issuer alt name 2", ext);
1663c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_BASIC_CONSTRAINTS2))
1664c2c66affSColin Finck dump_basic_constraints2(ext);
1665c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_NAME_CONSTRAINTS))
1666c2c66affSColin Finck dump_name_constraints(ext);
1667c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_CERT_POLICIES))
1668c2c66affSColin Finck dump_cert_policies(ext);
1669c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_ENHANCED_KEY_USAGE))
1670c2c66affSColin Finck dump_enhanced_key_usage(ext);
1671c2c66affSColin Finck else if (!strcmp(ext->pszObjId, szOID_NETSCAPE_CERT_TYPE))
1672c2c66affSColin Finck dump_netscape_cert_type(ext);
1673c2c66affSColin Finck }
1674c2c66affSColin Finck
filetime_to_str(const FILETIME * time)1675c2c66affSColin Finck static LPCSTR filetime_to_str(const FILETIME *time)
1676c2c66affSColin Finck {
1677c2c66affSColin Finck char date[80];
1678c2c66affSColin Finck char dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
1679c2c66affSColin Finck SYSTEMTIME sysTime;
1680c2c66affSColin Finck
1681c2c66affSColin Finck if (!time) return "(null)";
1682c2c66affSColin Finck
1683*997d44c9SAmine Khaldi GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt, ARRAY_SIZE(dateFmt));
1684c2c66affSColin Finck FileTimeToSystemTime(time, &sysTime);
1685*997d44c9SAmine Khaldi GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date, ARRAY_SIZE(date));
1686c2c66affSColin Finck return wine_dbg_sprintf("%s", date);
1687c2c66affSColin Finck }
1688c2c66affSColin Finck
dump_element(PCCERT_CONTEXT cert)1689c2c66affSColin Finck static void dump_element(PCCERT_CONTEXT cert)
1690c2c66affSColin Finck {
1691c2c66affSColin Finck LPWSTR name = NULL;
1692c2c66affSColin Finck DWORD len, i;
1693c2c66affSColin Finck
1694c2c66affSColin Finck TRACE_(chain)("%p: version %d\n", cert, cert->pCertInfo->dwVersion);
1695c2c66affSColin Finck len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
1696c2c66affSColin Finck CERT_NAME_ISSUER_FLAG, NULL, NULL, 0);
1697c2c66affSColin Finck name = CryptMemAlloc(len * sizeof(WCHAR));
1698c2c66affSColin Finck if (name)
1699c2c66affSColin Finck {
1700c2c66affSColin Finck CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
1701c2c66affSColin Finck CERT_NAME_ISSUER_FLAG, NULL, name, len);
1702c2c66affSColin Finck TRACE_(chain)("issued by %s\n", debugstr_w(name));
1703c2c66affSColin Finck CryptMemFree(name);
1704c2c66affSColin Finck }
1705c2c66affSColin Finck len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
1706c2c66affSColin Finck NULL, 0);
1707c2c66affSColin Finck name = CryptMemAlloc(len * sizeof(WCHAR));
1708c2c66affSColin Finck if (name)
1709c2c66affSColin Finck {
1710c2c66affSColin Finck CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
1711c2c66affSColin Finck name, len);
1712c2c66affSColin Finck TRACE_(chain)("issued to %s\n", debugstr_w(name));
1713c2c66affSColin Finck CryptMemFree(name);
1714c2c66affSColin Finck }
1715c2c66affSColin Finck TRACE_(chain)("valid from %s to %s\n",
1716c2c66affSColin Finck filetime_to_str(&cert->pCertInfo->NotBefore),
1717c2c66affSColin Finck filetime_to_str(&cert->pCertInfo->NotAfter));
1718c2c66affSColin Finck TRACE_(chain)("%d extensions\n", cert->pCertInfo->cExtension);
1719c2c66affSColin Finck for (i = 0; i < cert->pCertInfo->cExtension; i++)
1720c2c66affSColin Finck dump_extension(&cert->pCertInfo->rgExtension[i]);
1721c2c66affSColin Finck }
1722c2c66affSColin Finck
CRYPT_KeyUsageValid(CertificateChainEngine * engine,PCCERT_CONTEXT cert,BOOL isRoot,BOOL isCA,DWORD index)1723c2c66affSColin Finck static BOOL CRYPT_KeyUsageValid(CertificateChainEngine *engine,
1724c2c66affSColin Finck PCCERT_CONTEXT cert, BOOL isRoot, BOOL isCA, DWORD index)
1725c2c66affSColin Finck {
1726c2c66affSColin Finck PCERT_EXTENSION ext;
1727c2c66affSColin Finck BOOL ret;
1728c2c66affSColin Finck BYTE usageBits = 0;
1729c2c66affSColin Finck
1730c2c66affSColin Finck ext = CertFindExtension(szOID_KEY_USAGE, cert->pCertInfo->cExtension,
1731c2c66affSColin Finck cert->pCertInfo->rgExtension);
1732c2c66affSColin Finck if (ext)
1733c2c66affSColin Finck {
1734c2c66affSColin Finck CRYPT_BIT_BLOB usage;
1735c2c66affSColin Finck DWORD size = sizeof(usage);
1736c2c66affSColin Finck
1737c2c66affSColin Finck ret = CryptDecodeObjectEx(cert->dwCertEncodingType, X509_BITS,
1738c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL,
1739c2c66affSColin Finck &usage, &size);
1740c2c66affSColin Finck if (!ret)
1741c2c66affSColin Finck return FALSE;
1742c2c66affSColin Finck else if (usage.cbData > 2)
1743c2c66affSColin Finck {
1744c2c66affSColin Finck /* The key usage extension only defines 9 bits => no more than 2
1745c2c66affSColin Finck * bytes are needed to encode all known usages.
1746c2c66affSColin Finck */
1747c2c66affSColin Finck return FALSE;
1748c2c66affSColin Finck }
1749c2c66affSColin Finck else
1750c2c66affSColin Finck {
1751c2c66affSColin Finck /* The only bit relevant to chain validation is the keyCertSign
1752c2c66affSColin Finck * bit, which is always in the least significant byte of the
1753c2c66affSColin Finck * key usage bits.
1754c2c66affSColin Finck */
1755c2c66affSColin Finck usageBits = usage.pbData[usage.cbData - 1];
1756c2c66affSColin Finck }
1757c2c66affSColin Finck }
1758c2c66affSColin Finck if (isCA)
1759c2c66affSColin Finck {
1760c2c66affSColin Finck if (!ext)
1761c2c66affSColin Finck {
1762c2c66affSColin Finck /* MS appears to violate RFC 5280, section 4.2.1.3 (Key Usage)
1763c2c66affSColin Finck * here. Quoting the RFC:
1764c2c66affSColin Finck * "This [key usage] extension MUST appear in certificates that
1765c2c66affSColin Finck * contain public keys that are used to validate digital signatures
1766c2c66affSColin Finck * on other public key certificates or CRLs."
1767c2c66affSColin Finck * MS appears to accept certs that do not contain key usage
1768c2c66affSColin Finck * extensions as CA certs. V1 and V2 certificates did not have
1769c2c66affSColin Finck * extensions, and many root certificates are V1 certificates, so
1770c2c66affSColin Finck * perhaps this is prudent. On the other hand, MS also accepts V3
1771c2c66affSColin Finck * certs without key usage extensions. Because some CAs, e.g.
1772c2c66affSColin Finck * Certum, also do not include key usage extensions in their
1773c2c66affSColin Finck * intermediate certificates, we are forced to accept V3
1774c2c66affSColin Finck * certificates without key usage extensions as well.
1775c2c66affSColin Finck */
1776c2c66affSColin Finck ret = TRUE;
1777c2c66affSColin Finck }
1778c2c66affSColin Finck else
1779c2c66affSColin Finck {
1780c2c66affSColin Finck if (!(usageBits & CERT_KEY_CERT_SIGN_KEY_USAGE))
1781c2c66affSColin Finck {
1782c2c66affSColin Finck WARN_(chain)("keyCertSign not asserted on a CA cert\n");
1783c2c66affSColin Finck ret = FALSE;
1784c2c66affSColin Finck }
1785c2c66affSColin Finck else
1786c2c66affSColin Finck ret = TRUE;
1787c2c66affSColin Finck }
1788c2c66affSColin Finck }
1789c2c66affSColin Finck else
1790c2c66affSColin Finck {
1791c2c66affSColin Finck if (ext && (usageBits & CERT_KEY_CERT_SIGN_KEY_USAGE))
1792c2c66affSColin Finck {
1793c2c66affSColin Finck WARN_(chain)("keyCertSign asserted on a non-CA cert\n");
1794c2c66affSColin Finck ret = FALSE;
1795c2c66affSColin Finck }
1796c2c66affSColin Finck else
1797c2c66affSColin Finck ret = TRUE;
1798c2c66affSColin Finck }
1799c2c66affSColin Finck return ret;
1800c2c66affSColin Finck }
1801c2c66affSColin Finck
CRYPT_CriticalExtensionsSupported(PCCERT_CONTEXT cert)1802c2c66affSColin Finck static BOOL CRYPT_CriticalExtensionsSupported(PCCERT_CONTEXT cert)
1803c2c66affSColin Finck {
1804c2c66affSColin Finck BOOL ret = TRUE;
1805c2c66affSColin Finck DWORD i;
1806c2c66affSColin Finck
1807c2c66affSColin Finck for (i = 0; ret && i < cert->pCertInfo->cExtension; i++)
1808c2c66affSColin Finck {
1809c2c66affSColin Finck if (cert->pCertInfo->rgExtension[i].fCritical)
1810c2c66affSColin Finck {
1811c2c66affSColin Finck LPCSTR oid = cert->pCertInfo->rgExtension[i].pszObjId;
1812c2c66affSColin Finck
1813c2c66affSColin Finck if (!strcmp(oid, szOID_BASIC_CONSTRAINTS))
1814c2c66affSColin Finck ret = TRUE;
1815c2c66affSColin Finck else if (!strcmp(oid, szOID_BASIC_CONSTRAINTS2))
1816c2c66affSColin Finck ret = TRUE;
1817c2c66affSColin Finck else if (!strcmp(oid, szOID_NAME_CONSTRAINTS))
1818c2c66affSColin Finck ret = TRUE;
1819c2c66affSColin Finck else if (!strcmp(oid, szOID_KEY_USAGE))
1820c2c66affSColin Finck ret = TRUE;
1821c2c66affSColin Finck else if (!strcmp(oid, szOID_SUBJECT_ALT_NAME))
1822c2c66affSColin Finck ret = TRUE;
1823c2c66affSColin Finck else if (!strcmp(oid, szOID_SUBJECT_ALT_NAME2))
1824c2c66affSColin Finck ret = TRUE;
1825c2c66affSColin Finck else if (!strcmp(oid, szOID_CERT_POLICIES))
1826c2c66affSColin Finck ret = TRUE;
1827c2c66affSColin Finck else if (!strcmp(oid, szOID_ENHANCED_KEY_USAGE))
1828c2c66affSColin Finck ret = TRUE;
1829c2c66affSColin Finck else
1830c2c66affSColin Finck {
1831c2c66affSColin Finck FIXME("unsupported critical extension %s\n",
1832c2c66affSColin Finck debugstr_a(oid));
1833c2c66affSColin Finck ret = FALSE;
1834c2c66affSColin Finck }
1835c2c66affSColin Finck }
1836c2c66affSColin Finck }
1837c2c66affSColin Finck return ret;
1838c2c66affSColin Finck }
1839c2c66affSColin Finck
CRYPT_IsCertVersionValid(PCCERT_CONTEXT cert)1840c2c66affSColin Finck static BOOL CRYPT_IsCertVersionValid(PCCERT_CONTEXT cert)
1841c2c66affSColin Finck {
1842c2c66affSColin Finck BOOL ret = TRUE;
1843c2c66affSColin Finck
1844c2c66affSColin Finck /* Checks whether the contents of the cert match the cert's version. */
1845c2c66affSColin Finck switch (cert->pCertInfo->dwVersion)
1846c2c66affSColin Finck {
1847c2c66affSColin Finck case CERT_V1:
1848c2c66affSColin Finck /* A V1 cert may not contain unique identifiers. See RFC 5280,
1849c2c66affSColin Finck * section 4.1.2.8:
1850c2c66affSColin Finck * "These fields MUST only appear if the version is 2 or 3 (Section
1851c2c66affSColin Finck * 4.1.2.1). These fields MUST NOT appear if the version is 1."
1852c2c66affSColin Finck */
1853c2c66affSColin Finck if (cert->pCertInfo->IssuerUniqueId.cbData ||
1854c2c66affSColin Finck cert->pCertInfo->SubjectUniqueId.cbData)
1855c2c66affSColin Finck ret = FALSE;
1856c2c66affSColin Finck /* A V1 cert may not contain extensions. See RFC 5280, section 4.1.2.9:
1857c2c66affSColin Finck * "This field MUST only appear if the version is 3 (Section 4.1.2.1)."
1858c2c66affSColin Finck */
1859c2c66affSColin Finck if (cert->pCertInfo->cExtension)
1860c2c66affSColin Finck ret = FALSE;
1861c2c66affSColin Finck break;
1862c2c66affSColin Finck case CERT_V2:
1863c2c66affSColin Finck /* A V2 cert may not contain extensions. See RFC 5280, section 4.1.2.9:
1864c2c66affSColin Finck * "This field MUST only appear if the version is 3 (Section 4.1.2.1)."
1865c2c66affSColin Finck */
1866c2c66affSColin Finck if (cert->pCertInfo->cExtension)
1867c2c66affSColin Finck ret = FALSE;
1868c2c66affSColin Finck break;
1869c2c66affSColin Finck case CERT_V3:
1870c2c66affSColin Finck /* Do nothing, all fields are allowed for V3 certs */
1871c2c66affSColin Finck break;
1872c2c66affSColin Finck default:
1873c2c66affSColin Finck WARN_(chain)("invalid cert version %d\n", cert->pCertInfo->dwVersion);
1874c2c66affSColin Finck ret = FALSE;
1875c2c66affSColin Finck }
1876c2c66affSColin Finck return ret;
1877c2c66affSColin Finck }
1878c2c66affSColin Finck
CRYPT_CheckSimpleChain(CertificateChainEngine * engine,PCERT_SIMPLE_CHAIN chain,LPFILETIME time)1879c2c66affSColin Finck static void CRYPT_CheckSimpleChain(CertificateChainEngine *engine,
1880c2c66affSColin Finck PCERT_SIMPLE_CHAIN chain, LPFILETIME time)
1881c2c66affSColin Finck {
1882c2c66affSColin Finck PCERT_CHAIN_ELEMENT rootElement = chain->rgpElement[chain->cElement - 1];
1883c2c66affSColin Finck int i;
1884c2c66affSColin Finck BOOL pathLengthConstraintViolated = FALSE;
1885c2c66affSColin Finck CERT_BASIC_CONSTRAINTS2_INFO constraints = { FALSE, FALSE, 0 };
1886*997d44c9SAmine Khaldi DWORD status;
1887c2c66affSColin Finck
1888c2c66affSColin Finck TRACE_(chain)("checking chain with %d elements for time %s\n",
1889c2c66affSColin Finck chain->cElement, filetime_to_str(time));
1890c2c66affSColin Finck for (i = chain->cElement - 1; i >= 0; i--)
1891c2c66affSColin Finck {
1892c2c66affSColin Finck BOOL isRoot;
1893c2c66affSColin Finck
1894c2c66affSColin Finck if (TRACE_ON(chain))
1895c2c66affSColin Finck dump_element(chain->rgpElement[i]->pCertContext);
1896c2c66affSColin Finck if (i == chain->cElement - 1)
1897c2c66affSColin Finck isRoot = CRYPT_IsCertificateSelfSigned(
1898*997d44c9SAmine Khaldi chain->rgpElement[i]->pCertContext);
1899c2c66affSColin Finck else
1900c2c66affSColin Finck isRoot = FALSE;
1901c2c66affSColin Finck if (!CRYPT_IsCertVersionValid(chain->rgpElement[i]->pCertContext))
1902c2c66affSColin Finck {
1903c2c66affSColin Finck /* MS appears to accept certs whose versions don't match their
1904c2c66affSColin Finck * contents, so there isn't an appropriate error code.
1905c2c66affSColin Finck */
1906c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1907c2c66affSColin Finck CERT_TRUST_INVALID_EXTENSION;
1908c2c66affSColin Finck }
1909c2c66affSColin Finck if (CertVerifyTimeValidity(time,
1910c2c66affSColin Finck chain->rgpElement[i]->pCertContext->pCertInfo) != 0)
1911c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1912c2c66affSColin Finck CERT_TRUST_IS_NOT_TIME_VALID;
1913c2c66affSColin Finck if (i != 0)
1914c2c66affSColin Finck {
1915c2c66affSColin Finck /* Check the signature of the cert this issued */
1916c2c66affSColin Finck if (!CryptVerifyCertificateSignatureEx(0, X509_ASN_ENCODING,
1917c2c66affSColin Finck CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT,
1918c2c66affSColin Finck (void *)chain->rgpElement[i - 1]->pCertContext,
1919c2c66affSColin Finck CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT,
1920c2c66affSColin Finck (void *)chain->rgpElement[i]->pCertContext, 0, NULL))
1921c2c66affSColin Finck chain->rgpElement[i - 1]->TrustStatus.dwErrorStatus |=
1922c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID;
1923c2c66affSColin Finck /* Once a path length constraint has been violated, every remaining
1924c2c66affSColin Finck * CA cert's basic constraints is considered invalid.
1925c2c66affSColin Finck */
1926c2c66affSColin Finck if (pathLengthConstraintViolated)
1927c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1928c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
1929c2c66affSColin Finck else if (!CRYPT_CheckBasicConstraintsForCA(engine,
1930c2c66affSColin Finck chain->rgpElement[i]->pCertContext, &constraints, i - 1, isRoot,
1931c2c66affSColin Finck &pathLengthConstraintViolated))
1932c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1933c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
1934c2c66affSColin Finck else if (constraints.fPathLenConstraint &&
1935c2c66affSColin Finck constraints.dwPathLenConstraint)
1936c2c66affSColin Finck {
1937c2c66affSColin Finck /* This one's valid - decrement max length */
1938c2c66affSColin Finck constraints.dwPathLenConstraint--;
1939c2c66affSColin Finck }
1940c2c66affSColin Finck }
1941c2c66affSColin Finck else
1942c2c66affSColin Finck {
1943c2c66affSColin Finck /* Check whether end cert has a basic constraints extension */
1944c2c66affSColin Finck if (!CRYPT_DecodeBasicConstraints(
1945c2c66affSColin Finck chain->rgpElement[i]->pCertContext, &constraints, FALSE))
1946c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1947c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
1948c2c66affSColin Finck }
1949c2c66affSColin Finck if (!CRYPT_KeyUsageValid(engine, chain->rgpElement[i]->pCertContext,
1950c2c66affSColin Finck isRoot, constraints.fCA, i))
1951c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1952c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
1953c2c66affSColin Finck if (CRYPT_IsSimpleChainCyclic(chain))
1954c2c66affSColin Finck {
1955c2c66affSColin Finck /* If the chain is cyclic, then the path length constraints
1956c2c66affSColin Finck * are violated, because the chain is infinitely long.
1957c2c66affSColin Finck */
1958c2c66affSColin Finck pathLengthConstraintViolated = TRUE;
1959c2c66affSColin Finck chain->TrustStatus.dwErrorStatus |=
1960c2c66affSColin Finck CERT_TRUST_IS_PARTIAL_CHAIN |
1961c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
1962c2c66affSColin Finck }
1963c2c66affSColin Finck /* Check whether every critical extension is supported */
1964c2c66affSColin Finck if (!CRYPT_CriticalExtensionsSupported(
1965c2c66affSColin Finck chain->rgpElement[i]->pCertContext))
1966c2c66affSColin Finck chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
1967c2c66affSColin Finck CERT_TRUST_INVALID_EXTENSION |
1968c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT;
1969c2c66affSColin Finck CRYPT_CombineTrustStatus(&chain->TrustStatus,
1970c2c66affSColin Finck &chain->rgpElement[i]->TrustStatus);
1971c2c66affSColin Finck }
1972c2c66affSColin Finck CRYPT_CheckChainNameConstraints(chain);
1973c2c66affSColin Finck CRYPT_CheckChainPolicies(chain);
1974*997d44c9SAmine Khaldi if ((status = CRYPT_IsCertificateSelfSigned(rootElement->pCertContext)))
1975c2c66affSColin Finck {
1976*997d44c9SAmine Khaldi rootElement->TrustStatus.dwInfoStatus |= status;
1977c2c66affSColin Finck CRYPT_CheckRootCert(engine->hRoot, rootElement);
1978c2c66affSColin Finck }
1979c2c66affSColin Finck CRYPT_CombineTrustStatus(&chain->TrustStatus, &rootElement->TrustStatus);
1980c2c66affSColin Finck }
1981c2c66affSColin Finck
CRYPT_FindIssuer(const CertificateChainEngine * engine,const CERT_CONTEXT * cert,HCERTSTORE store,DWORD type,void * para,DWORD flags,PCCERT_CONTEXT prev_issuer)1982c2c66affSColin Finck static PCCERT_CONTEXT CRYPT_FindIssuer(const CertificateChainEngine *engine, const CERT_CONTEXT *cert,
1983c2c66affSColin Finck HCERTSTORE store, DWORD type, void *para, DWORD flags, PCCERT_CONTEXT prev_issuer)
1984c2c66affSColin Finck {
1985c2c66affSColin Finck CRYPT_URL_ARRAY *urls;
1986c2c66affSColin Finck PCCERT_CONTEXT issuer;
1987c2c66affSColin Finck DWORD size;
1988c2c66affSColin Finck BOOL res;
1989c2c66affSColin Finck
1990c2c66affSColin Finck issuer = CertFindCertificateInStore(store, cert->dwCertEncodingType, 0, type, para, prev_issuer);
1991c2c66affSColin Finck if(issuer) {
1992c2c66affSColin Finck TRACE("Found in store %p\n", issuer);
1993c2c66affSColin Finck return issuer;
1994c2c66affSColin Finck }
1995c2c66affSColin Finck
1996c2c66affSColin Finck /* FIXME: For alternate issuers, we don't search world store nor try to retrieve issuer from URL.
1997c2c66affSColin Finck * This needs more tests.
1998c2c66affSColin Finck */
1999c2c66affSColin Finck if(prev_issuer)
2000c2c66affSColin Finck return NULL;
2001c2c66affSColin Finck
2002c2c66affSColin Finck if(engine->hWorld) {
2003c2c66affSColin Finck issuer = CertFindCertificateInStore(engine->hWorld, cert->dwCertEncodingType, 0, type, para, NULL);
2004c2c66affSColin Finck if(issuer) {
2005c2c66affSColin Finck TRACE("Found in world %p\n", issuer);
2006c2c66affSColin Finck return issuer;
2007c2c66affSColin Finck }
2008c2c66affSColin Finck }
2009c2c66affSColin Finck
2010c2c66affSColin Finck res = CryptGetObjectUrl(URL_OID_CERTIFICATE_ISSUER, (void*)cert, 0, NULL, &size, NULL, NULL, NULL);
2011c2c66affSColin Finck if(!res)
2012c2c66affSColin Finck return NULL;
2013c2c66affSColin Finck
2014c2c66affSColin Finck urls = HeapAlloc(GetProcessHeap(), 0, size);
2015c2c66affSColin Finck if(!urls)
2016c2c66affSColin Finck return NULL;
2017c2c66affSColin Finck
2018c2c66affSColin Finck res = CryptGetObjectUrl(URL_OID_CERTIFICATE_ISSUER, (void*)cert, 0, urls, &size, NULL, NULL, NULL);
2019c2c66affSColin Finck if(res)
2020c2c66affSColin Finck {
2021c2c66affSColin Finck CERT_CONTEXT *new_cert;
2022c2c66affSColin Finck HCERTSTORE new_store;
2023c2c66affSColin Finck unsigned i;
2024c2c66affSColin Finck
2025c2c66affSColin Finck for(i=0; i < urls->cUrl; i++)
2026c2c66affSColin Finck {
2027c2c66affSColin Finck TRACE("Trying URL %s\n", debugstr_w(urls->rgwszUrl[i]));
2028c2c66affSColin Finck
2029c2c66affSColin Finck res = CryptRetrieveObjectByUrlW(urls->rgwszUrl[i], CONTEXT_OID_CERTIFICATE,
2030c2c66affSColin Finck (flags & CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL) ? CRYPT_CACHE_ONLY_RETRIEVAL : CRYPT_AIA_RETRIEVAL,
2031c2c66affSColin Finck 0, (void**)&new_cert, NULL, NULL, NULL, NULL);
2032c2c66affSColin Finck if(!res)
2033c2c66affSColin Finck {
2034c2c66affSColin Finck TRACE("CryptRetrieveObjectByUrlW failed: %u\n", GetLastError());
2035c2c66affSColin Finck continue;
2036c2c66affSColin Finck }
2037c2c66affSColin Finck
2038c2c66affSColin Finck /* FIXME: Use new_cert->hCertStore once cert ref count bug is fixed. */
2039c2c66affSColin Finck new_store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2040c2c66affSColin Finck CertAddCertificateContextToStore(new_store, new_cert, CERT_STORE_ADD_NEW, NULL);
2041c2c66affSColin Finck issuer = CertFindCertificateInStore(new_store, cert->dwCertEncodingType, 0, type, para, NULL);
2042c2c66affSColin Finck CertFreeCertificateContext(new_cert);
2043c2c66affSColin Finck CertCloseStore(new_store, 0);
2044c2c66affSColin Finck if(issuer)
2045c2c66affSColin Finck {
2046c2c66affSColin Finck TRACE("Found downloaded issuer %p\n", issuer);
2047c2c66affSColin Finck break;
2048c2c66affSColin Finck }
2049c2c66affSColin Finck }
2050c2c66affSColin Finck }
2051c2c66affSColin Finck
2052c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, urls);
2053c2c66affSColin Finck return issuer;
2054c2c66affSColin Finck }
2055c2c66affSColin Finck
CRYPT_GetIssuer(const CertificateChainEngine * engine,HCERTSTORE store,PCCERT_CONTEXT subject,PCCERT_CONTEXT prevIssuer,DWORD flags,DWORD * infoStatus)2056c2c66affSColin Finck static PCCERT_CONTEXT CRYPT_GetIssuer(const CertificateChainEngine *engine,
2057c2c66affSColin Finck HCERTSTORE store, PCCERT_CONTEXT subject, PCCERT_CONTEXT prevIssuer,
2058c2c66affSColin Finck DWORD flags, DWORD *infoStatus)
2059c2c66affSColin Finck {
2060c2c66affSColin Finck PCCERT_CONTEXT issuer = NULL;
2061c2c66affSColin Finck PCERT_EXTENSION ext;
2062c2c66affSColin Finck DWORD size;
2063c2c66affSColin Finck
2064c2c66affSColin Finck *infoStatus = 0;
2065c2c66affSColin Finck if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
2066c2c66affSColin Finck subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
2067c2c66affSColin Finck {
2068c2c66affSColin Finck CERT_AUTHORITY_KEY_ID_INFO *info;
2069c2c66affSColin Finck BOOL ret;
2070c2c66affSColin Finck
2071c2c66affSColin Finck ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
2072c2c66affSColin Finck X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
2073c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
2074c2c66affSColin Finck &info, &size);
2075c2c66affSColin Finck if (ret)
2076c2c66affSColin Finck {
2077c2c66affSColin Finck CERT_ID id;
2078c2c66affSColin Finck
2079c2c66affSColin Finck if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
2080c2c66affSColin Finck {
2081c2c66affSColin Finck id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
2082c2c66affSColin Finck memcpy(&id.u.IssuerSerialNumber.Issuer, &info->CertIssuer,
2083c2c66affSColin Finck sizeof(CERT_NAME_BLOB));
2084c2c66affSColin Finck memcpy(&id.u.IssuerSerialNumber.SerialNumber,
2085c2c66affSColin Finck &info->CertSerialNumber, sizeof(CRYPT_INTEGER_BLOB));
2086c2c66affSColin Finck
2087c2c66affSColin Finck issuer = CRYPT_FindIssuer(engine, subject, store, CERT_FIND_CERT_ID, &id, flags, prevIssuer);
2088c2c66affSColin Finck if (issuer)
2089c2c66affSColin Finck {
2090c2c66affSColin Finck TRACE_(chain)("issuer found by issuer/serial number\n");
2091c2c66affSColin Finck *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
2092c2c66affSColin Finck }
2093c2c66affSColin Finck }
2094c2c66affSColin Finck else if (info->KeyId.cbData)
2095c2c66affSColin Finck {
2096c2c66affSColin Finck id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
2097c2c66affSColin Finck
2098c2c66affSColin Finck memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
2099c2c66affSColin Finck issuer = CRYPT_FindIssuer(engine, subject, store, CERT_FIND_CERT_ID, &id, flags, prevIssuer);
2100c2c66affSColin Finck if (issuer)
2101c2c66affSColin Finck {
2102c2c66affSColin Finck TRACE_(chain)("issuer found by key id\n");
2103c2c66affSColin Finck *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
2104c2c66affSColin Finck }
2105c2c66affSColin Finck }
2106c2c66affSColin Finck LocalFree(info);
2107c2c66affSColin Finck }
2108c2c66affSColin Finck }
2109c2c66affSColin Finck else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
2110c2c66affSColin Finck subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
2111c2c66affSColin Finck {
2112c2c66affSColin Finck CERT_AUTHORITY_KEY_ID2_INFO *info;
2113c2c66affSColin Finck BOOL ret;
2114c2c66affSColin Finck
2115c2c66affSColin Finck ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
2116c2c66affSColin Finck X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
2117c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
2118c2c66affSColin Finck &info, &size);
2119c2c66affSColin Finck if (ret)
2120c2c66affSColin Finck {
2121c2c66affSColin Finck CERT_ID id;
2122c2c66affSColin Finck
2123c2c66affSColin Finck if (info->AuthorityCertIssuer.cAltEntry &&
2124c2c66affSColin Finck info->AuthorityCertSerialNumber.cbData)
2125c2c66affSColin Finck {
2126c2c66affSColin Finck PCERT_ALT_NAME_ENTRY directoryName = NULL;
2127c2c66affSColin Finck DWORD i;
2128c2c66affSColin Finck
2129c2c66affSColin Finck for (i = 0; !directoryName &&
2130c2c66affSColin Finck i < info->AuthorityCertIssuer.cAltEntry; i++)
2131c2c66affSColin Finck if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
2132c2c66affSColin Finck == CERT_ALT_NAME_DIRECTORY_NAME)
2133c2c66affSColin Finck directoryName =
2134c2c66affSColin Finck &info->AuthorityCertIssuer.rgAltEntry[i];
2135c2c66affSColin Finck if (directoryName)
2136c2c66affSColin Finck {
2137c2c66affSColin Finck id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
2138c2c66affSColin Finck memcpy(&id.u.IssuerSerialNumber.Issuer,
2139c2c66affSColin Finck &directoryName->u.DirectoryName, sizeof(CERT_NAME_BLOB));
2140c2c66affSColin Finck memcpy(&id.u.IssuerSerialNumber.SerialNumber,
2141c2c66affSColin Finck &info->AuthorityCertSerialNumber,
2142c2c66affSColin Finck sizeof(CRYPT_INTEGER_BLOB));
2143c2c66affSColin Finck
2144c2c66affSColin Finck issuer = CRYPT_FindIssuer(engine, subject, store, CERT_FIND_CERT_ID, &id, flags, prevIssuer);
2145c2c66affSColin Finck if (issuer)
2146c2c66affSColin Finck {
2147c2c66affSColin Finck TRACE_(chain)("issuer found by directory name\n");
2148c2c66affSColin Finck *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
2149c2c66affSColin Finck }
2150c2c66affSColin Finck }
2151c2c66affSColin Finck else
2152c2c66affSColin Finck FIXME("no supported name type in authority key id2\n");
2153c2c66affSColin Finck }
2154c2c66affSColin Finck else if (info->KeyId.cbData)
2155c2c66affSColin Finck {
2156c2c66affSColin Finck id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
2157c2c66affSColin Finck memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
2158c2c66affSColin Finck issuer = CRYPT_FindIssuer(engine, subject, store, CERT_FIND_CERT_ID, &id, flags, prevIssuer);
2159c2c66affSColin Finck if (issuer)
2160c2c66affSColin Finck {
2161c2c66affSColin Finck TRACE_(chain)("issuer found by key id\n");
2162c2c66affSColin Finck *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
2163c2c66affSColin Finck }
2164c2c66affSColin Finck }
2165c2c66affSColin Finck LocalFree(info);
2166c2c66affSColin Finck }
2167c2c66affSColin Finck }
2168c2c66affSColin Finck else
2169c2c66affSColin Finck {
2170c2c66affSColin Finck issuer = CRYPT_FindIssuer(engine, subject, store, CERT_FIND_SUBJECT_NAME,
2171c2c66affSColin Finck &subject->pCertInfo->Issuer, flags, prevIssuer);
2172c2c66affSColin Finck TRACE_(chain)("issuer found by name\n");
2173c2c66affSColin Finck *infoStatus = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
2174c2c66affSColin Finck }
2175c2c66affSColin Finck return issuer;
2176c2c66affSColin Finck }
2177c2c66affSColin Finck
2178c2c66affSColin Finck /* Builds a simple chain by finding an issuer for the last cert in the chain,
2179c2c66affSColin Finck * until reaching a self-signed cert, or until no issuer can be found.
2180c2c66affSColin Finck */
CRYPT_BuildSimpleChain(const CertificateChainEngine * engine,HCERTSTORE world,DWORD flags,PCERT_SIMPLE_CHAIN chain)2181c2c66affSColin Finck static BOOL CRYPT_BuildSimpleChain(const CertificateChainEngine *engine,
2182c2c66affSColin Finck HCERTSTORE world, DWORD flags, PCERT_SIMPLE_CHAIN chain)
2183c2c66affSColin Finck {
2184c2c66affSColin Finck BOOL ret = TRUE;
2185c2c66affSColin Finck PCCERT_CONTEXT cert = chain->rgpElement[chain->cElement - 1]->pCertContext;
2186c2c66affSColin Finck
2187c2c66affSColin Finck while (ret && !CRYPT_IsSimpleChainCyclic(chain) &&
2188*997d44c9SAmine Khaldi !CRYPT_IsCertificateSelfSigned(cert))
2189c2c66affSColin Finck {
2190c2c66affSColin Finck PCCERT_CONTEXT issuer = CRYPT_GetIssuer(engine, world, cert, NULL, flags,
2191c2c66affSColin Finck &chain->rgpElement[chain->cElement - 1]->TrustStatus.dwInfoStatus);
2192c2c66affSColin Finck
2193c2c66affSColin Finck if (issuer)
2194c2c66affSColin Finck {
2195c2c66affSColin Finck ret = CRYPT_AddCertToSimpleChain(engine, chain, issuer,
2196c2c66affSColin Finck chain->rgpElement[chain->cElement - 1]->TrustStatus.dwInfoStatus);
2197c2c66affSColin Finck /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it to
2198c2c66affSColin Finck * close the enumeration that found it
2199c2c66affSColin Finck */
2200c2c66affSColin Finck CertFreeCertificateContext(issuer);
2201c2c66affSColin Finck cert = issuer;
2202c2c66affSColin Finck }
2203c2c66affSColin Finck else
2204c2c66affSColin Finck {
2205c2c66affSColin Finck TRACE_(chain)("Couldn't find issuer, halting chain creation\n");
2206c2c66affSColin Finck chain->TrustStatus.dwErrorStatus |= CERT_TRUST_IS_PARTIAL_CHAIN;
2207c2c66affSColin Finck break;
2208c2c66affSColin Finck }
2209c2c66affSColin Finck }
2210c2c66affSColin Finck return ret;
2211c2c66affSColin Finck }
2212c2c66affSColin Finck
debugstr_filetime(LPFILETIME pTime)2213c2c66affSColin Finck static LPCSTR debugstr_filetime(LPFILETIME pTime)
2214c2c66affSColin Finck {
2215c2c66affSColin Finck if (!pTime)
2216c2c66affSColin Finck return "(nil)";
2217c2c66affSColin Finck return wine_dbg_sprintf("%p (%s)", pTime, filetime_to_str(pTime));
2218c2c66affSColin Finck }
2219c2c66affSColin Finck
CRYPT_GetSimpleChainForCert(CertificateChainEngine * engine,HCERTSTORE world,PCCERT_CONTEXT cert,LPFILETIME pTime,DWORD flags,PCERT_SIMPLE_CHAIN * ppChain)2220c2c66affSColin Finck static BOOL CRYPT_GetSimpleChainForCert(CertificateChainEngine *engine,
2221c2c66affSColin Finck HCERTSTORE world, PCCERT_CONTEXT cert, LPFILETIME pTime, DWORD flags,
2222c2c66affSColin Finck PCERT_SIMPLE_CHAIN *ppChain)
2223c2c66affSColin Finck {
2224c2c66affSColin Finck BOOL ret = FALSE;
2225c2c66affSColin Finck PCERT_SIMPLE_CHAIN chain;
2226c2c66affSColin Finck
2227c2c66affSColin Finck TRACE("(%p, %p, %p, %s)\n", engine, world, cert, debugstr_filetime(pTime));
2228c2c66affSColin Finck
2229c2c66affSColin Finck chain = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
2230c2c66affSColin Finck if (chain)
2231c2c66affSColin Finck {
2232c2c66affSColin Finck memset(chain, 0, sizeof(CERT_SIMPLE_CHAIN));
2233c2c66affSColin Finck chain->cbSize = sizeof(CERT_SIMPLE_CHAIN);
2234c2c66affSColin Finck ret = CRYPT_AddCertToSimpleChain(engine, chain, cert, 0);
2235c2c66affSColin Finck if (ret)
2236c2c66affSColin Finck {
2237c2c66affSColin Finck ret = CRYPT_BuildSimpleChain(engine, world, flags, chain);
2238c2c66affSColin Finck if (ret)
2239c2c66affSColin Finck CRYPT_CheckSimpleChain(engine, chain, pTime);
2240c2c66affSColin Finck }
2241c2c66affSColin Finck if (!ret)
2242c2c66affSColin Finck {
2243c2c66affSColin Finck CRYPT_FreeSimpleChain(chain);
2244c2c66affSColin Finck chain = NULL;
2245c2c66affSColin Finck }
2246c2c66affSColin Finck *ppChain = chain;
2247c2c66affSColin Finck }
2248c2c66affSColin Finck return ret;
2249c2c66affSColin Finck }
2250c2c66affSColin Finck
CRYPT_BuildCandidateChainFromCert(CertificateChainEngine * engine,PCCERT_CONTEXT cert,LPFILETIME pTime,HCERTSTORE hAdditionalStore,DWORD flags,CertificateChain ** ppChain)2251c2c66affSColin Finck static BOOL CRYPT_BuildCandidateChainFromCert(CertificateChainEngine *engine,
2252c2c66affSColin Finck PCCERT_CONTEXT cert, LPFILETIME pTime, HCERTSTORE hAdditionalStore, DWORD flags,
2253c2c66affSColin Finck CertificateChain **ppChain)
2254c2c66affSColin Finck {
2255c2c66affSColin Finck PCERT_SIMPLE_CHAIN simpleChain = NULL;
2256c2c66affSColin Finck HCERTSTORE world;
2257c2c66affSColin Finck BOOL ret;
2258c2c66affSColin Finck
2259c2c66affSColin Finck world = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
2260c2c66affSColin Finck CERT_STORE_CREATE_NEW_FLAG, NULL);
2261c2c66affSColin Finck CertAddStoreToCollection(world, engine->hWorld, 0, 0);
2262c2c66affSColin Finck if (hAdditionalStore)
2263c2c66affSColin Finck CertAddStoreToCollection(world, hAdditionalStore, 0, 0);
2264c2c66affSColin Finck /* FIXME: only simple chains are supported for now, as CTLs aren't
2265c2c66affSColin Finck * supported yet.
2266c2c66affSColin Finck */
2267c2c66affSColin Finck if ((ret = CRYPT_GetSimpleChainForCert(engine, world, cert, pTime, flags, &simpleChain)))
2268c2c66affSColin Finck {
2269c2c66affSColin Finck CertificateChain *chain = CryptMemAlloc(sizeof(CertificateChain));
2270c2c66affSColin Finck
2271c2c66affSColin Finck if (chain)
2272c2c66affSColin Finck {
2273c2c66affSColin Finck chain->ref = 1;
2274c2c66affSColin Finck chain->world = world;
2275c2c66affSColin Finck chain->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
2276c2c66affSColin Finck chain->context.TrustStatus = simpleChain->TrustStatus;
2277c2c66affSColin Finck chain->context.cChain = 1;
2278c2c66affSColin Finck chain->context.rgpChain = CryptMemAlloc(sizeof(PCERT_SIMPLE_CHAIN));
2279c2c66affSColin Finck chain->context.rgpChain[0] = simpleChain;
2280c2c66affSColin Finck chain->context.cLowerQualityChainContext = 0;
2281c2c66affSColin Finck chain->context.rgpLowerQualityChainContext = NULL;
2282c2c66affSColin Finck chain->context.fHasRevocationFreshnessTime = FALSE;
2283c2c66affSColin Finck chain->context.dwRevocationFreshnessTime = 0;
2284c2c66affSColin Finck }
2285c2c66affSColin Finck else
2286c2c66affSColin Finck {
2287c2c66affSColin Finck CRYPT_FreeSimpleChain(simpleChain);
2288c2c66affSColin Finck ret = FALSE;
2289c2c66affSColin Finck }
2290c2c66affSColin Finck *ppChain = chain;
2291c2c66affSColin Finck }
2292c2c66affSColin Finck return ret;
2293c2c66affSColin Finck }
2294c2c66affSColin Finck
2295c2c66affSColin Finck /* Makes and returns a copy of chain, up to and including element iElement. */
CRYPT_CopySimpleChainToElement(const CERT_SIMPLE_CHAIN * chain,DWORD iElement)2296c2c66affSColin Finck static PCERT_SIMPLE_CHAIN CRYPT_CopySimpleChainToElement(
2297c2c66affSColin Finck const CERT_SIMPLE_CHAIN *chain, DWORD iElement)
2298c2c66affSColin Finck {
2299c2c66affSColin Finck PCERT_SIMPLE_CHAIN copy = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
2300c2c66affSColin Finck
2301c2c66affSColin Finck if (copy)
2302c2c66affSColin Finck {
2303c2c66affSColin Finck memset(copy, 0, sizeof(CERT_SIMPLE_CHAIN));
2304c2c66affSColin Finck copy->cbSize = sizeof(CERT_SIMPLE_CHAIN);
2305c2c66affSColin Finck copy->rgpElement =
2306c2c66affSColin Finck CryptMemAlloc((iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
2307c2c66affSColin Finck if (copy->rgpElement)
2308c2c66affSColin Finck {
2309c2c66affSColin Finck DWORD i;
2310c2c66affSColin Finck BOOL ret = TRUE;
2311c2c66affSColin Finck
2312c2c66affSColin Finck memset(copy->rgpElement, 0,
2313c2c66affSColin Finck (iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
2314c2c66affSColin Finck for (i = 0; ret && i <= iElement; i++)
2315c2c66affSColin Finck {
2316c2c66affSColin Finck PCERT_CHAIN_ELEMENT element =
2317c2c66affSColin Finck CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT));
2318c2c66affSColin Finck
2319c2c66affSColin Finck if (element)
2320c2c66affSColin Finck {
2321c2c66affSColin Finck *element = *chain->rgpElement[i];
2322c2c66affSColin Finck element->pCertContext = CertDuplicateCertificateContext(
2323c2c66affSColin Finck chain->rgpElement[i]->pCertContext);
2324c2c66affSColin Finck /* Reset the trust status of the copied element, it'll get
2325c2c66affSColin Finck * rechecked after the new chain is done.
2326c2c66affSColin Finck */
2327c2c66affSColin Finck memset(&element->TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
2328c2c66affSColin Finck copy->rgpElement[copy->cElement++] = element;
2329c2c66affSColin Finck }
2330c2c66affSColin Finck else
2331c2c66affSColin Finck ret = FALSE;
2332c2c66affSColin Finck }
2333c2c66affSColin Finck if (!ret)
2334c2c66affSColin Finck {
2335c2c66affSColin Finck for (i = 0; i <= iElement; i++)
2336c2c66affSColin Finck CryptMemFree(copy->rgpElement[i]);
2337c2c66affSColin Finck CryptMemFree(copy->rgpElement);
2338c2c66affSColin Finck CryptMemFree(copy);
2339c2c66affSColin Finck copy = NULL;
2340c2c66affSColin Finck }
2341c2c66affSColin Finck }
2342c2c66affSColin Finck else
2343c2c66affSColin Finck {
2344c2c66affSColin Finck CryptMemFree(copy);
2345c2c66affSColin Finck copy = NULL;
2346c2c66affSColin Finck }
2347c2c66affSColin Finck }
2348c2c66affSColin Finck return copy;
2349c2c66affSColin Finck }
2350c2c66affSColin Finck
CRYPT_FreeLowerQualityChains(CertificateChain * chain)2351c2c66affSColin Finck static void CRYPT_FreeLowerQualityChains(CertificateChain *chain)
2352c2c66affSColin Finck {
2353c2c66affSColin Finck DWORD i;
2354c2c66affSColin Finck
2355c2c66affSColin Finck for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
2356c2c66affSColin Finck CertFreeCertificateChain(chain->context.rgpLowerQualityChainContext[i]);
2357c2c66affSColin Finck CryptMemFree(chain->context.rgpLowerQualityChainContext);
2358c2c66affSColin Finck chain->context.cLowerQualityChainContext = 0;
2359c2c66affSColin Finck chain->context.rgpLowerQualityChainContext = NULL;
2360c2c66affSColin Finck }
2361c2c66affSColin Finck
CRYPT_FreeChainContext(CertificateChain * chain)2362c2c66affSColin Finck static void CRYPT_FreeChainContext(CertificateChain *chain)
2363c2c66affSColin Finck {
2364c2c66affSColin Finck DWORD i;
2365c2c66affSColin Finck
2366c2c66affSColin Finck CRYPT_FreeLowerQualityChains(chain);
2367c2c66affSColin Finck for (i = 0; i < chain->context.cChain; i++)
2368c2c66affSColin Finck CRYPT_FreeSimpleChain(chain->context.rgpChain[i]);
2369c2c66affSColin Finck CryptMemFree(chain->context.rgpChain);
2370c2c66affSColin Finck CertCloseStore(chain->world, 0);
2371c2c66affSColin Finck CryptMemFree(chain);
2372c2c66affSColin Finck }
2373c2c66affSColin Finck
2374c2c66affSColin Finck /* Makes and returns a copy of chain, up to and including element iElement of
2375c2c66affSColin Finck * simple chain iChain.
2376c2c66affSColin Finck */
CRYPT_CopyChainToElement(CertificateChain * chain,DWORD iChain,DWORD iElement)2377c2c66affSColin Finck static CertificateChain *CRYPT_CopyChainToElement(CertificateChain *chain,
2378c2c66affSColin Finck DWORD iChain, DWORD iElement)
2379c2c66affSColin Finck {
2380c2c66affSColin Finck CertificateChain *copy = CryptMemAlloc(sizeof(CertificateChain));
2381c2c66affSColin Finck
2382c2c66affSColin Finck if (copy)
2383c2c66affSColin Finck {
2384c2c66affSColin Finck copy->ref = 1;
2385c2c66affSColin Finck copy->world = CertDuplicateStore(chain->world);
2386c2c66affSColin Finck copy->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
2387c2c66affSColin Finck /* Leave the trust status of the copied chain unset, it'll get
2388c2c66affSColin Finck * rechecked after the new chain is done.
2389c2c66affSColin Finck */
2390c2c66affSColin Finck memset(©->context.TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
2391c2c66affSColin Finck copy->context.cLowerQualityChainContext = 0;
2392c2c66affSColin Finck copy->context.rgpLowerQualityChainContext = NULL;
2393c2c66affSColin Finck copy->context.fHasRevocationFreshnessTime = FALSE;
2394c2c66affSColin Finck copy->context.dwRevocationFreshnessTime = 0;
2395c2c66affSColin Finck copy->context.rgpChain = CryptMemAlloc(
2396c2c66affSColin Finck (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
2397c2c66affSColin Finck if (copy->context.rgpChain)
2398c2c66affSColin Finck {
2399c2c66affSColin Finck BOOL ret = TRUE;
2400c2c66affSColin Finck DWORD i;
2401c2c66affSColin Finck
2402c2c66affSColin Finck memset(copy->context.rgpChain, 0,
2403c2c66affSColin Finck (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
2404c2c66affSColin Finck if (iChain)
2405c2c66affSColin Finck {
2406c2c66affSColin Finck for (i = 0; ret && iChain && i < iChain - 1; i++)
2407c2c66affSColin Finck {
2408c2c66affSColin Finck copy->context.rgpChain[i] =
2409c2c66affSColin Finck CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
2410c2c66affSColin Finck chain->context.rgpChain[i]->cElement - 1);
2411c2c66affSColin Finck if (!copy->context.rgpChain[i])
2412c2c66affSColin Finck ret = FALSE;
2413c2c66affSColin Finck }
2414c2c66affSColin Finck }
2415c2c66affSColin Finck else
2416c2c66affSColin Finck i = 0;
2417c2c66affSColin Finck if (ret)
2418c2c66affSColin Finck {
2419c2c66affSColin Finck copy->context.rgpChain[i] =
2420c2c66affSColin Finck CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
2421c2c66affSColin Finck iElement);
2422c2c66affSColin Finck if (!copy->context.rgpChain[i])
2423c2c66affSColin Finck ret = FALSE;
2424c2c66affSColin Finck }
2425c2c66affSColin Finck if (!ret)
2426c2c66affSColin Finck {
2427c2c66affSColin Finck CRYPT_FreeChainContext(copy);
2428c2c66affSColin Finck copy = NULL;
2429c2c66affSColin Finck }
2430c2c66affSColin Finck else
2431c2c66affSColin Finck copy->context.cChain = iChain + 1;
2432c2c66affSColin Finck }
2433c2c66affSColin Finck else
2434c2c66affSColin Finck {
2435c2c66affSColin Finck CryptMemFree(copy);
2436c2c66affSColin Finck copy = NULL;
2437c2c66affSColin Finck }
2438c2c66affSColin Finck }
2439c2c66affSColin Finck return copy;
2440c2c66affSColin Finck }
2441c2c66affSColin Finck
CRYPT_BuildAlternateContextFromChain(CertificateChainEngine * engine,LPFILETIME pTime,HCERTSTORE hAdditionalStore,DWORD flags,CertificateChain * chain)2442c2c66affSColin Finck static CertificateChain *CRYPT_BuildAlternateContextFromChain(
2443c2c66affSColin Finck CertificateChainEngine *engine, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
2444c2c66affSColin Finck DWORD flags, CertificateChain *chain)
2445c2c66affSColin Finck {
2446c2c66affSColin Finck CertificateChain *alternate;
2447c2c66affSColin Finck
2448c2c66affSColin Finck TRACE("(%p, %s, %p, %p)\n", engine, debugstr_filetime(pTime),
2449c2c66affSColin Finck hAdditionalStore, chain);
2450c2c66affSColin Finck
2451c2c66affSColin Finck /* Always start with the last "lower quality" chain to ensure a consistent
2452c2c66affSColin Finck * order of alternate creation:
2453c2c66affSColin Finck */
2454c2c66affSColin Finck if (chain->context.cLowerQualityChainContext)
2455c2c66affSColin Finck chain = (CertificateChain*)chain->context.rgpLowerQualityChainContext[
2456c2c66affSColin Finck chain->context.cLowerQualityChainContext - 1];
2457c2c66affSColin Finck /* A chain with only one element can't have any alternates */
2458c2c66affSColin Finck if (chain->context.cChain <= 1 && chain->context.rgpChain[0]->cElement <= 1)
2459c2c66affSColin Finck alternate = NULL;
2460c2c66affSColin Finck else
2461c2c66affSColin Finck {
2462c2c66affSColin Finck DWORD i, j, infoStatus;
2463c2c66affSColin Finck PCCERT_CONTEXT alternateIssuer = NULL;
2464c2c66affSColin Finck
2465c2c66affSColin Finck alternate = NULL;
2466c2c66affSColin Finck for (i = 0; !alternateIssuer && i < chain->context.cChain; i++)
2467c2c66affSColin Finck for (j = 0; !alternateIssuer &&
2468c2c66affSColin Finck j < chain->context.rgpChain[i]->cElement - 1; j++)
2469c2c66affSColin Finck {
2470c2c66affSColin Finck PCCERT_CONTEXT subject =
2471c2c66affSColin Finck chain->context.rgpChain[i]->rgpElement[j]->pCertContext;
2472c2c66affSColin Finck PCCERT_CONTEXT prevIssuer = CertDuplicateCertificateContext(
2473c2c66affSColin Finck chain->context.rgpChain[i]->rgpElement[j + 1]->pCertContext);
2474c2c66affSColin Finck
2475c2c66affSColin Finck alternateIssuer = CRYPT_GetIssuer(engine, prevIssuer->hCertStore,
2476c2c66affSColin Finck subject, prevIssuer, flags, &infoStatus);
2477c2c66affSColin Finck }
2478c2c66affSColin Finck if (alternateIssuer)
2479c2c66affSColin Finck {
2480c2c66affSColin Finck i--;
2481c2c66affSColin Finck j--;
2482c2c66affSColin Finck alternate = CRYPT_CopyChainToElement(chain, i, j);
2483c2c66affSColin Finck if (alternate)
2484c2c66affSColin Finck {
2485c2c66affSColin Finck BOOL ret = CRYPT_AddCertToSimpleChain(engine,
2486c2c66affSColin Finck alternate->context.rgpChain[i], alternateIssuer, infoStatus);
2487c2c66affSColin Finck
2488c2c66affSColin Finck /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it
2489c2c66affSColin Finck * to close the enumeration that found it
2490c2c66affSColin Finck */
2491c2c66affSColin Finck CertFreeCertificateContext(alternateIssuer);
2492c2c66affSColin Finck if (ret)
2493c2c66affSColin Finck {
2494c2c66affSColin Finck ret = CRYPT_BuildSimpleChain(engine, alternate->world,
2495c2c66affSColin Finck flags, alternate->context.rgpChain[i]);
2496c2c66affSColin Finck if (ret)
2497c2c66affSColin Finck CRYPT_CheckSimpleChain(engine,
2498c2c66affSColin Finck alternate->context.rgpChain[i], pTime);
2499c2c66affSColin Finck CRYPT_CombineTrustStatus(&alternate->context.TrustStatus,
2500c2c66affSColin Finck &alternate->context.rgpChain[i]->TrustStatus);
2501c2c66affSColin Finck }
2502c2c66affSColin Finck if (!ret)
2503c2c66affSColin Finck {
2504c2c66affSColin Finck CRYPT_FreeChainContext(alternate);
2505c2c66affSColin Finck alternate = NULL;
2506c2c66affSColin Finck }
2507c2c66affSColin Finck }
2508c2c66affSColin Finck }
2509c2c66affSColin Finck }
2510c2c66affSColin Finck TRACE("%p\n", alternate);
2511c2c66affSColin Finck return alternate;
2512c2c66affSColin Finck }
2513c2c66affSColin Finck
2514c2c66affSColin Finck #define CHAIN_QUALITY_SIGNATURE_VALID 0x16
2515c2c66affSColin Finck #define CHAIN_QUALITY_TIME_VALID 8
2516c2c66affSColin Finck #define CHAIN_QUALITY_COMPLETE_CHAIN 4
2517c2c66affSColin Finck #define CHAIN_QUALITY_BASIC_CONSTRAINTS 2
2518c2c66affSColin Finck #define CHAIN_QUALITY_TRUSTED_ROOT 1
2519c2c66affSColin Finck
2520c2c66affSColin Finck #define CHAIN_QUALITY_HIGHEST \
2521c2c66affSColin Finck CHAIN_QUALITY_SIGNATURE_VALID | CHAIN_QUALITY_TIME_VALID | \
2522c2c66affSColin Finck CHAIN_QUALITY_COMPLETE_CHAIN | CHAIN_QUALITY_BASIC_CONSTRAINTS | \
2523c2c66affSColin Finck CHAIN_QUALITY_TRUSTED_ROOT
2524c2c66affSColin Finck
2525c2c66affSColin Finck #define IS_TRUST_ERROR_SET(TrustStatus, bits) \
2526c2c66affSColin Finck (TrustStatus)->dwErrorStatus & (bits)
2527c2c66affSColin Finck
CRYPT_ChainQuality(const CertificateChain * chain)2528c2c66affSColin Finck static DWORD CRYPT_ChainQuality(const CertificateChain *chain)
2529c2c66affSColin Finck {
2530c2c66affSColin Finck DWORD quality = CHAIN_QUALITY_HIGHEST;
2531c2c66affSColin Finck
2532c2c66affSColin Finck if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
2533c2c66affSColin Finck CERT_TRUST_IS_UNTRUSTED_ROOT))
2534c2c66affSColin Finck quality &= ~CHAIN_QUALITY_TRUSTED_ROOT;
2535c2c66affSColin Finck if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
2536c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS))
2537c2c66affSColin Finck quality &= ~CHAIN_QUALITY_BASIC_CONSTRAINTS;
2538c2c66affSColin Finck if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
2539c2c66affSColin Finck CERT_TRUST_IS_PARTIAL_CHAIN))
2540c2c66affSColin Finck quality &= ~CHAIN_QUALITY_COMPLETE_CHAIN;
2541c2c66affSColin Finck if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
2542c2c66affSColin Finck CERT_TRUST_IS_NOT_TIME_VALID | CERT_TRUST_IS_NOT_TIME_NESTED))
2543c2c66affSColin Finck quality &= ~CHAIN_QUALITY_TIME_VALID;
2544c2c66affSColin Finck if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
2545c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID))
2546c2c66affSColin Finck quality &= ~CHAIN_QUALITY_SIGNATURE_VALID;
2547c2c66affSColin Finck return quality;
2548c2c66affSColin Finck }
2549c2c66affSColin Finck
2550c2c66affSColin Finck /* Chooses the highest quality chain among chain and its "lower quality"
2551c2c66affSColin Finck * alternate chains. Returns the highest quality chain, with all other
2552c2c66affSColin Finck * chains as lower quality chains of it.
2553c2c66affSColin Finck */
CRYPT_ChooseHighestQualityChain(CertificateChain * chain)2554c2c66affSColin Finck static CertificateChain *CRYPT_ChooseHighestQualityChain(
2555c2c66affSColin Finck CertificateChain *chain)
2556c2c66affSColin Finck {
2557c2c66affSColin Finck DWORD i;
2558c2c66affSColin Finck
2559c2c66affSColin Finck /* There are always only two chains being considered: chain, and an
2560c2c66affSColin Finck * alternate at chain->rgpLowerQualityChainContext[i]. If the alternate
2561c2c66affSColin Finck * has a higher quality than chain, the alternate gets assigned the lower
2562c2c66affSColin Finck * quality contexts, with chain taking the alternate's place among the
2563c2c66affSColin Finck * lower quality contexts.
2564c2c66affSColin Finck */
2565c2c66affSColin Finck for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
2566c2c66affSColin Finck {
2567c2c66affSColin Finck CertificateChain *alternate =
2568c2c66affSColin Finck (CertificateChain*)chain->context.rgpLowerQualityChainContext[i];
2569c2c66affSColin Finck
2570c2c66affSColin Finck if (CRYPT_ChainQuality(alternate) > CRYPT_ChainQuality(chain))
2571c2c66affSColin Finck {
2572c2c66affSColin Finck alternate->context.cLowerQualityChainContext =
2573c2c66affSColin Finck chain->context.cLowerQualityChainContext;
2574c2c66affSColin Finck alternate->context.rgpLowerQualityChainContext =
2575c2c66affSColin Finck chain->context.rgpLowerQualityChainContext;
2576c2c66affSColin Finck alternate->context.rgpLowerQualityChainContext[i] =
2577c2c66affSColin Finck (PCCERT_CHAIN_CONTEXT)chain;
2578c2c66affSColin Finck chain->context.cLowerQualityChainContext = 0;
2579c2c66affSColin Finck chain->context.rgpLowerQualityChainContext = NULL;
2580c2c66affSColin Finck chain = alternate;
2581c2c66affSColin Finck }
2582c2c66affSColin Finck }
2583c2c66affSColin Finck return chain;
2584c2c66affSColin Finck }
2585c2c66affSColin Finck
CRYPT_AddAlternateChainToChain(CertificateChain * chain,const CertificateChain * alternate)2586c2c66affSColin Finck static BOOL CRYPT_AddAlternateChainToChain(CertificateChain *chain,
2587c2c66affSColin Finck const CertificateChain *alternate)
2588c2c66affSColin Finck {
2589c2c66affSColin Finck BOOL ret;
2590c2c66affSColin Finck
2591c2c66affSColin Finck if (chain->context.cLowerQualityChainContext)
2592c2c66affSColin Finck chain->context.rgpLowerQualityChainContext =
2593c2c66affSColin Finck CryptMemRealloc(chain->context.rgpLowerQualityChainContext,
2594c2c66affSColin Finck (chain->context.cLowerQualityChainContext + 1) *
2595c2c66affSColin Finck sizeof(PCCERT_CHAIN_CONTEXT));
2596c2c66affSColin Finck else
2597c2c66affSColin Finck chain->context.rgpLowerQualityChainContext =
2598c2c66affSColin Finck CryptMemAlloc(sizeof(PCCERT_CHAIN_CONTEXT));
2599c2c66affSColin Finck if (chain->context.rgpLowerQualityChainContext)
2600c2c66affSColin Finck {
2601c2c66affSColin Finck chain->context.rgpLowerQualityChainContext[
2602c2c66affSColin Finck chain->context.cLowerQualityChainContext++] =
2603c2c66affSColin Finck (PCCERT_CHAIN_CONTEXT)alternate;
2604c2c66affSColin Finck ret = TRUE;
2605c2c66affSColin Finck }
2606c2c66affSColin Finck else
2607c2c66affSColin Finck ret = FALSE;
2608c2c66affSColin Finck return ret;
2609c2c66affSColin Finck }
2610c2c66affSColin Finck
CRYPT_FindIthElementInChain(const CERT_CHAIN_CONTEXT * chain,DWORD i)2611c2c66affSColin Finck static PCERT_CHAIN_ELEMENT CRYPT_FindIthElementInChain(
2612c2c66affSColin Finck const CERT_CHAIN_CONTEXT *chain, DWORD i)
2613c2c66affSColin Finck {
2614c2c66affSColin Finck DWORD j, iElement;
2615c2c66affSColin Finck PCERT_CHAIN_ELEMENT element = NULL;
2616c2c66affSColin Finck
2617c2c66affSColin Finck for (j = 0, iElement = 0; !element && j < chain->cChain; j++)
2618c2c66affSColin Finck {
2619c2c66affSColin Finck if (iElement + chain->rgpChain[j]->cElement < i)
2620c2c66affSColin Finck iElement += chain->rgpChain[j]->cElement;
2621c2c66affSColin Finck else
2622c2c66affSColin Finck element = chain->rgpChain[j]->rgpElement[i - iElement];
2623c2c66affSColin Finck }
2624c2c66affSColin Finck return element;
2625c2c66affSColin Finck }
2626c2c66affSColin Finck
2627c2c66affSColin Finck typedef struct _CERT_CHAIN_PARA_NO_EXTRA_FIELDS {
2628c2c66affSColin Finck DWORD cbSize;
2629c2c66affSColin Finck CERT_USAGE_MATCH RequestedUsage;
2630c2c66affSColin Finck } CERT_CHAIN_PARA_NO_EXTRA_FIELDS;
2631c2c66affSColin Finck
CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain,LPFILETIME pTime,HCERTSTORE hAdditionalStore,const CERT_CHAIN_PARA * pChainPara,DWORD chainFlags)2632c2c66affSColin Finck static void CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain,
2633c2c66affSColin Finck LPFILETIME pTime, HCERTSTORE hAdditionalStore,
2634c2c66affSColin Finck const CERT_CHAIN_PARA *pChainPara, DWORD chainFlags)
2635c2c66affSColin Finck {
2636c2c66affSColin Finck DWORD cContext;
2637c2c66affSColin Finck
2638c2c66affSColin Finck if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_END_CERT)
2639c2c66affSColin Finck cContext = 1;
2640c2c66affSColin Finck else if ((chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN) ||
2641c2c66affSColin Finck (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT))
2642c2c66affSColin Finck {
2643c2c66affSColin Finck DWORD i;
2644c2c66affSColin Finck
2645c2c66affSColin Finck for (i = 0, cContext = 0; i < chain->cChain; i++)
2646c2c66affSColin Finck {
2647c2c66affSColin Finck if (i < chain->cChain - 1 ||
2648c2c66affSColin Finck chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN)
2649c2c66affSColin Finck cContext += chain->rgpChain[i]->cElement;
2650c2c66affSColin Finck else
2651c2c66affSColin Finck cContext += chain->rgpChain[i]->cElement - 1;
2652c2c66affSColin Finck }
2653c2c66affSColin Finck }
2654c2c66affSColin Finck else
2655c2c66affSColin Finck cContext = 0;
2656c2c66affSColin Finck if (cContext)
2657c2c66affSColin Finck {
2658c2c66affSColin Finck DWORD i, j, iContext, revocationFlags;
2659c2c66affSColin Finck CERT_REVOCATION_PARA revocationPara = { sizeof(revocationPara), 0 };
2660c2c66affSColin Finck CERT_REVOCATION_STATUS revocationStatus =
2661c2c66affSColin Finck { sizeof(revocationStatus), 0 };
2662c2c66affSColin Finck BOOL ret;
2663c2c66affSColin Finck
2664c2c66affSColin Finck revocationFlags = CERT_VERIFY_REV_CHAIN_FLAG;
2665c2c66affSColin Finck if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY)
2666c2c66affSColin Finck revocationFlags |= CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION;
2667c2c66affSColin Finck if (chainFlags & CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT)
2668c2c66affSColin Finck revocationFlags |= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG;
2669c2c66affSColin Finck revocationPara.pftTimeToUse = pTime;
2670c2c66affSColin Finck if (hAdditionalStore)
2671c2c66affSColin Finck {
2672c2c66affSColin Finck revocationPara.cCertStore = 1;
2673c2c66affSColin Finck revocationPara.rgCertStore = &hAdditionalStore;
2674c2c66affSColin Finck revocationPara.hCrlStore = hAdditionalStore;
2675c2c66affSColin Finck }
2676c2c66affSColin Finck if (pChainPara->cbSize == sizeof(CERT_CHAIN_PARA))
2677c2c66affSColin Finck {
2678c2c66affSColin Finck revocationPara.dwUrlRetrievalTimeout =
2679c2c66affSColin Finck pChainPara->dwUrlRetrievalTimeout;
2680c2c66affSColin Finck revocationPara.fCheckFreshnessTime =
2681c2c66affSColin Finck pChainPara->fCheckRevocationFreshnessTime;
2682c2c66affSColin Finck revocationPara.dwFreshnessTime =
2683c2c66affSColin Finck pChainPara->dwRevocationFreshnessTime;
2684c2c66affSColin Finck }
2685c2c66affSColin Finck for (i = 0, iContext = 0; iContext < cContext && i < chain->cChain; i++)
2686c2c66affSColin Finck {
2687c2c66affSColin Finck for (j = 0; iContext < cContext &&
2688c2c66affSColin Finck j < chain->rgpChain[i]->cElement; j++, iContext++)
2689c2c66affSColin Finck {
2690c2c66affSColin Finck PCCERT_CONTEXT certToCheck =
2691c2c66affSColin Finck chain->rgpChain[i]->rgpElement[j]->pCertContext;
2692c2c66affSColin Finck
2693c2c66affSColin Finck if (j < chain->rgpChain[i]->cElement - 1)
2694c2c66affSColin Finck revocationPara.pIssuerCert =
2695c2c66affSColin Finck chain->rgpChain[i]->rgpElement[j + 1]->pCertContext;
2696c2c66affSColin Finck else
2697*997d44c9SAmine Khaldi revocationPara.pIssuerCert = NULL;
2698c2c66affSColin Finck ret = CertVerifyRevocation(X509_ASN_ENCODING,
2699c2c66affSColin Finck CERT_CONTEXT_REVOCATION_TYPE, 1, (void **)&certToCheck,
2700c2c66affSColin Finck revocationFlags, &revocationPara, &revocationStatus);
2701c2c66affSColin Finck
2702*997d44c9SAmine Khaldi if (!ret && chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN
2703*997d44c9SAmine Khaldi && revocationStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK && revocationPara.pIssuerCert == NULL)
2704c2c66affSColin Finck ret = TRUE;
2705c2c66affSColin Finck
2706c2c66affSColin Finck if (!ret)
2707c2c66affSColin Finck {
2708c2c66affSColin Finck PCERT_CHAIN_ELEMENT element = CRYPT_FindIthElementInChain(
2709c2c66affSColin Finck chain, iContext);
2710c2c66affSColin Finck DWORD error;
2711c2c66affSColin Finck
2712c2c66affSColin Finck switch (revocationStatus.dwError)
2713c2c66affSColin Finck {
2714c2c66affSColin Finck case CRYPT_E_NO_REVOCATION_CHECK:
2715c2c66affSColin Finck case CRYPT_E_NO_REVOCATION_DLL:
2716c2c66affSColin Finck case CRYPT_E_NOT_IN_REVOCATION_DATABASE:
2717c2c66affSColin Finck /* If the revocation status is unknown, it's assumed
2718c2c66affSColin Finck * to be offline too.
2719c2c66affSColin Finck */
2720c2c66affSColin Finck error = CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
2721c2c66affSColin Finck CERT_TRUST_IS_OFFLINE_REVOCATION;
2722c2c66affSColin Finck break;
2723c2c66affSColin Finck case CRYPT_E_REVOCATION_OFFLINE:
2724c2c66affSColin Finck error = CERT_TRUST_IS_OFFLINE_REVOCATION;
2725c2c66affSColin Finck break;
2726c2c66affSColin Finck case CRYPT_E_REVOKED:
2727c2c66affSColin Finck error = CERT_TRUST_IS_REVOKED;
2728c2c66affSColin Finck break;
2729c2c66affSColin Finck default:
2730c2c66affSColin Finck WARN("unmapped error %08x\n", revocationStatus.dwError);
2731c2c66affSColin Finck error = 0;
2732c2c66affSColin Finck }
2733c2c66affSColin Finck if (element)
2734c2c66affSColin Finck {
2735c2c66affSColin Finck /* FIXME: set element's pRevocationInfo member */
2736c2c66affSColin Finck element->TrustStatus.dwErrorStatus |= error;
2737c2c66affSColin Finck }
2738c2c66affSColin Finck chain->TrustStatus.dwErrorStatus |= error;
2739c2c66affSColin Finck }
2740c2c66affSColin Finck }
2741c2c66affSColin Finck }
2742c2c66affSColin Finck }
2743c2c66affSColin Finck }
2744c2c66affSColin Finck
CRYPT_CheckUsages(PCERT_CHAIN_CONTEXT chain,const CERT_CHAIN_PARA * pChainPara)2745c2c66affSColin Finck static void CRYPT_CheckUsages(PCERT_CHAIN_CONTEXT chain,
2746c2c66affSColin Finck const CERT_CHAIN_PARA *pChainPara)
2747c2c66affSColin Finck {
2748c2c66affSColin Finck if (pChainPara->cbSize >= sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS) &&
2749c2c66affSColin Finck pChainPara->RequestedUsage.Usage.cUsageIdentifier)
2750c2c66affSColin Finck {
2751c2c66affSColin Finck PCCERT_CONTEXT endCert;
2752c2c66affSColin Finck PCERT_EXTENSION ext;
2753c2c66affSColin Finck BOOL validForUsage;
2754c2c66affSColin Finck
2755c2c66affSColin Finck /* A chain, if created, always includes the end certificate */
2756c2c66affSColin Finck endCert = chain->rgpChain[0]->rgpElement[0]->pCertContext;
2757c2c66affSColin Finck /* The extended key usage extension specifies how a certificate's
2758c2c66affSColin Finck * public key may be used. From RFC 5280, section 4.2.1.12:
2759c2c66affSColin Finck * "This extension indicates one or more purposes for which the
2760c2c66affSColin Finck * certified public key may be used, in addition to or in place of the
2761c2c66affSColin Finck * basic purposes indicated in the key usage extension."
2762c2c66affSColin Finck * If the extension is present, it only satisfies the requested usage
2763c2c66affSColin Finck * if that usage is included in the extension:
2764c2c66affSColin Finck * "If the extension is present, then the certificate MUST only be used
2765c2c66affSColin Finck * for one of the purposes indicated."
2766c2c66affSColin Finck * There is also the special anyExtendedKeyUsage OID, but it doesn't
2767c2c66affSColin Finck * have to be respected:
2768c2c66affSColin Finck * "Applications that require the presence of a particular purpose
2769c2c66affSColin Finck * MAY reject certificates that include the anyExtendedKeyUsage OID
2770c2c66affSColin Finck * but not the particular OID expected for the application."
2771c2c66affSColin Finck * For now, I'm being more conservative and ignoring the presence of
2772c2c66affSColin Finck * the anyExtendedKeyUsage OID.
2773c2c66affSColin Finck */
2774c2c66affSColin Finck if ((ext = CertFindExtension(szOID_ENHANCED_KEY_USAGE,
2775c2c66affSColin Finck endCert->pCertInfo->cExtension, endCert->pCertInfo->rgExtension)))
2776c2c66affSColin Finck {
2777c2c66affSColin Finck const CERT_ENHKEY_USAGE *requestedUsage =
2778c2c66affSColin Finck &pChainPara->RequestedUsage.Usage;
2779c2c66affSColin Finck CERT_ENHKEY_USAGE *usage;
2780c2c66affSColin Finck DWORD size;
2781c2c66affSColin Finck
2782c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING,
2783c2c66affSColin Finck X509_ENHANCED_KEY_USAGE, ext->Value.pbData, ext->Value.cbData,
2784c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG, NULL, &usage, &size))
2785c2c66affSColin Finck {
2786c2c66affSColin Finck if (pChainPara->RequestedUsage.dwType == USAGE_MATCH_TYPE_AND)
2787c2c66affSColin Finck {
2788c2c66affSColin Finck DWORD i, j;
2789c2c66affSColin Finck
2790c2c66affSColin Finck /* For AND matches, all usages must be present */
2791c2c66affSColin Finck validForUsage = TRUE;
2792c2c66affSColin Finck for (i = 0; validForUsage &&
2793c2c66affSColin Finck i < requestedUsage->cUsageIdentifier; i++)
2794c2c66affSColin Finck {
2795c2c66affSColin Finck BOOL match = FALSE;
2796c2c66affSColin Finck
2797c2c66affSColin Finck for (j = 0; !match && j < usage->cUsageIdentifier; j++)
2798c2c66affSColin Finck match = !strcmp(usage->rgpszUsageIdentifier[j],
2799c2c66affSColin Finck requestedUsage->rgpszUsageIdentifier[i]);
2800c2c66affSColin Finck if (!match)
2801c2c66affSColin Finck validForUsage = FALSE;
2802c2c66affSColin Finck }
2803c2c66affSColin Finck }
2804c2c66affSColin Finck else
2805c2c66affSColin Finck {
2806c2c66affSColin Finck DWORD i, j;
2807c2c66affSColin Finck
2808c2c66affSColin Finck /* For OR matches, any matching usage suffices */
2809c2c66affSColin Finck validForUsage = FALSE;
2810c2c66affSColin Finck for (i = 0; !validForUsage &&
2811c2c66affSColin Finck i < requestedUsage->cUsageIdentifier; i++)
2812c2c66affSColin Finck {
2813c2c66affSColin Finck for (j = 0; !validForUsage &&
2814c2c66affSColin Finck j < usage->cUsageIdentifier; j++)
2815c2c66affSColin Finck validForUsage =
2816c2c66affSColin Finck !strcmp(usage->rgpszUsageIdentifier[j],
2817c2c66affSColin Finck requestedUsage->rgpszUsageIdentifier[i]);
2818c2c66affSColin Finck }
2819c2c66affSColin Finck }
2820c2c66affSColin Finck LocalFree(usage);
2821c2c66affSColin Finck }
2822c2c66affSColin Finck else
2823c2c66affSColin Finck validForUsage = FALSE;
2824c2c66affSColin Finck }
2825c2c66affSColin Finck else
2826c2c66affSColin Finck {
2827c2c66affSColin Finck /* If the extension isn't present, any interpretation is valid:
2828c2c66affSColin Finck * "Certificate using applications MAY require that the extended
2829c2c66affSColin Finck * key usage extension be present and that a particular purpose
2830c2c66affSColin Finck * be indicated in order for the certificate to be acceptable to
2831c2c66affSColin Finck * that application."
2832c2c66affSColin Finck * Not all web sites include the extended key usage extension, so
2833c2c66affSColin Finck * accept chains without it.
2834c2c66affSColin Finck */
2835c2c66affSColin Finck TRACE_(chain)("requested usage from certificate with no usages\n");
2836c2c66affSColin Finck validForUsage = TRUE;
2837c2c66affSColin Finck }
2838c2c66affSColin Finck if (!validForUsage)
2839c2c66affSColin Finck {
2840c2c66affSColin Finck chain->TrustStatus.dwErrorStatus |=
2841c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
2842c2c66affSColin Finck chain->rgpChain[0]->rgpElement[0]->TrustStatus.dwErrorStatus |=
2843c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
2844c2c66affSColin Finck }
2845c2c66affSColin Finck }
2846c2c66affSColin Finck if (pChainPara->cbSize >= sizeof(CERT_CHAIN_PARA) &&
2847c2c66affSColin Finck pChainPara->RequestedIssuancePolicy.Usage.cUsageIdentifier)
2848c2c66affSColin Finck FIXME("unimplemented for RequestedIssuancePolicy\n");
2849c2c66affSColin Finck }
2850c2c66affSColin Finck
dump_usage_match(LPCSTR name,const CERT_USAGE_MATCH * usageMatch)2851c2c66affSColin Finck static void dump_usage_match(LPCSTR name, const CERT_USAGE_MATCH *usageMatch)
2852c2c66affSColin Finck {
2853c2c66affSColin Finck if (usageMatch->Usage.cUsageIdentifier)
2854c2c66affSColin Finck {
2855c2c66affSColin Finck DWORD i;
2856c2c66affSColin Finck
2857c2c66affSColin Finck TRACE_(chain)("%s: %s\n", name,
2858c2c66affSColin Finck usageMatch->dwType == USAGE_MATCH_TYPE_AND ? "AND" : "OR");
2859c2c66affSColin Finck for (i = 0; i < usageMatch->Usage.cUsageIdentifier; i++)
2860c2c66affSColin Finck TRACE_(chain)("%s\n", usageMatch->Usage.rgpszUsageIdentifier[i]);
2861c2c66affSColin Finck }
2862c2c66affSColin Finck }
2863c2c66affSColin Finck
dump_chain_para(const CERT_CHAIN_PARA * pChainPara)2864c2c66affSColin Finck static void dump_chain_para(const CERT_CHAIN_PARA *pChainPara)
2865c2c66affSColin Finck {
2866c2c66affSColin Finck TRACE_(chain)("%d\n", pChainPara->cbSize);
2867c2c66affSColin Finck if (pChainPara->cbSize >= sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS))
2868c2c66affSColin Finck dump_usage_match("RequestedUsage", &pChainPara->RequestedUsage);
2869c2c66affSColin Finck if (pChainPara->cbSize >= sizeof(CERT_CHAIN_PARA))
2870c2c66affSColin Finck {
2871c2c66affSColin Finck dump_usage_match("RequestedIssuancePolicy",
2872c2c66affSColin Finck &pChainPara->RequestedIssuancePolicy);
2873c2c66affSColin Finck TRACE_(chain)("%d\n", pChainPara->dwUrlRetrievalTimeout);
2874c2c66affSColin Finck TRACE_(chain)("%d\n", pChainPara->fCheckRevocationFreshnessTime);
2875c2c66affSColin Finck TRACE_(chain)("%d\n", pChainPara->dwRevocationFreshnessTime);
2876c2c66affSColin Finck }
2877c2c66affSColin Finck }
2878c2c66affSColin Finck
CertGetCertificateChain(HCERTCHAINENGINE hChainEngine,PCCERT_CONTEXT pCertContext,LPFILETIME pTime,HCERTSTORE hAdditionalStore,PCERT_CHAIN_PARA pChainPara,DWORD dwFlags,LPVOID pvReserved,PCCERT_CHAIN_CONTEXT * ppChainContext)2879c2c66affSColin Finck BOOL WINAPI CertGetCertificateChain(HCERTCHAINENGINE hChainEngine,
2880c2c66affSColin Finck PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
2881c2c66affSColin Finck PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, LPVOID pvReserved,
2882c2c66affSColin Finck PCCERT_CHAIN_CONTEXT* ppChainContext)
2883c2c66affSColin Finck {
2884c2c66affSColin Finck CertificateChainEngine *engine;
2885c2c66affSColin Finck BOOL ret;
2886c2c66affSColin Finck CertificateChain *chain = NULL;
2887c2c66affSColin Finck
2888c2c66affSColin Finck TRACE("(%p, %p, %s, %p, %p, %08x, %p, %p)\n", hChainEngine, pCertContext,
2889c2c66affSColin Finck debugstr_filetime(pTime), hAdditionalStore, pChainPara, dwFlags,
2890c2c66affSColin Finck pvReserved, ppChainContext);
2891c2c66affSColin Finck
2892c2c66affSColin Finck engine = get_chain_engine(hChainEngine, TRUE);
2893c2c66affSColin Finck if (!engine)
2894c2c66affSColin Finck return FALSE;
2895c2c66affSColin Finck
2896c2c66affSColin Finck if (ppChainContext)
2897c2c66affSColin Finck *ppChainContext = NULL;
2898c2c66affSColin Finck if (!pChainPara)
2899c2c66affSColin Finck {
2900c2c66affSColin Finck SetLastError(E_INVALIDARG);
2901c2c66affSColin Finck return FALSE;
2902c2c66affSColin Finck }
2903c2c66affSColin Finck if (!pCertContext->pCertInfo->SignatureAlgorithm.pszObjId)
2904c2c66affSColin Finck {
2905c2c66affSColin Finck SetLastError(ERROR_INVALID_DATA);
2906c2c66affSColin Finck return FALSE;
2907c2c66affSColin Finck }
2908c2c66affSColin Finck
2909c2c66affSColin Finck if (TRACE_ON(chain))
2910c2c66affSColin Finck dump_chain_para(pChainPara);
2911c2c66affSColin Finck /* FIXME: what about HCCE_LOCAL_MACHINE? */
2912c2c66affSColin Finck ret = CRYPT_BuildCandidateChainFromCert(engine, pCertContext, pTime,
2913c2c66affSColin Finck hAdditionalStore, dwFlags, &chain);
2914c2c66affSColin Finck if (ret)
2915c2c66affSColin Finck {
2916c2c66affSColin Finck CertificateChain *alternate = NULL;
2917c2c66affSColin Finck PCERT_CHAIN_CONTEXT pChain;
2918c2c66affSColin Finck
2919c2c66affSColin Finck do {
2920c2c66affSColin Finck alternate = CRYPT_BuildAlternateContextFromChain(engine,
2921c2c66affSColin Finck pTime, hAdditionalStore, dwFlags, chain);
2922c2c66affSColin Finck
2923c2c66affSColin Finck /* Alternate contexts are added as "lower quality" contexts of
2924c2c66affSColin Finck * chain, to avoid loops in alternate chain creation.
2925c2c66affSColin Finck * The highest-quality chain is chosen at the end.
2926c2c66affSColin Finck */
2927c2c66affSColin Finck if (alternate)
2928c2c66affSColin Finck ret = CRYPT_AddAlternateChainToChain(chain, alternate);
2929c2c66affSColin Finck } while (ret && alternate);
2930c2c66affSColin Finck chain = CRYPT_ChooseHighestQualityChain(chain);
2931c2c66affSColin Finck if (!(dwFlags & CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS))
2932c2c66affSColin Finck CRYPT_FreeLowerQualityChains(chain);
2933c2c66affSColin Finck pChain = (PCERT_CHAIN_CONTEXT)chain;
2934c2c66affSColin Finck CRYPT_VerifyChainRevocation(pChain, pTime, hAdditionalStore,
2935c2c66affSColin Finck pChainPara, dwFlags);
2936c2c66affSColin Finck CRYPT_CheckUsages(pChain, pChainPara);
2937c2c66affSColin Finck TRACE_(chain)("error status: %08x\n",
2938c2c66affSColin Finck pChain->TrustStatus.dwErrorStatus);
2939c2c66affSColin Finck if (ppChainContext)
2940c2c66affSColin Finck *ppChainContext = pChain;
2941c2c66affSColin Finck else
2942c2c66affSColin Finck CertFreeCertificateChain(pChain);
2943c2c66affSColin Finck }
2944c2c66affSColin Finck TRACE("returning %d\n", ret);
2945c2c66affSColin Finck return ret;
2946c2c66affSColin Finck }
2947c2c66affSColin Finck
CertDuplicateCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext)2948c2c66affSColin Finck PCCERT_CHAIN_CONTEXT WINAPI CertDuplicateCertificateChain(
2949c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext)
2950c2c66affSColin Finck {
2951c2c66affSColin Finck CertificateChain *chain = (CertificateChain*)pChainContext;
2952c2c66affSColin Finck
2953c2c66affSColin Finck TRACE("(%p)\n", pChainContext);
2954c2c66affSColin Finck
2955c2c66affSColin Finck if (chain)
2956c2c66affSColin Finck InterlockedIncrement(&chain->ref);
2957c2c66affSColin Finck return pChainContext;
2958c2c66affSColin Finck }
2959c2c66affSColin Finck
CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext)2960c2c66affSColin Finck VOID WINAPI CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext)
2961c2c66affSColin Finck {
2962c2c66affSColin Finck CertificateChain *chain = (CertificateChain*)pChainContext;
2963c2c66affSColin Finck
2964c2c66affSColin Finck TRACE("(%p)\n", pChainContext);
2965c2c66affSColin Finck
2966c2c66affSColin Finck if (chain)
2967c2c66affSColin Finck {
2968c2c66affSColin Finck if (InterlockedDecrement(&chain->ref) == 0)
2969c2c66affSColin Finck CRYPT_FreeChainContext(chain);
2970c2c66affSColin Finck }
2971c2c66affSColin Finck }
2972c2c66affSColin Finck
CertFindChainInStore(HCERTSTORE store,DWORD certEncodingType,DWORD findFlags,DWORD findType,const void * findPara,PCCERT_CHAIN_CONTEXT prevChainContext)2973c2c66affSColin Finck PCCERT_CHAIN_CONTEXT WINAPI CertFindChainInStore(HCERTSTORE store,
2974c2c66affSColin Finck DWORD certEncodingType, DWORD findFlags, DWORD findType,
2975c2c66affSColin Finck const void *findPara, PCCERT_CHAIN_CONTEXT prevChainContext)
2976c2c66affSColin Finck {
2977c2c66affSColin Finck FIXME("(%p, %08x, %08x, %d, %p, %p): stub\n", store, certEncodingType,
2978c2c66affSColin Finck findFlags, findType, findPara, prevChainContext);
2979c2c66affSColin Finck return NULL;
2980c2c66affSColin Finck }
2981c2c66affSColin Finck
find_element_with_error(PCCERT_CHAIN_CONTEXT chain,DWORD error,LONG * iChain,LONG * iElement)2982c2c66affSColin Finck static void find_element_with_error(PCCERT_CHAIN_CONTEXT chain, DWORD error,
2983c2c66affSColin Finck LONG *iChain, LONG *iElement)
2984c2c66affSColin Finck {
2985c2c66affSColin Finck DWORD i, j;
2986c2c66affSColin Finck
2987c2c66affSColin Finck for (i = 0; i < chain->cChain; i++)
2988c2c66affSColin Finck for (j = 0; j < chain->rgpChain[i]->cElement; j++)
2989c2c66affSColin Finck if (chain->rgpChain[i]->rgpElement[j]->TrustStatus.dwErrorStatus &
2990c2c66affSColin Finck error)
2991c2c66affSColin Finck {
2992c2c66affSColin Finck *iChain = i;
2993c2c66affSColin Finck *iElement = j;
2994c2c66affSColin Finck return;
2995c2c66affSColin Finck }
2996c2c66affSColin Finck }
2997c2c66affSColin Finck
verify_base_policy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)2998c2c66affSColin Finck static BOOL WINAPI verify_base_policy(LPCSTR szPolicyOID,
2999c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3000c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3001c2c66affSColin Finck {
3002c2c66affSColin Finck DWORD checks = 0;
3003c2c66affSColin Finck
3004c2c66affSColin Finck if (pPolicyPara)
3005c2c66affSColin Finck checks = pPolicyPara->dwFlags;
3006c2c66affSColin Finck pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
3007c2c66affSColin Finck pPolicyStatus->dwError = NO_ERROR;
3008c2c66affSColin Finck if (pChainContext->TrustStatus.dwErrorStatus &
3009c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID)
3010c2c66affSColin Finck {
3011c2c66affSColin Finck pPolicyStatus->dwError = TRUST_E_CERT_SIGNATURE;
3012c2c66affSColin Finck find_element_with_error(pChainContext,
3013c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID, &pPolicyStatus->lChainIndex,
3014c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3015c2c66affSColin Finck }
3016c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_CYCLIC)
3017c2c66affSColin Finck {
3018c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_CHAINING;
3019c2c66affSColin Finck find_element_with_error(pChainContext, CERT_TRUST_IS_CYCLIC,
3020c2c66affSColin Finck &pPolicyStatus->lChainIndex, &pPolicyStatus->lElementIndex);
3021c2c66affSColin Finck /* For a cyclic chain, which element is a cycle isn't meaningful */
3022c2c66affSColin Finck pPolicyStatus->lElementIndex = -1;
3023c2c66affSColin Finck }
3024c2c66affSColin Finck if (!pPolicyStatus->dwError &&
3025c2c66affSColin Finck pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT &&
3026c2c66affSColin Finck !(checks & CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG))
3027c2c66affSColin Finck {
3028c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_UNTRUSTEDROOT;
3029c2c66affSColin Finck find_element_with_error(pChainContext,
3030c2c66affSColin Finck CERT_TRUST_IS_UNTRUSTED_ROOT, &pPolicyStatus->lChainIndex,
3031c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3032c2c66affSColin Finck }
3033c2c66affSColin Finck if (!pPolicyStatus->dwError &&
3034c2c66affSColin Finck pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
3035c2c66affSColin Finck {
3036c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_EXPIRED;
3037c2c66affSColin Finck find_element_with_error(pChainContext,
3038c2c66affSColin Finck CERT_TRUST_IS_NOT_TIME_VALID, &pPolicyStatus->lChainIndex,
3039c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3040c2c66affSColin Finck }
3041c2c66affSColin Finck if (!pPolicyStatus->dwError &&
3042c2c66affSColin Finck pChainContext->TrustStatus.dwErrorStatus &
3043c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE &&
3044c2c66affSColin Finck !(checks & CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG))
3045c2c66affSColin Finck {
3046c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_WRONG_USAGE;
3047c2c66affSColin Finck find_element_with_error(pChainContext,
3048c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE, &pPolicyStatus->lChainIndex,
3049c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3050c2c66affSColin Finck }
3051c2c66affSColin Finck if (!pPolicyStatus->dwError &&
3052c2c66affSColin Finck pChainContext->TrustStatus.dwErrorStatus &
3053c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT &&
3054c2c66affSColin Finck !(checks & CERT_CHAIN_POLICY_IGNORE_NOT_SUPPORTED_CRITICAL_EXT_FLAG))
3055c2c66affSColin Finck {
3056c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_CRITICAL;
3057c2c66affSColin Finck find_element_with_error(pChainContext,
3058c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT, &pPolicyStatus->lChainIndex,
3059c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3060c2c66affSColin Finck }
3061c2c66affSColin Finck return TRUE;
3062c2c66affSColin Finck }
3063c2c66affSColin Finck
3064c2c66affSColin Finck static BYTE msTestPubKey1[] = {
3065c2c66affSColin Finck 0x30,0x47,0x02,0x40,0x81,0x55,0x22,0xb9,0x8a,0xa4,0x6f,0xed,0xd6,0xe7,0xd9,
3066c2c66affSColin Finck 0x66,0x0f,0x55,0xbc,0xd7,0xcd,0xd5,0xbc,0x4e,0x40,0x02,0x21,0xa2,0xb1,0xf7,
3067c2c66affSColin Finck 0x87,0x30,0x85,0x5e,0xd2,0xf2,0x44,0xb9,0xdc,0x9b,0x75,0xb6,0xfb,0x46,0x5f,
3068c2c66affSColin Finck 0x42,0xb6,0x9d,0x23,0x36,0x0b,0xde,0x54,0x0f,0xcd,0xbd,0x1f,0x99,0x2a,0x10,
3069c2c66affSColin Finck 0x58,0x11,0xcb,0x40,0xcb,0xb5,0xa7,0x41,0x02,0x03,0x01,0x00,0x01 };
3070c2c66affSColin Finck static BYTE msTestPubKey2[] = {
3071c2c66affSColin Finck 0x30,0x47,0x02,0x40,0x9c,0x50,0x05,0x1d,0xe2,0x0e,0x4c,0x53,0xd8,0xd9,0xb5,
3072c2c66affSColin Finck 0xe5,0xfd,0xe9,0xe3,0xad,0x83,0x4b,0x80,0x08,0xd9,0xdc,0xe8,0xe8,0x35,0xf8,
3073c2c66affSColin Finck 0x11,0xf1,0xe9,0x9b,0x03,0x7a,0x65,0x64,0x76,0x35,0xce,0x38,0x2c,0xf2,0xb6,
3074c2c66affSColin Finck 0x71,0x9e,0x06,0xd9,0xbf,0xbb,0x31,0x69,0xa3,0xf6,0x30,0xa0,0x78,0x7b,0x18,
3075c2c66affSColin Finck 0xdd,0x50,0x4d,0x79,0x1e,0xeb,0x61,0xc1,0x02,0x03,0x01,0x00,0x01 };
3076c2c66affSColin Finck
dump_authenticode_extra_chain_policy_para(AUTHENTICODE_EXTRA_CERT_CHAIN_POLICY_PARA * extraPara)3077c2c66affSColin Finck static void dump_authenticode_extra_chain_policy_para(
3078c2c66affSColin Finck AUTHENTICODE_EXTRA_CERT_CHAIN_POLICY_PARA *extraPara)
3079c2c66affSColin Finck {
3080c2c66affSColin Finck if (extraPara)
3081c2c66affSColin Finck {
3082c2c66affSColin Finck TRACE_(chain)("cbSize = %d\n", extraPara->cbSize);
3083c2c66affSColin Finck TRACE_(chain)("dwRegPolicySettings = %08x\n",
3084c2c66affSColin Finck extraPara->dwRegPolicySettings);
3085c2c66affSColin Finck TRACE_(chain)("pSignerInfo = %p\n", extraPara->pSignerInfo);
3086c2c66affSColin Finck }
3087c2c66affSColin Finck }
3088c2c66affSColin Finck
verify_authenticode_policy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)3089c2c66affSColin Finck static BOOL WINAPI verify_authenticode_policy(LPCSTR szPolicyOID,
3090c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3091c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3092c2c66affSColin Finck {
3093c2c66affSColin Finck BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
3094c2c66affSColin Finck pPolicyStatus);
3095c2c66affSColin Finck AUTHENTICODE_EXTRA_CERT_CHAIN_POLICY_PARA *extraPara = NULL;
3096c2c66affSColin Finck
3097c2c66affSColin Finck if (pPolicyPara)
3098c2c66affSColin Finck extraPara = pPolicyPara->pvExtraPolicyPara;
3099c2c66affSColin Finck if (TRACE_ON(chain))
3100c2c66affSColin Finck dump_authenticode_extra_chain_policy_para(extraPara);
3101c2c66affSColin Finck if (ret && pPolicyStatus->dwError == CERT_E_UNTRUSTEDROOT)
3102c2c66affSColin Finck {
3103c2c66affSColin Finck CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
3104c2c66affSColin Finck BOOL isMSTestRoot = FALSE;
3105c2c66affSColin Finck PCCERT_CONTEXT failingCert =
3106c2c66affSColin Finck pChainContext->rgpChain[pPolicyStatus->lChainIndex]->
3107c2c66affSColin Finck rgpElement[pPolicyStatus->lElementIndex]->pCertContext;
3108c2c66affSColin Finck DWORD i;
3109c2c66affSColin Finck CRYPT_DATA_BLOB keyBlobs[] = {
3110c2c66affSColin Finck { sizeof(msTestPubKey1), msTestPubKey1 },
3111c2c66affSColin Finck { sizeof(msTestPubKey2), msTestPubKey2 },
3112c2c66affSColin Finck };
3113c2c66affSColin Finck
3114c2c66affSColin Finck /* Check whether the root is an MS test root */
3115*997d44c9SAmine Khaldi for (i = 0; !isMSTestRoot && i < ARRAY_SIZE(keyBlobs); i++)
3116c2c66affSColin Finck {
3117c2c66affSColin Finck msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
3118c2c66affSColin Finck msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
3119c2c66affSColin Finck if (CertComparePublicKeyInfo(
3120c2c66affSColin Finck X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
3121c2c66affSColin Finck &failingCert->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
3122c2c66affSColin Finck isMSTestRoot = TRUE;
3123c2c66affSColin Finck }
3124c2c66affSColin Finck if (isMSTestRoot)
3125c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_UNTRUSTEDTESTROOT;
3126c2c66affSColin Finck }
3127c2c66affSColin Finck return ret;
3128c2c66affSColin Finck }
3129c2c66affSColin Finck
verify_basic_constraints_policy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)3130c2c66affSColin Finck static BOOL WINAPI verify_basic_constraints_policy(LPCSTR szPolicyOID,
3131c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3132c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3133c2c66affSColin Finck {
3134c2c66affSColin Finck pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
3135c2c66affSColin Finck if (pChainContext->TrustStatus.dwErrorStatus &
3136c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
3137c2c66affSColin Finck {
3138c2c66affSColin Finck pPolicyStatus->dwError = TRUST_E_BASIC_CONSTRAINTS;
3139c2c66affSColin Finck find_element_with_error(pChainContext,
3140c2c66affSColin Finck CERT_TRUST_INVALID_BASIC_CONSTRAINTS, &pPolicyStatus->lChainIndex,
3141c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3142c2c66affSColin Finck }
3143c2c66affSColin Finck else
3144c2c66affSColin Finck pPolicyStatus->dwError = NO_ERROR;
3145c2c66affSColin Finck return TRUE;
3146c2c66affSColin Finck }
3147c2c66affSColin Finck
match_dns_to_subject_alt_name(const CERT_EXTENSION * ext,LPCWSTR server_name)3148c2c66affSColin Finck static BOOL match_dns_to_subject_alt_name(const CERT_EXTENSION *ext,
3149c2c66affSColin Finck LPCWSTR server_name)
3150c2c66affSColin Finck {
3151c2c66affSColin Finck BOOL matches = FALSE;
3152c2c66affSColin Finck CERT_ALT_NAME_INFO *subjectName;
3153c2c66affSColin Finck DWORD size;
3154c2c66affSColin Finck
3155c2c66affSColin Finck TRACE_(chain)("%s\n", debugstr_w(server_name));
3156c2c66affSColin Finck /* This could be spoofed by the embedded NULL vulnerability, since the
3157c2c66affSColin Finck * returned CERT_ALT_NAME_INFO doesn't have a way to indicate the
3158c2c66affSColin Finck * encoded length of a name. Fortunately CryptDecodeObjectEx fails if
3159c2c66affSColin Finck * the encoded form of the name contains a NULL.
3160c2c66affSColin Finck */
3161c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME,
3162c2c66affSColin Finck ext->Value.pbData, ext->Value.cbData,
3163c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
3164c2c66affSColin Finck &subjectName, &size))
3165c2c66affSColin Finck {
3166c2c66affSColin Finck DWORD i;
3167c2c66affSColin Finck
3168c2c66affSColin Finck /* RFC 5280 states that multiple instances of each name type may exist,
3169c2c66affSColin Finck * in section 4.2.1.6:
3170c2c66affSColin Finck * "Multiple name forms, and multiple instances of each name form,
3171c2c66affSColin Finck * MAY be included."
3172c2c66affSColin Finck * It doesn't specify the behavior in such cases, but both RFC 2818
3173c2c66affSColin Finck * and RFC 2595 explicitly accept a certificate if any name matches.
3174c2c66affSColin Finck */
3175c2c66affSColin Finck for (i = 0; !matches && i < subjectName->cAltEntry; i++)
3176c2c66affSColin Finck {
3177c2c66affSColin Finck if (subjectName->rgAltEntry[i].dwAltNameChoice ==
3178c2c66affSColin Finck CERT_ALT_NAME_DNS_NAME)
3179c2c66affSColin Finck {
3180c2c66affSColin Finck TRACE_(chain)("dNSName: %s\n", debugstr_w(
3181c2c66affSColin Finck subjectName->rgAltEntry[i].u.pwszDNSName));
3182c2c66affSColin Finck if (subjectName->rgAltEntry[i].u.pwszDNSName[0] == '*')
3183c2c66affSColin Finck {
3184c2c66affSColin Finck LPCWSTR server_name_dot;
3185c2c66affSColin Finck
3186c2c66affSColin Finck /* Matching a wildcard: a wildcard matches a single name
3187c2c66affSColin Finck * component, which is terminated by a dot. RFC 1034
3188c2c66affSColin Finck * doesn't define whether multiple wildcards are allowed,
3189c2c66affSColin Finck * but I will assume that they are not until proven
3190c2c66affSColin Finck * otherwise. RFC 1034 also states that 'the "*" label
3191c2c66affSColin Finck * always matches at least one whole label and sometimes
3192c2c66affSColin Finck * more, but always whole labels.' Native crypt32 does not
3193c2c66affSColin Finck * match more than one label with a wildcard, so I do the
3194c2c66affSColin Finck * same here. Thus, a wildcard only accepts the first
3195c2c66affSColin Finck * label, then requires an exact match of the remaining
3196c2c66affSColin Finck * string.
3197c2c66affSColin Finck */
3198c2c66affSColin Finck server_name_dot = strchrW(server_name, '.');
3199c2c66affSColin Finck if (server_name_dot)
3200c2c66affSColin Finck {
3201c2c66affSColin Finck if (!strcmpiW(server_name_dot,
3202c2c66affSColin Finck subjectName->rgAltEntry[i].u.pwszDNSName + 1))
3203c2c66affSColin Finck matches = TRUE;
3204c2c66affSColin Finck }
3205c2c66affSColin Finck }
3206c2c66affSColin Finck else if (!strcmpiW(server_name,
3207c2c66affSColin Finck subjectName->rgAltEntry[i].u.pwszDNSName))
3208c2c66affSColin Finck matches = TRUE;
3209c2c66affSColin Finck }
3210c2c66affSColin Finck }
3211c2c66affSColin Finck LocalFree(subjectName);
3212c2c66affSColin Finck }
3213c2c66affSColin Finck return matches;
3214c2c66affSColin Finck }
3215c2c66affSColin Finck
find_matching_domain_component(const CERT_NAME_INFO * name,LPCWSTR component)3216c2c66affSColin Finck static BOOL find_matching_domain_component(const CERT_NAME_INFO *name,
3217c2c66affSColin Finck LPCWSTR component)
3218c2c66affSColin Finck {
3219c2c66affSColin Finck BOOL matches = FALSE;
3220c2c66affSColin Finck DWORD i, j;
3221c2c66affSColin Finck
3222c2c66affSColin Finck for (i = 0; !matches && i < name->cRDN; i++)
3223c2c66affSColin Finck for (j = 0; j < name->rgRDN[i].cRDNAttr; j++)
3224c2c66affSColin Finck if (!strcmp(szOID_DOMAIN_COMPONENT,
3225c2c66affSColin Finck name->rgRDN[i].rgRDNAttr[j].pszObjId))
3226c2c66affSColin Finck {
3227c2c66affSColin Finck const CERT_RDN_ATTR *attr;
3228c2c66affSColin Finck
3229c2c66affSColin Finck attr = &name->rgRDN[i].rgRDNAttr[j];
3230c2c66affSColin Finck /* Compare with memicmpW rather than strcmpiW in order to avoid
3231c2c66affSColin Finck * a match with a string with an embedded NULL. The component
3232c2c66affSColin Finck * must match one domain component attribute's entire string
3233c2c66affSColin Finck * value with a case-insensitive match.
3234c2c66affSColin Finck */
3235c2c66affSColin Finck matches = !memicmpW(component, (LPCWSTR)attr->Value.pbData,
3236c2c66affSColin Finck attr->Value.cbData / sizeof(WCHAR));
3237c2c66affSColin Finck }
3238c2c66affSColin Finck return matches;
3239c2c66affSColin Finck }
3240c2c66affSColin Finck
match_domain_component(LPCWSTR allowed_component,DWORD allowed_len,LPCWSTR server_component,DWORD server_len,BOOL allow_wildcards,BOOL * see_wildcard)3241c2c66affSColin Finck static BOOL match_domain_component(LPCWSTR allowed_component, DWORD allowed_len,
3242c2c66affSColin Finck LPCWSTR server_component, DWORD server_len, BOOL allow_wildcards,
3243c2c66affSColin Finck BOOL *see_wildcard)
3244c2c66affSColin Finck {
3245c2c66affSColin Finck LPCWSTR allowed_ptr, server_ptr;
3246c2c66affSColin Finck BOOL matches = TRUE;
3247c2c66affSColin Finck
3248c2c66affSColin Finck *see_wildcard = FALSE;
3249c2c66affSColin Finck
3250c2c66affSColin Finck if (server_len < allowed_len)
3251c2c66affSColin Finck {
3252c2c66affSColin Finck WARN_(chain)("domain component %s too short for %s\n",
3253c2c66affSColin Finck debugstr_wn(server_component, server_len),
3254c2c66affSColin Finck debugstr_wn(allowed_component, allowed_len));
3255c2c66affSColin Finck /* A domain component can't contain a wildcard character, so a domain
3256c2c66affSColin Finck * component shorter than the allowed string can't produce a match.
3257c2c66affSColin Finck */
3258c2c66affSColin Finck return FALSE;
3259c2c66affSColin Finck }
3260c2c66affSColin Finck for (allowed_ptr = allowed_component, server_ptr = server_component;
3261c2c66affSColin Finck matches && allowed_ptr - allowed_component < allowed_len;
3262c2c66affSColin Finck allowed_ptr++, server_ptr++)
3263c2c66affSColin Finck {
3264c2c66affSColin Finck if (*allowed_ptr == '*')
3265c2c66affSColin Finck {
3266c2c66affSColin Finck if (allowed_ptr - allowed_component < allowed_len - 1)
3267c2c66affSColin Finck {
3268c2c66affSColin Finck WARN_(chain)("non-wildcard characters after wildcard not supported\n");
3269c2c66affSColin Finck matches = FALSE;
3270c2c66affSColin Finck }
3271c2c66affSColin Finck else if (!allow_wildcards)
3272c2c66affSColin Finck {
3273c2c66affSColin Finck WARN_(chain)("wildcard after non-wildcard component\n");
3274c2c66affSColin Finck matches = FALSE;
3275c2c66affSColin Finck }
3276c2c66affSColin Finck else
3277c2c66affSColin Finck {
3278c2c66affSColin Finck /* the preceding characters must have matched, so the rest of
3279c2c66affSColin Finck * the component also matches.
3280c2c66affSColin Finck */
3281c2c66affSColin Finck *see_wildcard = TRUE;
3282c2c66affSColin Finck break;
3283c2c66affSColin Finck }
3284c2c66affSColin Finck }
3285c2c66affSColin Finck if (matches)
3286c2c66affSColin Finck matches = tolowerW(*allowed_ptr) == tolowerW(*server_ptr);
3287c2c66affSColin Finck }
3288c2c66affSColin Finck if (matches && server_ptr - server_component < server_len)
3289c2c66affSColin Finck {
3290c2c66affSColin Finck /* If there are unmatched characters in the server domain component,
3291c2c66affSColin Finck * the server domain only matches if the allowed string ended in a '*'.
3292c2c66affSColin Finck */
3293c2c66affSColin Finck matches = *allowed_ptr == '*';
3294c2c66affSColin Finck }
3295c2c66affSColin Finck return matches;
3296c2c66affSColin Finck }
3297c2c66affSColin Finck
match_common_name(LPCWSTR server_name,const CERT_RDN_ATTR * nameAttr)3298c2c66affSColin Finck static BOOL match_common_name(LPCWSTR server_name, const CERT_RDN_ATTR *nameAttr)
3299c2c66affSColin Finck {
3300c2c66affSColin Finck LPCWSTR allowed = (LPCWSTR)nameAttr->Value.pbData;
3301c2c66affSColin Finck LPCWSTR allowed_component = allowed;
3302c2c66affSColin Finck DWORD allowed_len = nameAttr->Value.cbData / sizeof(WCHAR);
3303c2c66affSColin Finck LPCWSTR server_component = server_name;
3304c2c66affSColin Finck DWORD server_len = strlenW(server_name);
3305c2c66affSColin Finck BOOL matches = TRUE, allow_wildcards = TRUE;
3306c2c66affSColin Finck
3307c2c66affSColin Finck TRACE_(chain)("CN = %s\n", debugstr_wn(allowed_component, allowed_len));
3308c2c66affSColin Finck
3309c2c66affSColin Finck /* Remove trailing NULLs from the allowed name; while they shouldn't appear
3310c2c66affSColin Finck * in a certificate in the first place, they sometimes do, and they should
3311c2c66affSColin Finck * be ignored.
3312c2c66affSColin Finck */
3313c2c66affSColin Finck while (allowed_len && allowed_component[allowed_len - 1] == 0)
3314c2c66affSColin Finck allowed_len--;
3315c2c66affSColin Finck
3316c2c66affSColin Finck /* From RFC 2818 (HTTP over TLS), section 3.1:
3317c2c66affSColin Finck * "Names may contain the wildcard character * which is considered to match
3318c2c66affSColin Finck * any single domain name component or component fragment. E.g.,
3319c2c66affSColin Finck * *.a.com matches foo.a.com but not bar.foo.a.com. f*.com matches foo.com
3320c2c66affSColin Finck * but not bar.com."
3321c2c66affSColin Finck *
3322c2c66affSColin Finck * And from RFC 2595 (Using TLS with IMAP, POP3 and ACAP), section 2.4:
3323c2c66affSColin Finck * "A "*" wildcard character MAY be used as the left-most name component in
3324c2c66affSColin Finck * the certificate. For example, *.example.com would match a.example.com,
3325c2c66affSColin Finck * foo.example.com, etc. but would not match example.com."
3326c2c66affSColin Finck *
3327c2c66affSColin Finck * There are other protocols which use TLS, and none of them is
3328c2c66affSColin Finck * authoritative. This accepts certificates in common usage, e.g.
3329c2c66affSColin Finck * *.domain.com matches www.domain.com but not domain.com, and
3330c2c66affSColin Finck * www*.domain.com matches www1.domain.com but not mail.domain.com.
3331c2c66affSColin Finck */
3332c2c66affSColin Finck do {
3333c2c66affSColin Finck LPCWSTR allowed_dot, server_dot;
3334c2c66affSColin Finck
3335c2c66affSColin Finck allowed_dot = memchrW(allowed_component, '.',
3336c2c66affSColin Finck allowed_len - (allowed_component - allowed));
3337c2c66affSColin Finck server_dot = memchrW(server_component, '.',
3338c2c66affSColin Finck server_len - (server_component - server_name));
3339c2c66affSColin Finck /* The number of components must match */
3340c2c66affSColin Finck if ((!allowed_dot && server_dot) || (allowed_dot && !server_dot))
3341c2c66affSColin Finck {
3342c2c66affSColin Finck if (!allowed_dot)
3343c2c66affSColin Finck WARN_(chain)("%s: too many components for CN=%s\n",
3344c2c66affSColin Finck debugstr_w(server_name), debugstr_wn(allowed, allowed_len));
3345c2c66affSColin Finck else
3346c2c66affSColin Finck WARN_(chain)("%s: not enough components for CN=%s\n",
3347c2c66affSColin Finck debugstr_w(server_name), debugstr_wn(allowed, allowed_len));
3348c2c66affSColin Finck matches = FALSE;
3349c2c66affSColin Finck }
3350c2c66affSColin Finck else
3351c2c66affSColin Finck {
3352c2c66affSColin Finck LPCWSTR allowed_end, server_end;
3353c2c66affSColin Finck BOOL has_wildcard;
3354c2c66affSColin Finck
3355c2c66affSColin Finck allowed_end = allowed_dot ? allowed_dot : allowed + allowed_len;
3356c2c66affSColin Finck server_end = server_dot ? server_dot : server_name + server_len;
3357c2c66affSColin Finck matches = match_domain_component(allowed_component,
3358c2c66affSColin Finck allowed_end - allowed_component, server_component,
3359c2c66affSColin Finck server_end - server_component, allow_wildcards, &has_wildcard);
3360c2c66affSColin Finck /* Once a non-wildcard component is seen, no wildcard components
3361c2c66affSColin Finck * may follow
3362c2c66affSColin Finck */
3363c2c66affSColin Finck if (!has_wildcard)
3364c2c66affSColin Finck allow_wildcards = FALSE;
3365c2c66affSColin Finck if (matches)
3366c2c66affSColin Finck {
3367c2c66affSColin Finck allowed_component = allowed_dot ? allowed_dot + 1 : allowed_end;
3368c2c66affSColin Finck server_component = server_dot ? server_dot + 1 : server_end;
3369c2c66affSColin Finck }
3370c2c66affSColin Finck }
3371c2c66affSColin Finck } while (matches && allowed_component &&
3372c2c66affSColin Finck allowed_component - allowed < allowed_len &&
3373c2c66affSColin Finck server_component && server_component - server_name < server_len);
3374c2c66affSColin Finck TRACE_(chain)("returning %d\n", matches);
3375c2c66affSColin Finck return matches;
3376c2c66affSColin Finck }
3377c2c66affSColin Finck
match_dns_to_subject_dn(PCCERT_CONTEXT cert,LPCWSTR server_name)3378c2c66affSColin Finck static BOOL match_dns_to_subject_dn(PCCERT_CONTEXT cert, LPCWSTR server_name)
3379c2c66affSColin Finck {
3380c2c66affSColin Finck BOOL matches = FALSE;
3381c2c66affSColin Finck CERT_NAME_INFO *name;
3382c2c66affSColin Finck DWORD size;
3383c2c66affSColin Finck
3384c2c66affSColin Finck TRACE_(chain)("%s\n", debugstr_w(server_name));
3385c2c66affSColin Finck if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_UNICODE_NAME,
3386c2c66affSColin Finck cert->pCertInfo->Subject.pbData, cert->pCertInfo->Subject.cbData,
3387c2c66affSColin Finck CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
3388c2c66affSColin Finck &name, &size))
3389c2c66affSColin Finck {
3390c2c66affSColin Finck /* If the subject distinguished name contains any name components,
3391c2c66affSColin Finck * make sure all of them are present.
3392c2c66affSColin Finck */
3393c2c66affSColin Finck if (CertFindRDNAttr(szOID_DOMAIN_COMPONENT, name))
3394c2c66affSColin Finck {
3395c2c66affSColin Finck LPCWSTR ptr = server_name;
3396c2c66affSColin Finck
3397c2c66affSColin Finck do {
3398c2c66affSColin Finck LPCWSTR dot = strchrW(ptr, '.'), end;
3399c2c66affSColin Finck /* 254 is the maximum DNS label length, see RFC 1035 */
3400c2c66affSColin Finck WCHAR component[255];
3401c2c66affSColin Finck DWORD len;
3402c2c66affSColin Finck
3403c2c66affSColin Finck end = dot ? dot : ptr + strlenW(ptr);
3404c2c66affSColin Finck len = end - ptr;
3405*997d44c9SAmine Khaldi if (len >= ARRAY_SIZE(component))
3406c2c66affSColin Finck {
3407c2c66affSColin Finck WARN_(chain)("domain component %s too long\n",
3408c2c66affSColin Finck debugstr_wn(ptr, len));
3409c2c66affSColin Finck matches = FALSE;
3410c2c66affSColin Finck }
3411c2c66affSColin Finck else
3412c2c66affSColin Finck {
3413c2c66affSColin Finck memcpy(component, ptr, len * sizeof(WCHAR));
3414c2c66affSColin Finck component[len] = 0;
3415c2c66affSColin Finck matches = find_matching_domain_component(name, component);
3416c2c66affSColin Finck }
3417c2c66affSColin Finck ptr = dot ? dot + 1 : end;
3418c2c66affSColin Finck } while (matches && ptr && *ptr);
3419c2c66affSColin Finck }
3420c2c66affSColin Finck else
3421c2c66affSColin Finck {
3422c2c66affSColin Finck DWORD i, j;
3423c2c66affSColin Finck
3424c2c66affSColin Finck /* If the certificate isn't using a DN attribute in the name, make
3425c2c66affSColin Finck * make sure at least one common name matches. From RFC 2818,
3426c2c66affSColin Finck * section 3.1:
3427c2c66affSColin Finck * "If more than one identity of a given type is present in the
3428c2c66affSColin Finck * certificate (e.g., more than one dNSName name, a match in any
3429c2c66affSColin Finck * one of the set is considered acceptable.)"
3430c2c66affSColin Finck */
3431c2c66affSColin Finck for (i = 0; !matches && i < name->cRDN; i++)
3432c2c66affSColin Finck for (j = 0; !matches && j < name->rgRDN[i].cRDNAttr; j++)
3433c2c66affSColin Finck {
3434c2c66affSColin Finck PCERT_RDN_ATTR attr = &name->rgRDN[i].rgRDNAttr[j];
3435c2c66affSColin Finck
3436c2c66affSColin Finck if (attr->pszObjId && !strcmp(szOID_COMMON_NAME,
3437c2c66affSColin Finck attr->pszObjId))
3438c2c66affSColin Finck matches = match_common_name(server_name, attr);
3439c2c66affSColin Finck }
3440c2c66affSColin Finck }
3441c2c66affSColin Finck LocalFree(name);
3442c2c66affSColin Finck }
3443c2c66affSColin Finck return matches;
3444c2c66affSColin Finck }
3445c2c66affSColin Finck
dump_ssl_extra_chain_policy_para(HTTPSPolicyCallbackData * sslPara)3446c2c66affSColin Finck static void dump_ssl_extra_chain_policy_para(HTTPSPolicyCallbackData *sslPara)
3447c2c66affSColin Finck {
3448c2c66affSColin Finck if (sslPara)
3449c2c66affSColin Finck {
3450c2c66affSColin Finck TRACE_(chain)("cbSize = %d\n", sslPara->u.cbSize);
3451c2c66affSColin Finck TRACE_(chain)("dwAuthType = %d\n", sslPara->dwAuthType);
3452c2c66affSColin Finck TRACE_(chain)("fdwChecks = %08x\n", sslPara->fdwChecks);
3453c2c66affSColin Finck TRACE_(chain)("pwszServerName = %s\n",
3454c2c66affSColin Finck debugstr_w(sslPara->pwszServerName));
3455c2c66affSColin Finck }
3456c2c66affSColin Finck }
3457c2c66affSColin Finck
verify_ssl_policy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)3458c2c66affSColin Finck static BOOL WINAPI verify_ssl_policy(LPCSTR szPolicyOID,
3459c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3460c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3461c2c66affSColin Finck {
3462c2c66affSColin Finck HTTPSPolicyCallbackData *sslPara = NULL;
3463c2c66affSColin Finck DWORD checks = 0;
3464c2c66affSColin Finck
3465c2c66affSColin Finck if (pPolicyPara)
3466c2c66affSColin Finck sslPara = pPolicyPara->pvExtraPolicyPara;
3467c2c66affSColin Finck if (TRACE_ON(chain))
3468c2c66affSColin Finck dump_ssl_extra_chain_policy_para(sslPara);
3469c2c66affSColin Finck if (sslPara && sslPara->u.cbSize >= sizeof(HTTPSPolicyCallbackData))
3470c2c66affSColin Finck checks = sslPara->fdwChecks;
3471c2c66affSColin Finck pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
3472c2c66affSColin Finck if (pChainContext->TrustStatus.dwErrorStatus &
3473c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID)
3474c2c66affSColin Finck {
3475c2c66affSColin Finck pPolicyStatus->dwError = TRUST_E_CERT_SIGNATURE;
3476c2c66affSColin Finck find_element_with_error(pChainContext,
3477c2c66affSColin Finck CERT_TRUST_IS_NOT_SIGNATURE_VALID, &pPolicyStatus->lChainIndex,
3478c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3479c2c66affSColin Finck }
3480c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3481c2c66affSColin Finck CERT_TRUST_IS_UNTRUSTED_ROOT &&
3482c2c66affSColin Finck !(checks & SECURITY_FLAG_IGNORE_UNKNOWN_CA))
3483c2c66affSColin Finck {
3484c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_UNTRUSTEDROOT;
3485c2c66affSColin Finck find_element_with_error(pChainContext,
3486c2c66affSColin Finck CERT_TRUST_IS_UNTRUSTED_ROOT, &pPolicyStatus->lChainIndex,
3487c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3488c2c66affSColin Finck }
3489c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_CYCLIC)
3490c2c66affSColin Finck {
3491c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_UNTRUSTEDROOT;
3492c2c66affSColin Finck find_element_with_error(pChainContext,
3493c2c66affSColin Finck CERT_TRUST_IS_CYCLIC, &pPolicyStatus->lChainIndex,
3494c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3495c2c66affSColin Finck /* For a cyclic chain, which element is a cycle isn't meaningful */
3496c2c66affSColin Finck pPolicyStatus->lElementIndex = -1;
3497c2c66affSColin Finck }
3498c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3499c2c66affSColin Finck CERT_TRUST_IS_NOT_TIME_VALID &&
3500c2c66affSColin Finck !(checks & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID))
3501c2c66affSColin Finck {
3502c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_EXPIRED;
3503c2c66affSColin Finck find_element_with_error(pChainContext,
3504c2c66affSColin Finck CERT_TRUST_IS_NOT_TIME_VALID, &pPolicyStatus->lChainIndex,
3505c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3506c2c66affSColin Finck }
3507c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3508c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE &&
3509c2c66affSColin Finck !(checks & SECURITY_FLAG_IGNORE_WRONG_USAGE))
3510c2c66affSColin Finck {
3511c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_WRONG_USAGE;
3512c2c66affSColin Finck find_element_with_error(pChainContext,
3513c2c66affSColin Finck CERT_TRUST_IS_NOT_VALID_FOR_USAGE, &pPolicyStatus->lChainIndex,
3514c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3515c2c66affSColin Finck }
3516c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3517c2c66affSColin Finck CERT_TRUST_IS_REVOKED && !(checks & SECURITY_FLAG_IGNORE_REVOCATION))
3518c2c66affSColin Finck {
3519c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_REVOKED;
3520c2c66affSColin Finck find_element_with_error(pChainContext,
3521c2c66affSColin Finck CERT_TRUST_IS_REVOKED, &pPolicyStatus->lChainIndex,
3522c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3523c2c66affSColin Finck }
3524c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3525c2c66affSColin Finck CERT_TRUST_IS_OFFLINE_REVOCATION &&
3526c2c66affSColin Finck !(checks & SECURITY_FLAG_IGNORE_REVOCATION))
3527c2c66affSColin Finck {
3528c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_REVOCATION_FAILURE;
3529c2c66affSColin Finck find_element_with_error(pChainContext,
3530c2c66affSColin Finck CERT_TRUST_IS_OFFLINE_REVOCATION, &pPolicyStatus->lChainIndex,
3531c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3532c2c66affSColin Finck }
3533c2c66affSColin Finck else if (pChainContext->TrustStatus.dwErrorStatus &
3534c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT)
3535c2c66affSColin Finck {
3536c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_CRITICAL;
3537c2c66affSColin Finck find_element_with_error(pChainContext,
3538c2c66affSColin Finck CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT, &pPolicyStatus->lChainIndex,
3539c2c66affSColin Finck &pPolicyStatus->lElementIndex);
3540c2c66affSColin Finck }
3541c2c66affSColin Finck else
3542c2c66affSColin Finck pPolicyStatus->dwError = NO_ERROR;
3543c2c66affSColin Finck /* We only need bother checking whether the name in the end certificate
3544c2c66affSColin Finck * matches if the chain is otherwise okay.
3545c2c66affSColin Finck */
3546c2c66affSColin Finck if (!pPolicyStatus->dwError && pPolicyPara &&
3547c2c66affSColin Finck pPolicyPara->cbSize >= sizeof(CERT_CHAIN_POLICY_PARA))
3548c2c66affSColin Finck {
3549c2c66affSColin Finck if (sslPara && sslPara->u.cbSize >= sizeof(HTTPSPolicyCallbackData))
3550c2c66affSColin Finck {
3551c2c66affSColin Finck if (sslPara->dwAuthType == AUTHTYPE_SERVER &&
3552c2c66affSColin Finck sslPara->pwszServerName &&
3553c2c66affSColin Finck !(checks & SECURITY_FLAG_IGNORE_CERT_CN_INVALID))
3554c2c66affSColin Finck {
3555c2c66affSColin Finck PCCERT_CONTEXT cert;
3556c2c66affSColin Finck PCERT_EXTENSION altNameExt;
3557c2c66affSColin Finck BOOL matches;
3558c2c66affSColin Finck
3559c2c66affSColin Finck cert = pChainContext->rgpChain[0]->rgpElement[0]->pCertContext;
3560c2c66affSColin Finck altNameExt = get_subject_alt_name_ext(cert->pCertInfo);
3561c2c66affSColin Finck /* If the alternate name extension exists, the name it contains
3562c2c66affSColin Finck * is bound to the certificate, so make sure the name matches
3563c2c66affSColin Finck * it. Otherwise, look for the server name in the subject
3564c2c66affSColin Finck * distinguished name. RFC5280, section 4.2.1.6:
3565c2c66affSColin Finck * "Whenever such identities are to be bound into a
3566c2c66affSColin Finck * certificate, the subject alternative name (or issuer
3567c2c66affSColin Finck * alternative name) extension MUST be used; however, a DNS
3568c2c66affSColin Finck * name MAY also be represented in the subject field using the
3569c2c66affSColin Finck * domainComponent attribute."
3570c2c66affSColin Finck */
3571c2c66affSColin Finck if (altNameExt)
3572c2c66affSColin Finck matches = match_dns_to_subject_alt_name(altNameExt,
3573c2c66affSColin Finck sslPara->pwszServerName);
3574c2c66affSColin Finck else
3575c2c66affSColin Finck matches = match_dns_to_subject_dn(cert,
3576c2c66affSColin Finck sslPara->pwszServerName);
3577c2c66affSColin Finck if (!matches)
3578c2c66affSColin Finck {
3579c2c66affSColin Finck pPolicyStatus->dwError = CERT_E_CN_NO_MATCH;
3580c2c66affSColin Finck pPolicyStatus->lChainIndex = 0;
3581c2c66affSColin Finck pPolicyStatus->lElementIndex = 0;
3582c2c66affSColin Finck }
3583c2c66affSColin Finck }
3584c2c66affSColin Finck }
3585c2c66affSColin Finck }
3586c2c66affSColin Finck return TRUE;
3587c2c66affSColin Finck }
3588c2c66affSColin Finck
3589c2c66affSColin Finck static BYTE msPubKey1[] = {
3590c2c66affSColin Finck 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xdf,0x08,0xba,0xe3,0x3f,0x6e,
3591c2c66affSColin Finck 0x64,0x9b,0xf5,0x89,0xaf,0x28,0x96,0x4a,0x07,0x8f,0x1b,0x2e,0x8b,0x3e,0x1d,
3592c2c66affSColin Finck 0xfc,0xb8,0x80,0x69,0xa3,0xa1,0xce,0xdb,0xdf,0xb0,0x8e,0x6c,0x89,0x76,0x29,
3593c2c66affSColin Finck 0x4f,0xca,0x60,0x35,0x39,0xad,0x72,0x32,0xe0,0x0b,0xae,0x29,0x3d,0x4c,0x16,
3594c2c66affSColin Finck 0xd9,0x4b,0x3c,0x9d,0xda,0xc5,0xd3,0xd1,0x09,0xc9,0x2c,0x6f,0xa6,0xc2,0x60,
3595c2c66affSColin Finck 0x53,0x45,0xdd,0x4b,0xd1,0x55,0xcd,0x03,0x1c,0xd2,0x59,0x56,0x24,0xf3,0xe5,
3596c2c66affSColin Finck 0x78,0xd8,0x07,0xcc,0xd8,0xb3,0x1f,0x90,0x3f,0xc0,0x1a,0x71,0x50,0x1d,0x2d,
3597c2c66affSColin Finck 0xa7,0x12,0x08,0x6d,0x7c,0xb0,0x86,0x6c,0xc7,0xba,0x85,0x32,0x07,0xe1,0x61,
3598c2c66affSColin Finck 0x6f,0xaf,0x03,0xc5,0x6d,0xe5,0xd6,0xa1,0x8f,0x36,0xf6,0xc1,0x0b,0xd1,0x3e,
3599c2c66affSColin Finck 0x69,0x97,0x48,0x72,0xc9,0x7f,0xa4,0xc8,0xc2,0x4a,0x4c,0x7e,0xa1,0xd1,0x94,
3600c2c66affSColin Finck 0xa6,0xd7,0xdc,0xeb,0x05,0x46,0x2e,0xb8,0x18,0xb4,0x57,0x1d,0x86,0x49,0xdb,
3601c2c66affSColin Finck 0x69,0x4a,0x2c,0x21,0xf5,0x5e,0x0f,0x54,0x2d,0x5a,0x43,0xa9,0x7a,0x7e,0x6a,
3602c2c66affSColin Finck 0x8e,0x50,0x4d,0x25,0x57,0xa1,0xbf,0x1b,0x15,0x05,0x43,0x7b,0x2c,0x05,0x8d,
3603c2c66affSColin Finck 0xbd,0x3d,0x03,0x8c,0x93,0x22,0x7d,0x63,0xea,0x0a,0x57,0x05,0x06,0x0a,0xdb,
3604c2c66affSColin Finck 0x61,0x98,0x65,0x2d,0x47,0x49,0xa8,0xe7,0xe6,0x56,0x75,0x5c,0xb8,0x64,0x08,
3605c2c66affSColin Finck 0x63,0xa9,0x30,0x40,0x66,0xb2,0xf9,0xb6,0xe3,0x34,0xe8,0x67,0x30,0xe1,0x43,
3606c2c66affSColin Finck 0x0b,0x87,0xff,0xc9,0xbe,0x72,0x10,0x5e,0x23,0xf0,0x9b,0xa7,0x48,0x65,0xbf,
3607c2c66affSColin Finck 0x09,0x88,0x7b,0xcd,0x72,0xbc,0x2e,0x79,0x9b,0x7b,0x02,0x03,0x01,0x00,0x01 };
3608c2c66affSColin Finck static BYTE msPubKey2[] = {
3609c2c66affSColin Finck 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xa9,0x02,0xbd,0xc1,0x70,0xe6,
3610c2c66affSColin Finck 0x3b,0xf2,0x4e,0x1b,0x28,0x9f,0x97,0x78,0x5e,0x30,0xea,0xa2,0xa9,0x8d,0x25,
3611c2c66affSColin Finck 0x5f,0xf8,0xfe,0x95,0x4c,0xa3,0xb7,0xfe,0x9d,0xa2,0x20,0x3e,0x7c,0x51,0xa2,
3612c2c66affSColin Finck 0x9b,0xa2,0x8f,0x60,0x32,0x6b,0xd1,0x42,0x64,0x79,0xee,0xac,0x76,0xc9,0x54,
3613c2c66affSColin Finck 0xda,0xf2,0xeb,0x9c,0x86,0x1c,0x8f,0x9f,0x84,0x66,0xb3,0xc5,0x6b,0x7a,0x62,
3614c2c66affSColin Finck 0x23,0xd6,0x1d,0x3c,0xde,0x0f,0x01,0x92,0xe8,0x96,0xc4,0xbf,0x2d,0x66,0x9a,
3615c2c66affSColin Finck 0x9a,0x68,0x26,0x99,0xd0,0x3a,0x2c,0xbf,0x0c,0xb5,0x58,0x26,0xc1,0x46,0xe7,
3616c2c66affSColin Finck 0x0a,0x3e,0x38,0x96,0x2c,0xa9,0x28,0x39,0xa8,0xec,0x49,0x83,0x42,0xe3,0x84,
3617c2c66affSColin Finck 0x0f,0xbb,0x9a,0x6c,0x55,0x61,0xac,0x82,0x7c,0xa1,0x60,0x2d,0x77,0x4c,0xe9,
3618c2c66affSColin Finck 0x99,0xb4,0x64,0x3b,0x9a,0x50,0x1c,0x31,0x08,0x24,0x14,0x9f,0xa9,0xe7,0x91,
3619c2c66affSColin Finck 0x2b,0x18,0xe6,0x3d,0x98,0x63,0x14,0x60,0x58,0x05,0x65,0x9f,0x1d,0x37,0x52,
3620c2c66affSColin Finck 0x87,0xf7,0xa7,0xef,0x94,0x02,0xc6,0x1b,0xd3,0xbf,0x55,0x45,0xb3,0x89,0x80,
3621c2c66affSColin Finck 0xbf,0x3a,0xec,0x54,0x94,0x4e,0xae,0xfd,0xa7,0x7a,0x6d,0x74,0x4e,0xaf,0x18,
3622c2c66affSColin Finck 0xcc,0x96,0x09,0x28,0x21,0x00,0x57,0x90,0x60,0x69,0x37,0xbb,0x4b,0x12,0x07,
3623c2c66affSColin Finck 0x3c,0x56,0xff,0x5b,0xfb,0xa4,0x66,0x0a,0x08,0xa6,0xd2,0x81,0x56,0x57,0xef,
3624c2c66affSColin Finck 0xb6,0x3b,0x5e,0x16,0x81,0x77,0x04,0xda,0xf6,0xbe,0xae,0x80,0x95,0xfe,0xb0,
3625c2c66affSColin Finck 0xcd,0x7f,0xd6,0xa7,0x1a,0x72,0x5c,0x3c,0xca,0xbc,0xf0,0x08,0xa3,0x22,0x30,
3626c2c66affSColin Finck 0xb3,0x06,0x85,0xc9,0xb3,0x20,0x77,0x13,0x85,0xdf,0x02,0x03,0x01,0x00,0x01 };
3627c2c66affSColin Finck static BYTE msPubKey3[] = {
3628c2c66affSColin Finck 0x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xf3,0x5d,0xfa,0x80,0x67,0xd4,
3629c2c66affSColin Finck 0x5a,0xa7,0xa9,0x0c,0x2c,0x90,0x20,0xd0,0x35,0x08,0x3c,0x75,0x84,0xcd,0xb7,
3630c2c66affSColin Finck 0x07,0x89,0x9c,0x89,0xda,0xde,0xce,0xc3,0x60,0xfa,0x91,0x68,0x5a,0x9e,0x94,
3631c2c66affSColin Finck 0x71,0x29,0x18,0x76,0x7c,0xc2,0xe0,0xc8,0x25,0x76,0x94,0x0e,0x58,0xfa,0x04,
3632c2c66affSColin Finck 0x34,0x36,0xe6,0xdf,0xaf,0xf7,0x80,0xba,0xe9,0x58,0x0b,0x2b,0x93,0xe5,0x9d,
3633c2c66affSColin Finck 0x05,0xe3,0x77,0x22,0x91,0xf7,0x34,0x64,0x3c,0x22,0x91,0x1d,0x5e,0xe1,0x09,
3634c2c66affSColin Finck 0x90,0xbc,0x14,0xfe,0xfc,0x75,0x58,0x19,0xe1,0x79,0xb7,0x07,0x92,0xa3,0xae,
3635c2c66affSColin Finck 0x88,0x59,0x08,0xd8,0x9f,0x07,0xca,0x03,0x58,0xfc,0x68,0x29,0x6d,0x32,0xd7,
3636c2c66affSColin Finck 0xd2,0xa8,0xcb,0x4b,0xfc,0xe1,0x0b,0x48,0x32,0x4f,0xe6,0xeb,0xb8,0xad,0x4f,
3637c2c66affSColin Finck 0xe4,0x5c,0x6f,0x13,0x94,0x99,0xdb,0x95,0xd5,0x75,0xdb,0xa8,0x1a,0xb7,0x94,
3638c2c66affSColin Finck 0x91,0xb4,0x77,0x5b,0xf5,0x48,0x0c,0x8f,0x6a,0x79,0x7d,0x14,0x70,0x04,0x7d,
3639c2c66affSColin Finck 0x6d,0xaf,0x90,0xf5,0xda,0x70,0xd8,0x47,0xb7,0xbf,0x9b,0x2f,0x6c,0xe7,0x05,
3640c2c66affSColin Finck 0xb7,0xe1,0x11,0x60,0xac,0x79,0x91,0x14,0x7c,0xc5,0xd6,0xa6,0xe4,0xe1,0x7e,
3641c2c66affSColin Finck 0xd5,0xc3,0x7e,0xe5,0x92,0xd2,0x3c,0x00,0xb5,0x36,0x82,0xde,0x79,0xe1,0x6d,
3642c2c66affSColin Finck 0xf3,0xb5,0x6e,0xf8,0x9f,0x33,0xc9,0xcb,0x52,0x7d,0x73,0x98,0x36,0xdb,0x8b,
3643c2c66affSColin Finck 0xa1,0x6b,0xa2,0x95,0x97,0x9b,0xa3,0xde,0xc2,0x4d,0x26,0xff,0x06,0x96,0x67,
3644c2c66affSColin Finck 0x25,0x06,0xc8,0xe7,0xac,0xe4,0xee,0x12,0x33,0x95,0x31,0x99,0xc8,0x35,0x08,
3645c2c66affSColin Finck 0x4e,0x34,0xca,0x79,0x53,0xd5,0xb5,0xbe,0x63,0x32,0x59,0x40,0x36,0xc0,0xa5,
3646c2c66affSColin Finck 0x4e,0x04,0x4d,0x3d,0xdb,0x5b,0x07,0x33,0xe4,0x58,0xbf,0xef,0x3f,0x53,0x64,
3647c2c66affSColin Finck 0xd8,0x42,0x59,0x35,0x57,0xfd,0x0f,0x45,0x7c,0x24,0x04,0x4d,0x9e,0xd6,0x38,
3648c2c66affSColin Finck 0x74,0x11,0x97,0x22,0x90,0xce,0x68,0x44,0x74,0x92,0x6f,0xd5,0x4b,0x6f,0xb0,
3649c2c66affSColin Finck 0x86,0xe3,0xc7,0x36,0x42,0xa0,0xd0,0xfc,0xc1,0xc0,0x5a,0xf9,0xa3,0x61,0xb9,
3650c2c66affSColin Finck 0x30,0x47,0x71,0x96,0x0a,0x16,0xb0,0x91,0xc0,0x42,0x95,0xef,0x10,0x7f,0x28,
3651c2c66affSColin Finck 0x6a,0xe3,0x2a,0x1f,0xb1,0xe4,0xcd,0x03,0x3f,0x77,0x71,0x04,0xc7,0x20,0xfc,
3652c2c66affSColin Finck 0x49,0x0f,0x1d,0x45,0x88,0xa4,0xd7,0xcb,0x7e,0x88,0xad,0x8e,0x2d,0xec,0x45,
3653c2c66affSColin Finck 0xdb,0xc4,0x51,0x04,0xc9,0x2a,0xfc,0xec,0x86,0x9e,0x9a,0x11,0x97,0x5b,0xde,
3654c2c66affSColin Finck 0xce,0x53,0x88,0xe6,0xe2,0xb7,0xfd,0xac,0x95,0xc2,0x28,0x40,0xdb,0xef,0x04,
3655c2c66affSColin Finck 0x90,0xdf,0x81,0x33,0x39,0xd9,0xb2,0x45,0xa5,0x23,0x87,0x06,0xa5,0x55,0x89,
3656c2c66affSColin Finck 0x31,0xbb,0x06,0x2d,0x60,0x0e,0x41,0x18,0x7d,0x1f,0x2e,0xb5,0x97,0xcb,0x11,
3657c2c66affSColin Finck 0xeb,0x15,0xd5,0x24,0xa5,0x94,0xef,0x15,0x14,0x89,0xfd,0x4b,0x73,0xfa,0x32,
3658c2c66affSColin Finck 0x5b,0xfc,0xd1,0x33,0x00,0xf9,0x59,0x62,0x70,0x07,0x32,0xea,0x2e,0xab,0x40,
3659c2c66affSColin Finck 0x2d,0x7b,0xca,0xdd,0x21,0x67,0x1b,0x30,0x99,0x8f,0x16,0xaa,0x23,0xa8,0x41,
3660c2c66affSColin Finck 0xd1,0xb0,0x6e,0x11,0x9b,0x36,0xc4,0xde,0x40,0x74,0x9c,0xe1,0x58,0x65,0xc1,
3661c2c66affSColin Finck 0x60,0x1e,0x7a,0x5b,0x38,0xc8,0x8f,0xbb,0x04,0x26,0x7c,0xd4,0x16,0x40,0xe5,
3662c2c66affSColin Finck 0xb6,0x6b,0x6c,0xaa,0x86,0xfd,0x00,0xbf,0xce,0xc1,0x35,0x02,0x03,0x01,0x00,
3663c2c66affSColin Finck 0x01 };
3664c2c66affSColin Finck
verify_ms_root_policy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)3665c2c66affSColin Finck static BOOL WINAPI verify_ms_root_policy(LPCSTR szPolicyOID,
3666c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3667c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3668c2c66affSColin Finck {
3669c2c66affSColin Finck BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
3670c2c66affSColin Finck pPolicyStatus);
3671c2c66affSColin Finck
3672c2c66affSColin Finck if (ret && !pPolicyStatus->dwError)
3673c2c66affSColin Finck {
3674c2c66affSColin Finck CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
3675c2c66affSColin Finck BOOL isMSRoot = FALSE;
3676c2c66affSColin Finck DWORD i;
3677c2c66affSColin Finck CRYPT_DATA_BLOB keyBlobs[] = {
3678c2c66affSColin Finck { sizeof(msPubKey1), msPubKey1 },
3679c2c66affSColin Finck { sizeof(msPubKey2), msPubKey2 },
3680c2c66affSColin Finck { sizeof(msPubKey3), msPubKey3 },
3681c2c66affSColin Finck };
3682c2c66affSColin Finck PCERT_SIMPLE_CHAIN rootChain =
3683c2c66affSColin Finck pChainContext->rgpChain[pChainContext->cChain -1 ];
3684c2c66affSColin Finck PCCERT_CONTEXT root =
3685c2c66affSColin Finck rootChain->rgpElement[rootChain->cElement - 1]->pCertContext;
3686c2c66affSColin Finck
3687*997d44c9SAmine Khaldi for (i = 0; !isMSRoot && i < ARRAY_SIZE(keyBlobs); i++)
3688c2c66affSColin Finck {
3689c2c66affSColin Finck msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
3690c2c66affSColin Finck msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
3691c2c66affSColin Finck if (CertComparePublicKeyInfo(
3692c2c66affSColin Finck X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
3693c2c66affSColin Finck &root->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
3694c2c66affSColin Finck isMSRoot = TRUE;
3695c2c66affSColin Finck }
3696c2c66affSColin Finck if (isMSRoot)
3697c2c66affSColin Finck pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = 0;
3698c2c66affSColin Finck }
3699c2c66affSColin Finck return ret;
3700c2c66affSColin Finck }
3701c2c66affSColin Finck
3702c2c66affSColin Finck typedef BOOL (WINAPI *CertVerifyCertificateChainPolicyFunc)(LPCSTR szPolicyOID,
3703c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3704c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus);
3705c2c66affSColin Finck
dump_policy_para(PCERT_CHAIN_POLICY_PARA para)3706c2c66affSColin Finck static void dump_policy_para(PCERT_CHAIN_POLICY_PARA para)
3707c2c66affSColin Finck {
3708c2c66affSColin Finck if (para)
3709c2c66affSColin Finck {
3710c2c66affSColin Finck TRACE_(chain)("cbSize = %d\n", para->cbSize);
3711c2c66affSColin Finck TRACE_(chain)("dwFlags = %08x\n", para->dwFlags);
3712c2c66affSColin Finck TRACE_(chain)("pvExtraPolicyPara = %p\n", para->pvExtraPolicyPara);
3713c2c66affSColin Finck }
3714c2c66affSColin Finck }
3715c2c66affSColin Finck
CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID,PCCERT_CHAIN_CONTEXT pChainContext,PCERT_CHAIN_POLICY_PARA pPolicyPara,PCERT_CHAIN_POLICY_STATUS pPolicyStatus)3716c2c66affSColin Finck BOOL WINAPI CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID,
3717c2c66affSColin Finck PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
3718c2c66affSColin Finck PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
3719c2c66affSColin Finck {
3720c2c66affSColin Finck static HCRYPTOIDFUNCSET set = NULL;
3721c2c66affSColin Finck BOOL ret = FALSE;
3722c2c66affSColin Finck CertVerifyCertificateChainPolicyFunc verifyPolicy = NULL;
3723c2c66affSColin Finck HCRYPTOIDFUNCADDR hFunc = NULL;
3724c2c66affSColin Finck
3725c2c66affSColin Finck TRACE("(%s, %p, %p, %p)\n", debugstr_a(szPolicyOID), pChainContext,
3726c2c66affSColin Finck pPolicyPara, pPolicyStatus);
3727c2c66affSColin Finck if (TRACE_ON(chain))
3728c2c66affSColin Finck dump_policy_para(pPolicyPara);
3729c2c66affSColin Finck
3730c2c66affSColin Finck if (IS_INTOID(szPolicyOID))
3731c2c66affSColin Finck {
3732c2c66affSColin Finck switch (LOWORD(szPolicyOID))
3733c2c66affSColin Finck {
3734c2c66affSColin Finck case LOWORD(CERT_CHAIN_POLICY_BASE):
3735c2c66affSColin Finck verifyPolicy = verify_base_policy;
3736c2c66affSColin Finck break;
3737c2c66affSColin Finck case LOWORD(CERT_CHAIN_POLICY_AUTHENTICODE):
3738c2c66affSColin Finck verifyPolicy = verify_authenticode_policy;
3739c2c66affSColin Finck break;
3740c2c66affSColin Finck case LOWORD(CERT_CHAIN_POLICY_SSL):
3741c2c66affSColin Finck verifyPolicy = verify_ssl_policy;
3742c2c66affSColin Finck break;
3743c2c66affSColin Finck case LOWORD(CERT_CHAIN_POLICY_BASIC_CONSTRAINTS):
3744c2c66affSColin Finck verifyPolicy = verify_basic_constraints_policy;
3745c2c66affSColin Finck break;
3746c2c66affSColin Finck case LOWORD(CERT_CHAIN_POLICY_MICROSOFT_ROOT):
3747c2c66affSColin Finck verifyPolicy = verify_ms_root_policy;
3748c2c66affSColin Finck break;
3749c2c66affSColin Finck default:
3750c2c66affSColin Finck FIXME("unimplemented for %d\n", LOWORD(szPolicyOID));
3751c2c66affSColin Finck }
3752c2c66affSColin Finck }
3753c2c66affSColin Finck if (!verifyPolicy)
3754c2c66affSColin Finck {
3755c2c66affSColin Finck if (!set)
3756c2c66affSColin Finck set = CryptInitOIDFunctionSet(
3757c2c66affSColin Finck CRYPT_OID_VERIFY_CERTIFICATE_CHAIN_POLICY_FUNC, 0);
3758c2c66affSColin Finck CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, szPolicyOID, 0,
3759c2c66affSColin Finck (void **)&verifyPolicy, &hFunc);
3760c2c66affSColin Finck }
3761c2c66affSColin Finck if (verifyPolicy)
3762c2c66affSColin Finck ret = verifyPolicy(szPolicyOID, pChainContext, pPolicyPara,
3763c2c66affSColin Finck pPolicyStatus);
3764c2c66affSColin Finck if (hFunc)
3765c2c66affSColin Finck CryptFreeOIDFunctionAddress(hFunc, 0);
3766c2c66affSColin Finck TRACE("returning %d (%08x)\n", ret, pPolicyStatus->dwError);
3767c2c66affSColin Finck return ret;
3768c2c66affSColin Finck }
3769