Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Implementing an SSL/TLS-Enabled Client/Server on Windows Using GSS API


HexWeb HTML

Listing 2: CSslCredentials class

... // other code not shown

// definition

class CSslCredentials

{

friend class CSslProvider;

protected:

CSslCredentials();

~CSslCredentials();

public:

HRESULT Obtain(BOOL bAsServer, LPCTSTR strPrincipal,

LPCTSTR strStore, BOOL bMutualAuth = FALSE,

DWORD dwProtoFlags = 0);

HRESULT Obtain(BOOL bAsServer, LPCTSTR strPrincipal,

HCERTSTORE hStore, BOOL bMutualAuth = FALSE,

DWORD dwProtoFlags = 0);

HRESULT Obtain(DWORD dwProtoFlags = 0);

HRESULT Attach(CredHandle hCredentials, BOOL bAsServer,

BOOL bMutualAuth = FALSE, DWORD dwProtoFlags = 0);

HRESULT Detach();

BOOL IsServer() {return m_bServer;}

BOOL MutualAuthRequired() {return m_bMutualAuth;}

DWORD GetProtoFlags() {return m_dwProtoFlags;}

BOOL HasCredHandle()

{

return SecIsValidHandle(&m_hCredentials);

}

PCredHandle GetHandle()

{

return (HasCredHandle() ? &m_hCredentials : NULL);

}

void CleanUp();

private:

HRESULT OpenSysStore(LPCTSTR strStore, HCERTSTORE& hStore);

HRESULT FindCertificate(HCERTSTORE hStore,

LPCTSTR strPrincipal, PCCERT_CONTEXT& certContext);

HRESULT ObtainImpl(PCCERT_CONTEXT certContext,

BOOL bAsServer, BOOL bMutualAuth, DWORD dwProtoFlags);

private:

CredHandle m_hCredentials;

BOOL m_bServer;

BOOL m_bMutualAuth;

DWORD m_dwProtoFlags;

HCERTSTORE m_hStore;

};

... // other code not shown

// implementation

HRESULT CSslCredentials::ObtainImpl(PCCERT_CONTEXT certContext,

BOOL bAsServer,

BOOL bMutualAuth,

DWORD dwProtoFlags)

{

// Build Schannel credentials structure.

SCHANNEL_CRED credSchannel = {0};

credSchannel.dwVersion = SCHANNEL_CRED_VERSION;

credSchannel.grbitEnabledProtocols = dwProtoFlags;

if (certContext != NULL)

{

credSchannel.cCreds = 1;

credSchannel.paCred = &certContext;

}

// Create SSL credentials.

TimeStamp tsExpires;

HRESULT hr = CSspiLib::AcquireCredentialsHandle(

NULL,

UNISP_NAME,

(bAsServer ?

SECPKG_CRED_INBOUND :

SECPKG_CRED_OUTBOUND),

NULL,

&credSchannel,

NULL,

NULL,

&m_hCredentials,

&tsExpires);

// See, if we have succeeded

if (SUCCEEDED(hr))

{

m_bServer = bAsServer;

if (m_bServer)

m_bMutualAuth = bMutualAuth;

m_dwProtoFlags = dwProtoFlags;

}

else

TRACE(_T("Line: %d. Error: 0x%08X\n"), __LINE__, hr);

return hr;

}

... // other code not shown

HRESULT CSslCredentials::OpenSysStore(LPCTSTR strStore,

HCERTSTORE& hStore)

{

CT2CA storeName(strStore);

m_hStore = ::CertOpenSystemStore(0, storeName);

if (m_hStore == NULL)

{

DWORD dwErrCode = ::GetLastError();

return HRESULT_FROM_WIN32(dwErrCode);

}

return S_OK;

}

HRESULT CSslCredentials::FindCertificate(

HCERTSTORE hStore,

LPCTSTR strPrincipal,

PCCERT_CONTEXT& certContext)

{

USES_CONVERSION;

CT2CA hostName(strPrincipal);

ASSERT(m_hStore != NULL);

certContext = ::CertFindCertificateInStore(

m_hStore,

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,

0,

CERT_FIND_SUBJECT_STR_A,

hostName,

NULL);

if (certContext == NULL)

{

DWORD dwErrCode = ::GetLastError();

return HRESULT_FROM_WIN32(dwErrCode);

}

return S_OK;

}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.