Пример #1
0
gcry_error_t decryptFile(FILE *infile, unsigned char *IV, unsigned char *key, FILE *outfile) {
  gcry_error_t error;
  gcry_cipher_hd_t hd;

  unsigned char inbuffer[16];
  unsigned char obp[16];
  unsigned char *outbuffer = NULL;

  int eof = 0;
  int n;

  int strength = getCipherStrength();

  int algorithm = strength == 256 ? GCRY_CIPHER_AES256 : GCRY_CIPHER_AES128;

  size_t keylength = strength == 256 ? 32 : 16;

  error = gcry_cipher_open(&hd, algorithm, GCRY_CIPHER_MODE_OFB, 0);

  if (error != GPG_ERR_NO_ERROR)
    return error;

  error = gcry_cipher_setkey(hd, key, keylength);

  if (error != GPG_ERR_NO_ERROR)
    return error;

  error = gcry_cipher_setiv(hd, IV, 16);

  if (error != GPG_ERR_NO_ERROR)
    return error;

  while (!eof) {
    n = fread(inbuffer, 1, 16, infile);

    if (n > 0 && n < 16)
      return gcry_error_from_errno(EPIPE);

    if (n == 0) {
      if (outbuffer == NULL)
	return gcry_error_from_errno(EINVAL);

      n = 16 - (int)outbuffer[15];

      eof = 1;
    }

    if (outbuffer != NULL)
      fwrite(outbuffer, 1, n, outfile);

    outbuffer = obp;
    
    error = gcry_cipher_decrypt(hd, outbuffer, 16, inbuffer, 16);

    if (error != GPG_ERR_NO_ERROR)
      return error;
  }

  return GPG_ERR_NO_ERROR;
}
Пример #2
0
static void test_otrl_instag_read(void)
{
	OtrlUserState us = otrl_userstate_create();
	OtrlInsTag* one, *two, *three, *four;
	char sone[9] = {0},
		 stwo[9] = {0},
		 sfour[9] = {0};
	one = two = three = four = NULL;
	ok(otrl_instag_read(us, "/non_existent_file") ==
			gcry_error_from_errno(ENOENT),
			"Non-existent file detected");

	ok(otrl_instag_read(us, instag_filepath) == GPG_ERR_NO_ERROR,
			"Instag called with success");

	one = otrl_instag_find(us, "alice_xmpp", "XMPP");
	snprintf(sone, sizeof(sone), "%08x", one->instag);

	two = otrl_instag_find(us, "alice_irc", "IRC");
	snprintf(stwo, sizeof(stwo), "%08x", two->instag);
	
	three = otrl_instag_find(us, "alice_inv", "IRC");
	
	four = otrl_instag_find(us, "alice_icq", "ICQ");
	snprintf(sfour, sizeof(sfour), "%08x", four->instag);

	ok(one && two && !three && four &&
			strcmp(sone, "01234567") == 0 &&
			strcmp(stwo, "9abcdef0") == 0 &&
			strcmp(sfour, "98765432") == 0,
			"Instag succesfully read");
}
Пример #3
0
/* Generate a private DSA key for a given account, storing it into a
 * file on disk, and loading it into the given OtrlUserState.  Overwrite any
 * previously generated keys for that account in that OtrlUserState. */
gcry_error_t otrl_privkey_generate(OtrlUserState us, const char *filename,
	const char *accountname, const char *protocol)
{
    gcry_error_t err;
    FILE *privf;
#ifndef WIN32
    mode_t oldmask;
#endif

#ifndef WIN32
    oldmask = umask(077);
#endif
    privf = fopen(filename, "w+b");
    if (!privf) {
#ifndef WIN32
	umask(oldmask);
#endif
	err = gcry_error_from_errno(errno);
	return err;
    }

    err = otrl_privkey_generate_FILEp(us, privf, accountname, protocol);

    fclose(privf);
#ifndef WIN32
    umask(oldmask);
#endif
    return err;
}
Пример #4
0
gcry_error_t jsapi_userstate_write_to_file(OtrlUserState us, const char *filename){
    gcry_error_t err = GPG_ERR_NO_ERROR;
    FILE *privf;
    OtrlPrivKey *p;
    mode_t oldmask;
    oldmask = umask(077);

    privf = fopen(filename, "w+b");

    if (!privf) {
        umask(oldmask);
    	err = gcry_error_from_errno(errno);
    	return err;
    }

    /* Output all the keys we know ...*/
    fprintf(privf, "(privkeys\n");

    for (p=us->privkey_root; p; p=p->next) {
	    jsapi_account_write(privf, p->accountname, p->protocol, p->privkey);
    }

    fprintf(privf, ")\n");
    fseek(privf, 0, SEEK_SET);
    fclose(privf);
    umask(oldmask);
    return err;
}
Пример #5
0
/* Read our instance tag from a file on disk into the given
 * OtrlUserState. */
