The following example demonstrates how a typical client would obtain Schannel credentials. The example follows the recommended practice of using the system defaults for ciphers and cipher strengths. This allows the Schannel package to use the strongest possible algorithms to secure the channel.
#include <windows.h>
#include <ntsecapi.h>
#include <stdio.h>
#include <sspi.h>
#include <schnlsp.h>
void getSchannelClientHandle(PCredHandle ppClientCred)
{
SECURITY_STATUS SecStatus;
TimeStamp Lifetime;
CredHandle hCred;
SCHANNEL_CRED credData;
ZeroMemory(&credData, sizeof(credData));
credData.dwVersion = SCHANNEL_CRED_VERSION;
//-------------------------------------------------------
// Specify the TLS V1.0 (client-side) security protocol.
credData.grbitEnabledProtocols = SP_PROT_TLS1_CLIENT;
SecStatus = AcquireCredentialsHandle (
NULL, // default principal
UNISP_NAME, // name of the SSP
SECPKG_CRED_OUTBOUND, // client will use the credentials
NULL, // use the current LOGON id
&credData, // protocol-specific data
NULL, // default
NULL, // default
&hCred, // receives the credential handle
&Lifetime // receives the credential time limit
);
printf("Client credentials status: 0x%x\n", SecStatus);
// Return the handle to the caller.
if(ppClientCred != NULL)
*ppClientCred = hCred;
return;
//-------------------------------------------------------
// When you have finished with this handle,
// free the handle by calling the
// FreeCredentialsHandle function.
}
The primary difference between the client-side and server-side request for credentials is that the server must provide a certificate using a CERT_CONTEXT structure as follows:
PCCERT_CONTEXT serverCert; // server-side certificate
//-------------------------------------------------------
// Get the server certificate.
if (! (serverCert = getServerCertificate())) return;
// getServerCertificate is a placeholder function.
credData.paCred = &serverCert;
The example function getServerCertificate is a placeholder function for this application-specific activity. For an example implementation of the getServerCertificate function, see Getting a Certificate.
Note:
When requesting credentials to be used by the server, change the third (fCredentialUse) parameter of the AcquireCredentialsHandle function from SECPKG_CRED_OUTBOUND to SECPKG_CRED_INBOUND.
Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.
