#include <security_pkcs12/pkcs12Coder.h>
#include <stdlib.h>
#include <stdio.h>
#include <Security/cssmtype.h>
#include <security_cdsa_utils/cuPrintCert.h>
#include <security_cdsa_utils/cuFileIo.h>
int p12Reencode(
const CSSM_DATA &pfx,
CSSM_CSP_HANDLE cspHand,
CFStringRef pwd, bool verbose,
unsigned loops)
{
int ourRtn;
for(unsigned loop=0; loop<loops; loop++) {
{
P12Coder coder;
CFDataRef cfd = CFDataCreate(NULL, pfx.Data, pfx.Length);
ourRtn = 0;
printf("...decoding...\n");
try {
coder.setCsp(cspHand);
coder.setMacPassPhrase(pwd);
coder.decode(cfd);
}
catch(...) {
printf("***decode error\n");
return 1;
}
CFRelease(cfd);
printf("...encoding...\n");
CFDataRef encPfx;
try {
coder.encode(&encPfx);
}
catch(...) {
printf("***encode error\n");
return 1;
}
writeFile("encoded.p12", CFDataGetBytePtr(encPfx),
CFDataGetLength(encPfx));
printf("...wrote %u bytes to encoded.p12\n",
(unsigned)CFDataGetLength(encPfx));
CFRelease(encPfx);
}
if(loops > 1) {
fpurge(stdin);
printf("CR to continue: ");
getchar();
}
if(ourRtn) {
return ourRtn;
}
}
return ourRtn;
}