gcry_error_t otrl_instag_read(OtrlUserState us, const char *filename)
{
    gcry_error_t err;
    FILE *instf;

    /* Open the instance tag file. */
    instf = fopen(filename, "rb");
    if (!instf) {
	return gcry_error_from_errno(errno);
    }

    err = otrl_instag_read_FILEp(us, instf);
    fclose(instf);
    return err;
}
Пример #6
0
/* Generate a new instance tag for the given account and write to file */
gcry_error_t otrl_instag_generate(OtrlUserState us, const char *filename,
	const char *accountname, const char *protocol)
{
    gcry_error_t err;
    FILE *instf;

    /* Open the instance tag file. */
    instf = fopen(filename, "wb");
    if (!instf) {
	return gcry_error_from_errno(errno);
    }

    err = otrl_instag_generate_FILEp(us, instf, accountname, protocol);
    fclose(instf);
    return err;
}
Пример #7
0
/* Write the fingerprint store from a given OtrlUserState to a file on disk. */
gcry_error_t otrl_privkey_write_fingerprints(OtrlUserState us,
	const char *filename)
{
    gcry_error_t err;
    FILE *storef;

    storef = fopen(filename, "wb");
    if (!storef) {
	err = gcry_error_from_errno(errno);
	return err;
    }

    err = otrl_privkey_write_fingerprints_FILEp(us, storef);

    fclose(storef);
    return err;
}
Пример #8
0
/* Read the fingerprint store from a file on disk into the given
 * OtrlUserState.  Use add_app_data to add application data to each
 * ConnContext so created. */
gcry_error_t otrl_privkey_read_fingerprints(OtrlUserState us,
	const char *filename,
	void (*add_app_data)(void *data, ConnContext *context),
	void  *data)
{
    gcry_error_t err;
    FILE *storef;

    storef = fopen(filename, "rb");
    if (!storef) {
	err = gcry_error_from_errno(errno);
	return err;
    }

    err = otrl_privkey_read_fingerprints_FILEp(us, storef, add_app_data, data);

    fclose(storef);
    return err;
}
Пример #9
0
/* Read a sets of private DSA keys from a file on disk into the given
 * OtrlUserState. */
gcry_error_t otrl_privkey_read(OtrlUserState us, const char *filename)
{
    FILE *privf;
    gcry_error_t err;

    /* Open the privkey file.  We use rb mode so that on WIN32, fread()
     * reads the same number of bytes that fstat() indicates are in the
     * file. */
    privf = fopen(filename, "rb");
    if (!privf) {
	err = gcry_error_from_errno(errno);
	return err;
    }

    err = otrl_privkey_read_FILEp(us, privf);

    fclose(privf);
    return err;
}
Пример #10
0
/* Write only the TRUSTED fingerprints from store from a given OtrlUserState to a file on disk. */
gcry_error_t jsapi_privkey_write_trusted_fingerprints(OtrlUserState us,
    const char *filename)
{
    gcry_error_t error;
    FILE *storef;
    ConnContext *context;
    Fingerprint *fingerprint;

    error = gcry_error(GPG_ERR_NO_ERROR);

    for(context = us->context_root; context; context = context->next) {
        /* Fingerprints are only stored in the master contexts */
        if (context->their_instance != OTRL_INSTAG_MASTER) continue;

        /* Don't bother with the first (fingerprintless) entry. */
        for (fingerprint = context->fingerprint_root.next; fingerprint && fingerprint->trust[0]!='\0' ;
            fingerprint = fingerprint->next) {
            int i;
            //only open the file if we have something to write
            if(storef == NULL){
                storef = fopen(filename, "wb");
                if(!storef) {
                    error = gcry_error_from_errno(errno);
                    return error;
                }
            }
            fprintf(storef, "%s\t%s\t%s\t", context->username,
                context->accountname, context->protocol);
            for(i=0;i<20;++i) {
                fprintf(storef, "%02x", fingerprint->fingerprint[i]);
            }
            fprintf(storef, "\t%s\n", fingerprint->trust ? fingerprint->trust : "");
        }
    }
  if(storef != NULL) fclose(storef);
  return error;
}
Пример #11
0
/* Read a sets of private DSA keys from a FILE* into the given
 * OtrlUserState.  The FILE* must be open for reading. */
