Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / security / nss / lib / jar / jarjart.c
blob8946fc228f8bed990ce0e15d1d31c96c3790c8e2
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * JARJART
40 * JAR functions used by Jartool
43 /* This allows manifest files above 64k to be
44 processed on non-win16 platforms */
46 #include "jar.h"
47 #include "jarint.h"
48 #include "jarjart.h"
49 #include "blapi.h" /* JAR is supposed to be above the line!! */
50 #include "pk11func.h" /* PK11 wrapper funcs are all above the line. */
51 #include "certdb.h"
53 /* from certdb.h */
54 #define CERTDB_USER (1<<6)
57 * S O B _ l i s t _ c e r t s
59 * Return a list of newline separated certificate nicknames
60 * (this function used by the Jartool)
64 static SECStatus jar_list_cert_callback
65 (CERTCertificate *cert, SECItem *k, void *data)
67 char *name;
68 char **ugly_list;
70 int trusted;
72 ugly_list = (char **) data;
74 if (cert)
76 name = cert->nickname;
78 trusted = cert->trust->objectSigningFlags & CERTDB_USER;
80 /* Add this name or email to list */
82 if (name && trusted)
84 *ugly_list = (char*)PORT_Realloc
85 (*ugly_list, PORT_Strlen (*ugly_list) + PORT_Strlen (name) + 2);
87 if (*ugly_list)
89 if (**ugly_list)
90 PORT_Strcat (*ugly_list, "\n");
92 PORT_Strcat (*ugly_list, name);
97 return (SECSuccess);
101 * S O B _ J A R _ l i s t _ c e r t s
103 * Return a linfeed separated ascii list of certificate
104 * nicknames for the Jartool.
108 char *JAR_JAR_list_certs (void)
110 SECStatus status = SECFailure;
111 CERTCertDBHandle *certdb;
112 CERTCertList *certs;
113 CERTCertListNode *node;
115 char *ugly_list;
117 certdb = JAR_open_database();
119 /* a little something */
120 ugly_list = (char*)PORT_ZAlloc (16);
122 if (ugly_list)
124 *ugly_list = 0;
126 certs = PK11_ListCerts(PK11CertListUnique, NULL/* pwarg*/);
127 if (certs)
129 for (node = CERT_LIST_HEAD(certs); !CERT_LIST_END(node,certs);
130 node = CERT_LIST_NEXT(node))
132 jar_list_cert_callback(node->cert, NULL, (void *)&ugly_list);
134 CERT_DestroyCertList(certs);
135 status = SECSuccess;
139 JAR_close_database (certdb);
141 return (status != SECSuccess) ? NULL : ugly_list;
144 int JAR_JAR_validate_archive (char *filename)
146 JAR *jar;
147 int status = -1;
149 jar = JAR_new();
151 if (jar)
153 status = JAR_pass_archive (jar, jarArchGuess, filename, "");
155 if (status == 0)
156 status = jar->valid;
158 JAR_destroy (jar);
161 return status;
164 char *JAR_JAR_get_error (int status)
166 return JAR_get_error (status);
170 * S O B _ J A R _ h a s h
172 * Hash algorithm interface for use by the Jartool. Since we really
173 * don't know the private sizes of the context, and Java does need to
174 * know this number, allocate 512 bytes for it.
176 * In april 1997 hashes in this file were changed to call PKCS11,
177 * as FIPS requires that when a smartcard has failed validation,
178 * hashes are not to be performed. But because of the difficulty of
179 * preserving pointer context between calls to the JAR_JAR hashing
180 * functions, the hash routines are called directly, though after
181 * checking to see if hashing is allowed.
185 void *JAR_JAR_new_hash (int alg)
187 void *context;
189 MD5Context *md5;
190 SHA1Context *sha1;
192 /* this is a hack because this whole PORT_ZAlloc stuff looks scary */
194 if (!PK11_HashOK (alg == 1 ? SEC_OID_MD5 : SEC_OID_SHA1))
195 return NULL;
197 context = PORT_ZAlloc (512);
199 if (context)
201 switch (alg)
203 case 1: /* MD5 */
204 md5 = (MD5Context *) context;
205 MD5_Begin (md5);
206 break;
208 case 2: /* SHA1 */
209 sha1 = (SHA1Context *) context;
210 SHA1_Begin (sha1);
211 break;
215 return context;
218 void *JAR_JAR_hash (int alg, void *cookie, int length, void *data)
220 MD5Context *md5;
221 SHA1Context *sha1;
223 /* this is a hack because this whole PORT_ZAlloc stuff looks scary */
225 if (!PK11_HashOK (alg == 1 ? SEC_OID_MD5 : SEC_OID_SHA1))
226 return NULL;
228 if (length > 0)
230 switch (alg)
232 case 1: /* MD5 */
233 md5 = (MD5Context *) cookie;
234 MD5_Update (md5, (unsigned char*)data, length);
235 break;
237 case 2: /* SHA1 */
238 sha1 = (SHA1Context *) cookie;
239 SHA1_Update (sha1, (unsigned char*)data, length);
240 break;
244 return cookie;
247 void *JAR_JAR_end_hash (int alg, void *cookie)
249 int length;
250 unsigned char *data;
251 char *ascii;
253 MD5Context *md5;
254 SHA1Context *sha1;
256 unsigned int md5_length;
257 unsigned char md5_digest [MD5_LENGTH];
259 unsigned int sha1_length;
260 unsigned char sha1_digest [SHA1_LENGTH];
262 /* this is a hack because this whole PORT_ZAlloc stuff looks scary */
264 if (!PK11_HashOK (alg == 1 ? SEC_OID_MD5 : SEC_OID_SHA1))
265 return NULL;
267 switch (alg)
269 case 1: /* MD5 */
271 md5 = (MD5Context *) cookie;
273 MD5_End (md5, md5_digest, &md5_length, MD5_LENGTH);
274 /* MD5_DestroyContext (md5, PR_TRUE); */
276 data = md5_digest;
277 length = md5_length;
279 break;
281 case 2: /* SHA1 */
283 sha1 = (SHA1Context *) cookie;
285 SHA1_End (sha1, sha1_digest, &sha1_length, SHA1_LENGTH);
286 /* SHA1_DestroyContext (sha1, PR_TRUE); */
288 data = sha1_digest;
289 length = sha1_length;
291 break;
293 default: return NULL;
296 /* Instead of destroy context, since we created it */
297 /* PORT_Free (cookie); */
299 ascii = BTOA_DataToAscii(data, length);
301 return ascii ? PORT_Strdup (ascii) : NULL;
305 * S O B _ J A R _ s i g n _ a r c h i v e
307 * A simple API to sign a JAR archive.
311 int JAR_JAR_sign_archive
312 (char *nickname, char *password, char *sf, char *outsig)
314 int status = JAR_ERR_GENERAL;
315 JAR_FILE sf_fp;
316 JAR_FILE out_fp;
318 CERTCertDBHandle *certdb;
319 void *keydb;
321 CERTCertificate *cert;
323 if (PORT_Strlen (sf) < 5)
325 return JAR_ERR_GENERAL;
328 /* open cert and key databases */
330 certdb = JAR_open_database();
331 if (certdb == NULL)
332 return JAR_ERR_GENERAL;
334 keydb = jar_open_key_database();
335 if (keydb == NULL)
337 JAR_close_database(certdb);
338 return JAR_ERR_GENERAL;
341 sf_fp = JAR_FOPEN (sf, "rb");
342 out_fp = JAR_FOPEN (outsig, "wb");
344 cert = CERT_FindCertByNickname (certdb, nickname);
346 if (cert && sf_fp && out_fp)
348 status = jar_create_pk7 (certdb, keydb, cert, password, sf_fp, out_fp);
351 /* remove password from prying eyes */
352 PORT_Memset (password, 0, PORT_Strlen (password));
354 JAR_FCLOSE (sf_fp);
355 JAR_FCLOSE (out_fp);
357 JAR_close_database (certdb);
358 jar_close_key_database (keydb);
360 return status;