21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (pIssuer) {<br />

if (!(pCACert = SpcLookupCACert(pIssuer))) return 0;<br />

if (pCACert->lpszCRLURL) {<br />

lpszURL = (LPSTR)LocalAlloc(LMEM_FIXED, lstrlenA(pCACert->lpszCRLURL) + 1);<br />

if (!lpszURL) return 0;<br />

lstrcpy(lpszURL, pCACert->lpszCRLURL);<br />

return lpszURL;<br />

}<br />

}<br />

return 0;<br />

}<br />

The worker function RetrieveWebData( ) is a wrapper around the WinInet API to<br />

retrieve the CRLdata from an HTTP or FTP server, depending on the URL. It simply<br />

establishes a connection to the server, retrieves the data if it can, and returns the<br />

data that was retrieved to the caller. The CRLdata is dynamically allocated with<br />

LocalAlloc( ), and it is expected that the caller will free the data with LocalFree( )<br />

when it is no longer needed. (The WinInet API is discussed in detail in Recipe 9.4.)<br />

static BYTE *RetrieveWebData(LPSTR lpszURL, DWORD *lpdwDataLength) {<br />

DWORD dwContentLength, dwFlags, dwNumberOfBytesRead,<br />

dwNumberOfBytesToRead;<br />

LPVOID lpBuffer, lpFullBuffer, lpNewBuffer;<br />

HINTERNET hRequest, hSession;<br />

hSession = InternetOpen(TEXT("Secure Programming Cookbook Recipe 10.11"),<br />

INTERNET_OPEN_TYPE_PROXY, 0, 0, 0);<br />

if (!hSession) return 0;<br />

dwFlags = INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |<br />

INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_NO_COOKIES |<br />

INTERNET_FLAG_NO_UI | INTERNET_FLAG_PASSIVE;<br />

hRequest = InternetOpenUrl(hSession, lpszURL, 0, 0, dwFlags, 0);<br />

if (!hRequest) {<br />

InternetCloseHandle(hSession);<br />

return 0;<br />

}<br />

dwContentLength = 0;<br />

dwNumberOfBytesToRead = 1024;<br />

lpFullBuffer = lpBuffer = LocalAlloc(LMEM_FIXED, dwNumberOfBytesToRead);<br />

while (InternetReadFile(hRequest, lpBuffer, dwNumberOfBytesToRead,<br />

&dwNumberOfBytesRead)) {<br />

dwContentLength = dwContentLength + dwNumberOfBytesRead;<br />

if (dwNumberOfBytesRead != dwNumberOfBytesToRead) break;<br />

if (!(lpNewBuffer = LocalReAlloc(lpFullBuffer, dwContentLength +<br />

dwNumberOfBytesToRead, 0))) {<br />

LocalFree(lpFullBuffer);<br />

InternetCloseHandle(hRequest);<br />

InternetCloseHandle(hSession);<br />

return 0;<br />

}<br />

560 | Chapter 10: Public Key Infrastructure<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!