/** \ingroup HighLevel_KeyringRead \brief Reads a keyring from memory \param keyring Pointer to existing ops_keyring_t struct \param armour ops_true if file is armoured; else ops_false \param mem Pointer to a ops_memory_t struct containing keyring to be read \return ops true if OK; ops_false on error \note Keyring struct must already exist. \note Can be used with either a public or secret keyring. \note You must call ops_keyring_free() after usage to free alloc-ed memory. \note If you call this twice on the same keyring struct, without calling ops_keyring_free() between these calls, you will introduce a memory leak. \sa ops_keyring_read_from_file \sa ops_keyring_free Example code: \code ops_memory_t* mem; // Filled with keyring packets ops_keyring_t* keyring=ops_mallocz(sizeof *keyring); ops_boolean_t armoured=ops_false; ops_keyring_read_from_mem(keyring, armoured, mem); ... ops_keyring_free(keyring); free (keyring); \endcode */ ops_boolean_t ops_keyring_read_from_mem(ops_keyring_t *keyring, const ops_boolean_t armour, ops_memory_t* mem) { ops_parse_info_t *pinfo=NULL; ops_boolean_t res = ops_true; ops_setup_memory_read(&pinfo, mem, NULL, cb_keyring_read, OPS_ACCUMULATE_NO); ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 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); // don't call teardown_memory_read because memory was passed // in. But we need to free the parse_info object allocated by // ops_setup_memory_read(). ops_parse_info_delete(pinfo); return res; }
void check_sig_with_ops_core(ops_parse_info_t *pinfo, ops_boolean_t use_armour, validate_data_cb_arg_t *validate_arg) { ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); int rtn=0; memset(validate_arg, '\0', sizeof *validate_arg); validate_arg->result=result; validate_arg->keyring=&pub_keyring; validate_arg->rarg=ops_reader_get_arg_from_pinfo(pinfo); ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); pinfo->rinfo.accumulate=ops_true; // Set up armour/passphrase options if (use_armour) ops_reader_push_dearmour(pinfo); // Do the verification rtn=ops_parse_and_print_errors(pinfo); CU_ASSERT(rtn == 1); // Tidy up if (use_armour) ops_reader_pop_dearmour(pinfo); ops_parse_info_delete(pinfo); ops_validate_result_free(result); }
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=open(filename,O_RDONLY | O_BINARY); 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_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; }
ops_boolean_t ops_decrypt_memory(const unsigned char *encrypted_memory,int em_length, unsigned char **decrypted_memory,int *out_length, ops_keyring_t* keyring, const ops_boolean_t use_armour, 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 ops_memory_t *input_mem = ops_memory_new() ; ops_memory_add(input_mem,encrypted_memory,em_length) ; ops_setup_memory_read(&pinfo, input_mem, NULL, callback_write_parsed, ops_false); if (pinfo == NULL) { perror("cannot create memory read"); return ops_false; } // setup memory chunk ops_memory_t *output_mem; ops_setup_memory_write(&pinfo->cbinfo.cinfo, &output_mem,0) ; if (output_mem == NULL) { perror("Cannot create output memory"); ops_teardown_memory_read(pinfo, input_mem); return ops_false; } // \todo check for suffix matching armour param // 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); // copy output memory to supplied buffer. // *out_length = ops_memory_get_length(output_mem) ; *decrypted_memory = ops_mallocz(*out_length) ; memcpy(*decrypted_memory,ops_memory_get_data(output_mem),*out_length) ; ops_decrypt_memory_ABORT: ops_teardown_memory_write(pinfo->cbinfo.cinfo, output_mem); ops_teardown_memory_read(pinfo, input_mem); return res ; }