設定憑證屬性
使用 ICertServerPolicy::SetCertificateProperty 方法來設定憑證的主體屬性。 主體屬性是與憑證擁有者或要求憑證的個人相關的屬性。 如需主體屬性的清單,請參閱 命名屬性。
您也可以使用 SetCertificateProperty 方法來設定 NotBefore 和 NotAfter 憑證屬性。 如需 NotBefore 和 NotAfter 憑證屬性的描述,請參閱 憑證屬性。
使用 ICertServerPolicy::SetCertificateExtension 方法,將任意數目的延伸模組新增至憑證。 您可以使用延伸模組,將補充主體或使用資訊新增至憑證。 如需詳細資訊,請參閱 延伸模組處理常式。
下列範例會在憑證上設定憑證屬性和延伸模組。 您可以在ICertPolicy2::VerifyRequest實作中呼叫SetCertificateProperty和SetCertificateExtension方法。 此範例不是完整的 VerifyRequest 實作;此範例不會顯示驗證邏輯。
#include <windows.h>
#include <stdio.h>
STDMETHODIMP CCertPolicy::VerifyRequest(
BSTR const strConfig,
LONG Context,
LONG bNewRequest,
LONG Flags,
LONG __RPC_FAR *pDisposition)
{
HRESULT hr = S_OK;
ICertServerPolicy *pServer = NULL;
BSTR bstrPropName = NULL;
VARIANT vPropValue;
BSTR bstrExtName = NULL;
VARIANT vExtValue;
// Retrieve an ICertServerPolicy interface pointer.
hr = CoCreateInstance( CLSID_CCertServerPolicy,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICertServerPolicy,
(void **) &pServer );
if (FAILED( hr ))
{
printf("Failed CoCreateInstance for ICertServerPolicy "
"- %x\n", hr );
return hr;
}
// Set the context to which this request refers.
hr = pServer->SetContext(Context);
if (FAILED( hr ))
{
printf("Failed SetContext(%u) - %x\n", Context, hr );
pServer->Release();
return hr;
}
// Specify the subject property to set on the certificate.
bstrPropName = SysAllocString(L"Subject.EMail");
if ( NULL == bstrPropName )
{
hr = E_OUTOFMEMORY;
printf("Failed SysAllocString for bstrPropName "
"(no memory)\n" );
pServer->Release();
return hr;
}
VariantInit( &vPropValue );
vPropValue.VT_BSTR;
vPropValue.bstrVal = SysAllocString(L"someone@example.com");
if ( NULL == vPropValue.bstrVal )
{
hr = E_OUTOFMEMORY;
printf("Failed SysAllocString for vPropValue "
"(no memory)\n" );
SysFreeString(bstrPropName);
pServer->Release();
return hr;
}
// Set the subject property on the certificate.
hr = pServer->SetCertificateProperty( bstrPropName,
PROPTYPE_STRING,
&vPropValue );
SysFreeString(bstrPropName);
VariantClear(&vPropValue);
if (FAILED(hr))
{
printf("Failed SetCertificateProperty - %x\n", hr);
pServer->Release();
return hr;
}
// Specify the extension property to set on the certificate.
bstrExtName = SysAllocString(L"2.29.38.4");
if ( NULL == bstrExtName )
{
hr = E_OUTOFMEMORY;
printf("Failed SysAllocString for bstrExtName "
"(no memory)\n" );
pServer->Release();
return hr;
}
VariantInit( &vExtValue );
vExtValue.VT_BSTR;
vExtValue.bstrVal = SysAllocString
(L"https://example.microsoft.com");
if ( NULL == vExtValue.bstrVal )
{
hr = E_OUTOFMEMORY;
printf("Failed SysAllocString for vExtValue (no memory)\n" );
SysFreeString(bstrExtName);
pServer->Release();
return hr;
}
// Set the extension property on the certificate.
hr = pServer->SetCertificateExtension( bstrExtName,
PROPTYPE_STRING,
EXTENSION_CRITICAL_FLAG,
&vExtValue );
SysFreeString(bstrExtName);
VariantClear(&vExtValue);
if (FAILED(hr))
{
printf("Failed SetCertificateExtension - %x\n", hr);
pServer->Release();
return hr;
}
pServer->Release();
return(hr);
}