text
stringlengths
0
357
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();
if (context==NULL || raw_key==NULL
|| raw_key->binary==NULL || raw_key->bytes <= 0
|| public_keys==NULL || private_keys==NULL || keysmem==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, raw_key->binary, raw_key->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem); /* function returns 0 on any error in any packet - this does not mean, we cannot use the key. We check the details below therefore. */
if (raw_key->type==DC_KEY_PUBLIC && public_keys->keyc >= 1) {
key_is_valid = 1;
}
else if (raw_key->type==DC_KEY_PRIVATE && private_keys->keyc >= 1) {
key_is_valid = 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 key_is_valid;
}
#endif // !DC_USE_RPGP
#ifdef DC_USE_RPGP
int dc_pgp_calc_fingerprint(const dc_key_t* raw_key, uint8_t** ret_fingerprint, size_t* ret_fingerprint_bytes) {
int success = 0;
rpgp_public_or_secret_key* key = NULL;
rpgp_cvec* fingerprint = NULL;
if (raw_key==NULL || ret_fingerprint==NULL || *ret_fingerprint!=NULL || ret_fingerprint_bytes==NULL || *ret_fingerprint_bytes!=0
|| raw_key->binary==NULL || raw_key->bytes <= 0) {
goto cleanup;
}
/* get the key into the right format */
key = rpgp_key_from_bytes(raw_key->binary, raw_key->bytes);
if (dc_pgp_handle_rpgp_error(NULL)) {
goto cleanup;
}
/* calc the fingerprint */
fingerprint = rpgp_key_fingerprint(key);
if (dc_pgp_handle_rpgp_error(NULL)) {
goto cleanup;
}
/* copy into the result */
*ret_fingerprint_bytes = rpgp_cvec_len(fingerprint);
*ret_fingerprint = malloc(*ret_fingerprint_bytes);
memcpy(*ret_fingerprint, rpgp_cvec_data(fingerprint), *ret_fingerprint_bytes);
success = 1;
cleanup:
if (key) { rpgp_key_drop(key); }
if (fingerprint) { rpgp_cvec_drop(fingerprint); }
return success;
}
#else // !DC_USE_RPGP
int dc_pgp_calc_fingerprint(const dc_key_t* raw_key, uint8_t** ret_fingerprint, size_t* ret_fingerprint_bytes)
{
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();
if (raw_key==NULL || ret_fingerprint==NULL || *ret_fingerprint!=NULL || ret_fingerprint_bytes==NULL || *ret_fingerprint_bytes!=0
|| raw_key->binary==NULL || raw_key->bytes <= 0
|| public_keys==NULL || private_keys==NULL || keysmem==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, raw_key->binary, raw_key->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem);
if (raw_key->type != DC_KEY_PUBLIC || public_keys->keyc <= 0) {
goto cleanup;
}
pgp_key_t* key0 = &public_keys->keys[0];
pgp_pubkey_t* pubkey0 = &key0->key.pubkey;
if (!pgp_fingerprint(&key0->pubkeyfpr, pubkey0, 0)) {
goto cleanup;
}
*ret_fingerprint_bytes = key0->pubkeyfpr.length;
*ret_fingerprint = malloc(*ret_fingerprint_bytes);