PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/jar.h

http://firefox-mac-pdf.googlecode.com/
C Header | 481 lines | 187 code | 95 blank | 199 comment | 1 complexity | 9ada3881ae7c1e9f589694b1f894e918 MD5 | raw file
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  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/
  8. *
  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.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  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.
  20. *
  21. * Contributor(s):
  22. *
  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.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #ifndef __JAR_h_
  37. #define __JAR_h_
  38. /*
  39. * In general, any functions that return pointers
  40. * have memory owned by the caller.
  41. *
  42. */
  43. /* security includes */
  44. #include "cert.h"
  45. #include "hasht.h"
  46. /* nspr 2.0 includes */
  47. #include "prio.h"
  48. #ifndef ZHUGEP
  49. #ifdef XP_WIN16
  50. #define ZHUGEP __huge
  51. #else
  52. #define ZHUGEP
  53. #endif
  54. #endif
  55. #include <stdio.h>
  56. /* various types */
  57. typedef enum
  58. {
  59. jarTypeMF = 2,
  60. jarTypeSF = 3,
  61. jarTypeMeta = 6,
  62. jarTypePhy = 7,
  63. jarTypeSign = 10,
  64. jarTypeSect = 11,
  65. jarTypeOwner = 13
  66. }
  67. jarType;
  68. /* void data in ZZList's contain JAR_Item type */
  69. typedef struct JAR_Item_
  70. {
  71. char *pathname; /* relative. inside zip file */
  72. jarType type; /* various types */
  73. size_t size; /* size of data below */
  74. void *data; /* totally opaque */
  75. }
  76. JAR_Item;
  77. /* hashes */
  78. typedef enum
  79. {
  80. jarHashNone = 0,
  81. jarHashBad = 1,
  82. jarHashPresent = 2
  83. }
  84. jarHash;
  85. typedef struct JAR_Digest_
  86. {
  87. jarHash md5_status;
  88. unsigned char md5 [MD5_LENGTH];
  89. jarHash sha1_status;
  90. unsigned char sha1 [SHA1_LENGTH];
  91. }
  92. JAR_Digest;
  93. /* physical archive formats */
  94. typedef enum
  95. {
  96. jarArchGuess = 0,
  97. jarArchNone = 1,
  98. jarArchZip = 2,
  99. jarArchTar = 3
  100. }
  101. jarArch;
  102. #include "jar-ds.h"
  103. /* jar object */
  104. typedef struct JAR_
  105. {
  106. jarArch format; /* physical archive format */
  107. char *url; /* Where it came from */
  108. char *filename; /* Disk location */
  109. FILE *fp; /* For multiple extractions */ /* JAR_FILE */
  110. /* various linked lists */
  111. ZZList *manifest; /* Digests of MF sections */
  112. ZZList *hashes; /* Digests of actual signed files */
  113. ZZList *phy; /* Physical layout of JAR file */
  114. ZZList *metainfo; /* Global metainfo */
  115. JAR_Digest *globalmeta; /* digest of .MF global portion */
  116. /* Below will change to a linked list to support multiple sigs */
  117. int pkcs7; /* Enforced opaqueness */
  118. int valid; /* PKCS7 signature validated */
  119. ZZList *signers; /* the above, per signer */
  120. /* Window context, very necessary for PKCS11 now */
  121. void *mw; /* MWContext window context */
  122. /* Signal callback function */
  123. int (*signal) (int status, struct JAR_ *jar,
  124. const char *metafile, char *pathname, char *errorstring);
  125. }
  126. JAR;
  127. /*
  128. * Iterator
  129. *
  130. * Context for iterative operations. Certain operations
  131. * require iterating multiple linked lists because of
  132. * multiple signers. "nextsign" is used for this purpose.
  133. *
  134. */
  135. typedef struct JAR_Context_
  136. {
  137. JAR *jar; /* Jar we are searching */
  138. char *pattern; /* Regular expression */
  139. jarType finding; /* Type of item to find */
  140. ZZLink *next; /* Next item in find */
  141. ZZLink *nextsign; /* Next signer, sometimes */
  142. }
  143. JAR_Context;
  144. typedef struct JAR_Signer_
  145. {
  146. int pkcs7; /* Enforced opaqueness */
  147. int valid; /* PKCS7 signature validated */
  148. char *owner; /* name of .RSA file */
  149. JAR_Digest *digest; /* of .SF file */
  150. ZZList *sf; /* Linked list of .SF file contents */
  151. ZZList *certs; /* Signing information */
  152. }
  153. JAR_Signer;
  154. /* Meta informaton, or "policy", from the manifest file.
  155. Right now just one tuple per JAR_Item. */
  156. typedef struct JAR_Metainfo_
  157. {
  158. char *header;
  159. char *info;
  160. }
  161. JAR_Metainfo;
  162. /* This should not be global */
  163. typedef struct JAR_Physical_
  164. {
  165. unsigned char compression;
  166. unsigned long offset;
  167. unsigned long length;
  168. unsigned long uncompressed_length;
  169. #if defined(XP_UNIX) || defined(XP_BEOS)
  170. uint16 mode;
  171. #endif
  172. }
  173. JAR_Physical;
  174. typedef struct JAR_Cert_
  175. {
  176. size_t length;
  177. void *key;
  178. CERTCertificate *cert;
  179. }
  180. JAR_Cert;
  181. /* certificate stuff */
  182. typedef enum
  183. {
  184. jarCertCompany = 1,
  185. jarCertCA = 2,
  186. jarCertSerial = 3,
  187. jarCertExpires = 4,
  188. jarCertNickname = 5,
  189. jarCertFinger = 6,
  190. jarCertJavaHack = 100
  191. }
  192. jarCert;
  193. /* callback types */
  194. #define JAR_CB_SIGNAL 1
  195. /*
  196. * This is the base for the JAR error codes. It will
  197. * change when these are incorporated into allxpstr.c,
  198. * but right now they won't let me put them there.
  199. *
  200. */
  201. #ifndef SEC_ERR_BASE
  202. #define SEC_ERR_BASE (-0x2000)
  203. #endif
  204. #define JAR_BASE SEC_ERR_BASE + 300
  205. /* Jar specific error definitions */
  206. #define JAR_ERR_GENERAL (JAR_BASE + 1)
  207. #define JAR_ERR_FNF (JAR_BASE + 2)
  208. #define JAR_ERR_CORRUPT (JAR_BASE + 3)
  209. #define JAR_ERR_MEMORY (JAR_BASE + 4)
  210. #define JAR_ERR_DISK (JAR_BASE + 5)
  211. #define JAR_ERR_ORDER (JAR_BASE + 6)
  212. #define JAR_ERR_SIG (JAR_BASE + 7)
  213. #define JAR_ERR_METADATA (JAR_BASE + 8)
  214. #define JAR_ERR_ENTRY (JAR_BASE + 9)
  215. #define JAR_ERR_HASH (JAR_BASE + 10)
  216. #define JAR_ERR_PK7 (JAR_BASE + 11)
  217. #define JAR_ERR_PNF (JAR_BASE + 12)
  218. /*
  219. * Birth and death
  220. *
  221. */
  222. extern JAR *JAR_new (void);
  223. extern void PR_CALLBACK JAR_destroy (JAR *jar);
  224. extern char *JAR_get_error (int status);
  225. extern int JAR_set_callback (int type, JAR *jar,
  226. int (*fn) (int status, JAR *jar,
  227. const char *metafile, char *pathname, char *errortext));
  228. extern void JAR_init_callbacks
  229. ( char *(*string_cb)(int), void *(*find_cx)(void), void *(*init_cx)(void) );
  230. /*
  231. * JAR_set_context
  232. *
  233. * PKCS11 may require a password to be entered by the user
  234. * before any crypto routines may be called. This will require
  235. * a window context if used from inside Mozilla.
  236. *
  237. * Call this routine with your context before calling
  238. * verifying or signing. If you have no context, call with NULL
  239. * and one will be chosen for you.
  240. *
  241. */
  242. int JAR_set_context (JAR *jar, void /*MWContext*/ *mw);
  243. /*
  244. * Iterative operations
  245. *
  246. * JAR_find sets up for repeated calls with JAR_find_next.
  247. * I never liked findfirst and findnext, this is nicer.
  248. *
  249. * Pattern contains a relative pathname to match inside the
  250. * archive. It is currently assumed to be "*".
  251. *
  252. * To use:
  253. *
  254. * JAR_Item *item;
  255. * JAR_find (jar, "*.class", jarTypeMF);
  256. * while (JAR_find_next (jar, &item) >= 0)
  257. * { do stuff }
  258. *
  259. */
  260. /* Replacement functions with an external context */
  261. extern JAR_Context *JAR_find (JAR *jar, char *pattern, jarType type);
  262. extern int JAR_find_next (JAR_Context *ctx, JAR_Item **it);
  263. extern void JAR_find_end (JAR_Context *ctx);
  264. /*
  265. * Function to parse manifest file:
  266. *
  267. * Many signatures may be attached to a single filename located
  268. * inside the zip file. We only support one.
  269. *
  270. * Several manifests may be included in the zip file.
  271. *
  272. * You must pass the MANIFEST.MF file before any .SF files.
  273. *
  274. * Right now this returns a big ole list, privately in the jar structure.
  275. * If you need to traverse it, use JAR_find if possible.
  276. *
  277. * The path is needed to determine what type of binary signature is
  278. * being passed, though it is technically not needed for manifest files.
  279. *
  280. * When parsing an ASCII file, null terminate the ASCII raw_manifest
  281. * prior to sending it, and indicate a length of 0. For binary digital
  282. * signatures only, indicate the true length of the signature.
  283. * (This is legacy behavior.)
  284. *
  285. * You may free the manifest after parsing it.
  286. *
  287. */
  288. extern int JAR_parse_manifest
  289. (JAR *jar, char ZHUGEP *raw_manifest,
  290. long length, const char *path, const char *url);
  291. /*
  292. * Verify data (nonstreaming). The signature is actually
  293. * checked by JAR_parse_manifest or JAR_pass_archive.
  294. *
  295. */
  296. extern JAR_Digest * PR_CALLBACK JAR_calculate_digest
  297. (void ZHUGEP *data, long length);
  298. extern int PR_CALLBACK JAR_verify_digest
  299. (JAR *jar, const char *name, JAR_Digest *dig);
  300. extern int JAR_digest_file (char *filename, JAR_Digest *dig);
  301. /*
  302. * Get attribute from certificate:
  303. *
  304. * Returns any special signed attribute associated with this cert
  305. * or signature (passed in "data"). Attributes jarCert*. Most of the time
  306. * this will return a zero terminated string.
  307. *
  308. */
  309. extern int PR_CALLBACK JAR_cert_attribute
  310. (JAR *jar, jarCert attrib, long keylen, void *key,
  311. void **result, unsigned long *length);
  312. /*
  313. * Meta information
  314. *
  315. * Currently, since this call does not support passing of an owner
  316. * (certificate, or physical name of the .sf file), it is restricted to
  317. * returning information located in the manifest.mf file.
  318. *
  319. * Meta information is a name/value pair inside the archive file. Here,
  320. * the name is passed in *header and value returned in **info.
  321. *
  322. * Pass a NULL as the name to retrieve metainfo from the global section.
  323. *
  324. * Data is returned in **info, of size *length. The return value
  325. * will indicate if no data was found.
  326. *
  327. */
  328. extern int JAR_get_metainfo
  329. (JAR *jar, char *name, char *header, void **info, unsigned long *length);
  330. extern char *JAR_get_filename (JAR *jar);
  331. extern char *JAR_get_url (JAR *jar);
  332. /*
  333. * Return an HTML mockup of a certificate or signature.
  334. *
  335. * Returns a zero terminated ascii string
  336. * in raw HTML format.
  337. *
  338. */
  339. extern char *JAR_cert_html
  340. (JAR *jar, int style, long keylen, void *key, int *result);
  341. /* save the certificate with this fingerprint in persistent
  342. storage, somewhere, for retrieval in a future session when there
  343. is no corresponding JAR structure. */
  344. extern int PR_CALLBACK JAR_stash_cert
  345. (JAR *jar, long keylen, void *key);
  346. /* retrieve a certificate presumably stashed with the above
  347. function, but may be any certificate. Type is &CERTCertificate */
  348. void *JAR_fetch_cert (long length, void *key);
  349. /*
  350. * New functions to handle archives alone
  351. * (call JAR_new beforehand)
  352. *
  353. * JAR_pass_archive acts much like parse_manifest. Certificates
  354. * are returned in the JAR structure but as opaque data. When calling
  355. * JAR_verified_extract you still need to decide which of these
  356. * certificates to honor.
  357. *
  358. * Code to examine a JAR structure is in jarbert.c. You can obtain both
  359. * a list of filenames and certificates from traversing the linked list.
  360. *
  361. */
  362. extern int JAR_pass_archive
  363. (JAR *jar, jarArch format, char *filename, const char *url);
  364. /*
  365. * Same thing, but don't check signatures
  366. */
  367. extern int JAR_pass_archive_unverified
  368. (JAR *jar, jarArch format, char *filename, const char *url);
  369. /*
  370. * Extracts a relative pathname from the archive and places it
  371. * in the filename specified.
  372. *
  373. * Call JAR_set_nailed if you want to keep the file descriptors
  374. * open between multiple calls to JAR_verify_extract.
  375. *
  376. */
  377. extern int JAR_verified_extract
  378. (JAR *jar, char *path, char *outpath);
  379. /*
  380. * JAR_extract does no crypto checking. This can be used if you
  381. * need to extract a manifest file or signature, etc.
  382. *
  383. */
  384. extern int JAR_extract
  385. (JAR *jar, char *path, char *outpath);
  386. #endif /* __JAR_h_ */