Ejemplo n.º 1
0
int upton_modmult (unitptr prod, unitptr multiplicand, unitptr multiplier)
{
 unitptr d = d_data;
 while (mp_compare(d,modulus) > 0)
   memcpy((void*)(prod), (const void*)(d), (global_precision));
}
Ejemplo n.º 2
0
/* Adds (prepends) key file to key ring file. */
int _addto_keyring(char *keyfile, char *ringfile)
{
    FILE *f, *g, *h;
    long file_position, fp;
    int pktlen;
    byte ctb;
    int status;
    unit n[MAX_UNIT_PRECISION], e[MAX_UNIT_PRECISION];
    unit n1[MAX_UNIT_PRECISION];
    byte keyID[KEYFRAGSIZE];
    byte userid[256];		/* key certificate userid */
    byte userid1[256];
    word32 tstamp;
    byte *timestamp = (byte *) & tstamp;	/* key certificate timestamp */
    boolean userid_seen = FALSE;
    int commonkeys = 0;
    int copying;
    struct newkey *nkey, *nkeys = NULL;
    char *scratchf;

    /* open file f for read, in binary (not text) mode... */
    if ((f = fopen(keyfile, FOPRBIN)) == NULL) {
	fprintf(pgpout, LANG("\n\007Can't open key file '%s'\n"), keyfile);
	return -1;
    }
    ctb = 0;
    if (fread(&ctb, 1, 1, f) != 1 || !is_key_ctb(ctb)) {
	fclose(f);
	return -1;
    }
    rewind(f);

    setoutdir(ringfile);
    scratchf = tempfile(0);

    /*
     * get userids from both files, maybe should also use the default public
     * keyring if ringfile is not the default ring.
     */
    setkrent(ringfile);
    setkrent(keyfile);
    init_userhash();

    if (!file_exists(ringfile)) {
	/* ringfile does not exist.  Can it be created? */
	/* open file g for writing, in binary (not text) mode... */
	g = fopen(ringfile, FOPWBIN);
	if (g == NULL) {
	    fprintf(pgpout,
		    LANG("\nKey ring file '%s' cannot be created.\n"),
		    ringfile);
	    fclose(f);
	    goto err;
	}
	fclose(g);
    }
    /* Create working output file */
    /* open file g for writing, in binary (not text) mode... */
    if ((g = fopen(scratchf, FOPWBIN)) == NULL) {
	fclose(f);
	goto err;
    }
    newkeys = newsigs = newids = newrvks = 0;

    /* Pass 1 - copy all keys from f which aren't in ring file */
    /* Also copy userid and signature packets. */
    fprintf(pgpout, LANG("\nLooking for new keys...\n"));
    copying = FALSE;
    if (gpk_open(ringfile) < 0) {
	fclose(f);		/* close key file */
	fclose(g);
	goto err;
    }
    for (;;) {
	file_position = ftell(f);

	status = readkeypacket(f, FALSE, &ctb,
			       timestamp, (char *) userid, n, e,
			       NULL, NULL, NULL, NULL, NULL, NULL);
	/* Note that readkeypacket has called set_precision */
	if (status == -1)	/* EOF */
	    break;
	if (status == -2 || status == -3) {
	    fprintf(pgpout,
		    LANG("\n\007Could not read key from file '%s'.\n"),
		    keyfile);
	    fclose(f);		/* close key file */
	    fclose(g);
	    goto err;
	}
	if (status < 0) {
	    copying = FALSE;
	    continue;	/* don't merge keys from unrecognized version */
	}
	/* Check to see if key is already on key ring */
	if (is_key_ctb(ctb)) {
	    extract_keyID(keyID, n);	/* from keyfile, not ringfile */
	    publickey = is_ctb_type(ctb, CTB_CERT_PUBKEY_TYPE);

	    /*      Check for duplicate key in key ring: */
	    status = get_publickey(&fp, NULL, keyID, timestamp, userid, n1, e);
	    if (status == 0) {
		/* key in both keyring and keyfile */
		if (mp_compare(n, n1) != 0) {
		    fprintf(pgpout,
LANG("\n\007Warning: Key ID %s matches key ID of key already on \n\
key ring '%s', but the keys themselves differ.\n\
This is highly suspicious.  This key will not be added to ring.\n\
Acknowledge by pressing return: "), keyIDstring(keyID), ringfile);
		    getyesno('n');
		} else {
		    ++commonkeys;
		}
		copying = FALSE;
	    } else if (status == -1) {	/* key NOT in keyring */