Back to index

lightning-sunbird  0.9+nobinonly
Defines | Typedefs | Functions
jarsign.c File Reference
#include "jar.h"
#include "jarint.h"
#include "jarevil.h"
#include "pk11func.h"
#include "sechash.h"

Go to the source code of this file.

Defines

#define USE_MOZ_THREAD
#define CHUNQ   64000
#define FILECHUNQ   32768

Typedefs

typedef void(* ETVoidPtrFunc )(void *data)

Functions

JAR_Digest *PR_CALLBACK JAR_calculate_digest (void ZHUGEP *data, long length)
int JAR_digest_file (char *filename, JAR_Digest *dig)
voidjar_open_key_database (void)
int jar_close_key_database (void *keydb)
static void jar_pk7_out (void *arg, const char *buf, unsigned long len)
int jar_create_pk7 (CERTCertDBHandle *certdb, void *keydb, CERTCertificate *cert, char *password, JAR_FILE infp, JAR_FILE outfp)

Define Documentation

#define CHUNQ   64000

Definition at line 75 of file jarsign.c.

#define FILECHUNQ   32768

Definition at line 76 of file jarsign.c.

Definition at line 44 of file jarsign.c.


Typedef Documentation

typedef void(* ETVoidPtrFunc)(void *data)

Definition at line 57 of file jarsign.c.


Function Documentation

Definition at line 89 of file jarsign.c.

  {
  long chunq;
  JAR_Digest *dig;

  unsigned int md5_length, sha1_length;

  PK11Context *md5  = 0;
  PK11Context *sha1 = 0;

  dig = (JAR_Digest *) PORT_ZAlloc (sizeof (JAR_Digest));

  if (dig == NULL) 
    {
    /* out of memory allocating digest */
    return NULL;
    }

#if defined(XP_WIN16)
  PORT_Assert ( !IsBadHugeReadPtr(data, length) );
#endif

  md5  = PK11_CreateDigestContext (SEC_OID_MD5);
  sha1 = PK11_CreateDigestContext (SEC_OID_SHA1);

  if (length >= 0) 
    {
    PK11_DigestBegin (md5);
    PK11_DigestBegin (sha1);

    do {
       chunq = length;

#ifdef XP_WIN16
       if (length > CHUNQ) chunq = CHUNQ;

       /*
        *  If the block of data crosses one or more segment 
        *  boundaries then only pass the chunk of data in the 
        *  first segment.
        * 
        *  This allows the data to be treated as FAR by the
        *  PK11_DigestOp(...) routine.
        *
        */

       if (OFFSETOF(data) + chunq >= 0x10000) 
         chunq = 0x10000 - OFFSETOF(data);
#endif

       PK11_DigestOp (md5,  (unsigned char*)data, chunq);
       PK11_DigestOp (sha1, (unsigned char*)data, chunq);

       length -= chunq;
       data = ((char ZHUGEP *) data + chunq);
       } 
    while (length > 0);

    PK11_DigestFinal (md5,  dig->md5,  &md5_length,  MD5_LENGTH);
    PK11_DigestFinal (sha1, dig->sha1, &sha1_length, SHA1_LENGTH);

    PK11_DestroyContext (md5,  PR_TRUE);
    PK11_DestroyContext (sha1, PR_TRUE);
    }

  return dig;
  }

Definition at line 236 of file jarsign.c.

  {
  /* We never do close it */
  return 0;
  }
int jar_create_pk7 ( CERTCertDBHandle *  certdb,
void keydb,
CERTCertificate *  cert,
char *  password,
JAR_FILE  infp,
JAR_FILE  outfp 
)

