crypt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2007-Mar-4 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*
  9. crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
  10. The main encryption/decryption source code for Info-Zip software was
  11. originally written in Europe. To the best of our knowledge, it can
  12. be freely distributed in both source and object forms from any country,
  13. including the USA under License Exception TSU of the U.S. Export
  14. Administration Regulations (section 740.13(e)) of 6 June 2002.
  15. NOTE on copyright history:
  16. Previous versions of this source package (up to version 2.8) were
  17. not copyrighted and put in the public domain. If you cannot comply
  18. with the Info-Zip LICENSE, you may want to look for one of those
  19. public domain versions.
  20. */
  21. /*
  22. This encryption code is a direct transcription of the algorithm from
  23. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  24. file (appnote.txt) is distributed with the PKZIP program (even in the
  25. version without encryption capabilities).
  26. */
  27. #define ZCRYPT_INTERNAL
  28. #include "zip.h"
  29. #include "crypt.h"
  30. #include "ttyio.h"
  31. #if CRYPT
  32. #ifndef FALSE
  33. # define FALSE 0
  34. #endif
  35. #ifdef ZIP
  36. /* For the encoding task used in Zip (and ZipCloak), we want to initialize
  37. the crypt algorithm with some reasonably unpredictable bytes, see
  38. the crypthead() function. The standard rand() library function is
  39. used to supply these `random' bytes, which in turn is initialized by
  40. a srand() call. The srand() function takes an "unsigned" (at least 16bit)
  41. seed value as argument to determine the starting point of the rand()
  42. pseudo-random number generator.
  43. This seed number is constructed as "Seed = Seed1 .XOR. Seed2" with
  44. Seed1 supplied by the current time (= "(unsigned)time()") and Seed2
  45. as some (hopefully) nondeterministic bitmask. On many (most) systems,
  46. we use some "process specific" number, as the PID or something similar,
  47. but when nothing unpredictable is available, a fixed number may be
  48. sufficient.
  49. NOTE:
  50. 1.) This implementation requires the availability of the following
  51. standard UNIX C runtime library functions: time(), rand(), srand().
  52. On systems where some of them are missing, the environment that
  53. incorporates the crypt routines must supply suitable replacement
  54. functions.
  55. 2.) It is a very bad idea to use a second call to time() to set the
  56. "Seed2" number! In this case, both "Seed1" and "Seed2" would be
  57. (almost) identical, resulting in a (mostly) "zero" constant seed
  58. number passed to srand().
  59. The implementation environment defined in the "zip.h" header should
  60. supply a reasonable definition for ZCR_SEED2 (an unsigned number; for
  61. most implementations of rand() and srand(), only the lower 16 bits are
  62. significant!). An example that works on many systems would be
  63. "#define ZCR_SEED2 (unsigned)getpid()".
  64. The default definition for ZCR_SEED2 supplied below should be regarded
  65. as a fallback to allow successful compilation in "beta state"
  66. environments.
  67. */
  68. # include <time.h> /* time() function supplies first part of crypt seed */
  69. /* "last resort" source for second part of crypt seed pattern */
  70. # ifndef ZCR_SEED2
  71. # define ZCR_SEED2 (unsigned)3141592654L /* use PI as default pattern */
  72. # endif
  73. # ifdef GLOBAL /* used in Amiga system headers, maybe others too */
  74. # undef GLOBAL
  75. # endif
  76. # define GLOBAL(g) g
  77. #else /* !ZIP */
  78. # define GLOBAL(g) G.g
  79. #endif /* ?ZIP */
  80. #ifdef UNZIP
  81. /* char *key = (char *)NULL; moved to globals.h */
  82. # ifndef FUNZIP
  83. local int testp OF((__GPRO__ ZCONST uch *h));
  84. local int testkey OF((__GPRO__ ZCONST uch *h, ZCONST char *key));
  85. # endif
  86. #else /* def UNZIP */ /* moved to globals.h for UnZip */
  87. local z_uint4 keys[3]; /* keys defining the pseudo-random sequence */
  88. #endif /* def UNZIP [else] */
  89. #ifndef Trace
  90. # ifdef CRYPT_DEBUG
  91. # define Trace(x) fprintf x
  92. # else
  93. # define Trace(x)
  94. # endif
  95. #endif
  96. #include "crc32.h"
  97. #ifdef IZ_CRC_BE_OPTIMIZ
  98. local z_uint4 near crycrctab[256];
  99. local z_uint4 near *cry_crctb_p = NULL;
  100. local z_uint4 near *crytab_init OF((__GPRO));
  101. # define CRY_CRC_TAB cry_crctb_p
  102. # undef CRC32
  103. # define CRC32(c, b, crctab) (crctab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  104. #else
  105. # define CRY_CRC_TAB CRC_32_TAB
  106. #endif /* ?IZ_CRC_BE_OPTIMIZ */
  107. /***********************************************************************
  108. * Return the next byte in the pseudo-random sequence
  109. */
  110. int decrypt_byte(__G)
  111. __GDEF
  112. {
  113. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  114. * unpredictable manner on 16-bit systems; not a problem
  115. * with any known compiler so far, though */
  116. temp = ((unsigned)GLOBAL(keys[2]) & 0xffff) | 2;
  117. return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
  118. }
  119. /***********************************************************************
  120. * Update the encryption keys with the next byte of plain text
  121. */
  122. int update_keys(__G__ c)
  123. __GDEF
  124. int c; /* byte of plain text */
  125. {
  126. GLOBAL(keys[0]) = CRC32(GLOBAL(keys[0]), c, CRY_CRC_TAB);
  127. GLOBAL(keys[1]) = (GLOBAL(keys[1])
  128. + (GLOBAL(keys[0]) & 0xff))
  129. * 134775813L + 1;
  130. {
  131. register int keyshift = (int)(GLOBAL(keys[1]) >> 24);
  132. GLOBAL(keys[2]) = CRC32(GLOBAL(keys[2]), keyshift, CRY_CRC_TAB);
  133. }
  134. return c;
  135. }
  136. /***********************************************************************
  137. * Initialize the encryption keys and the random header according to
  138. * the given password.
  139. */
  140. void init_keys(__G__ passwd)
  141. __GDEF
  142. ZCONST char *passwd; /* password string with which to modify keys */
  143. {
  144. #ifdef IZ_CRC_BE_OPTIMIZ
  145. if (cry_crctb_p == NULL) {
  146. cry_crctb_p = crytab_init(__G);
  147. }
  148. #endif
  149. GLOBAL(keys[0]) = 305419896L;
  150. GLOBAL(keys[1]) = 591751049L;
  151. GLOBAL(keys[2]) = 878082192L;
  152. while (*passwd != '\0') {
  153. update_keys(__G__ (int)*passwd);
  154. passwd++;
  155. }
  156. }
  157. /***********************************************************************
  158. * Initialize the local copy of the table of precomputed crc32 values.
  159. * Whereas the public crc32-table is optimized for crc32 calculations
  160. * on arrays of bytes, the crypt code needs the crc32 values in an
  161. * byte-order-independent form as 32-bit unsigned numbers. On systems
  162. * with Big-Endian byte order using the optimized crc32 code, this
  163. * requires inverting the byte-order of the values in the
  164. * crypt-crc32-table.
  165. */
  166. #ifdef IZ_CRC_BE_OPTIMIZ
  167. local z_uint4 near *crytab_init(__G)
  168. __GDEF
  169. {
  170. int i;
  171. for (i = 0; i < 256; i++) {
  172. crycrctab[i] = REV_BE(CRC_32_TAB[i]);
  173. }
  174. return crycrctab;
  175. }
  176. #endif
  177. #ifdef ZIP
  178. /***********************************************************************
  179. * Write encryption header to file zfile using the password passwd
  180. * and the cyclic redundancy check crc.
  181. */
  182. void crypthead(passwd, crc)
  183. ZCONST char *passwd; /* password string */
  184. ulg crc; /* crc of file being encrypted */
  185. {
  186. int n; /* index in random header */
  187. int t; /* temporary */
  188. int c; /* random byte */
  189. uch header[RAND_HEAD_LEN]; /* random header */
  190. static unsigned calls = 0; /* ensure different random header each time */
  191. /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
  192. * output of rand() to get less predictability, since rand() is
  193. * often poorly implemented.
  194. */
  195. if (++calls == 1) {
  196. srand((unsigned)time(NULL) ^ ZCR_SEED2);
  197. }
  198. init_keys(passwd);
  199. for (n = 0; n < RAND_HEAD_LEN-2; n++) {
  200. c = (rand() >> 7) & 0xff;
  201. header[n] = (uch)zencode(c, t);
  202. }
  203. /* Encrypt random header (last two bytes is high word of crc) */
  204. init_keys(passwd);
  205. for (n = 0; n < RAND_HEAD_LEN-2; n++) {
  206. header[n] = (uch)zencode(header[n], t);
  207. }
  208. header[RAND_HEAD_LEN-2] = (uch)zencode((int)(crc >> 16) & 0xff, t);
  209. header[RAND_HEAD_LEN-1] = (uch)zencode((int)(crc >> 24) & 0xff, t);
  210. bfwrite(header, 1, RAND_HEAD_LEN, BFWRITE_DATA);
  211. }
  212. #ifdef UTIL
  213. /***********************************************************************
  214. * Encrypt the zip entry described by z from file in_file to file y
  215. * using the password passwd. Return an error code in the ZE_ class.
  216. */
  217. int zipcloak(z, passwd)
  218. struct zlist far *z; /* zip entry to encrypt */
  219. ZCONST char *passwd; /* password string */
  220. {
  221. int c; /* input byte */
  222. int res; /* result code */
  223. zoff_t n; /* holds offset and counts size */
  224. int t; /* temporary */
  225. struct zlist far *localz; /* local header */
  226. uch buf[1024]; /* write buffer */
  227. int b; /* bytes in buffer */
  228. /* Set encrypted bit, clear extended local header bit and write local
  229. header to output file */
  230. if ((n = (zoff_t)zftello(y)) == (zoff_t)-1L) return ZE_TEMP;
  231. /* assume this archive is one disk and the file is open */
  232. /* read the local header */
  233. res = readlocal(&localz, z);
  234. /* update disk and offset */
  235. z->dsk = 0;
  236. z->off = n;
  237. /* Set encryption and unset any extended local header */
  238. z->flg |= 1, z->flg &= ~8;
  239. localz->lflg |= 1, localz->lflg &= ~8;
  240. /* Add size of encryption header */
  241. localz->siz += RAND_HEAD_LEN;
  242. z->siz = localz->siz;
  243. /* Put the local header */
  244. if ((res = putlocal(localz, PUTLOCAL_WRITE)) != ZE_OK) return res;
  245. /* Initialize keys with password and write random header */
  246. crypthead(passwd, localz->crc);
  247. /* Encrypt data */
  248. b = 0;
  249. for (n = z->siz - RAND_HEAD_LEN; n; n--) {
  250. if ((c = getc(in_file)) == EOF) {
  251. return ferror(in_file) ? ZE_READ : ZE_EOF;
  252. }
  253. buf[b] = (uch)zencode(c, t);
  254. b++;
  255. if (b >= 1024) {
  256. /* write the buffer */
  257. bfwrite(buf, 1, b, BFWRITE_DATA);
  258. b = 0;
  259. }
  260. }
  261. if (b) {
  262. /* write the buffer */
  263. bfwrite(buf, 1, b, BFWRITE_DATA);
  264. b = 0;
  265. }
  266. /* Since we seek to the start of each local header can skip
  267. reading any extended local header */
  268. /*
  269. if ((flag & 8) != 0 && zfseeko(in_file, 16L, SEEK_CUR)) {
  270. return ferror(in_file) ? ZE_READ : ZE_EOF;
  271. }
  272. if (fflush(y) == EOF) return ZE_TEMP;
  273. */
  274. /* Update number of bytes written to output file */
  275. tempzn += (4 + LOCHEAD) + localz->nam + localz->ext + localz->siz;
  276. /* Free local header */
  277. if (localz->ext) free(localz->extra);
  278. if (localz->nam) free(localz->iname);
  279. if (localz->nam) free(localz->name);
  280. #ifdef UNICODE_SUPPORT
  281. if (localz->uname) free(localz->uname);
  282. #endif
  283. free(localz);
  284. return ZE_OK;
  285. }
  286. /***********************************************************************
  287. * Decrypt the zip entry described by z from file in_file to file y
  288. * using the password passwd. Return an error code in the ZE_ class.
  289. */
  290. int zipbare(z, passwd)
  291. struct zlist far *z; /* zip entry to encrypt */
  292. ZCONST char *passwd; /* password string */
  293. {
  294. #ifdef ZIP10
  295. int c0 /* byte preceding the last input byte */
  296. #endif
  297. int c1; /* last input byte */
  298. /* all file offset and size now zoff_t - 8/28/04 EG */
  299. zoff_t size; /* size of input data */
  300. struct zlist far *localz; /* local header */
  301. uch buf[1024]; /* write buffer */
  302. int b; /* bytes in buffer */
  303. zoff_t n;
  304. int r; /* size of encryption header */
  305. int res; /* return code */
  306. /* Save position */
  307. if ((n = (zoff_t)zftello(y)) == (zoff_t)-1L) return ZE_TEMP;
  308. /* Read local header */
  309. res = readlocal(&localz, z);
  310. /* Update disk and offset */
  311. z->dsk = 0;
  312. z->off = n;
  313. /* Initialize keys with password */
  314. init_keys(passwd);
  315. /* Decrypt encryption header, save last two bytes */
  316. c1 = 0;
  317. for (r = RAND_HEAD_LEN; r; r--) {
  318. #ifdef ZIP10
  319. c0 = c1;
  320. #endif
  321. if ((c1 = getc(in_file)) == EOF) {
  322. return ferror(in_file) ? ZE_READ : ZE_EOF;
  323. }
  324. Trace((stdout, " (%02x)", c1));
  325. zdecode(c1);
  326. Trace((stdout, " %02x", c1));
  327. }
  328. Trace((stdout, "\n"));
  329. /* If last two bytes of header don't match crc (or file time in the
  330. * case of an extended local header), back up and just copy. For
  331. * pkzip 2.0, the check has been reduced to one byte only.
  332. */
  333. #ifdef ZIP10
  334. if ((ush)(c0 | (c1<<8)) !=
  335. (z->flg & 8 ? (ush) z->tim & 0xffff : (ush)(z->crc >> 16))) {
  336. #else
  337. if ((ush)c1 != (z->flg & 8 ? (ush) z->tim >> 8 : (ush)(z->crc >> 24))) {
  338. #endif
  339. if (zfseeko(in_file, n, SEEK_SET)) {
  340. return ferror(in_file) ? ZE_READ : ZE_EOF;
  341. }
  342. if ((res = zipcopy(z)) != ZE_OK) {
  343. ziperr(res, "was copying an entry");
  344. }
  345. return ZE_MISS;
  346. }
  347. z->siz -= RAND_HEAD_LEN;
  348. localz->siz = z->siz;
  349. localz->flg = z->flg &= ~9;
  350. z->lflg = localz->lflg &= ~9;
  351. if ((res = putlocal(localz, PUTLOCAL_WRITE)) != ZE_OK) return res;
  352. /* Decrypt data */
  353. b = 0;
  354. for (size = z->siz; size; size--) {
  355. if ((c1 = getc(in_file)) == EOF) {
  356. return ferror(in_file) ? ZE_READ : ZE_EOF;
  357. }
  358. zdecode(c1);
  359. buf[b] = c1;
  360. b++;
  361. if (b >= 1024) {
  362. /* write the buffer */
  363. bfwrite(buf, 1, b, BFWRITE_DATA);
  364. b = 0;
  365. }
  366. }
  367. if (b) {
  368. /* write the buffer */
  369. bfwrite(buf, 1, b, BFWRITE_DATA);
  370. b = 0;
  371. }
  372. /* Since we seek to the start of each local header can skip
  373. reading any extended local header */
  374. /* Update number of bytes written to output file */
  375. tempzn += (4 + LOCHEAD) + localz->nam + localz->ext + localz->siz;
  376. /* Free local header */
  377. if (localz->ext) free(localz->extra);
  378. if (localz->nam) free(localz->iname);
  379. if (localz->nam) free(localz->name);
  380. #ifdef UNICODE_SUPPORT
  381. if (localz->uname) free(localz->uname);
  382. #endif
  383. free(localz);
  384. return ZE_OK;
  385. }
  386. #else /* !UTIL */
  387. /***********************************************************************
  388. * If requested, encrypt the data in buf, and in any case call fwrite()
  389. * with the arguments to zfwrite(). Return what fwrite() returns.
  390. *
  391. * now write to global y
  392. *
  393. * A bug has been found when encrypting large files that don't
  394. * compress. See trees.c for the details and the fix.
  395. */
  396. unsigned zfwrite(buf, item_size, nb)
  397. zvoid *buf; /* data buffer */
  398. extent item_size; /* size of each item in bytes */
  399. extent nb; /* number of items */
  400. #if 0
  401. FILE *f; /* file to write to */
  402. #endif
  403. {
  404. int t; /* temporary */
  405. if (key != (char *)NULL) { /* key is the global password pointer */
  406. ulg size; /* buffer size */
  407. char *p = (char *)buf; /* steps through buffer */
  408. /* Encrypt data in buffer */
  409. for (size = item_size*(ulg)nb; size != 0; p++, size--) {
  410. *p = (char)zencode(*p, t);
  411. }
  412. }
  413. /* Write the buffer out */
  414. return bfwrite(buf, item_size, nb, BFWRITE_DATA);
  415. }
  416. #endif /* ?UTIL */
  417. #endif /* ZIP */
  418. #if (defined(UNZIP) && !defined(FUNZIP))
  419. /***********************************************************************
  420. * Get the password and set up keys for current zipfile member.
  421. * Return PK_ class error.
  422. */
  423. int decrypt(__G__ passwrd)
  424. __GDEF
  425. ZCONST char *passwrd;
  426. {
  427. ush b;
  428. int n, r;
  429. uch h[RAND_HEAD_LEN];
  430. Trace((stdout, "\n[incnt = %d]: ", GLOBAL(incnt)));
  431. /* get header once (turn off "encrypted" flag temporarily so we don't
  432. * try to decrypt the same data twice) */
  433. GLOBAL(pInfo->encrypted) = FALSE;
  434. defer_leftover_input(__G);
  435. for (n = 0; n < RAND_HEAD_LEN; n++) {
  436. b = NEXTBYTE;
  437. h[n] = (uch)b;
  438. Trace((stdout, " (%02x)", h[n]));
  439. }
  440. undefer_input(__G);
  441. GLOBAL(pInfo->encrypted) = TRUE;
  442. if (GLOBAL(newzip)) { /* this is first encrypted member in this zipfile */
  443. GLOBAL(newzip) = FALSE;
  444. if (passwrd != (char *)NULL) { /* user gave password on command line */
  445. if (!GLOBAL(key)) {
  446. if ((GLOBAL(key) = (char *)malloc(strlen(passwrd)+1)) ==
  447. (char *)NULL)
  448. return PK_MEM2;
  449. strcpy(GLOBAL(key), passwrd);
  450. GLOBAL(nopwd) = TRUE; /* inhibit password prompting! */
  451. }
  452. } else if (GLOBAL(key)) { /* get rid of previous zipfile's key */
  453. free(GLOBAL(key));
  454. GLOBAL(key) = (char *)NULL;
  455. }
  456. }
  457. /* if have key already, test it; else allocate memory for it */
  458. if (GLOBAL(key)) {
  459. if (!testp(__G__ h))
  460. return PK_COOL; /* existing password OK (else prompt for new) */
  461. else if (GLOBAL(nopwd))
  462. return PK_WARN; /* user indicated no more prompting */
  463. } else if ((GLOBAL(key) = (char *)malloc(IZ_PWLEN+1)) == (char *)NULL)
  464. return PK_MEM2;
  465. /* try a few keys */
  466. n = 0;
  467. do {
  468. r = (*G.decr_passwd)((zvoid *)&G, &n, GLOBAL(key), IZ_PWLEN+1,
  469. GLOBAL(zipfn), GLOBAL(filename));
  470. if (r == IZ_PW_ERROR) { /* internal error in fetch of PW */
  471. free (GLOBAL(key));
  472. GLOBAL(key) = NULL;
  473. return PK_MEM2;
  474. }
  475. if (r != IZ_PW_ENTERED) { /* user replied "skip" or "skip all" */
  476. *GLOBAL(key) = '\0'; /* We try the NIL password, ... */
  477. n = 0; /* and cancel fetch for this item. */
  478. }
  479. if (!testp(__G__ h))
  480. return PK_COOL;
  481. if (r == IZ_PW_CANCELALL) /* User replied "Skip all" */
  482. GLOBAL(nopwd) = TRUE; /* inhibit any further PW prompt! */
  483. } while (n > 0);
  484. return PK_WARN;
  485. } /* end function decrypt() */
  486. /***********************************************************************
  487. * Test the password. Return -1 if bad, 0 if OK.
  488. */
  489. local int testp(__G__ h)
  490. __GDEF
  491. ZCONST uch *h;
  492. {
  493. int r;
  494. char *key_translated;
  495. /* On systems with "obscure" native character coding (e.g., EBCDIC),
  496. * the first test translates the password to the "main standard"
  497. * character coding. */
  498. #ifdef STR_TO_CP1
  499. /* allocate buffer for translated password */
  500. if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
  501. return -1;
  502. /* first try, test password translated "standard" charset */
  503. r = testkey(__G__ h, STR_TO_CP1(key_translated, GLOBAL(key)));
  504. #else /* !STR_TO_CP1 */
  505. /* first try, test password as supplied on the extractor's host */
  506. r = testkey(__G__ h, GLOBAL(key));
  507. #endif /* ?STR_TO_CP1 */
  508. #ifdef STR_TO_CP2
  509. if (r != 0) {
  510. #ifndef STR_TO_CP1
  511. /* now prepare for second (and maybe third) test with translated pwd */
  512. if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
  513. return -1;
  514. #endif
  515. /* second try, password translated to alternate ("standard") charset */
  516. r = testkey(__G__ h, STR_TO_CP2(key_translated, GLOBAL(key)));
  517. #ifdef STR_TO_CP3
  518. if (r != 0)
  519. /* third try, password translated to another "standard" charset */
  520. r = testkey(__G__ h, STR_TO_CP3(key_translated, GLOBAL(key)));
  521. #endif
  522. #ifndef STR_TO_CP1
  523. free(key_translated);
  524. #endif
  525. }
  526. #endif /* STR_TO_CP2 */
  527. #ifdef STR_TO_CP1
  528. free(key_translated);
  529. if (r != 0) {
  530. /* last resort, test password as supplied on the extractor's host */
  531. r = testkey(__G__ h, GLOBAL(key));
  532. }
  533. #endif /* STR_TO_CP1 */
  534. return r;
  535. } /* end function testp() */
  536. local int testkey(__G__ h, key)
  537. __GDEF
  538. ZCONST uch *h; /* decrypted header */
  539. ZCONST char *key; /* decryption password to test */
  540. {
  541. ush b;
  542. #ifdef ZIP10
  543. ush c;
  544. #endif
  545. int n;
  546. uch *p;
  547. uch hh[RAND_HEAD_LEN]; /* decrypted header */
  548. /* set keys and save the encrypted header */
  549. init_keys(__G__ key);
  550. memcpy(hh, h, RAND_HEAD_LEN);
  551. /* check password */
  552. for (n = 0; n < RAND_HEAD_LEN; n++) {
  553. zdecode(hh[n]);
  554. Trace((stdout, " %02x", hh[n]));
  555. }
  556. /* use fzofft to format zoff_t as strings - 10/19/04 from SMS */
  557. Trace((stdout,
  558. "\n lrec.crc= %08lx crec.crc= %08lx pInfo->ExtLocHdr= %s\n",
  559. GLOBAL(lrec.crc32), GLOBAL(pInfo->crc),
  560. GLOBAL(pInfo->ExtLocHdr) ? "true":"false"));
  561. Trace((stdout, " incnt = %d unzip offset into zipfile = %s\n",
  562. GLOBAL(incnt),
  563. fzofft(GLOBAL(cur_zipfile_bufstart)+(GLOBAL(inptr)-GLOBAL(inbuf)),
  564. NULL, NULL)));
  565. /* same test as in zipbare(): */
  566. #ifdef ZIP10 /* check two bytes */
  567. c = hh[RAND_HEAD_LEN-2], b = hh[RAND_HEAD_LEN-1];
  568. Trace((stdout,
  569. " (c | (b<<8)) = %04x (crc >> 16) = %04x lrec.time = %04x\n",
  570. (ush)(c | (b<<8)), (ush)(GLOBAL(lrec.crc32) >> 16),
  571. ((ush)GLOBAL(lrec.last_mod_dos_datetime) & 0xffff))));
  572. if ((ush)(c | (b<<8)) != (GLOBAL(pInfo->ExtLocHdr) ?
  573. ((ush)GLOBAL(lrec.last_mod_dos_datetime) & 0xffff) :
  574. (ush)(GLOBAL(lrec.crc32) >> 16)))
  575. return -1; /* bad */
  576. #else
  577. b = hh[RAND_HEAD_LEN-1];
  578. Trace((stdout, " b = %02x (crc >> 24) = %02x (lrec.time >> 8) = %02x\n",
  579. b, (ush)(GLOBAL(lrec.crc32) >> 24),
  580. ((ush)GLOBAL(lrec.last_mod_dos_datetime) >> 8) & 0xff));
  581. if (b != (GLOBAL(pInfo->ExtLocHdr) ?
  582. ((ush)GLOBAL(lrec.last_mod_dos_datetime) >> 8) & 0xff :
  583. (ush)(GLOBAL(lrec.crc32) >> 24)))
  584. return -1; /* bad */
  585. #endif
  586. /* password OK: decrypt current buffer contents before leaving */
  587. for (n = (zoff_t)GLOBAL(incnt) > GLOBAL(csize) ?
  588. (int)GLOBAL(csize) : GLOBAL(incnt),
  589. p = GLOBAL(inptr); n--; p++)
  590. zdecode(*p);
  591. return 0; /* OK */
  592. } /* end function testkey() */
  593. #endif /* UNZIP && !FUNZIP */
  594. #else /* !CRYPT */
  595. /* something "externally visible" to shut up compiler/linker warnings */
  596. int zcr_dummy;
  597. #endif /* ?CRYPT */