text
stringlengths
0
357
memcpy(*ret_fingerprint, key0->pubkeyfpr.fingerprint, *ret_fingerprint_bytes);
success = 1;
cleanup:
if (keysmem) { pgp_memory_free(keysmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
return success;
}
#endif // !DC_USE_RPGP
#ifdef DC_USE_RPGP
int dc_pgp_split_key(dc_context_t* context, const dc_key_t* private_in, dc_key_t* ret_public_key)
{
int success = 0;
rpgp_signed_secret_key* key = NULL;
rpgp_signed_public_key* pub_key = NULL;
rpgp_cvec* buf = NULL;
if (context==NULL || private_in==NULL || ret_public_key==NULL) {
goto cleanup;
}
if (private_in->type!=DC_KEY_PRIVATE) {
dc_log_warning(context, 0, "Split key: Given key is no private key.");
goto cleanup;
}
/* deserialize secret key */
key = rpgp_skey_from_bytes(private_in->binary, private_in->bytes);
if (dc_pgp_handle_rpgp_error(context)) {
goto cleanup;
}
/* convert to public key */
pub_key = rpgp_skey_public_key(key);
if (dc_pgp_handle_rpgp_error(context)) {
goto cleanup;
}
/* serialize public key */
buf = rpgp_pkey_to_bytes(pub_key);
if (dc_pgp_handle_rpgp_error(context)) {
goto cleanup;
}
/* create return value */
dc_key_set_from_binary(ret_public_key, rpgp_cvec_data(buf), rpgp_cvec_len(buf), DC_KEY_PUBLIC);
success = 1;
cleanup:
if (key) { rpgp_skey_drop(key); }
if (pub_key) { rpgp_pkey_drop(pub_key); }
if (buf) { rpgp_cvec_drop(buf); }
return success;
}
#else // !DC_USE_RPGP
int dc_pgp_split_key(dc_context_t* context, const dc_key_t* private_in, dc_key_t* ret_public_key)
{
int success = 0;
pgp_keyring_t* public_keys = calloc(1, sizeof(pgp_keyring_t));
pgp_keyring_t* private_keys = calloc(1, sizeof(pgp_keyring_t));
pgp_memory_t* keysmem = pgp_memory_new();
pgp_memory_t* pubmem = pgp_memory_new();
pgp_output_t* pubout = pgp_output_new();
if (context==NULL || private_in==NULL || ret_public_key==NULL
|| public_keys==NULL || private_keys==NULL || keysmem==NULL || pubmem==NULL || pubout==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, private_in->binary, private_in->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem);
if (private_in->type!=DC_KEY_PRIVATE || private_keys->keyc <= 0) {
dc_log_warning(context, 0, "Split key: Given key is no private key.");
goto cleanup;
}
if (public_keys->keyc <= 0) {
dc_log_warning(context, 0, "Split key: Given key does not contain a public key.");
goto cleanup;
}
pgp_writer_set_memory(pubout, pubmem);
if (!pgp_write_xfer_key(pubout, &public_keys->keys[0], 0/*armored*/)
|| pubmem->buf==NULL || pubmem->length <= 0) {
goto cleanup;
}
dc_key_set_from_binary(ret_public_key, pubmem->buf, pubmem->length, DC_KEY_PUBLIC);
success = 1;