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: * p7env -- A command to create a pkcs7 enveloped data. 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: 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 fread(char *, size_t, size_t, FILE*); 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: static void michael@0: Usage(char *progName) michael@0: { michael@0: fprintf(stderr, michael@0: "Usage: %s -r recipient [-d dbdir] [-i input] [-o output]\n", michael@0: progName); michael@0: fprintf(stderr, "%-20s Nickname of cert to use for encryption\n", michael@0: "-r recipient"); michael@0: fprintf(stderr, "%-20s 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: struct recipient { michael@0: struct recipient *next; michael@0: char *nickname; michael@0: CERTCertificate *cert; michael@0: }; michael@0: michael@0: static void michael@0: EncryptOut(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: michael@0: static int michael@0: EncryptFile(FILE *outFile, FILE *inFile, struct recipient *recipients, michael@0: char *progName) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SEC_PKCS7EncoderContext *ecx; michael@0: struct recipient *rcpt; michael@0: SECStatus rv; michael@0: michael@0: if (outFile == NULL || inFile == NULL || recipients == NULL) michael@0: return -1; michael@0: michael@0: /* XXX Need a better way to handle that certUsage stuff! */ michael@0: /* XXX keysize? */ michael@0: cinfo = SEC_PKCS7CreateEnvelopedData (recipients->cert, michael@0: certUsageEmailRecipient, michael@0: NULL, SEC_OID_DES_EDE3_CBC, 0, michael@0: NULL, NULL); michael@0: if (cinfo == NULL) michael@0: return -1; michael@0: michael@0: for (rcpt = recipients->next; rcpt != NULL; rcpt = rcpt->next) { michael@0: rv = SEC_PKCS7AddRecipient (cinfo, rcpt->cert, certUsageEmailRecipient, michael@0: NULL); michael@0: if (rv != SECSuccess) { michael@0: SECU_PrintError(progName, "error adding recipient \"%s\"", michael@0: rcpt->nickname); michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: ecx = SEC_PKCS7EncoderStart (cinfo, EncryptOut, outFile, NULL); michael@0: if (ecx == NULL) michael@0: return -1; michael@0: michael@0: for (;;) { michael@0: char ibuf[1024]; michael@0: int nb; michael@0: michael@0: if (feof(inFile)) michael@0: break; michael@0: nb = fread(ibuf, 1, sizeof(ibuf), inFile); michael@0: if (nb == 0) { michael@0: if (ferror(inFile)) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: rv = SECFailure; michael@0: } michael@0: break; michael@0: } michael@0: rv = SEC_PKCS7EncoderUpdate(ecx, ibuf, nb); michael@0: if (rv != SECSuccess) michael@0: break; michael@0: } michael@0: michael@0: if (SEC_PKCS7EncoderFinish(ecx, NULL, NULL) != SECSuccess) michael@0: rv = SECFailure; michael@0: michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: michael@0: if (rv != SECSuccess) michael@0: return -1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: FILE *inFile, *outFile; michael@0: char *certName; michael@0: CERTCertDBHandle *certHandle; michael@0: struct recipient *recipients, *rcpt; 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: certName = NULL; michael@0: recipients = NULL; michael@0: rcpt = NULL; michael@0: michael@0: /* michael@0: * Parse command line arguments michael@0: * XXX This needs to be enhanced to allow selection of algorithms michael@0: * and key sizes (or to look up algorithms and key sizes for each michael@0: * recipient in the magic database). michael@0: */ michael@0: optstate = PL_CreateOptState(argc, argv, "d:i:o:r:"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case '?': michael@0: Usage(progName); michael@0: break; michael@0: michael@0: case 'd': michael@0: SECU_ConfigDirectory(optstate->value); michael@0: break; michael@0: michael@0: case 'i': michael@0: inFile = fopen(optstate->value, "r"); 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, "wb"); 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 'r': michael@0: if (rcpt == NULL) { michael@0: recipients = rcpt = PORT_Alloc (sizeof(struct recipient)); michael@0: } else { michael@0: rcpt->next = PORT_Alloc (sizeof(struct recipient)); michael@0: rcpt = rcpt->next; michael@0: } michael@0: if (rcpt == NULL) { michael@0: fprintf(stderr, "%s: unable to allocate recipient struct\n", michael@0: progName); michael@0: return -1; michael@0: } michael@0: rcpt->nickname = strdup(optstate->value); michael@0: rcpt->cert = NULL; michael@0: rcpt->next = NULL; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!recipients) Usage(progName); michael@0: michael@0: if (!inFile) inFile = stdin; michael@0: if (!outFile) outFile = stdout; michael@0: michael@0: /* Call the NSS 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: /* open cert database */ michael@0: certHandle = CERT_GetDefaultCertDB(); michael@0: if (certHandle == NULL) { michael@0: return -1; michael@0: } michael@0: michael@0: /* find certs */ michael@0: for (rcpt = recipients; rcpt != NULL; rcpt = rcpt->next) { michael@0: rcpt->cert = CERT_FindCertByNickname(certHandle, rcpt->nickname); michael@0: if (rcpt->cert == NULL) { michael@0: SECU_PrintError(progName, michael@0: "the cert for name \"%s\" not found in database", michael@0: rcpt->nickname); michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: if (EncryptFile(outFile, inFile, recipients, progName)) { michael@0: SECU_PrintError(progName, "problem encrypting data"); michael@0: return -1; michael@0: } michael@0: michael@0: return 0; michael@0: }