Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
p7sign.c File Reference
#include "nspr.h"
#include "plgetopt.h"
#include "secutil.h"
#include "secpkcs7.h"
#include "cert.h"
#include "certdb.h"
#include "sechash.h"
#include "nss.h"
#include "pk11func.h"
#include <stdio.h>
#include <string.h>

Go to the source code of this file.


char * MyPK11PasswordFunc (PK11SlotInfo *slot, PRBool retry, void *arg)
static void Usage (char *progName)
static void SignOut (void *arg, const char *buf, unsigned long len)
static int CreateDigest (SECItem *data, char *digestdata, unsigned int *len, unsigned int maxlen)
static int SignFile (FILE *outFile, PRFileDesc *inFile, CERTCertificate *cert, PRBool encapsulated)
int main (int argc, char **argv)
 The Xalan testcases app.


char * KeyDbPassword = 0

Function Documentation

static int CreateDigest ( SECItem *  data,
char *  digestdata,
unsigned int len,
unsigned int  maxlen 
) [static]

Definition at line 111 of file p7sign.c.

    const SECHashObject *hashObj;
    void *hashcx;

    /* XXX probably want to extend interface to allow other hash algorithms */
    hashObj = HASH_GetHashObject(HASH_AlgSHA1);

    hashcx = (* hashObj->create)();
    if (hashcx == NULL)
       return -1;

    (* hashObj->begin)(hashcx);
    (* hashObj->update)(hashcx, data->data, data->len);
    (* hashObj->end)(hashcx, (unsigned char *)digestdata, len, maxlen);
    (* hashObj->destroy)(hashcx, PR_TRUE);
    return 0;

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 

The Xalan testcases app.

Definition at line 188 of file p7sign.c.

    char *progName;
    FILE *outFile;
    PRFileDesc *inFile;
    char *keyName;
    CERTCertDBHandle *certHandle;
    CERTCertificate *cert;
    PRBool encapsulated = PR_FALSE;
    PLOptState *optstate;
    PLOptStatus status;
    SECStatus rv;

    progName = strrchr(argv[0], '/');
    progName = progName ? progName+1 : argv[0];

    inFile = NULL;
    outFile = NULL;
    keyName = NULL;

     * Parse command line arguments
    optstate = PL_CreateOptState(argc, argv, "ed:k:i:o:p:");
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
       switch (optstate->option) {
         case '?':

         case 'e':
           /* create a message with the signed content encapsulated */
           encapsulated = PR_TRUE;

         case 'd':

         case 'i':
           inFile = PR_Open(optstate->value, PR_RDONLY, 0);
           if (!inFile) {
              fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
                     progName, optstate->value);
              return -1;

         case 'k':
           keyName = strdup(optstate->value);

         case 'o':
           outFile = fopen(optstate->value, "w");
           if (!outFile) {
              fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
                     progName, optstate->value);
              return -1;
         case 'p':
            KeyDbPassword = strdup (optstate->value);

    if (!keyName) Usage(progName);

    if (!inFile) inFile = PR_STDIN;
    if (!outFile) outFile = stdout;

    /* Call the initialization routines */
    rv = NSS_Init(SECU_ConfigDirectory(NULL));
    if (rv != SECSuccess) {
       return -1;

    PK11_SetPasswordFunc (MyPK11PasswordFunc);

    /* open cert database */
    certHandle = CERT_GetDefaultCertDB();
    if (certHandle == NULL) {
       return -1;

    /* find cert */
    cert = CERT_FindCertByNickname(certHandle, keyName);
    if (cert == NULL) {
                      "the corresponding cert for key \"%s\" does not exist",
       return -1;

    if (SignFile(outFile, inFile, cert, encapsulated)) {
       SECU_PrintError(progName, "problem signing data");
       return -1;

    if (NSS_Shutdown() != SECSuccess) {

    return 0;

Here is the call graph for this function:

char* MyPK11PasswordFunc ( PK11SlotInfo *  slot,
PRBool  retry,
void arg 

Definition at line 70 of file p7sign.c.

    char *ret=0;

    if (retry == PR_TRUE)
        return NULL;
    ret = PL_strdup (KeyDbPassword);
    return ret;

Here is the caller graph for this function:

static int SignFile ( FILE outFile,
PRFileDesc inFile,
CERTCertificate *  cert,
PRBool  encapsulated 
) [static]

Definition at line 131 of file p7sign.c.

    char digestdata[32];
    unsigned int len;
    SECItem digest, data2sign;
    SEC_PKCS7ContentInfo *cinfo;
    SECStatus rv;

    if (outFile == NULL || inFile == NULL || cert == NULL)
       return -1;

    /* suck the file in */
       if (SECU_ReadDERFromFile(&data2sign, inFile, PR_FALSE) != SECSuccess)
       return -1;

    if (!encapsulated) {
       /* unfortunately, we must create the digest ourselves */
       /* SEC_PKCS7CreateSignedData should have a flag to not include */
       /* the content for non-encapsulated content at encode time, but */
       /* should always compute the hash itself */
       if (CreateDigest(&data2sign, digestdata, &len, 32) < 0)
           return -1; = (unsigned char *)digestdata;
       digest.len = len;

    /* XXX Need a better way to handle that usage stuff! */
    cinfo = SEC_PKCS7CreateSignedData (cert, certUsageEmailSigner, NULL,
                                   encapsulated ? NULL : &digest,
                                   NULL, NULL);
    if (cinfo == NULL)
       return -1;

    if (encapsulated) {
       SEC_PKCS7SetContent(cinfo, (char *), data2sign.len);

    rv = SEC_PKCS7IncludeCertChain (cinfo, NULL);
    if (rv != SECSuccess) {
       SEC_PKCS7DestroyContentInfo (cinfo);
       return -1;

    rv = SEC_PKCS7Encode (cinfo, SignOut, outFile, NULL,
                       NULL, NULL);

    SEC_PKCS7DestroyContentInfo (cinfo);

    if (rv != SECSuccess)
       return -1;

    return 0;

Here is the call graph for this function:

Here is the caller graph for this function:

static void SignOut ( void arg,
const char *  buf,
unsigned long  len 
) [static]

Definition at line 102 of file p7sign.c.

   FILE *out;

   out = (FILE*) arg; 
   fwrite (buf, len, 1, out);

Here is the call graph for this function:

Here is the caller graph for this function:

static void Usage ( char *  progName) [static]

Definition at line 82 of file p7sign.c.

           "Usage:  %s -k keyname [-d keydir] [-i input] [-o output]\n",
    fprintf(stderr, "%-20s Nickname of key to use for signature\n",
           "-k keyname");
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
           "-d keydir");
    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
           "-i input");
    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
           "-o output");
    fprintf(stderr, "%-20s Encapsulate content in signature message\n",
    fprintf(stderr, "%-20s Password to the key databse\n", "-p");

Here is the call graph for this function:

Variable Documentation

char* KeyDbPassword = 0

Definition at line 67 of file p7sign.c.