#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include "cconsolestream.h"
using namespace P6R;
namespace {
#define ONEYEAR (60*60*24*365) // seconds in one year
class CKmipExample11
{
public:
CKmipExample11()
: m_port(0), m_compatMask(0), m_pHostName(NULL)
{ }
~CKmipExample11()
{
if (NULL != m_pHostName) m_cpStr->wstrfree( m_pHostName );
if (NULL != m_cpStoreInit) m_cpStoreInit->close();
}
protected:
};
{
{
{
{
}
}
}
return err;
}
{
}
return err;
}
{
if (NULL == pwszFilename) return eInvalidArg;
{
{
{
{
{
if ( pBuffer )
{
{
{
keyBuffer.
length = cBytesRead;
err = createPKCS8Key( keyBuffer, keySize, ppNewKey );
}
}
}
else err = eIoError;
delete [] pBuffer;
}
else err = eNoMemory;
}
else {
err = eTooSmall;
}
}
}
}
return err;
}
{
*ppGenKeys = NULL;
{
{
}
}
return err;
}
{
*ppIface = NULL;
{
{
(*ppIface)->release();
*ppIface = NULL;
}
}
return err;
}
{
if (
P6FAILED( err = m_cpStr->wstrlcat( confPath,
P6CNTOF(confPath), pFilePath, NULL )))
return err;
{
{
err = pKeystore->
setCertificate( pNamespace, szHash, cpNewCert, NULL );
}
}
return err;
}
{
if (
P6FAILED( err = m_cpStr->wstrlcat( confPath,
P6CNTOF(confPath), pFilePath, NULL )))
return err;
{
err = pKeystore->
setCertificate( pNamespace, pName, cpNewCert, NULL );
}
return err;
}
{
if (
P6FAILED( err = m_cpStr->wstrlcat( confPath,
P6CNTOF(confPath), pFilePath, NULL )))
return err;
{
err = pKeystore->
setKey( pNamespace, pName, cpNewKey, NULL, 0, NULL );
}
return err;
}
{
*ppInit = NULL;
*ppKeystore = NULL;
{
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 = saveRootCertInStore( (*ppKeystore),
P6TEXT(
"p6r.trustedroot"), rootPEM )))
return err;
if (
P6FAILED( err = saveClientCertInStore( (*ppKeystore),
P6TEXT(
"p6r.clientauth"), m_pHostName, certPEM )))
return err;
if (
P6FAILED( err = savePrivKeyInStore( (*ppKeystore),
P6TEXT(
"p6r.clientauth"), m_pHostName, privPEM, privSize )))
return err;
return eOk;
}
{
{
{
{
}
}
}
}
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;
{
}
}
if (
P6SUCCEEDED( err )) err = m_cpCrypto->setKey( cpKey );
if (
P6FAILED( err = m_cpStr->wstrlcat( dbPath,
P6CNTOF(dbPath),
P6TEXT(
"/db/KMIP12_keystore"), NULL )))
return err;
if (
P6FAILED( err = m_cpStr->wstrlcat( dbPath,
P6CNTOF(dbPath),
P6TEXT(
"/db/KMIP12_keystore.sig"), NULL )))
return err;
return createKeystore(
P6TEXT(
"KMIP12_keystore"),
P6TEXT(
"RootCert.pem"),
P6TEXT(
"ClientCert.pem"),
P6TEXT(
"ClientPrivate.pem"), 2048, 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, 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");
}
bMatch = isEqualId( pUniqueId, objectId );
if (!bMatch) {
printf("\nKMIP server to destroy object with wrong object uniqueId\n");
}
if (bFree)
delete [] objectId.
pString;
}
return err;
}
{
printf("\ncall to getAttributeList has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
{
bMatch = isEqualId( attribNames.
pUniqueId, objId );
count = 0;
{
printf(
"\nattributeList [%s] %d\n", enumStr.
pString, enumStr.
length );
}
}
return err;
}
{
printf("\ncall to getAttributes has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
{
number = 0;
{
printf( "\nlist the types of attributes returned: %d\n", attribType );
}
}
return err;
}
{
attribNames[0].
pString =
"Certificate Identifier";
attribNames[1].
pString =
"Certificate Issuer";
attribNames[2].
pString =
"Certificate Subject";
attribNames[3].
pString =
"Certificate Type";
attribNames[4].
pString =
"Digital Signature Algorithm";
attribNames[5].
pString =
"Cryptographic Length";
attribNames[6].
pString =
"Certificate Length";
attribNames[7].
pString =
"X.509 Certificate Identifier";
attribNames[8].
pString =
"X.509 Certificate Issuer";
attribNames[9].
pString =
"X.509 Certificate Subject";
printf("\ncall to getAttributes has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
{
{
switch( attribType ) {
break;
{
}
}
if (NULL != pName) { delete [] pName; pName = NULL; }
break;
break;
break;
break;
break;
break;
break;
break;
break;
default:
break;
}
}
}
return err;
}
{
setPreferences( &preferences,
P6TEXT(
"ENUMERATOR_1"), 0, 0, 0, 0, 60000, 30000, 30000, 120000, 2, 2 );
if (
P6FAILED( err = createSession( cpClient )))
return err;
if (
P6FAILED( err = getGenCerts( &pGenCerts )))
return err;
{
}
if (
P6FAILED( err = pGenCerts->
genCertificate( gKMIP_11_Issuer, 6, pPubKey, pPrvKey, NULL, ONEYEAR, NULL, 0, &pSelfCert ))) {
return err;
}
attributeList[0].
index = 0;
attributeList[1].
index = 0;
printf("\ncall to registerCertificateObject for cert has failed %x\n", err );
}
printf("\nKMIP server returned an error\n");
}
err = extractUniqueId( certObj.
pUniqueId, &certId );
}
if (
P6FAILED( err = getAttributeList( cpClient, certId ))) {
printf("\ncall to getAttributeList for cert has failed %x\n", err );
}
if (
P6FAILED( err = getServerCertAttributes( cpClient, certId ))) {
printf("\ncall to getServerCert Attributes for cert has failed %x\n", err );
}
if (
P6FAILED( err = getAllAttributes( cpClient, certId ))) {
printf("\ncall to get All Attributes for cert has failed %x\n", err );
}
destroyObject( cpClient, certId,
P6TRUE );
return err;
}
{
CKmipExample11 example;
err = example.run(pDataStream);
}
}
}
int main(int argc,char *argv[])
{
{
{
KMIP_Attributes( cpDataStream );
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n", err );
}
else printf( "ERROR: Failed to create CConsoleStream [ %x ]\n", err );
return err;
}