Setting Certificate Properties
Use the ICertServerPolicy::SetCertificateProperty method to set the subject properties of a certificate. Subject properties are properties related to the owner of the certificate or the individual who requested the certificate. For a list of subject properties, see Name Properties.
You can also use the SetCertificateProperty method to set the NotBefore and NotAfter certificate properties. For a description of the NotBefore and NotAfter certificate properties, see Certificate Properties.
Use the ICertServerPolicy::SetCertificateExtension method to add any number of extensions to the certificate. You can use extensions to add supplemental subject or usage information to the certificate. For more information, see Extension Handlers.
The following example sets a certificate property and extension on a certificate. You call the SetCertificateProperty and SetCertificateExtension methods in your ICertPolicy2::VerifyRequest implementation. The example is not a complete VerifyRequest implementation; the example does not show verification logic.
#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);
}