home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / util / pgp / pgpamiga / source / keymgmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  76.8 KB  |  2,725 lines

  1. /*    keymgmt.c  - Key management routines for PGP.
  2.     PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  3.  
  4.     (c) Copyright 1990-1992 by Philip Zimmermann.  All rights reserved.
  5.     The author assumes no liability for damages resulting from the use
  6.     of this software, even if the damage results from defects in this
  7.     software.  No warranty is expressed or implied.
  8.  
  9.     All the source code Philip Zimmermann wrote for PGP is available for
  10.     free under the "Copyleft" General Public License from the Free
  11.     Software Foundation.  A copy of that license agreement is included in
  12.     the source release package of PGP.  Code developed by others for PGP
  13.     is also freely available.  Other code that has been incorporated into
  14.     PGP from other sources was either originally published in the public
  15.     domain or was used with permission from the various authors.  See the
  16.     PGP User's Guide for more complete information about licensing,
  17.     patent restrictions on certain algorithms, trademarks, copyrights,
  18.     and export controls.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #ifdef UNIX
  24. #include <sys/types.h>
  25. #endif
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include "system.h"
  29. #include "mpilib.h"
  30. #include "idea.h"
  31. #include "random.h"
  32. #include "crypto.h"
  33. #include "fileio.h"
  34. #include "keymgmt.h"
  35. #include "rsagen.h"
  36. #include "mpiio.h"
  37. #include "language.h"
  38. #include "pgp.h"
  39. #include "md5.h"
  40. #include "charset.h"
  41. #include "keymaint.h"
  42.  
  43. int init_userhash(void);
  44. void endkrent(void);
  45.  
  46. /*
  47. **    Convert to or from external byte order.
  48. **    Note that convert_byteorder does nothing if the external byteorder
  49. **  is the same as the internal byteorder.
  50. */
  51. #define convert2(x,lx)    convert_byteorder( (byteptr)&(x), (lx) )
  52. #define convert(x)        convert2( (x), sizeof(x) )
  53.  
  54.  
  55. /*
  56.  * check if userid matches the substring, magic characters ^ and $
  57.  * can be used to match start and end of userid.
  58.  * if n is NULL, only return TRUE if substr is an exact match of
  59.  * userid, a substring does not match in this case.
  60.  * the comparison is always case insensitive
  61.  */
  62. static boolean userid_match(char *userid, char *substr,unitptr n)
  63. {
  64.     boolean match_end = FALSE;
  65.     int id_len, sub_len, i;
  66.     char buf[256], sub[256], *p;
  67.  
  68.     if (substr == NULL || *substr == '\0')
  69.         return(TRUE);
  70.     if (userid == NULL || *userid == '\0')
  71.         return(FALSE);
  72.  
  73.     /* Check whether we have an ASCII or hex userID to check for */
  74.     if (n != NULL && substr[ 0 ] == '0' && to_lower( substr[ 1 ] ) == 'x' )
  75.     {
  76.         userid = key2IDstring( n );
  77.         substr += 2;
  78.     }
  79.  
  80.     id_len = strlen(userid);
  81.     for (i = 0; i <= id_len; ++i)
  82.         buf[i] = to_lower(userid[i]);
  83.  
  84.     sub_len = strlen(substr);
  85.     for (i = 0; i <= sub_len; ++i)
  86.         sub[i] = to_lower(substr[i]);
  87.  
  88.     if (n == NULL)
  89.     {
  90.                 return (boolean) !strcmp(buf, sub);
  91.     }
  92. #ifdef MAGIC_MATCH
  93.     if (sub_len > 1 && sub[sub_len - 1] == '$')
  94.     {
  95.         match_end = TRUE;
  96.         sub[--sub_len] = '\0';
  97.     }
  98.     if (*sub == '^')
  99.     {    if (match_end)
  100.             return(!strcmp(buf, sub + 1));
  101.         else
  102.             return(!strncmp(buf, sub + 1, sub_len - 1));
  103.     }
  104. #endif
  105.     if (sub_len > id_len)
  106.         return(FALSE);
  107.  
  108.     if (match_end)
  109.                 return((boolean) !strcmp(buf + id_len - sub_len, sub));
  110.  
  111.     p = buf;
  112.     while ((p = strchr(p, *sub)) != NULL)
  113.     {
  114.         if (strncmp(p, sub, sub_len) == 0)
  115.             return(TRUE);
  116.         ++p;
  117.     }
  118.     return(FALSE);
  119. }
  120.  
  121. int
  122. is_key_ctb (byte ctb)
  123. {
  124.     return(ctb == CTB_CERT_PUBKEY  ||  ctb == CTB_CERT_SECKEY);
  125. }
  126.  
  127.  
  128. /*
  129. **    keyIDstring
  130. **
  131. **    Return printable key fragment, which is an abbreviation of the public
  132. **    key.  Show LEAST significant 64 bits (KEYFRAGSIZE bytes) of modulus,
  133. **    LSB last.  Yes, that's LSB LAST.
  134. **      If PGP26_COMPAT is defined we show 8 bytes.
  135. */
  136.  
  137. char *keyIDstring( byte *keyID )
  138.     {
  139.     short        i;
  140.     char        *bufptr;    /* ptr to Key ID string */
  141.     static char    keyIDbuf[2*KEYFRAGSIZE+1];
  142.  
  143.       /*      only show bottom 3 or 4 bytes of keyID  */
  144.  
  145.     bufptr = keyIDbuf;
  146.     
  147. #ifdef XLOWFIRST
  148.     /*
  149.     **    LSB-first keyID format
  150.     */
  151.  
  152.       for (i =
  153. #ifdef PGP26_COMPAT
  154.            3;
  155. #else
  156.            2;
  157. #endif
  158.            i >= 0; i--)
  159.         {
  160.         sprintf( bufptr, "%02X", keyID[i] );
  161.         bufptr += 2;
  162.         }
  163. #else
  164.     /*
  165.     **    MSB-first keyID format
  166.     */
  167.  
  168.       for (i = KEYFRAGSIZE-
  169. #ifdef PGP26_COMPAT
  170.                4;
  171. #else
  172.                3;
  173. #endif
  174.            i < KEYFRAGSIZE; i++)
  175.         {
  176.         sprintf( bufptr, "%02X", keyID[i] );
  177.         bufptr += 2;
  178.         }
  179. #endif
  180.     *bufptr = '\0';
  181.     return( keyIDbuf );
  182.     }    /* keyIDstring */
  183.  
  184.  
  185.  
  186. void extract_keyID(byteptr keyID, unitptr n)
  187. /*    Extract key fragment from modulus n.  keyID byte array must be
  188.     at least KEYFRAGSIZE bytes long.
  189. */
  190. {    byte buf[MAX_BYTE_PRECISION+2];
  191.     short i, j;
  192.  
  193.     fill0(buf,KEYFRAGSIZE+2); /* in case n is too short */
  194.     reg2mpi(buf,n);    /* MUST be at least KEYFRAGSIZE long */
  195. #ifdef XLOWFIRST
  196.     i = reg2mpi(buf,n);    /* MUST be at least KEYFRAGSIZE long */
  197.     /* For LSB-first keyID format, start of keyID is: */
  198.     i = 2;    /* skip over the 2 bytes of bitcount */
  199.     for (j=0; j<KEYFRAGSIZE; )
  200.         keyID[j++] = buf[i++];
  201. #else
  202.     i = reg2mpi(buf,n);    /* MUST be at least KEYFRAGSIZE long */
  203.     /* For MSB-first keyID format, start of keyID is: */
  204.     i = i + 2 - KEYFRAGSIZE;
  205.     for (j=0; j<KEYFRAGSIZE; )
  206.         keyID[j++] = buf[i++];
  207. #endif
  208.  
  209. }    /* extract_keyID */
  210.  
  211.  
  212.  
  213. char *key2IDstring(unitptr n)
  214. /*    Derive the key abbreviation fragment from the modulus n,
  215.     and return printable string of key ID.
  216.     n is key modulus from which to extract keyID.
  217. */
  218. {    byte keyID[KEYFRAGSIZE];
  219.     extract_keyID(keyID, n);
  220.     return (keyIDstring(keyID));
  221. }    /* key2IDstring */
  222.  
  223.  
  224.  
  225. static void showkeyID(byteptr keyID)
  226. /*    Print key fragment, which is an abbreviation of the public key. */
  227. {
  228.     fprintf(pgpout,"%s",keyIDstring(keyID));
  229. }    /* showkeyID */
  230.  
  231.  
  232.  
  233. void writekeyID(unitptr n, FILE *f)
  234. /*    Write message prefix keyID to a file.
  235.     n is key modulus from which to extract keyID.
  236. */
  237. {    byte keyID[KEYFRAGSIZE];
  238.     extract_keyID(keyID, n);
  239.     fwrite(keyID,1,KEYFRAGSIZE,f);
  240. }    /* writekeyID */
  241.  
  242.  
  243.  
  244. static boolean checkkeyID(byte *keyID, unitptr n)
  245. /*    Compare specified keyID with one derived from actual key modulus n. */
  246. {
  247.     byte keyID0[KEYFRAGSIZE];
  248.     if (keyID==NULL) /* no key ID -- assume a good match */
  249.         return (TRUE);
  250.     extract_keyID(keyID0, n);
  251.         return((boolean) equal_buffers(keyID,keyID0,KEYFRAGSIZE));
  252. }    /* checkkeyID */
  253.  
  254.  
  255.  
  256. /* external function prototype, from mpiio.c */
  257. void dump_unit_array(string s, unitptr r);
  258.  
  259. void write_trust (FILE *f, byte trustbyte)
  260. /*    Write a key control packet to f, with the specified trustbyte data.
  261.  */
  262. {
  263.     byte ctb;
  264.     byte keyctrllen;
  265.  
  266.     ctb = CTB_KEYCTRL;
  267.     fwrite(&ctb,1,1,f);        /* write key control header byte */
  268.     keyctrllen = 1;
  269.     fwrite(&keyctrllen,1,1,f);    /* write key control length */
  270.     fwrite(&trustbyte,1,1,f);    /* write key control */
  271. }
  272.  
  273. static
  274. short writekeyfile(char *fname, boolean hidekey, byte *timestamp, byte *userid, 
  275.     unitptr n, unitptr e, unitptr d, unitptr p, unitptr q, unitptr u)
  276. /*    Write key components p, q, n, e, d, and u to specified file.
  277.     hidekey is TRUE iff key should be encrypted.
  278.     userid is a length-prefixed Pascal-type character string. 
  279.     We write three packets: a key packet, a key control packet, and
  280.     a userid packet.  We assume the key being written is our own,
  281.     so we set the control bits for full trust.
  282. */
  283. {    FILE *f;
  284.     byte ctb;
  285.     byte alg, version;
  286.     word16 validity;
  287.     word16 cert_length;
  288.     extern word16 mpi_checksum;
  289.     byte iv[8];
  290.     int i;
  291.  
  292.     /* open file f for write, in binary (not text) mode...*/
  293.     if ((f = fopen(fname,FOPWBIN)) == NULL)
  294.     {    fprintf(pgpout,PSTR("\n\007Unable to create key file '%s'.\n"), fname);
  295.         return(-1);
  296.     }
  297.     /*** Begin key certificate header fields ***/
  298.     if (d==NULL)
  299.     {    /* public key certificate */
  300.         ctb = CTB_CERT_PUBKEY;
  301.         cert_length = 1 + SIZEOF_TIMESTAMP + 2 + 1 + (countbytes(n)+2)
  302.             + (countbytes(e)+2);
  303.     }    /* public key certificate */
  304.     else
  305.     {    /* secret key certificate */
  306.         ctb = CTB_CERT_SECKEY;
  307.             cert_length = 1 + SIZEOF_TIMESTAMP + 2 + 1
  308.             + (countbytes(n)+2)
  309.             + (countbytes(e)+2)
  310.             + 1 + (hidekey ? 8 : 0)        /* IDEA algorithm byte and IV */
  311.             + (countbytes(d)+2)
  312.             + (countbytes(p)+2)    + (countbytes(q)+2) 
  313.             + (countbytes(u)+2) + 2;
  314.  
  315.     }    /* secret key certificate */
  316.  
  317.     fwrite(&ctb,1,1,f);        /* write key certificate header byte */
  318.     convert(cert_length);    /* convert to external byteorder */
  319.     fwrite(&cert_length,1,sizeof(cert_length),f);
  320.       version = VERSION_BYTE_OUT;
  321.     fwrite(&version,1,1,f);        /* set version number */
  322.     convert_byteorder(timestamp,4);    /* convert to external form */
  323.     fwrite(timestamp,1,4,f); /* write certificate timestamp */
  324.     convert_byteorder(timestamp,4);    /* convert back to internal form */
  325.     validity = 0;
  326.     fwrite (&validity,1,sizeof(validity),f);    /* validity period */
  327.     alg = RSA_ALGORITHM_BYTE;
  328.     fwrite(&alg,1,1,f);
  329.     write_mpi(n,f,FALSE);
  330.     write_mpi(e,f,FALSE);
  331.  
  332.     if (is_secret_key(ctb))    /* secret key */
  333.     {
  334.         /* Write byte for following algorithm */
  335.         alg = (hidekey)?IDEA_ALGORITHM_BYTE:0;
  336.         fwrite(&alg,1,1,f);
  337.  
  338.         if (hidekey)  /* store encrypted IV */
  339.         {    for (i=0; i<8; i++)
  340.                 iv[i] = randombyte();
  341.             if (hidekey)  /* encrypt the IV */
  342.                 ideacfb(iv,8);
  343.             fwrite(iv,1,8,f);    /* write out the IV */
  344.         }
  345.         mpi_checksum = 0;
  346.         write_mpi(d,f,hidekey);
  347.         write_mpi(p,f,hidekey);
  348.         write_mpi(q,f,hidekey);
  349.         write_mpi(u,f,hidekey);
  350.         /* Write checksum here - based on plaintext values */
  351.         convert(mpi_checksum);
  352.         fwrite (&mpi_checksum,1,sizeof(mpi_checksum),f);
  353.     } else {
  354.         /* Keyring control packet, public keys only */
  355.         write_trust (f, KC_OWNERTRUST_ULTIMATE | KC_BUCKSTOP);
  356.     }
  357.     /* User ID packet */
  358.     ctb = CTB_USERID;
  359.     fwrite(&ctb,1,1,f);        /* write userid header byte */
  360.     fwrite(userid,1,userid[0]+1,f);    /* write user ID */
  361.     if (d == NULL)    /* only on public keyring */
  362.         write_trust (f, KC_LEGIT_COMPLETE);
  363.     if (write_error(f))
  364.     {    fclose(f);
  365.         return -1;
  366.     }
  367.     fclose(f);
  368.     if (verbose)
  369.         fprintf(pgpout,"%d-bit %s key written to file '%s'.\n",
  370.             countbits(n),
  371.             is_secret_key(ctb) ? "secret" : "public" ,
  372.             fname);
  373.     return(0);
  374. }    /* writekeyfile */
  375.  
  376.  
  377.  
  378. /* Return -1 on EOF, else read next key packet, return its ctb, and
  379.  * advance pointer to beyond the packet.
  380.  * This is short of a "short form" of readkeypacket
  381.  */
  382. short nextkeypacket(FILE *f, byte *pctb)
  383. {
  384.     word32 cert_length;
  385.     int count;
  386.     byte ctb;
  387.  
  388.     *pctb = 0;    /* assume no ctb for caller at first */
  389.     count = fread(&ctb,1,1,f);    /* read key certificate CTB byte */
  390.     if (count==0) return(-1);    /* premature eof */
  391.     *pctb = ctb;    /* returns type to caller */
  392.     if ((ctb != CTB_CERT_PUBKEY) && (ctb != CTB_CERT_SECKEY)  &&
  393.             (ctb != CTB_USERID)  &&  (ctb != CTB_KEYCTRL)  &&
  394.             !is_ctb_type(ctb,CTB_SKE_TYPE) &&
  395.             !is_ctb_type(ctb,CTB_COMMENT_TYPE))
  396.         /* Either bad key packet or X/Ymodem padding detected */
  397.                 return (short) ((ctb == 0x1A) ? -1 : -2);
  398.  
  399.     cert_length = getpastlength(ctb, f); /* read certificate length */
  400.  
  401.     if (cert_length > MAX_KEYCERT_LENGTH-3)
  402.         return(-3);    /* bad length */
  403.  
  404.     fseek(f, cert_length, SEEK_CUR);
  405.     return(0);
  406. } /* nextkeypacket */
  407.  
  408.  
  409. short readkeypacket(FILE *f, boolean hidekey, byte *pctb,
  410.     byte *timestamp, char *userid,
  411.     unitptr n ,unitptr e, unitptr d, unitptr p, unitptr q, unitptr u,
  412.     byte *sigkeyID, byte *keyctrl)
  413. /*    Reads a key certificate from the current file position of file f.
  414.     Depending on the certificate type, it will set the proper fields
  415.     of the return arguments.  Other fields will not be set.
  416.     pctb is always set.
  417.     If the packet is CTB_CERT_PUBKEY or CTB_CERT_SECKEY, it will
  418.     return timestamp, n, e, and if the secret key components are
  419.     present and d is not NULL, it will read, decrypt if hidekey is
  420.     true, and return d, p, q, and u.
  421.     If the packet is CTB_KEYCTRL, it will return keyctrl as that byte.
  422.     If the packet is CTB_USERID, it will return userid.
  423.     If the packet is CTB_COMMENT_TYPE, it won't return anything extra.
  424.     The file pointer is left positioned after the certificate.
  425.  
  426.     If the key could not be read because of a version error or bad
  427.     data, the return value is -6 or -4, the file pointer will be
  428.     positioned after the certificate, only the arguments pctb and
  429.     userid will valid in this case, other arguments are undefined.
  430.     Return value -3 means the error is unrecoverable.
  431.  
  432. */
  433. {
  434.     byte ctb;
  435.     word16 cert_length;
  436.     int count;
  437.     byte version, alg, mdlen;
  438.     word16 validity;
  439.     word16 chksum;
  440.     extern word16 mpi_checksum;
  441.     long next_packet;
  442.     byte iv[8];
  443.  
  444.     /*** Begin certificate header fields ***/
  445.     *pctb = 0;    /* assume no ctb for caller at first */
  446.     count = fread(&ctb,1,1,f);    /* read key certificate CTB byte */
  447.     if (count==0) return(-1);    /* premature eof */
  448.     *pctb = ctb;    /* returns type to caller */
  449.     if ((ctb != CTB_CERT_PUBKEY) && (ctb != CTB_CERT_SECKEY)  &&
  450.             (ctb != CTB_USERID)  &&  (ctb != CTB_KEYCTRL)  &&
  451.             !is_ctb_type(ctb,CTB_SKE_TYPE) &&
  452.             !is_ctb_type(ctb,CTB_COMMENT_TYPE))
  453.         /* Either bad key packet or X/Ymodem padding detected */
  454.                 return (short) ((ctb == 0x1A) ? -1 : -2);
  455.  
  456.     cert_length = getpastlength(ctb, f); /* read certificate length */
  457.  
  458.     if (cert_length > MAX_KEYCERT_LENGTH-3)
  459.         return(-3);    /* bad length */
  460.  
  461.     next_packet = ftell(f) + cert_length;
  462.  
  463.     /* skip packet and return, keeps us in sync when we hit a
  464.        version error or bad data */
  465. #define    SKIP_RETURN(x) \
  466.     do \
  467.     { \
  468.         fseek(f,next_packet,SEEK_SET); \
  469.         return(x); \
  470.     } while(0)
  471.  
  472.     if (ctb == CTB_USERID)
  473.     {    if (cert_length > 255)
  474.             return(-3);            /* Bad length error */
  475.         if (userid)
  476.         {    userid[0] = cert_length;        /* Save user ID length */
  477.             fread(userid+1,1,cert_length,f); /* read rest of user ID */
  478.         } else
  479.             fseek (f, (long)cert_length, SEEK_CUR);
  480.         return(0);    /* normal return */
  481.     }
  482.     else if (is_ctb_type (ctb, CTB_SKE_TYPE))
  483.     {    if (sigkeyID)
  484.         {    fread(&version,1,1,f);        /* Read version of sig packet */
  485. #ifdef PGP26_COMPAT
  486.                       if (version_error(version, VERSION_BYTE) &&
  487.                           version_error(version, 3))
  488. #else
  489.             if (version_error(version, VERSION_BYTE))
  490. #endif
  491.                 SKIP_RETURN(-6);            /* Need a later version */
  492.             /* Skip timestamp, validity period, and type byte */
  493.             fread(&mdlen, 1, 1, f);
  494.             fseek(f, (long) mdlen, SEEK_CUR);
  495.             /* Read and return KEY ID */
  496.             fread(sigkeyID,1,KEYFRAGSIZE,f);
  497.         }
  498.         SKIP_RETURN(0);    /* normal return */
  499.     }
  500.     else if (ctb == CTB_KEYCTRL)
  501.     {    if (cert_length != 1)
  502.             return(-3);            /* Bad length error */
  503.         if (keyctrl)
  504.             fread(keyctrl,1,cert_length,f);    /* Read key control byte */
  505.         else
  506.             fseek (f, (long)cert_length, SEEK_CUR);
  507.         return(0);    /* normal return */
  508.     }
  509.     else if (!is_key_ctb(ctb))    /* comment or other packet */
  510.         SKIP_RETURN(0);    /* normal return */
  511.  
  512.     /* Here we have a key packet */
  513.     if (n != NULL)
  514.         set_precision(MAX_UNIT_PRECISION);    /* safest opening assumption */
  515.     fread(&version,1,1,f);    /* read and check version */
  516. #ifdef PGP26_COMPAT
  517.       if (version_error(version, VERSION_BYTE) &&
  518.           version_error(version, 3))
  519. #else
  520.     if (version_error(version, VERSION_BYTE))
  521. #endif
  522.         SKIP_RETURN(-6);            /* Need a later version */
  523.     if (timestamp)
  524.     {    fread(timestamp,1,SIZEOF_TIMESTAMP,f);    /* read certificate timestamp */
  525.         convert_byteorder(timestamp,SIZEOF_TIMESTAMP); /* convert from external form */
  526.     } else
  527.         fseek(f, (long)SIZEOF_TIMESTAMP, SEEK_CUR);
  528.     fread(&validity,1,sizeof(validity),f);    /* Read validity period */
  529.     convert(validity);    /* convert from external byteorder */
  530.     /* We don't use validity period yet */
  531.     fread (&alg, 1, 1, f);
  532.     if (version_error(alg, RSA_ALGORITHM_BYTE))
  533.         SKIP_RETURN(-6);            /* Need a later version */
  534.     /*** End certificate header fields ***/
  535.  
  536.     /* We're past certificate headers, now look at some key material...*/
  537.  
  538.     cert_length -= 1 + SIZEOF_TIMESTAMP + 2 + 1;
  539.  
  540.     if (n==NULL)    /* Skip key certificate data */
  541.         SKIP_RETURN(0);
  542.  
  543.     if (read_mpi(n,f,TRUE,FALSE) < 0)
  544.         SKIP_RETURN(-4);    /* data corrupted, return error */
  545.  
  546.     /* Note that precision was adjusted for n */
  547.  
  548.     if (read_mpi(e,f,FALSE,FALSE) < 0)
  549.         SKIP_RETURN(-4);    /* data corrupted, error return */
  550.  
  551.     cert_length -= (countbytes(n)+2) + (countbytes(e)+2);
  552.  
  553.     if (d==NULL)    /* skip rest of this key certificate */
  554.         SKIP_RETURN(0);            /* Normal return */
  555.     if (is_secret_key(ctb))
  556.     {
  557.         fread (&alg,1,1,f);
  558.         if (alg && version_error(alg,IDEA_ALGORITHM_BYTE))
  559.             SKIP_RETURN(-6);            /* Unknown version */
  560.  
  561.         if (!hidekey && alg)
  562.             /* Don't bother trying if hidekey is false and alg is true */
  563.             SKIP_RETURN(-5);
  564.  
  565.         if (alg)    /* if secret components are encrypted... */
  566.         {    /* process encrypted CFB IV before reading secret components */
  567.             count = fread(iv,1,8,f);
  568.             if (count < 8)
  569.                 return(-4);    /* data corrupted, error return */
  570.             ideacfb(iv,8);
  571.             cert_length -= 8;    /* take IV length into account */
  572.         }
  573.  
  574.         /* Reset checksum before these reads */
  575.         mpi_checksum = 0;
  576.  
  577.         if (read_mpi(d,f,FALSE,hidekey) < 0)
  578.             return(-4);    /* data corrupted, error return */
  579.         if (read_mpi(p,f,FALSE,hidekey) < 0)
  580.             return(-4);    /* data corrupted, error return */
  581.         if (read_mpi(q,f,FALSE,hidekey) < 0)
  582.             return(-4);    /* data corrupted, error return */
  583.  
  584.         /* use register 'u' briefly as scratchpad */
  585.         mp_mult(u,p,q);    /* compare p*q against n */
  586.         if (mp_compare(n,u)!=0)    /* bad pass phrase? */
  587.             return(-5);    /* possible bad pass phrase, error return */
  588.         /* now read in real u */
  589.         if (read_mpi(u,f,FALSE,hidekey) < 0)
  590.             return(-4);    /* data corrupted, error return */
  591.  
  592.         /* Read checksum, compare with mpi_checksum */
  593.         fread (&chksum,1,sizeof(chksum),f);
  594.         convert(chksum);
  595.         if (chksum != mpi_checksum)
  596.             return(-5);    /* possible bad pass phrase */
  597.  
  598.         cert_length -= 1 + (countbytes(d)+2) + (countbytes(p)+2)
  599.             + (countbytes(q)+2) + (countbytes(u)+2) + 2;
  600.  
  601.     }    /* secret key */
  602.     else /* not a secret key */
  603.     {    mp_init(d,0);
  604.         mp_init(p,0);
  605.         mp_init(q,0);
  606.         mp_init(u,0);
  607.     }
  608.  
  609.     if (cert_length != 0)
  610.     {    fprintf(pgpout,"\n\007Corrupted key.  Bad length, off by %d bytes.\n",
  611.             (int) cert_length);
  612.         SKIP_RETURN(-4);    /* data corrupted, error return */
  613.     }
  614.  
  615.     return(0);    /* normal return */
  616.  
  617. }    /* readkeypacket */
  618.  
  619.  
  620.  
  621. int getpublickey(int flags, char *keyfile, long *_file_position,
  622.     int *_pktlen, byte *keyID, byte *timestamp, byte *userid,
  623.     unitptr n, unitptr e)
  624. /*    keyID contains key fragment we expect to find in keyfile.
  625.     If keyID is NULL, then userid contains a C string search target of
  626.     userid to find in keyfile.
  627.     keyfile is the file to begin search in, and it may be modified
  628.     to indicate true filename of where the key was found.  It can be
  629.     either a public key file or a secret key file.
  630.     file_position is returned as the byte offset within the keyfile
  631.     that the key was found at.  pktlen is the length of the key packet.
  632.     These values are for the key packet itself, not including any
  633.     following userid, control, signature, or comment packets.
  634.  
  635.     possible flags:
  636.     GPK_GIVEUP: we are just going to do a single file search only.
  637.     GPK_SHOW: show the key if found.
  638.     GPK_NORVK: skip revoked keys.
  639.     GPK_DISABLED: don't ignore disabled keys (when doing userid lookup)
  640.  
  641.     Returns -6 if the key was found but the key was not read because of a
  642.     version error or bad data.  The arguments timestamp, n and e are
  643.     undefined in this case.
  644. */
  645. {
  646.     byte ctb;    /* returned by readkeypacket */
  647.     FILE *f;
  648.     int status, keystatus = -1;
  649.     boolean keyfound = FALSE;
  650.     boolean secret = FALSE;
  651.     char matchid[256];    /* C string format */
  652.     long fpos;
  653.     long file_position = 0;
  654.     int pktlen = 0;
  655.     boolean skip = FALSE;    /* if TRUE: skip until next key packet */
  656.     byte keyctrl;
  657.  
  658.     if (keyID==NULL)    /* then userid has search target */
  659.         strcpy(matchid,(char *)userid);
  660.     else
  661.         matchid[0] = '\0';
  662.  
  663. top:
  664.     if (strlen(keyfile) == 0)    /* null filename */
  665.         return(-1);    /* give up, error return */
  666.  
  667.     default_extension(keyfile,PGP_EXTENSION);
  668.  
  669.     if (!file_exists(keyfile))
  670.     {    if (flags & GPK_GIVEUP)
  671.             return(-1);    /* give up, error return */
  672.         fprintf(pgpout,PSTR("\n\007Keyring file '%s' does not exist. "),keyfile);
  673.         goto nogood;
  674.     }
  675.     if (verbose)
  676.     {    fprintf(pgpout,"searching key ring file '%s' ",keyfile);
  677.         if (keyID)
  678.             fprintf(pgpout, "for keyID %s\n", keyIDstring(keyID));
  679.         else
  680.             fprintf(pgpout, "for userid \"%s\"\n", userid);
  681.     }
  682.  
  683.     /* open file f for read, in binary (not text) mode...*/
  684.     if ((f = fopen(keyfile,FOPRBIN)) == NULL)
  685.         return(-1);    /* error return */
  686.  
  687.     keyfound = FALSE;
  688.     while (TRUE) 
  689.     {
  690.         fpos = ftell(f);
  691.         status = readkeypacket(f,FALSE,&ctb,timestamp,(char *)userid,n,e,
  692.                 NULL,NULL,NULL,NULL,NULL,NULL);
  693.         /* Note that readkeypacket has called set_precision */
  694.  
  695.         if (status == -1)    /* end of file */
  696.             break;
  697.  
  698.         if (status < -1 && status != -4 && status != -6)
  699.         {    fprintf(pgpout,PSTR("\n\007Could not read key from file '%s'.\n"),
  700.                 keyfile);
  701.             fclose(f);    /* close key file */
  702.             return(status);
  703.         }
  704.  
  705.         /* Remember packet position and size for last key packet */
  706.         if (is_key_ctb(ctb))
  707.         {    file_position = fpos;
  708.             pktlen = (int)(ftell(f) - fpos);
  709.             secret = is_ctb_type(ctb, CTB_CERT_SECKEY_TYPE);
  710.             keystatus = status;
  711.             if (!keyID && !(flags & GPK_DISABLED) && !secret
  712.                     && read_trust(f, &keyctrl) == 0
  713.                     && (keyctrl & KC_DISABLED))
  714.                 skip = TRUE;
  715.             else
  716.                 skip = FALSE;
  717.         }
  718.  
  719.         /* Only check for matches when we find a USERID packet */
  720.         if (!skip && ctb == CTB_USERID)
  721.         {    /* keyID contains key fragment.  Check it against n from keyfile. */
  722.             if (keyID!=NULL)
  723.             {    if (keystatus == 0)
  724.                     keyfound = checkkeyID(keyID,n);
  725.             }
  726.             else
  727.             {    /* matchid is already a C string */
  728.                 PascalToC((char *)userid);    /* for C string functions */
  729.                 /* Accept any matching subset */
  730.                 keyfound = userid_match((char *)userid,matchid,n);
  731.                 CToPascal((char *)userid);
  732.             }
  733.         }
  734.  
  735.         if (keyfound)
  736.         {    if (flags & GPK_SHOW)
  737.                 show_key(f, file_position, 0);
  738.             fseek(f, file_position, SEEK_SET);
  739.             if ((flags&GPK_NORVK) && keystatus == 0 && is_compromised(f))
  740.             {
  741.                 if (flags&GPK_SHOW)    /* already printed user ID */
  742.                     fprintf(pgpout, PSTR("\n\007Sorry, this key has been revoked by its owner.\n"));
  743.                 else
  744.                 {
  745.                     PascalToC((char *) userid);
  746.                     fprintf(pgpout, PSTR("\nKey for user ID \"%s\"\n\
  747. has been revoked.  You cannot use this key.\n"), 
  748.                         LOCAL_CHARSET((char *)userid));
  749.                 }
  750.                 keyfound = FALSE;
  751.                 skip = TRUE;
  752.                 /* we're positioned at the key packet, skip it */
  753.                 nextkeypacket(f, &ctb);
  754.             }
  755.             else
  756.             {    /* found key, normal return */
  757.                 if (_pktlen)
  758.                     *_pktlen = pktlen;
  759.                 if (_file_position)
  760.                     *_file_position = file_position;
  761.                 fclose(f);
  762.                 return(keystatus);
  763.             }
  764.         }
  765.     }    /* while TRUE */
  766.  
  767.     fclose(f);    /* close key file */
  768.  
  769.     if (flags & GPK_GIVEUP)
  770.         return(-1);    /* give up, error return */
  771.  
  772.     if (keyID!=NULL)
  773.     {
  774.         fprintf(pgpout,PSTR("\n\007Key matching expected Key ID %s not found in file '%s'.\n"),
  775.             keyIDstring(keyID),keyfile);
  776.     }
  777.     else
  778.     {    fprintf(pgpout,PSTR("\n\007Key matching userid '%s' not found in file '%s'.\n"),
  779.             LOCAL_CHARSET(matchid),keyfile);
  780.     }
  781.  
  782. nogood:
  783.     if (filter_mode || batchmode)
  784.         return(-1);    /* give up, error return */
  785.  
  786.     if (secret)
  787.         fprintf(pgpout,PSTR("Enter secret key filename: "));
  788.     else
  789.         fprintf(pgpout,PSTR("Enter public key filename: "));
  790.  
  791.     getstring(keyfile,59,TRUE);    /* echo keyboard input */
  792.     goto top;
  793.  
  794. }    /* getpublickey */
  795.  
  796.  
  797. int getpubuserid(char *keyfile, long key_position, byte *userid,
  798.     long *userid_position, int *userid_len, boolean exact_match)
  799. /*  Start at key_position in keyfile, and scan for the key packet
  800.     that contains userid.  Return userid_position and userid_len.
  801.     Return 0 if OK, -1 on error.  Userid should be a C string.
  802.     If exact_match is TRUE, the userid must match for full length,
  803.     a substring is not enough.
  804. */
  805. {
  806.     unit n[MAX_UNIT_PRECISION];
  807.     unit e[MAX_UNIT_PRECISION];
  808.     byte ctb;    /* returned by readkeypacket */
  809.     FILE *f;
  810.     int status;
  811.     char userid0[256];    /* C string format */
  812.     long fpos;
  813.  
  814.     /* open file f for read, in binary (not text) mode...*/
  815.     if ((f = fopen(keyfile,FOPRBIN)) == NULL)
  816.         return(-1);    /* error return */
  817.  
  818.     /* Start off at correct location */
  819.     fseek (f, key_position, SEEK_SET);
  820.     (void)nextkeypacket(f, &ctb);    /* Skip key */
  821.     while (TRUE)
  822.     {
  823.         fpos = ftell(f);
  824.         status = readkeypacket(f,FALSE,&ctb,NULL,(char *)userid0,n,e,
  825.                 NULL,NULL,NULL,NULL,NULL,NULL);
  826.  
  827.         if (status < 0  ||  is_key_ctb(ctb))
  828.         {    fclose(f);    /* close key file */
  829.             return(status ? status : -1);    /* give up, error return */
  830.         }
  831.  
  832.         /* Only check for matches when we find a USERID packet */
  833.         if (ctb == CTB_USERID)
  834.         {    if (userid[0] == '0' && userid[1] == 'x')
  835.                 break;    /* use first userid if user specified a keyID */
  836.             /* userid is already a C string */
  837.             PascalToC((char *)userid0);    /* for C string functions */
  838.             /* Accept any matching subset if exact_match is FALSE */
  839.             if (userid_match((char *)userid0, (char *) userid,
  840.                     (exact_match ? NULL : n)))
  841.                 break;
  842.         }
  843.     }    /* while TRUE */
  844.     *userid_position = fpos;
  845.     *userid_len = ( int ) ( ftell(f) - fpos );
  846.     fclose(f);
  847.     return(0);    /* normal return */
  848. }    /* getpubuserid */
  849.  
  850.  
  851. int getpubusersig(char *keyfile, long user_position, byte *sigkeyID,
  852.     long *sig_position, int *sig_len)
  853. /*  Start at user_position in keyfile, and scan for the signature packet
  854.     that matches sigkeyID.  Return sig_position and sig_len.
  855.     Return 0 if OK, -1 on error.
  856. */
  857. {
  858.     byte ctb;    /* returned by readkeypacket */
  859.     FILE *f;
  860.     int status;
  861.     byte keyID0[KEYFRAGSIZE];
  862.     long fpos;
  863.  
  864.     /* open file f for read, in binary (not text) mode...*/
  865.     if ((f = fopen(keyfile,FOPRBIN)) == NULL)
  866.         return(-1);    /* error return */
  867.  
  868.     /* Start off at correct location */
  869.     fseek (f, user_position, SEEK_SET);
  870.     (void)nextkeypacket(f, &ctb);    /* Skip userid packet */
  871.     while (TRUE) 
  872.     {
  873.         fpos = ftell(f);
  874.         status = readkeypacket(f,FALSE,&ctb,NULL,NULL,NULL,NULL,
  875.                 NULL,NULL,NULL,NULL,keyID0,NULL);
  876.  
  877.         if (status < 0  ||  is_key_ctb(ctb)  ||  ctb==CTB_USERID)
  878.             break;
  879.  
  880.         /* Only check for matches when we find a signature packet */
  881.         if (is_ctb_type(ctb,CTB_SKE_TYPE))
  882.         {    if (equal_buffers(sigkeyID,keyID0,KEYFRAGSIZE))
  883.             {    *sig_position = fpos;
  884.                 *sig_len = ( int ) ( ftell(f) - fpos );
  885.                 fclose(f);
  886.                 return(0);    /* normal return */
  887.             }
  888.         }
  889.     }    /* while TRUE */
  890.  
  891.     fclose(f);    /* close key file */
  892.     return(status ? status : -1);    /* give up, error return */
  893. }    /* getpubusersig */
  894.  
  895.  
  896. int getsecretkey(int flags, char *keyfile, byte *keyID,
  897.     byte *timestamp, byte *hpass, boolean *hkey, byte *userid,
  898.         unitptr n, unitptr e, unitptr d, unitptr p, unitptr q,
  899.     unitptr u)
  900. /*    keyID contains key fragment we expect to find in keyfile.
  901.     If keyID is NULL, then userid contains search target of
  902.     userid to find in keyfile.
  903.     giveup controls whether we ask the user for the name of the
  904.     secret key file on failure.  showkey controls whether we print
  905.     out the key information when we find it.  keyfile, if non-NULL,
  906.     is the name of the secret key file; if NULL, we use the
  907.     default.  hpass and hkey, if non-NULL, get returned with a copy
  908.     of the hashed password buffer and hidekey variable.
  909. */
  910. {
  911.     byte ctb;    /* returned by readkeypacket */
  912.     FILE *f;
  913.     char keyfilename[MAX_PATH];    /* for getpublickey */
  914.     long file_position;
  915.     int status;
  916.     boolean hidekey;    /* TRUE iff secret key is encrypted */
  917.     word16 iv[4];        /* initialization vector for encryption */
  918.     byte ideakey[16];
  919.     int guesses = 3;
  920.     struct hashedpw *hpw, **hpwp;
  921.  
  922.     if (keyfile == NULL)
  923.     {    /* use default pathname */
  924.         buildfilename(keyfilename,SECRET_KEYRING_FILENAME);
  925.         keyfile = keyfilename;
  926.     }
  927.  
  928.     status = getpublickey(flags, keyfile, &file_position, NULL, keyID,
  929.         timestamp, userid, n, e);
  930.     if (status < 0)
  931.         return(status);    /* error return */
  932.  
  933.     /* open file f for read, in binary (not text) mode...*/
  934.     if ((f = fopen(keyfile,FOPRBIN)) == NULL)
  935.         return(-1);    /* error return */
  936.  
  937.     /* First guess is no password */
  938.     hidekey = FALSE;
  939.     fseek(f,file_position,SEEK_SET); /* reposition file to key */
  940.     status = readkeypacket(f,hidekey,&ctb,timestamp,(char *)userid,
  941.                     n,e,d,p,q,u,NULL,NULL);
  942.     if (status != -5)    /* Anything except bad password */
  943.         goto done;
  944.  
  945.     /* If we're not signing a key (when we force asking the user),
  946.      * check the prevosuly known passwords.
  947.      */
  948.     if (!(flags & GPK_ASKPASS)) {
  949.         hidekey = TRUE;
  950.         /* Then come existing key passwords */
  951.         hpw = keypasswds;
  952.         while (hpw) {
  953.             fill0(iv,8);
  954.             memcpy(ideakey, hpw->hash, sizeof(ideakey));
  955.             initcfb_idea(iv,ideakey,TRUE);
  956.             fseek(f,file_position,SEEK_SET);
  957.             status = readkeypacket(f,hidekey,&ctb,timestamp,
  958.                     (char *)userid,n,e,d,p,q,u,NULL,NULL);
  959.             close_idea();
  960.             if (status != -5)
  961.                 goto done;
  962.             hpw = hpw->next;
  963.         }
  964.         /* Then try "other" passwords" */
  965.         hpwp = &passwds;
  966.         hpw = *hpwp;
  967.         while (hpw) {
  968.             fill0(iv,8);
  969.             memcpy(ideakey, hpw->hash, sizeof(ideakey));
  970.             initcfb_idea(iv,ideakey,TRUE);
  971.             fseek(f,file_position,SEEK_SET);
  972.             status = readkeypacket(f,hidekey,&ctb,timestamp,
  973.                     (char *)userid,n,e,d,p,q,u,NULL,NULL);
  974.             close_idea();
  975.             if (status >= 0)
  976.             {    /* Success - move to key password list */
  977.                 *hpwp = hpw->next;
  978.                 hpw->next = keypasswds;
  979.                 keypasswds = hpw;
  980.             }
  981.             if (status != -5)
  982.                 goto done;
  983.             hpwp = &hpw->next;
  984.             hpw = *hpwp;
  985.         }
  986.     }
  987.     /* If batchmode, we don't ask the user. */
  988.     if (batchmode)
  989.     {    /* PGPPASS (or -z) wrong or not set */
  990.         fprintf(pgpout,PSTR("\n\007Error:  Bad pass phrase.\n"));
  991.         fclose(f);    /* close key file */
  992.         return -1;
  993.     }
  994.     /* Finally, prompt the user. */
  995.     fprintf(pgpout,PSTR("\nYou need a pass phrase to unlock your RSA secret key. "));
  996.     if (!(flags & GPK_SHOW))
  997.     {    /* let user know for which key he should type his password */
  998.         PascalToC((char *)userid);
  999.         fprintf(pgpout, PSTR("\nKey for user ID \"%s\"\n"), 
  1000.             LOCAL_CHARSET((char *)userid));
  1001.         CToPascal((char *)userid);
  1002.     }
  1003.     do
  1004.     {    hidekey = (GetHashedPassPhrase((char *) ideakey, 1) > 0);
  1005.         fill0(iv,8);
  1006.         initcfb_idea(iv,ideakey,TRUE);
  1007.         fseek(f,file_position,SEEK_SET);
  1008.         status = readkeypacket(f,hidekey,&ctb,timestamp,
  1009.                 (char *)userid,n,e,d,p,q,u,NULL,NULL);
  1010.         close_idea();
  1011.         if (status >= 0)
  1012.         {    /* Success - remember this key for later use */
  1013.             if (flags & GPK_ASKPASS)
  1014.             { /* This may be a duplicate because we didn't
  1015.                * search the lists before - check.
  1016.                */
  1017.                 hpw = passwds;
  1018.                 while (hpw)
  1019.                 {    if (memcmp(hpw->hash, ideakey,
  1020.                                sizeof(ideakey)) == 0)
  1021.                         goto done;
  1022.                     hpw = hpw->next;
  1023.                 }
  1024.                 hpw = keypasswds;
  1025.                 while (hpw)
  1026.                 {    if (memcmp(hpw->hash, ideakey,
  1027.                                sizeof(ideakey)) == 0)
  1028.                         goto done;
  1029.                     hpw = hpw->next;
  1030.                 }
  1031.             }
  1032.             /* Insert new key into remember lists. */
  1033.             hpw = (struct hashedpw *)malloc(sizeof(struct hashedpw));
  1034.             if (hpw)
  1035.             { /* If malloc fails, just don't remember the phrase */
  1036.                 memcpy(hpw->hash, ideakey, sizeof(hpw->hash));
  1037.                 hpw->next = keypasswds;
  1038.                 keypasswds = hpw;
  1039.             }
  1040.         }
  1041.         if (status != -5)
  1042.             goto done;
  1043.         fprintf(pgpout, PSTR("\n\007Error:  Bad pass phrase.\n"));
  1044.     } while (--guesses);
  1045.     /* Failed - fall through to done */
  1046.  
  1047. done:
  1048.     fclose(f);
  1049.     if (hkey)
  1050.         *hkey = hidekey;
  1051.     if (status == -5)
  1052.         return status;
  1053.     if (status < 0)
  1054.     {    fprintf(pgpout,PSTR("\n\007Could not read key from file '%s'.\n"),
  1055.             keyfile);
  1056.         fclose(f);    /* close key file */
  1057.         return(-1);
  1058.     }
  1059.  
  1060.     if (hpass)
  1061.         memcpy(hpass, ideakey, sizeof(ideakey));
  1062.     burn (ideakey);
  1063.  
  1064.     /* Note that readkeypacket has called set_precision */
  1065.  
  1066.     if (d != NULL)    /* No effective check of pass phrase if d is NULL */
  1067.     {    
  1068.         if (!quietmode)
  1069.         {
  1070.             if (!hidekey)
  1071.                 fprintf(pgpout,PSTR("\nAdvisory warning: This RSA secret key is not protected by a passphrase.\n"));
  1072.             else
  1073.                 fprintf(pgpout,PSTR("Pass phrase is good.  "));
  1074.         }
  1075.  
  1076.         if (testeq(d,0))    /* didn't get secret key components */
  1077.         {    fprintf(pgpout,PSTR("\n\007Key file '%s' is not a secret key file.\n"),keyfile);
  1078.             return(-1);
  1079.         }
  1080.     }
  1081.  
  1082.     return(0);    /* normal return */
  1083.  
  1084. }    /* getsecretkey */
  1085.  
  1086.  
  1087. int is_compromised(FILE *f)
  1088. /* check if a key has a compromise certificate, file pointer must
  1089.    be positioned at or right after the key packet.
  1090. */
  1091. {
  1092.     long pos, savepos;
  1093.     byte class, ctb;
  1094.     int cert_len;
  1095.     int status = 0;
  1096.  
  1097.     pos = savepos = ftell(f);
  1098.  
  1099.     nextkeypacket(f, &ctb);
  1100.     if (is_key_ctb(ctb))
  1101.     {    pos = ftell(f);
  1102.         nextkeypacket(f, &ctb);
  1103.     }
  1104.     if (ctb != CTB_KEYCTRL)
  1105.         fseek(f, pos, SEEK_SET);
  1106.  
  1107.     /* file pointer now positioned where compromise cert. should be */
  1108.     if (fread(&ctb, 1, 1, f) != 1)
  1109.     {    status = -1;
  1110.         goto ex;
  1111.     }
  1112.     
  1113.     if (is_ctb_type(ctb, CTB_SKE_TYPE))
  1114.     {
  1115.         cert_len = ( int ) getpastlength(ctb, f);
  1116.         if (cert_len > MAX_SIGCERT_LENGTH)    /* Huge packet length */
  1117.         {    status = -1;
  1118.             goto ex;
  1119.         }
  1120.  
  1121.         /* skip version and mdlen byte */
  1122.         fseek(f, 2L, SEEK_CUR);
  1123.         if (fread(&class, 1, 1, f) != 1)
  1124.         {    status = -1;
  1125.             goto ex;
  1126.         }
  1127.         status = (class == KC_SIGNATURE_BYTE);
  1128.     }
  1129. ex:
  1130.     fseek(f, savepos, SEEK_SET);
  1131.     return(status);
  1132. }
  1133.  
  1134.  
  1135. /*    Alfred Hitchcock coined the term "mcguffin" for the generic object 
  1136.     being sought in his films-- the diamond, the microfilm, etc. 
  1137. */
  1138.  
  1139.  
  1140. /*    Calculate and display a hash for the public components of the key.
  1141.     The components are converted to their external (big-endian) 
  1142.     representation, concatenated, and an MD5 on the bit values 
  1143.     (ie excluding the length value) calculated and displayed in hex.
  1144.  
  1145.     The hash, or "fingerprint", of the key is useful mainly for quickly
  1146.     and easily verifying over the phone that you have a good copy of 
  1147.     someone's public key.  Just read the hash over the phone and have
  1148.     them check it against theirs.
  1149. */
  1150.  
  1151. void getKeyHash( byte *hash, unitptr n, unitptr e )
  1152. {
  1153.     MD5_CTX mdContext;
  1154.     byte buffer[ MAX_BYTE_PRECISION + 2 ];
  1155.     byte mdBuffer[ MAX_BYTE_PRECISION * 2 ];
  1156.     int i, mdIndex = 0, bufIndex;
  1157.  
  1158.     /* Convert n and e to external (big-endian) byte order and move to mdBuffer */
  1159.     i = reg2mpi( buffer, n );
  1160.     for( bufIndex = 2; bufIndex < i + 2; bufIndex++ )    /* +2 skips count */
  1161.         mdBuffer[ mdIndex++ ] = buffer[ bufIndex ];
  1162.     i = reg2mpi( buffer, e );
  1163.     for( bufIndex = 2; bufIndex < i + 2; bufIndex++ )    /* +2 skips count */
  1164.         mdBuffer[ mdIndex++ ] = buffer[ bufIndex ];
  1165.  
  1166.     /* Now evaluate the MD5 for the two MPI's */
  1167.     MD5Init( &mdContext );
  1168.     MD5Update( &mdContext, mdBuffer, mdIndex );
  1169.     MD5Final( hash, &mdContext );
  1170.  
  1171. }    /* getKeyHash */
  1172.  
  1173.  
  1174. void printKeyHash( byteptr hash, boolean indent )
  1175. {
  1176.     int i;
  1177.  
  1178. /*    Display the hash.  The format is:
  1179. pub  1024/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1180.           Key fingerprint =  xx xx xx xx xx xx xx xx  xx xx xx xx xx xx xx xx 
  1181. */
  1182.     fprintf( pgpout, "%*s  ", indent ? 27 : 1, PSTR("Key fingerprint =" ) );
  1183.     for( i = 0; i < 8; i++ )
  1184.         fprintf(pgpout, "%02X ", hash[ i ] );
  1185.     putc( ' ', pgpout);
  1186.     for( i = 8; i < 16; i++ )
  1187.         fprintf(pgpout, "%02X ", hash[ i ] );
  1188.     putc( '\n', pgpout);
  1189.  
  1190. }    /* printKeyHash */
  1191.  
  1192.  
  1193. void showKeyHash( unitptr n, unitptr e )
  1194. {
  1195.     byte hash[16];
  1196.  
  1197.     getKeyHash(hash,n,e);    /* compute hash of (n,e) */
  1198.  
  1199.     printKeyHash(hash, TRUE);
  1200. }    /* showKeyHash */
  1201.  
  1202.  
  1203. int view_keyring(char *mcguffin, char *ringfile, boolean show_signatures, boolean show_hashes)
  1204. /*    Lists all entries in keyring that have mcguffin string in userid.
  1205.     mcguffin is a null-terminated C string.
  1206. */
  1207. {    FILE *f;
  1208.     byte ctb, keyctb=0;
  1209.     int status;
  1210.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  1211.     byte keyID[KEYFRAGSIZE];
  1212.     byte sigkeyID[KEYFRAGSIZE];
  1213.     byte userid[256];        /* key certificate userid */
  1214.     char *siguserid;    /* signator userid */
  1215.     char dfltring[MAX_PATH];
  1216.     word32 tstamp;
  1217.     byte *timestamp = (byte *) &tstamp;        /* key certificate timestamp */
  1218.     int keycounter = 0;
  1219.     int firstuser = 0;
  1220.     int compromised = 0;
  1221.     boolean shownKeyHash=FALSE;
  1222.     boolean invalid_key=FALSE;    /* unsupported version or bad data */
  1223.     boolean match = FALSE;
  1224.     boolean disabled = FALSE;
  1225.  
  1226.     /* Default keyring to check signature ID's */
  1227.     buildfilename(dfltring,PUBLIC_KEYRING_FILENAME);
  1228.  
  1229.     /* open file f for read, in binary (not text) mode...*/
  1230.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  1231.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  1232.         return(-1);
  1233.     }
  1234.     if (show_signatures)
  1235.     {
  1236.         setkrent(ringfile);
  1237.         setkrent(dfltring);
  1238.         init_userhash();
  1239.     }
  1240.  
  1241. /*    Here's a good format for display of key or signature certificates:
  1242.  
  1243. #ifdef PGP26_COMPAT
  1244. Type bits/keyID     Date       User ID
  1245. pub  1024/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1246. sec   512/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1247. sig   384/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1248. #else
  1249. Type bits/keyID   Date       User ID
  1250. pub  1024/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1251. sec   512/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1252. sig   384/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1253. #endif
  1254. */
  1255.  
  1256.     if (moreflag)
  1257.         open_more();
  1258.     if (!quietmode)
  1259.     {
  1260.         fprintf(pgpout,PSTR("\nKey ring: '%s'"),ringfile);
  1261.         if (mcguffin && strlen(mcguffin) > 0)
  1262.             fprintf(pgpout,PSTR(", looking for user ID \"%s\"."),LOCAL_CHARSET(mcguffin));
  1263.     }
  1264. #ifdef PGP26_COMPAT
  1265.       fprintf(pgpout,PSTR("\nType bits/keyID    Date       User ID\n"));
  1266. #else
  1267.     fprintf(pgpout,PSTR("\nType bits/keyID   Date       User ID\n"));
  1268. #endif
  1269.     for ( ; ; )
  1270.     {
  1271.         status = readkeypacket(f,FALSE,&ctb,timestamp,(char *)userid,n,e,
  1272.                 NULL,NULL,NULL,NULL,sigkeyID,NULL);
  1273.         /* Note that readkeypacket has called set_precision */
  1274.         if (status== -1)
  1275.         {    status = 0;
  1276.             break;    /* eof reached */
  1277.         }
  1278.         if (status == -4 || status == -6)
  1279.         {    /* only ctb and userid are valid */
  1280.             memset(sigkeyID, 0, KEYFRAGSIZE);
  1281.             tstamp = 0;
  1282.         }
  1283.         else if (status < 0)
  1284.         {    fprintf(pgpout,PSTR("\n\007Could not read key from file '%s'.\n"),
  1285.                 ringfile);
  1286.             break;
  1287.         }
  1288.  
  1289.         if (is_key_ctb(ctb))
  1290.         {
  1291.             byte keyctrl;
  1292.  
  1293.             firstuser = 1;
  1294.             keyctb = ctb;
  1295.             compromised = is_compromised(f);
  1296.             shownKeyHash = FALSE;
  1297.             if (status < 0)
  1298.             {    invalid_key = TRUE;
  1299.                 memset(keyID, 0, KEYFRAGSIZE);
  1300.             }
  1301.             else
  1302.             {    invalid_key = FALSE;
  1303.                 extract_keyID(keyID, n);
  1304.                 if (read_trust(f, &keyctrl) == 0 && (keyctrl & KC_DISABLED))
  1305.                     disabled = TRUE;
  1306.                 else
  1307.                     disabled = FALSE;
  1308.             }
  1309.         }
  1310.  
  1311.         if (ctb != CTB_USERID  &&  !is_ctb_type(ctb, CTB_SKE_TYPE))
  1312.             continue;
  1313.         if (ctb == CTB_USERID)
  1314.         {    PascalToC((char *)userid);
  1315.             match = userid_match((char *)userid,mcguffin,n);
  1316.         }
  1317.         if (match)
  1318.         {
  1319.             if (ctb == CTB_USERID)
  1320.             {    if (firstuser)
  1321.                 {    keycounter++;
  1322.                     if (is_ctb_type(keyctb,CTB_CERT_PUBKEY_TYPE))
  1323.                         fprintf(pgpout,"pub");
  1324.                     else if (is_ctb_type(keyctb,CTB_CERT_SECKEY_TYPE))
  1325.                         fprintf(pgpout,"sec");
  1326.                     else
  1327.                         fprintf(pgpout,"???");
  1328.                     if (invalid_key)
  1329.                         fprintf(pgpout,"? ");
  1330.                     else if (disabled)
  1331.                         fprintf(pgpout,"@ ");
  1332.                     else
  1333.                         fprintf(pgpout,"  ");
  1334.                                       fprintf(pgpout,
  1335. #ifdef PGP26_COMPAT
  1336.                                               "%4d/%s %s ",
  1337. #else
  1338.                                               "%4d/%s %s  ",
  1339. #endif
  1340.                         countbits(n),keyIDstring(keyID),cdate(&tstamp));
  1341.                 }
  1342.                 else
  1343. #ifdef PGP26_COMPAT
  1344.                                       fprintf(pgpout,"                              ");
  1345. #else
  1346.                     fprintf(pgpout,"                             ");
  1347. #endif
  1348.                 if (compromised && firstuser)
  1349.                 {    fprintf(pgpout, PSTR("*** KEY REVOKED ***\n"));
  1350. #ifdef PGP26_COMPAT
  1351.                                       fprintf(pgpout,"                              ");
  1352. #else
  1353.                     fprintf(pgpout,"                             ");
  1354. #endif
  1355.                 }
  1356.                 firstuser = 0;
  1357.                 fprintf(pgpout,"%s\n",LOCAL_CHARSET((char *)userid));
  1358.  
  1359.                 /* Display the hashes for n and e if required */
  1360.                 if( show_hashes && !shownKeyHash )
  1361.                 {    showKeyHash( n, e );
  1362.                     shownKeyHash = TRUE;
  1363.                 }
  1364.             }
  1365.             else if (show_signatures && !(firstuser && compromised))    /* Must be sig cert */
  1366.             {    fprintf(pgpout,"sig%c      ", status < 0 ? '?' : ' ');
  1367.                 showkeyID(sigkeyID);
  1368. #ifdef PGP26_COMPAT
  1369.                               fprintf(pgpout,"             "); /* Indent signator userid */
  1370. #else
  1371.                 fprintf(pgpout,"               "); /* Indent signator userid */
  1372. #endif
  1373.                 if ((siguserid = user_from_keyID(sigkeyID)) == NULL)
  1374.                     fprintf(pgpout,PSTR("(Unknown signator, can't be checked)\n"));
  1375.                 else
  1376.                     fprintf(pgpout,"%s\n",LOCAL_CHARSET(siguserid));
  1377.             } /* printing a sig cert */
  1378.         }    /* if it has mcguffin */
  1379.     }    /* loop for all packets */
  1380.  
  1381.     fclose(f);    /* close key file */
  1382.     if (show_signatures)
  1383.         endkrent();
  1384.     fprintf(pgpout,PSTR("%d key(s) examined.\n"),keycounter);
  1385.     close_more();
  1386.  
  1387.     if (status < 0)
  1388.         return status;
  1389.     if (mcguffin != NULL && *mcguffin != '\0')
  1390.     {    /* user specified substring */
  1391.         if (keycounter == 0)
  1392.             return 67;    /* user not found */
  1393.         else if (keycounter > 1)
  1394.             return 1;    /* more than one match */
  1395.     }
  1396.     return(0);    /* normal return */
  1397.  
  1398. }    /* view_keyring */
  1399.  
  1400.  
  1401. int dokeycheck(char *mcguffin, char *ringfile, int options)
  1402. /*    Lists all entries in keyring that have mcguffin string in userid.
  1403.     mcguffin is a null-terminated C string.
  1404.     If options is CHECK_NEW, only new signatures are checked and are
  1405.     marked as being checked in the trustbyte (called from addto_keyring).
  1406. */
  1407. {    FILE *f, *fixedf=NULL;
  1408.     byte ctb, keyctb=0;
  1409.     long fpsig = 0, fpkey = 0, fixpos = 0, trustpos = -1;
  1410.     int status, sigstatus;
  1411.     int keypktlen = 0, sigpktlen = 0;
  1412.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  1413.     byte keyID[KEYFRAGSIZE];
  1414.     byte sigkeyID[KEYFRAGSIZE];
  1415.     byte keyuserid[256];        /* key certificate userid */
  1416.     byte siguserid[256];        /* sig certificate userid */
  1417.     char dfltring[MAX_PATH];
  1418.     char *tempring = NULL;
  1419.     word32 tstamp;
  1420.     byte *timestamp = (byte *) &tstamp;        /* key certificate timestamp */
  1421.     word32 sigtstamp;
  1422.     byte *sigtimestamp = (byte *) &sigtstamp;
  1423.     byte sigclass;
  1424.     int firstuser = 0;
  1425.     int compromised = 0;
  1426.     boolean invalid_key=FALSE;    /* unsupported version or bad data */
  1427.     boolean failed=FALSE;
  1428.     boolean print_userid=FALSE;
  1429.     byte sigtrust;
  1430.  
  1431.     /* Default keyring to check signature ID's */
  1432.     buildfilename(dfltring,PUBLIC_KEYRING_FILENAME);
  1433.  
  1434.     /* open file f, in binary (not text) mode...*/
  1435.     if (options & CHECK_NEW)
  1436.         f = fopen(ringfile,FOPRWBIN);
  1437.     else
  1438.         f = fopen(ringfile,FOPRBIN);
  1439.     if (f == NULL)
  1440.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  1441.         return(-1);
  1442.     }
  1443.  
  1444. /*    Here's a good format for display of key or signature certificates:
  1445. #ifdef PGP26_COMPAT
  1446. Type bits/keyID     Date       User ID
  1447. pub  1024/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1448. sec   512/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1449. sig   384/xxxxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1450. #else
  1451. Type bits/keyID   Date       User ID
  1452. pub  1024/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1453. sec   512/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1454. sig   384/xxxxxx yyyy-mm-dd  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1455. #endif
  1456. */
  1457.  
  1458.     if (options & CHECK_NEW)
  1459.     {    fprintf(pgpout,PSTR("\nChecking signatures...\n"));
  1460.     }
  1461.     else
  1462.     {    
  1463.         if (moreflag)
  1464.             open_more();
  1465.         if (!quietmode)
  1466.         {    fprintf(pgpout,PSTR("\nKey ring: '%s'"),ringfile);
  1467.             if (mcguffin && strlen(mcguffin) > 0)
  1468.                 fprintf(pgpout,PSTR(", looking for user ID \"%s\"."),LOCAL_CHARSET(mcguffin));
  1469.         }
  1470. #ifdef PGP26_COMPAT
  1471.               fprintf(pgpout,PSTR("\nType bits/keyID    Date       User ID\n"));
  1472. #else
  1473.         fprintf(pgpout,PSTR("\nType bits/keyID   Date       User ID\n"));
  1474. #endif
  1475.     }
  1476.     for ( ; ; )
  1477.     {    long fpos = ftell(f);
  1478.         status = readkeypacket(f,FALSE,&ctb,timestamp,(char *)keyuserid,n,e,
  1479.                 NULL,NULL,NULL,NULL,sigkeyID,NULL);
  1480.         /* Note that readkeypacket has called set_precision */
  1481.         if (status== -1 ) break;    /* eof reached */
  1482.         if (status == -4 || status == -6)
  1483.         {    /* only ctb and userid are valid */
  1484.             memset(sigkeyID, 0, KEYFRAGSIZE);
  1485.             tstamp = 0;
  1486.         }
  1487.         else if (status < 0)
  1488.         {    fprintf(pgpout,PSTR("\n\007Could not read key from file '%s'.\n"),
  1489.                 ringfile);
  1490.             fclose(f);    /* close key file */
  1491.             return(-1);
  1492.         }
  1493.  
  1494.         if (is_key_ctb(ctb))
  1495.         {    firstuser = 1;
  1496.             keyctb = ctb;
  1497.             fpkey = fpos;
  1498.             keypktlen = ( int ) ( ftell(f) - fpkey );
  1499.             compromised = is_compromised(f);
  1500.             if (status < 0)
  1501.             {    invalid_key = TRUE;
  1502.                 memset(keyID, 0, KEYFRAGSIZE);
  1503.             }
  1504.             else
  1505.             {    invalid_key = FALSE;
  1506.                 extract_keyID(keyID, n);
  1507.             }
  1508.             if (options & CHECK_NEW)
  1509.                 print_userid = TRUE;
  1510.         }
  1511.  
  1512.         if (ctb == CTB_USERID)
  1513.             PascalToC((char *)keyuserid);
  1514.         else if (is_ctb_type(ctb, CTB_SKE_TYPE))
  1515.         {    fpsig = fpos;
  1516.             sigpktlen = ( int ) ( ftell(f) - fpsig );
  1517.         } else
  1518.             continue;
  1519.  
  1520.         if (options & CHECK_NEW)
  1521.         {
  1522.             if (!is_ctb_type(ctb, CTB_SKE_TYPE))
  1523.                 continue;
  1524.             trustpos = ftell(f);
  1525.             status = read_trust(f, &sigtrust);
  1526.             if (status == -1)
  1527.                 break;    /* EOF */
  1528.             if (status == -7)
  1529.             {    trustpos = -1;
  1530.                 continue;    /* not a keyring or this was a compromise cert. */
  1531.             }
  1532.             if (status < 0)
  1533.             {    fclose(f);
  1534.                 return status;
  1535.             }
  1536.             if (sigtrust & KC_SIG_CHECKED)
  1537.                 continue;
  1538.             /* addto_keyring has called setkrent() */
  1539.             if (user_from_keyID(sigkeyID) == NULL)
  1540.                 continue;    /* unknown signator */
  1541.         }
  1542.  
  1543.         if ((options & CHECK_NEW) || userid_match((char *)keyuserid,mcguffin,n))
  1544.         {
  1545.             if (ctb == CTB_USERID || print_userid)
  1546.              {    /* CHECK_NEW: only print userid if it has new signature */
  1547.                 print_userid = FALSE;
  1548.                 if (firstuser)
  1549.                 {    if (is_ctb_type(keyctb,CTB_CERT_PUBKEY_TYPE))
  1550.                         fprintf(pgpout,"pub");
  1551.                     else if (is_ctb_type(keyctb,CTB_CERT_SECKEY_TYPE))
  1552.                         fprintf(pgpout,"sec");
  1553.                     else
  1554.                         fprintf(pgpout,"???");
  1555.                     if (invalid_key)
  1556.                         fprintf(pgpout,"? ");
  1557.                     else
  1558.                         fprintf(pgpout,"  ");
  1559. #ifdef PGP26_COMPAT
  1560.                                       fprintf(pgpout,"%4d/%s %s ",
  1561. #else
  1562.                     fprintf(pgpout,"%4d/%s %s  ",
  1563. #endif
  1564.                         countbits(n),keyIDstring(keyID),cdate(&tstamp));
  1565.                 }
  1566.                 else
  1567. #ifdef PGP26_COMPAT
  1568.                                       fprintf(pgpout,"                              ");
  1569. #else
  1570.                     fprintf(pgpout,"                             ");
  1571. #endif
  1572.                 if (compromised && firstuser)
  1573.                 {    fprintf(pgpout, PSTR("*** KEY REVOKED ***\n"));
  1574. #ifdef PGP26_COMPAT
  1575.                                       fprintf(pgpout,"                              ");
  1576. #else
  1577.                     fprintf(pgpout,"                             ");
  1578. #endif
  1579.                 }
  1580.                 firstuser = 0;
  1581.                 fprintf(pgpout,"%s\n",LOCAL_CHARSET((char *)keyuserid));
  1582.             }
  1583.             if (is_ctb_type(ctb, CTB_SKE_TYPE))
  1584.             {    /* Try checking signature on either this ring or dflt ring */
  1585.                 CToPascal((char *)keyuserid);
  1586.                 sigstatus = check_key_sig (f, fpkey, keypktlen, (char *) keyuserid,
  1587.                     f, fpsig, ringfile, (char *) siguserid, sigtimestamp, &sigclass);
  1588.                 if (sigstatus == -1  &&  strcmp(ringfile,dfltring) != 0)
  1589.                     sigstatus = check_key_sig (f, fpkey, keypktlen, (char *) keyuserid,
  1590.                         f, fpsig, dfltring, (char *) siguserid, sigtimestamp, &sigclass);
  1591.                 PascalToC((char *)keyuserid);
  1592.                 fseek (f, fpsig+sigpktlen, SEEK_SET);
  1593.                 if (sigclass == KC_SIGNATURE_BYTE)
  1594.                     fprintf(pgpout,"com");
  1595.                 else
  1596.                     fprintf(pgpout,"sig");
  1597.                 if (sigstatus >= 0)
  1598.                     fprintf(pgpout,"!      ");
  1599.                 else if (status < 0 || sigstatus == -1)
  1600.                     fprintf(pgpout,"?      ");
  1601.                 else
  1602.                     fprintf(pgpout,"*      ");    /* bad signature */
  1603.                 showkeyID(sigkeyID);
  1604.                 if (sigstatus == -1)
  1605.                 {    fprintf(pgpout,"               "); /* Indent signator userid */
  1606.                     fprintf(pgpout,PSTR("(Unknown signator, can't be checked)\n"));
  1607.                 }
  1608.                 else
  1609.                 {    PascalToC((char *) siguserid);
  1610. #ifdef PGP26_COMPAT
  1611.                                       fprintf(pgpout," %s ",cdate(&sigtstamp));
  1612. #else
  1613.                     fprintf(pgpout," %s  ",cdate(&sigtstamp));
  1614. #endif
  1615.                     if (sigclass != KC_SIGNATURE_BYTE)
  1616. #ifdef PGP26_COMPAT
  1617.                                               fprintf(pgpout, " ");
  1618. #else
  1619.                         fprintf(pgpout, "  ");
  1620. #endif
  1621.                     fprintf(pgpout,"%s\n", LOCAL_CHARSET((char *)siguserid));
  1622.                     if (sigstatus >= 0)
  1623.                     {    if (options & CHECK_NEW && trustpos > 0)
  1624.                             write_trust_pos(f, sigtrust|KC_SIG_CHECKED, trustpos);
  1625.                     }
  1626.                     else
  1627.                     {    fprintf(pgpout,"                               ");
  1628.                         fprintf(pgpout,PSTR("\007***** BAD SIGNATURE! *****\n"));
  1629.                         if (!failed)
  1630.                         {    /* first bad signature: create scratch file */
  1631.                             tempring = tempfile(TMP_TMPDIR);
  1632.                             fixedf = fopen(tempring, FOPWBIN);
  1633.                             failed = TRUE;
  1634.                         }
  1635.                         if (fixedf != NULL)
  1636.                         {
  1637.                             copyfilepos(f, fixedf, fpsig - fixpos, fixpos);
  1638.                             fseek(f, fpsig+sigpktlen, SEEK_SET);
  1639.                             if (nextkeypacket(f, &ctb) < 0 || ctb != CTB_KEYCTRL)
  1640.                                 fseek(f, fpsig+sigpktlen, SEEK_SET);
  1641.                             fixpos = ftell(f);
  1642.                         }
  1643.                     }
  1644.                 }
  1645.             } /* checking a signature */
  1646.         }    /* if it has mcguffin */
  1647.     }    /* loop for all packets */
  1648.  
  1649.     close_more();
  1650.     if (status < -1)
  1651.     {
  1652.         fclose(f);
  1653.         return status;
  1654.     }
  1655.     fputc('\n',pgpout);
  1656.  
  1657.     if (failed)
  1658.     {
  1659.         copyfilepos(f, fixedf, -1L, fixpos);
  1660.         if (write_error(fixedf))
  1661.         {    fclose(fixedf);
  1662.             fclose(f);
  1663.             return -1;
  1664.         }
  1665.         fclose(fixedf);
  1666.         if (!batchmode)
  1667.             fprintf(pgpout, PSTR("Remove bad signatures (Y/n)? "));
  1668.         if (batchmode || getyesno('y'))
  1669.         {
  1670.             savetempbak(tempring, ringfile);
  1671.             failed = 0;
  1672.         }
  1673.     }
  1674.     fclose(f);    /* close key file */
  1675.  
  1676.     return(failed?-1:0);    /* normal return */
  1677.  
  1678. }    /* dokeycheck */
  1679.  
  1680. int backup_rename(char *scratchfile, char *destfile)
  1681. {    /* rename scratchfile to destfile after making a backup file */
  1682.     char bakfile[MAX_PATH];
  1683.  
  1684.     if (is_tempfile(destfile))
  1685.     {
  1686.         remove(destfile);
  1687.     }
  1688.     else
  1689.     {    if (file_exists(destfile))
  1690.         {
  1691.             strcpy(bakfile, destfile);
  1692.             force_extension(bakfile, BAK_EXTENSION);
  1693.             remove(bakfile);
  1694.             rename(destfile, bakfile);
  1695.         }
  1696.     }
  1697.     return(rename2(scratchfile, destfile));
  1698. }
  1699.  
  1700. int remove_sigs(char *mcguffin, char*ringfile)
  1701. /* Lists all signatures for keys with specified mcguffin string, and asks
  1702.  * if they should be removed.
  1703.  */
  1704. {    FILE *f, *g;
  1705.     byte ctb;
  1706.     long fp, fpuser;
  1707.     int packetlength;
  1708.     int status;
  1709.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  1710.     byte sigkeyID[KEYFRAGSIZE];
  1711.     byte userid[256];        /* key certificate userid */
  1712.     char dfltring[MAX_PATH];
  1713.     word32 tstamp;
  1714.     byte *timestamp = (byte *) &tstamp;        /* key certificate timestamp */
  1715.     int nsigs = 0, nremoved = 0;
  1716.     int keeping;
  1717.     char *scratchf;
  1718.  
  1719.     /* Default keyring to check signature ID's */
  1720.     buildfilename(dfltring,PUBLIC_KEYRING_FILENAME);
  1721.  
  1722.     if (!mcguffin  ||  strlen(mcguffin) == 0)
  1723.         return(-1);
  1724.  
  1725.     setoutdir(ringfile);
  1726.     scratchf = tempfile(0);
  1727.  
  1728.     strcpy((char *)userid,mcguffin);
  1729.  
  1730.     fprintf(pgpout,PSTR("\nRemoving signatures from userid '%s' in key ring '%s'\n"),
  1731.                 LOCAL_CHARSET(mcguffin), ringfile);
  1732.  
  1733.     status = getpublickey(GPK_GIVEUP|GPK_SHOW, ringfile, &fp, &packetlength, NULL, timestamp, userid, n, e);
  1734.     if (status < 0)
  1735.     {    fprintf(pgpout,PSTR("\n\007Key not found in key ring '%s'.\n"),ringfile);
  1736.         return(0);    /* normal return */
  1737.     }
  1738.  
  1739.     strcpy((char *)userid,mcguffin);
  1740.     getpubuserid (ringfile, fp, userid, &fpuser, &packetlength, FALSE);
  1741.     packetlength += ( int ) ( fpuser - fp );
  1742.  
  1743.     /* open file f for read, in binary (not text) mode...*/
  1744.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  1745.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  1746.         return(-1);
  1747.     }
  1748.  
  1749.     /* Count signatures */
  1750.     fseek (f, fp+packetlength, SEEK_SET);
  1751.     for ( ; ; )
  1752.     {    status = nextkeypacket(f, &ctb);
  1753.         if (status < 0  ||  is_key_ctb(ctb)  ||  ctb==CTB_USERID)
  1754.             break;
  1755.         if (is_ctb_type(ctb,CTB_SKE_TYPE))
  1756.             ++nsigs;
  1757.     }
  1758.         
  1759.     rewind(f);
  1760.  
  1761.     if (nsigs == 0)
  1762.     {    fprintf (pgpout,PSTR("\nKey has no signatures to remove.\n"));
  1763.         fclose (f);
  1764.         return (0);        /* Normal return */
  1765.     }
  1766.  
  1767.     fprintf (pgpout, PSTR("\nKey has %d signature(s):\n"), nsigs);
  1768.  
  1769.     /* open file g for writing, in binary (not text) mode...*/
  1770.     if ((g = fopen(scratchf,FOPWBIN)) == NULL)
  1771.     {    fclose(f);
  1772.         return(-1);
  1773.     }
  1774.     copyfile(f,g,fp+packetlength);    /* copy file f to g up through key */
  1775.  
  1776.     /* Now print out any following sig certs */
  1777.     keeping = 1;
  1778.     for ( ; ; )
  1779.     {    fp = ftell(f);
  1780.         status = readkeypacket(f, FALSE, &ctb, NULL, NULL, NULL, NULL,
  1781.                 NULL,NULL,NULL,NULL,sigkeyID,NULL);
  1782.         packetlength = ( int ) ( ftell(f) - fp ); 
  1783.         if ((status < 0 && status != -6 && status != -4) ||
  1784.                 is_key_ctb(ctb)  ||  ctb==CTB_USERID)
  1785.             break;
  1786.         if (is_ctb_type(ctb,CTB_SKE_TYPE))
  1787.         {    fprintf(pgpout,"sig%c     ", status < 0 ? '?' : ' ');
  1788.             if (status < 0)
  1789.                 memset(sigkeyID, 0, KEYFRAGSIZE);
  1790.             showkeyID(sigkeyID);
  1791.             fprintf(pgpout,"               "); /* Indent signator userid */
  1792.             if (getpublickey(GPK_GIVEUP, ringfile, NULL, NULL, sigkeyID,
  1793.                     timestamp, userid, n, e)>=0 ||
  1794.                 getpublickey(GPK_GIVEUP, dfltring, NULL, NULL, sigkeyID,
  1795.                     timestamp, userid, n, e)>=0)
  1796.             {    PascalToC((char *)userid);
  1797.                 fprintf(pgpout,"%s\n",LOCAL_CHARSET((char *)userid));
  1798.             }
  1799.             else
  1800.                 fprintf(pgpout,PSTR("(Unknown signator, can't be checked)\n"));
  1801.             fprintf(pgpout, PSTR("Remove this signature (y/N)? "));
  1802.             if (!(keeping=!getyesno('n')))
  1803.                 ++nremoved;
  1804.         }
  1805.         if (keeping)
  1806.             copyfilepos (f, g, (long) packetlength, fp);
  1807.     }    /* scanning sig certs */
  1808.     copyfilepos (f, g, -1L, fp);        /* Copy rest of file */
  1809.  
  1810.     fclose(f);    /* close key file */
  1811.     if (write_error(g))
  1812.     {    fclose(g);
  1813.         return -1;
  1814.     }
  1815.     fclose(g);    /* close scratch file */
  1816.     savetempbak(scratchf,ringfile);
  1817.     if (nremoved == 0)
  1818.         fprintf(pgpout,PSTR("\nNo key signatures removed.\n"));
  1819.     else
  1820.         fprintf(pgpout,PSTR("\n%d key signature(s) removed.\n"), nremoved);
  1821.  
  1822.     return(0);    /* normal return */
  1823.  
  1824. }    /* remove_sigs */
  1825.  
  1826.  
  1827. int remove_from_keyring(byte *keyID, char *mcguffin, char *ringfile, boolean secring_too)
  1828. /*    Remove the first entry in key ring that has mcguffin string in userid.
  1829.     Or it removes the first matching keyID from the ring.
  1830.     A non-NULL keyID takes precedence over a mcguffin specifier.
  1831.     mcguffin is a null-terminated C string.
  1832.     If secring_too is TRUE, the secret keyring is also checked.
  1833. */
  1834. {
  1835.     FILE *f;
  1836.     FILE *g;
  1837.     long fp, nfp;
  1838.     int packetlength;
  1839.     byte ctb;
  1840.     int status;
  1841.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  1842.     byte userid[256];        /* key certificate userid */
  1843.     word32 tstamp; byte *timestamp = (byte *) &tstamp;        /* key certificate timestamp */
  1844.     int userids;
  1845.     boolean rmuserid = FALSE;
  1846.     char *scratchf;
  1847.  
  1848.     default_extension(ringfile,PGP_EXTENSION);
  1849.  
  1850.     if ((keyID==NULL) && (!mcguffin  ||  strlen(mcguffin)==0))
  1851.         return(-1); /* error, null mcguffin will match everything */
  1852.  
  1853. top:
  1854.     if (mcguffin)
  1855.         strcpy((char *)userid,mcguffin);
  1856.  
  1857.     fprintf(pgpout,PSTR("\nRemoving from key ring: '%s'"),ringfile);
  1858.     if (mcguffin  &&  strlen(mcguffin) > 0)
  1859.         fprintf(pgpout,PSTR(", userid \"%s\".\n"),
  1860.             LOCAL_CHARSET(mcguffin));
  1861.  
  1862.     status = getpublickey(GPK_GIVEUP|GPK_SHOW, ringfile, &fp, &packetlength, NULL, timestamp, userid, n, e);
  1863.     if (status < 0 && status != -4 && status != -6)
  1864.     {    fprintf(pgpout,PSTR("\n\007Key not found in key ring '%s'.\n"),ringfile);
  1865.         return(0);    /* normal return */
  1866.     }
  1867.  
  1868.     /* Now add to packetlength the subordinate following certificates */
  1869.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  1870.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  1871.         return(-1);
  1872.     }
  1873.     fseek (f, fp+packetlength, SEEK_SET);
  1874.     userids = 0;
  1875.     do     /* count user ID's, position nfp at next key */
  1876.     {    nfp = ftell(f);
  1877.         status = nextkeypacket(f, &ctb);
  1878.         if (status == 0 && ctb == CTB_USERID)
  1879.             ++userids;
  1880.     } while (status == 0 && !is_key_ctb(ctb));
  1881.     if (status < -1)
  1882.     {    fclose(f);
  1883.         return(-1);
  1884.     }
  1885.  
  1886.     if (keyID==NULL)    /* Human confirmation is required. */
  1887.     {    /* Supposedly the key was fully displayed by getpublickey */
  1888.         if (userids > 1)
  1889.         {    fprintf(pgpout, PSTR("\nKey has more than one user ID.\n\
  1890. Do you want to remove the whole key (y/N)? "));
  1891.             if (!getyesno('n'))
  1892.             {    /* find out which userid should be removed */
  1893.                 rmuserid = TRUE;
  1894.                 fseek (f, fp+packetlength, SEEK_SET);
  1895.                 for ( ; ; )
  1896.                 {    fp = ftell(f);
  1897.                     status = readkpacket(f, &ctb, (char *) userid, NULL, NULL);
  1898.                     if (status < 0 && status != -4 && status != -6 || is_key_ctb(ctb))
  1899.                     {    fclose(f);
  1900.                         fprintf(pgpout, PSTR("\nNo more user ID's\n"));
  1901.                         return(-1);
  1902.                     }
  1903.                     if (ctb == CTB_USERID)
  1904.                     {    fprintf(pgpout, PSTR("Remove \"%s\" (y/N)? "), userid);
  1905.                         if (getyesno('n'))
  1906.                             break;
  1907.                     }
  1908.                 }
  1909.                 do     /* also remove signatures and trust bytes */
  1910.                 {    nfp = ftell(f);
  1911.                     status = nextkeypacket(f, &ctb);
  1912.                 } while ((status == 0 || status == -4 || status == -6) &&
  1913.                         !is_key_ctb(ctb) && ctb != CTB_USERID);
  1914.                 if (status < -1 && status != -4 && status != -6)
  1915.                 {    fclose(f);
  1916.                     return(-1);
  1917.                 }
  1918.             }
  1919.         }
  1920.         else if (!force_flag)    /* only one user ID */
  1921.         {    fprintf(pgpout,
  1922.                 PSTR("\nAre you sure you want this key removed (y/N)? "));
  1923.             if (!getyesno('n'))
  1924.             {    fclose(f);
  1925.                 return(-1);    /* user said "no" */
  1926.             }
  1927.         }
  1928.     }
  1929.     fclose(f);
  1930.     packetlength = ( int ) ( nfp - fp );
  1931.  
  1932.     /* open file f for read, in binary (not text) mode...*/
  1933.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  1934.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  1935.         return(-1);
  1936.     }
  1937.  
  1938.     setoutdir(ringfile);
  1939.     scratchf = tempfile(0);
  1940.     /* open file g for writing, in binary (not text) mode...*/
  1941.     if ((g = fopen(scratchf,FOPWBIN)) == NULL)
  1942.     {    fclose(f);
  1943.         return(-1);
  1944.     }
  1945.     copyfilepos(f,g,fp,0L);    /* copy file f to g up to position fp */
  1946.     copyfilepos(f,g,-1L,fp+packetlength); /* copy rest of file f */
  1947.     fclose(f);    /* close key file */
  1948.     if (write_error(g))
  1949.     {    fclose(g);
  1950.         return -1;
  1951.     }
  1952.     fclose(g);    /* close scratch file */
  1953.     if (secring_too) /* TRUE if this is the public keyring */
  1954.         maint_update(scratchf);
  1955.     savetempbak(scratchf,ringfile);
  1956.     if (rmuserid)
  1957.         fprintf(pgpout,PSTR("\nUser ID removed from key ring.\n"));
  1958.     else
  1959.         fprintf(pgpout,PSTR("\nKey removed from key ring.\n"));
  1960.     
  1961.     if (secring_too)
  1962.     {    secring_too = FALSE;
  1963.         buildfilename(ringfile, SECRET_KEYRING_FILENAME);
  1964.         strcpy((char *)userid,mcguffin);
  1965.         if (getpublickey(GPK_GIVEUP, ringfile, NULL, NULL, NULL, timestamp, userid, n, e) == 0)
  1966.         {    fprintf(pgpout, PSTR("\nKey or user ID is also present in secret keyring.\n\
  1967. Do you also want to remove it from the secret keyring (y/N)? "));
  1968.             if (getyesno('n'))
  1969.                 goto top;
  1970.         }
  1971.     }
  1972.  
  1973.     return(0);    /* normal return */
  1974.  
  1975. }    /* remove_from_keyring */
  1976.  
  1977.  
  1978. int extract_from_keyring (char *mcguffin, char *keyfile, char *ringfile,
  1979.                 boolean transflag)
  1980. /*    Copy the first entry in key ring that has mcguffin string in
  1981.     userid and put it into keyfile.
  1982.     mcguffin is a null-terminated C string.
  1983. */
  1984. {
  1985.     FILE *f;
  1986.     FILE *g;
  1987.     long fp;
  1988.     int packetlength=0;
  1989.     byte ctb;
  1990.     byte keyctrl;
  1991.     int status;
  1992.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  1993.     byte keyID[KEYFRAGSIZE];
  1994.     byte userid[256];    /* key certificate userid */
  1995.     char fname[MAX_PATH], transfile[MAX_PATH], transname[MAX_PATH];
  1996.     char *tempf = NULL;
  1997.     word32 tstamp; byte *timestamp = (byte *) &tstamp; /* key cert tstamp */
  1998.     boolean append = FALSE;
  1999.     boolean whole_ring = FALSE;
  2000.  
  2001.     default_extension(ringfile, PGP_EXTENSION);
  2002.  
  2003.     if (!mcguffin  ||  strlen(mcguffin)==0 || strcmp(mcguffin, "*") == 0)
  2004.         whole_ring = TRUE;
  2005.  
  2006.     /* open file f for read, in binary (not text) mode...*/
  2007.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  2008.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  2009.         return(-1);
  2010.     }
  2011.  
  2012.     if (!whole_ring)
  2013.     {
  2014.         strcpy((char *)userid, mcguffin);
  2015.         fprintf(pgpout,PSTR("\nExtracting from key ring: '%s'"),ringfile);
  2016.         fprintf(pgpout,PSTR(", userid \"%s\".\n"),LOCAL_CHARSET(mcguffin));
  2017.  
  2018.         status = getpublickey(GPK_GIVEUP|GPK_SHOW, ringfile, &fp, &packetlength, NULL,
  2019.                     timestamp, userid, n, e);
  2020.         if (status < 0 && status != -4 && status != -6)
  2021.         {    fprintf(pgpout,PSTR("\n\007Key not found in key ring '%s'.\n"),
  2022.                                     ringfile);
  2023.             fclose(f);
  2024.             return(1);    /* non-normal return */
  2025.         }
  2026.         extract_keyID(keyID, n);
  2027.     }
  2028.     else
  2029.     {
  2030.         do    /* set fp to first key packet */
  2031.             fp = ftell(f);
  2032.         while ((status = nextkeypacket(f, &ctb)) >= 0 && !is_key_ctb(ctb));
  2033.         if (status < 0)
  2034.         {    fclose(f);
  2035.             return(-1);
  2036.         }
  2037.         packetlength = ( int ) ( ftell(f) - fp );
  2038.     }
  2039.  
  2040.     if (!keyfile  ||  strlen(keyfile)==0)
  2041.     {    fprintf(pgpout, PSTR("\nExtract the above key into which file? "));
  2042.         if (batchmode)
  2043.             return -1;
  2044.         getstring( fname, sizeof(fname)-4, TRUE );
  2045.         if (*fname == '\0')
  2046.             return(-1);
  2047.     }
  2048.     else
  2049.         strcpy(fname,keyfile);
  2050.     default_extension(fname,PGP_EXTENSION);
  2051.  
  2052.     /* If transport armoring, use a dummy file for keyfile */
  2053.     if (transflag)
  2054.     {    strcpy(transname, fname);
  2055.         strcpy(transfile, fname);
  2056.         force_extension(transfile, ASC_EXTENSION);
  2057.         tempf = tempfile(TMP_TMPDIR|TMP_WIPE);
  2058.         strcpy(fname, tempf);
  2059.     }
  2060.     if (file_exists( transflag?transfile:fname ))
  2061.     {
  2062.         if (!transflag && !whole_ring)
  2063.         {    /* see if the key is already present in fname */
  2064.             status = getpublickey(GPK_GIVEUP, fname, NULL, NULL, keyID,
  2065.                     timestamp, userid, n, e);
  2066.             if (status >= 0 || status == -4 || status == -6)
  2067.             {    fclose(f);
  2068.                 fprintf(pgpout,PSTR("Key ID %s is already included in key ring '%s'.\n"),
  2069.                     keyIDstring(keyID), fname);
  2070.                 return(-1);
  2071.             }
  2072.         }
  2073.         if (whole_ring || transflag || status < -1)
  2074.         {    if (!is_tempfile(fname) && !force_flag) 
  2075.                 /* Don't ask this for mailmode or for 
  2076.                  * a tempfile, since its ok.
  2077.                  */
  2078.             { /* if status < -1 then fname is not a keyfile, ask if it should be overwritten */
  2079.                 fprintf(pgpout,PSTR("\n\007Output file '%s' already exists.  Overwrite (y/N)? "),
  2080.                     transflag?transfile:fname);
  2081.                 if (!getyesno( 'n' ))
  2082.                 {    fclose(f);
  2083.                     return(-1);    /* user chose to abort */
  2084.                 }
  2085.             }
  2086.         }
  2087.         else
  2088.             append = TRUE;
  2089.     }
  2090.  
  2091.     if (append)
  2092.         g = fopen(fname, FOPRWBIN);
  2093.     else
  2094.         g = fopen(fname, FOPWBIN);
  2095.     if (g == NULL)
  2096.     {    if (append)
  2097.             fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  2098.         else
  2099.             fprintf(pgpout,PSTR("\n\007Unable to create key file '%s'.\n"), fname);
  2100.         fclose(f);
  2101.         return(-1);
  2102.     }
  2103.     if (append)
  2104.         fseek(g, 0L, SEEK_END);
  2105.     do
  2106.     {    /* file f is positioned right after key packet */
  2107.         if (whole_ring && read_trust(f, &keyctrl) == 0
  2108.             && (keyctrl & KC_DISABLED))
  2109.         {
  2110.             do    /* skip this key */
  2111.             {
  2112.                 fp = ftell(f);
  2113.                 status = nextkeypacket(f, &ctb);
  2114.                 packetlength = ( int ) ( ftell(f) - fp );
  2115.             }
  2116.             while (!is_key_ctb(ctb) && status >= 0);
  2117.             continue;
  2118.         }
  2119.         if (copyfilepos(f, g, (long) packetlength, fp) < 0)    /* Copy key out */
  2120.         {    status = -2;
  2121.             break;
  2122.         }
  2123.         /* Copy any following signature or userid packets */
  2124.         for ( ; ; )
  2125.         {    fp = ftell(f);
  2126.             status = nextkeypacket(f, &ctb);
  2127.             packetlength = ( int ) ( ftell(f) - fp );
  2128.             if (status < 0  ||  is_key_ctb(ctb))
  2129.                 break;
  2130.             if (ctb==CTB_USERID  ||  is_ctb_type(ctb,CTB_SKE_TYPE))
  2131.                 if (copyfilepos(f, g, (long) packetlength, fp) < 0)
  2132.                 {    status = -2;
  2133.                     break;
  2134.                 }
  2135.         }
  2136.     }
  2137.     while (whole_ring && status >= 0);
  2138.  
  2139.     fclose(f);
  2140.     if (status < -1 || write_error(g))
  2141.     {    fclose(g);
  2142.         return(-1);
  2143.     }
  2144.     fclose(g);
  2145.  
  2146.     if (transflag)
  2147.     {    status = armor_file (fname, transfile, transname, NULL);
  2148.         rmtemp (tempf);
  2149.         if (status)
  2150.             return(-1);
  2151.     }
  2152.  
  2153.     fprintf (pgpout,PSTR("\nKey extracted to file '%s'.\n"), transflag?transfile:fname);
  2154.  
  2155.     return (0);    /* normal return */
  2156. }    /* extract_from_keyring */
  2157.  
  2158.  
  2159. /*======================================================================*/
  2160.  
  2161. static int merge_key_to_ringfile(char *keyfile, char* ringfile, long fp,
  2162.                 int packetlength, long keylen)
  2163. /* Copy the key data in keyfile into ringfile, replacing the data that
  2164.    is in ringfile starting at fp and for length packetlength.
  2165.    keylen is the number of bytes to copy from keyfile
  2166. */
  2167. {    FILE    *f, *g, *h;
  2168.     char *tempf;
  2169.     int rc;
  2170.  
  2171.     setoutdir(ringfile);
  2172.     tempf = tempfile(TMP_WIPE);
  2173.     /* open file f for reading, binary, as keyring file */
  2174.     if ((f = fopen(ringfile,FOPRBIN)) == NULL)
  2175.         return(-1);
  2176.     /* open file g for writing, binary, as scratch keyring file */
  2177.     if ((g = fopen(tempf,FOPWBIN)) == NULL)
  2178.     {    fclose(f);
  2179.         return(-1);
  2180.     }
  2181.     /* open file h for reading, binary, as key file to be inserted */
  2182.     if ((h = fopen(keyfile,FOPRBIN)) == NULL)
  2183.     {    fclose(f);
  2184.         fclose(g);
  2185.         return(-1);
  2186.     }
  2187.     /* Copy pre-key keyring data from f to g */
  2188.     copyfile(f,g,fp);
  2189.     /* Copy temp key data from h to g */
  2190.     copyfile(h,g,keylen);
  2191.     /* Copy post-key keyring data from f to g */
  2192.     copyfilepos(f,g,-1L,fp+packetlength);
  2193.     fclose(f);
  2194.     rc = write_error(g);
  2195.     fclose(g);
  2196.     fclose(h);
  2197.  
  2198.     if (!rc)
  2199.         savetempbak(tempf,ringfile);
  2200.  
  2201.     return(rc ? -1 : 0);
  2202. }    /* merge_key_to_ringfile */
  2203.  
  2204. static int insert_userid(char *keyfile, byte *userid, long fpos)
  2205. {    /* insert userid and trust byte at position fpos in file keyfile */
  2206.     char *tmpf;
  2207.     FILE *f, *g;
  2208.  
  2209.     tmpf = tempfile(TMP_TMPDIR);
  2210.     if ((f = fopen(keyfile, FOPRBIN)) == NULL)
  2211.         return(-1);
  2212.     if ((g = fopen(tmpf, FOPWBIN)) == NULL)
  2213.     {    fclose(f);
  2214.         return(-1);
  2215.     }
  2216.     copyfile(f, g, fpos);
  2217.     putc(CTB_USERID, g);
  2218.     fwrite(userid, 1, userid[0]+1, g);
  2219.     write_trust(g, KC_LEGIT_COMPLETE);
  2220.     copyfile(f, g, -1L);
  2221.     fclose(f);
  2222.     if (write_error(g))
  2223.     {    fclose(g);
  2224.         return(-1);
  2225.     }
  2226.     fclose(g);
  2227.     return(savetempbak(tmpf, keyfile));
  2228. }
  2229.  
  2230. int dokeyedit(char *mcguffin, char *ringfile)
  2231. /*    Edit the userid and/or pass phrase for an RSA key pair, and
  2232.     put them back into the ring files.
  2233. */
  2234. {    unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION],
  2235.          d[MAX_UNIT_PRECISION], p[MAX_UNIT_PRECISION],
  2236.          q[MAX_UNIT_PRECISION], u[MAX_UNIT_PRECISION];
  2237.     char *fname, secring[MAX_PATH];
  2238.     FILE *f;
  2239.     word16 iv[4]; /* for IDEA CFB mode, to protect RSA secret key */
  2240.     byte userid[256];
  2241.     byte userid1[256];
  2242.     word32 tstamp; byte *timestamp = (byte *) &tstamp;    /* key certificate timestamp */
  2243.     byte keyID[KEYFRAGSIZE];
  2244.     boolean hidekey;    /* TRUE iff secret key is encrypted */
  2245.     boolean changed=FALSE, changeID=FALSE;
  2246.     byte ctb;
  2247.     int status;
  2248.     long fpp,fps,trust_pos, keylen;
  2249.     int pplength=0, pslength=0;
  2250.     byte ideakey[16];
  2251.     byte keyctrl;
  2252.  
  2253.     if (!ringfile || strlen(ringfile)==0 || !mcguffin || strlen(mcguffin)==0)
  2254.         return(-1);    /* Need ringfile name, user name */
  2255.  
  2256.     force_extension(ringfile,PGP_EXTENSION);
  2257.  
  2258.     if (!strncmp( ringfile, SECRET_KEYRING_FILENAME, strlen( SECRET_KEYRING_FILENAME )))
  2259.     {
  2260.         fprintf(pgpout, PSTR("\nThis operation may not be performed on a secret keyring.\n\
  2261. Defaulting to public keyring."));
  2262.         buildfilename( ringfile, PUBLIC_KEYRING_FILENAME );
  2263.     }
  2264.  
  2265.     strcpy((char *)userid, mcguffin);
  2266.     fprintf(pgpout,PSTR("\nEditing userid \"%s\" in key ring: '%s'.\n"),
  2267.         LOCAL_CHARSET((char *)userid),ringfile);
  2268.  
  2269.     if (!file_exists (ringfile))
  2270.     {    fprintf(pgpout,PSTR("\nCan't open public key ring file '%s'\n"),
  2271.             ringfile);
  2272.         return(-1);
  2273.     }
  2274.  
  2275.     status = getpublickey(GPK_GIVEUP|GPK_SHOW, ringfile, &fpp, &pplength, NULL,
  2276.                 timestamp, userid, n, e);
  2277.     if (status < 0)
  2278.     {    fprintf(pgpout,PSTR("\n\007Key not found in key ring '%s'.\n"),
  2279.             ringfile);
  2280.         return(-1);
  2281.     }
  2282.  
  2283.     /* Now add to pplength any following key control certificate */
  2284.     if ((f = fopen(ringfile,FOPRWBIN)) == NULL)
  2285.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),ringfile);
  2286.         return(-1);
  2287.     }
  2288.  
  2289.     if (fread(&ctb, 1, 1, f) != 1 || !is_ctb_type(ctb, CTB_CERT_PUBKEY_TYPE))
  2290.     {    fprintf(pgpout,PSTR("\n\007File '%s' is not a public keyring.\n"),ringfile);
  2291.         fclose(f);
  2292.         return(-1);
  2293.     }
  2294.  
  2295.     fseek(f, fpp, SEEK_SET);
  2296.     if (is_compromised(f))
  2297.     {    fprintf(pgpout, PSTR("\n\007This key has been revoked by its owner.\n"));
  2298.         fclose(f);
  2299.         return(-1);
  2300.     }
  2301.     trust_pos = fpp+pplength;
  2302.     fseek(f, trust_pos, SEEK_SET);
  2303.     if (read_trust(f, &keyctrl) < 0)
  2304.         trust_pos = -1;        /* keyfile: no trust byte */
  2305.  
  2306.     extract_keyID(keyID, n);
  2307.  
  2308.     /* Now read private key, too */
  2309.     strcpy(secring, ringfile);
  2310.     strcpy(file_tail(secring), SECRET_KEYRING_FILENAME);
  2311.  
  2312.     if (!file_exists (secring))
  2313.     {    fprintf(pgpout,PSTR("\nCan't open secret key ring file '%s'\n"),
  2314.             secring);
  2315.         fclose(f);
  2316.         return(-1);
  2317.     }
  2318.  
  2319.     /* Get position of key in secret key file */
  2320.     (void)getpublickey(GPK_GIVEUP, secring, &fps, &pslength, keyID,
  2321.         timestamp, userid1, n, e);
  2322.     /* This was done to get us fps and pslength */
  2323.     status = getsecretkey(GPK_GIVEUP, secring, keyID, timestamp,
  2324.         ideakey, &hidekey, userid1, n, e, d, p, q, u);
  2325.  
  2326.     if (status < 0)        /* key not in secret keyring: edit owner trust */
  2327.     {    int i;
  2328.  
  2329.         fprintf(pgpout, PSTR("\nNo secret key available.  Editing public key trust parameter.\n"));
  2330.         if (trust_pos < 0)
  2331.         {    fprintf(pgpout,PSTR("\n\007File '%s' is not a public keyring.\n"), ringfile);
  2332.             fclose(f);
  2333.             return(-1);
  2334.         }
  2335.         show_key(f, fpp, SHOW_ALL);
  2336.  
  2337.         init_trust_lst();
  2338.         fprintf(pgpout, PSTR("Current trust for this key's owner is: %s\n"),
  2339.                 trust_lst[keyctrl & KC_OWNERTRUST_MASK]);
  2340.  
  2341.         PascalToC((char *)userid);    /* convert to C string for display */
  2342.         i = ask_owntrust((char *) userid, keyctrl);
  2343.         if (i == (keyctrl & KC_OWNERTRUST_MASK))
  2344.         {    fclose(f);
  2345.             return(0);    /* unchanged */
  2346.         }
  2347.  
  2348.         if (i < 0 || i > KC_OWNERTRUST_ALWAYS)
  2349.         {
  2350.             fclose(f);
  2351.             return(-1);
  2352.         }
  2353.         keyctrl = (keyctrl & ~KC_OWNERTRUST_MASK) | i;
  2354.  
  2355.         fseek(f, trust_pos, SEEK_SET);
  2356.         write_trust(f, keyctrl);
  2357.         fclose(f);
  2358.         fprintf (pgpout, PSTR("Public key ring updated.\n"));
  2359.         return(0);
  2360.     }
  2361.     if (trust_pos > 0 && (keyctrl & (KC_BUCKSTOP|KC_OWNERTRUST_MASK)) !=
  2362.             (KC_OWNERTRUST_ULTIMATE|KC_BUCKSTOP))
  2363.     {    /* key is in secret keyring but buckstop is not set */
  2364.         fprintf(pgpout, PSTR("\nUse this key as an ultimately-trusted introducer (y/N)? "), userid);
  2365.         if (getyesno('n'))
  2366.         {    fseek(f, trust_pos, SEEK_SET);
  2367.             keyctrl = KC_OWNERTRUST_ULTIMATE|KC_BUCKSTOP;
  2368.             write_trust(f, keyctrl);
  2369.         }
  2370.     }
  2371.  
  2372.     /* Show user her ID again to be clear */
  2373.     PascalToC((char *)userid);
  2374.     fprintf(pgpout,PSTR("\nCurrent user ID: %s"),
  2375.         LOCAL_CHARSET((char *)userid));
  2376.     CToPascal((char *)userid);
  2377.  
  2378.     fprintf(pgpout, PSTR("\nDo you want to change your user ID (y/N)? "));
  2379.     if (getyesno('n'))    /* user said yes */
  2380.     {    fprintf(pgpout,PSTR("\nEnter the new user ID: "));
  2381.         getstring((char *)userid,255,TRUE); /* echo keyboard input */
  2382.         if (userid[0] == '\0')
  2383.         {    fclose(f);
  2384.             return(-1);
  2385.         }
  2386.         CONVERT_TO_CANONICAL_CHARSET((char *)userid);
  2387.         fprintf(pgpout, PSTR("\nMake this user ID the primary user ID for this key (y/N)? "));
  2388.         if (!getyesno('n'))
  2389.         {    /* position file pointer at selected user id */
  2390.             int pktlen;
  2391.             long fpuser;
  2392.  
  2393.             strcpy((char *)userid1, mcguffin);
  2394.             if (getpubuserid(ringfile, fpp, userid1, &fpuser, &pktlen, FALSE) < 0)
  2395.             {    fclose(f);
  2396.                 return(-1);
  2397.             }
  2398.             fseek(f, fpuser, SEEK_SET);
  2399.         }
  2400.         else    /* position file pointer at key packet */
  2401.             fseek(f, fpp, SEEK_SET);
  2402.         nextkeypacket(f, &ctb);    /* skip userid or key packet */
  2403.         do    /* new user id will be inserted before next userid or key packet */
  2404.         {    fpp = ftell(f);
  2405.             if (nextkeypacket(f, &ctb) < 0)
  2406.                 break;
  2407.         } while (ctb != CTB_USERID && !is_key_ctb(ctb));
  2408.         CToPascal((char *)userid);        /* convert to length-prefixed string */
  2409.         changeID = TRUE;
  2410.         changed = TRUE;
  2411.     }
  2412.     fclose(f);
  2413.  
  2414.     fprintf (pgpout,PSTR("\nDo you want to change your pass phrase (y/N)? "));
  2415.     if (getyesno('n'))    /* user said yes */
  2416.     {    hidekey = (GetHashedPassPhrase((char *) ideakey, 2) > 0);
  2417.         changed = TRUE;
  2418.     }
  2419.  
  2420.     if (!changed)
  2421.     {    fprintf (pgpout, PSTR("(No changes will be made.)\n"));
  2422.         if (hidekey)
  2423.             burn(ideakey);
  2424.         goto done;
  2425.     }
  2426.  
  2427.     /* init CFB IDEA key */
  2428.     if (hidekey)
  2429.     {    fill0(iv,8);
  2430.         initcfb_idea(iv,ideakey,FALSE);
  2431.         burn(ideakey);
  2432.     }
  2433.  
  2434.     /* First write secret key data to a file */
  2435.     fname = tempfile(TMP_TMPDIR|TMP_WIPE);
  2436.     writekeyfile(fname,hidekey,timestamp,userid,n,e,d,p,q,u);
  2437.     if (changeID)
  2438.         keylen = -1;
  2439.     else
  2440.     {    /* don't copy userid */
  2441.         f = fopen(fname, FOPRBIN);
  2442.         if (f == NULL)
  2443.             goto err;
  2444.         nextkeypacket(f, &ctb);    /* skip key packet */
  2445.         keylen = ftell(f);
  2446.         fclose(f);
  2447.     }
  2448.     if (merge_key_to_ringfile(fname,secring,fps,pslength,keylen) < 0)
  2449.     {    fprintf (pgpout, PSTR("\n\007Unable to update secret key ring.\n"));
  2450.         goto err;
  2451.     }
  2452.     fprintf (pgpout, PSTR("\nSecret key ring updated...\n"));
  2453.  
  2454.     /* Now write public key data to file */
  2455.     if (changeID)
  2456.     {
  2457.         if (insert_userid(ringfile, userid, fpp) < 0)
  2458.         {    fprintf (pgpout, PSTR("\n\007Unable to update public key ring.\n"));
  2459.             goto err;
  2460.         }
  2461.         fprintf (pgpout, PSTR("Public key ring updated.\n"));
  2462.     }
  2463.     else
  2464.         fprintf (pgpout, PSTR("(No need to update public key ring)\n"));
  2465.  
  2466.     if (hidekey)    /* done with IDEA to protect RSA secret key */
  2467.         close_idea();
  2468.  
  2469.     rmtemp(fname);
  2470.  
  2471. done:
  2472.     mp_burn(d);    /* burn sensitive data on stack    */
  2473.     mp_burn(p);
  2474.     mp_burn(q);
  2475.     mp_burn(u);
  2476.     mp_burn(e);
  2477.     mp_burn(n);
  2478.     burn(iv);
  2479.  
  2480.     return(0);    /* normal return */
  2481. err:
  2482.     mp_burn(d);    /* burn sensitive data on stack */
  2483.     mp_burn(p);
  2484.     mp_burn(q);
  2485.     mp_burn(u);
  2486.     mp_burn(e);
  2487.     mp_burn(n);
  2488.     burn(iv);
  2489.  
  2490.     rmtemp(fname);
  2491.  
  2492.     return(-1);    /* error return */
  2493.  
  2494. }    /* dokeyedit */
  2495.  
  2496.  
  2497. int disable_key(char *keyguffin, char *keyfile)
  2498. {    
  2499.     FILE *f;
  2500.     byte keyctrl;
  2501.     byte keyID[KEYFRAGSIZE];
  2502.     byte userid[256];
  2503.     unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
  2504.     long fp;
  2505.     int pktlen;
  2506.  
  2507.     strcpy((char *)userid, keyguffin);
  2508.     if (getpublickey(GPK_SHOW|GPK_DISABLED, keyfile, &fp, &pktlen, NULL,
  2509.             NULL, userid, n, e) < 0)
  2510.         return(-1);
  2511.  
  2512.     extract_keyID(keyID, n);
  2513.     if (getsecretkey(GPK_GIVEUP, NULL, keyID, NULL, NULL, NULL,
  2514.                      userid, n, e, NULL, NULL, NULL, NULL) >= 0)
  2515.     {    /* can only compromise if key also in secring */
  2516.         PascalToC((char *) userid);
  2517.         fprintf(pgpout, 
  2518. PSTR("\nDo you want to permanently revoke your public key\n\
  2519. by issuing a secret key compromise certificate\n\
  2520. for \"%s\" (y/N)? "), LOCAL_CHARSET((char *)userid));
  2521.         if (getyesno('n'))
  2522.             return compromise(keyID, keyfile);
  2523.     }
  2524.     if ((f = fopen(keyfile,FOPRWBIN)) == NULL)
  2525.     {    fprintf(pgpout,PSTR("\n\007Can't open key ring file '%s'\n"),keyfile);
  2526.         return(-1);
  2527.     }
  2528.     fseek(f, fp+pktlen, SEEK_SET);
  2529.     if (read_trust(f, &keyctrl) < 0)
  2530.     {
  2531.         fprintf(pgpout,PSTR("\n\007File '%s' is not a public keyring.\n"), keyfile);
  2532.         fprintf(pgpout, PSTR("You can only disable keys on your public keyring.\n"));
  2533.         fclose(f);
  2534.         return -1;
  2535.     }
  2536.     if (keyctrl & KC_DISABLED)
  2537.     {
  2538.         fprintf(pgpout, PSTR("\nKey is already disabled.\n\
  2539. Do you want to enable this key again (y/N)? "));
  2540.         keyctrl &= ~KC_DISABLED;
  2541.     }
  2542.     else
  2543.     {
  2544.         fprintf(pgpout, PSTR("\nDisable this key (y/N)? "));
  2545.         keyctrl |= KC_DISABLED;
  2546.     }
  2547.     if (!getyesno('n'))
  2548.     {    fclose(f);
  2549.         return -1;
  2550.     }
  2551.     write_trust_pos(f, keyctrl, fp+pktlen);
  2552.     fclose(f);
  2553.     return 0;
  2554. }    /* disable_key */
  2555.  
  2556.  
  2557. /*======================================================================*/
  2558.  
  2559.  
  2560.  
  2561. int dokeygen(char *numstr, char *numstr2)
  2562. /*    Do an RSA key pair generation, and write them out to the keyring files.
  2563.     numstr is a decimal string, the desired bitcount for the modulus n.
  2564.     numstr2 is a decimal string, the desired bitcount for the exponent e.
  2565. */
  2566. {    unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION], d[MAX_UNIT_PRECISION],
  2567.          p[MAX_UNIT_PRECISION], q[MAX_UNIT_PRECISION], u[MAX_UNIT_PRECISION];
  2568.     char *fname;
  2569.     char ringfile[MAX_PATH];
  2570.     word16 iv[4]; /* for IDEA CFB mode, to protect RSA secret key */
  2571.     byte userid[256];
  2572.     short keybits,ebits;
  2573.     word32 tstamp; byte *timestamp = (byte *) &tstamp;        /* key certificate timestamp */
  2574.     boolean hidekey;    /* TRUE iff secret key is encrypted */
  2575.     boolean seedfileexists;    /* FALSE if we need to create one */
  2576.     byte ideakey[16];
  2577.  
  2578.     if (!numstr || strlen(numstr)==0)
  2579.     {    fprintf(pgpout,PSTR("\nPick your RSA key size:\
  2580. \n    1)     384 bits- Casual grade, fast but less secure\
  2581. \n    2)     512 bits- Commercial grade, medium speed, good security\
  2582. \n    3)    1024 bits- Military grade, very slow, highest security\
  2583. \nChoose 1, 2, or 3, or enter desired number of bits: "));
  2584.         numstr = (char *)userid;    /* use userid buffer as scratchpad */
  2585.         getstring(numstr,5,TRUE);    /* echo keyboard */
  2586.     }
  2587.  
  2588.     keybits = 0;
  2589.     while ((*numstr>='0') && (*numstr<='9'))
  2590.         keybits = keybits*10 + (*numstr++ - '0');
  2591.  
  2592.     if (keybits==0)    /* user entered null response */
  2593.         return(-1);    /* error return */
  2594.  
  2595.     /* Standard default key sizes: */
  2596.     if (keybits==1) keybits=384;    /* Casual grade */
  2597.     if (keybits==2) keybits=512;    /* Commercial grade */
  2598.     if (keybits==3) keybits=1024;    /* Military grade */
  2599.  
  2600. #ifndef DEBUG
  2601.     /* minimum RSA keysize: */
  2602.     if (keybits < 384) keybits=384;
  2603.     if (keybits > MAX_BIT_PRECISION-UNITSIZE)    /* Paranoia */
  2604.         keybits = MAX_BIT_PRECISION-UNITSIZE;
  2605. #else
  2606.     if (keybits > MAX_BIT_PRECISION)
  2607.         keybits = MAX_BIT_PRECISION;
  2608. #endif
  2609.  
  2610. #ifdef notdef    /* This annoys everyone, so take it out. */
  2611. /*    If we use Merritt's modmult algorithm, the primes p and q's 
  2612.     bit length should not be an exact multiple of UNITSIZE, 
  2613.     because Merritt's modmult algorithm performs slowest in that 
  2614.     case, wasting an extra unit of precision for overflow.
  2615. */
  2616.     if ((keybits % (2*UNITSIZE))==0)
  2617.         keybits -= 2;    /* make each prime one bit shorter. */
  2618. #endif    /* MERRITT */
  2619.  
  2620.     ebits = 0;    /* number of bits in e */
  2621.     while ((*numstr2>='0') && (*numstr2<='9')) 
  2622.         ebits = ebits*10 + (*numstr2++ - '0');
  2623.  
  2624.     fprintf(pgpout,PSTR("\nGenerating an RSA key with a %d-bit modulus... "),keybits);
  2625.  
  2626.     fprintf(pgpout,
  2627. PSTR("\nYou need a user ID for your public key.  The desired form for this\n\
  2628. user ID is your name, followed by your E-mail address enclosed in\n\
  2629. <angle brackets>, if you have an E-mail address.\n\
  2630. For example:  John Q. Smith <12345.6789@compuserve.com>\n"));
  2631.     fprintf(pgpout,PSTR("\nEnter a user ID for your public key: \n"));
  2632. #ifdef VMS
  2633.     putch('\n'); /* That last newline was just a return, do a real one */
  2634. #endif
  2635.     getstring((char *)userid,255,TRUE);    /* echo keyboard input */
  2636.     if (userid[0]=='\0')    /* user entered null response */
  2637.         return(-1);    /* error return */
  2638.     CONVERT_TO_CANONICAL_CHARSET((char *)userid);
  2639.     CToPascal((char *)userid);    /* convert to length-prefixed string */
  2640.  
  2641.     {    fprintf(pgpout,
  2642. PSTR("\nYou need a pass phrase to protect your RSA secret key.\n\
  2643. Your pass phrase can be any sentence or phrase and may have many\n\
  2644. words, spaces, punctuation, or any other printable characters. "));
  2645.         hidekey = (GetHashedPassPhrase((char *) ideakey, 2) > 0);
  2646.         /* init CFB IDEA key */
  2647.         if (hidekey)
  2648.         {    fill0(iv,8);
  2649.             initcfb_idea(iv,ideakey,FALSE);
  2650.             randaccum_later(64);    /* IV for encryption */
  2651.         }
  2652.     }
  2653. /* As rsa_keygen does a major accumulation of random bits, if we need
  2654.    any others for a seed file, let's get them at the same time. */
  2655.     seedfileexists = seedfile_exists();
  2656.  
  2657.     fprintf(pgpout,PSTR("\nNote that key generation is a VERY lengthy process.\n"));
  2658.  
  2659.     if (rsa_keygen(n,e,d,p,q,u,keybits,ebits) < 0)
  2660.     {    fprintf(pgpout,PSTR("\n\007Keygen failed!\n"));
  2661.         return(-1);    /* error return */
  2662.     }
  2663.  
  2664.     if (verbose)
  2665.     {
  2666.         fprintf(pgpout,PSTR("Key ID %s\n"), key2IDstring(n));
  2667.  
  2668.         mp_display(" modulus n = ",n);
  2669.         mp_display("exponent e = ",e);
  2670.  
  2671.         mp_display("exponent d = ",d);
  2672.         mp_display("   prime p = ",p);
  2673.         mp_display("   prime q = ",q);
  2674.         mp_display(" inverse u = ",u);
  2675.     }
  2676.  
  2677.     get_timestamp(timestamp);    /* Timestamp when key was generated */
  2678.  
  2679.     fputc('\007',pgpout);  /* sound the bell when done with lengthy process */
  2680.     fputc('\n',pgpout);
  2681.  
  2682.     /* First, write out the secret key... */
  2683.     fname = tempfile(TMP_TMPDIR|TMP_WIPE);
  2684.     writekeyfile(fname,hidekey,timestamp,userid,n,e,d,p,q,u); 
  2685.     buildfilename(ringfile,SECRET_KEYRING_FILENAME);
  2686.     if (file_exists(ringfile))
  2687.     {    merge_key_to_ringfile(fname,ringfile,0L,0,-1L);
  2688.         rmtemp(fname);
  2689.     }
  2690.     else
  2691.         savetemp(fname, ringfile);
  2692.  
  2693.     /* Second, write out the public key... */
  2694.     fname = tempfile(TMP_TMPDIR|TMP_WIPE);
  2695.     writekeyfile(fname,FALSE,timestamp,userid,n,e,NULL,NULL,NULL,NULL);
  2696.     buildfilename(ringfile,PUBLIC_KEYRING_FILENAME);
  2697.     if (file_exists(ringfile))
  2698.     {    merge_key_to_ringfile(fname,ringfile,0L,0,-1L);
  2699.         rmtemp(fname);
  2700.     }
  2701.     else
  2702.         savetemp(fname, ringfile);
  2703.     
  2704.     if (hidekey)    /* done with IDEA to protect RSA secret key */
  2705.         close_idea();
  2706.  
  2707.     mp_burn(d);    /* burn sensitive data on stack */
  2708.     mp_burn(p);    /* burn sensitive data on stack */
  2709.     mp_burn(q);    /* burn sensitive data on stack */
  2710.     mp_burn(u);    /* burn sensitive data on stack */
  2711.     mp_burn(e);    /* burn sensitive data on stack */
  2712.     mp_burn(n);    /* burn sensitive data on stack */
  2713.     burn(iv);    /* burn sensitive data on stack */
  2714.  
  2715.     fprintf(pgpout,PSTR("\007Key generation completed.\n"));
  2716.  
  2717.     /*    If we need a seed file, create it now.
  2718.     */
  2719.     if (!seedfileexists)
  2720.         create_seedfile();
  2721.  
  2722.     return(0);    /* normal return */
  2723. }    /* dokeygen */
  2724.  
  2725.