/** \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; }
/** \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; }
int main(int argc,char **argv) { const char *keyfile; const char *plainfile; const char *user_id; const char *hashstr; const char *sigfile; ops_secret_key_t *skey; ops_create_signature_t *sig; ops_hash_algorithm_t alg; int fd; ops_create_info_t *info; unsigned char keyid[OPS_KEY_ID_SIZE]; if(argc != 6) { fprintf(stderr,"%s <secret key file> <user_id> <hash> <plaintext file>" " <signature file>\n",argv[0]); exit(1); } keyfile=argv[1]; user_id=argv[2]; hashstr=argv[3]; plainfile=argv[4]; sigfile=argv[5]; ops_init(); skey=get_secret_key(keyfile); assert(skey); alg=ops_hash_algorithm_from_text(hashstr); if(alg == OPS_HASH_UNKNOWN) { fprintf(stderr,"Unkonwn hash algorithm: %s\n",hashstr); exit(2); } sig=ops_create_signature_new(); ops_signature_start_cleartext_signature(sig,skey,alg,OPS_SIG_BINARY); fd=open(plainfile,O_RDONLY); if(fd < 0) { perror(plainfile); exit(3); } for( ; ; ) { unsigned char buf[8192]; int n; n=read(fd,buf,sizeof buf); if(!n) break; if(n < 0) { perror(plainfile); exit(4); } ops_signature_add_data(sig,buf,n); } close(fd); ops_signature_add_creation_time(sig,time(NULL)); ops_keyid(keyid,&skey->public_key); ops_signature_add_issuer_key_id(sig,keyid); ops_signature_hashed_subpackets_end(sig); fd=open(sigfile,O_CREAT|O_TRUNC|O_WRONLY,0666); if(fd < 0) { perror(sigfile); exit(5); } info=ops_create_info_new(); ops_writer_set_fd(info,fd); ops_write_signature(sig,&skey->public_key,skey,info); ops_secret_key_free(skey); return 0; }