#ifdef WIN32
# include <windows.h>
#endif
#include <stdio.h>
#include "npki.h"
#include "pkierr.h"
NWRCODE StoreServerCerts(void)
{
NWRCODE ccode = PKI_SUCCESS;
NPKIContext myPKI = NPKI_INVALID_CONTEXT;
unicode const *organizationalCADN = NULL;
nuint8 *cert = NULL;
nuint32 certSize = 0;
nuint8 *CAcert = NULL;
nuint32 CAcertSize = 0;
nuint32 finished = 0;
nuint32 numberOfCertsInList = 0;
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 serverDN[] = {'T','e','s','t','5','1','.','n','o','v','e','l','l',0};
unicode certName[] = {'E','x','t','e','r','n','a','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;
}
NPKICertificateList(myPKI, NULL, 0, PKI_CLEAR_CERTS, NULL);
{
FILE *stream = NULL;
size_t size = 0;
stream = fopen("ExternalCert.cer", "rb");
if (stream != NULL)
{
fseek(stream, 0L, SEEK_END);
size = ftell(stream);
fseek(stream, 0L, SEEK_SET);
if ((cert = (nuint8 *)malloc(size)) == NULL)
{
ccode = PKI_E_INSUFFICIENT_MEMORY;
goto ERR_EXIT;
}
certSize = fread(cert, sizeof(char), size, stream);
fclose(stream);
}
else
{
ccode = PKI_E_FILE_OPEN;
goto ERR_EXIT;
}
stream = fopen("CAcert.cer", "rb");
if (stream != NULL)
{
fseek(stream, 0L, SEEK_END);
size = ftell(stream);
fseek(stream, 0L, SEEK_SET);
if ((CAcert = (nuint8 *)malloc(size)) == NULL)
{
ccode = PKI_E_INSUFFICIENT_MEMORY;
goto ERR_EXIT;
}
CAcertSize = fread(CAcert, sizeof(char), size, stream);
fclose(stream);
}
else
{
ccode = PKI_E_FILE_OPEN;
goto ERR_EXIT;
}
}
ccode = NPKICertificateList(myPKI, cert, certSize, PKI_ADD_CERT, NULL);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKICertificateList(myPKI, CAcert, CAcertSize, PKI_ADD_CERT, NULL);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKICertificateList(myPKI, NULL, 0, PKI_SORT_LIST, &numberOfCertsInList);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ccode = NPKIStoreServerCertificatesFromCertificateList
(
myPKI,
serverDN,
certName,
0,
numberOfCertsInList,
NULL,
NULL
);
if (ccode != PKI_SUCCESS)
{
goto ERR_EXIT;
}
ERR_EXIT:
if (cert != NULL)
{
free(cert);
}
if (CAcert != NULL)
{
free(CAcert);
}
NPKIDSLogout(myPKI);
if (myPKI != NPKI_INVALID_CONTEXT)
NPKIFreeContext(myPKI);
return ccode;
}