/** \ingroup HighLevel_Keyring \brief Frees keydata and its memory \param keydata Key to be freed. \note This frees the keydata itself, as well as any other memory alloc-ed by it. */ void pgp_keydata_free(pgp_key_t *keydata) { unsigned n; for (n = 0; n < keydata->uidc; ++n) { pgp_userid_free(&keydata->uids[n]); } free(keydata->uids); keydata->uids = NULL; keydata->uidc = 0; for (n = 0; n < keydata->packetc; ++n) { pgp_subpacket_free(&keydata->packets[n]); } free(keydata->packets); keydata->packets = NULL; keydata->packetc = 0; if (keydata->type == PGP_PTAG_CT_PUBLIC_KEY) { pgp_pubkey_free(&keydata->key.pubkey); } else { pgp_seckey_free(&keydata->key.seckey); } free(keydata); }
/* sign a file, and put the signature in a separate file */ int pgp_sign_detached(pgp_io_t *io, const char *f, char *sigfile, pgp_seckey_t *seckey, const char *hash, const int64_t from, const uint64_t duration, const unsigned armored, const unsigned overwrite) { pgp_create_sig_t *sig; pgp_hash_alg_t hash_alg; pgp_output_t *output; pgp_memory_t *mem; uint8_t keyid[PGP_KEY_ID_SIZE]; int fd; /* find out which hash algorithm to use */ hash_alg = pgp_str_to_hash_alg(hash); if (hash_alg == PGP_HASH_UNKNOWN) { (void) fprintf(io->errs,"Unknown hash algorithm: %s\n", hash); return 0; } /* setup output file */ fd = open_output_file(&output, f, sigfile, (armored) ? "asc" : "sig", overwrite); if (fd < 0) { (void) fprintf(io->errs,"Can't open output file: %s\n", f); return 0; } /* create a new signature */ sig = pgp_create_sig_new(); pgp_start_sig(sig, seckey, hash_alg, PGP_SIG_BINARY); /* read the contents of 'f', and add that to the signature */ mem = pgp_memory_new(); if (!pgp_mem_readfile(mem, f)) { pgp_teardown_file_write(output, fd); return 0; } /* set armoured/not armoured here */ if (armored) { pgp_writer_push_armor_msg(output); } pgp_sig_add_data(sig, pgp_mem_data(mem), pgp_mem_len(mem)); pgp_memory_free(mem); /* calculate the signature */ pgp_add_time(sig, from, "birth"); pgp_add_time(sig, (int64_t)duration, "expiration"); pgp_keyid(keyid, sizeof(keyid), &seckey->pubkey, hash_alg); pgp_add_issuer_keyid(sig, keyid); pgp_end_hashed_subpkts(sig); pgp_write_sig(output, sig, &seckey->pubkey, seckey); pgp_teardown_file_write(output, fd); pgp_seckey_free(seckey); return 1; }