/** \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; }
ops_memory_t* ops_write_mem_from_file(const char *filename, int* errnum) { size_t initial_size=1024; int fd=0; unsigned char buf[1024]; ops_memory_t* mem=NULL; *errnum=0; fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) { *errnum=errno; return ops_false; } mem=ops_memory_new(); ops_memory_init(mem,initial_size); for (;;) { ssize_t n=0; n=read(fd,buf,1024); if (n<0) { *errnum=errno; break; } if (!n) break; ops_memory_add(mem, &buf[0], n); } close(fd); return mem; }
int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len, const ops_boolean_t overwrite) { int fd=0; size_t n=0; int flags=0; flags=O_WRONLY | O_CREAT; if (overwrite==ops_true) flags |= O_TRUNC; else flags |= O_EXCL; flags |= O_BINARY; fd=ops_open(filename,flags, 0600); if (fd < 0) { perror(NULL); return 0; } n=write(fd,buf,len); if (n!=len) return 0; if(!close(fd)) return 1; return 0; }
ops_boolean_t ops_keyring_read_from_file(ops_keyring_t *keyring, const ops_boolean_t armour, const char *filename) { ops_parse_info_t *pinfo; int fd; ops_boolean_t res = ops_true; pinfo=ops_parse_info_new(); // add this for the moment, // \todo need to fix the problems with reading signature subpackets later // ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_RAW); ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if(fd < 0) { ops_parse_info_delete(pinfo); perror(filename); return ops_false; } ops_reader_set_fd(pinfo,fd); ops_parse_cb_set(pinfo,cb_keyring_read,NULL); if (armour) { ops_reader_push_dearmour(pinfo); } if ( ops_parse_and_accumulate(keyring,pinfo) == 0 ) { res = ops_false; } else { res = ops_true; } ops_print_errors(ops_parse_info_get_errors(pinfo)); if (armour) ops_reader_pop_dearmour(pinfo); close(fd); ops_parse_info_delete(pinfo); return res; }
ops_boolean_t ops_write_literal_data_from_file(const char *filename, const ops_literal_data_type_t type, ops_create_info_t *info) { size_t initial_size=1024; int fd=0; ops_boolean_t rtn; unsigned char buf[1024]; ops_memory_t* mem=NULL; size_t len=0; fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) return ops_false; mem=ops_memory_new(); ops_memory_init(mem,initial_size); for (;;) { ssize_t n=0; n=read(fd,buf,1024); if (!n) break; if (n == -1) { close(fd); ops_memory_free(mem); return ops_false; } ops_memory_add(mem, &buf[0], n); } close(fd); // \todo do we need to check text data for <cr><lf> line endings ? len=ops_memory_get_length(mem); rtn=ops_write_ptag(OPS_PTAG_CT_LITERAL_DATA, info) && ops_write_length(1+1+4+len,info) && ops_write_scalar(type, 1, info) && ops_write_scalar(0, 1, info) // filename && ops_write_scalar(0, 4, info) // date && ops_write(ops_memory_get_data(mem), len, info); ops_memory_free(mem); return rtn; }