#if defined( P6_LINUX ) || defined( P6_SOLARIS )
#include <unistd.h>
#else
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include "p6sax2contenthandler.h"
#include "cconsolestream.h"
using namespace P6R;
namespace {
class CKmipExample2
{
public:
CKmipExample2()
: m_port(0), m_compatMask(0), m_pHostName(NULL)
{ }
~CKmipExample2()
{
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;
}
{
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;
}
{
*pInit = NULL;
*pKeystore = NULL;
if (
P6FAILED( err = (*pInit)->initialize( P6KEYSTORE_NOFLAGS, m_cpCrypto,
SH_SHA256, m_cpSignKey )))
{
if (NULL != (*pKeystore)) (*pKeystore)->release();
(*pKeystore) = NULL;
(*pInit)->release();
(*pInit) = NULL;
return err;
}
if (
P6FAILED( err = (*pInit)->openSigned( NULL, pKeystoreName )))
{
if (NULL != (*pKeystore)) (*pKeystore)->release();
(*pKeystore) = NULL;
(*pInit)->release();
(*pInit) = NULL;
return err;
}
if (
P6FAILED( err = keystoreAddRootCertFromFile( (*pKeystore), rootPEM )))
return err;
if (
P6FAILED( err = keystoreAddClientCertFromFile( (*pKeystore), 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;
}
{
pAsynchCorrelation->
pString = NULL;
pAsynchCorrelation->
length = 0;
err = pAsynch->
next( &buffer );
{
if (NULL == (pHandle =
new(std::nothrow)
P6UCHAR[buffer.
length + 2]))
return eNoMemory;
pHandle[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);
}
{
P6NCSTR buffer = { tempName, 300 };
if (NULL != pUniqueId)
{
err = pUniqueId->
reset();
{
match = -1;
if (buffer.
length ==
id.length)
{
if (
P6SUCCEEDED( err = m_cpStr->strcmp( buffer.
pString,
id.pString,
id.length, &match ))) {
}
}
}
}
}
{
*pKey = NULL;
pClient->
getObject( getParams, &managedObj, &resultCodes );
return verifyKeyProperties( managedObj, keyId, cipher, keyLength, pKey );
}
{
{
if (KMIP_OBJECT_SYMMETRICKEY == managedObj.
type)
{
printf("\nunexpected key type returned\n");
}
printf("\nunexpected key length returned\n");
}
if (NULL != (*pKey))
{
err = (*pKey)->getInfo( &keyClass, &keyType, &Guid, &keySize, &version );
printf("\nkey meta-data class not as expected\n");
}
printf("\nkey meta-data type not as expected\n");
}
printf("\nkey meta-data length not as exoected\n");
}
}
}
}
return err;
}
{
err = pClient->
poll( asynchCorrelation, NULL, cpQuery.
addressof() );
if (eNotSupported == err) err = pClient->
poll( asynchCorrelation, NULL, cpQuery.
addressof() );
err = cpQuery->next( &batchResponse );
{
#if defined( P6_LINUX ) || defined( P6_SOLARIS )
sleep( 2 );
#else
Sleep( 2000 );
#endif
err = cpQuery->next( &batchResponse );
maxWait++;
}
if (maxSleep < maxWait) return ePending;
cpQuery->reset();
return err;
}
{
setPreferences( &preferences,
P6TEXT(
"TC_32_12"), 1, 0, 0, 0, 60000, 30000, 30000, 120000, 2, 2 );
err = cpClient->initialize( (P6KMIPFLG_TRACE_MSGS | P6KMIPFLG_TRACE_FORMATKMIPXML), m_cpKeystore, preferences );
attributeList[0].
type = KMIP_ATTRIB_OBJECTTYPE;
attributeList[0].
index = 0;
attributeList[1].
type = KMIP_ATTRIB_NAME;
attributeList[1].
index = 0;
if (
P6FAILED( err = cpClient->locateObject( locateParams, cpUniqueId.
addressof(), &resultCodes ))) {
printf("\ncall to locateObject by name has failed %x\n", err );
}
printf("\nKMIP server returned an error when calling locateObject by Name\n");
}
{
cpClient->close();
return err;
}
{
err = cpPollResult->count( &number );
if (
P6SUCCEEDED( err = cpPollResult->next( &batchResponse )))
{
printf("\ncall to pollResult has failed\n" );
}
if (KMIP_OP_LOCATE != batchResponse.
type) {
printf("\ncall to pollResult type not as expected\n" );
}
printf("\ncall to pollResult batch request index not as expected\n" );
}
}
}
else {
printf("\nERROR: call to pollResult was not long enough for the operation to finish.\n" );
}
}
if (NULL != cpUniqueId)
{
bMatch = isEqualId( cpUniqueId, keyId );
if (!bMatch) {
printf("\nexpected key located by group--asynch client was not returned as expected\n" );
}
cpClient->setAsynchronousIndicator(
P6FALSE );
err = getSymmetricKey( cpClient, keyId, KMIP_AES, 128, cpKey.
addressof() );
cpClient->setAsynchronousIndicator(
P6TRUE );
}
attributeList[0].
type = KMIP_ATTRIB_OBJECTTYPE;
attributeList[0].
index = 0;
attributeList[1].
type = KMIP_ATTRIB_OBJECTGROUP;
attributeList[1].
index = 0;
printf("\ncall to locateObject by Group has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
{
{
err = cpPollResult->count( &number );
if (
P6SUCCEEDED( err = cpPollResult->next( &batchResponse )))
{
printf("\ncall to pollResult has failed\n" );
}
if (KMIP_OP_LOCATE != batchResponse.
type) {
printf("\ncall to pollResult type not as expected\n" );
}
printf("\ncall to pollResult batch request index not as expected\n" );
}
}
}
else {
printf("\nERROR: call to pollResult was not long enough for the operation to finish.\n" );
}
}
if (NULL != cpUniqueId)
{
bMatch = isIdContainedIn( cpUniqueId, keyId );
if (!bMatch) {
printf("\nexpected key located by group--asynch client was not returned as expected\n" );
}
cpClient->setAsynchronousIndicator(
P6FALSE );
err = getSymmetricKey( cpClient, keyId, KMIP_AES, 128, cpKey.
addressof() );
cpClient->setAsynchronousIndicator(
P6TRUE );
}
attributeList[0].
type = KMIP_ATTRIB_OBJECTTYPE;
attributeList[0].
index = 0;
attributeList[1].
type = KMIP_ATTRIB_NAME;
attributeList[1].
index = 0;
printf("\ncall to locateObject by Name has failed %x\n", err );
}
printf("\nKMIP server returned an error when calling locateObject by Name\n");
}
{
if (
P6FAILED( err = cpClient->cancel( asynchCorr, NULL, &resultCodes, &cancelResult ))) {
printf("\ncall to cancel has failed %x\n", err );
}
printf("\nKMIP server returned an error when calling cancel\n");
}
}
return cpClient->close();
}
{
setPreferences( &preferences,
P6TEXT(
"TC_32_12"), 0, 0, 0, 0, 60000, 30000, 30000, 120000, 2, 2 );
err = cpClient->initialize( (P6KMIPFLG_TRACE_MSGS | P6KMIPFLG_TRACE_FORMATKMIPXML), m_cpKeystore, preferences );
attributeList[0].
type = KMIP_ATTRIB_CRYPTOALGORITHM;
attributeList[0].
index = 0;
attributeList[1].
type = KMIP_ATTRIB_CRYPTOLENGTH;
attributeList[1].
index = 0;
attributeList[2].
type = KMIP_ATTRIB_NAME;
attributeList[2].
index = 0;
attributeList[3].
type = KMIP_ATTRIB_CRYPTOUSAGEMASK;
attributeList[3].
index = 0;
attributeList[4].
type = KMIP_ATTRIB_OBJECTGROUP;
attributeList[4].
index = 0;
if (
P6FAILED( err = cpClient->createKeyObject( keyParams, &newKey, &resultCodes ))) {
printf("\ncall to createKeyObject has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
{
err = extractUniqueId( newKey.
pUniqueId, &keyId );
}
err = asynchClient( keyId );
{
err = cpClient->destroyObject( keyId, NULL, &newKey.
pUniqueId, &resultCodes );
printf("\ndestroying key call has failed\n");
}
printf("\nKMIP server returned an error\n");
}
}
return cpClient->close();
}
{
CKmipExample2 example;
err = example.run(pDataStream);
}
}
}
int main(int argc,char *argv[])
{
{
{
KMIP_TC_32_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;
}