ops_boolean_t ops_write_keyring_to_file(const ops_keyring_t *keyring,ops_boolean_t armoured,const char *filename,ops_boolean_t write_all_packets) { ops_create_info_t *info; int fd = ops_setup_file_write(&info, filename, ops_true); if (fd < 0) { fprintf(stderr,"ops_write_keyring(): ERROR: Cannot write to %s\n",filename ) ; return ops_false ; } int i; for(i=0;i<keyring->nkeys;++i) // if(keyring->keys[i].key.pkey.algorithm == OPS_PKA_RSA) if(write_all_packets) ops_write_transferable_public_key_from_packet_data(&keyring->keys[i],armoured,info) ; else ops_write_transferable_public_key(&keyring->keys[i],armoured,info) ; // else // { // fprintf(stdout, "ops_write_keyring: not writing key. Algorithm not handled: ") ; // ops_print_public_keydata(&keyring->keys[i]); // fprintf(stdout, "\n") ; // } ops_teardown_file_write(info, fd); return ops_true ; }
/** \ingroup HighLevel_Crypto Encrypt a file \param input_filename Name of file to be encrypted \param output_filename Name of file to write to. If NULL, name is constructed from input_filename \param pub_key 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 ops_true if OK; else ops_false */ ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite) { int fd_in=0; int fd_out=0; ops_create_info_t *cinfo; #ifdef WIN32 fd_in=ops_open(input_filename, O_RDONLY | O_BINARY, 0); #else fd_in=ops_open(input_filename, O_RDONLY, 0); #endif if(fd_in < 0) { perror(input_filename); return ops_false; } fd_out=ops_setup_file_write(&cinfo, output_filename, allow_overwrite); if (fd_out < 0) return ops_false; // set armoured/not armoured here if (use_armour) ops_writer_push_armoured_message(cinfo); // Push the encrypted writer ops_writer_push_stream_encrypt_se_ip(cinfo, pub_key); ops_writer_push_literal(cinfo); // Do the writing unsigned buffer[10240]; for (;;) { int n=0; n=read(fd_in, buffer, sizeof buffer); if (!n) break; if(n < 0) return ops_false ; // FIXME: apparently writing can't fail. ops_write(buffer, n, cinfo); } // tidy up close(fd_in); ops_teardown_file_write(cinfo, fd_out); return ops_true; }
/** \ingroup Core_Writers \brief As ops_teardown_file_write() */ void ops_teardown_file_append(ops_create_info_t *cinfo, int fd) { ops_teardown_file_write(cinfo,fd); }
ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t* keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite, ops_parse_cb_t* cb_get_passphrase) { int fd_in=0; int fd_out=0; char* myfilename=NULL; // ops_parse_info_t *pinfo=NULL; // setup for reading from given input file fd_in=ops_setup_file_read(&pinfo, input_filename, NULL, callback_write_parsed, ops_false); if (fd_in < 0) { perror(input_filename); return ops_false; } // setup output filename if (output_filename) { fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, output_filename, allow_overwrite); if (fd_out < 0) { perror(output_filename); ops_teardown_file_read(pinfo, fd_in); return ops_false; } } else { int suffixlen=4; char *defaultsuffix=".decrypted"; const char *suffix=input_filename+strlen(input_filename)-suffixlen; if (!strcmp(suffix, ".gpg") || !strcmp(suffix, ".asc")) { myfilename=ops_mallocz(strlen(input_filename)-suffixlen+1); strncpy(myfilename, input_filename, strlen(input_filename)-suffixlen); } else { unsigned filenamelen=strlen(input_filename)+strlen(defaultsuffix)+1; myfilename=ops_mallocz(filenamelen); snprintf(myfilename, filenamelen, "%s%s", input_filename, defaultsuffix); } fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, myfilename, allow_overwrite); if (fd_out < 0) { perror(myfilename); free(myfilename); ops_teardown_file_read(pinfo, fd_in); return ops_false; } free (myfilename); } // \todo check for suffix matching armour param // setup for writing decrypted contents to given output file // setup keyring and passphrase callback pinfo->cbinfo.cryptinfo.keyring=keyring; pinfo->cbinfo.cryptinfo.cb_get_passphrase=cb_get_passphrase; // Set up armour/passphrase options if (use_armour) ops_reader_push_dearmour(pinfo); // Do it ops_boolean_t res = ops_parse_and_print_errors(pinfo); // Unsetup if (use_armour) ops_reader_pop_dearmour(pinfo); ops_teardown_file_write(pinfo->cbinfo.cinfo, fd_out); ops_teardown_file_read(pinfo, fd_in); // \todo cleardown crypt return res; }