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 #define NONAMELESSUNION
22 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(cryptnet
);
39 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
41 static const WCHAR cryptNet
[] = { 'c','r','y','p','t','n','e','t','.',
44 /***********************************************************************
45 * DllRegisterServer (CRYPTNET.@)
47 HRESULT WINAPI
DllRegisterServer(void)
50 CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING
,
51 CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0, cryptNet
);
52 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap",
53 cryptNet
, "LdapProvOpenStore");
54 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
55 CERT_STORE_PROV_LDAP_W
, cryptNet
, "LdapProvOpenStore");
59 /***********************************************************************
60 * DllUnregisterServer (CRYPTNET.@)
62 HRESULT WINAPI
DllUnregisterServer(void)
65 CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING
,
66 CRYPT_OID_VERIFY_REVOCATION_FUNC
, cryptNet
);
67 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap");
68 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
69 CERT_STORE_PROV_LDAP_W
);
73 static const char *url_oid_to_str(LPCSTR oid
)
81 #define _x(oid) case LOWORD(oid): return #oid
82 _x(URL_OID_CERTIFICATE_ISSUER
);
83 _x(URL_OID_CERTIFICATE_CRL_DIST_POINT
);
84 _x(URL_OID_CTL_ISSUER
);
85 _x(URL_OID_CTL_NEXT_UPDATE
);
86 _x(URL_OID_CRL_ISSUER
);
87 _x(URL_OID_CERTIFICATE_FRESHEST_CRL
);
88 _x(URL_OID_CRL_FRESHEST_CRL
);
89 _x(URL_OID_CROSS_CERT_DIST_POINT
);
92 snprintf(buf
, sizeof(buf
), "%d", LOWORD(oid
));
100 typedef BOOL (WINAPI
*UrlDllGetObjectUrlFunc
)(LPCSTR
, LPVOID
, DWORD
,
101 PCRYPT_URL_ARRAY
, DWORD
*, PCRYPT_URL_INFO
, DWORD
*, LPVOID
);
103 static BOOL WINAPI
CRYPT_GetUrlFromCertificateIssuer(LPCSTR pszUrlOid
,
104 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
105 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
107 PCCERT_CONTEXT cert
= pvPara
;
111 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
112 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
114 SetLastError(CRYPT_E_NOT_FOUND
);
117 if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
118 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
120 CERT_AUTHORITY_INFO_ACCESS
*aia
;
123 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
124 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
128 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
130 for (i
= 0, cUrl
= 0; i
< aia
->cAccDescr
; i
++)
131 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
132 szOID_PKIX_CA_ISSUERS
))
134 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
137 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
140 bytesNeeded
+= sizeof(LPWSTR
) +
141 (lstrlenW(aia
->rgAccDescr
[i
].AccessLocation
.u
.
142 pwszURL
) + 1) * sizeof(WCHAR
);
146 FIXME("unsupported alt name type %d\n",
147 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
151 SetLastError(E_INVALIDARG
);
155 *pcbUrlArray
= bytesNeeded
;
156 else if (*pcbUrlArray
< bytesNeeded
)
158 SetLastError(ERROR_MORE_DATA
);
159 *pcbUrlArray
= bytesNeeded
;
166 *pcbUrlArray
= bytesNeeded
;
168 pUrlArray
->rgwszUrl
=
169 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
170 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
171 + cUrl
* sizeof(LPWSTR
));
172 for (i
= 0; i
< aia
->cAccDescr
; i
++)
173 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
174 szOID_PKIX_CA_ISSUERS
))
176 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
177 == CERT_ALT_NAME_URL
)
179 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
182 aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
);
183 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
185 nextUrl
+= (lstrlenW(nextUrl
) + 1);
194 FIXME("url info: stub\n");
196 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
197 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
199 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
200 SetLastError(ERROR_MORE_DATA
);
205 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
206 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
214 SetLastError(CRYPT_E_NOT_FOUND
);
218 static BOOL
CRYPT_GetUrlFromCRLDistPointsExt(const CRYPT_DATA_BLOB
*value
,
219 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
223 CRL_DIST_POINTS_INFO
*info
;
226 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CRL_DIST_POINTS
,
227 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
230 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
232 for (i
= 0, cUrl
= 0; i
< info
->cDistPoint
; i
++)
233 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
234 == CRL_DIST_POINT_FULL_NAME
)
237 CERT_ALT_NAME_INFO
*name
=
238 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
240 for (j
= 0; j
< name
->cAltEntry
; j
++)
241 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
244 if (name
->rgAltEntry
[j
].u
.pwszURL
)
247 bytesNeeded
+= sizeof(LPWSTR
) +
248 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1)
255 SetLastError(E_INVALIDARG
);
259 *pcbUrlArray
= bytesNeeded
;
260 else if (*pcbUrlArray
< bytesNeeded
)
262 SetLastError(ERROR_MORE_DATA
);
263 *pcbUrlArray
= bytesNeeded
;
270 *pcbUrlArray
= bytesNeeded
;
272 pUrlArray
->rgwszUrl
=
273 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
274 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
275 + cUrl
* sizeof(LPWSTR
));
276 for (i
= 0; i
< info
->cDistPoint
; i
++)
277 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
278 == CRL_DIST_POINT_FULL_NAME
)
281 CERT_ALT_NAME_INFO
*name
=
282 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
284 for (j
= 0; j
< name
->cAltEntry
; j
++)
285 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
288 if (name
->rgAltEntry
[j
].u
.pwszURL
)
291 name
->rgAltEntry
[j
].u
.pwszURL
);
292 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
295 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1);
304 FIXME("url info: stub\n");
306 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
307 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
309 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
310 SetLastError(ERROR_MORE_DATA
);
315 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
316 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
325 static BOOL WINAPI
CRYPT_GetUrlFromCertificateCRLDistPoint(LPCSTR pszUrlOid
,
326 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
327 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
329 PCCERT_CONTEXT cert
= pvPara
;
333 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
334 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
336 SetLastError(CRYPT_E_NOT_FOUND
);
339 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
340 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
341 ret
= CRYPT_GetUrlFromCRLDistPointsExt(&ext
->Value
, pUrlArray
,
342 pcbUrlArray
, pUrlInfo
, pcbUrlInfo
);
344 SetLastError(CRYPT_E_NOT_FOUND
);
348 /***********************************************************************
349 * CryptGetObjectUrl (CRYPTNET.@)
351 BOOL WINAPI
CryptGetObjectUrl(LPCSTR pszUrlOid
, LPVOID pvPara
, DWORD dwFlags
,
352 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
353 DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
355 UrlDllGetObjectUrlFunc func
= NULL
;
356 HCRYPTOIDFUNCADDR hFunc
= NULL
;
359 TRACE("(%s, %p, %08x, %p, %p, %p, %p, %p)\n", debugstr_a(pszUrlOid
),
360 pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
, pUrlInfo
, pcbUrlInfo
, pvReserved
);
362 if (IS_INTOID(pszUrlOid
))
364 switch (LOWORD(pszUrlOid
))
366 case LOWORD(URL_OID_CERTIFICATE_ISSUER
):
367 func
= CRYPT_GetUrlFromCertificateIssuer
;
369 case LOWORD(URL_OID_CERTIFICATE_CRL_DIST_POINT
):
370 func
= CRYPT_GetUrlFromCertificateCRLDistPoint
;
373 FIXME("unimplemented for %s\n", url_oid_to_str(pszUrlOid
));
374 SetLastError(ERROR_FILE_NOT_FOUND
);
379 static HCRYPTOIDFUNCSET set
= NULL
;
382 set
= CryptInitOIDFunctionSet(URL_OID_GET_OBJECT_URL_FUNC
, 0);
383 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszUrlOid
, 0,
384 (void **)&func
, &hFunc
);
387 ret
= func(pszUrlOid
, pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
,
388 pUrlInfo
, pcbUrlInfo
, pvReserved
);
390 CryptFreeOIDFunctionAddress(hFunc
, 0);
394 /***********************************************************************
395 * CryptRetrieveObjectByUrlA (CRYPTNET.@)
397 BOOL WINAPI
CryptRetrieveObjectByUrlA(LPCSTR pszURL
, LPCSTR pszObjectOid
,
398 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
399 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
400 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
405 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_a(pszURL
),
406 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
407 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
411 SetLastError(ERROR_INVALID_PARAMETER
);
414 len
= MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, NULL
, 0);
417 LPWSTR url
= CryptMemAlloc(len
* sizeof(WCHAR
));
421 MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, url
, len
);
422 ret
= CryptRetrieveObjectByUrlW(url
, pszObjectOid
,
423 dwRetrievalFlags
, dwTimeout
, ppvObject
, hAsyncRetrieve
,
424 pCredentials
, pvVerify
, pAuxInfo
);
428 SetLastError(ERROR_OUTOFMEMORY
);
433 static void WINAPI
CRYPT_FreeBlob(LPCSTR pszObjectOid
,
434 PCRYPT_BLOB_ARRAY pObject
, void *pvFreeContext
)
438 for (i
= 0; i
< pObject
->cBlob
; i
++)
439 CryptMemFree(pObject
->rgBlob
[i
].pbData
);
440 CryptMemFree(pObject
->rgBlob
);
443 static BOOL
CRYPT_GetObjectFromFile(HANDLE hFile
, PCRYPT_BLOB_ARRAY pObject
)
448 if ((ret
= GetFileSizeEx(hFile
, &size
)))
452 WARN("file too big\n");
453 SetLastError(ERROR_INVALID_DATA
);
458 CRYPT_DATA_BLOB blob
;
460 blob
.pbData
= CryptMemAlloc(size
.u
.LowPart
);
463 ret
= ReadFile(hFile
, blob
.pbData
, size
.u
.LowPart
, &blob
.cbData
,
467 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
471 memcpy(pObject
->rgBlob
, &blob
, sizeof(CRYPT_DATA_BLOB
));
475 SetLastError(ERROR_OUTOFMEMORY
);
480 CryptMemFree(blob
.pbData
);
484 SetLastError(ERROR_OUTOFMEMORY
);
492 static BOOL
CRYPT_GetObjectFromCache(LPCWSTR pszURL
, PCRYPT_BLOB_ARRAY pObject
,
493 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
496 INTERNET_CACHE_ENTRY_INFOW
*pCacheInfo
= NULL
;
499 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pObject
, pAuxInfo
);
501 RetrieveUrlCacheEntryFileW(pszURL
, NULL
, &size
, 0);
502 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
505 pCacheInfo
= CryptMemAlloc(size
);
508 SetLastError(ERROR_OUTOFMEMORY
);
512 if ((ret
= RetrieveUrlCacheEntryFileW(pszURL
, pCacheInfo
, &size
, 0)))
516 GetSystemTimeAsFileTime(&ft
);
517 if (CompareFileTime(&pCacheInfo
->ExpireTime
, &ft
) >= 0)
519 HANDLE hFile
= CreateFileW(pCacheInfo
->lpszLocalFileName
, GENERIC_READ
,
520 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
522 if (hFile
!= INVALID_HANDLE_VALUE
)
524 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
526 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
527 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
528 pLastSyncTime
) + sizeof(PFILETIME
) &&
529 pAuxInfo
->pLastSyncTime
)
530 memcpy(pAuxInfo
->pLastSyncTime
,
531 &pCacheInfo
->LastSyncTime
,
538 DeleteUrlCacheEntryW(pszURL
);
544 DeleteUrlCacheEntryW(pszURL
);
547 UnlockUrlCacheEntryFileW(pszURL
, 0);
549 CryptMemFree(pCacheInfo
);
550 TRACE("returning %d\n", ret
);
554 /* Parses the URL, and sets components' lpszHostName and lpszUrlPath members
555 * to NULL-terminated copies of those portions of the URL (to be freed with
558 static BOOL
CRYPT_CrackUrl(LPCWSTR pszURL
, URL_COMPONENTSW
*components
)
562 TRACE("(%s, %p)\n", debugstr_w(pszURL
), components
);
564 memset(components
, 0, sizeof(*components
));
565 components
->dwStructSize
= sizeof(*components
);
566 components
->lpszHostName
= CryptMemAlloc(INTERNET_MAX_HOST_NAME_LENGTH
* sizeof(WCHAR
));
567 components
->dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
568 if (!components
->lpszHostName
)
570 SetLastError(ERROR_OUTOFMEMORY
);
573 components
->lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
574 components
->dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
575 if (!components
->lpszUrlPath
)
577 CryptMemFree(components
->lpszHostName
);
578 SetLastError(ERROR_OUTOFMEMORY
);
582 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, components
);
585 switch (components
->nScheme
)
587 case INTERNET_SCHEME_FTP
:
588 if (!components
->nPort
)
589 components
->nPort
= INTERNET_DEFAULT_FTP_PORT
;
591 case INTERNET_SCHEME_HTTP
:
592 if (!components
->nPort
)
593 components
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
599 TRACE("returning %d\n", ret
);
610 static struct InetContext
*CRYPT_MakeInetContext(DWORD dwTimeout
)
612 struct InetContext
*context
= CryptMemAlloc(sizeof(struct InetContext
));
616 context
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
619 CryptMemFree(context
);
624 context
->timeout
= dwTimeout
;
625 context
->error
= ERROR_SUCCESS
;
631 static BOOL
CRYPT_DownloadObject(DWORD dwRetrievalFlags
, HINTERNET hHttp
,
632 struct InetContext
*context
, PCRYPT_BLOB_ARRAY pObject
,
633 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
635 CRYPT_DATA_BLOB object
= { 0, NULL
};
636 DWORD bytesAvailable
;
640 if ((ret
= InternetQueryDataAvailable(hHttp
, &bytesAvailable
, 0, 0)))
645 object
.pbData
= CryptMemRealloc(object
.pbData
,
646 object
.cbData
+ bytesAvailable
);
648 object
.pbData
= CryptMemAlloc(bytesAvailable
);
651 INTERNET_BUFFERSA buffer
= { sizeof(buffer
), 0 };
653 buffer
.dwBufferLength
= bytesAvailable
;
654 buffer
.lpvBuffer
= object
.pbData
+ object
.cbData
;
655 if (!(ret
= InternetReadFileExA(hHttp
, &buffer
, IRF_NO_WAIT
,
656 (DWORD_PTR
)context
)))
658 if (GetLastError() == ERROR_IO_PENDING
)
660 if (WaitForSingleObject(context
->event
,
661 context
->timeout
) == WAIT_TIMEOUT
)
662 SetLastError(ERROR_TIMEOUT
);
663 else if (context
->error
)
664 SetLastError(context
->error
);
670 object
.cbData
+= buffer
.dwBufferLength
;
674 SetLastError(ERROR_OUTOFMEMORY
);
679 else if (GetLastError() == ERROR_IO_PENDING
)
681 if (WaitForSingleObject(context
->event
, context
->timeout
) ==
683 SetLastError(ERROR_TIMEOUT
);
687 } while (ret
&& bytesAvailable
);
690 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
691 if (!pObject
->rgBlob
)
693 CryptMemFree(object
.pbData
);
694 SetLastError(ERROR_OUTOFMEMORY
);
699 pObject
->rgBlob
[0].cbData
= object
.cbData
;
700 pObject
->rgBlob
[0].pbData
= object
.pbData
;
704 TRACE("returning %d\n", ret
);
708 /* Finds the object specified by pszURL in the cache. If it's not found,
709 * creates a new cache entry for the object and writes the object to it.
710 * Sets the expiration time of the cache entry to expires.
712 static void CRYPT_CacheURL(LPCWSTR pszURL
, const CRYPT_BLOB_ARRAY
*pObject
,
713 DWORD dwRetrievalFlags
, FILETIME expires
)
715 WCHAR cacheFileName
[MAX_PATH
];
717 DWORD size
= 0, entryType
;
720 GetUrlCacheEntryInfoW(pszURL
, NULL
, &size
);
721 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
723 INTERNET_CACHE_ENTRY_INFOW
*info
= CryptMemAlloc(size
);
727 ERR("out of memory\n");
731 if (GetUrlCacheEntryInfoW(pszURL
, info
, &size
))
733 lstrcpyW(cacheFileName
, info
->lpszLocalFileName
);
734 /* Check if the existing cache entry is up to date. If it isn't,
735 * remove the existing cache entry, and create a new one with the
738 GetSystemTimeAsFileTime(&ft
);
739 if (CompareFileTime(&info
->ExpireTime
, &ft
) < 0)
741 DeleteUrlCacheEntryW(pszURL
);
745 info
->ExpireTime
= expires
;
746 SetUrlCacheEntryInfoW(pszURL
, info
, CACHE_ENTRY_EXPTIME_FC
);
754 if (!CreateUrlCacheEntryW(pszURL
, pObject
->rgBlob
[0].cbData
, NULL
, cacheFileName
, 0))
757 hCacheFile
= CreateFileW(cacheFileName
, GENERIC_WRITE
, 0,
758 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
759 if(hCacheFile
== INVALID_HANDLE_VALUE
)
762 WriteFile(hCacheFile
, pObject
->rgBlob
[0].pbData
,
763 pObject
->rgBlob
[0].cbData
, &size
, NULL
);
764 CloseHandle(hCacheFile
);
766 if (!(dwRetrievalFlags
& CRYPT_STICKY_CACHE_RETRIEVAL
))
767 entryType
= NORMAL_CACHE_ENTRY
;
769 entryType
= STICKY_CACHE_ENTRY
;
770 memset(&ft
, 0, sizeof(ft
));
771 CommitUrlCacheEntryW(pszURL
, cacheFileName
, expires
, ft
, entryType
,
772 NULL
, 0, NULL
, NULL
);
775 static void CALLBACK
CRYPT_InetStatusCallback(HINTERNET hInt
,
776 DWORD_PTR dwContext
, DWORD status
, void *statusInfo
, DWORD statusInfoLen
)
778 struct InetContext
*context
= (struct InetContext
*)dwContext
;
779 LPINTERNET_ASYNC_RESULT result
;
783 case INTERNET_STATUS_REQUEST_COMPLETE
:
785 context
->error
= result
->dwError
;
786 SetEvent(context
->event
);
790 static BOOL
CRYPT_Connect(const URL_COMPONENTSW
*components
,
791 struct InetContext
*context
, PCRYPT_CREDENTIALS pCredentials
,
792 HINTERNET
*phInt
, HINTERNET
*phHost
)
796 TRACE("(%s:%d, %p, %p, %p, %p)\n", debugstr_w(components
->lpszHostName
),
797 components
->nPort
, context
, pCredentials
, phInt
, phInt
);
800 *phInt
= InternetOpenW(NULL
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
,
801 context
? INTERNET_FLAG_ASYNC
: 0);
807 InternetSetStatusCallbackW(*phInt
, CRYPT_InetStatusCallback
);
808 switch (components
->nScheme
)
810 case INTERNET_SCHEME_FTP
:
811 service
= INTERNET_SERVICE_FTP
;
813 case INTERNET_SCHEME_HTTP
:
814 service
= INTERNET_SERVICE_HTTP
;
819 /* FIXME: use pCredentials for username/password */
820 *phHost
= InternetConnectW(*phInt
, components
->lpszHostName
,
821 components
->nPort
, NULL
, NULL
, service
, 0, (DWORD_PTR
)context
);
824 InternetCloseHandle(*phInt
);
833 TRACE("returning %d\n", ret
);
837 static BOOL WINAPI
FTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
838 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
839 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
840 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
841 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
843 FIXME("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
844 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
845 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
848 pObject
->rgBlob
= NULL
;
849 *ppfnFreeObject
= CRYPT_FreeBlob
;
850 *ppvFreeContext
= NULL
;
854 static const WCHAR x509cacert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
855 '/','x','-','x','5','0','9','-','c','a','-','c','e','r','t',0 };
856 static const WCHAR x509emailcert
[] = { 'a','p','p','l','i','c','a','t','i','o',
857 'n','/','x','-','x','5','0','9','-','e','m','a','i','l','-','c','e','r','t',
859 static const WCHAR x509servercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
860 'n','/','x','-','x','5','0','9','-','s','e','r','v','e','r','-','c','e','r',
862 static const WCHAR x509usercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
863 'n','/','x','-','x','5','0','9','-','u','s','e','r','-','c','e','r','t',0 };
864 static const WCHAR pkcs7cert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
865 '/','x','-','p','k','c','s','7','-','c','e','r','t','i','f','c','a','t','e',
867 static const WCHAR pkixCRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
868 '/','p','k','i','x','-','c','r','l',0 };
869 static const WCHAR pkcs7CRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
870 '/','x','-','p','k','c','s','7','-','c','r','l',0 };
871 static const WCHAR pkcs7sig
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
872 '/','x','-','p','k','c','s','7','-','s','i','g','n','a','t','u','r','e',0 };
873 static const WCHAR pkcs7mime
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
874 '/','x','-','p','k','c','s','7','-','m','i','m','e',0 };
876 static BOOL WINAPI
HTTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
877 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
878 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
879 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
880 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
884 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
885 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
886 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
889 pObject
->rgBlob
= NULL
;
890 *ppfnFreeObject
= CRYPT_FreeBlob
;
891 *ppvFreeContext
= NULL
;
893 if (!(dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
))
894 ret
= CRYPT_GetObjectFromCache(pszURL
, pObject
, pAuxInfo
);
895 if (!ret
&& (!(dwRetrievalFlags
& CRYPT_CACHE_ONLY_RETRIEVAL
) ||
896 (dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
)))
898 URL_COMPONENTSW components
;
900 if ((ret
= CRYPT_CrackUrl(pszURL
, &components
)))
902 HINTERNET hInt
, hHost
;
903 struct InetContext
*context
= NULL
;
906 context
= CRYPT_MakeInetContext(dwTimeout
);
907 ret
= CRYPT_Connect(&components
, context
, pCredentials
, &hInt
,
911 static LPCWSTR types
[] = { x509cacert
, x509emailcert
,
912 x509servercert
, x509usercert
, pkcs7cert
, pkixCRL
, pkcs7CRL
,
913 pkcs7sig
, pkcs7mime
, NULL
};
914 HINTERNET hHttp
= HttpOpenRequestW(hHost
, NULL
,
915 components
.lpszUrlPath
, NULL
, NULL
, types
,
916 INTERNET_FLAG_NO_COOKIES
| INTERNET_FLAG_NO_UI
,
923 InternetSetOptionW(hHttp
,
924 INTERNET_OPTION_RECEIVE_TIMEOUT
, &dwTimeout
,
926 InternetSetOptionW(hHttp
, INTERNET_OPTION_SEND_TIMEOUT
,
927 &dwTimeout
, sizeof(dwTimeout
));
929 ret
= HttpSendRequestExW(hHttp
, NULL
, NULL
, 0,
931 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
933 if (WaitForSingleObject(context
->event
,
934 context
->timeout
) == WAIT_TIMEOUT
)
935 SetLastError(ERROR_TIMEOUT
);
940 !(ret
= HttpEndRequestW(hHttp
, NULL
, 0, (DWORD_PTR
)context
)) &&
941 GetLastError() == ERROR_IO_PENDING
)
943 if (WaitForSingleObject(context
->event
,
944 context
->timeout
) == WAIT_TIMEOUT
)
945 SetLastError(ERROR_TIMEOUT
);
950 ret
= CRYPT_DownloadObject(dwRetrievalFlags
, hHttp
,
951 context
, pObject
, pAuxInfo
);
952 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
))
956 DWORD len
= sizeof(st
);
958 if (HttpQueryInfoW(hHttp
, HTTP_QUERY_EXPIRES
| HTTP_QUERY_FLAG_SYSTEMTIME
,
959 &st
, &len
, NULL
) && SystemTimeToFileTime(&st
, &ft
))
960 CRYPT_CacheURL(pszURL
, pObject
, dwRetrievalFlags
, ft
);
962 InternetCloseHandle(hHttp
);
964 InternetCloseHandle(hHost
);
965 InternetCloseHandle(hInt
);
969 CloseHandle(context
->event
);
970 CryptMemFree(context
);
972 CryptMemFree(components
.lpszUrlPath
);
973 CryptMemFree(components
.lpszHostName
);
976 TRACE("returning %d\n", ret
);
980 static BOOL WINAPI
File_RetrieveEncodedObjectW(LPCWSTR pszURL
,
981 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
982 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
983 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
984 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
986 URL_COMPONENTSW components
= { sizeof(components
), 0 };
989 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
990 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
991 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
994 pObject
->rgBlob
= NULL
;
995 *ppfnFreeObject
= CRYPT_FreeBlob
;
996 *ppvFreeContext
= NULL
;
998 components
.lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
999 components
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
1000 if (!components
.lpszUrlPath
)
1002 SetLastError(ERROR_OUTOFMEMORY
);
1006 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, &components
);
1011 /* 3 == lstrlenW(L"c:") + 1 */
1012 path
= CryptMemAlloc((components
.dwUrlPathLength
+ 3) * sizeof(WCHAR
));
1017 /* Try to create the file directly - Wine handles / in pathnames */
1018 lstrcpynW(path
, components
.lpszUrlPath
,
1019 components
.dwUrlPathLength
+ 1);
1020 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1021 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1022 if (hFile
== INVALID_HANDLE_VALUE
)
1024 /* Try again on the current drive */
1025 GetCurrentDirectoryW(components
.dwUrlPathLength
, path
);
1028 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1029 components
.dwUrlPathLength
+ 1);
1030 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1031 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1033 if (hFile
== INVALID_HANDLE_VALUE
)
1035 /* Try again on the Windows drive */
1036 GetWindowsDirectoryW(path
, components
.dwUrlPathLength
);
1039 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1040 components
.dwUrlPathLength
+ 1);
1041 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1042 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1046 if (hFile
!= INVALID_HANDLE_VALUE
)
1048 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
1050 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
1051 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
1052 pLastSyncTime
) + sizeof(PFILETIME
) &&
1053 pAuxInfo
->pLastSyncTime
)
1054 GetFileTime(hFile
, NULL
, NULL
,
1055 pAuxInfo
->pLastSyncTime
);
1065 SetLastError(ERROR_OUTOFMEMORY
);
1069 CryptMemFree(components
.lpszUrlPath
);
1073 typedef BOOL (WINAPI
*SchemeDllRetrieveEncodedObjectW
)(LPCWSTR pwszUrl
,
1074 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
1075 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
1076 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
1077 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
);
1079 static BOOL
CRYPT_GetRetrieveFunction(LPCWSTR pszURL
,
1080 SchemeDllRetrieveEncodedObjectW
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1082 URL_COMPONENTSW components
= { sizeof(components
), 0 };
1085 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pFunc
, phFunc
);
1089 components
.dwSchemeLength
= 1;
1090 ret
= InternetCrackUrlW(pszURL
, 0, 0, &components
);
1093 /* Microsoft always uses CryptInitOIDFunctionSet/
1094 * CryptGetOIDFunctionAddress, but there doesn't seem to be a pressing
1095 * reason to do so for builtin schemes.
1097 switch (components
.nScheme
)
1099 case INTERNET_SCHEME_FTP
:
1100 *pFunc
= FTP_RetrieveEncodedObjectW
;
1102 case INTERNET_SCHEME_HTTP
:
1103 *pFunc
= HTTP_RetrieveEncodedObjectW
;
1105 case INTERNET_SCHEME_FILE
:
1106 *pFunc
= File_RetrieveEncodedObjectW
;
1110 int len
= WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1111 components
.dwSchemeLength
, NULL
, 0, NULL
, NULL
);
1115 LPSTR scheme
= CryptMemAlloc(len
);
1119 static HCRYPTOIDFUNCSET set
= NULL
;
1122 set
= CryptInitOIDFunctionSet(
1123 SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC
, 0);
1124 WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1125 components
.dwSchemeLength
, scheme
, len
, NULL
, NULL
);
1126 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
,
1127 scheme
, 0, (void **)pFunc
, phFunc
);
1128 CryptMemFree(scheme
);
1132 SetLastError(ERROR_OUTOFMEMORY
);
1141 TRACE("returning %d\n", ret
);
1145 static BOOL WINAPI
CRYPT_CreateBlob(LPCSTR pszObjectOid
,
1146 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1149 CRYPT_BLOB_ARRAY
*context
;
1152 size
= sizeof(CRYPT_BLOB_ARRAY
) + pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1153 for (i
= 0; i
< pObject
->cBlob
; i
++)
1154 size
+= pObject
->rgBlob
[i
].cbData
;
1155 context
= CryptMemAlloc(size
);
1162 (CRYPT_DATA_BLOB
*)((LPBYTE
)context
+ sizeof(CRYPT_BLOB_ARRAY
));
1164 (LPBYTE
)context
->rgBlob
+ pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1165 for (i
= 0; i
< pObject
->cBlob
; i
++)
1167 memcpy(nextData
, pObject
->rgBlob
[i
].pbData
,
1168 pObject
->rgBlob
[i
].cbData
);
1169 context
->rgBlob
[i
].pbData
= nextData
;
1170 context
->rgBlob
[i
].cbData
= pObject
->rgBlob
[i
].cbData
;
1171 nextData
+= pObject
->rgBlob
[i
].cbData
;
1174 *ppvContext
= context
;
1180 typedef BOOL (WINAPI
*AddContextToStore
)(HCERTSTORE hCertStore
,
1181 const void *pContext
, DWORD dwAddDisposition
, const void **ppStoreContext
);
1183 static BOOL
decode_base64_blob( const CRYPT_DATA_BLOB
*in
, CRYPT_DATA_BLOB
*out
)
1186 DWORD len
= in
->cbData
;
1188 while (len
&& !in
->pbData
[len
- 1]) len
--;
1189 if (!CryptStringToBinaryA( (char *)in
->pbData
, len
, CRYPT_STRING_BASE64_ANY
,
1190 NULL
, &out
->cbData
, NULL
, NULL
)) return FALSE
;
1192 if (!(out
->pbData
= CryptMemAlloc( out
->cbData
))) return FALSE
;
1193 ret
= CryptStringToBinaryA( (char *)in
->pbData
, len
, CRYPT_STRING_BASE64_ANY
,
1194 out
->pbData
, &out
->cbData
, NULL
, NULL
);
1195 if (!ret
) CryptMemFree( out
->pbData
);
1199 static BOOL
CRYPT_CreateContext(const CRYPT_BLOB_ARRAY
*pObject
,
1200 DWORD dwExpectedContentTypeFlags
, AddContextToStore addFunc
, void **ppvContext
)
1203 CRYPT_DATA_BLOB blob
;
1205 if (!pObject
->cBlob
)
1207 SetLastError(ERROR_INVALID_DATA
);
1211 else if (pObject
->cBlob
== 1)
1213 if (decode_base64_blob(&pObject
->rgBlob
[0], &blob
))
1215 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
1216 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
, 0,
1217 NULL
, NULL
, NULL
, NULL
, NULL
, (const void **)ppvContext
);
1218 CryptMemFree(blob
.pbData
);
1222 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1223 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
, 0,
1224 NULL
, NULL
, NULL
, NULL
, NULL
, (const void **)ppvContext
);
1228 SetLastError(CRYPT_E_NO_MATCH
);
1234 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1235 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1240 const void *context
;
1242 for (i
= 0; i
< pObject
->cBlob
; i
++)
1244 if (decode_base64_blob(&pObject
->rgBlob
[i
], &blob
))
1246 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
1247 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
,
1248 0, NULL
, NULL
, NULL
, NULL
, NULL
, &context
);
1249 CryptMemFree(blob
.pbData
);
1253 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1254 &pObject
->rgBlob
[i
], dwExpectedContentTypeFlags
,
1255 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
, NULL
,
1260 if (!addFunc(store
, context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1265 SetLastError(CRYPT_E_NO_MATCH
);
1272 *ppvContext
= store
;
1277 static BOOL WINAPI
CRYPT_CreateCert(LPCSTR pszObjectOid
,
1278 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1280 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CERT
,
1281 (AddContextToStore
)CertAddCertificateContextToStore
, ppvContext
);
1284 static BOOL WINAPI
CRYPT_CreateCRL(LPCSTR pszObjectOid
,
1285 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1287 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CRL
,
1288 (AddContextToStore
)CertAddCRLContextToStore
, ppvContext
);
1291 static BOOL WINAPI
CRYPT_CreateCTL(LPCSTR pszObjectOid
,
1292 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1294 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CTL
,
1295 (AddContextToStore
)CertAddCTLContextToStore
, ppvContext
);
1298 static BOOL WINAPI
CRYPT_CreatePKCS7(LPCSTR pszObjectOid
,
1299 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1303 if (!pObject
->cBlob
)
1305 SetLastError(ERROR_INVALID_DATA
);
1309 else if (pObject
->cBlob
== 1)
1310 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1311 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1312 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
, CERT_QUERY_FORMAT_FLAG_BINARY
,
1313 0, NULL
, NULL
, NULL
, ppvContext
, NULL
, NULL
);
1316 FIXME("multiple messages unimplemented\n");
1322 static BOOL WINAPI
CRYPT_CreateAny(LPCSTR pszObjectOid
,
1323 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1327 if (!pObject
->cBlob
)
1329 SetLastError(ERROR_INVALID_DATA
);
1335 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
1336 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1340 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1341 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1345 CertAddStoreToCollection(store
, memStore
,
1346 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1347 CertCloseStore(memStore
, 0);
1351 CertCloseStore(store
, 0);
1360 for (i
= 0; i
< pObject
->cBlob
; i
++)
1362 DWORD contentType
, expectedContentTypes
=
1363 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1364 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
|
1365 CERT_QUERY_CONTENT_FLAG_CERT
|
1366 CERT_QUERY_CONTENT_FLAG_CRL
|
1367 CERT_QUERY_CONTENT_FLAG_CTL
;
1368 HCERTSTORE contextStore
;
1369 const void *context
;
1371 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1372 &pObject
->rgBlob
[i
], expectedContentTypes
,
1373 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, &contentType
, NULL
,
1374 &contextStore
, NULL
, &context
))
1376 switch (contentType
)
1378 case CERT_QUERY_CONTENT_CERT
:
1379 if (!CertAddCertificateContextToStore(store
,
1380 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1382 CertFreeCertificateContext(context
);
1384 case CERT_QUERY_CONTENT_CRL
:
1385 if (!CertAddCRLContextToStore(store
,
1386 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1388 CertFreeCRLContext(context
);
1390 case CERT_QUERY_CONTENT_CTL
:
1391 if (!CertAddCTLContextToStore(store
,
1392 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1394 CertFreeCTLContext(context
);
1397 CertAddStoreToCollection(store
, contextStore
, 0, 0);
1399 CertCloseStore(contextStore
, 0);
1407 *ppvContext
= store
;
1412 typedef BOOL (WINAPI
*ContextDllCreateObjectContext
)(LPCSTR pszObjectOid
,
1413 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
);
1415 static BOOL
CRYPT_GetCreateFunction(LPCSTR pszObjectOid
,
1416 ContextDllCreateObjectContext
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1420 TRACE("(%s, %p, %p)\n", debugstr_a(pszObjectOid
), pFunc
, phFunc
);
1424 if (IS_INTOID(pszObjectOid
))
1426 switch (LOWORD(pszObjectOid
))
1429 *pFunc
= CRYPT_CreateBlob
;
1431 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1432 *pFunc
= CRYPT_CreateCert
;
1434 case LOWORD(CONTEXT_OID_CRL
):
1435 *pFunc
= CRYPT_CreateCRL
;
1437 case LOWORD(CONTEXT_OID_CTL
):
1438 *pFunc
= CRYPT_CreateCTL
;
1440 case LOWORD(CONTEXT_OID_PKCS7
):
1441 *pFunc
= CRYPT_CreatePKCS7
;
1443 case LOWORD(CONTEXT_OID_CAPI2_ANY
):
1444 *pFunc
= CRYPT_CreateAny
;
1450 static HCRYPTOIDFUNCSET set
= NULL
;
1453 set
= CryptInitOIDFunctionSet(
1454 CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC
, 0);
1455 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszObjectOid
,
1456 0, (void **)pFunc
, phFunc
);
1458 TRACE("returning %d\n", ret
);
1462 static BOOL
CRYPT_GetExpiration(const void *object
, const char *pszObjectOid
, FILETIME
*expiration
)
1464 if (!IS_INTOID(pszObjectOid
))
1467 switch (LOWORD(pszObjectOid
)) {
1468 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1469 *expiration
= ((const CERT_CONTEXT
*)object
)->pCertInfo
->NotAfter
;
1471 case LOWORD(CONTEXT_OID_CRL
):
1472 *expiration
= ((const CRL_CONTEXT
*)object
)->pCrlInfo
->NextUpdate
;
1474 case LOWORD(CONTEXT_OID_CTL
):
1475 *expiration
= ((const CTL_CONTEXT
*)object
)->pCtlInfo
->NextUpdate
;
1482 /***********************************************************************
1483 * CryptRetrieveObjectByUrlW (CRYPTNET.@)
1485 BOOL WINAPI
CryptRetrieveObjectByUrlW(LPCWSTR pszURL
, LPCSTR pszObjectOid
,
1486 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
1487 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
1488 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
1491 SchemeDllRetrieveEncodedObjectW retrieve
;
1492 ContextDllCreateObjectContext create
;
1493 HCRYPTOIDFUNCADDR hRetrieve
= 0, hCreate
= 0;
1495 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
1496 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
1497 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
1501 SetLastError(ERROR_INVALID_PARAMETER
);
1504 ret
= CRYPT_GetRetrieveFunction(pszURL
, &retrieve
, &hRetrieve
);
1506 ret
= CRYPT_GetCreateFunction(pszObjectOid
, &create
, &hCreate
);
1509 CRYPT_BLOB_ARRAY object
= { 0, NULL
};
1510 PFN_FREE_ENCODED_OBJECT_FUNC freeObject
;
1514 ret
= retrieve(pszURL
, pszObjectOid
, dwRetrievalFlags
, dwTimeout
,
1515 &object
, &freeObject
, &freeContext
, hAsyncRetrieve
, pCredentials
,
1519 ret
= create(pszObjectOid
, dwRetrievalFlags
, &object
, ppvObject
);
1520 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
) &&
1521 CRYPT_GetExpiration(*ppvObject
, pszObjectOid
, &expires
))
1523 CRYPT_CacheURL(pszURL
, &object
, dwRetrievalFlags
, expires
);
1525 freeObject(pszObjectOid
, &object
, freeContext
);
1529 CryptFreeOIDFunctionAddress(hCreate
, 0);
1531 CryptFreeOIDFunctionAddress(hRetrieve
, 0);
1532 TRACE("returning %d\n", ret
);
1536 static DWORD
verify_cert_revocation_with_crl_online(PCCERT_CONTEXT cert
,
1537 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1538 PCERT_REVOCATION_STATUS pRevStatus
)
1541 PCRL_ENTRY entry
= NULL
;
1543 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1546 error
= CRYPT_E_REVOKED
;
1547 pRevStatus
->dwIndex
= index
;
1551 /* Since the CRL was retrieved for the cert being checked, then it's
1552 * guaranteed to be fresh, and the cert is not revoked.
1554 error
= ERROR_SUCCESS
;
1559 static DWORD
verify_cert_revocation_from_dist_points_ext(
1560 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1561 FILETIME
*pTime
, DWORD dwFlags
, const CERT_REVOCATION_PARA
*pRevPara
,
1562 PCERT_REVOCATION_STATUS pRevStatus
)
1564 DWORD error
= ERROR_SUCCESS
, cbUrlArray
;
1566 if (CRYPT_GetUrlFromCRLDistPointsExt(value
, NULL
, &cbUrlArray
, NULL
, NULL
))
1568 CRYPT_URL_ARRAY
*urlArray
= CryptMemAlloc(cbUrlArray
);
1572 DWORD j
, retrievalFlags
= 0, startTime
, endTime
, timeout
;
1575 ret
= CRYPT_GetUrlFromCRLDistPointsExt(value
, urlArray
,
1576 &cbUrlArray
, NULL
, NULL
);
1577 if (dwFlags
& CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION
)
1578 retrievalFlags
|= CRYPT_CACHE_ONLY_RETRIEVAL
;
1579 if (dwFlags
& CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG
&&
1580 pRevPara
&& pRevPara
->cbSize
>= offsetof(CERT_REVOCATION_PARA
,
1581 dwUrlRetrievalTimeout
) + sizeof(DWORD
))
1583 startTime
= GetTickCount();
1584 endTime
= startTime
+ pRevPara
->dwUrlRetrievalTimeout
;
1585 timeout
= pRevPara
->dwUrlRetrievalTimeout
;
1588 endTime
= timeout
= 0;
1590 error
= GetLastError();
1591 /* continue looping if one was offline; break if revoked or timed out */
1592 for (j
= 0; (!error
|| error
== CRYPT_E_REVOCATION_OFFLINE
) && j
< urlArray
->cUrl
; j
++)
1596 ret
= CryptRetrieveObjectByUrlW(urlArray
->rgwszUrl
[j
],
1597 CONTEXT_OID_CRL
, retrievalFlags
, timeout
, (void **)&crl
,
1598 NULL
, NULL
, NULL
, NULL
);
1601 error
= verify_cert_revocation_with_crl_online(cert
, crl
,
1602 index
, pTime
, pRevStatus
);
1603 if (!error
&& timeout
)
1605 DWORD time
= GetTickCount();
1607 if ((int)(endTime
- time
) <= 0)
1609 error
= ERROR_TIMEOUT
;
1610 pRevStatus
->dwIndex
= index
;
1613 timeout
= endTime
- time
;
1615 CertFreeCRLContext(crl
);
1618 error
= CRYPT_E_REVOCATION_OFFLINE
;
1620 CryptMemFree(urlArray
);
1624 error
= ERROR_OUTOFMEMORY
;
1625 pRevStatus
->dwIndex
= index
;
1630 error
= GetLastError();
1631 pRevStatus
->dwIndex
= index
;
1636 static DWORD
verify_cert_revocation_from_aia_ext(
1637 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1638 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1639 PCERT_REVOCATION_STATUS pRevStatus
)
1643 CERT_AUTHORITY_INFO_ACCESS
*aia
;
1645 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
1646 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &aia
, &size
);
1651 for (i
= 0; i
< aia
->cAccDescr
; i
++)
1652 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
1655 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
1657 FIXME("OCSP URL = %s\n",
1658 debugstr_w(aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
));
1660 FIXME("unsupported AccessLocation type %d\n",
1661 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
1664 /* FIXME: lie and pretend OCSP validated the cert */
1665 error
= ERROR_SUCCESS
;
1668 error
= GetLastError();
1672 static DWORD
verify_cert_revocation_with_crl_offline(PCCERT_CONTEXT cert
,
1673 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1674 PCERT_REVOCATION_STATUS pRevStatus
)
1679 valid
= CompareFileTime(pTime
, &crl
->pCrlInfo
->ThisUpdate
);
1682 /* If this CRL is not older than the time being verified, there's no
1683 * way to know whether the certificate was revoked.
1685 TRACE("CRL not old enough\n");
1686 error
= CRYPT_E_REVOCATION_OFFLINE
;
1690 PCRL_ENTRY entry
= NULL
;
1692 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1695 error
= CRYPT_E_REVOKED
;
1696 pRevStatus
->dwIndex
= index
;
1700 /* Since the CRL was not retrieved for the cert being checked,
1701 * there's no guarantee it's fresh, so the cert *might* be okay,
1702 * but it's safer not to guess.
1704 TRACE("certificate not found\n");
1705 error
= CRYPT_E_REVOCATION_OFFLINE
;
1711 static DWORD
verify_cert_revocation(PCCERT_CONTEXT cert
, DWORD index
,
1712 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1713 PCERT_REVOCATION_STATUS pRevStatus
)
1715 DWORD error
= ERROR_SUCCESS
;
1716 PCERT_EXTENSION ext
;
1718 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
1719 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1720 error
= verify_cert_revocation_from_dist_points_ext(&ext
->Value
, cert
,
1721 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1722 else if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
1723 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1724 error
= verify_cert_revocation_from_aia_ext(&ext
->Value
, cert
,
1725 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1728 if (pRevPara
&& pRevPara
->hCrlStore
&& pRevPara
->pIssuerCert
)
1730 PCCRL_CONTEXT crl
= NULL
;
1733 /* If the caller told us about the issuer, make sure the issuer
1734 * can sign CRLs before looking for one.
1736 if ((ext
= CertFindExtension(szOID_KEY_USAGE
,
1737 pRevPara
->pIssuerCert
->pCertInfo
->cExtension
,
1738 pRevPara
->pIssuerCert
->pCertInfo
->rgExtension
)))
1740 CRYPT_BIT_BLOB usage
;
1741 DWORD size
= sizeof(usage
);
1743 if (!CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_BITS
,
1744 ext
->Value
.pbData
, ext
->Value
.cbData
,
1745 CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &usage
, &size
))
1746 canSignCRLs
= FALSE
;
1747 else if (usage
.cbData
> 2)
1749 /* The key usage extension only defines 9 bits => no more
1750 * than 2 bytes are needed to encode all known usages.
1752 canSignCRLs
= FALSE
;
1756 BYTE usageBits
= usage
.pbData
[usage
.cbData
- 1];
1758 canSignCRLs
= usageBits
& CERT_CRL_SIGN_KEY_USAGE
;
1765 /* If the caller was helpful enough to tell us where to find a
1766 * CRL for the cert, look for one and check it.
1768 crl
= CertFindCRLInStore(pRevPara
->hCrlStore
,
1769 cert
->dwCertEncodingType
,
1770 CRL_FIND_ISSUED_BY_SIGNATURE_FLAG
|
1771 CRL_FIND_ISSUED_BY_AKI_FLAG
,
1772 CRL_FIND_ISSUED_BY
, pRevPara
->pIssuerCert
, NULL
);
1776 error
= verify_cert_revocation_with_crl_offline(cert
, crl
,
1777 index
, pTime
, pRevStatus
);
1778 CertFreeCRLContext(crl
);
1782 TRACE("no CRL found\n");
1783 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1784 pRevStatus
->dwIndex
= index
;
1790 WARN("no CERT_REVOCATION_PARA\n");
1791 else if (!pRevPara
->hCrlStore
)
1792 WARN("no dist points/aia extension and no CRL store\n");
1793 else if (!pRevPara
->pIssuerCert
)
1794 WARN("no dist points/aia extension and no issuer\n");
1795 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1796 pRevStatus
->dwIndex
= index
;
1802 typedef struct _CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
{
1804 PCCERT_CONTEXT pIssuerCert
;
1806 HCERTSTORE
*rgCertStore
;
1807 HCERTSTORE hCrlStore
;
1808 LPFILETIME pftTimeToUse
;
1809 } CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
;
1811 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1816 } OLD_CERT_REVOCATION_STATUS
;
1818 /***********************************************************************
1819 * CertDllVerifyRevocation (CRYPTNET.@)
1821 BOOL WINAPI
CertDllVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1822 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1823 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1827 LPFILETIME pTime
= NULL
;
1829 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1830 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1832 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1833 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1835 SetLastError(E_INVALIDARG
);
1840 SetLastError(E_INVALIDARG
);
1843 if (pRevPara
&& pRevPara
->cbSize
>=
1844 sizeof(CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
))
1845 pTime
= pRevPara
->pftTimeToUse
;
1848 GetSystemTimeAsFileTime(&now
);
1851 memset(&pRevStatus
->dwIndex
, 0, pRevStatus
->cbSize
- sizeof(DWORD
));
1852 if (dwRevType
!= CERT_CONTEXT_REVOCATION_TYPE
)
1853 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1856 for (i
= 0; !error
&& i
< cContext
; i
++)
1857 error
= verify_cert_revocation(rgpvContext
[i
], i
, pTime
, dwFlags
,
1858 pRevPara
, pRevStatus
);
1862 SetLastError(error
);
1863 pRevStatus
->dwError
= error
;
1865 TRACE("returning %d (%08x)\n", !error
, error
);