50
50
/* from libevent.h */
51
51
typedef void (*ETVoidPtrFunc) (void * data);
53
#ifdef MOZILLA_CLIENT_OLD
55
extern void ET_moz_CallFunction (ETVoidPtrFunc fn, void *data);
58
/* extern MWContext *XP_FindSomeContext(void); */
59
extern void *XP_FindSomeContext(void);
63
53
/* key database wrapper */
65
54
/* static SECKEYKeyDBHandle *jar_open_key_database (void); */
67
55
/* CHUNQ is our bite size */
69
57
#define CHUNQ 64000
70
58
#define FILECHUNQ 32768
73
* J A R _ c a l c u l a t e _ d i g e s t
61
* J A R _ c a l c u l a t e _ d i g e s t
75
63
* Quick calculation of a digest for
76
64
* the specified block of memory. Will calculate
77
65
* for all supported algorithms, now MD5.
79
67
* This version supports huge pointers for WIN16.
83
JAR_Digest * PR_CALLBACK JAR_calculate_digest (void ZHUGEP *data, long length)
88
unsigned int md5_length, sha1_length;
91
PK11Context *sha1 = 0;
93
dig = (JAR_Digest *) PORT_ZAlloc (sizeof (JAR_Digest));
97
/* out of memory allocating digest */
101
#if defined(XP_WIN16)
102
PORT_Assert ( !IsBadHugeReadPtr(data, length) );
105
md5 = PK11_CreateDigestContext (SEC_OID_MD5);
106
sha1 = PK11_CreateDigestContext (SEC_OID_SHA1);
110
PK11_DigestBegin (md5);
111
PK11_DigestBegin (sha1);
117
if (length > CHUNQ) chunq = CHUNQ;
120
* If the block of data crosses one or more segment
121
* boundaries then only pass the chunk of data in the
124
* This allows the data to be treated as FAR by the
125
* PK11_DigestOp(...) routine.
129
if (OFFSETOF(data) + chunq >= 0x10000)
130
chunq = 0x10000 - OFFSETOF(data);
133
PK11_DigestOp (md5, (unsigned char*)data, chunq);
134
PK11_DigestOp (sha1, (unsigned char*)data, chunq);
137
data = ((char ZHUGEP *) data + chunq);
141
PK11_DigestFinal (md5, dig->md5, &md5_length, MD5_LENGTH);
142
PK11_DigestFinal (sha1, dig->sha1, &sha1_length, SHA1_LENGTH);
144
PK11_DestroyContext (md5, PR_TRUE);
145
PK11_DestroyContext (sha1, PR_TRUE);
70
JAR_Digest * PR_CALLBACK
71
JAR_calculate_digest(void *data, long length)
74
PK11Context *sha1 = 0;
75
JAR_Digest *dig = PORT_ZNew(JAR_Digest);
77
unsigned int md5_length, sha1_length;
80
/* out of memory allocating digest */
84
md5 = PK11_CreateDigestContext(SEC_OID_MD5);
85
sha1 = PK11_CreateDigestContext(SEC_OID_SHA1);
88
PK11_DigestBegin (md5);
89
PK11_DigestBegin (sha1);
94
PK11_DigestOp(md5, (unsigned char*)data, chunq);
95
PK11_DigestOp(sha1, (unsigned char*)data, chunq);
97
data = ((char *) data + chunq);
101
PK11_DigestFinal (md5, dig->md5, &md5_length, MD5_LENGTH);
102
PK11_DigestFinal (sha1, dig->sha1, &sha1_length, SHA1_LENGTH);
104
PK11_DestroyContext (md5, PR_TRUE);
105
PK11_DestroyContext (sha1, PR_TRUE);
152
111
* J A R _ d i g e s t _ f i l e
154
* Calculates the MD5 and SHA1 digests for a file
113
* Calculates the MD5 and SHA1 digests for a file
155
114
* present on disk, and returns these in JAR_Digest struct.
159
int JAR_digest_file (char *filename, JAR_Digest *dig)
118
JAR_digest_file (char *filename, JAR_Digest *dig)
121
PK11Context *md5 = 0;
122
PK11Context *sha1 = 0;
123
unsigned char *buf = (unsigned char *) PORT_ZAlloc (FILECHUNQ);
166
PK11Context *md5 = 0;
167
PK11Context *sha1 = 0;
169
125
unsigned int md5_length, sha1_length;
171
buf = (unsigned char *) PORT_ZAlloc (FILECHUNQ);
175
return JAR_ERR_MEMORY;
178
if ((fp = JAR_FOPEN (filename, "rb")) == 0)
180
/* perror (filename); FIX XXX XXX XXX XXX XXX XXX */
129
return JAR_ERR_MEMORY;
132
if ((fp = JAR_FOPEN (filename, "rb")) == 0) {
133
/* perror (filename); FIX XXX XXX XXX XXX XXX XXX */
185
138
md5 = PK11_CreateDigestContext (SEC_OID_MD5);
186
139
sha1 = PK11_CreateDigestContext (SEC_OID_SHA1);
188
if (md5 == NULL || sha1 == NULL)
190
/* can't generate digest contexts */
193
return JAR_ERR_GENERAL;
141
if (md5 == NULL || sha1 == NULL) {
142
/* can't generate digest contexts */
145
return JAR_ERR_GENERAL;
196
148
PK11_DigestBegin (md5);
197
149
PK11_DigestBegin (sha1);
201
if ((num = JAR_FREAD (fp, buf, FILECHUNQ)) == 0)
152
if ((num = JAR_FREAD (fp, buf, FILECHUNQ)) == 0)
204
PK11_DigestOp (md5, buf, num);
205
PK11_DigestOp (sha1, buf, num);
155
PK11_DigestOp (md5, buf, num);
156
PK11_DigestOp (sha1, buf, num);
208
159
PK11_DigestFinal (md5, dig->md5, &md5_length, MD5_LENGTH);
209
160
PK11_DigestFinal (sha1, dig->sha1, &sha1_length, SHA1_LENGTH);
242
195
static void jar_pk7_out (void *arg, const char *buf, unsigned long len)
244
JAR_FWRITE ((JAR_FILE) arg, buf, len);
248
(CERTCertDBHandle *certdb, void *keydb,
249
CERTCertificate *cert, char *password, JAR_FILE infp, JAR_FILE outfp)
252
unsigned char buffer [4096], digestdata[32];
253
const SECHashObject *hashObj;
261
SEC_PKCS7ContentInfo *cinfo;
264
void /*MWContext*/ *mw;
266
if (outfp == NULL || infp == NULL || cert == NULL)
267
return JAR_ERR_GENERAL;
269
/* we sign with SHA */
270
hashObj = HASH_GetHashObject(HASH_AlgSHA1);
272
hashcx = (* hashObj->create)();
274
return JAR_ERR_GENERAL;
276
(* hashObj->begin)(hashcx);
280
/* nspr2.0 doesn't support feof
281
if (feof (infp)) break; */
283
nb = JAR_FREAD (infp, buffer, sizeof (buffer));
289
(* hashObj->update) (hashcx, buffer, nb);
292
(* hashObj->end) (hashcx, digestdata, &len, 32);
293
(* hashObj->destroy) (hashcx, PR_TRUE);
295
digest.data = digestdata;
298
/* signtool must use any old context it can find since it's
299
calling from inside javaland. */
301
#ifdef MOZILLA_CLIENT_OLD
302
mw = XP_FindSomeContext();
309
cinfo = SEC_PKCS7CreateSignedData
310
(cert, certUsageObjectSigner, NULL,
311
SEC_OID_SHA1, &digest, NULL, (void *) mw);
316
rv = SEC_PKCS7IncludeCertChain (cinfo, NULL);
317
if (rv != SECSuccess)
319
status = PORT_GetError();
197
JAR_FWRITE ((JAR_FILE) arg, buf, len);
201
jar_create_pk7(CERTCertDBHandle *certdb, void *keydb, CERTCertificate *cert,
202
char *password, JAR_FILE infp, JAR_FILE outfp)
204
SEC_PKCS7ContentInfo *cinfo;
205
const SECHashObject *hashObj;
213
unsigned char digestdata[32];
214
unsigned char buffer[4096];
216
if (outfp == NULL || infp == NULL || cert == NULL)
217
return JAR_ERR_GENERAL;
219
/* we sign with SHA */
220
hashObj = HASH_GetHashObject(HASH_AlgSHA1);
222
hashcx = (* hashObj->create)();
224
return JAR_ERR_GENERAL;
226
(* hashObj->begin)(hashcx);
228
int nb = JAR_FREAD(infp, buffer, sizeof buffer);
229
if (nb == 0) { /* eof */
232
(* hashObj->update) (hashcx, buffer, nb);
234
(* hashObj->end)(hashcx, digestdata, &len, 32);
235
(* hashObj->destroy)(hashcx, PR_TRUE);
237
digest.data = digestdata;
240
/* signtool must use any old context it can find since it's
241
calling from inside javaland. */
243
cinfo = SEC_PKCS7CreateSignedData(cert, certUsageObjectSigner, NULL,
244
SEC_OID_SHA1, &digest, NULL, mw);
248
rv = SEC_PKCS7IncludeCertChain(cinfo, NULL);
249
if (rv != SECSuccess) {
250
status = PORT_GetError();
251
SEC_PKCS7DestroyContentInfo(cinfo);
255
/* Having this here forces signtool to always include signing time. */
256
rv = SEC_PKCS7AddSigningTime(cinfo);
257
/* don't check error */
260
/* if calling from mozilla thread*/
261
rv = SEC_PKCS7Encode(cinfo, jar_pk7_out, outfp, NULL, NULL, mw);
262
if (rv != SECSuccess)
263
status = PORT_GetError();
320
264
SEC_PKCS7DestroyContentInfo (cinfo);
324
/* Having this here forces signtool to always include
327
rv = SEC_PKCS7AddSigningTime (cinfo);
328
if (rv != SECSuccess)
330
/* don't check error */
335
/* if calling from mozilla thread*/
337
(cinfo, jar_pk7_out, outfp,
338
NULL, /* pwfn */ NULL, /* pwarg */ (void *) mw);
340
if (rv != SECSuccess)
341
status = PORT_GetError();
343
SEC_PKCS7DestroyContentInfo (cinfo);
345
if (rv != SECSuccess)
347
errstring = JAR_get_error (status);
348
/*XP_TRACE (("Jar signing failed (reason %d = %s)", status, errstring));*/
349
return status < 0 ? status : JAR_ERR_GENERAL;
265
if (rv != SECSuccess) {
266
errstring = JAR_get_error (status);
267
return ((status < 0) ? status : JAR_ERR_GENERAL);