????? ????? ??????? ?????? SSL ???? ??????? ?????? (IIS)

?????? ????????? ?????? ?????????
???? ???????: 313624 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??? ????? ??? ??????? ??? ??????? ??? ????? ????? ????? ???????. ??? ????? ????? ??????? ?????? ?? ???? ???? ???????? ????? ??????? ???? ???? ?????. ?????? ??? ??????? ??? ????? ?????? ????? ???? ????? ????????? "??? ???????/??????? ?????" ?? ???? ?????? ???????? ?? Microsoft (MMC).

??????


???? ??? ??????? ???? ????? ????? ????? ??????? ??? ?? ????? ????? Sockets Layer (SSL) ?????? ???? ??????? ?????? (IIS). ????? IIS ??????? 4.0 ? IIS ??????? 5.0 ?? ????? ????? ?????? (UI) ?????? ?? ?????? ?????? ???? SSL ????? ????? ????? ?????? ???????.



??????? ??????? ?????? ?? ?? ?????? ???????? SSL

????? ???? ????? ?? ???? ??????? ??????? ?????? ?????? SSL ????? IIS:
  1. ??? ???? ????? ??? ??? ?????? ?????? ?????? ????? ????. ??? ??? ???? ?????? ??????? ?? ????? ?? ????? ??? ???? IIS ???? ?? ??? ?????? ?????.
  2. ??????? ??????? ??? ???? ???????? ???????.
  3. ????? IIS ???????? ??????? ???? ???? ???? ?? ?????? 1.



????? IIS ?????? ??? ?? ????? SSL ????????

????? ??? ??? ?????? ????? ??????? ?????? ??? ????? ??? ????? ???? ?????? ????? ???? ????? ????? ?? ???? ?????? ????? ???????. ??? ???? Microsoft ?? ????? ?? ?? ???????? ???? ???? ??? ????? ????? ??????? ?????? ??? ?????. ????? ????? ??????? ??? ???????? ??????.

?????? ??? ???? ???????? ????? ????? ??????? ??? ??????. ???? ??????? ??????? ?????? IIS ????? ??? ?? ????? SSL ????????:
  1. ??????? APIs ????? Windows Microsoft ?????? ??? ???? ??????? ??????? ???????.
  2. ??? ???? ????? ????? ????? ????? IIS SSLCertHash ??? ???? ???? ???????.
  3. ??? ???? ????? ????? ????? ????? IIS SSLStoreName ??? ???? ???? ???? ?????????.
???? ??????? ??????? ????? ??? ??????? ??? ???????? ?????? ??? ????? ???????? ???????? ?????? ??? ???? ??????? ???????? ?? ?????? ??? ??????? SSLCertHash:
C Code to Obtain the Thumbprint of a Server Authentication Certificate
C Code to Enter the SSLCertHash Property into the Metabase



????? ????? SSL ??? ???? ??? ????

??? ????? ??????? ???????? ?? ???? "????? IIS ??? ????? ??? ?? ??? ????? SSL ????????" ? ????? ????? SSL ??? ???? ???? ?? ?? ???? ????. ?????? ???? ??? ????? SSL ?????? ?? ?????? ???? ???? ????????. ?????? ??????? ??????? ?? ?? ???? ??????? ?? ??? "????? IIS ??? ????? ??? ?? ??? ????? SSL ????????":
  1. ??? ??????? ??????? CEnroll::createPKCS10() ??? ????? ??? ???????. ????? ????????? ??? ???? ??????? ?????? ???????: szOID_PKIX_KP_SERVER_AUTH:
    #define szOID_PKIX_KP_SERVER_AUTH       "1.3.6.1.5.5.7.3.1"		
  2. ??? ??????? ??????? () ICertRequest::Submit ??? ????? ??? ??????? ??? ?????? ??????.
  3. ??????? ??????? ?? ?????? ?????? ?? ??? ?????? ?? ???? ????????. ???? IIS ???? ????? ???????? ???????? ???? ???? ????????? ?? ??? ?????? ?????? ?? ???? ?????? "????????? ??????".




??????? ??????? ?????? IIS

