62,047
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
void main(int argc, _TCHAR *argv[])
{
if(argc<3) {
printf("USAGE: %s [webserver] [username]\n", argv[0]);
return;
}
// Open the internet connection
HINTERNET hInternetSession = InternetOpen(
"Wininet Client App", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0) ;
HINTERNET hInternetConnect = InternetConnect(hInternetSession, argv[1],
INTERNET_DEFAULT_HTTPS_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", "",
HTTP_VERSION, "", NULL,
INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);
BOOL bRet;
DWORD dwLastError = 0;
// Send the HTTP request
bRet = HttpSendRequest(hHttpOpenRequest, NULL, 0, 0, 0);
if (!bRet) dwLastError = GetLastError();
// Get the certificate information from the server if available.
INTERNET_CERTIFICATE_INFO sInfo;
DWORD length = sizeof(sInfo);
InternetQueryOption(hHttpOpenRequest, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
(LPVOID)&sInfo, &length);
printf("The Issuer of the server certificate is:\n%s\n", sInfo.lpszIssuerInfo );
printf("The Subject of the server certificate is:\n%s\n", sInfo.lpszSubjectInfo );
// Look up the local certicate store to get a certificate that matches the one of the server.
PCCERT_CONTEXT pCertContext = NULL;
HCERTSTORE hCertStore = CertOpenSystemStore(NULL, "MY");
char pszIssuer[256] = "\0";
char pszSubject[256] = "\0";
BOOL bGotCert = FALSE;
while(pCertContext = CertEnumCertificatesInStore(hCertStore,pCertContext))
{
printf("The following certificates are available:\n");
CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL,
pszIssuer, 256);
printf("Issuer: %s\n", pszIssuer);
CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE,0,NULL,
pszSubject, 256);
printf("Subject: %s\n", pszSubject);
if ((strcmp(pszSubject, argv[2]) == 0) && (strcmp(pszIssuer, sInfo.lpszIssuerInfo) == 0)) {
bGotCert = TRUE;
printf("\nFound a certificate that matches the web server certificate.\n");
break;
}
}
// If the web server requires a cert and we found a matching one, attach the cert to the request.
if (( dwLastError == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED ) && bGotCert)
{
printf("Sending a HTTP request to the web server with the found certificate...\n");
if (InternetSetOption(hHttpOpenRequest, INTERNET_OPTION_CLIENT_CERT_CONTEXT,
(void*)pCertContext, sizeof(*pCertContext)) )
{
CertDuplicateCertificateContext(pCertContext);
}
}
bRet = HttpSendRequest(hHttpOpenRequest, NULL, 0, 0, 0);
if (bRet) {
DWORD dwCode, dwSize;
dwSize = sizeof(dwCode);
if ( !HttpQueryInfo (hHttpOpenRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL))
{
}
if (dwCode == 200)
printf("Request sent to the server successfully.(HTTP Status: %d)\n", dwCode);
if (dwCode == 403)
printf("You didn't select a valid certificate. (HTTP Status: %d)\n", dwCode);
} else {
printf("Untrusted CA.\n");
}
CertFreeCertificateContext(pCertContext);
CertCloseStore(hCertStore,0);
InternetCloseHandle (hInternetSession);
InternetCloseHandle (hInternetConnect);
InternetCloseHandle (hHttpOpenRequest);
printf("Press any key to continue...");
getchar();
}
using System;
#include <wininet.h>
private static void Main(int argc, _TCHAR[] argv)
{
if(argc<3)
{
Console.Write("USAGE: {0} [webserver] [username]\n", argv[0]);
return;
}
// Open the internet connection
HINTERNET hInternetSession = InternetOpen("Wininet Client App", INTERNET_OPEN_TYPE_PRECONFIG, null, null, 0);
HINTERNET hInternetConnect = InternetConnect(hInternetSession, argv[1], INTERNET_DEFAULT_HTTPS_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", "", HTTP_VERSION, "", null, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);
int bRet;
uint dwLastError = 0;
// Send the HTTP request
bRet = HttpSendRequest(hHttpOpenRequest, null, 0, 0, 0);
if (!bRet)
dwLastError = GetLastError();
// Get the certificate information from the server if available.
INTERNET_CERTIFICATE_INFO sInfo;
uint length = sizeof(sInfo);
InternetQueryOption(hHttpOpenRequest, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, (IntPtr)&sInfo, ref length);
Console.Write("The Issuer of the server certificate is:\n{0}\n", sInfo.lpszIssuerInfo);
Console.Write("The Subject of the server certificate is:\n{0}\n", sInfo.lpszSubjectInfo);
// Look up the local certicate store to get a certificate that matches the one of the server.
PCCERT_CONTEXT pCertContext = null;
HCERTSTORE hCertStore = CertOpenSystemStore(null, "MY");
string pszIssuer = "\0";
string pszSubject = "\0";
int bGotCert = 0;
while(pCertContext = CertEnumCertificatesInStore(hCertStore,pCertContext))
{
Console.Write("The following certificates are available:\n");
CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, null, pszIssuer, 256);
Console.Write("Issuer: {0}\n", pszIssuer);
CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE,0,null, pszSubject, 256);
Console.Write("Subject: {0}\n", pszSubject);
if ((string.Compare(pszSubject, argv[2]) == 0) && (string.Compare(pszIssuer, sInfo.lpszIssuerInfo) == 0))
{
bGotCert = 1;
Console.Write("\nFound a certificate that matches the web server certificate.\n");
break;
}
}
// If the web server requires a cert and we found a matching one, attach the cert to the request.
if ((dwLastError == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED) && bGotCert != 0)
{
Console.Write("Sending a HTTP request to the web server with the found certificate...\n");
if (InternetSetOption(hHttpOpenRequest, INTERNET_OPTION_CLIENT_CERT_CONTEXT, (IntPtr)pCertContext, sizeof(*pCertContext)))
{
CertDuplicateCertificateContext(pCertContext);
}
}
bRet = HttpSendRequest(hHttpOpenRequest, null, 0, 0, 0);
if (bRet != 0)
{
uint dwCode;
uint dwSize;
dwSize = sizeof(dwCode);
if (!HttpQueryInfo (hHttpOpenRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, ref dwCode, ref dwSize, null))
{
}
if (dwCode == 200)
Console.Write("Request sent to the server successfully.(HTTP Status: {0:D})\n", dwCode);
if (dwCode == 403)
Console.Write("You didn't select a valid certificate. (HTTP Status: {0:D})\n", dwCode);
}
else
{
Console.Write("Untrusted CA.\n");
}
CertFreeCertificateContext(pCertContext);
CertCloseStore(hCertStore,0);
InternetCloseHandle (hInternetSession);
InternetCloseHandle (hInternetConnect);
InternetCloseHandle (hHttpOpenRequest);
Console.Write("Press any key to continue...");
Console.Read();
}