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
22 #include "wine/port.h"
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(cryptnet
);
43 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
45 static const WCHAR cryptNet
[] = { 'c','r','y','p','t','n','e','t','.',
48 /***********************************************************************
49 * DllRegisterServer (CRYPTNET.@)
51 HRESULT WINAPI
DllRegisterServer(void)
54 CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING
,
55 CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0, cryptNet
);
56 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap",
57 cryptNet
, "LdapProvOpenStore");
58 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
59 CERT_STORE_PROV_LDAP_W
, cryptNet
, "LdapProvOpenStore");
63 /***********************************************************************
64 * DllUnregisterServer (CRYPTNET.@)
66 HRESULT WINAPI
DllUnregisterServer(void)
69 CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING
,
70 CRYPT_OID_VERIFY_REVOCATION_FUNC
, cryptNet
);
71 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap");
72 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
73 CERT_STORE_PROV_LDAP_W
);
77 static const char *url_oid_to_str(LPCSTR oid
)
85 #define _x(oid) case LOWORD(oid): return #oid
86 _x(URL_OID_CERTIFICATE_ISSUER
);
87 _x(URL_OID_CERTIFICATE_CRL_DIST_POINT
);
88 _x(URL_OID_CTL_ISSUER
);
89 _x(URL_OID_CTL_NEXT_UPDATE
);
90 _x(URL_OID_CRL_ISSUER
);
91 _x(URL_OID_CERTIFICATE_FRESHEST_CRL
);
92 _x(URL_OID_CRL_FRESHEST_CRL
);
93 _x(URL_OID_CROSS_CERT_DIST_POINT
);
96 snprintf(buf
, sizeof(buf
), "%d", LOWORD(oid
));
104 typedef BOOL (WINAPI
*UrlDllGetObjectUrlFunc
)(LPCSTR
, LPVOID
, DWORD
,
105 PCRYPT_URL_ARRAY
, DWORD
*, PCRYPT_URL_INFO
, DWORD
*, LPVOID
);
107 static BOOL WINAPI
CRYPT_GetUrlFromCertificateIssuer(LPCSTR pszUrlOid
,
108 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
109 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
111 PCCERT_CONTEXT cert
= pvPara
;
115 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
116 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
118 SetLastError(CRYPT_E_NOT_FOUND
);
121 if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
122 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
124 CERT_AUTHORITY_INFO_ACCESS
*aia
;
127 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
128 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
132 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
134 for (i
= 0, cUrl
= 0; i
< aia
->cAccDescr
; i
++)
135 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
136 szOID_PKIX_CA_ISSUERS
))
138 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
141 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
144 bytesNeeded
+= sizeof(LPWSTR
) +
145 (lstrlenW(aia
->rgAccDescr
[i
].AccessLocation
.u
.
146 pwszURL
) + 1) * sizeof(WCHAR
);
150 FIXME("unsupported alt name type %d\n",
151 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
155 SetLastError(E_INVALIDARG
);
159 *pcbUrlArray
= bytesNeeded
;
160 else if (*pcbUrlArray
< bytesNeeded
)
162 SetLastError(ERROR_MORE_DATA
);
163 *pcbUrlArray
= bytesNeeded
;
170 *pcbUrlArray
= bytesNeeded
;
172 pUrlArray
->rgwszUrl
=
173 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
174 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
175 + cUrl
* sizeof(LPWSTR
));
176 for (i
= 0; i
< aia
->cAccDescr
; i
++)
177 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
178 szOID_PKIX_CA_ISSUERS
))
180 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
181 == CERT_ALT_NAME_URL
)
183 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
186 aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
);
187 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
189 nextUrl
+= (lstrlenW(nextUrl
) + 1);
198 FIXME("url info: stub\n");
200 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
201 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
203 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
204 SetLastError(ERROR_MORE_DATA
);
209 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
210 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
218 SetLastError(CRYPT_E_NOT_FOUND
);
222 static BOOL
CRYPT_GetUrlFromCRLDistPointsExt(const CRYPT_DATA_BLOB
*value
,
223 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
227 CRL_DIST_POINTS_INFO
*info
;
230 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CRL_DIST_POINTS
,
231 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
234 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
236 for (i
= 0, cUrl
= 0; i
< info
->cDistPoint
; i
++)
237 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
238 == CRL_DIST_POINT_FULL_NAME
)
241 CERT_ALT_NAME_INFO
*name
=
242 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
244 for (j
= 0; j
< name
->cAltEntry
; j
++)
245 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
248 if (name
->rgAltEntry
[j
].u
.pwszURL
)
251 bytesNeeded
+= sizeof(LPWSTR
) +
252 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1)
259 SetLastError(E_INVALIDARG
);
263 *pcbUrlArray
= bytesNeeded
;
264 else if (*pcbUrlArray
< bytesNeeded
)
266 SetLastError(ERROR_MORE_DATA
);
267 *pcbUrlArray
= bytesNeeded
;
274 *pcbUrlArray
= bytesNeeded
;
276 pUrlArray
->rgwszUrl
=
277 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
278 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
279 + cUrl
* sizeof(LPWSTR
));
280 for (i
= 0; i
< info
->cDistPoint
; i
++)
281 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
282 == CRL_DIST_POINT_FULL_NAME
)
285 CERT_ALT_NAME_INFO
*name
=
286 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
288 for (j
= 0; j
< name
->cAltEntry
; j
++)
289 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
292 if (name
->rgAltEntry
[j
].u
.pwszURL
)
295 name
->rgAltEntry
[j
].u
.pwszURL
);
296 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
299 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1);
308 FIXME("url info: stub\n");
310 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
311 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
313 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
314 SetLastError(ERROR_MORE_DATA
);
319 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
320 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
329 static BOOL WINAPI
CRYPT_GetUrlFromCertificateCRLDistPoint(LPCSTR pszUrlOid
,
330 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
331 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
333 PCCERT_CONTEXT cert
= pvPara
;
337 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
338 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
340 SetLastError(CRYPT_E_NOT_FOUND
);
343 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
344 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
345 ret
= CRYPT_GetUrlFromCRLDistPointsExt(&ext
->Value
, pUrlArray
,
346 pcbUrlArray
, pUrlInfo
, pcbUrlInfo
);
348 SetLastError(CRYPT_E_NOT_FOUND
);
352 /***********************************************************************
353 * CryptGetObjectUrl (CRYPTNET.@)
355 BOOL WINAPI
CryptGetObjectUrl(LPCSTR pszUrlOid
, LPVOID pvPara
, DWORD dwFlags
,
356 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
357 DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
359 UrlDllGetObjectUrlFunc func
= NULL
;
360 HCRYPTOIDFUNCADDR hFunc
= NULL
;
363 TRACE("(%s, %p, %08x, %p, %p, %p, %p, %p)\n", debugstr_a(pszUrlOid
),
364 pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
, pUrlInfo
, pcbUrlInfo
, pvReserved
);
366 if (IS_INTOID(pszUrlOid
))
368 switch (LOWORD(pszUrlOid
))
370 case LOWORD(URL_OID_CERTIFICATE_ISSUER
):
371 func
= CRYPT_GetUrlFromCertificateIssuer
;
373 case LOWORD(URL_OID_CERTIFICATE_CRL_DIST_POINT
):
374 func
= CRYPT_GetUrlFromCertificateCRLDistPoint
;
377 FIXME("unimplemented for %s\n", url_oid_to_str(pszUrlOid
));
378 SetLastError(ERROR_FILE_NOT_FOUND
);
383 static HCRYPTOIDFUNCSET set
= NULL
;
386 set
= CryptInitOIDFunctionSet(URL_OID_GET_OBJECT_URL_FUNC
, 0);
387 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszUrlOid
, 0,
388 (void **)&func
, &hFunc
);
391 ret
= func(pszUrlOid
, pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
,
392 pUrlInfo
, pcbUrlInfo
, pvReserved
);
394 CryptFreeOIDFunctionAddress(hFunc
, 0);
398 /***********************************************************************
399 * CryptRetrieveObjectByUrlA (CRYPTNET.@)
401 BOOL WINAPI
CryptRetrieveObjectByUrlA(LPCSTR pszURL
, LPCSTR pszObjectOid
,
402 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
403 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
404 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
409 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_a(pszURL
),
410 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
411 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
415 SetLastError(ERROR_INVALID_PARAMETER
);
418 len
= MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, NULL
, 0);
421 LPWSTR url
= CryptMemAlloc(len
* sizeof(WCHAR
));
425 MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, url
, len
);
426 ret
= CryptRetrieveObjectByUrlW(url
, pszObjectOid
,
427 dwRetrievalFlags
, dwTimeout
, ppvObject
, hAsyncRetrieve
,
428 pCredentials
, pvVerify
, pAuxInfo
);
432 SetLastError(ERROR_OUTOFMEMORY
);
437 static void WINAPI
CRYPT_FreeBlob(LPCSTR pszObjectOid
,
438 PCRYPT_BLOB_ARRAY pObject
, void *pvFreeContext
)
442 for (i
= 0; i
< pObject
->cBlob
; i
++)
443 CryptMemFree(pObject
->rgBlob
[i
].pbData
);
444 CryptMemFree(pObject
->rgBlob
);
447 static BOOL
CRYPT_GetObjectFromFile(HANDLE hFile
, PCRYPT_BLOB_ARRAY pObject
)
452 if ((ret
= GetFileSizeEx(hFile
, &size
)))
456 WARN("file too big\n");
457 SetLastError(ERROR_INVALID_DATA
);
462 CRYPT_DATA_BLOB blob
;
464 blob
.pbData
= CryptMemAlloc(size
.u
.LowPart
);
467 ret
= ReadFile(hFile
, blob
.pbData
, size
.u
.LowPart
, &blob
.cbData
,
471 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
475 memcpy(pObject
->rgBlob
, &blob
, sizeof(CRYPT_DATA_BLOB
));
479 SetLastError(ERROR_OUTOFMEMORY
);
484 CryptMemFree(blob
.pbData
);
488 SetLastError(ERROR_OUTOFMEMORY
);
496 static BOOL
CRYPT_GetObjectFromCache(LPCWSTR pszURL
, PCRYPT_BLOB_ARRAY pObject
,
497 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
500 INTERNET_CACHE_ENTRY_INFOW
*pCacheInfo
= NULL
;
503 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pObject
, pAuxInfo
);
505 RetrieveUrlCacheEntryFileW(pszURL
, NULL
, &size
, 0);
506 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
509 pCacheInfo
= CryptMemAlloc(size
);
512 SetLastError(ERROR_OUTOFMEMORY
);
516 if ((ret
= RetrieveUrlCacheEntryFileW(pszURL
, pCacheInfo
, &size
, 0)))
520 GetSystemTimeAsFileTime(&ft
);
521 if (CompareFileTime(&pCacheInfo
->ExpireTime
, &ft
) >= 0)
523 HANDLE hFile
= CreateFileW(pCacheInfo
->lpszLocalFileName
, GENERIC_READ
,
524 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
526 if (hFile
!= INVALID_HANDLE_VALUE
)
528 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
530 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
531 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
532 pLastSyncTime
) + sizeof(PFILETIME
) &&
533 pAuxInfo
->pLastSyncTime
)
534 memcpy(pAuxInfo
->pLastSyncTime
,
535 &pCacheInfo
->LastSyncTime
,
542 DeleteUrlCacheEntryW(pszURL
);
548 DeleteUrlCacheEntryW(pszURL
);
551 UnlockUrlCacheEntryFileW(pszURL
, 0);
553 CryptMemFree(pCacheInfo
);
554 TRACE("returning %d\n", ret
);
558 /* Parses the URL, and sets components' lpszHostName and lpszUrlPath members
559 * to NULL-terminated copies of those portions of the URL (to be freed with
562 static BOOL
CRYPT_CrackUrl(LPCWSTR pszURL
, URL_COMPONENTSW
*components
)
566 TRACE("(%s, %p)\n", debugstr_w(pszURL
), components
);
568 memset(components
, 0, sizeof(*components
));
569 components
->dwStructSize
= sizeof(*components
);
570 components
->lpszHostName
= CryptMemAlloc(INTERNET_MAX_HOST_NAME_LENGTH
* sizeof(WCHAR
));
571 components
->dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
572 if (!components
->lpszHostName
)
574 SetLastError(ERROR_OUTOFMEMORY
);
577 components
->lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
578 components
->dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
579 if (!components
->lpszUrlPath
)
581 CryptMemFree(components
->lpszHostName
);
582 SetLastError(ERROR_OUTOFMEMORY
);
586 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, components
);
589 switch (components
->nScheme
)
591 case INTERNET_SCHEME_FTP
:
592 if (!components
->nPort
)
593 components
->nPort
= INTERNET_DEFAULT_FTP_PORT
;
595 case INTERNET_SCHEME_HTTP
:
596 if (!components
->nPort
)
597 components
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
603 TRACE("returning %d\n", ret
);
614 static struct InetContext
*CRYPT_MakeInetContext(DWORD dwTimeout
)
616 struct InetContext
*context
= CryptMemAlloc(sizeof(struct InetContext
));
620 context
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
623 CryptMemFree(context
);
628 context
->timeout
= dwTimeout
;
629 context
->error
= ERROR_SUCCESS
;
635 static BOOL
CRYPT_DownloadObject(DWORD dwRetrievalFlags
, HINTERNET hHttp
,
636 struct InetContext
*context
, PCRYPT_BLOB_ARRAY pObject
,
637 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
639 CRYPT_DATA_BLOB object
= { 0, NULL
};
640 DWORD bytesAvailable
;
644 if ((ret
= InternetQueryDataAvailable(hHttp
, &bytesAvailable
, 0, 0)))
649 object
.pbData
= CryptMemRealloc(object
.pbData
,
650 object
.cbData
+ bytesAvailable
);
652 object
.pbData
= CryptMemAlloc(bytesAvailable
);
655 INTERNET_BUFFERSA buffer
= { sizeof(buffer
), 0 };
657 buffer
.dwBufferLength
= bytesAvailable
;
658 buffer
.lpvBuffer
= object
.pbData
+ object
.cbData
;
659 if (!(ret
= InternetReadFileExA(hHttp
, &buffer
, IRF_NO_WAIT
,
660 (DWORD_PTR
)context
)))
662 if (GetLastError() == ERROR_IO_PENDING
)
664 if (WaitForSingleObject(context
->event
,
665 context
->timeout
) == WAIT_TIMEOUT
)
666 SetLastError(ERROR_TIMEOUT
);
667 else if (context
->error
)
668 SetLastError(context
->error
);
674 object
.cbData
+= buffer
.dwBufferLength
;
678 SetLastError(ERROR_OUTOFMEMORY
);
683 else if (GetLastError() == ERROR_IO_PENDING
)
685 if (WaitForSingleObject(context
->event
, context
->timeout
) ==
687 SetLastError(ERROR_TIMEOUT
);
691 } while (ret
&& bytesAvailable
);
694 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
695 if (!pObject
->rgBlob
)
697 CryptMemFree(object
.pbData
);
698 SetLastError(ERROR_OUTOFMEMORY
);
703 pObject
->rgBlob
[0].cbData
= object
.cbData
;
704 pObject
->rgBlob
[0].pbData
= object
.pbData
;
708 TRACE("returning %d\n", ret
);
712 /* Finds the object specified by pszURL in the cache. If it's not found,
713 * creates a new cache entry for the object and writes the object to it.
714 * Sets the expiration time of the cache entry to expires.
716 static void CRYPT_CacheURL(LPCWSTR pszURL
, const CRYPT_BLOB_ARRAY
*pObject
,
717 DWORD dwRetrievalFlags
, FILETIME expires
)
719 WCHAR cacheFileName
[MAX_PATH
];
721 DWORD size
= 0, entryType
;
724 GetUrlCacheEntryInfoW(pszURL
, NULL
, &size
);
725 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
727 INTERNET_CACHE_ENTRY_INFOW
*info
= CryptMemAlloc(size
);
731 ERR("out of memory\n");
735 if (GetUrlCacheEntryInfoW(pszURL
, info
, &size
))
737 lstrcpyW(cacheFileName
, info
->lpszLocalFileName
);
738 /* Check if the existing cache entry is up to date. If it isn't,
739 * remove the existing cache entry, and create a new one with the
742 GetSystemTimeAsFileTime(&ft
);
743 if (CompareFileTime(&info
->ExpireTime
, &ft
) < 0)
745 DeleteUrlCacheEntryW(pszURL
);
749 info
->ExpireTime
= expires
;
750 SetUrlCacheEntryInfoW(pszURL
, info
, CACHE_ENTRY_EXPTIME_FC
);
758 if (!CreateUrlCacheEntryW(pszURL
, pObject
->rgBlob
[0].cbData
, NULL
, cacheFileName
, 0))
761 hCacheFile
= CreateFileW(cacheFileName
, GENERIC_WRITE
, 0,
762 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
763 if(hCacheFile
== INVALID_HANDLE_VALUE
)
766 WriteFile(hCacheFile
, pObject
->rgBlob
[0].pbData
,
767 pObject
->rgBlob
[0].cbData
, &size
, NULL
);
768 CloseHandle(hCacheFile
);
770 if (!(dwRetrievalFlags
& CRYPT_STICKY_CACHE_RETRIEVAL
))
771 entryType
= NORMAL_CACHE_ENTRY
;
773 entryType
= STICKY_CACHE_ENTRY
;
774 memset(&ft
, 0, sizeof(ft
));
775 CommitUrlCacheEntryW(pszURL
, cacheFileName
, expires
, ft
, entryType
,
776 NULL
, 0, NULL
, NULL
);
779 static void CALLBACK
CRYPT_InetStatusCallback(HINTERNET hInt
,
780 DWORD_PTR dwContext
, DWORD status
, void *statusInfo
, DWORD statusInfoLen
)
782 struct InetContext
*context
= (struct InetContext
*)dwContext
;
783 LPINTERNET_ASYNC_RESULT result
;
787 case INTERNET_STATUS_REQUEST_COMPLETE
:
789 context
->error
= result
->dwError
;
790 SetEvent(context
->event
);
794 static BOOL
CRYPT_Connect(const URL_COMPONENTSW
*components
,
795 struct InetContext
*context
, PCRYPT_CREDENTIALS pCredentials
,
796 HINTERNET
*phInt
, HINTERNET
*phHost
)
800 TRACE("(%s:%d, %p, %p, %p, %p)\n", debugstr_w(components
->lpszHostName
),
801 components
->nPort
, context
, pCredentials
, phInt
, phInt
);
804 *phInt
= InternetOpenW(NULL
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
,
805 context
? INTERNET_FLAG_ASYNC
: 0);
811 InternetSetStatusCallbackW(*phInt
, CRYPT_InetStatusCallback
);
812 switch (components
->nScheme
)
814 case INTERNET_SCHEME_FTP
:
815 service
= INTERNET_SERVICE_FTP
;
817 case INTERNET_SCHEME_HTTP
:
818 service
= INTERNET_SERVICE_HTTP
;
823 /* FIXME: use pCredentials for username/password */
824 *phHost
= InternetConnectW(*phInt
, components
->lpszHostName
,
825 components
->nPort
, NULL
, NULL
, service
, 0, (DWORD_PTR
)context
);
828 InternetCloseHandle(*phInt
);
837 TRACE("returning %d\n", ret
);
841 static BOOL WINAPI
FTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
842 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
843 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
844 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
845 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
847 FIXME("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
848 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
849 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
852 pObject
->rgBlob
= NULL
;
853 *ppfnFreeObject
= CRYPT_FreeBlob
;
854 *ppvFreeContext
= NULL
;
858 static const WCHAR x509cacert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
859 '/','x','-','x','5','0','9','-','c','a','-','c','e','r','t',0 };
860 static const WCHAR x509emailcert
[] = { 'a','p','p','l','i','c','a','t','i','o',
861 'n','/','x','-','x','5','0','9','-','e','m','a','i','l','-','c','e','r','t',
863 static const WCHAR x509servercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
864 'n','/','x','-','x','5','0','9','-','s','e','r','v','e','r','-','c','e','r',
866 static const WCHAR x509usercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
867 'n','/','x','-','x','5','0','9','-','u','s','e','r','-','c','e','r','t',0 };
868 static const WCHAR pkcs7cert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
869 '/','x','-','p','k','c','s','7','-','c','e','r','t','i','f','c','a','t','e',
871 static const WCHAR pkixCRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
872 '/','p','k','i','x','-','c','r','l',0 };
873 static const WCHAR pkcs7CRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
874 '/','x','-','p','k','c','s','7','-','c','r','l',0 };
875 static const WCHAR pkcs7sig
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
876 '/','x','-','p','k','c','s','7','-','s','i','g','n','a','t','u','r','e',0 };
877 static const WCHAR pkcs7mime
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
878 '/','x','-','p','k','c','s','7','-','m','i','m','e',0 };
880 static BOOL WINAPI
HTTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
881 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
882 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
883 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
884 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
888 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
889 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
890 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
893 pObject
->rgBlob
= NULL
;
894 *ppfnFreeObject
= CRYPT_FreeBlob
;
895 *ppvFreeContext
= NULL
;
897 if (!(dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
))
898 ret
= CRYPT_GetObjectFromCache(pszURL
, pObject
, pAuxInfo
);
899 if (!ret
&& (!(dwRetrievalFlags
& CRYPT_CACHE_ONLY_RETRIEVAL
) ||
900 (dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
)))
902 URL_COMPONENTSW components
;
904 if ((ret
= CRYPT_CrackUrl(pszURL
, &components
)))
906 HINTERNET hInt
, hHost
;
907 struct InetContext
*context
= NULL
;
910 context
= CRYPT_MakeInetContext(dwTimeout
);
911 ret
= CRYPT_Connect(&components
, context
, pCredentials
, &hInt
,
915 static LPCWSTR types
[] = { x509cacert
, x509emailcert
,
916 x509servercert
, x509usercert
, pkcs7cert
, pkixCRL
, pkcs7CRL
,
917 pkcs7sig
, pkcs7mime
, NULL
};
918 HINTERNET hHttp
= HttpOpenRequestW(hHost
, NULL
,
919 components
.lpszUrlPath
, NULL
, NULL
, types
,
920 INTERNET_FLAG_NO_COOKIES
| INTERNET_FLAG_NO_UI
,
927 InternetSetOptionW(hHttp
,
928 INTERNET_OPTION_RECEIVE_TIMEOUT
, &dwTimeout
,
930 InternetSetOptionW(hHttp
, INTERNET_OPTION_SEND_TIMEOUT
,
931 &dwTimeout
, sizeof(dwTimeout
));
933 ret
= HttpSendRequestExW(hHttp
, NULL
, NULL
, 0,
935 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
937 if (WaitForSingleObject(context
->event
,
938 context
->timeout
) == WAIT_TIMEOUT
)
939 SetLastError(ERROR_TIMEOUT
);
944 !(ret
= HttpEndRequestW(hHttp
, NULL
, 0, (DWORD_PTR
)context
)) &&
945 GetLastError() == ERROR_IO_PENDING
)
947 if (WaitForSingleObject(context
->event
,
948 context
->timeout
) == WAIT_TIMEOUT
)
949 SetLastError(ERROR_TIMEOUT
);
954 ret
= CRYPT_DownloadObject(dwRetrievalFlags
, hHttp
,
955 context
, pObject
, pAuxInfo
);
956 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
))
960 DWORD len
= sizeof(st
);
962 if (HttpQueryInfoW(hHttp
, HTTP_QUERY_EXPIRES
| HTTP_QUERY_FLAG_SYSTEMTIME
,
963 &st
, &len
, NULL
) && SystemTimeToFileTime(&st
, &ft
))
964 CRYPT_CacheURL(pszURL
, pObject
, dwRetrievalFlags
, ft
);
966 InternetCloseHandle(hHttp
);
968 InternetCloseHandle(hHost
);
969 InternetCloseHandle(hInt
);
973 CloseHandle(context
->event
);
974 CryptMemFree(context
);
976 CryptMemFree(components
.lpszUrlPath
);
977 CryptMemFree(components
.lpszHostName
);
980 TRACE("returning %d\n", ret
);
984 static BOOL WINAPI
File_RetrieveEncodedObjectW(LPCWSTR pszURL
,
985 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
986 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
987 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
988 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
990 URL_COMPONENTSW components
= { sizeof(components
), 0 };
993 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
994 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
995 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
998 pObject
->rgBlob
= NULL
;
999 *ppfnFreeObject
= CRYPT_FreeBlob
;
1000 *ppvFreeContext
= NULL
;
1002 components
.lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
1003 components
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
1004 if (!components
.lpszUrlPath
)
1006 SetLastError(ERROR_OUTOFMEMORY
);
1010 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, &components
);
1015 /* 3 == lstrlenW(L"c:") + 1 */
1016 path
= CryptMemAlloc((components
.dwUrlPathLength
+ 3) * sizeof(WCHAR
));
1021 /* Try to create the file directly - Wine handles / in pathnames */
1022 lstrcpynW(path
, components
.lpszUrlPath
,
1023 components
.dwUrlPathLength
+ 1);
1024 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1025 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1026 if (hFile
== INVALID_HANDLE_VALUE
)
1028 /* Try again on the current drive */
1029 GetCurrentDirectoryW(components
.dwUrlPathLength
, path
);
1032 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1033 components
.dwUrlPathLength
+ 1);
1034 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1035 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1037 if (hFile
== INVALID_HANDLE_VALUE
)
1039 /* Try again on the Windows drive */
1040 GetWindowsDirectoryW(path
, components
.dwUrlPathLength
);
1043 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1044 components
.dwUrlPathLength
+ 1);
1045 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1046 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1050 if (hFile
!= INVALID_HANDLE_VALUE
)
1052 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
1054 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
1055 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
1056 pLastSyncTime
) + sizeof(PFILETIME
) &&
1057 pAuxInfo
->pLastSyncTime
)
1058 GetFileTime(hFile
, NULL
, NULL
,
1059 pAuxInfo
->pLastSyncTime
);
1069 SetLastError(ERROR_OUTOFMEMORY
);
1073 CryptMemFree(components
.lpszUrlPath
);
1077 typedef BOOL (WINAPI
*SchemeDllRetrieveEncodedObjectW
)(LPCWSTR pwszUrl
,
1078 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
1079 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
1080 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
1081 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
);
1083 static BOOL
CRYPT_GetRetrieveFunction(LPCWSTR pszURL
,
1084 SchemeDllRetrieveEncodedObjectW
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1086 URL_COMPONENTSW components
= { sizeof(components
), 0 };
1089 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pFunc
, phFunc
);
1093 components
.dwSchemeLength
= 1;
1094 ret
= InternetCrackUrlW(pszURL
, 0, 0, &components
);
1097 /* Microsoft always uses CryptInitOIDFunctionSet/
1098 * CryptGetOIDFunctionAddress, but there doesn't seem to be a pressing
1099 * reason to do so for builtin schemes.
1101 switch (components
.nScheme
)
1103 case INTERNET_SCHEME_FTP
:
1104 *pFunc
= FTP_RetrieveEncodedObjectW
;
1106 case INTERNET_SCHEME_HTTP
:
1107 *pFunc
= HTTP_RetrieveEncodedObjectW
;
1109 case INTERNET_SCHEME_FILE
:
1110 *pFunc
= File_RetrieveEncodedObjectW
;
1114 int len
= WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1115 components
.dwSchemeLength
, NULL
, 0, NULL
, NULL
);
1119 LPSTR scheme
= CryptMemAlloc(len
);
1123 static HCRYPTOIDFUNCSET set
= NULL
;
1126 set
= CryptInitOIDFunctionSet(
1127 SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC
, 0);
1128 WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1129 components
.dwSchemeLength
, scheme
, len
, NULL
, NULL
);
1130 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
,
1131 scheme
, 0, (void **)pFunc
, phFunc
);
1132 CryptMemFree(scheme
);
1136 SetLastError(ERROR_OUTOFMEMORY
);
1145 TRACE("returning %d\n", ret
);
1149 static BOOL WINAPI
CRYPT_CreateBlob(LPCSTR pszObjectOid
,
1150 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1153 CRYPT_BLOB_ARRAY
*context
;
1156 size
= sizeof(CRYPT_BLOB_ARRAY
) + pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1157 for (i
= 0; i
< pObject
->cBlob
; i
++)
1158 size
+= pObject
->rgBlob
[i
].cbData
;
1159 context
= CryptMemAlloc(size
);
1166 (CRYPT_DATA_BLOB
*)((LPBYTE
)context
+ sizeof(CRYPT_BLOB_ARRAY
));
1168 (LPBYTE
)context
->rgBlob
+ pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1169 for (i
= 0; i
< pObject
->cBlob
; i
++)
1171 memcpy(nextData
, pObject
->rgBlob
[i
].pbData
,
1172 pObject
->rgBlob
[i
].cbData
);
1173 context
->rgBlob
[i
].pbData
= nextData
;
1174 context
->rgBlob
[i
].cbData
= pObject
->rgBlob
[i
].cbData
;
1175 nextData
+= pObject
->rgBlob
[i
].cbData
;
1178 *ppvContext
= context
;
1184 typedef BOOL (WINAPI
*AddContextToStore
)(HCERTSTORE hCertStore
,
1185 const void *pContext
, DWORD dwAddDisposition
, const void **ppStoreContext
);
1187 static BOOL
CRYPT_CreateContext(const CRYPT_BLOB_ARRAY
*pObject
,
1188 DWORD dwExpectedContentTypeFlags
, AddContextToStore addFunc
, void **ppvContext
)
1192 if (!pObject
->cBlob
)
1194 SetLastError(ERROR_INVALID_DATA
);
1198 else if (pObject
->cBlob
== 1)
1200 if (!CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1201 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
,
1202 NULL
, NULL
, NULL
, NULL
, (const void **)ppvContext
))
1204 SetLastError(CRYPT_E_NO_MATCH
);
1210 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1211 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1216 const void *context
;
1218 for (i
= 0; i
< pObject
->cBlob
; i
++)
1220 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1221 &pObject
->rgBlob
[i
], dwExpectedContentTypeFlags
,
1222 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
, NULL
,
1225 if (!addFunc(store
, context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1230 SetLastError(CRYPT_E_NO_MATCH
);
1237 *ppvContext
= store
;
1242 static BOOL WINAPI
CRYPT_CreateCert(LPCSTR pszObjectOid
,
1243 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1245 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CERT
,
1246 (AddContextToStore
)CertAddCertificateContextToStore
, ppvContext
);
1249 static BOOL WINAPI
CRYPT_CreateCRL(LPCSTR pszObjectOid
,
1250 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1252 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CRL
,
1253 (AddContextToStore
)CertAddCRLContextToStore
, ppvContext
);
1256 static BOOL WINAPI
CRYPT_CreateCTL(LPCSTR pszObjectOid
,
1257 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1259 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CTL
,
1260 (AddContextToStore
)CertAddCTLContextToStore
, ppvContext
);
1263 static BOOL WINAPI
CRYPT_CreatePKCS7(LPCSTR pszObjectOid
,
1264 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1268 if (!pObject
->cBlob
)
1270 SetLastError(ERROR_INVALID_DATA
);
1274 else if (pObject
->cBlob
== 1)
1275 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1276 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1277 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
, CERT_QUERY_FORMAT_FLAG_BINARY
,
1278 0, NULL
, NULL
, NULL
, ppvContext
, NULL
, NULL
);
1281 FIXME("multiple messages unimplemented\n");
1287 static BOOL WINAPI
CRYPT_CreateAny(LPCSTR pszObjectOid
,
1288 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1292 if (!pObject
->cBlob
)
1294 SetLastError(ERROR_INVALID_DATA
);
1300 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
1301 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1305 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1306 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1310 CertAddStoreToCollection(store
, memStore
,
1311 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1312 CertCloseStore(memStore
, 0);
1316 CertCloseStore(store
, 0);
1325 for (i
= 0; i
< pObject
->cBlob
; i
++)
1327 DWORD contentType
, expectedContentTypes
=
1328 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1329 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
|
1330 CERT_QUERY_CONTENT_FLAG_CERT
|
1331 CERT_QUERY_CONTENT_FLAG_CRL
|
1332 CERT_QUERY_CONTENT_FLAG_CTL
;
1333 HCERTSTORE contextStore
;
1334 const void *context
;
1336 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1337 &pObject
->rgBlob
[i
], expectedContentTypes
,
1338 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, &contentType
, NULL
,
1339 &contextStore
, NULL
, &context
))
1341 switch (contentType
)
1343 case CERT_QUERY_CONTENT_CERT
:
1344 if (!CertAddCertificateContextToStore(store
,
1345 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1347 CertFreeCertificateContext(context
);
1349 case CERT_QUERY_CONTENT_CRL
:
1350 if (!CertAddCRLContextToStore(store
,
1351 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1353 CertFreeCRLContext(context
);
1355 case CERT_QUERY_CONTENT_CTL
:
1356 if (!CertAddCTLContextToStore(store
,
1357 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1359 CertFreeCTLContext(context
);
1362 CertAddStoreToCollection(store
, contextStore
, 0, 0);
1364 CertCloseStore(contextStore
, 0);
1372 *ppvContext
= store
;
1377 typedef BOOL (WINAPI
*ContextDllCreateObjectContext
)(LPCSTR pszObjectOid
,
1378 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
);
1380 static BOOL
CRYPT_GetCreateFunction(LPCSTR pszObjectOid
,
1381 ContextDllCreateObjectContext
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1385 TRACE("(%s, %p, %p)\n", debugstr_a(pszObjectOid
), pFunc
, phFunc
);
1389 if (IS_INTOID(pszObjectOid
))
1391 switch (LOWORD(pszObjectOid
))
1394 *pFunc
= CRYPT_CreateBlob
;
1396 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1397 *pFunc
= CRYPT_CreateCert
;
1399 case LOWORD(CONTEXT_OID_CRL
):
1400 *pFunc
= CRYPT_CreateCRL
;
1402 case LOWORD(CONTEXT_OID_CTL
):
1403 *pFunc
= CRYPT_CreateCTL
;
1405 case LOWORD(CONTEXT_OID_PKCS7
):
1406 *pFunc
= CRYPT_CreatePKCS7
;
1408 case LOWORD(CONTEXT_OID_CAPI2_ANY
):
1409 *pFunc
= CRYPT_CreateAny
;
1415 static HCRYPTOIDFUNCSET set
= NULL
;
1418 set
= CryptInitOIDFunctionSet(
1419 CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC
, 0);
1420 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszObjectOid
,
1421 0, (void **)pFunc
, phFunc
);
1423 TRACE("returning %d\n", ret
);
1427 typedef BOOL (*get_object_expiration_func
)(const void *pvContext
,
1428 FILETIME
*expiration
);
1430 static BOOL
CRYPT_GetExpirationFromCert(const void *pvObject
, FILETIME
*expiration
)
1432 PCCERT_CONTEXT cert
= pvObject
;
1434 *expiration
= cert
->pCertInfo
->NotAfter
;
1438 static BOOL
CRYPT_GetExpirationFromCRL(const void *pvObject
, FILETIME
*expiration
)
1440 PCCRL_CONTEXT cert
= pvObject
;
1442 *expiration
= cert
->pCrlInfo
->NextUpdate
;
1446 static BOOL
CRYPT_GetExpirationFromCTL(const void *pvObject
, FILETIME
*expiration
)
1448 PCCTL_CONTEXT cert
= pvObject
;
1450 *expiration
= cert
->pCtlInfo
->NextUpdate
;
1454 static BOOL
CRYPT_GetExpirationFunction(LPCSTR pszObjectOid
,
1455 get_object_expiration_func
*getExpiration
)
1459 if (IS_INTOID(pszObjectOid
))
1461 switch (LOWORD(pszObjectOid
))
1463 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1464 *getExpiration
= CRYPT_GetExpirationFromCert
;
1467 case LOWORD(CONTEXT_OID_CRL
):
1468 *getExpiration
= CRYPT_GetExpirationFromCRL
;
1471 case LOWORD(CONTEXT_OID_CTL
):
1472 *getExpiration
= CRYPT_GetExpirationFromCTL
;
1484 /***********************************************************************
1485 * CryptRetrieveObjectByUrlW (CRYPTNET.@)
1487 BOOL WINAPI
CryptRetrieveObjectByUrlW(LPCWSTR pszURL
, LPCSTR pszObjectOid
,
1488 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
1489 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
1490 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
1493 SchemeDllRetrieveEncodedObjectW retrieve
;
1494 ContextDllCreateObjectContext create
;
1495 HCRYPTOIDFUNCADDR hRetrieve
= 0, hCreate
= 0;
1497 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
1498 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
1499 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
1503 SetLastError(ERROR_INVALID_PARAMETER
);
1506 ret
= CRYPT_GetRetrieveFunction(pszURL
, &retrieve
, &hRetrieve
);
1508 ret
= CRYPT_GetCreateFunction(pszObjectOid
, &create
, &hCreate
);
1511 CRYPT_BLOB_ARRAY object
= { 0, NULL
};
1512 PFN_FREE_ENCODED_OBJECT_FUNC freeObject
;
1515 ret
= retrieve(pszURL
, pszObjectOid
, dwRetrievalFlags
, dwTimeout
,
1516 &object
, &freeObject
, &freeContext
, hAsyncRetrieve
, pCredentials
,
1520 get_object_expiration_func getExpiration
;
1522 ret
= create(pszObjectOid
, dwRetrievalFlags
, &object
, ppvObject
);
1523 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
) &&
1524 CRYPT_GetExpirationFunction(pszObjectOid
, &getExpiration
))
1528 if (getExpiration(*ppvObject
, &expires
))
1529 CRYPT_CacheURL(pszURL
, &object
, dwRetrievalFlags
, expires
);
1531 freeObject(pszObjectOid
, &object
, freeContext
);
1535 CryptFreeOIDFunctionAddress(hCreate
, 0);
1537 CryptFreeOIDFunctionAddress(hRetrieve
, 0);
1538 TRACE("returning %d\n", ret
);
1542 static DWORD
verify_cert_revocation_with_crl_online(PCCERT_CONTEXT cert
,
1543 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1544 PCERT_REVOCATION_STATUS pRevStatus
)
1547 PCRL_ENTRY entry
= NULL
;
1549 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1552 error
= CRYPT_E_REVOKED
;
1553 pRevStatus
->dwIndex
= index
;
1557 /* Since the CRL was retrieved for the cert being checked, then it's
1558 * guaranteed to be fresh, and the cert is not revoked.
1560 error
= ERROR_SUCCESS
;
1565 static DWORD
verify_cert_revocation_from_dist_points_ext(
1566 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1567 FILETIME
*pTime
, DWORD dwFlags
, const CERT_REVOCATION_PARA
*pRevPara
,
1568 PCERT_REVOCATION_STATUS pRevStatus
)
1570 DWORD error
= ERROR_SUCCESS
, cbUrlArray
;
1572 if (CRYPT_GetUrlFromCRLDistPointsExt(value
, NULL
, &cbUrlArray
, NULL
, NULL
))
1574 CRYPT_URL_ARRAY
*urlArray
= CryptMemAlloc(cbUrlArray
);
1578 DWORD j
, retrievalFlags
= 0, startTime
, endTime
, timeout
;
1581 ret
= CRYPT_GetUrlFromCRLDistPointsExt(value
, urlArray
,
1582 &cbUrlArray
, NULL
, NULL
);
1583 if (dwFlags
& CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION
)
1584 retrievalFlags
|= CRYPT_CACHE_ONLY_RETRIEVAL
;
1585 if (dwFlags
& CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG
&&
1586 pRevPara
&& pRevPara
->cbSize
>= offsetof(CERT_REVOCATION_PARA
,
1587 dwUrlRetrievalTimeout
) + sizeof(DWORD
))
1589 startTime
= GetTickCount();
1590 endTime
= startTime
+ pRevPara
->dwUrlRetrievalTimeout
;
1591 timeout
= pRevPara
->dwUrlRetrievalTimeout
;
1594 endTime
= timeout
= 0;
1596 error
= GetLastError();
1597 for (j
= 0; !error
&& j
< urlArray
->cUrl
; j
++)
1601 ret
= CryptRetrieveObjectByUrlW(urlArray
->rgwszUrl
[j
],
1602 CONTEXT_OID_CRL
, retrievalFlags
, timeout
, (void **)&crl
,
1603 NULL
, NULL
, NULL
, NULL
);
1606 error
= verify_cert_revocation_with_crl_online(cert
, crl
,
1607 index
, pTime
, pRevStatus
);
1608 if (!error
&& timeout
)
1610 DWORD time
= GetTickCount();
1612 if ((int)(endTime
- time
) <= 0)
1614 error
= ERROR_TIMEOUT
;
1615 pRevStatus
->dwIndex
= index
;
1618 timeout
= endTime
- time
;
1620 CertFreeCRLContext(crl
);
1623 error
= CRYPT_E_REVOCATION_OFFLINE
;
1625 CryptMemFree(urlArray
);
1629 error
= ERROR_OUTOFMEMORY
;
1630 pRevStatus
->dwIndex
= index
;
1635 error
= GetLastError();
1636 pRevStatus
->dwIndex
= index
;
1641 static DWORD
verify_cert_revocation_from_aia_ext(
1642 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1643 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1644 PCERT_REVOCATION_STATUS pRevStatus
)
1648 CERT_AUTHORITY_INFO_ACCESS
*aia
;
1650 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
1651 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &aia
, &size
);
1656 for (i
= 0; i
< aia
->cAccDescr
; i
++)
1657 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
1660 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
1662 FIXME("OCSP URL = %s\n",
1663 debugstr_w(aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
));
1665 FIXME("unsupported AccessLocation type %d\n",
1666 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
1669 /* FIXME: lie and pretend OCSP validated the cert */
1670 error
= ERROR_SUCCESS
;
1673 error
= GetLastError();
1677 static DWORD
verify_cert_revocation_with_crl_offline(PCCERT_CONTEXT cert
,
1678 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1679 PCERT_REVOCATION_STATUS pRevStatus
)
1684 valid
= CompareFileTime(pTime
, &crl
->pCrlInfo
->ThisUpdate
);
1687 /* If this CRL is not older than the time being verified, there's no
1688 * way to know whether the certificate was revoked.
1690 TRACE("CRL not old enough\n");
1691 error
= CRYPT_E_REVOCATION_OFFLINE
;
1695 PCRL_ENTRY entry
= NULL
;
1697 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1700 error
= CRYPT_E_REVOKED
;
1701 pRevStatus
->dwIndex
= index
;
1705 /* Since the CRL was not retrieved for the cert being checked,
1706 * there's no guarantee it's fresh, so the cert *might* be okay,
1707 * but it's safer not to guess.
1709 TRACE("certificate not found\n");
1710 error
= CRYPT_E_REVOCATION_OFFLINE
;
1716 static DWORD
verify_cert_revocation(PCCERT_CONTEXT cert
, DWORD index
,
1717 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1718 PCERT_REVOCATION_STATUS pRevStatus
)
1720 DWORD error
= ERROR_SUCCESS
;
1721 PCERT_EXTENSION ext
;
1723 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
1724 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1725 error
= verify_cert_revocation_from_dist_points_ext(&ext
->Value
, cert
,
1726 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1727 else if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
1728 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1729 error
= verify_cert_revocation_from_aia_ext(&ext
->Value
, cert
,
1730 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1733 if (pRevPara
&& pRevPara
->hCrlStore
&& pRevPara
->pIssuerCert
)
1735 PCCRL_CONTEXT crl
= NULL
;
1738 /* If the caller told us about the issuer, make sure the issuer
1739 * can sign CRLs before looking for one.
1741 if ((ext
= CertFindExtension(szOID_KEY_USAGE
,
1742 pRevPara
->pIssuerCert
->pCertInfo
->cExtension
,
1743 pRevPara
->pIssuerCert
->pCertInfo
->rgExtension
)))
1745 CRYPT_BIT_BLOB usage
;
1746 DWORD size
= sizeof(usage
);
1748 if (!CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_BITS
,
1749 ext
->Value
.pbData
, ext
->Value
.cbData
,
1750 CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &usage
, &size
))
1751 canSignCRLs
= FALSE
;
1752 else if (usage
.cbData
> 2)
1754 /* The key usage extension only defines 9 bits => no more
1755 * than 2 bytes are needed to encode all known usages.
1757 canSignCRLs
= FALSE
;
1761 BYTE usageBits
= usage
.pbData
[usage
.cbData
- 1];
1763 canSignCRLs
= usageBits
& CERT_CRL_SIGN_KEY_USAGE
;
1770 /* If the caller was helpful enough to tell us where to find a
1771 * CRL for the cert, look for one and check it.
1773 crl
= CertFindCRLInStore(pRevPara
->hCrlStore
,
1774 cert
->dwCertEncodingType
,
1775 CRL_FIND_ISSUED_BY_SIGNATURE_FLAG
|
1776 CRL_FIND_ISSUED_BY_AKI_FLAG
,
1777 CRL_FIND_ISSUED_BY
, pRevPara
->pIssuerCert
, NULL
);
1781 error
= verify_cert_revocation_with_crl_offline(cert
, crl
,
1782 index
, pTime
, pRevStatus
);
1783 CertFreeCRLContext(crl
);
1787 TRACE("no CRL found\n");
1788 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1789 pRevStatus
->dwIndex
= index
;
1795 WARN("no CERT_REVOCATION_PARA\n");
1796 else if (!pRevPara
->hCrlStore
)
1797 WARN("no dist points/aia extension and no CRL store\n");
1798 else if (!pRevPara
->pIssuerCert
)
1799 WARN("no dist points/aia extension and no issuer\n");
1800 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1801 pRevStatus
->dwIndex
= index
;
1807 typedef struct _CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
{
1809 PCCERT_CONTEXT pIssuerCert
;
1811 HCERTSTORE
*rgCertStore
;
1812 HCERTSTORE hCrlStore
;
1813 LPFILETIME pftTimeToUse
;
1814 } CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
;
1816 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1821 } OLD_CERT_REVOCATION_STATUS
;
1823 /***********************************************************************
1824 * CertDllVerifyRevocation (CRYPTNET.@)
1826 BOOL WINAPI
CertDllVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1827 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1828 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1832 LPFILETIME pTime
= NULL
;
1834 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1835 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1837 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1838 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1840 SetLastError(E_INVALIDARG
);
1845 SetLastError(E_INVALIDARG
);
1848 if (pRevPara
&& pRevPara
->cbSize
>=
1849 sizeof(CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
))
1850 pTime
= pRevPara
->pftTimeToUse
;
1853 GetSystemTimeAsFileTime(&now
);
1856 memset(&pRevStatus
->dwIndex
, 0, pRevStatus
->cbSize
- sizeof(DWORD
));
1857 if (dwRevType
!= CERT_CONTEXT_REVOCATION_TYPE
)
1858 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1861 for (i
= 0; !error
&& i
< cContext
; i
++)
1862 error
= verify_cert_revocation(rgpvContext
[i
], i
, pTime
, dwFlags
,
1863 pRevPara
, pRevStatus
);
1867 SetLastError(error
);
1868 pRevStatus
->dwError
= error
;
1870 TRACE("returning %d (%08x)\n", !error
, error
);