Definition at line 254 of file jarsign.c.

  {
  int nb;
  unsigned char buffer [4096], digestdata[32];
  const SECHashObject *hashObj;
  void *hashcx;
  unsigned int len;

  int status = 0;
  char *errstring;

  SECItem digest;
  SEC_PKCS7ContentInfo *cinfo;
  SECStatus rv;

  void /*MWContext*/ *mw;

  if (outfp == NULL || infp == NULL || cert == NULL)
    return JAR_ERR_GENERAL;

  /* we sign with SHA */
  hashObj = HASH_GetHashObject(HASH_AlgSHA1);

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

  (* hashObj->begin)(hashcx);

  while (1)
    {
    /* nspr2.0 doesn't support feof 
       if (feof (infp)) break; */

    nb = JAR_FREAD (infp, buffer, sizeof (buffer));
    if (nb == 0) 
      {
#if 0
      if (ferror(infp)) 
        {
        /* PORT_SetError(SEC_ERROR_IO); */ /* FIX */
       (* hashObj->destroy) (hashcx, PR_TRUE);
       return JAR_ERR_GENERAL;
        }
#endif
      /* eof */
      break;
      }
    (* hashObj->update) (hashcx, buffer, nb);
    }

  (* hashObj->end) (hashcx, digestdata, &len, 32);
  (* hashObj->destroy) (hashcx, PR_TRUE);

  digest.data = digestdata;
  digest.len = len;

  /* signtool must use any old context it can find since it's
     calling from inside javaland. */

#ifdef MOZILLA_CLIENT_OLD
  mw = XP_FindSomeContext();
#else
  mw = NULL;
#endif

  PORT_SetError (0);

  cinfo = SEC_PKCS7CreateSignedData 
             (cert, certUsageObjectSigner, NULL, 
                SEC_OID_SHA1, &digest, NULL, (void *) mw);

  if (cinfo == NULL)
    return JAR_ERR_PK7;

  rv = SEC_PKCS7IncludeCertChain (cinfo, NULL);
  if (rv != SECSuccess) 
    {
    status = PORT_GetError();
    SEC_PKCS7DestroyContentInfo (cinfo);
    return status;
    }

  /* Having this here forces signtool to always include
     signing time. */

  rv = SEC_PKCS7AddSigningTime (cinfo);
  if (rv != SECSuccess)
    {
    /* don't check error */
    }

  PORT_SetError (0);

#ifdef USE_MOZ_THREAD
  /* if calling from mozilla */
  rv = jar_moz_encode
             (cinfo, jar_pk7_out, outfp, 
                 NULL,  /* pwfn */ NULL,  /* pwarg */ (void *) mw);
#else
  /* if calling from mozilla thread*/
  rv = SEC_PKCS7Encode 
             (cinfo, jar_pk7_out, outfp, 
                 NULL,  /* pwfn */ NULL,  /* pwarg */ (void *) mw):
#endif

  if (rv != SECSuccess)
    status = PORT_GetError();

  SEC_PKCS7DestroyContentInfo (cinfo);

  if (rv != SECSuccess)
    {
    errstring = JAR_get_error (status);
    /*XP_TRACE (("Jar signing failed (reason %d = %s)", status, errstring));*/
    return status < 0 ? status : JAR_ERR_GENERAL;
    }

  return 0;
  }
int JAR_digest_file ( char *  filename,
JAR_Digest dig 
)

Definition at line 165 of file jarsign.c.

    {
    JAR_FILE fp;

    int num;
    unsigned char *buf;

    PK11Context *md5 = 0;
    PK11Context *sha1 = 0;

    unsigned int md5_length, sha1_length;

    buf = (unsigned char *) PORT_ZAlloc (FILECHUNQ);
    if (buf == NULL)
      {
      /* out of memory */
      return JAR_ERR_MEMORY;
      }
 
    if ((fp = JAR_FOPEN (filename, "rb")) == 0)
      {
      /* perror (filename); FIX XXX XXX XXX XXX XXX XXX */
      PORT_Free (buf);
      return JAR_ERR_FNF;
      }

    md5 = PK11_CreateDigestContext (SEC_OID_MD5);
    sha1 = PK11_CreateDigestContext (SEC_OID_SHA1);

    if (md5 == NULL || sha1 == NULL) 
      {
      /* can't generate digest contexts */
      PORT_Free (buf);
      JAR_FCLOSE (fp);
      return JAR_ERR_GENERAL;
      }

    PK11_DigestBegin (md5);
    PK11_DigestBegin (sha1);

    while (1)
      {
      if ((num = JAR_FREAD (fp, buf, FILECHUNQ)) == 0)
        break;

      PK11_DigestOp (md5, buf, num);
      PK11_DigestOp (sha1, buf, num);
      }

    PK11_DigestFinal (md5, dig->md5, &md5_length, MD5_LENGTH);
    PK11_DigestFinal (sha1, dig->sha1, &sha1_length, SHA1_LENGTH);

    PK11_DestroyContext (md5, PR_TRUE);
    PK11_DestroyContext (sha1, PR_TRUE);

    PORT_Free (buf);
    JAR_FCLOSE (fp);

    return 0;
    }

Definition at line 231 of file jarsign.c.

  {
    return NULL;
  }
static void jar_pk7_out ( void arg,
const char *  buf,
unsigned long  len 
) [static]

Definition at line 248 of file jarsign.c.

Here is the caller graph for this function: