Download user manual
Transcript
296
Complete Code Samples
int createServerSession( CRYPT_SESSION *serverSession,
const CRYPT_SESSION_TYPE sessionType,
const char *privKeysetName,
const char *privKeyLabel,
const char *privKeyPassword )
{
CRYPT_SESSION cryptSession;
CRYPT_CONTEXT cryptContext;
int status;
/* Clear the return value */
*serverSession = -1;
/* Create a server session */
status = cryptCreateSession( &cryptSession, CRYPT_UNUSED,
sessionType );
if( cryptStatusError( status ) )
return( status );
/* Add the server's key to the session */
status = getPrivateKey( &cryptContext, privKeysetName,
privKeyLabel, privKeyPassword );
if( cryptStatusOK( status ) )
{
/* Add the key (and associated certificate) to the server
session */
status = cryptSetAttribute( cryptSession,
CRYPT_SESSINFO_PRIVATEKEY, cryptContext );
cryptDestroyContext( cryptContext );
}
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
return( status );
}
/* Return the session object to the caller */
*serverSession = cryptSession;
return( CRYPT_OK );
}
Key/Certificate Examples
The next set of code samples use the utility functions above to create keys and
certificates. createSimplifiedCert creates a simplified certificate for a user with the
given name, optional email address, and optional server DNS name. Note that this
isn’t a standard CA-issued certificate but merely one intended for use as a convenient
key container:
int createSimplifiedCert( CRYPT_CERTIFICATE *userCertificate,
const CRYPT_CONTEXT certKey,
const char *certOwnerName,
const char *certOwnerEmail,
const char *certOwnerDNSName )
{
CRYPT_CERTIFICATE cryptCertificate;
int status;
/* Clear the return value */
*userCertificate = -1;
/* Create a certificate and mark it as a simlified certificate */
status = cryptCreateCert( &cryptCertificate, CRYPT_UNUSED,
CRYPT_CERTTYPE_CERTIFICATE );
if( cryptStatusError( status ) )
return( status );
status = cryptSetAttribute( cryptCertificate, CRYPT_CERTINFO_XYZZY,
1 );
if( cryptStatusError( status ) )
{
cryptDestroyCert( cryptCertificate );
return( status );
}