--- pgp/src/crypto.c 2018/04/24 16:40:26 1.1.1.4 +++ pgp/src/crypto.c 2018/04/24 16:42:34 1.1.1.6 @@ -1,21 +1,22 @@ /* crypto.c - Cryptographic routines for PGP. PGP: Pretty Good(tm) Privacy - public key cryptography for the masses. - (c) Copyright 1990-1992 by Philip Zimmermann. All rights reserved. + (c) Copyright 1990-1994 by Philip Zimmermann. All rights reserved. The author assumes no liability for damages resulting from the use of this software, even if the damage results from defects in this software. No warranty is expressed or implied. - All the source code Philip Zimmermann wrote for PGP is available for - free under the "Copyleft" General Public License from the Free - Software Foundation. A copy of that license agreement is included in - the source release package of PGP. Code developed by others for PGP - is also freely available. Other code that has been incorporated into - PGP from other sources was either originally published in the public - domain or was used with permission from the various authors. See the - PGP User's Guide for more complete information about licensing, - patent restrictions on certain algorithms, trademarks, copyrights, - and export controls. + Note that while most PGP source modules bear Philip Zimmermann's + copyright notice, many of them have been revised or entirely written + by contributors who frequently failed to put their names in their + code. Code that has been incorporated into PGP from other authors + was either originally published in the public domain or is used with + permission from the various authors. + + PGP is available for free to the public under certain restrictions. + See the PGP User's Guide (included in the release package) for + important information about licensing, patent restrictions on + certain algorithms, trademarks, copyrights, and export controls. Modified: 12-Nov-92 HAJK Add FDL stuff for VAX/VMS local mode. @@ -41,7 +42,6 @@ #include "mpilib.h" #include "mpiio.h" #include "random.h" -#include "idea.h" #include "crypto.h" #include "keymgmt.h" #include "keymaint.h" @@ -53,6 +53,7 @@ #include "exitpgp.h" #include "zipup.h" #include "rsaglue.h" +#include "idea.h" #define ENCRYPT_IT FALSE /* to pass to idea_file */ #define DECRYPT_IT TRUE /* to pass to idea_file */ @@ -61,7 +62,7 @@ /* This variable stores the md5 hash of the current file, if it is - available. It is used in open_strong_pseudorandom. */ + available. It is used in make_random_ideakey. */ static unsigned char md5buf[16]; /* This flag is set if the buffer above has been filled. */ @@ -80,7 +81,8 @@ long time(); void CToPascal(char *s) -{ /* "xyz\0" --> "\3xyz" ... converts C string to Pascal string */ +{ + /* "xyz\0" --> "\3xyz" ... converts C string to Pascal string */ int i,j; j = string_length(s); for (i=j; i!=0; i--) @@ -90,7 +92,8 @@ void CToPascal(char *s) void PascalToC( char *s ) -{ /* "\3xyz" --> "xyz\0" ... converts Pascal string to C string */ +{ + /* "\3xyz" --> "xyz\0" ... converts Pascal string to C string */ int i,j; for (i=0,j=((byte *) s)[0]; i= 0) - { f = fopen(keyfile, FOPRBIN); + { + f = fopen(keyfile, FOPRBIN); fseek (f, fpusr+usrpktlen, SEEK_SET); /* Read trust byte */ trust_status = read_trust(f, &keyctrl); fseek(f, fp, SEEK_SET); - if (is_compromised(f)) - { + if (is_compromised(f)) { CToPascal(userid); fprintf(pgpout, "\n"); show_key(f, fp, 0); fclose (f); - fprintf(pgpout, PSTR("\007\nWARNING: This key has been revoked by its owner,\n\ + fprintf(pgpout, LANG("\007\nWARNING: This key has been revoked by its owner,\n\ possibly because the secret key was compromised.\n")); - if (warn_only) - { /* this is only for checking signatures */ - fprintf(pgpout, PSTR("This could mean that this signature is a forgery.\n")); - return(1); - } - else - { /* don't use it for encryption */ - fprintf(pgpout, PSTR("You cannot use this revoked key.\n")); - return(-1); + if (warn_only) { + /* this is only for checking signatures */ + fprintf(pgpout, LANG("This could mean that this signature is a forgery.\n")); + return 1; + } else { /* don't use it for encryption */ + fprintf(pgpout, LANG("You cannot use this revoked key.\n")); + return -1; } } fclose (f); } CToPascal(userid); - if ((keyctrl & KC_LEGIT_MASK) != KC_LEGIT_COMPLETE) - { byte userid0[256]; + if ((keyctrl & KC_LEGIT_MASK) != KC_LEGIT_COMPLETE) { + byte userid0[256]; PascalToC(userid); strcpy ((char *) userid0, userid); CToPascal(userid); if ((keyctrl & KC_LEGIT_MASK) == KC_LEGIT_UNKNOWN) - fprintf(pgpout,PSTR("\007\nWARNING: Because this public key is not certified with a trusted\n\ + fprintf(pgpout,LANG("\007\nWARNING: Because this public key is not certified with a trusted\n\ signature, it is not known with high confidence that this public key\n\ actually belongs to: \"%s\".\n"), LOCAL_CHARSET((char *)userid0)); if ((keyctrl & KC_LEGIT_MASK) == KC_LEGIT_UNTRUSTED) - fprintf(pgpout, PSTR("\007\nWARNING: This public key is not trusted to actually belong to:\n\ + fprintf(pgpout, LANG("\007\nWARNING: This public key is not trusted to actually belong to:\n\ \"%s\".\n"), LOCAL_CHARSET((char *)userid0)); if ((keyctrl & KC_LEGIT_MASK) == KC_LEGIT_MARGINAL) - fprintf(pgpout,PSTR("\007\nWARNING: Because this public key is not certified with enough trusted\n\ + fprintf(pgpout,LANG("\007\nWARNING: Because this public key is not certified with enough trusted\n\ signatures, it is not known with high confidence that this public key\n\ actually belongs to: \"%s\".\n"), LOCAL_CHARSET((char *)userid0)); - if (keyctrl & KC_WARNONLY) - { /* KC_WARNONLY bit already set, user must have approved before. */ - fprintf(pgpout, PSTR("But you previously approved using this public key anyway.\n")); + if (keyctrl & KC_WARNONLY) { + /* KC_WARNONLY bit already set, user must have approved before. */ + fprintf(pgpout, LANG("But you previously approved using this public key anyway.\n")); } if (!filter_mode && !batchmode && !warn_only && !(keyctrl & KC_WARNONLY)) - { fprintf(pgpout,PSTR("\nAre you sure you want to use this public key (y/N)? ")); + { + fprintf(pgpout,LANG("\nAre you sure you want to use this public key (y/N)? ")); if (!getyesno('n')) - return(-1); + return -1; if (trust_status == 0 && (f = fopen(keyfile, FOPRWBIN)) != NULL) - { fseek (f, fpusr+usrpktlen, SEEK_SET); + { + fseek (f, fpusr+usrpktlen, SEEK_SET); keyctrl |= KC_WARNONLY; write_trust(f, keyctrl); fclose(f); } } } - return(0); + return 0; } /* warn_signatures */ boolean legal_ctb(byte ctb) -{ /* Used to determine if nesting should be allowed. */ +/* Used to determine if nesting should be allowed. */ +{ boolean legal; byte ctbtype; if (!is_ctb(ctb)) /* not even a bonafide CTB */ - return(FALSE); + return FALSE; /* Sure hope CTB internal bit definitions don't change... */ ctbtype = (ctb & CTB_TYPE_MASK) >> 2; /* Only allow these CTB types to be nested... */ - legal = ( - (ctbtype==CTB_PKE_TYPE) - || (ctbtype==CTB_SKE_TYPE) - || (ctbtype==CTB_CERT_SECKEY_TYPE) - || (ctbtype==CTB_CERT_PUBKEY_TYPE) - || (ctbtype==CTB_LITERAL_TYPE) - || (ctbtype==CTB_LITERAL2_TYPE) - || (ctbtype==CTB_COMPRESSED_TYPE) - || (ctbtype==CTB_CKE_TYPE) + legal = ( (ctbtype==CTB_PKE_TYPE) + || (ctbtype==CTB_SKE_TYPE) + || (ctbtype==CTB_CERT_SECKEY_TYPE) + || (ctbtype==CTB_CERT_PUBKEY_TYPE) + || (ctbtype==CTB_LITERAL_TYPE) + || (ctbtype==CTB_LITERAL2_TYPE) + || (ctbtype==CTB_COMPRESSED_TYPE) + || (ctbtype==CTB_CKE_TYPE) ); - return(legal); + return legal; } /* legal_ctb */ @@ -313,179 +321,83 @@ boolean legal_ctb(byte ctb) */ int version_error(int val, int checkval) -{ if (val != checkval) - { fprintf (pgpout, PSTR( -"\n\007Unsupported packet format - you need a newer version of PGP for this file.\n")); - return(1); +{ + if (val != checkval) { + fprintf (pgpout, LANG("\n\007Unsupported packet format - you need a newer version of PGP for this file.\n")); + return 1; } - return(0); + return 0; } +int +version_byte_error(int val) +{ + if (val != VERSION_BYTE_OLD && val != VERSION_BYTE_KLUDGE) { + fprintf (pgpout, LANG("\n\007Unsupported packet format - you need a newer version of PGP for this file.\n")); + return 1; + } + return 0; +} + + /*-------------------------------------------------------------------------*/ #define RAND_PREFIX_LENGTH 8 /* Length of IV for IDEA encryption */ -#define RANDSEED_SIZE 24 /* Size of randseed.bin file */ -int seedfile_exists(void) +static int make_random_ideakey(byte key[IDEAKEYSIZE+RAND_PREFIX_LENGTH], int skip) /* - If the file randseed.bin does not exist, this returns 0 and - schedules the appropriate number of random bytes for later - collection from the keyboard. + * Make a random IDEA key. Returns its length (the constant 16). + * It also generates a random IV, which is placed in the key array + * after the key proper, but is not counted in the length. + * Reads IDEA random key and random number seed from file, cranks the + * seed through the IDEA strong pseudorandom number generator, + * and writes them back out. This is used for generation of + * cryptographically strong pseudorandom numbers. This is mainly to + * save the user the trouble of having to type in lengthy keyboard + * sequences for generation of truly random numbers every time we want + * to make a random session key. This pseudorandom generator will only + * work if the file containing the random seed exists and is not empty. + * If this is not the case, it will be automatically created. + * + * The MD5 of the current file is used to "prewash" the random numbers, + * to make it more difficult for an attacker to predict the output. + * + * The "skip" parameter says to skip that many bytes at the beginning, + * used to generate a random IV only for conventional encryption. */ -{ char seedfile[MAX_PATH]; /* Random seed filename */ - - buildfilename(seedfile,RANDSEED_FILENAME); - if (file_exists(seedfile)) - return 1; /* True */ - randaccum_later(8*RANDSEED_SIZE); - return 0; -} - -void create_seedfile(void) -/* - Create the seedfile, from the random bits that seedfile_exists - buffered for accumulation. "randseed.bin" is used to generate - cryptographically strong pseudorandom numbers for session keys. - */ -{ char seedfile[MAX_PATH]; /* Random seed filename */ - FILE *f; - byte randbuf[RANDSEED_SIZE]; - int i; - - buildfilename(seedfile,RANDSEED_FILENAME); - - f = fopen(seedfile,FOPWBIN); /* open for writing binary */ - if (f==NULL) /* failed to create seedfile */ - return; /* error: no random number seed file available */ - fprintf(pgpout,PSTR("Initializing random seed file...")); -/* This is not part of the string above because it was changed after the 2.2 - release translations were started. It should be cleaned up eventually. */ - fputc('\n', pgpout); - for (i=1; i 0xFFFFL)) - { llength = 4; + if (big || (length > 0xFFFFL)) { + llength = 4; llenb = 2; - } - else if ((word16)length > 0xFF) - { llength = 2; + } else if ((word16)length > 0xFF) { + llength = 2; llenb = 1; - } - else - { llength = 1; + } else { + llength = 1; llenb = 0; } - ctb = CTB_BYTE(ctb_type, llenb); - fwrite( &ctb, 1, 1, f ); + + putc(CTB_BYTE(ctb_type, llenb), f); /* convert length to external byteorder... */ if (llength==1) buf[0] = length; @@ -554,67 +465,70 @@ void write_ctb_len (FILE *f, byte ctb_ty static int idea_file(byte *ideakey, boolean decryp, FILE *f, FILE *g, word32 lenfile) -/* Use IDEA in cipher feedback (CFB) mode to encrypt or decrypt a file. - The encrypted material starts out with a 64-bit random prefix, which - serves as an encrypted random CFB initialization vector, and - following that is 16 bits of "key check" material, which is a - duplicate of the last 2 bytes of the random prefix. Encrypted key - check bytes detect if correct IDEA key was used to decrypt ciphertext. -*/ -{ int count, status = 0; - word16 iv[4]; +/* + * Use IDEA in cipher feedback (CFB) mode to encrypt or decrypt a file. + * The encrypted material starts out with a 64-bit random prefix, which + * serves as an encrypted random CFB initialization vector, and + * following that is 16 bits of "key check" material, which is a + * duplicate of the last 2 bytes of the random prefix. Encrypted key + * check bytes detect if correct IDEA key was used to decrypt ciphertext. + */ +{ + int count, status = 0; extern byte textbuf[DISKBUFSIZE]; + struct IdeaCfbContext cfb; #define RAND_PREFIX_LENGTH 8 /* init CFB key */ - fill0(iv,sizeof(iv)); /* define initialization vector IV as 0 */ - initcfb_idea(iv,ideakey,decryp); + ideaCfbInit(&cfb, ideakey); - if (!decryp) /* encrypt-- insert key check bytes */ - { /* There is a random prefix followed by 2 key check bytes */ + if (!decryp) { /* encrypt-- insert key check bytes */ + /* There is a random prefix followed by 2 key check bytes */ memcpy(textbuf, ideakey+IDEAKEYSIZE, RAND_PREFIX_LENGTH); /* key check bytes are simply duplicates of final 2 random bytes */ textbuf[RAND_PREFIX_LENGTH] = textbuf[RAND_PREFIX_LENGTH-2]; textbuf[RAND_PREFIX_LENGTH+1] = textbuf[RAND_PREFIX_LENGTH-1]; - ideacfb(textbuf,RAND_PREFIX_LENGTH+2); + ideaCfbEncrypt(&cfb, textbuf, textbuf, RAND_PREFIX_LENGTH+2); fwrite(textbuf,1,RAND_PREFIX_LENGTH+2,g); - } - else /* decrypt-- check for key check bytes */ - { /* See if the redundancy is present after the random prefix */ + } else { /* decrypt-- check for key check bytes */ + /* See if the redundancy is present after the random prefix */ count = fread(textbuf,1,RAND_PREFIX_LENGTH+2,f); lenfile -= count; - if (count==(RAND_PREFIX_LENGTH+2)) - { ideacfb(textbuf,RAND_PREFIX_LENGTH+2); + if (count==(RAND_PREFIX_LENGTH+2)) { + ideaCfbDecrypt(&cfb, textbuf, textbuf, RAND_PREFIX_LENGTH+2); if ((textbuf[RAND_PREFIX_LENGTH] != textbuf[RAND_PREFIX_LENGTH-2]) || (textbuf[RAND_PREFIX_LENGTH+1] != textbuf[RAND_PREFIX_LENGTH-1])) - { status = -2; /* bad key error */ + { + status = -2; /* bad key error */ } - } - else /* file too short for key check bytes */ + } else /* file too short for key check bytes */ status = -3; /* error of the weird kind */ } + ideaCfbSync(&cfb); /* read and write the whole file in CFB mode... */ count = (lenfile < DISKBUFSIZE) ? (int)lenfile : DISKBUFSIZE; - while (count && status == 0) - { if ((count = fread(textbuf,1,count,f)) <= 0) - { status = -3; + while (count && status == 0) { + if ((count = fread(textbuf,1,count,f)) <= 0) { + status = -3; break; } lenfile -= count; - ideacfb(textbuf,count); + if (decryp) + ideaCfbDecrypt(&cfb, textbuf, textbuf, count); + else + ideaCfbEncrypt(&cfb, textbuf, textbuf, count); if (fwrite(textbuf,1,count,g) != count) status = -3; count = (lenfile < DISKBUFSIZE) ? (int)lenfile : DISKBUFSIZE; } - close_idea(); /* Clean up data structures */ - burn(iv); /* burn sensitive data on stack */ + ideaCfbDestroy(&cfb); /* Clean up data structures */ burn(textbuf); /* burn sensitive data on stack */ - return(status); /* should always take normal return */ + return status; /* should always take normal return */ } /* idea_file */ @@ -625,15 +539,18 @@ int idea_file(byte *ideakey, boolean dec */ word16 mpi_checksum; -int read_mpi(unitptr r, FILE *f, boolean adjust_precision, boolean scrambled) -/* Read a mutiprecision integer from a file. - adjust_precision is TRUE iff we should call set_precision to the - size of the number read in. - scrambled is TRUE iff field is encrypted (protects secret key fields). - Returns the bitcount of the number read in, or returns a negative - number if an error is detected. -*/ -{ byte buf[MAX_BYTE_PRECISION+2]; +int read_mpi(unitptr r, FILE *f, boolean adjust_precision, + struct IdeaCfbContext *cfb) +/* + * Read a mutiprecision integer from a file. + * adjust_precision is TRUE iff we should call set_precision to the + * size of the number read in. + * scrambled is TRUE iff field is encrypted (protects secret key fields). + * Returns the bitcount of the number read in, or returns a negative + * number if an error is detected. + */ +{ + byte buf[MAX_BYTE_PRECISION+2]; unsigned int count; word16 bytecount,bitcount; @@ -644,16 +561,18 @@ int read_mpi(unitptr r, FILE *f, boolean bitcount = fetch_word16(buf); if (bits2units(bitcount) > global_precision) - return(-1); /* error -- possible corrupted bitcount */ + return -1; /* error -- possible corrupted bitcount */ bytecount = bits2bytes(bitcount); count = fread(buf+2,1,bytecount,f); if (count < bytecount) - return(-1); /* error -- premature eof */ + return -1; /* error -- premature eof */ - if (scrambled) /* decrypt the field */ - ideacfb(buf+2,bytecount); + if (cfb) { /* decrypt the field */ + ideaCfbSync(cfb); + ideaCfbDecrypt(cfb, buf+2, buf+2, bytecount); + } /* Update running checksum, in case anyone cares... */ mpi_checksum += checksum (buf, bytecount+2); @@ -664,33 +583,37 @@ int read_mpi(unitptr r, FILE *f, boolean countbits, then call set_precision, then recall mpi2reg again. */ - if (adjust_precision && bytecount) - { /* set the precision to that specified by the number read. */ + if (adjust_precision && bytecount) { + /* set the precision to that specified by the number read. */ if (bitcount > MAX_BIT_PRECISION-SLOP_BITS) - return(-1); + return -1; set_precision(bits2units(bitcount+SLOP_BITS)); /* Now that precision is optimally set, call mpi2reg */ } if (mpi2reg(r,buf) == -1) /* convert to internal format */ - return(-1); + return -1; burn(buf); /* burn sensitive data on stack */ return (bitcount); } /* read_mpi */ -void write_mpi(unitptr n, FILE *f, boolean scrambled) -/* Write a multiprecision integer to a file. - scrambled is TRUE iff we should scramble field on the way out, - which is used to protect secret key fields. -*/ -{ byte buf[MAX_BYTE_PRECISION+2]; +void write_mpi(unitptr n, FILE *f, struct IdeaCfbContext *cfb) +/* + * Write a multiprecision integer to a file. + * scrambled is TRUE iff we should scramble field on the way out, + * which is used to protect secret key fields. + */ +{ + byte buf[MAX_BYTE_PRECISION+2]; short bytecount; bytecount = reg2mpi(buf,n); mpi_checksum += checksum (buf, bytecount+2); - if (scrambled) /* encrypt the field, skipping over the bitcount */ - ideacfb(buf+2,bytecount); + if (cfb) { /* encrypt the field, skipping over the bitcount */ + ideaCfbSync(cfb); + ideaCfbEncrypt(cfb, buf+2, buf+2, bytecount); + } fwrite(buf,1,bytecount+2,f); burn(buf); /* burn sensitive data on stack */ } /* write_mpi */ @@ -701,15 +624,16 @@ void write_mpi(unitptr n, FILE *f, boole int get_header_info_from_file(char *infile, byte *header, int count) /* Reads the first count bytes from infile into header. */ -{ FILE *f; +{ + FILE *f; fill0(header,count); /* open file f for read, in binary (not text) mode...*/ if ((f = fopen(infile,FOPRBIN)) == NULL) - return(-1); + return -1; /* read Cipher Type Byte, and maybe more */ count = fread(header,1,count,f); fclose(f); - return(count); /* normal return */ + return count; /* normal return */ } /* get_header_info_from_file */ @@ -718,8 +642,8 @@ int get_header_info_from_file(char *infi static -int make_signature_certificate(byte *certificate, MD5_CTX *MD, byte class, - unitptr e, unitptr d, unitptr p, unitptr q, unitptr u, unitptr n) +int make_signature_certificate(byte *certificate, struct MD5Context *MD, + byte class, unitptr e, unitptr d, unitptr p, unitptr q, unitptr u, unitptr n) /* Constructs a signed message digest in a signature certificate. Returns total certificate length in bytes, or returns negative error status. @@ -737,15 +661,16 @@ int make_signature_certificate(byte *cer complete message digest packet in a single RSA block. */ blocksize = countbytes(n)-1; /* size of a plaintext block */ - if (blocksize < 31) - { fprintf(pgpout,"\n\007Error: RSA key length must be at least 256 bits.\n"); - return(-1); + if (blocksize < 31) { + fprintf(pgpout,"\n\007Error: RSA key length must be at least 256 bits.\n"); + return -1; } get_timestamp(timestamp); /* Timestamp when signature was made */ - if (tstamp < REASONABLE_DATE) /* complain about bad time/date setting */ - { fprintf(pgpout,PSTR("\n\007Error: System clock/calendar is set wrong.\n")); - return(-1); + if (tstamp < REASONABLE_DATE) { + /* complain about bad time/date setting */ + fprintf(pgpout,LANG("\n\007Error: System clock/calendar is set wrong.\n")); + return -1; } convert_byteorder(timestamp,4); /* convert to external form */ @@ -756,14 +681,25 @@ int make_signature_certificate(byte *cer for random number generation later. Also make a note of that fact. */ already_have_md5 = 1; - if (!quietmode) - { fprintf(pgpout,PSTR("Just a moment...")); /* RSA will take a while. */ + if (!quietmode) { + fprintf(pgpout,LANG("Just a moment...")); /* RSA will take a while. */ fflush(pgpout); } /* do RSA signature calculation: */ - rsa_private_encrypt((unitptr)outbuf, md5buf, sizeof(md5buf), - e, d, p, q, u, n); + i = rsa_private_encrypt((unitptr)outbuf, md5buf, sizeof(md5buf), + e, d, p, q, u, n); + if (i < 0) { + if (i == -4) { + fprintf(pgpout,"\n\007Error: RSA key length must be at least 256 bits.\n"); + } else if (i == -3) { + fputs(LANG("\a\nError: key is too large. RSA keys may be no longer than 1024 bits,\n\ +due to limitations imposed by software provided by RSADSI.\n"), pgpout); + } else { + fprintf(pgpout, "\a\nUnexpected error %d signing\n", i); + } + return i; + } /* bytecount does not include the 2 prefix bytes */ bytecount = reg2mpi(outbuf,(unitptr)outbuf); /* convert to external format */ @@ -786,31 +722,17 @@ int make_signature_certificate(byte *cer put_word16((word16) ske_length, certificate+certificate_length); certificate_length+=2; /* advance past word */ - certificate[certificate_length++] = VERSION_BYTE; + certificate[certificate_length++] = version_byte; /* Begin fields that are included in MD calculation... */ certificate[certificate_length++] = mdlen; /* mdlen is length of MD-extras */ certificate[certificate_length++] = class & 0xff; - mdlen--; /* assume class byte always present */ /* timestamp already in external format */ - if (mdlen>0) - { for (j=0; j0) - { val = 0; /* Validation period */ - put_word16(val, certificate+certificate_length); - certificate_length+=2; /* advance past word */ - mdlen-=2; - } - /* hopefully, mdlen is now zero. */ + for (j=0; j 93-02-25 */ if (getsecretkey(GPK_ASKPASS, NULL, NULL, timestamp, NULL, NULL, - userid, n, e, d, p, q, u) < 0) - { fclose(f); - return(-1); /* problem with secret key file. error return. */ + userid, n, e, d, p, q, u) < 0) + { + fclose(f); + return -1; /* problem with secret key file. error return. */ } certificate_length = make_signature_certificate(certificate, &MD, K0_SIGNATURE_BYTE, e, d, p, q, u, n); if (certificate_length < 0) - return(-1); /* error return from make_signature_certificate() */ + return -1; /* error return from make_signature_certificate() */ } /* end of scope for some buffers */ /* open file g for write, in binary (not text) mode...*/ tempring = tempfile(TMP_TMPDIR); - if ((g = fopen(tempring,FOPWBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't create output file to update key ring.\n")); + if ((g = fopen(tempring,FOPWBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't create output file to update key ring.\n")); fclose(f); - return(-1); + return -1; } /* Copy pre-key and key to file g */ @@ -1257,16 +1190,16 @@ above user ID (y/N)? ")); copyfile (f, g, -1L); fclose(f); - if (write_error(g)) - { fclose(g); - return(-1); + if (write_error(g)) { + fclose(g); + return -1; } fclose(g); savetempbak(tempring,keyfile); - fprintf(pgpout, PSTR("\nKey signature certificate added.\n")); - return(0); /* normal return */ + fprintf(pgpout, LANG("\nKey signature certificate added.\n")); + return 0; /* normal return */ } /* signkey */ @@ -1275,17 +1208,18 @@ above user ID (y/N)? ")); int check_signaturefile(char *infile, char *outfile, boolean strip_signature, char *preserved_name) -{ /* Check signature in infile for validity. Strip off the signature - and write the remaining packet to outfile. If strip_signature, - also write the signature to outfile.sig. - the original filename is stored in preserved_name - */ +/* Check signature in infile for validity. Strip off the signature + * and write the remaining packet to outfile. If strip_signature, + * also write the signature to outfile.sig. + * the original filename is stored in preserved_name + */ +{ byte ctb,ctb2=0; /* Cipher Type Bytes */ char keyfile[MAX_PATH]; /* for getpublickey */ char sigfile[MAX_PATH]; /* .sig file if strip_signature */ char plainfile[MAX_PATH]; /* buffer for getstring() */ #ifndef CANONICAL_TEXT - char *tempFileName; /* Name for temporary uncanonicalized file */ + char *tempFileName; /* Name for temporary uncanonicalized file */ FILE *tempFile; #endif /* !CANONICAL_TEXT */ long fp; @@ -1304,7 +1238,7 @@ int check_signaturefile(char *infile, ch byte *timestamp = (byte *) &tstamp; /* key certificate timestamp */ word32 dummystamp; byte userid[256]; - MD5_CTX MD; + struct MD5Context MD; byte digest[16]; boolean separate_signature; boolean fixedLiteral = FALSE; /* Whether it's a fixed literal2 packet */ @@ -1332,7 +1266,7 @@ int check_signaturefile(char *infile, ch set_precision(MAX_UNIT_PRECISION); /* safest opening assumption */ - buildfilename(keyfile,PUBLIC_KEYRING_FILENAME); /* use default pathname */ + strcpy(keyfile, globalPubringName); /* use default pathname */ if (verbose) fprintf(pgpout,"check_signaturefile: infile = '%s', outfile = '%s'\n", @@ -1342,9 +1276,9 @@ int check_signaturefile(char *infile, ch *preserved_name = '\0'; /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(infile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open ciphertext file '%s'\n"),infile); - return(-1); + if ((f = fopen(infile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open ciphertext file '%s'\n"),infile); + return -1; } /******************** Read header CTB and length field ******************/ @@ -1367,28 +1301,28 @@ int check_signaturefile(char *infile, ch goto badcert; /* complain and return bad status */ version = *certificate++; - if (version_error(version, VERSION_BYTE)) + if (version_byte_error(version)) goto err1; mdlensave = mdlen = *certificate++; /* length of material to be added to MD */ mdextras = certificate; /* pointer to extra material for MD calculation */ class = *certificate++; - if (class != SM_SIGNATURE_BYTE && class != SB_SIGNATURE_BYTE) - { (void) version_error(class, SM_SIGNATURE_BYTE); + if (class != SM_SIGNATURE_BYTE && class != SB_SIGNATURE_BYTE) { + (void) version_error(class, SM_SIGNATURE_BYTE); goto err1; } mdlen--; - if (mdlen>0) /* if more MD material is included... */ - { for (i=0; i0) { /* if more MD material is included... */ + for (i=0; i0) /* if more MD material is included... */ - { certificate+=2; /* skip past unused validity period field */ + if (mdlen>0) { /* if more MD material is included... */ + certificate+=2; /* skip past unused validity period field */ mdlen-=2; } @@ -1423,45 +1357,42 @@ int check_signaturefile(char *infile, ch start_text = ftell(f); /* mark position of text for later */ - if (fread(outbuf,1,1,f) < 1) /* see if any plaintext is there */ - { /* Signature certificate has no plaintext following it. + if (fread(outbuf,1,1,f) < 1) { /* see if any plaintext is there */ + /* Signature certificate has no plaintext following it. Must be in another file. Go look. */ separate_signature = TRUE; if (preserved_name) /* let caller know there is no output file */ strcpy(preserved_name, "/dev/null"); fclose(f); - fprintf(pgpout,PSTR("\nFile '%s' has signature, but with no text."),infile); - if (myArgc > 3 && file_exists(myArgv[3])) - { outfile = myArgv[3]; - fprintf(pgpout,PSTR("\nText is assumed to be in file '%s'.\n"),outfile); - } - else - { strcpy(plainfile, outfile); + fprintf(pgpout,LANG("\nFile '%s' has signature, but with no text."),infile); + if (myArgc > 3 && file_exists(myArgv[3])) { + outfile = myArgv[3]; + fprintf(pgpout,LANG("\nText is assumed to be in file '%s'.\n"),outfile); + } else { + strcpy(plainfile, outfile); outfile = plainfile; drop_extension(outfile); - if (file_exists(outfile)) - fprintf(pgpout,PSTR("\nText is assumed to be in file '%s'.\n"),outfile); - else - { + if (file_exists(outfile)) { + fprintf(pgpout,LANG("\nText is assumed to be in file '%s'.\n"),outfile); + } else { if (batchmode) return -1; - fprintf(pgpout,PSTR("\nPlease enter filename of text that signature applies to: ")); + fprintf(pgpout,LANG("\nPlease enter filename of material that signature applies to: ")); getstring(outfile,59,TRUE); /* echo keyboard */ if ((int)strlen(outfile) == 0) - return(-1); + return -1; } } /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(outfile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open file '%s'\n"),outfile); - return(-1); + if ((f = fopen(outfile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open file '%s'\n"),outfile); + return -1; } start_text = ftell(f); /* mark position of text for later */ text_len = fsize(f); /* remember length of text */ - } /* had to open new input file */ - else - { separate_signature = FALSE; + } else { + separate_signature = FALSE; /* We just read 1 byte, so outbuf[0] should contain a ctb, maybe a CTB_LITERAL byte. */ ctb2 = outbuf[0]; @@ -1473,13 +1404,14 @@ int check_signaturefile(char *infile, ch fread (&lit_mode,1,1,f); /* get literal packet mode byte */ if (lit_mode != MODE_TEXT && lit_mode != MODE_BINARY && lit_mode != MODE_LOCAL) - { fprintf(pgpout,"\n\007Error: Illegal mode byte %02x in literal packet.\n", + { + fprintf(pgpout,"\n\007Error: Illegal mode byte %02x in literal packet.\n", lit_mode); /* English-only diagnostic for debugging */ (void) version_error(lit_mode, MODE_BINARY); goto err1; } if (verbose) - fprintf(pgpout, PSTR("File type: '%c'\n"), lit_mode); + fprintf(pgpout, LANG("File type: '%c'\n"), lit_mode); /* Read literal file name, use it if possible */ litfile[0] = 0; fread (litfile,1,1,f); @@ -1488,18 +1420,19 @@ int check_signaturefile(char *infile, ch the filename and the timestamp and mode byte and litfile length byte */ text_len -= litfile[0] + sizeof(dummystamp) + 2; if (litfile[0] > 0) - { if ((int)litfile[0] >= MAX_PATH) - { fseek(f, litfile[0], SEEK_CUR); + { + if ((int)litfile[0] >= MAX_PATH) { + fseek(f, litfile[0], SEEK_CUR); litfile[0] = 0; + } else { + fread (litfile+1,1,litfile[0],f); } - else - fread (litfile+1,1,litfile[0],f); } /* Use litfile if it's writeable and he didn't say an outfile */ - if (litfile[0]) - { PascalToC( (char *)litfile ); + if (litfile[0]) { + PascalToC( (char *)litfile ); if (verbose) - fprintf(pgpout, PSTR("Original plaintext file name was: '%s'\n"), litfile); + fprintf(pgpout, LANG("Original plaintext file name was: '%s'\n"), litfile); if (preserved_name) strcpy(preserved_name, (char *) litfile); } @@ -1515,9 +1448,10 @@ int check_signaturefile(char *infile, ch fread(&fdl_len, 2, 1, f); fdl = (char *) malloc(fdl_len); fread(fdl, 1, fdl_len, f); - if ((g = fdl_create( fdl, fdl_len, outfile, (char *) litfile)) == NULL) { + if ((g = fdl_create( fdl, fdl_len, outfile, (char *) litfile)) == NULL) + { fprintf(pgpout,"\n\007Unable to create file %s\n", outfile); - return(-1); + return -1; } free(fdl); if (preserved_name) @@ -1526,7 +1460,7 @@ int check_signaturefile(char *infile, ch #endif /* VMS */ } else { fprintf(pgpout,"\n\007Unrecognised local binary type %s\n",org_sys); - return(-1); + return -1; } } else { /* Discard file creation timestamp for now */ @@ -1542,7 +1476,7 @@ int check_signaturefile(char *infile, ch if (getpublickey(0, keyfile, &fp, NULL, keyID, (byte *)&dummystamp, userid, n, e) < 0) { /* Can't get public key. Complain and process file copy anyway. */ - fprintf(pgpout,PSTR("\n\007WARNING: Can't find the right public key-- can't check signature integrity.\n")); + fprintf(pgpout,LANG("\nWARNING: Can't find the right public key-- can't check signature integrity.\n")); goto outsig; } /* Can't find public key */ @@ -1551,20 +1485,31 @@ int check_signaturefile(char *infile, ch if (!quietmode) fputc('.',pgpout); /* Signal RSA completion. */ - if (count == -1) - { fprintf(pgpout,PSTR("\n\007Error: RSA-decrypted block is corrupted.\n\ -This may be caused either by corrupted data or by using the wrong RSA key.\n")); - goto outsig; /* Output data anyway */ - } - /* outbuf should contain message digest packet */ /*==================================================================*/ /* Look at nested stuff within RSA block... */ - if (count == -2 || count != sizeof(digest)) - { fprintf(pgpout,PSTR("\007\nUnrecognized message digest algorithm.\n")); - fprintf(pgpout,PSTR("This may require a newer version of PGP.\n")); - fprintf(pgpout,PSTR("Can't check signature integrity.\n")); + if (count == -7 || (count > 0 && count != sizeof(digest))) + { + fputs(LANG("\007\nUnrecognized message digest algorithm.\n\ +This may require a newer version of PGP.\n\ +Can't check signature integrity.\n"), pgpout); + goto outsig; /* Output data anyway */ + } + if (count == -5) { /* RSAREF returned malformed */ + fputs(LANG("\a\nMalformed or obsolete signature. Can't check signature integrity.\n"), + pgpout); + goto outsig; + } + if (count == -3) { /* Key too big */ + fputs(LANG("\a\nSigning key is too large. RSA keys may be no longer than 1024 bits,\n\ +due to limitations imposed by software provided by RSADSI.\n\ +Can't check signature integrity.\n"), pgpout); + goto outsig; + } + if (count < 0) { /* Catch-all */ + fprintf(pgpout,LANG("\n\007Error: RSA-decrypted block is corrupted.\n\ +This may be caused either by corrupted data or by using the wrong RSA key.\n")); goto outsig; /* Output data anyway */ } @@ -1573,7 +1518,8 @@ This may be caused either by corrupted d if (outbuf[outbufoffset] != mdlow2[0] || outbuf[outbufoffset+1] != mdlow2[1]) - { fprintf(pgpout,PSTR("\n\007Error: RSA-decrypted block is corrupted.\n\ + { + fprintf(pgpout,LANG("\n\007Error: RSA-decrypted block is corrupted.\n\ This may be caused either by corrupted data or by using the wrong RSA key.\n")); goto outsig; /* Output data anyway */ } @@ -1590,18 +1536,18 @@ This may be caused either by corrupted d /* now compare computed MD with claimed MD */ /* Assume MSB external byte ordering */ - if (!equal_buffers(digest, outbuf+outbufoffset, 16)) - { + if (!equal_buffers(digest, outbuf+outbufoffset, 16)) { #ifndef CANONICAL_TEXT - /* IF the signature is bad, AND this machine does not use MSDOS-style - canonical text as its native text format, AND this is a detached - signature certificate, AND this file appears to contain non- - canonical ASCII text, THEN we convert the file to canonical text - form and check the signature again. This is because a detached - signature certificate probably means the file is not currently in - a canonical text packet, but it was in canonical text form when - the signature was created, so by re-canonicalizing it we can - check the signature. */ + /* IF the signature is bad, AND this machine does not use + MSDOS-stype canonical text as its native text format, AND + this is a detached signature certificate, AND this file + appears to contain non-canonical ASCII text, THEN we + convert the file to canonical text form and check the + signature again. This is because a detached signature + certificate probably means the file is not currently in + a canonical text packet, but it was in canonical text form + when the signature was created, so by re-canonicalizing + it we can check the signature. */ if (class == SM_SIGNATURE_BYTE && separate_signature && is_text_file(outfile)) { /* Reposition file to where the plaintext begins and canonicalize it */ rewind( f ); @@ -1630,13 +1576,13 @@ This may be caused either by corrupted d } #endif /* !CANONICAL_TEXT */ - fprintf(pgpout,PSTR("\007\nWARNING: Bad signature, doesn't match file contents!\007\n")); - fprintf(pgpout,PSTR("\nBad signature from user \"%s\".\n"), + fprintf(pgpout,LANG("\007\nWARNING: Bad signature, doesn't match file contents!\007\n")); + fprintf(pgpout,LANG("\nBad signature from user \"%s\".\n"), LOCAL_CHARSET((char *)userid)); - fprintf(pgpout,PSTR("Signature made %s\n"),ctdate((word32 *)timestamp)); - if (moreflag && !batchmode) - { /* more will scroll the message off the screen */ - fprintf(pgpout, PSTR("\nPress ENTER to continue...")); + fprintf(pgpout,LANG("Signature made %s\n"),ctdate((word32 *)timestamp)); + if (moreflag && !batchmode) { + /* more will scroll the message off the screen */ + fprintf(pgpout, LANG("\nPress ENTER to continue...")); fflush(pgpout); getyesno('n'); } @@ -1645,9 +1591,9 @@ This may be caused either by corrupted d goodsig: signature_checked = TRUE; /* set flag for batch processing */ - fprintf(pgpout,PSTR("\nGood signature from user \"%s\".\n"), + fprintf(pgpout,LANG("\nGood signature from user \"%s\".\n"), LOCAL_CHARSET((char *)userid)); - fprintf(pgpout,PSTR("Signature made %s\n"),ctdate((word32 *)timestamp)); + fprintf(pgpout,LANG("Signature made %s\n"),ctdate((word32 *)timestamp)); warnsig: /* warn only, don't ask if user wants to use the key */ @@ -1660,10 +1606,10 @@ outsig: if (separate_signature) { if (!quietmode) - fprintf(pgpout,PSTR("\nSignature and text are separate. No output file produced. ")); - } - else /* signature precedes plaintext in file... */ - { /* produce a plaintext output file from signature file */ + fprintf(pgpout,LANG("\nSignature and text are separate. No output file produced. ")); + } else { + /* signature precedes plaintext in file... */ + /* produce a plaintext output file from signature file */ /* open file g for write, in binary or text mode...*/ if (lit_mode==MODE_LOCAL) { #ifdef VMS @@ -1677,53 +1623,51 @@ outsig: } fdl_close(g); #endif /*VMS */ - } - else - { + } else { if (lit_mode == MODE_BINARY) g = fopen(outfile, FOPWBIN); else g = fopen(outfile, FOPWTXT); - if (g == NULL) - { fprintf(pgpout,PSTR("\n\007Can't create plaintext file '%s'\n"),outfile); - goto err1; - } - CONVERSION = (lit_mode == MODE_TEXT) ? EXT_CONV : NO_CONV; - if (lit_mode == MODE_BINARY) + if (g == NULL) { + fprintf(pgpout,LANG("\n\007Can't create plaintext file '%s'\n"),outfile); + goto err1; + } + CONVERSION = (lit_mode==MODE_TEXT)?EXT_CONV:NO_CONV; + if (lit_mode == MODE_BINARY) status = copyfile(f, g, text_len); else status = copyfile_from_canon(f, g, text_len); CONVERSION = NO_CONV; - if (write_error(g) || status < 0) - { fclose(g); + if (write_error(g) || status < 0) { + fclose(g); goto err1; } fclose(g); } - if (strip_signature) - { /* Copy signature to a .sig file */ + if (strip_signature) { + /* Copy signature to a .sig file */ strcpy (sigfile, outfile); force_extension(sigfile,SIG_EXTENSION); - if (!force_flag && file_exists(sigfile)) - { fprintf(pgpout,PSTR("\n\007Signature file '%s' already exists. Overwrite (y/N)? "), + if (!force_flag && file_exists(sigfile)) { + fprintf(pgpout,LANG("\n\007Signature file '%s' already exists. Overwrite (y/N)? "), sigfile); if (!getyesno('n')) goto err1; } - if ((g = fopen(sigfile,FOPWBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't create signature file '%s'\n"),sigfile); + if ((g = fopen(sigfile,FOPWBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't create signature file '%s'\n"),sigfile); goto err1; } fseek (f,0L,SEEK_SET); copyfile (f,g,(unsigned long)(cert_length+ctb_llength(ctb)+1)); - if (write_error(g)) - { fclose(g); + if (write_error(g)) { + fclose(g); goto err1; } fclose(g); if (!quietmode) - fprintf(pgpout,PSTR("\nWriting signature certificate to '%s'\n"),sigfile); + fprintf(pgpout,LANG("\nWriting signature certificate to '%s'\n"),sigfile); } } @@ -1731,23 +1675,23 @@ outsig: burn(outbuf); /* burn sensitive data on stack */ fclose(f); if (separate_signature) - return(0); /* normal return, no nested info */ + return 0; /* normal return, no nested info */ if (is_ctb(ctb2) && (is_ctb_type(ctb2,CTB_LITERAL_TYPE) || fixedLiteral)) /* we already stripped away the CTB_LITERAL */ - return(0); /* normal return, no nested info */ + return 0; /* normal return, no nested info */ /* Otherwise, it's best to assume a nested CTB */ - return(1); /* nested information return */ + return 1; /* nested information return */ badcert: /* Bad packet. Complain. */ - fprintf(pgpout,PSTR("\n\007Error: Badly-formed or corrupted signature certificate.\n")); - fprintf(pgpout,PSTR("File \"%s\" does not have a properly-formed signature.\n"),infile); + fprintf(pgpout,LANG("\n\007Error: Badly-formed or corrupted signature certificate.\n")); + fprintf(pgpout,LANG("File \"%s\" does not have a properly-formed signature.\n"),infile); /* Now just drop through to error exit... */ err1: burn(inbuf); /* burn sensitive data on stack */ burn(outbuf); /* burn sensitive data on stack */ fclose(f); - return(-1); /* error return */ + return -1; /* error return */ } /* check_signaturefile */ @@ -1755,11 +1699,23 @@ err1: int check_key_sig(FILE *fkey, long fpkey, int keypktlen, char *keyuserid, FILE *fsig, long fpsig, char *keyfile, char *siguserid, byte *xtimestamp, byte *sigclass) -{ /* Check signature of key in file fkey at position fpkey, using signature - in file fsig and position fpsig. keyfile tells the file to use to - look for the public key in to check the sig. Return 0 if OK, -1 if - we can't check the signature, -2 if bad or other problem. - */ +/* + * Check signature of key in file fkey at position fpkey, using signature + * in file fsig and position fpsig. keyfile tells the file to use to + * look for the public key in to check the sig. Return 0 if OK, + * -1 generic error + * -2 Can't find key + * -3 Key too big + * -4 Key too small + * -5 Maybe malformed RSA + * -6 Unknown PK algorithm + * -7 Unknown conventional algorithm + * -8 Unknown version + * -9 Malformed RSA packet + * -10 Malformed packet + * -20 BAD SIGNATURE + */ +{ byte ctb; /* Cipher Type Bytes */ long fp; word16 cert_length; @@ -1770,9 +1726,8 @@ int check_key_sig(FILE *fkey, long fpkey byte inbuf[MAX_BYTE_PRECISION]; byte outbuf[MAX_BYTE_PRECISION]; byte keyID[KEYFRAGSIZE]; - MD5_CTX MD; + struct MD5Context MD; byte digest[16]; - byte mdlensave; byte *mdextras; word32 tstamp; byte *timestamp = (byte *) &tstamp; /* key certificate timestamp */ @@ -1794,59 +1749,50 @@ int check_key_sig(FILE *fkey, long fpkey *certificate++ = ctb; /* copy ctb into certificate */ if (!is_ctb(ctb) || !is_ctb_type(ctb,CTB_SKE_TYPE)) - goto badcert2; /* complain and return bad status */ + goto badcert; /* complain and return bad status */ cert_length = getpastlength(ctb, fsig); /* read certificate length */ certificate += ctb_llength(ctb); /* either 1, 2, 4, or 8 */ if (cert_length > MAX_SIGCERT_LENGTH-3) /* Huge packet length */ - goto badcert2; /* complain and return bad status */ + goto badcert; /* complain and return bad status */ /* read whole certificate: */ if (fread((byteptr) certificate, 1, cert_length, fsig) < cert_length) /* bad packet length field */ - goto badcert2; /* complain and return bad status */ + goto badcert; /* complain and return bad status */ version = *certificate++; - if (version_error(version, VERSION_BYTE)) - goto err2; + if (version_byte_error(version)) + return -8; + + mdlen = *certificate++; /* length of material to be added to MD */ + if (version_error(mdlen, 5)) + return -8; - mdlensave = mdlen = *certificate++; /* length of material to be added to MD */ mdextras = certificate; /* pointer to extra material for MD calculation */ *sigclass = class = *certificate++; if (class != K0_SIGNATURE_BYTE && class != K1_SIGNATURE_BYTE && class != K2_SIGNATURE_BYTE && class != K3_SIGNATURE_BYTE && class != KC_SIGNATURE_BYTE) - { (void) version_error(class, K0_SIGNATURE_BYTE); - goto err2; - } - mdlen--; - - if (mdlen>0) /* if more MD material is included... */ - { for (i=0; i0) /* if more MD material is included... */ - { certificate+=2; /* skip past unused validity period word */ - mdlen-=2; + { + (void)version_error(class, K0_SIGNATURE_BYTE); + return -8; } - if (mdlen>0) /* if more MD material is included... */ - certificate+=mdlen; /* skip over the rest */ + for (i=0; ihash, sizeof(ideakey)); + if (passwds) { + memcpy(ideakey, passwds->hash, sizeof(ideakey)); memset(passwds->hash, 0, sizeof(passwds->hash)); hpw = passwds; passwds = passwds->next; free(hpw); - } - else - { if (!quietmode) - fprintf(pgpout,PSTR("\nYou need a pass phrase to encrypt the file. ")); + } else { + if (!quietmode) + fprintf(pgpout,LANG("\nYou need a pass phrase to encrypt the file. ")); if (batchmode || GetHashedPassPhrase((char *)ideakey,NOECHO2) <= 0) - { fclose(f); + { + fclose(f); fclose(g); - return(-1); + return -1; } } + /* + * Get an initial vector, and write out a new randseed.bin. + * We do this now so that we can use the random bytes from the + * user's keystroke timings. + */ + make_random_ideakey(ideakey, 16); - if (!quietmode) - { fprintf(pgpout,PSTR("Just a moment...")); /* this may take a while */ + if (!quietmode) { + fprintf(pgpout,LANG("Just a moment...")); /* this may take a while */ fflush(pgpout); } @@ -2113,13 +2043,13 @@ int idea_encryptfile(char *infile, char burn(ideakey); /* burn sensitive data on stack */ fclose(f); - if (write_error(g)) - { fclose(g); + if (write_error(g)) { + fclose(g); return -1; } fclose(g); - return(0); + return 0; } /* idea_encryptfile */ @@ -2128,6 +2058,23 @@ int idea_encryptfile(char *infile, char static byte (*keyID_list)[KEYFRAGSIZE] = NULL; +static int getmyname(char *userid) { + char keyfile[MAX_PATH]; + unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION]; + word32 tstamp; byte *timestamp = (byte *) &tstamp; + long fp; + int pktlen; + + strcpy(keyfile, globalSecringName); + + getpublickey(GPK_SECRET, keyfile, &fp, + NULL, NULL, timestamp, (unsigned char *)userid, n, e); + + PascalToC((char *)userid); + + return(0); +} + int encryptfile(char **mcguffins, char *infile, char *outfile, boolean attempt_compression) { @@ -2152,16 +2099,16 @@ int encryptfile(char **mcguffins, char * /* open file f for read, in binary (not text) mode...*/ if ((f = fopen( infile, FOPRBIN )) == NULL) { - fprintf(pgpout,PSTR("\n\007Can't open plaintext file '%s'\n"), infile ); - return(-1); + fprintf(pgpout,LANG("\n\007Can't open plaintext file '%s'\n"), infile ); + return -1; } /* open file g for write, in binary (not text) mode...*/ if ((g = fopen( outfile, FOPWBIN )) == NULL) { - fprintf(pgpout,PSTR("\n\007Can't create ciphertext file '%s'\n"), outfile ); + fprintf(pgpout,LANG("\n\007Can't create ciphertext file '%s'\n"), outfile ); fclose(f); - return(-1); + return -1; } /* Now we have to generate a random session key and IV. @@ -2182,8 +2129,9 @@ int encryptfile(char **mcguffins, char * without compatibility worries. */ - if (!already_have_md5) /* Obtain some random bits from the input file */ - { MD5_CTX MD; + if (!already_have_md5) { + /* Obtain some random bits from the input file */ + struct MD5Context MD; MD5Init(&MD); MDfile0_len(&MD, f, 4096); /* Ignore errors - what could be done? */ @@ -2193,7 +2141,7 @@ int encryptfile(char **mcguffins, char * fseek(f, 0, SEEK_SET); /* Get back to the beginning for encryption */ } - ckp_length = make_random_ideakey(ideakey); + ckp_length = make_random_ideakey(ideakey, 0); /* Returns a 24 byte random IDEA key */ /* Assume MSB external byte ordering */ @@ -2225,31 +2173,35 @@ int encryptfile(char **mcguffins, char * keyID_list = xmalloc(i * KEYFRAGSIZE); /* Iterate through users */ for (; *mcguffins && **mcguffins ; ++mcguffins) { - buildfilename(keyfile,PUBLIC_KEYRING_FILENAME); + strcpy(keyfile, globalPubringName); /* use default pathname */ - keys_used = + keys_used = encryptkeyintofile(g, *mcguffins, keybuf, keyfile, ckp_length, keys_used); } /* for */ - if (!keys_used) - { fclose(f); + if (!keys_used) { + fclose(f); fclose(g); free(keyID_list); return -1; } /* encrypt to myself if need be */ - if (encrypt_to_self && *my_name) { - keys_used = - encryptkeyintofile(g, my_name, keybuf, - keyfile, ckp_length, keys_used); + if (encrypt_to_self) { + if (!*my_name) + /* Find our name from our keyring */ + getmyname(my_name); + if (*my_name) + /* If we were successful */ + keys_used = + encryptkeyintofile(g, my_name, keybuf, + keyfile, ckp_length, keys_used); } free(keyID_list); - close_idearand(); - /** Finished with RSA block containing IDEA key. */ + /* Finished with RSA block containing IDEA key. */ /* Now compress the plaintext and encrypt it with IDEA... */ squish_and_idea_file( ideakey, f, g, attempt_compression ); @@ -2258,13 +2210,13 @@ int encryptfile(char **mcguffins, char * burn(ideakey); /* burn sensitive data on stack */ fclose(f); - if (write_error(g)) - { fclose(g); + if (write_error(g)) { + fclose(g); return -1; } fclose(g); - return(0); + return 0; } /* encryptfile */ @@ -2280,7 +2232,6 @@ encryptkeyintofile(FILE *g, char *mcguff byte userid[256]; long fp; int blocksize; - byte ver, alg; byte (*keyp)[KEYFRAGSIZE]; @@ -2298,7 +2249,8 @@ encryptkeyintofile(FILE *g, char *mcguff if (getpublickey((quietmode?0:GPK_SHOW)|GPK_NORVK, keyfile, &fp, NULL, NULL, timestamp, userid, n, e) < 0) - { fprintf(pgpout, PSTR("\n\007Cannot find the public key matching userid '%s'\n\ + { + fprintf(pgpout, LANG("\n\007Cannot find the public key matching userid '%s'\n\ This user will not be able to decrypt this message.\n"), LOCAL_CHARSET(mcguffin)); continue; @@ -2306,13 +2258,13 @@ This user will not be able to decrypt th /* Make sure we haven't already used this key */ extract_keyID(keyID, n); - for (keyp = keyID_list; keyp < keyID_list+keys_used; ++keyp) - { if (!memcmp(keyp, keyID, KEYFRAGSIZE)) + for (keyp = keyID_list; keyp < keyID_list+keys_used; ++keyp) { + if (!memcmp(keyp, keyID, KEYFRAGSIZE)) break; } - if (keyp < keyID_list + keys_used) - { /* This key was already specified. Quietly ignore it. */ + if (keyp < keyID_list + keys_used) { + /* This key was already specified. Quietly ignore it. */ continue; } @@ -2351,7 +2303,19 @@ This user will not be able to decrypt th fprintf(pgpout, "%02X ", keybuf[i]); fprintf(pgpout, "\n"); #endif - rsa_public_encrypt((unitptr)outbuf, keybuf, ckp_length, e, n); + i = rsa_public_encrypt((unitptr)outbuf, keybuf, ckp_length, e, n); + if (i < 0) { + if (i == -4) { + fprintf(pgpout,"\n\007Error: RSA key length must be at least 256 bits.\n"); + } else if (i == -3) { + fputs(LANG("\a\nError: key is too large. RSA keys may be no longer than 1024 bits,\n\ +due to limitations imposed by software provided by RSADSI.\n"), pgpout); + } else { + fprintf(pgpout, "\a\nUnexpected error %d encrypting\n", i); + } + fprintf(pgpout, LANG("Skipping userid %s\n"), mcguffin); + continue; + } /* write out header record to outfile ... */ @@ -2361,15 +2325,13 @@ This user will not be able to decrypt th FALSE); /* Write version byte */ - ver = VERSION_BYTE; - fwrite (&ver, 1, 1, g); + putc(version_byte, g); writekeyID( n, g ); /* write msg prefix fragment of modulus n */ /* Write algorithm byte */ - alg = RSA_ALGORITHM_BYTE; - fwrite (&alg, 1, 1, g); + putc(RSA_ALGORITHM_BYTE, g); /* convert RSA ciphertext block via reg2mpi and * write to file @@ -2383,14 +2345,16 @@ This user will not be able to decrypt th } while (0); - return(keys_used); + return keys_used; } /* encryptkeyintofile */ /*======================================================================*/ int make_literal(char *infile, char *outfile, char lit_mode, char *literalfile) -{ /* Prepend a CTB_LITERAL prefix to a file. Convert to canonical form if - lit_mode is MODE_TEXT. - */ +/* + * Prepend a CTB_LITERAL prefix to a file. Convert to canonical form if + * lit_mode is MODE_TEXT. + */ +{ char lfile[MAX_PATH]; FILE *f; FILE *g; @@ -2412,8 +2376,8 @@ int make_literal(char *infile, char *out #ifdef VMS if (lit_mode == MODE_LOCAL) { if (!(fdl_generate(infile, &fdl, &fdl_len ) & 01)) { - fprintf(pgpout,PSTR("\n\007Can't open input plaintext file '%s'\n"),infile); - return(-1); + fprintf(pgpout,LANG("\n\007Can't open input plaintext file '%s'\n"),infile); + return -1; } } #endif /*VMS*/ @@ -2421,24 +2385,23 @@ int make_literal(char *infile, char *out f = fopen(infile, FOPRTXT); else f = fopen(infile, FOPRBIN); - if (f == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open input plaintext file '%s'\n"),infile); - return(-1); + if (f == NULL) { + fprintf(pgpout,LANG("\n\007Can't open input plaintext file '%s'\n"),infile); + return -1; } flen = fsize(f); /* open file g for write, in binary (not text) mode... */ - if ((g = fopen( outfile,FOPWBIN )) == NULL) - { fprintf(pgpout, PSTR("\n\007Can't create plaintext file '%s'\n"), outfile ); + if ((g = fopen( outfile,FOPWBIN )) == NULL) { + fprintf(pgpout, LANG("\n\007Can't create plaintext file '%s'\n"), outfile ); goto err1; } - if (literalfile == NULL) - { /* Put in a zero byte to indicate no filename */ + if (literalfile == NULL) { + /* Put in a zero byte to indicate no filename */ lfile[0] = '\0'; - } - else - { strcpy( lfile, literalfile ); + } else { + strcpy( lfile, literalfile ); file_to_canon( lfile ); CToPascal( lfile ); } @@ -2461,7 +2424,7 @@ int make_literal(char *infile, char *out #ifdef USE_LITERAL2 fpos = ftell(g); #endif - fwrite ( &lit_mode, 1, 1, g ); /* write lit_mode */ + putc(lit_mode, g); if (lit_mode == MODE_LOCAL) { #ifdef VMS @@ -2495,17 +2458,17 @@ int make_literal(char *infile, char *out rewind (g); write_ctb_len (g, LIT_TYPE, fsize(g)-fpos, TRUE); } - if (write_error(g) || status < 0) - { fclose(g); + if (write_error(g) || status < 0) { + fclose(g); goto err1; } fclose(g); fclose(f); - return(0); /* normal return */ + return 0; /* normal return */ err1: fclose(f); - return(-1); /* error return */ + return -1; /* error return */ } /* make_literal */ #undef LENGTH_FIELD @@ -2515,14 +2478,16 @@ err1: /*======================================================================*/ int strip_literal(char *infile, char *outfile, char *preserved_name, char *lit_mode) -{ /* Strip off literal prefix from infile, copying to outfile. - Get lit_mode and literalfile info from - the prefix. Replace outfile with literalfile unless - literalfile is illegal - the original filename is stored in preserved_name - If lit_mode is MODE_TEXT, convert from canonical form as we - copy the data. - */ +/* + * Strip off literal prefix from infile, copying to outfile. + * Get lit_mode and literalfile info from + * the prefix. Replace outfile with literalfile unless + * literalfile is illegal + * the original filename is stored in preserved_name + * If lit_mode is MODE_TEXT, convert from canonical form as we + * copy the data. + */ +{ byte ctb; /* Cipher Type Byte */ FILE *f; FILE *g; @@ -2544,19 +2509,20 @@ int strip_literal(char *infile, char *ou *preserved_name = '\0'; /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(infile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open input plaintext file '%s'\n"),infile); - return(-1); + if ((f = fopen(infile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open input plaintext file '%s'\n"),infile); + return -1; } fread(&ctb,1,1,f); /* read Cipher Type Byte */ if (!is_ctb(ctb) || !(is_ctb_type(ctb,CTB_LITERAL_TYPE) || is_ctb_type(ctb,CTB_LITERAL2_TYPE))) - { /* debug message in English only -- something got corrupted */ + { + /* debug message in English only -- something got corrupted */ fprintf(pgpout,"\n\007'%s' is not a literal plaintext file.\n",infile); fclose(f); - return(-1); + return -1; } LITlength = getpastlength(ctb, f); /* read packet length */ @@ -2566,43 +2532,44 @@ int strip_literal(char *infile, char *ou fread (lit_mode,1,1,f); if ((*lit_mode != MODE_BINARY) && (*lit_mode != MODE_TEXT) && (*lit_mode != MODE_LOCAL)) - { (void) version_error(*lit_mode, MODE_TEXT); + { + (void) version_error(*lit_mode, MODE_TEXT); fclose(f); - return(-1); + return -1; } if (verbose) - fprintf(pgpout, PSTR("File type: '%c'\n"), *lit_mode); + fprintf(pgpout, LANG("File type: '%c'\n"), *lit_mode); /* Read literal file name, use it if possible */ litfile[0] = 0; fread (litfile,1,1,f); - if (is_ctb_type(ctb, CTB_LITERAL2_TYPE)) - { /* subtract header length: namelength + lengthbyte + modebyte + stamp */ + if (is_ctb_type(ctb, CTB_LITERAL2_TYPE)) { + /* subtract header length: namelength + lengthbyte + modebyte + stamp */ LITlength -= litfile[0] + 2 + sizeof(dummystamp); } /* Use litfile if it's writeable and he didn't say an outfile */ - if (litfile[0] > 0) - { if ((int)litfile[0] >= MAX_PATH) - { fseek(f, litfile[0], SEEK_CUR); + if (litfile[0] > 0) { + if ((int)litfile[0] >= MAX_PATH) { + fseek(f, litfile[0], SEEK_CUR); litfile[0] = 0; - } - else + } else { fread (litfile+1,1,litfile[0],f); + } } - if (litfile[0]) - { PascalToC( (char *)litfile ); + if (litfile[0]) { + PascalToC( (char *)litfile ); if (verbose) - fprintf(pgpout, PSTR("Original plaintext file name was: '%s'\n"), litfile); + fprintf(pgpout, LANG("Original plaintext file name was: '%s'\n"), litfile); if (preserved_name) strcpy(preserved_name, (char *) litfile); } if (*lit_mode == MODE_LOCAL) { - fread(org_sys, 1, 4, f); org_sys[4] = '\0'; + fread(org_sys, 1, 4, f); org_sys[4] = '\0'; #ifdef VMS #define LOCAL_TEST !strncmp("VMS ",org_sys,4) #else #define LOCAL_TEST FALSE #endif - if (LOCAL_TEST) { + if (LOCAL_TEST) { #ifdef VMS remove(outfile); /* Prevent litter, we recreate the file with correct chars. */ fread(&fdl_len, 2, 1, f); @@ -2610,17 +2577,17 @@ int strip_literal(char *infile, char *ou fread(fdl, 1, fdl_len, f); if ((g = fdl_create( fdl, fdl_len, outfile, (char *) litfile)) == NULL) { fprintf(pgpout,"\n\007Unable to create file %s\n", outfile); - return(-1); + return -1; } free(fdl); if (preserved_name) strcpy(preserved_name, (char *) litfile); LITlength -= (fdl_len + sizeof(fdl_len)); #endif /* VMS */ - } else { + } else { fprintf(pgpout,"\n\007Unrecognised local binary type %s\n",org_sys); - return(-1); - } + return -1; + } } else { /* Discard file creation timestamp for now */ fread (&dummystamp, 1, sizeof(dummystamp), f); @@ -2628,23 +2595,23 @@ int strip_literal(char *infile, char *ou if (*lit_mode==MODE_LOCAL) { #ifdef VMS - if (status = fdl_copyfile2bin( f, g, LITlength)) { /* Copy ok? */ + if (status = fdl_copyfile2bin( f, g, LITlength)) { /* Copy ok? */ if (status > 0) fprintf(stderr,"\n...copying to literal file\n"); else perror("\nError copying from work file"); fdl_close(g); goto err1; - } - fdl_close(g); + } + fdl_close(g); #endif /*VMS */ } else { if (*lit_mode == MODE_TEXT) g = fopen(outfile, FOPWTXT); else g = fopen(outfile, FOPWBIN); - if (g == NULL) - { fprintf(pgpout, PSTR("\n\007Can't create plaintext file '%s'\n"), outfile ); + if (g == NULL) { + fprintf(pgpout, LANG("\n\007Can't create plaintext file '%s'\n"), outfile ); goto err1; } /* copy rest of literal plaintext file */ @@ -2654,19 +2621,19 @@ int strip_literal(char *infile, char *ou else status = copyfile_from_canon(f, g, LITlength); CONVERSION = NO_CONV; - if (write_error(g) || status < 0) - { fclose(g); + if (write_error(g) || status < 0) { + fclose(g); goto err1; } fclose(g); } fclose(f); - return(0); /* normal return */ + return 0; /* normal return */ err1: fclose(f); - return(-1); /* error return */ + return -1; /* error return */ } /* strip_literal */ @@ -2703,9 +2670,9 @@ int decryptfile(char *infile, char *outf infile,outfile); /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(infile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open ciphertext file '%s'\n"),infile); - return(-1); + if ((f = fopen(infile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open ciphertext file '%s'\n"),infile); + return -1; } /* Now we have to keep reading in packets until we either get @@ -2730,9 +2697,9 @@ int decryptfile(char *infile, char *outf fread(&ctb,1,1,f); /* read Cipher Type Byte */ if (!is_ctb(ctb)) { - fprintf(pgpout,PSTR("\n\007'%s' is not a cipher file.\n"),infile); + fprintf(pgpout,LANG("\n\007'%s' is not a cipher file.\n"),infile); fclose(f); - return(-1); + return -1; } /* PKE is Public Key Encryption */ @@ -2744,19 +2711,19 @@ int decryptfile(char *infile, char *outf getpastlength(ctb, f); /* read packet length */ /* Read and check version */ - fread (&ver, 1, 1, f); - if (version_error(ver, VERSION_BYTE)) - { fclose (f); - return (-1); - } + ver = getc(f); + if (version_byte_error(ver)) { + fclose (f); + return (-1); + } fread(keyID,1,KEYFRAGSIZE,f); /* read key ID */ /* Use keyID prefix to look up key. */ /* Add this keyID to the list of keys read in */ - if ((nkey = (struct nkey *) malloc(sizeof(struct nkey))) - == NULL) { - fprintf(stderr, PSTR("\n\007Out of memory.\n")); + nkey = (struct nkey *) malloc(sizeof(struct nkey)); + if (nkey == NULL) { + fprintf(stderr, LANG("\n\007Out of memory.\n")); exitPGP(7); } memcpy(nkey->keyID, keyID, KEYFRAGSIZE); @@ -2764,11 +2731,11 @@ int decryptfile(char *infile, char *outf nkeys = nkey; /* Read and check algorithm */ - fread (&alg, 1, 1, f); - if (version_error(alg, RSA_ALGORITHM_BYTE)) - { fclose (f); - return (-1); - } + alg = getc(f); + if (version_error(alg, RSA_ALGORITHM_BYTE)) { + fclose (f); + return (-1); + } if (!gotkey) /* Only do this if we havent already */ /* Get and validate secret key from a key file: */ @@ -2801,19 +2768,28 @@ int decryptfile(char *infile, char *outf if (thiskey) { if (!quietmode) { - fprintf(pgpout,PSTR("Just a moment...")); + fprintf(pgpout,LANG("Just a moment...")); /* RSA will take a while. */ fflush(pgpout); } count = rsa_private_decrypt(outbuf, (unitptr)inbuf, e, d, p, q, u, n); - if (count < 0) - { fprintf(pgpout, -PSTR("\n\007Error: RSA-decrypted block is corrupted.\n\ + if (count < 0) { + if (count == -3) { + fputs(LANG("\a\nError: key is too large. RSA keys may be no longer than 1024 bits,\n\ +due to limitations imposed by software provided by RSADSI.\n"), pgpout); + } else if (count == -9 || count == -7) { + fprintf(pgpout, LANG("\n\007Error: RSA-decrypted block is corrupted.\n\ This may be caused either by corrupted data or by using the wrong RSA key.\n")); + } else if (count == -5) { + fprintf(pgpout, LANG("\n\007Error: RSA block is possibly malformed. Old format, maybe?\n")); + } else { + fprintf(pgpout, "\a\nUnexpected error %d decrypting\n", count); + } fclose(f); - return(-1); + return count; } + if (!quietmode) fputc('.',pgpout); /* Signal RSA completion. */ @@ -2832,7 +2808,7 @@ This may be caused either by corrupted d if (gotkey) /* verbose flag */ fprintf(pgpout,"\nRecipients:\n"); else - fprintf(pgpout,PSTR("\nThis message can only be read by:\n")); + fprintf(pgpout,LANG("\nThis message can only be read by:\n")); for (nkey = nkeys; nkey; nkey = nkey->next) { if ((user = user_from_keyID(nkey->keyID)) == NULL) @@ -2842,8 +2818,8 @@ This may be caused either by corrupted d } endkrent(); } - for (nkey = nkeys; nkey; ) - { nkey = nkey->next; + for (nkey = nkeys; nkey; ) { + nkey = nkey->next; free(nkeys); nkeys = nkey; } @@ -2852,34 +2828,32 @@ This may be caused either by corrupted d * that it can be decrypted properly. Things should be ok once I * reset some stuff here... -derek */ - if (gotkey) - { + if (gotkey) { fseek(f, fpos, SEEK_SET); /* Get back to the Real McCoy! */ set_precision(realprecision); /* reset the precision */ - } - else - { /* No secret key, exit gracefully (NOT!) */ - fprintf(pgpout, PSTR("\n\007You do not have the secret key needed to decrypt this file.\n")); + } else { + /* No secret key, exit gracefully (NOT!) */ + fprintf(pgpout, LANG("\n\007You do not have the secret key needed to decrypt this file.\n")); fclose(f); - return(-1); + return -1; } /* Verify that top of buffer has correct algorithm byte */ --count; /* one less byte to drop algorithm byte */ /* Assume MSB external byte ordering */ - if (version_error(outbuf[0], IDEA_ALGORITHM_BYTE)) - { fclose(f); - return(-1); + if (version_error(outbuf[0], IDEA_ALGORITHM_BYTE)) { + fclose(f); + return -1; } /* Verify checksum */ count -= 2; /* back up before checksum */ /* Assume MSB external byte ordering */ chksum = fetch_word16(outbuf+1+count); - if (chksum != checksum(outbuf+1, count)) - { fprintf(pgpout,PSTR("\n\007Error: RSA-decrypted block is corrupted.\n\ + if (chksum != checksum(outbuf+1, count)) { + fprintf(pgpout,LANG("\n\007Error: RSA-decrypted block is corrupted.\n\ This may be caused either by corrupted data or by using the wrong RSA key.\n")); fclose(f); - return(-1); + return -1; } /* outbuf should contain random IDEA key packet */ @@ -2887,14 +2861,14 @@ This may be caused either by corrupted d /* open file g for write, in binary (not text) mode... */ - if ((g = fopen( outfile, FOPWBIN )) == NULL) - { fprintf(pgpout, PSTR("\n\007Can't create plaintext file '%s'\n"), outfile ); + if ((g = fopen( outfile, FOPWBIN )) == NULL) { + fprintf(pgpout, LANG("\n\007Can't create plaintext file '%s'\n"), outfile ); goto err1; } fread(&ctbCKE,1,1,f); /* read Cipher Type Byte, should be CTB_CKE */ - if (!is_ctb(ctbCKE) || !is_ctb_type(ctbCKE,CTB_CKE_TYPE)) - { /* Should never get here. */ + if (!is_ctb(ctbCKE) || !is_ctb_type(ctbCKE,CTB_CKE_TYPE)) { + /* Should never get here. */ fprintf(pgpout,"\007\nBad or missing CTB_CKE byte.\n"); goto err1; /* Abandon ship! */ } @@ -2904,14 +2878,14 @@ This may be caused either by corrupted d /* Decrypt ciphertext file */ /* Assume MSB external byte ordering */ status = idea_file( outbuf+1, DECRYPT_IT, f, g, flen ); - if (status < 0) - { fprintf(pgpout,PSTR("\n\007Error: Decrypted plaintext is corrupted.\n")); + if (status < 0) { + fprintf(pgpout,LANG("\n\007Error: Decrypted plaintext is corrupted.\n")); } if (!quietmode) fputc('.',pgpout); /* show progress */ - if (write_error(g)) - { fclose(g); + if (write_error(g)) { + fclose(g); goto err1; } fclose(g); @@ -2923,8 +2897,8 @@ This may be caused either by corrupted d mp_burn(q); /* burn sensitive data on stack */ mp_burn(u); /* burn sensitive data on stack */ if (status < 0) /* if idea_file failed, then error return */ - return(status); - return(1); /* always indicate output file has nested stuff in it. */ + return status; + return 1; /* always indicate output file has nested stuff in it. */ err1: fclose(f); @@ -2934,7 +2908,7 @@ err1: mp_burn(p); /* burn sensitive data on stack */ mp_burn(q); /* burn sensitive data on stack */ mp_burn(u); /* burn sensitive data on stack */ - return(-1); /* error return */ + return -1; /* error return */ } /* decryptfile */ @@ -2955,14 +2929,14 @@ int idea_decryptfile(char *infile, char infile,outfile); /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(infile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open ciphertext file '%s'\n"),infile); - return(-1); + if ((f = fopen(infile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open ciphertext file '%s'\n"),infile); + return -1; } /* open file g for write, in binary (not text) mode... */ - if ((g = fopen( outfile, FOPWBIN )) == NULL) - { fprintf(pgpout, PSTR("\n\007Can't create plaintext file '%s'\n"), outfile ); + if ((g = fopen( outfile, FOPWBIN )) == NULL) { + fprintf(pgpout, LANG("\n\007Can't create plaintext file '%s'\n"), outfile ); goto err1; } @@ -2974,8 +2948,8 @@ int idea_decryptfile(char *infile, char { fread(&ctb,1,1,f); /* read Cipher Type Byte, should be CTB_CKE */ - if (!is_ctb(ctb) || !is_ctb_type(ctb,CTB_CKE_TYPE)) - { /* Should never get here. */ + if (!is_ctb(ctb) || !is_ctb_type(ctb,CTB_CKE_TYPE)) { + /* Should never get here. */ fprintf(pgpout,"\007\nBad or missing CTB_CKE byte.\n"); fclose(g); goto err1; /* Abandon ship! */ @@ -2983,42 +2957,41 @@ int idea_decryptfile(char *infile, char flen = getpastlength(ctb, f); /* read packet length */ /* Get IDEA password, hashed */ - if (hpw) - { /* first try environment passwords */ + if (hpw) { + /* first try environment passwords */ memcpy(ideakey, hpw->hash, sizeof(ideakey)); - } - else - { fprintf(pgpout,PSTR("\nYou need a pass phrase to decrypt this file. ")); + } else { + fprintf(pgpout,LANG("\nYou need a pass phrase to decrypt this file. ")); if (batchmode || GetHashedPassPhrase((char *)ideakey,NOECHO1) <= 0) - { fclose(f); + { + fclose(f); fclose(g); - return(-1); + return -1; } } - if (!quietmode) - { fprintf(pgpout,PSTR("Just a moment...")); /* this may take a while */ + if (!quietmode) { + fprintf(pgpout,LANG("Just a moment...")); /* this may take a while */ fflush(pgpout); } status = idea_file( ideakey, DECRYPT_IT, f, g, flen ); - if (status == 0) - { if (hpw) - { /* "Use up" password. */ + if (status == 0) { + if (hpw) { + /* "Use up" password. */ *hpwp = hpw->next; memset(hpw->hash, 0, sizeof(hpw->hash)); free(hpw); } break; } - if (hpw) - { /* Go to next available password */ + if (hpw) { + /* Go to next available password */ hpwp = &hpw->next; hpw = *hpwp; - } - else - { ++retries; - fprintf(pgpout,PSTR("\n\007Error: Bad pass phrase.\n")); + } else { + ++retries; + fprintf(pgpout,LANG("\n\007Error: Bad pass phrase.\n")); } rewind(f); @@ -3030,24 +3003,24 @@ int idea_decryptfile(char *infile, char if (status == 0 && !quietmode) fputc('.',pgpout); /* show progress */ - if (write_error(g)) - { fclose(g); + if (write_error(g)) { + fclose(g); goto err1; } fclose(g); fclose(f); - if (status < 0) /* if idea_file failed, then complain */ - { remove(outfile); /* throw away our mistake */ - return(status); /* error return */ + if (status < 0) { /* if idea_file failed, then complain */ + remove(outfile); /* throw away our mistake */ + return status; /* error return */ } if (!quietmode) - fprintf(pgpout,PSTR("Pass phrase appears good. ")); - return(1); /* always indicate output file has nested stuff in it. */ + fprintf(pgpout,LANG("Pass phrase appears good. ")); + return 1; /* always indicate output file has nested stuff in it. */ err1: fclose(f); - return(-1); /* error return */ + return -1; /* error return */ } /* idea_decryptfile */ @@ -3059,18 +3032,18 @@ int decompress_file(char *infile, char * FILE *f; FILE *g; extern void lzhDecode( FILE *, FILE * ); - extern void unzip( FILE *, FILE * ); - if (verbose) fprintf(pgpout, PSTR("Decompressing plaintext...") ); + extern int unzip( FILE *, FILE * ); + if (verbose) fprintf(pgpout, LANG("Decompressing plaintext...") ); /* open file f for read, in binary (not text) mode...*/ - if ((f = fopen(infile,FOPRBIN)) == NULL) - { fprintf(pgpout,PSTR("\n\007Can't open compressed file '%s'\n"),infile); - return(-1); + if ((f = fopen(infile,FOPRBIN)) == NULL) { + fprintf(pgpout,LANG("\n\007Can't open compressed file '%s'\n"),infile); + return -1; } fread(&ctb,1,1,f); /* read and skip over Cipher Type Byte */ - if (!is_ctb_type( ctb, CTB_COMPRESSED_TYPE )) - { /* Shouldn't get here, or why were we called to begin with? */ + if (!is_ctb_type( ctb, CTB_COMPRESSED_TYPE )) { + /* Shouldn't get here, or why were we called to begin with? */ fprintf(pgpout,"\007\nBad or missing CTB_COMPRESSED byte.\n"); goto err1; /* Abandon ship! */ } @@ -3079,37 +3052,43 @@ int decompress_file(char *infile, char * /* The packet length is ignored. Assume it's huge. */ fread(&ctb,1,1,f); /* read and skip over compression algorithm byte */ - if (ctb != ZIP2_ALGORITHM_BYTE) - { /* We only know how to uncompress deflate-compressed data. We - may hit imploded or Lharc'ed data but treat it as an error just - the same */ - fprintf(pgpout,PSTR("\007\nUnrecognized compression algorithm.\n\ + if (ctb != ZIP2_ALGORITHM_BYTE) { + /* We only know how to uncompress deflate-compressed data. We + may hit imploded or Lharc'ed data but treat it as an error + just the same */ + fprintf(pgpout,LANG("\007\nUnrecognized compression algorithm.\n\ This may require a newer version of PGP.\n")); goto err1; /* Abandon ship! */ } /* open file g for write, in binary (not text) mode... */ - if ((g = fopen( outfile, FOPWBIN )) == NULL) - { fprintf(pgpout, PSTR("\n\007Can't create decompressed file '%s'\n"), outfile ); + if ((g = fopen( outfile, FOPWBIN )) == NULL) { + fprintf(pgpout, LANG("\n\007Can't create decompressed file '%s'\n"), outfile ); goto err1; } - unzip( f, g ); + if (unzip( f, g )) { + fprintf(pgpout,LANG("\n\007Decompression error. Probable corrupted input.\n")); + goto err2; + } + if (verbose) - fprintf(pgpout, PSTR("done. ") ); + fprintf(pgpout, LANG("done. ") ); else if (!quietmode) fputc('.',pgpout); /* show progress */ if (write_error(g)) - { fclose(g); - goto err1; - } + goto err2; + fclose(g); fclose(f); - return(1); /* always indicate output file has nested stuff in it. */ + return 1; /* always indicate output file has nested stuff in it. */ + +err2: + fclose(g); err1: fclose(f); - return(-1); /* error return */ + return -1; /* error return */ -} /* decompress_file */ +} /* decompress_file */