gcry_error_t otrl_privkey_read_FILEp(OtrlUserState us, FILE *privf)
{
    int privfd;
    struct stat st;
    char *buf;
    const char *token;
    size_t tokenlen;
    gcry_error_t err;
    gcry_sexp_t allkeys;
    size_t i;

    if (!privf) return gcry_error(GPG_ERR_NO_ERROR);

    /* Release any old ideas we had about our keys */
    otrl_privkey_forget_all(us);

    /* Load the data into a buffer */
    privfd = fileno(privf);
    if (fstat(privfd, &st)) {
	err = gcry_error_from_errno(errno);
	return err;
    }
    buf = malloc(st.st_size);
    if (!buf && st.st_size > 0) {
	return gcry_error(GPG_ERR_ENOMEM);
    }
    if (fread(buf, st.st_size, 1, privf) != 1) {
	err = gcry_error_from_errno(errno);
	free(buf);
	return err;
    }

    err = gcry_sexp_new(&allkeys, buf, st.st_size, 0);
    free(buf);
    if (err) {
	return err;
    }

    token = gcry_sexp_nth_data(allkeys, 0, &tokenlen);
    if (tokenlen != 8 || strncmp(token, "privkeys", 8)) {
	gcry_sexp_release(allkeys);
	return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
    }

    /* Get each account */
    for(i=1; i<gcry_sexp_length(allkeys); ++i) {
	gcry_sexp_t names, protos, privs;
	char *name, *proto;
	gcry_sexp_t accounts;
	OtrlPrivKey *p;
	
	/* Get the ith "account" S-exp */
	accounts = gcry_sexp_nth(allkeys, i);
	
	/* It's really an "account" S-exp? */
	token = gcry_sexp_nth_data(accounts, 0, &tokenlen);
	if (tokenlen != 7 || strncmp(token, "account", 7)) {
	    gcry_sexp_release(accounts);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
	}
	/* Extract the name, protocol, and privkey S-exps */
	names = gcry_sexp_find_token(accounts, "name", 0);
	protos = gcry_sexp_find_token(accounts, "protocol", 0);
	privs = gcry_sexp_find_token(accounts, "private-key", 0);
	gcry_sexp_release(accounts);
	if (!names || !protos || !privs) {
	    gcry_sexp_release(names);
	    gcry_sexp_release(protos);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
	}
	/* Extract the actual name and protocol */
	token = gcry_sexp_nth_data(names, 1, &tokenlen);
	if (!token) {
	    gcry_sexp_release(names);
	    gcry_sexp_release(protos);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
	}
	name = malloc(tokenlen + 1);
	if (!name) {
	    gcry_sexp_release(names);
	    gcry_sexp_release(protos);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_ENOMEM);
	}
	memmove(name, token, tokenlen);
	name[tokenlen] = '\0';
	gcry_sexp_release(names);

	token = gcry_sexp_nth_data(protos, 1, &tokenlen);
	if (!token) {
	    free(name);
	    gcry_sexp_release(protos);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
	}
	proto = malloc(tokenlen + 1);
	if (!proto) {
	    free(name);
	    gcry_sexp_release(protos);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_ENOMEM);
	}
	memmove(proto, token, tokenlen);
	proto[tokenlen] = '\0';
	gcry_sexp_release(protos);

	/* Make a new OtrlPrivKey entry */
	p = malloc(sizeof(*p));
	if (!p) {
	    free(name);
	    free(proto);
	    gcry_sexp_release(privs);
	    gcry_sexp_release(allkeys);
	    return gcry_error(GPG_ERR_ENOMEM);
	}

	/* Fill it in and link it up */
	p->accountname = name;
	p->protocol = proto;
	p->pubkey_type = OTRL_PUBKEY_TYPE_DSA;
	p->privkey = privs;
	p->next = us->privkey_root;
	if (p->next) {
	    p->next->tous = &(p->next);
	}
	p->tous = &(us->privkey_root);
	us->privkey_root = p;
	err = make_pubkey(&(p->pubkey_data), &(p->pubkey_datalen), p->privkey);
	if (err) {
	    gcry_sexp_release(allkeys);
	    otrl_privkey_forget(p);
	    return gcry_error(GPG_ERR_UNUSABLE_SECKEY);
	}
    }
    gcry_sexp_release(allkeys);

    return gcry_error(GPG_ERR_NO_ERROR);
}