/** \ingroup Core_Keys \brief Add selfsigned User ID to key \param keydata Key to which to add user ID \param userid Self-signed User ID to add \return ops_true if OK; else ops_false */ ops_boolean_t ops_add_selfsigned_userid_to_keydata(ops_keydata_t* keydata, ops_user_id_t* userid) { ops_packet_t sigpacket; ops_memory_t* mem_userid=NULL; ops_create_info_t* cinfo_userid=NULL; ops_memory_t* mem_sig=NULL; ops_create_info_t* cinfo_sig=NULL; ops_create_signature_t *sig=NULL; /* * create signature packet for this userid */ // create userid pkt ops_setup_memory_write(&cinfo_userid, &mem_userid, 128); ops_write_struct_user_id(userid, cinfo_userid); // create sig for this pkt sig=ops_create_signature_new(); ops_signature_start_key_signature(sig, &keydata->key.skey.public_key, userid, OPS_CERT_POSITIVE); ops_signature_add_creation_time(sig,time(NULL)); ops_signature_add_issuer_key_id(sig,keydata->key_id); ops_signature_add_primary_user_id(sig, ops_true); ops_signature_hashed_subpackets_end(sig); ops_setup_memory_write(&cinfo_sig, &mem_sig, 128); ops_write_signature(sig,&keydata->key.skey.public_key,&keydata->key.skey, cinfo_sig); // add this packet to keydata sigpacket.length=ops_memory_get_length(mem_sig); sigpacket.raw=ops_memory_get_data(mem_sig); // add userid to keydata ops_add_signed_userid_to_keydata(keydata, userid, &sigpacket); // cleanup ops_create_signature_delete(sig); ops_create_info_delete(cinfo_userid); ops_create_info_delete(cinfo_sig); ops_memory_free(mem_userid); ops_memory_free(mem_sig); return ops_true; }
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; }
/** \ingroup Core_Readers \brief Create parse_info and sets to read from memory \param pinfo Address where new parse_info will be set \param mem Memory to read from \param arg Reader-specific arg \param callback Callback to use with reader \param accumulate Set if we need to accumulate as we read. (Usually false unless doing signature verification) \note It is the caller's responsiblity to free parse_info \sa ops_teardown_memory_read() */ void ops_setup_memory_read(ops_parse_info_t **pinfo, ops_memory_t *mem, void* arg, ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *), ops_boolean_t accumulate) { /* * initialise needed uctures for reading */ *pinfo=ops_parse_info_new(); ops_parse_cb_set(*pinfo,callback,arg); ops_reader_set_memory(*pinfo, ops_memory_get_data(mem), ops_memory_get_length(mem)); if (accumulate) (*pinfo)->rinfo.accumulate=ops_true; }
/** \ingroup Core_Keys \brief Add signature to given key \return ops_true if OK; else ops_false */ ops_boolean_t ops_sign_key(ops_keydata_t* keydata, const unsigned char *signers_key_id,ops_secret_key_t *signers_key) { /* ops_memory_t* mem_userid=NULL; */ ops_create_info_t* cinfo_userid=NULL; ops_memory_t* mem_sig=NULL; ops_create_info_t* cinfo_sig=NULL; ops_create_signature_t *sig=NULL; /* * create signature packet for this userid */ // create sig for this pkt sig=ops_create_signature_new(); ops_signature_start_key_signature(sig, &keydata->key.skey.public_key, &keydata->uids[0], OPS_CERT_GENERIC); ops_signature_add_creation_time(sig,time(NULL)); ops_signature_add_issuer_key_id(sig,signers_key_id); ops_signature_hashed_subpackets_end(sig); ops_setup_memory_write(&cinfo_sig, &mem_sig, 128); ops_write_signature(sig,&signers_key->public_key,signers_key, cinfo_sig); // add this packet to keydata ops_packet_t sigpacket; sigpacket.length=ops_memory_get_length(mem_sig); sigpacket.raw=ops_memory_get_data(mem_sig); // add userid to keydata ops_add_packet_to_keydata(keydata, &sigpacket); // cleanup ops_create_signature_delete(sig); ops_create_info_delete(cinfo_sig); ops_memory_free(mem_sig); return ops_true; }
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 ; }