??? ??? ??????? ??? ?????? ??? ???? ????? IIS ??? ???:
  1. ??????? ??????? CertGetCertificateContextProperty() ?????? ??? ??? ???? ??????? ???????? CERT_HASH_PROP_ID. ?????? ?????? CertGetCertificateContextProperty() ????? CryptHashCertificate() ????? ???? ??????? CERT_HASH_PROP_ID. ?? ???? ??? ???? ???? ??????? CertGetCertificateContextProperty() ????? ???????? ??????? SHA1.
  2. ??? ?? ???? ?????? ?????? ????? ????? ????? ????? ? SSLCertHash ? ???? ?????? ?? ???? ???. ????? SSLCertHash ??????? ???? ??????? ???? ???? ???? ?? ?????? 1 ?? ??? ??????. ??? ???? ??? ???????? ?????? ???? ??? ???? ????? ??????? SSLCertHash ?????? ???? ????? ???? ? ????? ?? ?????? ?????? ??? ?? ?? ????? ??????? ?????? ????? IIS ???????? SSLCertHash. ???? ??????? ?????? ????? ????? IIS ??? ??????? ??? ??????. ???????? SSLCertHash ??????? ????? ????? IIS ? ??? ??????? ???? ????? 5506.
  3. ??? ???? ????? ????? ????? ????? ????? ????? ? SSLStoreName ????? ??? ???????. ????? SSLStoreName ??? ????? ?????? MY. ????? ??? ????? SSLStoreName ???? IIS ?????? ??????? (??? ???? ??????? ?? Script ADSI) ?? ?? ???? ?????? ??????? ????? IIS. ???????? SSLStoreName ???????? ???????? IIS Admin ??????? ???? ????? 5511.




????? c ??? ?????? ??? ???? ??????? ?? ????? ?????? ??????

????? ??????? ????????? ???????? "C Microsoft ???? ??? ??????? ????? ?????? ??? ??????? ???? ??????? ?????" ?????? ???? ":
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#define MY_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

//--------------------------------------------------------------------
//    Define the name of the store where the needed certificate
//    can be found. 

#define CERT_STORE_NAME  L"MY"

//--------------------------------------------------------------------
//   Declare local functions.
//   Local function definitions follow main.

void HandleError(char *s);

void main(void)
{
//--------------------------------------------------------------------
// Declare and initialize local variables. 
// This includes initializing a pointer to the message. 
// Usually, the message will exist somewhere and a pointer will
// be passed to the application.

//--------------------------------------------------------------------
// System store handle

HCERTSTORE hStoreHandle;   

//--------------------------------------------------------------------
// Pointer to a certificate

PCCERT_CONTEXT pCert;
PCCERT_CONTEXT pPrevCert;
 
LPBYTE pEncodedBytes = NULL;
LPBYTE pHash;
DWORD cbData, i;

//--------------------------------------------------------------------
// Open a certificate store.

if ( !( hStoreHandle = CertOpenStore(
   CERT_STORE_PROV_SYSTEM,
   0,
   NULL,
   CERT_SYSTEM_STORE_LOCAL_MACHINE,
   CERT_STORE_NAME)))
{
     HandleError("The MY store could not be opened.");
}


pPrevCert = NULL;

for (; ((pCert = CertEnumCertificatesInStore(hStoreHandle, pPrevCert)) 
	                                         != NULL);
    pPrevCert = pCert)
{
    CERT_ENHKEY_USAGE *pKeyUsage;
    DWORD j, nLen;
    BOOL bFound = FALSE;
    char certName[1024];

    nLen = sizeof(certName);
    certName[0] = 0;
    if (CertNameToStr(MY_TYPE, &(pCert->pCertInfo->Subject),
        CERT_X500_NAME_STR,
        certName,
        sizeof(certName)))
    {
        printf("Checking %s certificate\n", certName);
    }

    cbData = 0;
    if (!CertGetEnhancedKeyUsage(pCert,
        0,
        NULL,
        &cbData) || cbData == 0)
    {
        if (GetLastError() == CRYPT_E_NOT_FOUND)
        {
            printf("%s certificate is for all key usages\n", certName);
            break;
        }
        else
            printf("CertGetEnhancedKeyUsage failed with error code : %08X\n",
                GetLastError());
    }
    pKeyUsage = (CERT_ENHKEY_USAGE *)
        HeapAlloc(GetProcessHeap(), 0, cbData);
    if (pKeyUsage == NULL)
    {
        printf("HeapAlloc failed with error code : %08X\n",
            GetLastError());
        HandleError("Certificate not found.");
    }
    if (!CertGetEnhancedKeyUsage(pCert,
        0,
        pKeyUsage,
        &cbData))
    {
        if (GetLastError() == CRYPT_E_NOT_FOUND)
        {
            printf("%s certificate is for all key usages\n", certName);
            HeapFree(GetProcessHeap(), 0, pKeyUsage);
            break;
        }
        else
        {
            printf("CertGetEnhancedKeyUsage failed with error code : %08X\n",
                GetLastError());
            HeapFree(GetProcessHeap(), 0, pKeyUsage);
            continue;
        }
    }

    if (pKeyUsage->cUsageIdentifier == 0)
    {
        printf("%s certificate is for all key usages\n", certName);
        HeapFree(GetProcessHeap(), 0, pKeyUsage);
        break;
    }

    bFound = FALSE;
    for (j = 0; j < pKeyUsage->cUsageIdentifier; j++)
    {
        if (strcmpi(pKeyUsage->rgpszUsageIdentifier[j], 
			        szOID_PKIX_KP_SERVER_AUTH) == 0)
        {
            printf("%s certificate is for Server Authentication\n", 
				   certName);
            bFound = TRUE;
            break;
        }
    }

    HeapFree(GetProcessHeap(), 0, pKeyUsage);
    if (bFound)
        break;
}

if (pCert == NULL)
   HandleError("Certificate not found.");

if (pPrevCert)
{
    CertFreeCertificateContext(pPrevCert);
    pPrevCert = NULL;
}

/// CASE 2 Get the hash from the certificate
pHash = NULL;
cbData = 0;
CertGetCertificateContextProperty(pCert, CERT_HASH_PROP_ID, NULL, &cbData);
if (cbData == 0)
{
   HandleError("CertGetCertificateContextProperty 1 failed");
}

pHash = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, cbData);
if (pHash == NULL)
{
   HandleError("HeapAlloc failed");
}
if (!CertGetCertificateContextProperty(pCert, CERT_HASH_PROP_ID, pHash, 
	                                   &cbData))
{
   HandleError("CertGetCertificateContextProperty 2 failed");
}

