/* read any gpg config file */ static int conffile(netpgp_t *netpgp, char *homedir, char *userid, size_t length) { regmatch_t matchv[10]; regex_t keyre; char buf[BUFSIZ]; FILE *fp; __PGP_USED(netpgp); (void) snprintf(buf, sizeof(buf), "%s/gpg.conf", homedir); if ((fp = fopen(buf, "r")) == NULL) { return 0; } (void) memset(&keyre, 0x0, sizeof(keyre)); (void) regcomp(&keyre, "^[ \t]*default-key[ \t]+([0-9a-zA-F]+)", REG_EXTENDED); while (fgets(buf, (int)sizeof(buf), fp) != NULL) { if (regexec(&keyre, buf, 10, matchv, 0) == 0) { (void) memcpy(userid, &buf[(int)matchv[1].rm_so], MIN((unsigned)(matchv[1].rm_eo - matchv[1].rm_so), length)); if (netpgp->passfp == NULL) { (void) fprintf(stderr, "netpgp: default key set to \"%.*s\"\n", (int)(matchv[1].rm_eo - matchv[1].rm_so), &buf[(int)matchv[1].rm_so]); } } } (void) fclose(fp); regfree(&keyre); return 1; }
/** \ingroup HighLevel_Crypto Encrypt a file \param infile Name of file to be encrypted \param outfile Name of file to write to. If NULL, name is constructed from infile \param pubkey Public Key to encrypt file for \param use_armour Write armoured text, if set \param allow_overwrite Allow output file to be overwrwritten if it exists \return 1 if OK; else 0 */ unsigned pgp_encrypt_file(pgp_io_t *io, const char *infile, const char *outfile, const pgp_key_t *key, const unsigned use_armour, const unsigned allow_overwrite, const char *cipher) { pgp_output_t *output; pgp_memory_t *inmem; int fd_out; __PGP_USED(io); inmem = pgp_memory_new(); if (!pgp_mem_readfile(inmem, infile)) { return 0; } fd_out = pgp_setup_file_write(&output, outfile, allow_overwrite); if (fd_out < 0) { pgp_memory_free(inmem); return 0; } /* set armoured/not armoured here */ if (use_armour) { pgp_writer_push_armor_msg(output); } /* Push the encrypted writer */ if (!pgp_push_enc_se_ip(output, key, cipher)) { pgp_memory_free(inmem); return 0; } /* This does the writing */ pgp_write(output, pgp_mem_data(inmem), (unsigned)pgp_mem_len(inmem)); /* tidy up */ pgp_memory_free(inmem); pgp_teardown_file_write(output, fd_out); return 1; }
/* this interface isn't right - hook into callback for getting passphrase */ char * pgp_export_key(pgp_io_t *io, const pgp_key_t *keydata, uint8_t *passphrase) { pgp_output_t *output; pgp_memory_t *mem; char *cp; __PGP_USED(io); pgp_setup_memory_write(&output, &mem, 128); if (keydata->type == PGP_PTAG_CT_PUBLIC_KEY) { pgp_write_xfer_pubkey(output, keydata, 1); } else { pgp_write_xfer_seckey(output, keydata, passphrase, strlen((char *)passphrase), 1); } cp = netpgp_strdup(pgp_mem_data(mem)); pgp_teardown_memory_write(output, mem); return cp; }
/* encrypt the contents of the input buffer, and return the mem structure */ pgp_memory_t * pgp_encrypt_buf(pgp_io_t *io, const void *input, const size_t insize, const pgp_key_t *pubkey, const unsigned use_armour, const char *cipher) { pgp_output_t *output; pgp_memory_t *outmem; __PGP_USED(io); if (input == NULL) { (void) fprintf(io->errs, "pgp_encrypt_buf: null memory\n"); return 0; } pgp_setup_memory_write(&output, &outmem, insize); /* set armoured/not armoured here */ if (use_armour) { pgp_writer_push_armor_msg(output); } /* Push the encrypted writer */ pgp_push_enc_se_ip(output, pubkey, cipher); /* This does the writing */ pgp_write(output, input, (unsigned)insize); /* tidy up */ pgp_writer_close(output); pgp_output_delete(output); return outmem; }