michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * p7content -- A command to display pkcs7 content. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: #include "secutil.h" michael@0: #include "plgetopt.h" michael@0: #include "secpkcs7.h" michael@0: #include "cert.h" michael@0: #include "certdb.h" michael@0: #include "nss.h" michael@0: #include "pk11pub.h" michael@0: michael@0: #if defined(XP_UNIX) michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #if (defined(XP_WIN) && !defined(WIN32)) || (defined(__sun) && !defined(SVR4)) michael@0: extern int fwrite(char *, size_t, size_t, FILE*); michael@0: extern int fprintf(FILE *, char *, ...); michael@0: #endif michael@0: michael@0: michael@0: michael@0: static void michael@0: Usage(char *progName) michael@0: { michael@0: fprintf(stderr, michael@0: "Usage: %s [-d dbdir] [-i input] [-o output]\n", michael@0: progName); michael@0: fprintf(stderr, michael@0: "%-20s Key/Cert database directory (default is ~/.netscape)\n", michael@0: "-d dbdir"); michael@0: fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", michael@0: "-i input"); michael@0: fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", michael@0: "-o output"); michael@0: exit(-1); michael@0: } michael@0: michael@0: static PRBool saw_content; michael@0: static secuPWData pwdata = { PW_NONE, 0 }; michael@0: michael@0: static void michael@0: PrintBytes(void *arg, const char *buf, unsigned long len) michael@0: { michael@0: FILE *out; michael@0: michael@0: out = arg; michael@0: fwrite (buf, len, 1, out); michael@0: michael@0: saw_content = PR_TRUE; michael@0: } michael@0: michael@0: /* michael@0: * XXX Someday we may want to do real policy stuff here. This allows michael@0: * anything to be decrypted, which is okay for a test program but does michael@0: * not set an example of how a real client with a real policy would michael@0: * need to do it. michael@0: */ michael@0: static PRBool michael@0: decryption_allowed(SECAlgorithmID *algid, PK11SymKey *key) michael@0: { michael@0: return PR_TRUE; michael@0: } michael@0: michael@0: int michael@0: DecodeAndPrintFile(FILE *out, PRFileDesc *in, char *progName) michael@0: { michael@0: SECItem derdata; michael@0: SEC_PKCS7ContentInfo *cinfo = NULL; michael@0: SEC_PKCS7DecoderContext *dcx; michael@0: michael@0: if (SECU_ReadDERFromFile(&derdata, in, PR_FALSE, PR_FALSE)) { michael@0: SECU_PrintError(progName, "error converting der"); michael@0: return -1; michael@0: } michael@0: michael@0: fprintf(out, michael@0: "Content printed between bars (newline added before second bar):"); michael@0: fprintf(out, "\n---------------------------------------------\n"); michael@0: michael@0: saw_content = PR_FALSE; michael@0: dcx = SEC_PKCS7DecoderStart(PrintBytes, out, NULL, &pwdata, michael@0: NULL, NULL, decryption_allowed); michael@0: if (dcx != NULL) { michael@0: #if 0 /* Test that decoder works when data is really streaming in. */ michael@0: { michael@0: unsigned long i; michael@0: for (i = 0; i < derdata.len; i++) michael@0: SEC_PKCS7DecoderUpdate(dcx, derdata.data + i, 1); michael@0: } michael@0: #else michael@0: SEC_PKCS7DecoderUpdate(dcx, (char *)derdata.data, derdata.len); michael@0: #endif michael@0: cinfo = SEC_PKCS7DecoderFinish(dcx); michael@0: } michael@0: michael@0: fprintf(out, "\n---------------------------------------------\n"); michael@0: michael@0: if (cinfo == NULL) michael@0: return -1; michael@0: michael@0: fprintf(out, "Content was%s encrypted.\n", michael@0: SEC_PKCS7ContentIsEncrypted(cinfo) ? "" : " not"); michael@0: michael@0: if (SEC_PKCS7ContentIsSigned(cinfo)) { michael@0: char *signer_cname, *signer_ename; michael@0: SECItem *signing_time; michael@0: michael@0: if (saw_content) { michael@0: fprintf(out, "Signature is "); michael@0: PORT_SetError(0); michael@0: if (SEC_PKCS7VerifySignature(cinfo, certUsageEmailSigner, PR_FALSE)) michael@0: fprintf(out, "valid.\n"); michael@0: else michael@0: fprintf(out, "invalid (Reason: %s).\n", michael@0: SECU_Strerror(PORT_GetError())); michael@0: } else { michael@0: fprintf(out, michael@0: "Content is detached; signature cannot be verified.\n"); michael@0: } michael@0: michael@0: signer_cname = SEC_PKCS7GetSignerCommonName(cinfo); michael@0: if (signer_cname != NULL) { michael@0: fprintf(out, "The signer's common name is %s\n", signer_cname); michael@0: PORT_Free(signer_cname); michael@0: } else { michael@0: fprintf(out, "No signer common name.\n"); michael@0: } michael@0: michael@0: signer_ename = SEC_PKCS7GetSignerEmailAddress(cinfo); michael@0: if (signer_ename != NULL) { michael@0: fprintf(out, "The signer's email address is %s\n", signer_ename); michael@0: PORT_Free(signer_ename); michael@0: } else { michael@0: fprintf(out, "No signer email address.\n"); michael@0: } michael@0: michael@0: signing_time = SEC_PKCS7GetSigningTime(cinfo); michael@0: if (signing_time != NULL) { michael@0: SECU_PrintTimeChoice(out, signing_time, "Signing time", 0); michael@0: } else { michael@0: fprintf(out, "No signing time included.\n"); michael@0: } michael@0: } else { michael@0: fprintf(out, "Content was not signed.\n"); michael@0: } michael@0: michael@0: fprintf(out, "There were%s certs or crls included.\n", michael@0: SEC_PKCS7ContainsCertsOrCrls(cinfo) ? "" : " no"); michael@0: michael@0: SEC_PKCS7DestroyContentInfo(cinfo); michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * Print the contents of a PKCS7 message, indicating signatures, etc. michael@0: */ michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: FILE *outFile; michael@0: PRFileDesc *inFile; michael@0: PLOptState *optstate; michael@0: PLOptStatus status; michael@0: SECStatus rv; michael@0: michael@0: progName = strrchr(argv[0], '/'); michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: inFile = NULL; michael@0: outFile = NULL; michael@0: michael@0: /* michael@0: * Parse command line arguments michael@0: */ michael@0: optstate = PL_CreateOptState(argc, argv, "d:i:o:p:f:"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case 'd': michael@0: SECU_ConfigDirectory(optstate->value); michael@0: break; michael@0: michael@0: case 'i': michael@0: inFile = PR_Open(optstate->value, PR_RDONLY, 0); michael@0: if (!inFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for reading\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'o': michael@0: outFile = fopen(optstate->value, "w"); michael@0: if (!outFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for writing\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'p': michael@0: pwdata.source = PW_PLAINTEXT; michael@0: pwdata.data = PORT_Strdup (optstate->value); michael@0: break; michael@0: michael@0: case 'f': michael@0: pwdata.source = PW_FROMFILE; michael@0: pwdata.data = PORT_Strdup (optstate->value); michael@0: break; michael@0: michael@0: default: michael@0: Usage(progName); michael@0: break; michael@0: } michael@0: } michael@0: if (status == PL_OPT_BAD) michael@0: Usage(progName); michael@0: michael@0: if (!inFile) inFile = PR_STDIN; michael@0: if (!outFile) outFile = stdout; michael@0: michael@0: /* Call the initialization routines */ michael@0: PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); michael@0: rv = NSS_Init(SECU_ConfigDirectory(NULL)); michael@0: if (rv != SECSuccess) { michael@0: SECU_PrintPRandOSError(progName); michael@0: return -1; michael@0: } michael@0: michael@0: PK11_SetPasswordFunc(SECU_GetModulePassword); michael@0: michael@0: if (DecodeAndPrintFile(outFile, inFile, progName)) { michael@0: SECU_PrintError(progName, "problem decoding data"); michael@0: return -1; michael@0: } michael@0: michael@0: if (NSS_Shutdown() != SECSuccess) { michael@0: exit(1); michael@0: } michael@0: michael@0: return 0; michael@0: }