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 WIN32_NO_STATUS
24 //#include "wine/port.h"
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
38 //#include "objbase.h"
41 #include <wine/debug.h>
43 WINE_DEFAULT_DEBUG_CHANNEL(cryptnet
);
45 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
47 static const WCHAR cryptNet
[] = { 'c','r','y','p','t','n','e','t','.',
50 /***********************************************************************
51 * DllRegisterServer (CRYPTNET.@)
53 HRESULT WINAPI
DllRegisterServer(void)
56 CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING
,
57 CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0, cryptNet
);
58 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap",
59 cryptNet
, "LdapProvOpenStore");
60 CryptRegisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
61 CERT_STORE_PROV_LDAP_W
, cryptNet
, "LdapProvOpenStore");
65 /***********************************************************************
66 * DllUnregisterServer (CRYPTNET.@)
68 HRESULT WINAPI
DllUnregisterServer(void)
71 CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING
,
72 CRYPT_OID_VERIFY_REVOCATION_FUNC
, cryptNet
);
73 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
, "Ldap");
74 CryptUnregisterOIDFunction(0, CRYPT_OID_OPEN_STORE_PROV_FUNC
,
75 CERT_STORE_PROV_LDAP_W
);
79 static const char *url_oid_to_str(LPCSTR oid
)
87 #define _x(oid) case LOWORD(oid): return #oid
88 _x(URL_OID_CERTIFICATE_ISSUER
);
89 _x(URL_OID_CERTIFICATE_CRL_DIST_POINT
);
90 _x(URL_OID_CTL_ISSUER
);
91 _x(URL_OID_CTL_NEXT_UPDATE
);
92 _x(URL_OID_CRL_ISSUER
);
93 _x(URL_OID_CERTIFICATE_FRESHEST_CRL
);
94 _x(URL_OID_CRL_FRESHEST_CRL
);
95 _x(URL_OID_CROSS_CERT_DIST_POINT
);
98 snprintf(buf
, sizeof(buf
), "%d", LOWORD(oid
));
106 typedef BOOL (WINAPI
*UrlDllGetObjectUrlFunc
)(LPCSTR
, LPVOID
, DWORD
,
107 PCRYPT_URL_ARRAY
, DWORD
*, PCRYPT_URL_INFO
, DWORD
*, LPVOID
);
109 static BOOL WINAPI
CRYPT_GetUrlFromCertificateIssuer(LPCSTR pszUrlOid
,
110 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
111 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
113 PCCERT_CONTEXT cert
= pvPara
;
117 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
118 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
120 SetLastError(CRYPT_E_NOT_FOUND
);
123 if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
124 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
126 CERT_AUTHORITY_INFO_ACCESS
*aia
;
129 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
130 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
134 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
136 for (i
= 0, cUrl
= 0; i
< aia
->cAccDescr
; i
++)
137 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
138 szOID_PKIX_CA_ISSUERS
))
140 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
143 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
146 bytesNeeded
+= sizeof(LPWSTR
) +
147 (lstrlenW(aia
->rgAccDescr
[i
].AccessLocation
.u
.
148 pwszURL
) + 1) * sizeof(WCHAR
);
152 FIXME("unsupported alt name type %d\n",
153 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
157 SetLastError(E_INVALIDARG
);
161 *pcbUrlArray
= bytesNeeded
;
162 else if (*pcbUrlArray
< bytesNeeded
)
164 SetLastError(ERROR_MORE_DATA
);
165 *pcbUrlArray
= bytesNeeded
;
172 *pcbUrlArray
= bytesNeeded
;
174 pUrlArray
->rgwszUrl
=
175 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
176 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
177 + cUrl
* sizeof(LPWSTR
));
178 for (i
= 0; i
< aia
->cAccDescr
; i
++)
179 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
180 szOID_PKIX_CA_ISSUERS
))
182 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
183 == CERT_ALT_NAME_URL
)
185 if (aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
)
188 aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
);
189 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
191 nextUrl
+= (lstrlenW(nextUrl
) + 1);
200 FIXME("url info: stub\n");
202 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
203 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
205 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
206 SetLastError(ERROR_MORE_DATA
);
211 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
212 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
220 SetLastError(CRYPT_E_NOT_FOUND
);
224 static BOOL
CRYPT_GetUrlFromCRLDistPointsExt(const CRYPT_DATA_BLOB
*value
,
225 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
229 CRL_DIST_POINTS_INFO
*info
;
232 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CRL_DIST_POINTS
,
233 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
236 DWORD i
, cUrl
, bytesNeeded
= sizeof(CRYPT_URL_ARRAY
);
238 for (i
= 0, cUrl
= 0; i
< info
->cDistPoint
; i
++)
239 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
240 == CRL_DIST_POINT_FULL_NAME
)
243 CERT_ALT_NAME_INFO
*name
=
244 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
246 for (j
= 0; j
< name
->cAltEntry
; j
++)
247 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
250 if (name
->rgAltEntry
[j
].u
.pwszURL
)
253 bytesNeeded
+= sizeof(LPWSTR
) +
254 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1)
261 SetLastError(E_INVALIDARG
);
265 *pcbUrlArray
= bytesNeeded
;
266 else if (*pcbUrlArray
< bytesNeeded
)
268 SetLastError(ERROR_MORE_DATA
);
269 *pcbUrlArray
= bytesNeeded
;
276 *pcbUrlArray
= bytesNeeded
;
278 pUrlArray
->rgwszUrl
=
279 (LPWSTR
*)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
));
280 nextUrl
= (LPWSTR
)((BYTE
*)pUrlArray
+ sizeof(CRYPT_URL_ARRAY
)
281 + cUrl
* sizeof(LPWSTR
));
282 for (i
= 0; i
< info
->cDistPoint
; i
++)
283 if (info
->rgDistPoint
[i
].DistPointName
.dwDistPointNameChoice
284 == CRL_DIST_POINT_FULL_NAME
)
287 CERT_ALT_NAME_INFO
*name
=
288 &info
->rgDistPoint
[i
].DistPointName
.u
.FullName
;
290 for (j
= 0; j
< name
->cAltEntry
; j
++)
291 if (name
->rgAltEntry
[j
].dwAltNameChoice
==
294 if (name
->rgAltEntry
[j
].u
.pwszURL
)
297 name
->rgAltEntry
[j
].u
.pwszURL
);
298 pUrlArray
->rgwszUrl
[pUrlArray
->cUrl
++] =
301 (lstrlenW(name
->rgAltEntry
[j
].u
.pwszURL
) + 1);
310 FIXME("url info: stub\n");
312 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
313 else if (*pcbUrlInfo
< sizeof(CRYPT_URL_INFO
))
315 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
316 SetLastError(ERROR_MORE_DATA
);
321 *pcbUrlInfo
= sizeof(CRYPT_URL_INFO
);
322 memset(pUrlInfo
, 0, sizeof(CRYPT_URL_INFO
));
331 static BOOL WINAPI
CRYPT_GetUrlFromCertificateCRLDistPoint(LPCSTR pszUrlOid
,
332 LPVOID pvPara
, DWORD dwFlags
, PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
,
333 PCRYPT_URL_INFO pUrlInfo
, DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
335 PCCERT_CONTEXT cert
= pvPara
;
339 /* The only applicable flag is CRYPT_GET_URL_FROM_EXTENSION */
340 if (dwFlags
&& !(dwFlags
& CRYPT_GET_URL_FROM_EXTENSION
))
342 SetLastError(CRYPT_E_NOT_FOUND
);
345 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
346 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
347 ret
= CRYPT_GetUrlFromCRLDistPointsExt(&ext
->Value
, pUrlArray
,
348 pcbUrlArray
, pUrlInfo
, pcbUrlInfo
);
350 SetLastError(CRYPT_E_NOT_FOUND
);
354 /***********************************************************************
355 * CryptGetObjectUrl (CRYPTNET.@)
357 BOOL WINAPI
CryptGetObjectUrl(LPCSTR pszUrlOid
, LPVOID pvPara
, DWORD dwFlags
,
358 PCRYPT_URL_ARRAY pUrlArray
, DWORD
*pcbUrlArray
, PCRYPT_URL_INFO pUrlInfo
,
359 DWORD
*pcbUrlInfo
, LPVOID pvReserved
)
361 UrlDllGetObjectUrlFunc func
= NULL
;
362 HCRYPTOIDFUNCADDR hFunc
= NULL
;
365 TRACE("(%s, %p, %08x, %p, %p, %p, %p, %p)\n", debugstr_a(pszUrlOid
),
366 pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
, pUrlInfo
, pcbUrlInfo
, pvReserved
);
368 if (IS_INTOID(pszUrlOid
))
370 switch (LOWORD(pszUrlOid
))
372 case LOWORD(URL_OID_CERTIFICATE_ISSUER
):
373 func
= CRYPT_GetUrlFromCertificateIssuer
;
375 case LOWORD(URL_OID_CERTIFICATE_CRL_DIST_POINT
):
376 func
= CRYPT_GetUrlFromCertificateCRLDistPoint
;
379 FIXME("unimplemented for %s\n", url_oid_to_str(pszUrlOid
));
380 SetLastError(ERROR_FILE_NOT_FOUND
);
385 static HCRYPTOIDFUNCSET set
= NULL
;
388 set
= CryptInitOIDFunctionSet(URL_OID_GET_OBJECT_URL_FUNC
, 0);
389 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszUrlOid
, 0,
390 (void **)&func
, &hFunc
);
393 ret
= func(pszUrlOid
, pvPara
, dwFlags
, pUrlArray
, pcbUrlArray
,
394 pUrlInfo
, pcbUrlInfo
, pvReserved
);
396 CryptFreeOIDFunctionAddress(hFunc
, 0);
400 /***********************************************************************
401 * CryptRetrieveObjectByUrlA (CRYPTNET.@)
403 BOOL WINAPI
CryptRetrieveObjectByUrlA(LPCSTR pszURL
, LPCSTR pszObjectOid
,
404 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
405 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
406 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
411 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_a(pszURL
),
412 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
413 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
417 SetLastError(ERROR_INVALID_PARAMETER
);
420 len
= MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, NULL
, 0);
423 LPWSTR url
= CryptMemAlloc(len
* sizeof(WCHAR
));
427 MultiByteToWideChar(CP_ACP
, 0, pszURL
, -1, url
, len
);
428 ret
= CryptRetrieveObjectByUrlW(url
, pszObjectOid
,
429 dwRetrievalFlags
, dwTimeout
, ppvObject
, hAsyncRetrieve
,
430 pCredentials
, pvVerify
, pAuxInfo
);
434 SetLastError(ERROR_OUTOFMEMORY
);
439 static void WINAPI
CRYPT_FreeBlob(LPCSTR pszObjectOid
,
440 PCRYPT_BLOB_ARRAY pObject
, void *pvFreeContext
)
444 for (i
= 0; i
< pObject
->cBlob
; i
++)
445 CryptMemFree(pObject
->rgBlob
[i
].pbData
);
446 CryptMemFree(pObject
->rgBlob
);
449 static BOOL
CRYPT_GetObjectFromFile(HANDLE hFile
, PCRYPT_BLOB_ARRAY pObject
)
454 if ((ret
= GetFileSizeEx(hFile
, &size
)))
458 WARN("file too big\n");
459 SetLastError(ERROR_INVALID_DATA
);
464 CRYPT_DATA_BLOB blob
;
466 blob
.pbData
= CryptMemAlloc(size
.u
.LowPart
);
469 ret
= ReadFile(hFile
, blob
.pbData
, size
.u
.LowPart
, &blob
.cbData
,
473 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
477 memcpy(pObject
->rgBlob
, &blob
, sizeof(CRYPT_DATA_BLOB
));
481 SetLastError(ERROR_OUTOFMEMORY
);
486 CryptMemFree(blob
.pbData
);
490 SetLastError(ERROR_OUTOFMEMORY
);
498 static BOOL
CRYPT_GetObjectFromCache(LPCWSTR pszURL
, PCRYPT_BLOB_ARRAY pObject
,
499 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
502 INTERNET_CACHE_ENTRY_INFOW
*pCacheInfo
= NULL
;
505 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pObject
, pAuxInfo
);
507 RetrieveUrlCacheEntryFileW(pszURL
, NULL
, &size
, 0);
508 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
511 pCacheInfo
= CryptMemAlloc(size
);
514 SetLastError(ERROR_OUTOFMEMORY
);
518 if ((ret
= RetrieveUrlCacheEntryFileW(pszURL
, pCacheInfo
, &size
, 0)))
522 GetSystemTimeAsFileTime(&ft
);
523 if (CompareFileTime(&pCacheInfo
->ExpireTime
, &ft
) >= 0)
525 HANDLE hFile
= CreateFileW(pCacheInfo
->lpszLocalFileName
, GENERIC_READ
,
526 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
528 if (hFile
!= INVALID_HANDLE_VALUE
)
530 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
532 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
533 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
534 pLastSyncTime
) + sizeof(PFILETIME
) &&
535 pAuxInfo
->pLastSyncTime
)
536 memcpy(pAuxInfo
->pLastSyncTime
,
537 &pCacheInfo
->LastSyncTime
,
544 DeleteUrlCacheEntryW(pszURL
);
550 DeleteUrlCacheEntryW(pszURL
);
553 UnlockUrlCacheEntryFileW(pszURL
, 0);
555 CryptMemFree(pCacheInfo
);
556 TRACE("returning %d\n", ret
);
560 /* Parses the URL, and sets components' lpszHostName and lpszUrlPath members
561 * to NULL-terminated copies of those portions of the URL (to be freed with
564 static BOOL
CRYPT_CrackUrl(LPCWSTR pszURL
, URL_COMPONENTSW
*components
)
568 TRACE("(%s, %p)\n", debugstr_w(pszURL
), components
);
570 memset(components
, 0, sizeof(*components
));
571 components
->dwStructSize
= sizeof(*components
);
572 components
->lpszHostName
= CryptMemAlloc(INTERNET_MAX_HOST_NAME_LENGTH
* sizeof(WCHAR
));
573 components
->dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
574 if (!components
->lpszHostName
)
576 SetLastError(ERROR_OUTOFMEMORY
);
579 components
->lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
580 components
->dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
581 if (!components
->lpszUrlPath
)
583 CryptMemFree(components
->lpszHostName
);
584 SetLastError(ERROR_OUTOFMEMORY
);
588 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, components
);
591 switch (components
->nScheme
)
593 case INTERNET_SCHEME_FTP
:
594 if (!components
->nPort
)
595 components
->nPort
= INTERNET_DEFAULT_FTP_PORT
;
597 case INTERNET_SCHEME_HTTP
:
598 if (!components
->nPort
)
599 components
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
605 TRACE("returning %d\n", ret
);
616 static struct InetContext
*CRYPT_MakeInetContext(DWORD dwTimeout
)
618 struct InetContext
*context
= CryptMemAlloc(sizeof(struct InetContext
));
622 context
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
625 CryptMemFree(context
);
630 context
->timeout
= dwTimeout
;
631 context
->error
= ERROR_SUCCESS
;
637 static BOOL
CRYPT_DownloadObject(DWORD dwRetrievalFlags
, HINTERNET hHttp
,
638 struct InetContext
*context
, PCRYPT_BLOB_ARRAY pObject
,
639 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
641 CRYPT_DATA_BLOB object
= { 0, NULL
};
642 DWORD bytesAvailable
;
646 if ((ret
= InternetQueryDataAvailable(hHttp
, &bytesAvailable
, 0, 0)))
651 object
.pbData
= CryptMemRealloc(object
.pbData
,
652 object
.cbData
+ bytesAvailable
);
654 object
.pbData
= CryptMemAlloc(bytesAvailable
);
657 INTERNET_BUFFERSA buffer
= { sizeof(buffer
), 0 };
659 buffer
.dwBufferLength
= bytesAvailable
;
660 buffer
.lpvBuffer
= object
.pbData
+ object
.cbData
;
661 if (!(ret
= InternetReadFileExA(hHttp
, &buffer
, IRF_NO_WAIT
,
662 (DWORD_PTR
)context
)))
664 if (GetLastError() == ERROR_IO_PENDING
)
666 if (WaitForSingleObject(context
->event
,
667 context
->timeout
) == WAIT_TIMEOUT
)
668 SetLastError(ERROR_TIMEOUT
);
669 else if (context
->error
)
670 SetLastError(context
->error
);
676 object
.cbData
+= buffer
.dwBufferLength
;
680 SetLastError(ERROR_OUTOFMEMORY
);
685 else if (GetLastError() == ERROR_IO_PENDING
)
687 if (WaitForSingleObject(context
->event
, context
->timeout
) ==
689 SetLastError(ERROR_TIMEOUT
);
693 } while (ret
&& bytesAvailable
);
696 pObject
->rgBlob
= CryptMemAlloc(sizeof(CRYPT_DATA_BLOB
));
697 if (!pObject
->rgBlob
)
699 CryptMemFree(object
.pbData
);
700 SetLastError(ERROR_OUTOFMEMORY
);
705 pObject
->rgBlob
[0].cbData
= object
.cbData
;
706 pObject
->rgBlob
[0].pbData
= object
.pbData
;
710 TRACE("returning %d\n", ret
);
714 /* Finds the object specified by pszURL in the cache. If it's not found,
715 * creates a new cache entry for the object and writes the object to it.
716 * Sets the expiration time of the cache entry to expires.
718 static void CRYPT_CacheURL(LPCWSTR pszURL
, const CRYPT_BLOB_ARRAY
*pObject
,
719 DWORD dwRetrievalFlags
, FILETIME expires
)
721 WCHAR cacheFileName
[MAX_PATH
];
723 DWORD size
= 0, entryType
;
726 GetUrlCacheEntryInfoW(pszURL
, NULL
, &size
);
727 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
729 INTERNET_CACHE_ENTRY_INFOW
*info
= CryptMemAlloc(size
);
733 ERR("out of memory\n");
737 if (GetUrlCacheEntryInfoW(pszURL
, info
, &size
))
739 lstrcpyW(cacheFileName
, info
->lpszLocalFileName
);
740 /* Check if the existing cache entry is up to date. If it isn't,
741 * remove the existing cache entry, and create a new one with the
744 GetSystemTimeAsFileTime(&ft
);
745 if (CompareFileTime(&info
->ExpireTime
, &ft
) < 0)
747 DeleteUrlCacheEntryW(pszURL
);
751 info
->ExpireTime
= expires
;
752 SetUrlCacheEntryInfoW(pszURL
, info
, CACHE_ENTRY_EXPTIME_FC
);
760 if (!CreateUrlCacheEntryW(pszURL
, pObject
->rgBlob
[0].cbData
, NULL
, cacheFileName
, 0))
763 hCacheFile
= CreateFileW(cacheFileName
, GENERIC_WRITE
, 0,
764 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
765 if(hCacheFile
== INVALID_HANDLE_VALUE
)
768 WriteFile(hCacheFile
, pObject
->rgBlob
[0].pbData
,
769 pObject
->rgBlob
[0].cbData
, &size
, NULL
);
770 CloseHandle(hCacheFile
);
772 if (!(dwRetrievalFlags
& CRYPT_STICKY_CACHE_RETRIEVAL
))
773 entryType
= NORMAL_CACHE_ENTRY
;
775 entryType
= STICKY_CACHE_ENTRY
;
776 memset(&ft
, 0, sizeof(ft
));
777 CommitUrlCacheEntryW(pszURL
, cacheFileName
, expires
, ft
, entryType
,
778 NULL
, 0, NULL
, NULL
);
781 static void CALLBACK
CRYPT_InetStatusCallback(HINTERNET hInt
,
782 DWORD_PTR dwContext
, DWORD status
, void *statusInfo
, DWORD statusInfoLen
)
784 struct InetContext
*context
= (struct InetContext
*)dwContext
;
785 LPINTERNET_ASYNC_RESULT result
;
789 case INTERNET_STATUS_REQUEST_COMPLETE
:
791 context
->error
= result
->dwError
;
792 SetEvent(context
->event
);
796 static BOOL
CRYPT_Connect(const URL_COMPONENTSW
*components
,
797 struct InetContext
*context
, PCRYPT_CREDENTIALS pCredentials
,
798 HINTERNET
*phInt
, HINTERNET
*phHost
)
802 TRACE("(%s:%d, %p, %p, %p, %p)\n", debugstr_w(components
->lpszHostName
),
803 components
->nPort
, context
, pCredentials
, phInt
, phInt
);
806 *phInt
= InternetOpenW(NULL
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
,
807 context
? INTERNET_FLAG_ASYNC
: 0);
813 InternetSetStatusCallbackW(*phInt
, CRYPT_InetStatusCallback
);
814 switch (components
->nScheme
)
816 case INTERNET_SCHEME_FTP
:
817 service
= INTERNET_SERVICE_FTP
;
819 case INTERNET_SCHEME_HTTP
:
820 service
= INTERNET_SERVICE_HTTP
;
825 /* FIXME: use pCredentials for username/password */
826 *phHost
= InternetConnectW(*phInt
, components
->lpszHostName
,
827 components
->nPort
, NULL
, NULL
, service
, 0, (DWORD_PTR
)context
);
830 InternetCloseHandle(*phInt
);
839 TRACE("returning %d\n", ret
);
843 static BOOL WINAPI
FTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
844 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
845 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
846 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
847 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
849 FIXME("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
850 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
851 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
854 pObject
->rgBlob
= NULL
;
855 *ppfnFreeObject
= CRYPT_FreeBlob
;
856 *ppvFreeContext
= NULL
;
860 static const WCHAR x509cacert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
861 '/','x','-','x','5','0','9','-','c','a','-','c','e','r','t',0 };
862 static const WCHAR x509emailcert
[] = { 'a','p','p','l','i','c','a','t','i','o',
863 'n','/','x','-','x','5','0','9','-','e','m','a','i','l','-','c','e','r','t',
865 static const WCHAR x509servercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
866 'n','/','x','-','x','5','0','9','-','s','e','r','v','e','r','-','c','e','r',
868 static const WCHAR x509usercert
[] = { 'a','p','p','l','i','c','a','t','i','o',
869 'n','/','x','-','x','5','0','9','-','u','s','e','r','-','c','e','r','t',0 };
870 static const WCHAR pkcs7cert
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
871 '/','x','-','p','k','c','s','7','-','c','e','r','t','i','f','c','a','t','e',
873 static const WCHAR pkixCRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
874 '/','p','k','i','x','-','c','r','l',0 };
875 static const WCHAR pkcs7CRL
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
876 '/','x','-','p','k','c','s','7','-','c','r','l',0 };
877 static const WCHAR pkcs7sig
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
878 '/','x','-','p','k','c','s','7','-','s','i','g','n','a','t','u','r','e',0 };
879 static const WCHAR pkcs7mime
[] = { 'a','p','p','l','i','c','a','t','i','o','n',
880 '/','x','-','p','k','c','s','7','-','m','i','m','e',0 };
882 static BOOL WINAPI
HTTP_RetrieveEncodedObjectW(LPCWSTR pszURL
,
883 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
884 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
885 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
886 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
890 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
891 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
892 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
895 pObject
->rgBlob
= NULL
;
896 *ppfnFreeObject
= CRYPT_FreeBlob
;
897 *ppvFreeContext
= NULL
;
899 if (!(dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
))
900 ret
= CRYPT_GetObjectFromCache(pszURL
, pObject
, pAuxInfo
);
901 if (!ret
&& (!(dwRetrievalFlags
& CRYPT_CACHE_ONLY_RETRIEVAL
) ||
902 (dwRetrievalFlags
& CRYPT_WIRE_ONLY_RETRIEVAL
)))
904 URL_COMPONENTSW components
;
906 if ((ret
= CRYPT_CrackUrl(pszURL
, &components
)))
908 HINTERNET hInt
, hHost
;
909 struct InetContext
*context
= NULL
;
912 context
= CRYPT_MakeInetContext(dwTimeout
);
913 ret
= CRYPT_Connect(&components
, context
, pCredentials
, &hInt
,
917 static LPCWSTR types
[] = { x509cacert
, x509emailcert
,
918 x509servercert
, x509usercert
, pkcs7cert
, pkixCRL
, pkcs7CRL
,
919 pkcs7sig
, pkcs7mime
, NULL
};
920 HINTERNET hHttp
= HttpOpenRequestW(hHost
, NULL
,
921 components
.lpszUrlPath
, NULL
, NULL
, types
,
922 INTERNET_FLAG_NO_COOKIES
| INTERNET_FLAG_NO_UI
,
929 InternetSetOptionW(hHttp
,
930 INTERNET_OPTION_RECEIVE_TIMEOUT
, &dwTimeout
,
932 InternetSetOptionW(hHttp
, INTERNET_OPTION_SEND_TIMEOUT
,
933 &dwTimeout
, sizeof(dwTimeout
));
935 ret
= HttpSendRequestExW(hHttp
, NULL
, NULL
, 0,
937 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
939 if (WaitForSingleObject(context
->event
,
940 context
->timeout
) == WAIT_TIMEOUT
)
941 SetLastError(ERROR_TIMEOUT
);
946 !(ret
= HttpEndRequestW(hHttp
, NULL
, 0, (DWORD_PTR
)context
)) &&
947 GetLastError() == ERROR_IO_PENDING
)
949 if (WaitForSingleObject(context
->event
,
950 context
->timeout
) == WAIT_TIMEOUT
)
951 SetLastError(ERROR_TIMEOUT
);
956 ret
= CRYPT_DownloadObject(dwRetrievalFlags
, hHttp
,
957 context
, pObject
, pAuxInfo
);
958 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
))
962 DWORD len
= sizeof(st
);
964 if (HttpQueryInfoW(hHttp
, HTTP_QUERY_EXPIRES
| HTTP_QUERY_FLAG_SYSTEMTIME
,
965 &st
, &len
, NULL
) && SystemTimeToFileTime(&st
, &ft
))
966 CRYPT_CacheURL(pszURL
, pObject
, dwRetrievalFlags
, ft
);
968 InternetCloseHandle(hHttp
);
970 InternetCloseHandle(hHost
);
971 InternetCloseHandle(hInt
);
975 CloseHandle(context
->event
);
976 CryptMemFree(context
);
978 CryptMemFree(components
.lpszUrlPath
);
979 CryptMemFree(components
.lpszHostName
);
982 TRACE("returning %d\n", ret
);
986 static BOOL WINAPI
File_RetrieveEncodedObjectW(LPCWSTR pszURL
,
987 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
988 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
989 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
990 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
992 URL_COMPONENTSW components
= { sizeof(components
), 0 };
995 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
996 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, pObject
,
997 ppfnFreeObject
, ppvFreeContext
, hAsyncRetrieve
, pCredentials
, pAuxInfo
);
1000 pObject
->rgBlob
= NULL
;
1001 *ppfnFreeObject
= CRYPT_FreeBlob
;
1002 *ppvFreeContext
= NULL
;
1004 components
.lpszUrlPath
= CryptMemAlloc(INTERNET_MAX_PATH_LENGTH
* sizeof(WCHAR
));
1005 components
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
1006 if (!components
.lpszUrlPath
)
1008 SetLastError(ERROR_OUTOFMEMORY
);
1012 ret
= InternetCrackUrlW(pszURL
, 0, ICU_DECODE
, &components
);
1017 /* 3 == lstrlenW(L"c:") + 1 */
1018 path
= CryptMemAlloc((components
.dwUrlPathLength
+ 3) * sizeof(WCHAR
));
1023 /* Try to create the file directly - Wine handles / in pathnames */
1024 lstrcpynW(path
, components
.lpszUrlPath
,
1025 components
.dwUrlPathLength
+ 1);
1026 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1027 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1029 if ((hFile
== INVALID_HANDLE_VALUE
) && (lstrlenW(components
.lpszUrlPath
) > 1) && (components
.lpszUrlPath
[1] != ':'))
1031 if ((hFile
== INVALID_HANDLE_VALUE
)
1034 /* Try again on the current drive */
1035 GetCurrentDirectoryW(components
.dwUrlPathLength
, path
);
1038 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1039 components
.dwUrlPathLength
+ 1);
1040 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1041 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1043 if (hFile
== INVALID_HANDLE_VALUE
)
1045 /* Try again on the Windows drive */
1046 GetWindowsDirectoryW(path
, components
.dwUrlPathLength
);
1049 lstrcpynW(path
+ 2, components
.lpszUrlPath
,
1050 components
.dwUrlPathLength
+ 1);
1051 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
,
1052 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1056 if (hFile
!= INVALID_HANDLE_VALUE
)
1058 if ((ret
= CRYPT_GetObjectFromFile(hFile
, pObject
)))
1060 if (pAuxInfo
&& pAuxInfo
->cbSize
>=
1061 offsetof(CRYPT_RETRIEVE_AUX_INFO
,
1062 pLastSyncTime
) + sizeof(PFILETIME
) &&
1063 pAuxInfo
->pLastSyncTime
)
1064 GetFileTime(hFile
, NULL
, NULL
,
1065 pAuxInfo
->pLastSyncTime
);
1075 SetLastError(ERROR_OUTOFMEMORY
);
1079 CryptMemFree(components
.lpszUrlPath
);
1083 typedef BOOL (WINAPI
*SchemeDllRetrieveEncodedObjectW
)(LPCWSTR pwszUrl
,
1084 LPCSTR pszObjectOid
, DWORD dwRetrievalFlags
, DWORD dwTimeout
,
1085 PCRYPT_BLOB_ARRAY pObject
, PFN_FREE_ENCODED_OBJECT_FUNC
*ppfnFreeObject
,
1086 void **ppvFreeContext
, HCRYPTASYNC hAsyncRetrieve
,
1087 PCRYPT_CREDENTIALS pCredentials
, PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
);
1089 static BOOL
CRYPT_GetRetrieveFunction(LPCWSTR pszURL
,
1090 SchemeDllRetrieveEncodedObjectW
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1092 URL_COMPONENTSW components
= { sizeof(components
), 0 };
1095 TRACE("(%s, %p, %p)\n", debugstr_w(pszURL
), pFunc
, phFunc
);
1099 components
.dwSchemeLength
= 1;
1100 ret
= InternetCrackUrlW(pszURL
, 0, 0, &components
);
1103 /* Microsoft always uses CryptInitOIDFunctionSet/
1104 * CryptGetOIDFunctionAddress, but there doesn't seem to be a pressing
1105 * reason to do so for builtin schemes.
1107 switch (components
.nScheme
)
1109 case INTERNET_SCHEME_FTP
:
1110 *pFunc
= FTP_RetrieveEncodedObjectW
;
1112 case INTERNET_SCHEME_HTTP
:
1113 *pFunc
= HTTP_RetrieveEncodedObjectW
;
1115 case INTERNET_SCHEME_FILE
:
1116 *pFunc
= File_RetrieveEncodedObjectW
;
1120 int len
= WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1121 components
.dwSchemeLength
, NULL
, 0, NULL
, NULL
);
1125 LPSTR scheme
= CryptMemAlloc(len
);
1129 static HCRYPTOIDFUNCSET set
= NULL
;
1132 set
= CryptInitOIDFunctionSet(
1133 SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC
, 0);
1134 WideCharToMultiByte(CP_ACP
, 0, components
.lpszScheme
,
1135 components
.dwSchemeLength
, scheme
, len
, NULL
, NULL
);
1136 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
,
1137 scheme
, 0, (void **)pFunc
, phFunc
);
1138 CryptMemFree(scheme
);
1142 SetLastError(ERROR_OUTOFMEMORY
);
1151 TRACE("returning %d\n", ret
);
1155 static BOOL WINAPI
CRYPT_CreateBlob(LPCSTR pszObjectOid
,
1156 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1159 CRYPT_BLOB_ARRAY
*context
;
1162 size
= sizeof(CRYPT_BLOB_ARRAY
) + pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1163 for (i
= 0; i
< pObject
->cBlob
; i
++)
1164 size
+= pObject
->rgBlob
[i
].cbData
;
1165 context
= CryptMemAlloc(size
);
1172 (CRYPT_DATA_BLOB
*)((LPBYTE
)context
+ sizeof(CRYPT_BLOB_ARRAY
));
1174 (LPBYTE
)context
->rgBlob
+ pObject
->cBlob
* sizeof(CRYPT_DATA_BLOB
);
1175 for (i
= 0; i
< pObject
->cBlob
; i
++)
1177 memcpy(nextData
, pObject
->rgBlob
[i
].pbData
,
1178 pObject
->rgBlob
[i
].cbData
);
1179 context
->rgBlob
[i
].pbData
= nextData
;
1180 context
->rgBlob
[i
].cbData
= pObject
->rgBlob
[i
].cbData
;
1181 nextData
+= pObject
->rgBlob
[i
].cbData
;
1184 *ppvContext
= context
;
1190 typedef BOOL (WINAPI
*AddContextToStore
)(HCERTSTORE hCertStore
,
1191 const void *pContext
, DWORD dwAddDisposition
, const void **ppStoreContext
);
1193 static BOOL
decode_base64_blob( const CRYPT_DATA_BLOB
*in
, CRYPT_DATA_BLOB
*out
)
1196 DWORD len
= in
->cbData
;
1198 while (len
&& !in
->pbData
[len
- 1]) len
--;
1199 if (!CryptStringToBinaryA( (char *)in
->pbData
, len
, CRYPT_STRING_BASE64_ANY
,
1200 NULL
, &out
->cbData
, NULL
, NULL
)) return FALSE
;
1202 if (!(out
->pbData
= CryptMemAlloc( out
->cbData
))) return FALSE
;
1203 ret
= CryptStringToBinaryA( (char *)in
->pbData
, len
, CRYPT_STRING_BASE64_ANY
,
1204 out
->pbData
, &out
->cbData
, NULL
, NULL
);
1205 if (!ret
) CryptMemFree( out
->pbData
);
1209 static BOOL
CRYPT_CreateContext(const CRYPT_BLOB_ARRAY
*pObject
,
1210 DWORD dwExpectedContentTypeFlags
, AddContextToStore addFunc
, void **ppvContext
)
1213 CRYPT_DATA_BLOB blob
;
1215 if (!pObject
->cBlob
)
1217 SetLastError(ERROR_INVALID_DATA
);
1221 else if (pObject
->cBlob
== 1)
1223 if (decode_base64_blob(&pObject
->rgBlob
[0], &blob
))
1225 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
1226 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
, 0,
1227 NULL
, NULL
, NULL
, NULL
, NULL
, (const void **)ppvContext
);
1228 CryptMemFree(blob
.pbData
);
1232 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1233 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
, 0,
1234 NULL
, NULL
, NULL
, NULL
, NULL
, (const void **)ppvContext
);
1238 SetLastError(CRYPT_E_NO_MATCH
);
1244 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1245 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1250 const void *context
;
1252 for (i
= 0; i
< pObject
->cBlob
; i
++)
1254 if (decode_base64_blob(&pObject
->rgBlob
[i
], &blob
))
1256 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
1257 dwExpectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_BINARY
,
1258 0, NULL
, NULL
, NULL
, NULL
, NULL
, &context
);
1259 CryptMemFree(blob
.pbData
);
1263 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1264 &pObject
->rgBlob
[i
], dwExpectedContentTypeFlags
,
1265 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
, NULL
,
1270 if (!addFunc(store
, context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1275 SetLastError(CRYPT_E_NO_MATCH
);
1282 *ppvContext
= store
;
1287 static BOOL WINAPI
CRYPT_CreateCert(LPCSTR pszObjectOid
,
1288 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1290 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CERT
,
1291 (AddContextToStore
)CertAddCertificateContextToStore
, ppvContext
);
1294 static BOOL WINAPI
CRYPT_CreateCRL(LPCSTR pszObjectOid
,
1295 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1297 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CRL
,
1298 (AddContextToStore
)CertAddCRLContextToStore
, ppvContext
);
1301 static BOOL WINAPI
CRYPT_CreateCTL(LPCSTR pszObjectOid
,
1302 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1304 return CRYPT_CreateContext(pObject
, CERT_QUERY_CONTENT_FLAG_CTL
,
1305 (AddContextToStore
)CertAddCTLContextToStore
, ppvContext
);
1308 static BOOL WINAPI
CRYPT_CreatePKCS7(LPCSTR pszObjectOid
,
1309 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1313 if (!pObject
->cBlob
)
1315 SetLastError(ERROR_INVALID_DATA
);
1319 else if (pObject
->cBlob
== 1)
1320 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &pObject
->rgBlob
[0],
1321 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1322 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
, CERT_QUERY_FORMAT_FLAG_BINARY
,
1323 0, NULL
, NULL
, NULL
, ppvContext
, NULL
, NULL
);
1326 FIXME("multiple messages unimplemented\n");
1332 static BOOL WINAPI
CRYPT_CreateAny(LPCSTR pszObjectOid
,
1333 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
)
1337 if (!pObject
->cBlob
)
1339 SetLastError(ERROR_INVALID_DATA
);
1345 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
1346 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1350 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1351 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1355 CertAddStoreToCollection(store
, memStore
,
1356 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1357 CertCloseStore(memStore
, 0);
1361 CertCloseStore(store
, 0);
1370 for (i
= 0; i
< pObject
->cBlob
; i
++)
1372 DWORD contentType
, expectedContentTypes
=
1373 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
1374 CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED
|
1375 CERT_QUERY_CONTENT_FLAG_CERT
|
1376 CERT_QUERY_CONTENT_FLAG_CRL
|
1377 CERT_QUERY_CONTENT_FLAG_CTL
;
1378 HCERTSTORE contextStore
;
1379 const void *context
;
1381 if (CryptQueryObject(CERT_QUERY_OBJECT_BLOB
,
1382 &pObject
->rgBlob
[i
], expectedContentTypes
,
1383 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, &contentType
, NULL
,
1384 &contextStore
, NULL
, &context
))
1386 switch (contentType
)
1388 case CERT_QUERY_CONTENT_CERT
:
1389 if (!CertAddCertificateContextToStore(store
,
1390 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1392 CertFreeCertificateContext(context
);
1394 case CERT_QUERY_CONTENT_CRL
:
1395 if (!CertAddCRLContextToStore(store
,
1396 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1398 CertFreeCRLContext(context
);
1400 case CERT_QUERY_CONTENT_CTL
:
1401 if (!CertAddCTLContextToStore(store
,
1402 context
, CERT_STORE_ADD_ALWAYS
, NULL
))
1404 CertFreeCTLContext(context
);
1407 CertAddStoreToCollection(store
, contextStore
, 0, 0);
1409 CertCloseStore(contextStore
, 0);
1417 *ppvContext
= store
;
1422 typedef BOOL (WINAPI
*ContextDllCreateObjectContext
)(LPCSTR pszObjectOid
,
1423 DWORD dwRetrievalFlags
, const CRYPT_BLOB_ARRAY
*pObject
, void **ppvContext
);
1425 static BOOL
CRYPT_GetCreateFunction(LPCSTR pszObjectOid
,
1426 ContextDllCreateObjectContext
*pFunc
, HCRYPTOIDFUNCADDR
*phFunc
)
1430 TRACE("(%s, %p, %p)\n", debugstr_a(pszObjectOid
), pFunc
, phFunc
);
1434 if (IS_INTOID(pszObjectOid
))
1436 switch (LOWORD(pszObjectOid
))
1439 *pFunc
= CRYPT_CreateBlob
;
1441 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1442 *pFunc
= CRYPT_CreateCert
;
1444 case LOWORD(CONTEXT_OID_CRL
):
1445 *pFunc
= CRYPT_CreateCRL
;
1447 case LOWORD(CONTEXT_OID_CTL
):
1448 *pFunc
= CRYPT_CreateCTL
;
1450 case LOWORD(CONTEXT_OID_PKCS7
):
1451 *pFunc
= CRYPT_CreatePKCS7
;
1453 case LOWORD(CONTEXT_OID_CAPI2_ANY
):
1454 *pFunc
= CRYPT_CreateAny
;
1460 static HCRYPTOIDFUNCSET set
= NULL
;
1463 set
= CryptInitOIDFunctionSet(
1464 CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC
, 0);
1465 ret
= CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, pszObjectOid
,
1466 0, (void **)pFunc
, phFunc
);
1468 TRACE("returning %d\n", ret
);
1472 static BOOL
CRYPT_GetExpiration(const void *object
, const char *pszObjectOid
, FILETIME
*expiration
)
1474 if (!IS_INTOID(pszObjectOid
))
1477 switch (LOWORD(pszObjectOid
)) {
1478 case LOWORD(CONTEXT_OID_CERTIFICATE
):
1479 *expiration
= ((const CERT_CONTEXT
*)object
)->pCertInfo
->NotAfter
;
1481 case LOWORD(CONTEXT_OID_CRL
):
1482 *expiration
= ((const CRL_CONTEXT
*)object
)->pCrlInfo
->NextUpdate
;
1484 case LOWORD(CONTEXT_OID_CTL
):
1485 *expiration
= ((const CTL_CONTEXT
*)object
)->pCtlInfo
->NextUpdate
;
1492 /***********************************************************************
1493 * CryptRetrieveObjectByUrlW (CRYPTNET.@)
1495 BOOL WINAPI
CryptRetrieveObjectByUrlW(LPCWSTR pszURL
, LPCSTR pszObjectOid
,
1496 DWORD dwRetrievalFlags
, DWORD dwTimeout
, LPVOID
*ppvObject
,
1497 HCRYPTASYNC hAsyncRetrieve
, PCRYPT_CREDENTIALS pCredentials
, LPVOID pvVerify
,
1498 PCRYPT_RETRIEVE_AUX_INFO pAuxInfo
)
1501 SchemeDllRetrieveEncodedObjectW retrieve
;
1502 ContextDllCreateObjectContext create
;
1503 HCRYPTOIDFUNCADDR hRetrieve
= 0, hCreate
= 0;
1505 TRACE("(%s, %s, %08x, %d, %p, %p, %p, %p, %p)\n", debugstr_w(pszURL
),
1506 debugstr_a(pszObjectOid
), dwRetrievalFlags
, dwTimeout
, ppvObject
,
1507 hAsyncRetrieve
, pCredentials
, pvVerify
, pAuxInfo
);
1511 SetLastError(ERROR_INVALID_PARAMETER
);
1514 ret
= CRYPT_GetRetrieveFunction(pszURL
, &retrieve
, &hRetrieve
);
1516 ret
= CRYPT_GetCreateFunction(pszObjectOid
, &create
, &hCreate
);
1519 CRYPT_BLOB_ARRAY object
= { 0, NULL
};
1520 PFN_FREE_ENCODED_OBJECT_FUNC freeObject
;
1524 ret
= retrieve(pszURL
, pszObjectOid
, dwRetrievalFlags
, dwTimeout
,
1525 &object
, &freeObject
, &freeContext
, hAsyncRetrieve
, pCredentials
,
1529 ret
= create(pszObjectOid
, dwRetrievalFlags
, &object
, ppvObject
);
1530 if (ret
&& !(dwRetrievalFlags
& CRYPT_DONT_CACHE_RESULT
) &&
1531 CRYPT_GetExpiration(*ppvObject
, pszObjectOid
, &expires
))
1533 CRYPT_CacheURL(pszURL
, &object
, dwRetrievalFlags
, expires
);
1535 freeObject(pszObjectOid
, &object
, freeContext
);
1539 CryptFreeOIDFunctionAddress(hCreate
, 0);
1541 CryptFreeOIDFunctionAddress(hRetrieve
, 0);
1542 TRACE("returning %d\n", ret
);
1546 static DWORD
verify_cert_revocation_with_crl_online(PCCERT_CONTEXT cert
,
1547 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1548 PCERT_REVOCATION_STATUS pRevStatus
)
1551 PCRL_ENTRY entry
= NULL
;
1553 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1556 error
= CRYPT_E_REVOKED
;
1557 pRevStatus
->dwIndex
= index
;
1561 /* Since the CRL was retrieved for the cert being checked, then it's
1562 * guaranteed to be fresh, and the cert is not revoked.
1564 error
= ERROR_SUCCESS
;
1569 static DWORD
verify_cert_revocation_from_dist_points_ext(
1570 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1571 FILETIME
*pTime
, DWORD dwFlags
, const CERT_REVOCATION_PARA
*pRevPara
,
1572 PCERT_REVOCATION_STATUS pRevStatus
)
1574 DWORD error
= ERROR_SUCCESS
, cbUrlArray
;
1576 if (CRYPT_GetUrlFromCRLDistPointsExt(value
, NULL
, &cbUrlArray
, NULL
, NULL
))
1578 CRYPT_URL_ARRAY
*urlArray
= CryptMemAlloc(cbUrlArray
);
1582 DWORD j
, retrievalFlags
= 0, startTime
, endTime
, timeout
;
1585 ret
= CRYPT_GetUrlFromCRLDistPointsExt(value
, urlArray
,
1586 &cbUrlArray
, NULL
, NULL
);
1587 if (dwFlags
& CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION
)
1588 retrievalFlags
|= CRYPT_CACHE_ONLY_RETRIEVAL
;
1589 if (dwFlags
& CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG
&&
1590 pRevPara
&& pRevPara
->cbSize
>= offsetof(CERT_REVOCATION_PARA
,
1591 dwUrlRetrievalTimeout
) + sizeof(DWORD
))
1593 startTime
= GetTickCount();
1594 endTime
= startTime
+ pRevPara
->dwUrlRetrievalTimeout
;
1595 timeout
= pRevPara
->dwUrlRetrievalTimeout
;
1598 endTime
= timeout
= 0;
1600 error
= GetLastError();
1601 /* continue looping if one was offline; break if revoked or timed out */
1602 for (j
= 0; (!error
|| error
== CRYPT_E_REVOCATION_OFFLINE
) && j
< urlArray
->cUrl
; j
++)
1606 ret
= CryptRetrieveObjectByUrlW(urlArray
->rgwszUrl
[j
],
1607 CONTEXT_OID_CRL
, retrievalFlags
, timeout
, (void **)&crl
,
1608 NULL
, NULL
, NULL
, NULL
);
1611 error
= verify_cert_revocation_with_crl_online(cert
, crl
,
1612 index
, pTime
, pRevStatus
);
1613 if (!error
&& timeout
)
1615 DWORD time
= GetTickCount();
1617 if ((int)(endTime
- time
) <= 0)
1619 error
= ERROR_TIMEOUT
;
1620 pRevStatus
->dwIndex
= index
;
1623 timeout
= endTime
- time
;
1625 CertFreeCRLContext(crl
);
1628 error
= CRYPT_E_REVOCATION_OFFLINE
;
1630 CryptMemFree(urlArray
);
1634 error
= ERROR_OUTOFMEMORY
;
1635 pRevStatus
->dwIndex
= index
;
1640 error
= GetLastError();
1641 pRevStatus
->dwIndex
= index
;
1646 static DWORD
verify_cert_revocation_from_aia_ext(
1647 const CRYPT_DATA_BLOB
*value
, PCCERT_CONTEXT cert
, DWORD index
,
1648 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1649 PCERT_REVOCATION_STATUS pRevStatus
)
1653 CERT_AUTHORITY_INFO_ACCESS
*aia
;
1655 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_AUTHORITY_INFO_ACCESS
,
1656 value
->pbData
, value
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &aia
, &size
);
1661 for (i
= 0; i
< aia
->cAccDescr
; i
++)
1662 if (!strcmp(aia
->rgAccDescr
[i
].pszAccessMethod
,
1665 if (aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
==
1667 FIXME("OCSP URL = %s\n",
1668 debugstr_w(aia
->rgAccDescr
[i
].AccessLocation
.u
.pwszURL
));
1670 FIXME("unsupported AccessLocation type %d\n",
1671 aia
->rgAccDescr
[i
].AccessLocation
.dwAltNameChoice
);
1674 /* FIXME: lie and pretend OCSP validated the cert */
1675 error
= ERROR_SUCCESS
;
1678 error
= GetLastError();
1682 static DWORD
verify_cert_revocation_with_crl_offline(PCCERT_CONTEXT cert
,
1683 PCCRL_CONTEXT crl
, DWORD index
, FILETIME
*pTime
,
1684 PCERT_REVOCATION_STATUS pRevStatus
)
1689 valid
= CompareFileTime(pTime
, &crl
->pCrlInfo
->ThisUpdate
);
1692 /* If this CRL is not older than the time being verified, there's no
1693 * way to know whether the certificate was revoked.
1695 TRACE("CRL not old enough\n");
1696 error
= CRYPT_E_REVOCATION_OFFLINE
;
1700 PCRL_ENTRY entry
= NULL
;
1702 CertFindCertificateInCRL(cert
, crl
, 0, NULL
, &entry
);
1705 error
= CRYPT_E_REVOKED
;
1706 pRevStatus
->dwIndex
= index
;
1710 /* Since the CRL was not retrieved for the cert being checked,
1711 * there's no guarantee it's fresh, so the cert *might* be okay,
1712 * but it's safer not to guess.
1714 TRACE("certificate not found\n");
1715 error
= CRYPT_E_REVOCATION_OFFLINE
;
1721 static DWORD
verify_cert_revocation(PCCERT_CONTEXT cert
, DWORD index
,
1722 FILETIME
*pTime
, DWORD dwFlags
, PCERT_REVOCATION_PARA pRevPara
,
1723 PCERT_REVOCATION_STATUS pRevStatus
)
1725 DWORD error
= ERROR_SUCCESS
;
1726 PCERT_EXTENSION ext
;
1728 if ((ext
= CertFindExtension(szOID_CRL_DIST_POINTS
,
1729 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1730 error
= verify_cert_revocation_from_dist_points_ext(&ext
->Value
, cert
,
1731 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1732 else if ((ext
= CertFindExtension(szOID_AUTHORITY_INFO_ACCESS
,
1733 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
1734 error
= verify_cert_revocation_from_aia_ext(&ext
->Value
, cert
,
1735 index
, pTime
, dwFlags
, pRevPara
, pRevStatus
);
1738 if (pRevPara
&& pRevPara
->hCrlStore
&& pRevPara
->pIssuerCert
)
1740 PCCRL_CONTEXT crl
= NULL
;
1743 /* If the caller told us about the issuer, make sure the issuer
1744 * can sign CRLs before looking for one.
1746 if ((ext
= CertFindExtension(szOID_KEY_USAGE
,
1747 pRevPara
->pIssuerCert
->pCertInfo
->cExtension
,
1748 pRevPara
->pIssuerCert
->pCertInfo
->rgExtension
)))
1750 CRYPT_BIT_BLOB usage
;
1751 DWORD size
= sizeof(usage
);
1753 if (!CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_BITS
,
1754 ext
->Value
.pbData
, ext
->Value
.cbData
,
1755 CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &usage
, &size
))
1756 canSignCRLs
= FALSE
;
1757 else if (usage
.cbData
> 2)
1759 /* The key usage extension only defines 9 bits => no more
1760 * than 2 bytes are needed to encode all known usages.
1762 canSignCRLs
= FALSE
;
1766 BYTE usageBits
= usage
.pbData
[usage
.cbData
- 1];
1768 canSignCRLs
= usageBits
& CERT_CRL_SIGN_KEY_USAGE
;
1775 /* If the caller was helpful enough to tell us where to find a
1776 * CRL for the cert, look for one and check it.
1778 crl
= CertFindCRLInStore(pRevPara
->hCrlStore
,
1779 cert
->dwCertEncodingType
,
1780 CRL_FIND_ISSUED_BY_SIGNATURE_FLAG
|
1781 CRL_FIND_ISSUED_BY_AKI_FLAG
,
1782 CRL_FIND_ISSUED_BY
, pRevPara
->pIssuerCert
, NULL
);
1786 error
= verify_cert_revocation_with_crl_offline(cert
, crl
,
1787 index
, pTime
, pRevStatus
);
1788 CertFreeCRLContext(crl
);
1792 TRACE("no CRL found\n");
1793 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1794 pRevStatus
->dwIndex
= index
;
1800 WARN("no CERT_REVOCATION_PARA\n");
1801 else if (!pRevPara
->hCrlStore
)
1802 WARN("no dist points/aia extension and no CRL store\n");
1803 else if (!pRevPara
->pIssuerCert
)
1804 WARN("no dist points/aia extension and no issuer\n");
1805 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1806 pRevStatus
->dwIndex
= index
;
1812 typedef struct _CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
{
1814 PCCERT_CONTEXT pIssuerCert
;
1816 HCERTSTORE
*rgCertStore
;
1817 HCERTSTORE hCrlStore
;
1818 LPFILETIME pftTimeToUse
;
1819 } CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
;
1821 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1826 } OLD_CERT_REVOCATION_STATUS
;
1828 /***********************************************************************
1829 * CertDllVerifyRevocation (CRYPTNET.@)
1831 BOOL WINAPI
CertDllVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1832 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1833 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1837 LPFILETIME pTime
= NULL
;
1839 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1840 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1842 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1843 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1845 SetLastError(E_INVALIDARG
);
1850 SetLastError(E_INVALIDARG
);
1853 if (pRevPara
&& pRevPara
->cbSize
>=
1854 sizeof(CERT_REVOCATION_PARA_NO_EXTRA_FIELDS
))
1855 pTime
= pRevPara
->pftTimeToUse
;
1858 GetSystemTimeAsFileTime(&now
);
1861 memset(&pRevStatus
->dwIndex
, 0, pRevStatus
->cbSize
- sizeof(DWORD
));
1862 if (dwRevType
!= CERT_CONTEXT_REVOCATION_TYPE
)
1863 error
= CRYPT_E_NO_REVOCATION_CHECK
;
1866 for (i
= 0; !error
&& i
< cContext
; i
++)
1867 error
= verify_cert_revocation(rgpvContext
[i
], i
, pTime
, dwFlags
,
1868 pRevPara
, pRevStatus
);
1872 SetLastError(error
);
1873 pRevStatus
->dwError
= error
;
1875 TRACE("returning %d (%08x)\n", !error
, error
);