mshtml: Implement IHTMLStyle_get_backgroundColor.
[wine/hacks.git] / dlls / cryptnet / cryptnet_main.c
blob83346a0bbd21aa4e82783fa595ad1a4df30a1981
1 /*
2 * Copyright (C) 2006 Maarten Lankhorst
3 * Copyright 2007 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
28 #include <stdio.h>
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnt.h"
34 #include "winnls.h"
35 #include "wininet.h"
36 #include "objbase.h"
37 #include "wincrypt.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(cryptnet);
43 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
45 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
47 switch (fdwReason) {
48 case DLL_PROCESS_ATTACH:
49 DisableThreadLibraryCalls(hinstDLL);
50 break;
51 case DLL_PROCESS_DETACH:
52 /* Do uninitialisation here */
53 break;
54 default: break;
56 return TRUE;
59 static const WCHAR cryptNet[] = { 'c','r','y','p','t','n','e','t','.',
60 'd','l','l',0 };
61 static const WCHAR ldapProvOpenStore[] = { 'L','d','a','p','P','r','o','v',
62 'O','p','e','S','t','o','r','e',0 };
64 /***********************************************************************
65 * DllRegisterServer (CRYPTNET.@)
67 HRESULT WINAPI DllRegisterServer(void)
69 TRACE("\n");
70 CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING,
71 CRYPT_OID_VERIFY_REVOCATION_FUNC, 0, cryptNet);
72 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC, "Ldap",
73 cryptNet, "LdapProvOpenStore");
74 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC,
75 CERT_STORE_PROV_LDAP_W, cryptNet, "LdapProvOpenStore");
76 return S_OK;
79 /***********************************************************************
80 * DllUnregisterServer (CRYPTNET.@)
82 HRESULT WINAPI DllUnregisterServer(void)
84 TRACE("\n");
85 CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING,
86 CRYPT_OID_VERIFY_REVOCATION_FUNC, cryptNet);
87 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC, "Ldap");
88 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC,
89 CERT_STORE_PROV_LDAP_W);
90 return S_OK;
93 static const char *url_oid_to_str(LPCSTR oid)
95 if (HIWORD(oid))
96 return oid;
97 else
99 static char buf[10];
101 switch (LOWORD(oid))
103 #define _x(oid) case LOWORD(oid): return #oid
104 _x(URL_OID_CERTIFICATE_ISSUER);
105 _x(URL_OID_CERTIFICATE_CRL_DIST_POINT);
106 _x(URL_OID_CTL_ISSUER);
107 _x(URL_OID_CTL_NEXT_UPDATE);
108 _x(URL_OID_CRL_ISSUER);
109 _x(URL_OID_CERTIFICATE_FRESHEST_CRL);
110 _x(URL_OID_CRL_FRESHEST_CRL);
111 _x(URL_OID_CROSS_CERT_DIST_POINT);
112 #undef _x
113 default:
114 snprintf(buf, sizeof(buf), "%d", LOWORD(oid));
115 return buf;
120 typedef BOOL (WINAPI *UrlDllGetObjectUrlFunc)(LPCSTR, LPVOID, DWORD,
121 PCRYPT_URL_ARRAY, DWORD *, PCRYPT_URL_INFO, DWORD *, LPVOID);
123 static BOOL WINAPI CRYPT_GetUrlFromCertificateIssuer(LPCSTR pszUrlOid,
124 LPVOID pvPara, DWORD dwFlags, PCRYPT_URL_ARRAY pUrlArray, DWORD *pcbUrlArray,
125 PCRYPT_URL_INFO pUrlInfo, DWORD *pcbUrlInfo, LPVOID pvReserved)
127 /* FIXME: This depends on the AIA (authority info access) extension being
128 * supported in crypt32.
130 FIXME("\n");
131 SetLastError(CRYPT_E_NOT_FOUND);
132 return FALSE;
135 static BOOL WINAPI CRYPT_GetUrlFromCertificateCRLDistPoint(LPCSTR pszUrlOid,
136 LPVOID pvPara, DWORD dwFlags, PCRYPT_URL_ARRAY pUrlArray, DWORD *pcbUrlArray,
137 PCRYPT_URL_INFO pUrlInfo, DWORD *pcbUrlInfo, LPVOID pvReserved)
139 PCCERT_CONTEXT cert = pvPara;
140 PCERT_EXTENSION ext;
141 BOOL ret = FALSE;
143 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
144 if (dwFlags && !(dwFlags & CRYPT_GET_URL_FROM_EXTENSION))
146 SetLastError(CRYPT_E_NOT_FOUND);
147 return FALSE;
149 if ((ext = CertFindExtension(szOID_CRL_DIST_POINTS,
150 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
152 CRL_DIST_POINTS_INFO *info;
153 DWORD size;
155 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CRL_DIST_POINTS,
156 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
157 &info, &size);
158 if (ret)
160 DWORD i, cUrl, bytesNeeded = sizeof(CRYPT_URL_ARRAY);
162 for (i = 0, cUrl = 0; i < info->cDistPoint; i++)
163 if (info->rgDistPoint[i].DistPointName.dwDistPointNameChoice
164 == CRL_DIST_POINT_FULL_NAME)
166 DWORD j;
167 CERT_ALT_NAME_INFO *name =
168 &info->rgDistPoint[i].DistPointName.u.FullName;
170 for (j = 0; j < name->cAltEntry; j++)
171 if (name->rgAltEntry[j].dwAltNameChoice ==
172 CERT_ALT_NAME_URL)
174 if (name->rgAltEntry[j].u.pwszURL)
176 cUrl++;
177 bytesNeeded += sizeof(LPWSTR) +
178 (lstrlenW(name->rgAltEntry[j].u.pwszURL) + 1)
179 * sizeof(WCHAR);
183 if (!pcbUrlArray)
185 SetLastError(E_INVALIDARG);
186 ret = FALSE;
188 else if (!pUrlArray)
189 *pcbUrlArray = bytesNeeded;
190 else if (*pcbUrlArray < bytesNeeded)
192 SetLastError(ERROR_MORE_DATA);
193 *pcbUrlArray = bytesNeeded;
194 ret = FALSE;
196 else
198 LPWSTR nextUrl;
200 *pcbUrlArray = bytesNeeded;
201 pUrlArray->cUrl = 0;
202 pUrlArray->rgwszUrl =
203 (LPWSTR *)((BYTE *)pUrlArray + sizeof(CRYPT_URL_ARRAY));
204 nextUrl = (LPWSTR)((BYTE *)pUrlArray + sizeof(CRYPT_URL_ARRAY)
205 + cUrl * sizeof(LPWSTR));
206 for (i = 0; i < info->cDistPoint; i++)
207 if (info->rgDistPoint[i].DistPointName.dwDistPointNameChoice
208 == CRL_DIST_POINT_FULL_NAME)
210 DWORD j;
211 CERT_ALT_NAME_INFO *name =
212 &info->rgDistPoint[i].DistPointName.u.FullName;
214 for (j = 0; j < name->cAltEntry; j++)
215 if (name->rgAltEntry[j].dwAltNameChoice ==
216 CERT_ALT_NAME_URL)
218 if (name->rgAltEntry[j].u.pwszURL)
220 lstrcpyW(nextUrl,
221 name->rgAltEntry[j].u.pwszURL);
222 pUrlArray->rgwszUrl[pUrlArray->cUrl++] =
223 nextUrl;
224 nextUrl +=
225 (lstrlenW(name->rgAltEntry[j].u.pwszURL) + 1);
230 if (ret)
232 if (pcbUrlInfo)
234 FIXME("url info: stub\n");
235 if (!pUrlInfo)
236 *pcbUrlInfo = sizeof(CRYPT_URL_INFO);
237 else if (*pcbUrlInfo < sizeof(CRYPT_URL_INFO))
239 *pcbUrlInfo = sizeof(CRYPT_URL_INFO);
240 SetLastError(ERROR_MORE_DATA);
241 ret = FALSE;
243 else
245 *pcbUrlInfo = sizeof(CRYPT_URL_INFO);
246 memset(pUrlInfo, 0, sizeof(CRYPT_URL_INFO));
250 LocalFree(info);
253 else
254 SetLastError(CRYPT_E_NOT_FOUND);
255 return ret;
258 /***********************************************************************
259 * CryptGetObjectUrl (CRYPTNET.@)
261 BOOL WINAPI CryptGetObjectUrl(LPCSTR pszUrlOid, LPVOID pvPara, DWORD dwFlags,
262 PCRYPT_URL_ARRAY pUrlArray, DWORD *pcbUrlArray, PCRYPT_URL_INFO pUrlInfo,
263 DWORD *pcbUrlInfo, LPVOID pvReserved)
265 UrlDllGetObjectUrlFunc func = NULL;
266 HCRYPTOIDFUNCADDR hFunc = NULL;
267 BOOL ret = FALSE;
269 TRACE("(%s, %p, %08x, %p, %p, %p, %p, %p)\n", debugstr_a(pszUrlOid),
270 pvPara, dwFlags, pUrlArray, pcbUrlArray, pUrlInfo, pcbUrlInfo, pvReserved);
272 if (!HIWORD(pszUrlOid))
274 switch (LOWORD(pszUrlOid))
276 case LOWORD(URL_OID_CERTIFICATE_ISSUER):
277 func = CRYPT_GetUrlFromCertificateIssuer;
278 break;
279 case LOWORD(URL_OID_CERTIFICATE_CRL_DIST_POINT):
280 func = CRYPT_GetUrlFromCertificateCRLDistPoint;
281 break;
282 default:
283 FIXME("unimplemented for %s\n", url_oid_to_str(pszUrlOid));
284 SetLastError(ERROR_FILE_NOT_FOUND);
287 else
289 static HCRYPTOIDFUNCSET set = NULL;
291 if (!set)
292 set = CryptInitOIDFunctionSet(URL_OID_GET_OBJECT_URL_FUNC, 0);
293 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, pszUrlOid, 0,
294 (void **)&func, &hFunc);
296 if (func)
297 ret = func(pszUrlOid, pvPara, dwFlags, pUrlArray, pcbUrlArray,
298 pUrlInfo, pcbUrlInfo, pvReserved);
299 if (hFunc)
300 CryptFreeOIDFunctionAddress(hFunc, 0);
301 return ret;
304 /***********************************************************************
305 * CryptRetrieveObjectByUrlA (CRYPTNET.@)
307 BOOL WINAPI CryptRetrieveObjectByUrlA(LPCSTR pszURL, LPCSTR pszObjectOid,
308 DWORD dwRetrievalFlags, DWORD dwTimeout, LPVOID *ppvObject,
309 HCRYPTASYNC hAsyncRetrieve, PCRYPT_CREDENTIALS pCredentials, LPVOID pvVerify,
310 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
312 BOOL ret = FALSE;
313 int len;
315 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_a(pszURL),
316 debugstr_a(pszObjectOid), dwRetrievalFlags, dwTimeout, ppvObject,
317 hAsyncRetrieve, pCredentials, pvVerify, pAuxInfo);
319 if (!pszURL)
321 SetLastError(ERROR_INVALID_PARAMETER);
322 return FALSE;
324 len = MultiByteToWideChar(CP_ACP, 0, pszURL, -1, NULL, 0);
325 if (len)
327 LPWSTR url = CryptMemAlloc(len * sizeof(WCHAR));
329 if (url)
331 MultiByteToWideChar(CP_ACP, 0, pszURL, -1, url, len);
332 ret = CryptRetrieveObjectByUrlW(url, pszObjectOid,
333 dwRetrievalFlags, dwTimeout, ppvObject, hAsyncRetrieve,
334 pCredentials, pvVerify, pAuxInfo);
335 CryptMemFree(url);
337 else
338 SetLastError(ERROR_OUTOFMEMORY);
340 return ret;
343 static void WINAPI CRYPT_FreeBlob(LPCSTR pszObjectOid,
344 PCRYPT_BLOB_ARRAY pObject, void *pvFreeContext)
346 DWORD i;
348 for (i = 0; i < pObject->cBlob; i++)
349 CryptMemFree(pObject->rgBlob[i].pbData);
350 CryptMemFree(pObject->rgBlob);
353 static BOOL CRYPT_GetObjectFromFile(HANDLE hFile, PCRYPT_BLOB_ARRAY pObject)
355 BOOL ret;
356 LARGE_INTEGER size;
358 if ((ret = GetFileSizeEx(hFile, &size)))
360 if (size.u.HighPart)
362 WARN("file too big\n");
363 SetLastError(ERROR_INVALID_DATA);
364 ret = FALSE;
366 else
368 CRYPT_DATA_BLOB blob;
370 blob.pbData = CryptMemAlloc(size.u.LowPart);
371 if (blob.pbData)
373 blob.cbData = size.u.LowPart;
374 ret = ReadFile(hFile, blob.pbData, size.u.LowPart, &blob.cbData,
375 NULL);
376 if (ret)
378 pObject->rgBlob = CryptMemAlloc(sizeof(CRYPT_DATA_BLOB));
379 if (pObject->rgBlob)
381 pObject->cBlob = 1;
382 memcpy(pObject->rgBlob, &blob, sizeof(CRYPT_DATA_BLOB));
384 else
386 SetLastError(ERROR_OUTOFMEMORY);
387 ret = FALSE;
390 if (!ret)
391 CryptMemFree(blob.pbData);
393 else
395 SetLastError(ERROR_OUTOFMEMORY);
396 ret = FALSE;
400 return ret;
403 static BOOL CRYPT_GetObjectFromCache(LPCWSTR pszURL, PCRYPT_BLOB_ARRAY pObject,
404 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
406 BOOL ret = FALSE;
407 INTERNET_CACHE_ENTRY_INFOW *pCacheInfo = NULL;
408 DWORD size = 0;
410 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL), pObject, pAuxInfo);
412 ret = GetUrlCacheEntryInfoW(pszURL, NULL, &size);
413 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
415 pCacheInfo = CryptMemAlloc(size);
416 if (pCacheInfo)
417 ret = TRUE;
418 else
419 SetLastError(ERROR_OUTOFMEMORY);
421 if (ret && (ret = GetUrlCacheEntryInfoW(pszURL, pCacheInfo, &size)))
423 FILETIME ft;
425 GetSystemTimeAsFileTime(&ft);
426 if (CompareFileTime(&pCacheInfo->ExpireTime, &ft) >= 0)
428 HANDLE hFile = CreateFileW(pCacheInfo->lpszLocalFileName,
429 GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
431 if (hFile != INVALID_HANDLE_VALUE)
433 if ((ret = CRYPT_GetObjectFromFile(hFile, pObject)))
435 if (pAuxInfo && pAuxInfo->cbSize >=
436 offsetof(CRYPT_RETRIEVE_AUX_INFO,
437 pLastSyncTime) + sizeof(PFILETIME) &&
438 pAuxInfo->pLastSyncTime)
439 memcpy(pAuxInfo->pLastSyncTime,
440 &pCacheInfo->LastSyncTime,
441 sizeof(FILETIME));
443 CloseHandle(hFile);
445 else
447 DeleteUrlCacheEntryW(pszURL);
448 ret = FALSE;
451 else
453 DeleteUrlCacheEntryW(pszURL);
454 ret = FALSE;
457 CryptMemFree(pCacheInfo);
458 TRACE("returning %d\n", ret);
459 return ret;
462 /* Parses the URL, and sets components's lpszHostName and lpszUrlPath members
463 * to NULL-terminated copies of those portions of the URL (to be freed with
464 * CryptMemFree.)
466 static BOOL CRYPT_CrackUrl(LPCWSTR pszURL, URL_COMPONENTSW *components)
468 BOOL ret;
470 TRACE("(%s, %p)\n", debugstr_w(pszURL), components);
472 memset(components, 0, sizeof(*components));
473 components->dwStructSize = sizeof(*components);
474 components->lpszHostName = CryptMemAlloc(MAX_PATH * sizeof(WCHAR));
475 components->dwHostNameLength = MAX_PATH;
476 components->lpszUrlPath = CryptMemAlloc(MAX_PATH * 2 * sizeof(WCHAR));
477 components->dwUrlPathLength = 2 * MAX_PATH;
478 ret = InternetCrackUrlW(pszURL, 0, ICU_DECODE, components);
479 if (ret)
481 if ((components->dwUrlPathLength == 2 * MAX_PATH - 1) ||
482 (components->dwHostNameLength == MAX_PATH - 1))
483 FIXME("Buffers are too small\n");
484 switch (components->nScheme)
486 case INTERNET_SCHEME_FTP:
487 if (!components->nPort)
488 components->nPort = INTERNET_DEFAULT_FTP_PORT;
489 break;
490 case INTERNET_SCHEME_HTTP:
491 if (!components->nPort)
492 components->nPort = INTERNET_DEFAULT_HTTP_PORT;
493 break;
494 default:
495 ; /* do nothing */
498 TRACE("returning %d\n", ret);
499 return ret;
502 struct InetContext
504 HANDLE event;
505 DWORD timeout;
506 DWORD error;
509 static struct InetContext *CRYPT_MakeInetContext(DWORD dwTimeout)
511 struct InetContext *context = CryptMemAlloc(sizeof(struct InetContext));
513 if (context)
515 context->event = CreateEventW(NULL, FALSE, FALSE, NULL);
516 if (!context->event)
518 CryptMemFree(context);
519 context = NULL;
521 else
523 context->timeout = dwTimeout;
524 context->error = ERROR_SUCCESS;
527 return context;
530 static BOOL CRYPT_DownloadObject(DWORD dwRetrievalFlags, HINTERNET hHttp,
531 struct InetContext *context, PCRYPT_BLOB_ARRAY pObject,
532 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
534 CRYPT_DATA_BLOB object = { 0, NULL };
535 DWORD bytesAvailable;
536 BOOL ret;
538 do {
539 if ((ret = InternetQueryDataAvailable(hHttp, &bytesAvailable, 0, 0)))
541 if (bytesAvailable)
543 if (object.pbData)
544 object.pbData = CryptMemRealloc(object.pbData,
545 object.cbData + bytesAvailable);
546 else
547 object.pbData = CryptMemAlloc(bytesAvailable);
548 if (object.pbData)
550 INTERNET_BUFFERSA buffer = { sizeof(buffer), 0 };
552 buffer.dwBufferLength = bytesAvailable;
553 buffer.lpvBuffer = object.pbData + object.cbData;
554 if (!(ret = InternetReadFileExA(hHttp, &buffer, IRF_NO_WAIT,
555 (DWORD_PTR)context)))
557 if (GetLastError() == ERROR_IO_PENDING)
559 if (WaitForSingleObject(context->event,
560 context->timeout) == WAIT_TIMEOUT)
561 SetLastError(ERROR_TIMEOUT);
562 else if (context->error)
563 SetLastError(context->error);
564 else
565 ret = TRUE;
568 if (ret)
569 object.cbData += bytesAvailable;
571 else
573 SetLastError(ERROR_OUTOFMEMORY);
574 ret = FALSE;
578 else if (GetLastError() == ERROR_IO_PENDING)
580 if (WaitForSingleObject(context->event, context->timeout) ==
581 WAIT_TIMEOUT)
582 SetLastError(ERROR_TIMEOUT);
583 else
584 ret = TRUE;
586 } while (ret && bytesAvailable);
587 if (ret)
589 pObject->rgBlob = CryptMemAlloc(sizeof(CRYPT_DATA_BLOB));
590 if (!pObject->rgBlob)
592 CryptMemFree(object.pbData);
593 SetLastError(ERROR_OUTOFMEMORY);
594 ret = FALSE;
596 else
598 pObject->rgBlob[0].cbData = object.cbData;
599 pObject->rgBlob[0].pbData = object.pbData;
600 pObject->cBlob = 1;
603 TRACE("returning %d\n", ret);
604 return ret;
607 /* Finds the object specified by pszURL in the cache. If it's not found,
608 * creates a new cache entry for the object and writes the object to it.
609 * Sets the expiration time of the cache entry to expires.
611 static void CRYPT_CacheURL(LPCWSTR pszURL, PCRYPT_BLOB_ARRAY pObject,
612 DWORD dwRetrievalFlags, FILETIME expires)
614 WCHAR cacheFileName[MAX_PATH];
615 DWORD size = 0;
616 BOOL ret, create = FALSE;
618 GetUrlCacheEntryInfoW(pszURL, NULL, &size);
619 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
621 INTERNET_CACHE_ENTRY_INFOW *info = CryptMemAlloc(size);
623 if (info)
625 FILETIME ft;
627 ret = GetUrlCacheEntryInfoW(pszURL, info, &size);
628 if (ret)
629 lstrcpyW(cacheFileName, info->lpszLocalFileName);
630 /* Check if the existing cache entry is up to date. If it isn't,
631 * overwite it with the new value.
633 GetSystemTimeAsFileTime(&ft);
634 if (CompareFileTime(&info->ExpireTime, &ft) < 0)
635 create = TRUE;
636 CryptMemFree(info);
638 else
639 ret = FALSE;
641 else
643 ret = CreateUrlCacheEntryW(pszURL, pObject->rgBlob[0].cbData, NULL,
644 cacheFileName, 0);
645 create = TRUE;
647 if (ret)
649 DWORD entryType;
650 FILETIME ft = { 0 };
652 if (create)
654 HANDLE hCacheFile = CreateFileW(cacheFileName, GENERIC_WRITE, 0,
655 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
657 if (hCacheFile != INVALID_HANDLE_VALUE)
659 DWORD bytesWritten;
661 WriteFile(hCacheFile, pObject->rgBlob[0].pbData,
662 pObject->rgBlob[0].cbData, &bytesWritten, NULL);
663 CloseHandle(hCacheFile);
665 else
666 ret = FALSE;
668 if (ret)
670 if (!(dwRetrievalFlags & CRYPT_STICKY_CACHE_RETRIEVAL))
671 entryType = NORMAL_CACHE_ENTRY;
672 else
673 entryType = STICKY_CACHE_ENTRY;
674 CommitUrlCacheEntryW(pszURL, cacheFileName, expires, ft, entryType,
675 NULL, 0, NULL, NULL);
680 static void CALLBACK CRYPT_InetStatusCallback(HINTERNET hInt,
681 DWORD_PTR dwContext, DWORD status, void *statusInfo, DWORD statusInfoLen)
683 struct InetContext *context = (struct InetContext *)dwContext;
684 LPINTERNET_ASYNC_RESULT result;
686 switch (status)
688 case INTERNET_STATUS_REQUEST_COMPLETE:
689 result = statusInfo;
690 context->error = result->dwError;
691 SetEvent(context->event);
695 static BOOL CRYPT_Connect(URL_COMPONENTSW *components,
696 struct InetContext *context, PCRYPT_CREDENTIALS pCredentials,
697 HINTERNET *phInt, HINTERNET *phHost)
699 BOOL ret;
701 TRACE("(%s:%d, %p, %p, %p, %p)\n", debugstr_w(components->lpszHostName),
702 components->nPort, context, pCredentials, phInt, phInt);
704 *phHost = NULL;
705 *phInt = InternetOpenW(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL,
706 context ? INTERNET_FLAG_ASYNC : 0);
707 if (*phInt)
709 DWORD service;
711 if (context)
712 InternetSetStatusCallbackW(*phInt, CRYPT_InetStatusCallback);
713 switch (components->nScheme)
715 case INTERNET_SCHEME_FTP:
716 service = INTERNET_SERVICE_FTP;
717 break;
718 case INTERNET_SCHEME_HTTP:
719 service = INTERNET_SERVICE_HTTP;
720 break;
721 default:
722 service = 0;
724 /* FIXME: use pCredentials for username/password */
725 *phHost = InternetConnectW(*phInt, components->lpszHostName,
726 components->nPort, NULL, NULL, service, 0, (DWORD_PTR)context);
727 if (!*phHost)
729 InternetCloseHandle(*phInt);
730 *phInt = NULL;
731 ret = FALSE;
733 else
734 ret = TRUE;
736 else
737 ret = FALSE;
738 TRACE("returning %d\n", ret);
739 return ret;
742 static BOOL WINAPI FTP_RetrieveEncodedObjectW(LPCWSTR pszURL,
743 LPCSTR pszObjectOid, DWORD dwRetrievalFlags, DWORD dwTimeout,
744 PCRYPT_BLOB_ARRAY pObject, PFN_FREE_ENCODED_OBJECT_FUNC *ppfnFreeObject,
745 void **ppvFreeContext, HCRYPTASYNC hAsyncRetrieve,
746 PCRYPT_CREDENTIALS pCredentials, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
748 FIXME("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL),
749 debugstr_a(pszObjectOid), dwRetrievalFlags, dwTimeout, pObject,
750 ppfnFreeObject, ppvFreeContext, hAsyncRetrieve, pCredentials, pAuxInfo);
752 pObject->cBlob = 0;
753 pObject->rgBlob = NULL;
754 *ppfnFreeObject = CRYPT_FreeBlob;
755 *ppvFreeContext = NULL;
756 return FALSE;
759 static const WCHAR x509cacert[] = { 'a','p','p','l','i','c','a','t','i','o','n',
760 '/','x','-','x','5','0','9','-','c','a','-','c','e','r','t',0 };
761 static const WCHAR x509emailcert[] = { 'a','p','p','l','i','c','a','t','i','o',
762 'n','/','x','-','x','5','0','9','-','e','m','a','i','l','-','c','e','r','t',
763 0 };
764 static const WCHAR x509servercert[] = { 'a','p','p','l','i','c','a','t','i','o',
765 'n','/','x','-','x','5','0','9','-','s','e','r','v','e','r','-','c','e','r',
766 't',0 };
767 static const WCHAR x509usercert[] = { 'a','p','p','l','i','c','a','t','i','o',
768 'n','/','x','-','x','5','0','9','-','u','s','e','r','-','c','e','r','t',0 };
769 static const WCHAR pkcs7cert[] = { 'a','p','p','l','i','c','a','t','i','o','n',
770 '/','x','-','p','k','c','s','7','-','c','e','r','t','i','f','c','a','t','e',
771 's',0 };
772 static const WCHAR pkixCRL[] = { 'a','p','p','l','i','c','a','t','i','o','n',
773 '/','p','k','i','x','-','c','r','l',0 };
774 static const WCHAR pkcs7CRL[] = { 'a','p','p','l','i','c','a','t','i','o','n',
775 '/','x','-','p','k','c','s','7','-','c','r','l',0 };
776 static const WCHAR pkcs7sig[] = { 'a','p','p','l','i','c','a','t','i','o','n',
777 '/','x','-','p','k','c','s','7','-','s','i','g','n','a','t','u','r','e',0 };
778 static const WCHAR pkcs7mime[] = { 'a','p','p','l','i','c','a','t','i','o','n',
779 '/','x','-','p','k','c','s','7','-','m','i','m','e',0 };
781 static BOOL WINAPI HTTP_RetrieveEncodedObjectW(LPCWSTR pszURL,
782 LPCSTR pszObjectOid, DWORD dwRetrievalFlags, DWORD dwTimeout,
783 PCRYPT_BLOB_ARRAY pObject, PFN_FREE_ENCODED_OBJECT_FUNC *ppfnFreeObject,
784 void **ppvFreeContext, HCRYPTASYNC hAsyncRetrieve,
785 PCRYPT_CREDENTIALS pCredentials, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
787 BOOL ret = FALSE;
789 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL),
790 debugstr_a(pszObjectOid), dwRetrievalFlags, dwTimeout, pObject,
791 ppfnFreeObject, ppvFreeContext, hAsyncRetrieve, pCredentials, pAuxInfo);
793 pObject->cBlob = 0;
794 pObject->rgBlob = NULL;
795 *ppfnFreeObject = CRYPT_FreeBlob;
796 *ppvFreeContext = NULL;
798 if (!(dwRetrievalFlags & CRYPT_WIRE_ONLY_RETRIEVAL))
799 ret = CRYPT_GetObjectFromCache(pszURL, pObject, pAuxInfo);
800 if (!ret && (!(dwRetrievalFlags & CRYPT_CACHE_ONLY_RETRIEVAL) ||
801 (dwRetrievalFlags & CRYPT_WIRE_ONLY_RETRIEVAL)))
803 URL_COMPONENTSW components;
805 if ((ret = CRYPT_CrackUrl(pszURL, &components)))
807 HINTERNET hInt, hHost;
808 struct InetContext *context = NULL;
810 if (dwTimeout)
811 context = CRYPT_MakeInetContext(dwTimeout);
812 ret = CRYPT_Connect(&components, context, pCredentials, &hInt,
813 &hHost);
814 if (ret)
816 static LPCWSTR types[] = { x509cacert, x509emailcert,
817 x509servercert, x509usercert, pkcs7cert, pkixCRL, pkcs7CRL,
818 pkcs7sig, pkcs7mime, NULL };
819 HINTERNET hHttp = HttpOpenRequestW(hHost, NULL,
820 components.lpszUrlPath, NULL, NULL, types,
821 INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI,
822 (DWORD_PTR)context);
824 if (hHttp)
826 if (dwTimeout)
828 InternetSetOptionW(hHttp,
829 INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeout,
830 sizeof(dwTimeout));
831 InternetSetOptionW(hHttp, INTERNET_OPTION_SEND_TIMEOUT,
832 &dwTimeout, sizeof(dwTimeout));
834 ret = HttpSendRequestExW(hHttp, NULL, NULL, 0,
835 (DWORD_PTR)context);
836 if (!ret && GetLastError() == ERROR_IO_PENDING)
838 if (WaitForSingleObject(context->event,
839 context->timeout) == WAIT_TIMEOUT)
840 SetLastError(ERROR_TIMEOUT);
841 else
842 ret = TRUE;
844 /* We don't set ret to TRUE in this block to avoid masking
845 * an error from HttpSendRequestExW.
847 if (!HttpEndRequestW(hHttp, NULL, 0, (DWORD_PTR)context) &&
848 GetLastError() == ERROR_IO_PENDING)
850 if (WaitForSingleObject(context->event,
851 context->timeout) == WAIT_TIMEOUT)
853 SetLastError(ERROR_TIMEOUT);
854 ret = FALSE;
857 if (ret)
858 ret = CRYPT_DownloadObject(dwRetrievalFlags, hHttp,
859 context, pObject, pAuxInfo);
860 if (ret && !(dwRetrievalFlags & CRYPT_DONT_CACHE_RESULT))
862 SYSTEMTIME st;
863 DWORD len = sizeof(st);
865 if (HttpQueryInfoW(hHttp,
866 HTTP_QUERY_EXPIRES | HTTP_QUERY_FLAG_SYSTEMTIME, &st,
867 &len, NULL))
869 FILETIME ft;
871 SystemTimeToFileTime(&st, &ft);
872 CRYPT_CacheURL(pszURL, pObject, dwRetrievalFlags,
873 ft);
876 InternetCloseHandle(hHttp);
878 InternetCloseHandle(hHost);
879 InternetCloseHandle(hInt);
881 if (context)
883 CloseHandle(context->event);
884 CryptMemFree(context);
886 CryptMemFree(components.lpszUrlPath);
887 CryptMemFree(components.lpszHostName);
890 TRACE("returning %d\n", ret);
891 return ret;
894 static BOOL WINAPI File_RetrieveEncodedObjectW(LPCWSTR pszURL,
895 LPCSTR pszObjectOid, DWORD dwRetrievalFlags, DWORD dwTimeout,
896 PCRYPT_BLOB_ARRAY pObject, PFN_FREE_ENCODED_OBJECT_FUNC *ppfnFreeObject,
897 void **ppvFreeContext, HCRYPTASYNC hAsyncRetrieve,
898 PCRYPT_CREDENTIALS pCredentials, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
900 URL_COMPONENTSW components = { sizeof(components), 0 };
901 BOOL ret;
903 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL),
904 debugstr_a(pszObjectOid), dwRetrievalFlags, dwTimeout, pObject,
905 ppfnFreeObject, ppvFreeContext, hAsyncRetrieve, pCredentials, pAuxInfo);
907 pObject->cBlob = 0;
908 pObject->rgBlob = NULL;
909 *ppfnFreeObject = CRYPT_FreeBlob;
910 *ppvFreeContext = NULL;
912 components.lpszUrlPath = CryptMemAlloc(MAX_PATH * 2 * sizeof(WCHAR));
913 components.dwUrlPathLength = 2 * MAX_PATH;
914 ret = InternetCrackUrlW(pszURL, 0, ICU_DECODE, &components);
915 if (ret)
917 LPWSTR path;
919 if (components.dwUrlPathLength == 2 * MAX_PATH - 1)
920 FIXME("Buffers are too small\n");
922 /* 3 == lstrlenW(L"c:") + 1 */
923 path = CryptMemAlloc((components.dwUrlPathLength + 3) * sizeof(WCHAR));
924 if (path)
926 HANDLE hFile;
928 /* Try to create the file directly - Wine handles / in pathnames */
929 lstrcpynW(path, components.lpszUrlPath,
930 components.dwUrlPathLength + 1);
931 hFile = CreateFileW(path, GENERIC_READ, 0, NULL, OPEN_EXISTING,
932 FILE_ATTRIBUTE_NORMAL, NULL);
933 if (hFile == INVALID_HANDLE_VALUE)
935 /* Try again on the current drive */
936 GetCurrentDirectoryW(components.dwUrlPathLength, path);
937 if (path[1] == ':')
939 lstrcpynW(path + 2, components.lpszUrlPath,
940 components.dwUrlPathLength + 1);
941 hFile = CreateFileW(path, GENERIC_READ, 0, NULL,
942 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
944 if (hFile == INVALID_HANDLE_VALUE)
946 /* Try again on the Windows drive */
947 GetWindowsDirectoryW(path, components.dwUrlPathLength);
948 if (path[1] == ':')
950 lstrcpynW(path + 2, components.lpszUrlPath,
951 components.dwUrlPathLength + 1);
952 hFile = CreateFileW(path, GENERIC_READ, 0, NULL,
953 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
957 if (hFile != INVALID_HANDLE_VALUE)
959 if ((ret = CRYPT_GetObjectFromFile(hFile, pObject)))
961 if (pAuxInfo && pAuxInfo->cbSize >=
962 offsetof(CRYPT_RETRIEVE_AUX_INFO,
963 pLastSyncTime) + sizeof(PFILETIME) &&
964 pAuxInfo->pLastSyncTime)
965 GetFileTime(hFile, NULL, NULL,
966 pAuxInfo->pLastSyncTime);
968 CloseHandle(hFile);
970 else
971 ret = FALSE;
972 CryptMemFree(path);
975 CryptMemFree(components.lpszUrlPath);
976 return ret;
979 typedef BOOL (WINAPI *SchemeDllRetrieveEncodedObjectW)(LPCWSTR pwszUrl,
980 LPCSTR pszObjectOid, DWORD dwRetrievalFlags, DWORD dwTimeout,
981 PCRYPT_BLOB_ARRAY pObject, PFN_FREE_ENCODED_OBJECT_FUNC *ppfnFreeObject,
982 void **ppvFreeContext, HCRYPTASYNC hAsyncRetrieve,
983 PCRYPT_CREDENTIALS pCredentials, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo);
985 static BOOL CRYPT_GetRetrieveFunction(LPCWSTR pszURL,
986 SchemeDllRetrieveEncodedObjectW *pFunc, HCRYPTOIDFUNCADDR *phFunc)
988 URL_COMPONENTSW components = { sizeof(components), 0 };
989 BOOL ret;
991 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL), pFunc, phFunc);
993 *pFunc = NULL;
994 *phFunc = 0;
995 components.dwSchemeLength = 1;
996 ret = InternetCrackUrlW(pszURL, 0, 0, &components);
997 if (ret)
999 /* Microsoft always uses CryptInitOIDFunctionSet/
1000 * CryptGetOIDFunctionAddress, but there doesn't seem to be a pressing
1001 * reason to do so for builtin schemes.
1003 switch (components.nScheme)
1005 case INTERNET_SCHEME_FTP:
1006 *pFunc = FTP_RetrieveEncodedObjectW;
1007 break;
1008 case INTERNET_SCHEME_HTTP:
1009 *pFunc = HTTP_RetrieveEncodedObjectW;
1010 break;
1011 case INTERNET_SCHEME_FILE:
1012 *pFunc = File_RetrieveEncodedObjectW;
1013 break;
1014 default:
1016 int len = WideCharToMultiByte(CP_ACP, 0, components.lpszScheme,
1017 components.dwSchemeLength, NULL, 0, NULL, NULL);
1019 if (len)
1021 LPSTR scheme = CryptMemAlloc(len);
1023 if (scheme)
1025 static HCRYPTOIDFUNCSET set = NULL;
1027 if (!set)
1028 set = CryptInitOIDFunctionSet(
1029 SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC, 0);
1030 WideCharToMultiByte(CP_ACP, 0, components.lpszScheme,
1031 components.dwSchemeLength, scheme, len, NULL, NULL);
1032 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1033 scheme, 0, (void **)pFunc, phFunc);
1034 CryptMemFree(scheme);
1036 else
1038 SetLastError(ERROR_OUTOFMEMORY);
1039 ret = FALSE;
1042 else
1043 ret = FALSE;
1047 TRACE("returning %d\n", ret);
1048 return ret;
1051 static BOOL WINAPI CRYPT_CreateBlob(LPCSTR pszObjectOid,
1052 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1054 DWORD size, i;
1055 CRYPT_BLOB_ARRAY *context;
1056 BOOL ret = FALSE;
1058 size = sizeof(CRYPT_BLOB_ARRAY) + pObject->cBlob * sizeof(CRYPT_DATA_BLOB);
1059 for (i = 0; i < pObject->cBlob; i++)
1060 size += pObject->rgBlob[i].cbData;
1061 context = CryptMemAlloc(size);
1062 if (context)
1064 LPBYTE nextData;
1066 context->cBlob = 0;
1067 context->rgBlob =
1068 (CRYPT_DATA_BLOB *)((LPBYTE)context + sizeof(CRYPT_BLOB_ARRAY));
1069 nextData =
1070 (LPBYTE)context->rgBlob + pObject->cBlob * sizeof(CRYPT_DATA_BLOB);
1071 for (i = 0; i < pObject->cBlob; i++)
1073 memcpy(nextData, pObject->rgBlob[i].pbData,
1074 pObject->rgBlob[i].cbData);
1075 context->rgBlob[i].pbData = nextData;
1076 context->rgBlob[i].cbData = pObject->rgBlob[i].cbData;
1077 nextData += pObject->rgBlob[i].cbData;
1078 context->cBlob++;
1080 *ppvContext = context;
1081 ret = TRUE;
1083 return ret;
1086 typedef BOOL (WINAPI *AddContextToStore)(HCERTSTORE hCertStore,
1087 const void *pContext, DWORD dwAddDisposition, const void **ppStoreContext);
1089 static BOOL CRYPT_CreateContext(PCRYPT_BLOB_ARRAY pObject,
1090 DWORD dwExpectedContentTypeFlags, AddContextToStore addFunc, void **ppvContext)
1092 BOOL ret = TRUE;
1094 if (!pObject->cBlob)
1096 SetLastError(ERROR_INVALID_DATA);
1097 *ppvContext = NULL;
1098 ret = FALSE;
1100 else if (pObject->cBlob == 1)
1102 if (!CryptQueryObject(CERT_QUERY_OBJECT_BLOB, &pObject->rgBlob[0],
1103 dwExpectedContentTypeFlags, CERT_QUERY_FORMAT_FLAG_BINARY, 0, NULL,
1104 NULL, NULL, NULL, NULL, (const void **)ppvContext))
1106 SetLastError(CRYPT_E_NO_MATCH);
1107 ret = FALSE;
1110 else
1112 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1113 CERT_STORE_CREATE_NEW_FLAG, NULL);
1115 if (store)
1117 DWORD i;
1118 const void *context;
1120 for (i = 0; i < pObject->cBlob; i++)
1122 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
1123 &pObject->rgBlob[i], dwExpectedContentTypeFlags,
1124 CERT_QUERY_FORMAT_FLAG_BINARY, 0, NULL, NULL, NULL, NULL,
1125 NULL, &context))
1127 if (!addFunc(store, context, CERT_STORE_ADD_ALWAYS, NULL))
1128 ret = FALSE;
1130 else
1132 SetLastError(CRYPT_E_NO_MATCH);
1133 ret = FALSE;
1137 else
1138 ret = FALSE;
1139 *ppvContext = store;
1141 return ret;
1144 static BOOL WINAPI CRYPT_CreateCert(LPCSTR pszObjectOid,
1145 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1147 return CRYPT_CreateContext(pObject, CERT_QUERY_CONTENT_FLAG_CERT,
1148 (AddContextToStore)CertAddCertificateContextToStore, ppvContext);
1151 static BOOL WINAPI CRYPT_CreateCRL(LPCSTR pszObjectOid,
1152 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1154 return CRYPT_CreateContext(pObject, CERT_QUERY_CONTENT_FLAG_CRL,
1155 (AddContextToStore)CertAddCRLContextToStore, ppvContext);
1158 static BOOL WINAPI CRYPT_CreateCTL(LPCSTR pszObjectOid,
1159 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1161 return CRYPT_CreateContext(pObject, CERT_QUERY_CONTENT_FLAG_CTL,
1162 (AddContextToStore)CertAddCTLContextToStore, ppvContext);
1165 static BOOL WINAPI CRYPT_CreatePKCS7(LPCSTR pszObjectOid,
1166 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1168 BOOL ret;
1170 if (!pObject->cBlob)
1172 SetLastError(ERROR_INVALID_DATA);
1173 *ppvContext = NULL;
1174 ret = FALSE;
1176 else if (pObject->cBlob == 1)
1177 ret = CryptQueryObject(CERT_QUERY_OBJECT_BLOB, &pObject->rgBlob[0],
1178 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
1179 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED, CERT_QUERY_FORMAT_FLAG_BINARY,
1180 0, NULL, NULL, NULL, ppvContext, NULL, NULL);
1181 else
1183 FIXME("multiple messages unimplemented\n");
1184 ret = FALSE;
1186 return ret;
1189 static BOOL WINAPI CRYPT_CreateAny(LPCSTR pszObjectOid,
1190 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext)
1192 BOOL ret;
1194 if (!pObject->cBlob)
1196 SetLastError(ERROR_INVALID_DATA);
1197 *ppvContext = NULL;
1198 ret = FALSE;
1200 else
1202 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1203 CERT_STORE_CREATE_NEW_FLAG, NULL);
1205 if (store)
1207 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1208 CERT_STORE_CREATE_NEW_FLAG, NULL);
1210 if (memStore)
1212 CertAddStoreToCollection(store, memStore,
1213 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1214 CertCloseStore(memStore, 0);
1216 else
1218 CertCloseStore(store, 0);
1219 store = NULL;
1222 if (store)
1224 DWORD i;
1226 ret = TRUE;
1227 for (i = 0; i < pObject->cBlob; i++)
1229 DWORD contentType, expectedContentTypes =
1230 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
1231 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED |
1232 CERT_QUERY_CONTENT_FLAG_CERT |
1233 CERT_QUERY_CONTENT_FLAG_CRL |
1234 CERT_QUERY_CONTENT_FLAG_CTL;
1235 HCERTSTORE contextStore;
1236 const void *context;
1238 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
1239 &pObject->rgBlob[i], expectedContentTypes,
1240 CERT_QUERY_FORMAT_FLAG_BINARY, 0, NULL, &contentType, NULL,
1241 &contextStore, NULL, &context))
1243 switch (contentType)
1245 case CERT_QUERY_CONTENT_CERT:
1246 if (!CertAddCertificateContextToStore(store,
1247 context, CERT_STORE_ADD_ALWAYS, NULL))
1248 ret = FALSE;
1249 break;
1250 case CERT_QUERY_CONTENT_CRL:
1251 if (!CertAddCRLContextToStore(store,
1252 context, CERT_STORE_ADD_ALWAYS, NULL))
1253 ret = FALSE;
1254 break;
1255 case CERT_QUERY_CONTENT_CTL:
1256 if (!CertAddCTLContextToStore(store,
1257 context, CERT_STORE_ADD_ALWAYS, NULL))
1258 ret = FALSE;
1259 break;
1260 default:
1261 CertAddStoreToCollection(store, contextStore, 0, 0);
1264 else
1265 ret = FALSE;
1268 else
1269 ret = FALSE;
1270 *ppvContext = store;
1272 return ret;
1275 typedef BOOL (WINAPI *ContextDllCreateObjectContext)(LPCSTR pszObjectOid,
1276 DWORD dwRetrievalFlags, PCRYPT_BLOB_ARRAY pObject, void **ppvContext);
1278 static BOOL CRYPT_GetCreateFunction(LPCSTR pszObjectOid,
1279 ContextDllCreateObjectContext *pFunc, HCRYPTOIDFUNCADDR *phFunc)
1281 BOOL ret = TRUE;
1283 TRACE("(%s, %p, %p)\n", debugstr_a(pszObjectOid), pFunc, phFunc);
1285 *pFunc = NULL;
1286 *phFunc = 0;
1287 if (!HIWORD(pszObjectOid))
1289 switch (LOWORD(pszObjectOid))
1291 case 0:
1292 *pFunc = CRYPT_CreateBlob;
1293 break;
1294 case LOWORD(CONTEXT_OID_CERTIFICATE):
1295 *pFunc = CRYPT_CreateCert;
1296 break;
1297 case LOWORD(CONTEXT_OID_CRL):
1298 *pFunc = CRYPT_CreateCRL;
1299 break;
1300 case LOWORD(CONTEXT_OID_CTL):
1301 *pFunc = CRYPT_CreateCTL;
1302 break;
1303 case LOWORD(CONTEXT_OID_PKCS7):
1304 *pFunc = CRYPT_CreatePKCS7;
1305 break;
1306 case LOWORD(CONTEXT_OID_CAPI2_ANY):
1307 *pFunc = CRYPT_CreateAny;
1308 break;
1311 if (!*pFunc)
1313 static HCRYPTOIDFUNCSET set = NULL;
1315 if (!set)
1316 set = CryptInitOIDFunctionSet(
1317 CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC, 0);
1318 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, pszObjectOid,
1319 0, (void **)pFunc, phFunc);
1321 TRACE("returning %d\n", ret);
1322 return ret;
1325 typedef BOOL (*get_object_expiration_func)(void *pvContext,
1326 FILETIME *expiration);
1328 static BOOL CRYPT_GetExpirationFromCert(void *pvObject, FILETIME *expiration)
1330 PCCERT_CONTEXT cert = pvObject;
1332 *expiration = cert->pCertInfo->NotAfter;
1333 return TRUE;
1336 static BOOL CRYPT_GetExpirationFromCRL(void *pvObject, FILETIME *expiration)
1338 PCCRL_CONTEXT cert = pvObject;
1340 *expiration = cert->pCrlInfo->NextUpdate;
1341 return TRUE;
1344 static BOOL CRYPT_GetExpirationFromCTL(void *pvObject, FILETIME *expiration)
1346 PCCTL_CONTEXT cert = pvObject;
1348 *expiration = cert->pCtlInfo->NextUpdate;
1349 return TRUE;
1352 static BOOL CRYPT_GetExpirationFunction(LPCSTR pszObjectOid,
1353 get_object_expiration_func *getExpiration)
1355 BOOL ret;
1357 if (!HIWORD(pszObjectOid))
1359 switch (LOWORD(pszObjectOid))
1361 case LOWORD(CONTEXT_OID_CERTIFICATE):
1362 *getExpiration = CRYPT_GetExpirationFromCert;
1363 ret = TRUE;
1364 break;
1365 case LOWORD(CONTEXT_OID_CRL):
1366 *getExpiration = CRYPT_GetExpirationFromCRL;
1367 ret = TRUE;
1368 break;
1369 case LOWORD(CONTEXT_OID_CTL):
1370 *getExpiration = CRYPT_GetExpirationFromCTL;
1371 ret = TRUE;
1372 break;
1373 default:
1374 ret = FALSE;
1377 else
1378 ret = FALSE;
1379 return ret;
1382 /***********************************************************************
1383 * CryptRetrieveObjectByUrlW (CRYPTNET.@)
1385 BOOL WINAPI CryptRetrieveObjectByUrlW(LPCWSTR pszURL, LPCSTR pszObjectOid,
1386 DWORD dwRetrievalFlags, DWORD dwTimeout, LPVOID *ppvObject,
1387 HCRYPTASYNC hAsyncRetrieve, PCRYPT_CREDENTIALS pCredentials, LPVOID pvVerify,
1388 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo)
1390 BOOL ret;
1391 SchemeDllRetrieveEncodedObjectW retrieve;
1392 ContextDllCreateObjectContext create;
1393 HCRYPTOIDFUNCADDR hRetrieve = 0, hCreate = 0;
1395 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL),
1396 debugstr_a(pszObjectOid), dwRetrievalFlags, dwTimeout, ppvObject,
1397 hAsyncRetrieve, pCredentials, pvVerify, pAuxInfo);
1399 if (!pszURL)
1401 SetLastError(ERROR_INVALID_PARAMETER);
1402 return FALSE;
1404 ret = CRYPT_GetRetrieveFunction(pszURL, &retrieve, &hRetrieve);
1405 if (ret)
1406 ret = CRYPT_GetCreateFunction(pszObjectOid, &create, &hCreate);
1407 if (ret)
1409 CRYPT_BLOB_ARRAY object = { 0, NULL };
1410 PFN_FREE_ENCODED_OBJECT_FUNC freeObject;
1411 void *freeContext;
1413 ret = retrieve(pszURL, pszObjectOid, dwRetrievalFlags, dwTimeout,
1414 &object, &freeObject, &freeContext, hAsyncRetrieve, pCredentials,
1415 pAuxInfo);
1416 if (ret)
1418 get_object_expiration_func getExpiration;
1420 ret = create(pszObjectOid, dwRetrievalFlags, &object, ppvObject);
1421 if (ret && !(dwRetrievalFlags & CRYPT_DONT_CACHE_RESULT) &&
1422 CRYPT_GetExpirationFunction(pszObjectOid, &getExpiration))
1424 FILETIME expires;
1426 if (getExpiration(*ppvObject, &expires))
1427 CRYPT_CacheURL(pszURL, &object, dwRetrievalFlags, expires);
1429 freeObject(pszObjectOid, &object, freeContext);
1432 if (hCreate)
1433 CryptFreeOIDFunctionAddress(hCreate, 0);
1434 if (hRetrieve)
1435 CryptFreeOIDFunctionAddress(hRetrieve, 0);
1436 TRACE("returning %d\n", ret);
1437 return ret;
1440 typedef struct _OLD_CERT_REVOCATION_STATUS {
1441 DWORD cbSize;
1442 DWORD dwIndex;
1443 DWORD dwError;
1444 DWORD dwReason;
1445 } OLD_CERT_REVOCATION_STATUS, *POLD_CERT_REVOCATION_STATUS;
1447 /***********************************************************************
1448 * CertDllVerifyRevocation (CRYPTNET.@)
1450 BOOL WINAPI CertDllVerifyRevocation(DWORD dwEncodingType, DWORD dwRevType,
1451 DWORD cContext, PVOID rgpvContext[], DWORD dwFlags,
1452 PCERT_REVOCATION_PARA pRevPara, PCERT_REVOCATION_STATUS pRevStatus)
1454 DWORD error = 0, i;
1455 BOOL ret;
1457 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType, dwRevType,
1458 cContext, rgpvContext, dwFlags, pRevPara, pRevStatus);
1460 if (pRevStatus->cbSize != sizeof(OLD_CERT_REVOCATION_STATUS) &&
1461 pRevStatus->cbSize != sizeof(CERT_REVOCATION_STATUS))
1463 SetLastError(E_INVALIDARG);
1464 return FALSE;
1466 memset(&pRevStatus->dwIndex, 0, pRevStatus->cbSize - sizeof(DWORD));
1467 if (dwRevType != CERT_CONTEXT_REVOCATION_TYPE)
1469 error = CRYPT_E_NO_REVOCATION_CHECK;
1470 ret = FALSE;
1472 else
1474 ret = TRUE;
1475 for (i = 0; ret && i < cContext; i++)
1477 DWORD cbUrlArray;
1479 ret = CryptGetObjectUrl(URL_OID_CERTIFICATE_CRL_DIST_POINT,
1480 rgpvContext[i], 0, NULL, &cbUrlArray, NULL, NULL, NULL);
1481 if (!ret && GetLastError() == CRYPT_E_NOT_FOUND)
1483 error = CRYPT_E_NO_REVOCATION_CHECK;
1484 pRevStatus->dwIndex = i;
1486 else if (ret)
1488 CRYPT_URL_ARRAY *urlArray = CryptMemAlloc(cbUrlArray);
1490 if (urlArray)
1492 DWORD j, retrievalFlags = 0, startTime, endTime, timeout;
1494 ret = CryptGetObjectUrl(URL_OID_CERTIFICATE_CRL_DIST_POINT,
1495 rgpvContext[i], 0, urlArray, &cbUrlArray, NULL, NULL,
1496 NULL);
1497 if (dwFlags & CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION)
1498 retrievalFlags |= CRYPT_CACHE_ONLY_RETRIEVAL;
1499 if (dwFlags & CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG &&
1500 pRevPara->cbSize >= offsetof(CERT_REVOCATION_PARA,
1501 dwUrlRetrievalTimeout) + sizeof(DWORD))
1503 startTime = GetTickCount();
1504 endTime = startTime + pRevPara->dwUrlRetrievalTimeout;
1505 timeout = pRevPara->dwUrlRetrievalTimeout;
1507 else
1508 endTime = timeout = 0;
1509 for (j = 0; ret && j < urlArray->cUrl; j++)
1511 PCCRL_CONTEXT crl;
1513 ret = CryptRetrieveObjectByUrlW(urlArray->rgwszUrl[j],
1514 CONTEXT_OID_CRL, retrievalFlags, timeout,
1515 (void **)&crl, NULL, NULL, NULL, NULL);
1516 if (ret)
1518 PCRL_ENTRY entry = NULL;
1520 CertFindCertificateInCRL(
1521 rgpvContext[i], crl, 0, NULL,
1522 &entry);
1523 if (entry)
1525 error = CRYPT_E_REVOKED;
1526 pRevStatus->dwIndex = i;
1527 ret = FALSE;
1529 else if (timeout)
1531 DWORD time = GetTickCount();
1533 if ((int)(endTime - time) <= 0)
1535 error = ERROR_TIMEOUT;
1536 pRevStatus->dwIndex = i;
1537 ret = FALSE;
1539 else
1540 timeout = endTime - time;
1542 CertFreeCRLContext(crl);
1544 else
1545 error = CRYPT_E_REVOCATION_OFFLINE;
1547 CryptMemFree(urlArray);
1549 else
1551 error = ERROR_OUTOFMEMORY;
1552 pRevStatus->dwIndex = i;
1553 ret = FALSE;
1556 else
1557 pRevStatus->dwIndex = i;
1561 if (!ret)
1563 SetLastError(error);
1564 pRevStatus->dwError = error;
1566 TRACE("returning %d (%08x)\n", ret, error);
1567 return ret;