#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include "p6sax2contenthandler.h"
#include "cconsolestream.h"
using namespace P6R;
namespace {
class CKmipExample8
{
public:
CKmipExample8()
: m_port(0), m_compatMask(0), m_pHostName(NULL)
{ }
~CKmipExample8()
{
if (NULL != m_pHostName) m_cpStr->wstrfree( m_pHostName );
if (NULL != m_cpStoreInit) m_cpStoreInit->close();
}
protected:
};
{
{
if (
P6SUCCEEDED( err = cpKeyInit->initialize( P6CKF_NONE, m_cpRandom )))
{
{
}
}
}
return err;
}
{
*ppGenKeys = NULL;
{
if (
P6SUCCEEDED( err = cpGenKeys->initialize( P6GENKEY_NOFLAGS, m_cpRandom )))
{
}
}
return err;
}
{
*ppIface = NULL;
{
err = (*ppIface)->initialize( P6GENCERTS_NOFLAGS );
{
(*ppIface)->release();
*ppIface = NULL;
}
}
return err;
}
{
if (!pKeystore || !pszCertificateFile) return eInvalidArg;
if (
P6FAILED( err = m_cpStr->wstrlcat( certPath,
P6CNTOF(certPath), pszCertificateFile, NULL )))
return err;
err = cpSSLHelp->importTrustedRootCertFromPEMFile( certPath, NULL );
}
return err;
}
{
if (!pKeystore || !pszHostname || !pszPrivateKeyFile || !pszCertificateFile ) return eInvalidArg;
if (
P6FAILED( err = m_cpStr->wstrlcat( keyPath,
P6CNTOF(keyPath), pszPrivateKeyFile, NULL )))
return err;
if (
P6FAILED( err = m_cpStr->wstrlcat( certPath,
P6CNTOF(certPath), pszCertificateFile, NULL )))
return err;
err = cpSSLHelp->importCredentialsPEM(
P6TRUE, pszHostname, keyPath, certPath, NULL, NULL );
}
return err;
}
{
*ppInit = NULL;
*ppKeystore = NULL;
if (
P6FAILED( err = (*ppInit)->initialize( P6KEYSTORE_NOFLAGS, m_cpCrypto,
SH_SHA256, m_cpSignKey )))
{
if (NULL != (*ppKeystore)) (*ppKeystore)->release();
(*ppKeystore) = NULL;
(*ppInit)->release();
(*ppInit) = NULL;
return err;
}
if (
P6FAILED( err = (*ppInit)->openSigned( NULL, pKeystoreName )))
{
if (NULL != (*ppKeystore)) (*ppKeystore)->release();
(*ppKeystore) = NULL;
(*ppInit)->release();
(*ppInit) = NULL;
return err;
}
if (
P6FAILED( err = keystoreAddRootCertFromFile( (*ppKeystore), rootPEM )))
return err;
if (
P6FAILED( err = keystoreAddClientCertFromFile( (*ppKeystore), m_pHostName, privPEM, certPEM )))
return err;
return eOk;
}
{
{
if (
P6SUCCEEDED( err = cpSource->initialize( P6ENTROPY_HIGH )))
{
{
if (
P6SUCCEEDED( err = cpInit->initialize( P6RAND_NOFLAGS, cpSource ))) {
}
}
}
}
return err;
}
{
if (
P6FAILED( err = cpDir->initialize()))
return err;
if (
P6FAILED( err = getRNG( m_cpRandom.addressof())))
return err;
m_port = 5696;
m_compatMask = 0;
if (
P6FAILED( err = m_cpStr->wstrdup(
P6TEXT(
"fqdn.com"), &m_pHostName )))
return err;
{
err = cpGenKey->genSymmetricKey( m_cpSignKey.addressof(), 256,
P6FALSE );
}
}
if (
P6SUCCEEDED( err )) err = m_cpCrypto->setKey( cpKey );
if (
P6FAILED( err = m_cpStr->wstrlcat( dbPath,
P6CNTOF(dbPath),
P6TEXT(
"/db/KMIP12_keystore"), NULL )))
return err;
cpDir->unlink( dbPath );
if (
P6FAILED( err = m_cpStr->wstrlcat( dbPath,
P6CNTOF(dbPath),
P6TEXT(
"/db/KMIP12_keystore.sig"), NULL )))
return err;
cpDir->unlink( dbPath );
return createKeystore(
P6TEXT(
"KMIP12_keystore"),
P6TEXT(
"RootCert.pem"),
P6TEXT(
"ClientCert.pem"),
P6TEXT(
"ClientPrivate.pem"), m_cpStoreInit.addressof(), m_cpKeystore.addressof());
}
{
return eOk;
}
{
if (
P6FAILED( err = pClient->
setSSLOptions( NULL, (P6SSF_METHOD_TLS1 | P6SSF_SECURE_CLIENT | P6SSF_SECURE_CLIENT_AUTH | P6SSF_LOG_X509SUBJECTLOOKUPS | P6SSF_VRFY_DISABLEHOSTMATCH))))
return err;
return pClient->
open( m_pHostName, m_port, (bWithCredentials ? &credential : NULL));
}
{
err = pEnum->
next( &buffer );
{
if (NULL == (pGUID =
new (std::nothrow)
P6CHAR[buffer.
length + 2]))
return eNoMemory;
pGUID[0] = 0;
}
else err = eFail;
return err;
}
{
if (NULL != pUniqueId)
{
if (
P6FAILED( err = extractUniqueId( pUniqueId, &enumStr )))
return P6FALSE;
if (enumStr.
length ==
id.length)
{
err = m_cpStr->strcmp( enumStr.
pString,
id.pString,
id.length, &match );
}
}
return (0 == match);
}
{
{
err = pClient->
destroyObject( objectId, NULL, &pUniqueId, &resultCodes );
printf("\ncall to destroyObject has failed %x\n", err );
}
printf("\nKMIP server returned an error - destroy object\n");
}
if (NULL != pUniqueId)
{
bMatch = isEqualId( pUniqueId, objectId );
if (!bMatch) {
printf("\nKMIP server to destroy object with wrong object uniqueId\n");
}
if (bFree)
delete [] objectId.
pString;
}
if (KMIP_RESULT_SUCCESS != resultCodes.
resultStatus)
return eFail;
}
return err;
}
{
*pKey = NULL;
err = pClient->
getObject( getParams, &managedObj, &resultCodes );
printf("\ncall to getObject has failed %x\n", err );
return err;
}
printf("\nKMIP server returned an error - get object\n");
return eFail;
}
{
pubOrPriv = managedObj.
type;
switch ( pubOrPriv ) {
case KMIP_OBJECT_PUBLICKEY:
break;
case KMIP_OBJECT_PRIVATEKEY:
break;
default:
err = eNotImplemented;
break;
}
}
return err;
}
{
setPreferences( &preferences,
P6TEXT(
"TC_81_12"), 0, 0, 0, 0, 60000, 30000, 30000, 120000, 2, 2 );
err = cpClient->initialize( (P6KMIPFLG_TRACE_MSGS | P6KMIPFLG_TRACE_FORMATKMIPXML), m_cpKeystore, preferences );
commonList[0].
type = KMIP_ATTRIB_CRYPTOALGORITHM;
commonList[1].
type = KMIP_ATTRIB_CRYPTOLENGTH;
privateList[0].
type = KMIP_ATTRIB_NAME;
privateList[0].
index = 0;
privateList[1].
type = KMIP_ATTRIB_CRYPTOUSAGEMASK;
privateList[1].
index = 0;
publicList[0].
type = KMIP_ATTRIB_NAME;
publicList[1].
type = KMIP_ATTRIB_CRYPTOUSAGEMASK;
if (
P6FAILED( err = cpClient->createKeyPairObjects( keyParams, &privKey, &pubKey, &resultCodes ))) {
printf("\ncall to createKeyPairObjects has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
err = extractUniqueId( privKey.
pUniqueId, &privId );
}
err = extractUniqueId( pubKey.
pUniqueId, &pubId );
}
{
commonList[0].
type = KMIP_ATTRIB_OBJECTTYPE;
commonList[1].
type = KMIP_ATTRIB_LINK;
pUniqueId = NULL;
if (
P6FAILED(err = cpClient->locateObject( locateParams, &pUniqueId, &resultCodes ))) {
printf("\ncall to locateObject has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
if (NULL != pUniqueId) {
isEqualId( pUniqueId, pubId );
}
}
{
commonList[0].
type = KMIP_ATTRIB_OBJECTTYPE;
commonList[1].
type = KMIP_ATTRIB_LINK;
pUniqueId = NULL;
if (
P6FAILED( err = cpClient->locateObject( locateParams, &pUniqueId, &resultCodes ))) {
printf("\ncall to locateObject has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
if (NULL != pUniqueId) {
isEqualId( pUniqueId, pubId );
}
}
err = getAsymmetricKey( cpClient, pubId, pubOrPriv, &pKey1 );
err = getAsymmetricKey( cpClient, privId, pubOrPriv, &pKey2 );
destroyObject( cpClient, privId,
P6TRUE );
destroyObject( cpClient, pubId,
P6TRUE );
err = cpClient->close();
return err;
}
{
CKmipExample8 example;
err = example.run(pDataStream);
}
}
}
int main(int argc,char *argv[])
{
{
{
KMIP_TC_81_12( cpDataStream );
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n", err );
}
else printf( "ERROR: Failed to create CConsoleStream [ %x ]\n", err );
return err;
}