printf("CERT_HASH_PROP_ID Length is %d\n", cbData);
printf("CERT_HASH_PROP_ID BYTES [", cbData);

for (i = 0; i < cbData; i++)
{
    printf("%02X", pHash[i]);
}
printf("]\n");

//--------------------------------------------------------------------
// Clean up and free memory.

if (pEncodedBytes)
    HeapFree(GetProcessHeap(), 0, pEncodedBytes);

if (pHash)
    HeapFree(GetProcessHeap(), 0, pHash);

if(pCert)
     CertFreeCertificateContext(pCert);
if(CertCloseStore(
      hStoreHandle, 
      CERT_CLOSE_STORE_CHECK_FLAG))
{
    printf("The store closed and all certificates are freed. \n");
}
else
{
    printf("Store closed -- \n"
          "not all certificates, CRLs or CTLs were freed");
}
} // End of main

//--------------------------------------------------------------------
//  This example uses the function HandleError, a simple error
//  handling function, to print an error message to the standard error 
//  (stderr) file and exit the program. 
//  For most applications, replace this function with one 
//  that does more extensive error reporting.

void HandleError(char *s)
{
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // End of HandleError




????? c ??? ????? ????? SSLCertHash ?? ????? ???????

??? ????? ????????? ???????? ?????? ????? ??????? ????? IIS ???????? ???????? ?????? ????? SSLCertHash ?? ????? ???????. ?????? ???????? ???????? ??????? ???? ????? ??????? ???? ??????? ?? ???????. ??????? ????????? ???????? ??????? ???? ???? ??????? ???? ???? ????? ??? ????? ????????? ???????? ?? ????? "?????? ??? ???? ??????? ?? ?????? ????? ??????". ???????? ??? ????? ????? ????? SetData ?? ??? ??????? "? ????? ??? ????? SSLCertHash ?? ??? ?? ?????? ????? SSL ????? ??? ????? ?????? ??? SSLCertHash ?????. The SSLCertHash method return the existing CertHash:
#define UNICODE // unicode must be defined for Metabase access
#define INITGUID 
#include <windows.h>
#include <httpfilt.h>
#include <stdio.h> 

#define SETDATA

#include <iadmw.h>    // COM Interface header 
#include <iiscnfg.h>  // MD_ & IIS_MD_ #defines 



extern "C" wmain (int argc, TCHAR ** argv)
{  
	IMSAdminBase  *pIMeta;  
	METADATA_HANDLE MyHandle; 
    HRESULT hres;
   	METADATA_RECORD record = {0};
    TCHAR szError [2048];
	BYTE *myData=NULL;
    DWORD dwSize = sizeof (record);
 	DWORD i;

	// this just a sample of some thumbprint
	BYTE bar[]={0x24, 0xC6, 0xBA, 0xBB, 0x81, 0x76, 0x05, 0xC9, 0xC3, 
		        0x97, 0x6D, 0x4D, 0xEB, 0x85, 0x8F, 0x4F, 0xBF, 0x38,
				0xFD};
    
    
    CoInitialize (NULL);
    
    // get a pointer to the IIS Admin Base Object
	hres = CoCreateInstance(CLSID_MSAdminBase, NULL, CLSCTX_ALL, 
			IID_IMSAdminBase, (void **) &pIMeta);  
	if (FAILED(hres))  
	{
	    wsprintf (szError, L"CoCreateInstance Failed. Error: %x\n", hres);
        printf ("%S\n", szError);
		CoUninitialize();
        return TRUE;  
	}
    
    // for this test use only 1st server instance
    hres = pIMeta->OpenKey(METADATA_MASTER_ROOT_HANDLE, L"/LM", 
		METADATA_PERMISSION_READ|METADATA_PERMISSION_WRITE, 20, &MyHandle); 
	if (FAILED (hres) )
	{
		wsprintf (szError, L"OpenKey Failed. Error: %x\n", hres);
        printf ("%S\n", szError);
		goto clean;
	}
    
	// SSLCertHash = 5506
	record.dwMDIdentifier =  5506;
    record.dwMDAttributes =METADATA_INHERIT;
    record.dwMDUserType=IIS_MD_UT_SERVER;
    record.dwMDDataType= BINARY_METADATA;
    record.pbMDData = (unsigned char *) myData;

#ifndef SETDATA
#pragma message ("Building for GetData\n")    
	again:
    hres = pIMeta->GetData (MyHandle,argv[1], &record, &dwSize);
   	if (FAILED (hres) )
	{
		if (hres == MD_ERROR_DATA_NOT_FOUND)
		{
			printf ("%S\n", L"Data not found, no certificate is set!");
			goto clean;
		}
		else if (HRESULT_CODE(hres)==ERROR_INSUFFICIENT_BUFFER)
		{
			record.dwMDDataLen=dwSize;
			myData = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwSize);
			record.pbMDData  = (unsigned char *)myData;
		    goto again;
		}
		else  
		{
			wsprintf (szError, L"GetData Failed. Error: %x\n", hres);
			printf ("%S\n", szError);
			goto clean;;
		}
	}

	printf ("%S", L"Got thumbprint. You can compare" 
		          L" it with the MMC for IIS value:\n");	
	for ( i=0; i<(record.dwMDDataLen/sizeof (BYTE)); i++)		
		printf ("%2X ", myData[i]);  
	HeapFree(GetProcessHeap(), 0, myData);
#else
#pragma message ("Building for SetData\n")

	
	record.pbMDData = bar;
	record.dwMDDataLen = 19; // in real code it should be the size 
                                                   // of the thumbprint buffer
	hres = pIMeta->SetData (MyHandle,argv[1], &record);
	if (FAILED (hres) )
	{
		printf ("Set data failed: 0x%x!\n", hres);
		goto clean;
	}
	else
		printf ("New thumbprint is set\n");
#endif

clean:
	pIMeta->CloseKey(MyHandle); 
	pIMeta->SaveData();
	pIMeta->Release();
	CoUninitialize();

	return 1;
}



?????

????? ?? ?????????? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
315588????? ????? ??? ??????? ASP.NET ???????? ?????? ??????

???????

???? ???????: 313624 - ????? ??? ??????: 15/????? ???????/1427 - ??????: 2.2
????? ???
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
????? ??????: 
kbmt kbcrypt kbapi kbhowtomaster kbsecurity kbisapiext kbhowto KB313624 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????313624

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com