X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/clxutils/certChain/certChain.cpp?ds=sidebyside diff --git a/SecurityTests/clxutils/certChain/certChain.cpp b/SecurityTests/clxutils/certChain/certChain.cpp new file mode 100644 index 00000000..92bf38e4 --- /dev/null +++ b/SecurityTests/clxutils/certChain/certChain.cpp @@ -0,0 +1,234 @@ +/* + * Given a cert, produce a complete ordered cert chain back to a root. + * Intermediate certs can be in any user keychain. + */ +#include +#include +#include /* private */ +#include /* private */ +#include +#include /* private */ + +static void usage(char **argv) +{ + printf("Usage:\n"); + printf(" %s certFileName [d(isable intermediates) [f filebase] [n(o cert dump)]\n", argv[0]); + exit(1); +} + +int main(int argc, char **argv) +{ + unsigned char *certData = NULL; // subject cert, raw data + unsigned certDataLen = 0; + OSStatus ortn; + SecTrustRef secTrust = NULL; + CFMutableArrayRef subjCerts = NULL; + SecPolicyRef policy = NULL; + SecPolicySearchRef policySearch = NULL; + SecTrustResultType secTrustResult; + CSSM_RETURN crtn = CSSM_OK; + CSSM_TP_APPLE_EVIDENCE_INFO *dummyEv; // not used + CFArrayRef certChain = NULL; // constructed chain + CFIndex numCerts; + bool disableLocalIntermediates = false; + char *fileBase = NULL; + bool enableCertDump = true; + + if(argc < 2) { + usage(argv); + } + if(readFile(argv[1], &certData, &certDataLen)) { + printf("***Error reading cert from %s. Aborting.\n", argv[1]); + exit(1); + } + for(int arg=2; arg 0)) { + char fname[200]; + sprintf(fname, "%s_%u", fileBase, i); + if(writeFile(fname, cd.Data, cd.Length)) { + printf("***Error writing to %s\n", fname); + } + else { + printf("...write %lu bytes to %s\n", cd.Length, fname); + } + } + } + } +errOut: + if(certData) { + /* mallocds by readFile() */ + free(certData); + } + if(secTrust) { + CFRelease(secTrust); + } + if(subjCerts) { + CFRelease(subjCerts); + } + if(policy) { + CFRelease(policy); + } + if(policySearch) { + CFRelease(policySearch); + } + return (int)ortn; +}