X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/clxutils/p12/p12Decode.cpp diff --git a/SecurityTests/clxutils/p12/p12Decode.cpp b/SecurityTests/clxutils/p12/p12Decode.cpp new file mode 100644 index 00000000..b690f0e4 --- /dev/null +++ b/SecurityTests/clxutils/p12/p12Decode.cpp @@ -0,0 +1,416 @@ +/* + * Decode P12 PFX using either C++ P12Coder or public + * C API (both from SecurityNssPkcs12) + */ + +#include +#include +#include +#include +#include +#include +#include "p12GetPassKey.h" +#include "p12.h" + +/* use this option to debug the P12Coder class directly */ +#define P12_DECODE_VIA_CPP 0 + +/* + * Print CFString - stored as unicode, we get the C string, + * print it plus newline + */ +static void printUcStr( + CFStringRef cfstr) +{ + CFIndex len = CFStringGetLength(cfstr) + 1; + char *outStr = (char *)malloc(len); + if(CFStringGetCString(cfstr, outStr, len, kCFStringEncodingASCII)) { + printf("%s\n", outStr); + } + else { + printf("***Error converting from unicode to ASCII\n"); + } + free(outStr); +} + +static void printDataAsHex( + CFDataRef d, + unsigned maxToPrint = 0) // optional, 0 means print it all +{ + unsigned i; + bool more = false; + uint32 len = CFDataGetLength(d); + const uint8 *cp = CFDataGetBytePtr(d); + + if((maxToPrint != 0) && (len > maxToPrint)) { + len = maxToPrint; + more = true; + } + for(i=0; i\n"); + } +} + +OSStatus p12Decode( + const CSSM_DATA &pfx, + CSSM_CSP_HANDLE cspHand, + CFStringRef pwd, + bool verbose, + unsigned loops) +{ + OSStatus ourRtn; + + for(unsigned loop=0; loopcertData(); + printCert(certData.Data, certData.Length, + CSSM_FALSE); + + } + printf("\n"); + } + + unsigned numCrls = coder.numCrls(); + printf("%u crls found\n", numCrls); + for(i=0; icrlData(); + printCrl(crlData.Data, crlData.Length, CSSM_FALSE); + + } + printf("\n"); + } + + unsigned numKeys = coder.numKeys(); + printf("%u keys found\n", numKeys); + for(i=0; ikey(); + CSSM_KEYHEADER &hdr = ckey->KeyHeader; + printf(" Key Alg : "); + printAlgAsString(hdr.AlgorithmId); + printf(" Key Size : %u bits\n", + (unsigned)hdr.LogicalKeySizeInBits); + printf("\n"); + } + + unsigned numBlobs = coder.numOpaqueBlobs(); + printf("%u blobs found\n", numBlobs); + } + catch(...) { + printf("***exception extracting fields\n"); + ourRtn = 1; + } + } + if(loops > 1) { + fpurge(stdin); + printf("CR to continue: "); + getchar(); + } + if(ourRtn) { + return ourRtn; + } + } + return ourRtn; +} + +#else /* P12_DECODE_VIA_CPP */ + +/* Normal decode using public API in SecPkcs12.h */ + +/* common bag attrs - friendlyName, localKeyId */ +static void printBagAttrs( + CFStringRef friendlyName, + CFDataRef localKeyId) +{ + if(friendlyName) { + printf(" friendlyName : "); + printUcStr(friendlyName); + } + + if(localKeyId) { + printf(" localKeyId : "); + printDataAsHex(localKeyId, 20); + } + if((friendlyName == NULL) && (localKeyId == NULL)) { + printf(" \n"); + } +} + +/* release attrs if present */ +static void releaseAttrs( + CFStringRef friendlyName, + CFDataRef localKeyId, + SecPkcs12AttrsRef attrs) +{ + if(friendlyName) { + CFRelease(friendlyName); + } + if(localKeyId) { + CFRelease(localKeyId); + } + if(attrs) { + SecPkcs12AttrsRelease(attrs); + } +} + +static void printOsError( + const char *op, + OSStatus ortn) +{ + /* may want to parse out CSSM errors */ + cssmPerror(op, ortn); + printf("\n"); +} + +/* Sec calls all return 1 - not the fault of SecNssPkcs12 */ +#define GET_CERTS_WORKING 1 + +OSStatus p12Decode( + const CSSM_DATA &pfx, + CSSM_CSP_HANDLE cspHand, + CFStringRef pwd, // explicit passphrase, mutually exclusive with... + bool usePassKey, // use SECURE_PASSPHRASE key + bool verbose, + unsigned loops) +{ + OSStatus ortn; + CSSM_KEY passKey; + CSSM_KEY_PTR passKeyPtr = NULL; + + CFDataRef cfd = CFDataCreate(NULL, pfx.Data, pfx.Length); + if(usePassKey) { + ortn = p12GetPassKey(cspHand, GPK_Decode, true, &passKey); + if(ortn) { + return ortn; + } + passKeyPtr = &passKey; + } + for(unsigned loop=0; loopKeyHeader; + printf(" Key Alg : "); + printAlgAsString(hdr.AlgorithmId); + printf(" Key Size : %u bits\n", + (unsigned)hdr.LogicalKeySizeInBits); + printf("\n"); + releaseAttrs(fname, keyId, NULL); + } + + CFIndex numBlobs; + SecPkcs12OpaqueBlobCount(coder, &numBlobs); + if(numBlobs != 0) { + printf("%ld blobs found\n", numBlobs); + } + + /* this should free all memory allocated in the decode */ + SecPkcs12CoderRelease(coder); + + if(loops > 1) { + fpurge(stdin); + printf("CR to continue: "); + getchar(); + } + } + return 0; +} + + +#endif /* P12_DECODE_VIA_CPP */