#ifdef WIN32
# include <windows.h>
#endif
#include <stdio.h>
#include "npki.h"
#include "pkierr.h"
NWRCODE GetServerCertificate(void)
{
NWRCODE ccode = PKI_SUCCESS;
NPKIContext myPKI = NPKI_INVALID_CONTEXT;
nuint32 numberOfServers = 0;
nuint32 flags = 0;
nuint32 objectCertSize = 0;
nuint8 const *objectCert = NULL;
nuint32 chainCertSize = 0;
nuint8 const *chainCert = NULL;
nuint8 const *wrappedKey = NULL;
nuint32 wrappedKeySize = 0;
nuint32 numberOfChainCerts = 0;
nuint32 index;
unicode myTree[] = {'T','E','S','T',0};
unicode myUser[] = {'A','d','m','i','n','.','n','o','v','e','l','l',0};
char password[] = {'t','e','s','t',0};
char* startIPAddress = "192.168.0.2";
unicode certName[] = {'c','e','r','t','i','f','i','c','a','t','e',0};
unicode serverDN[] = {'T','e','s','t','5','1','.','n','o','v','e','l','l',0};
ccode = NPKICreateContext(&myPKI);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKISetTreeName(myPKI, myTree);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKIConnectToIPAddress(myPKI, 0, 0, startIPAddress, NULL, NULL);
ccode = NPKIDSLogin(myPKI, myUser, password);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKIGetServerCertificateStatus(myPKI, serverDN, certName, &flags);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
if (flags != KMO_CERTIFICATE_PRESENT)
{
goto ERR_EXIT;
}
ccode = NPKIGetServerCertificates
(
myPKI,
serverDN,
certName,
PKI_OBJECT_KEY_CERTIFICATE | PKI_CHAIN_CERTIFICATE,
&objectCertSize,
&objectCert,
&numberOfChainCerts,
NULL,
NULL,
NULL
);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
{
FILE *stream = NULL;
size_t size = 0;
stream = fopen("ServerCert.cer", "wb");
if (stream != NULL)
{
size = fwrite(objectCert, sizeof(char), objectCertSize, stream);
fclose(stream);
}
}
for (index = 0; index < numberOfChainCerts; index++)
{
ccode = NPKIChainCertInfo
(
myPKI,
index,
&chainCertSize,
&chainCert,
NULL,
NULL
);
if (ccode != PKI_SUCCESS)
{
continue;
}
}
ERR_EXIT:
NPKIDSLogout(myPKI);
if (myPKI != NPKI_INVALID_CONTEXT)
NPKIFreeContext(myPKI);
return ccode;
}