pgp instrumentation

This commit is contained in:
Tony Olagbaiye 2021-07-22 06:53:36 +01:00
parent e13b46ce0f
commit 0322e7d20a
No known key found for this signature in database
GPG Key ID: 9E2FF3BDEBDFC910
4 changed files with 46 additions and 35 deletions

View File

@ -937,7 +937,7 @@ void channel__send_message(struct t_account *account, struct t_channel *channel,
xmpp_stanza_set_ns(message__x, "jabber:x:encrypted");
xmpp_stanza_t *message__x__text = xmpp_stanza_new(account->context);
char *ciphertext = pgp__encrypt(account->pgp, channel->pgp_id, body);
char *ciphertext = pgp__encrypt(channel->buffer, account->pgp, channel->pgp_id, body);
if (ciphertext)
xmpp_stanza_set_text(message__x__text, ciphertext);
free(ciphertext);

View File

@ -134,7 +134,7 @@ int connection__presence_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void
channel = channel__new(account, CHANNEL_TYPE_PM, from_bare, from_bare);
if (certificate)
{
channel->pgp_id = pgp__verify(account->pgp, certificate);
channel->pgp_id = pgp__verify(channel->buffer, account->pgp, certificate);
weechat_printf(channel->buffer, "[PGP]\t%sKey %s from %s",
weechat_color("gray"), channel->pgp_id, from);
}
@ -253,16 +253,6 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
"urn:xmpp:message-correct:0");
replace_id = replace ? xmpp_stanza_get_id(replace) : NULL;
x = xmpp_stanza_get_child_by_name_and_ns(stanza, "x", "jabber:x:encrypted");
intext = xmpp_stanza_get_text(body);
if (x)
{
char *ciphertext = xmpp_stanza_get_text(x);
cleartext = pgp__decrypt(account->pgp, ciphertext);
xmpp_free(account->context, ciphertext);
}
text = cleartext ? cleartext : intext;
const char *channel_id = weechat_strcasecmp(account_jid(account), from_bare)
== 0 ? to_bare : from_bare;
channel = channel__search(account, channel_id);
@ -271,6 +261,17 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
weechat_strcasecmp(type, "groupchat") == 0
? CHANNEL_TYPE_MUC : CHANNEL_TYPE_PM,
channel_id, channel_id);
x = xmpp_stanza_get_child_by_name_and_ns(stanza, "x", "jabber:x:encrypted");
intext = xmpp_stanza_get_text(body);
if (x)
{
char *ciphertext = xmpp_stanza_get_text(x);
cleartext = pgp__decrypt(channel->buffer, account->pgp, ciphertext);
xmpp_free(account->context, ciphertext);
}
text = cleartext ? cleartext : intext;
if (replace)
{
const char *orig = NULL;

50
pgp.c
View File

@ -60,7 +60,7 @@ void pgp__free(struct t_pgp *pgp)
}
}
char *pgp__encrypt(struct t_pgp *pgp, const char *target, const char *message)
char *pgp__encrypt(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *target, const char *message)
{
rnp_op_encrypt_t encrypt = NULL;
rnp_key_handle_t key = NULL;
@ -75,20 +75,20 @@ char *pgp__encrypt(struct t_pgp *pgp, const char *target, const char *message)
if ((ret = rnp_input_from_memory(&input, (uint8_t *)message, strlen(message), false)) !=
RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create input object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create input object: %s\n", reason);
goto encrypt_finish;
}
if ((ret = rnp_output_to_memory(&output, 0)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create output object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create output object: %s\n", reason);
goto encrypt_finish;
}
/* create encryption operation */
if ((ret = rnp_op_encrypt_create(&encrypt, pgp->context, input, output)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create encrypt operation: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create encrypt operation: %s\n", reason);
goto encrypt_finish;
}
@ -103,13 +103,13 @@ char *pgp__encrypt(struct t_pgp *pgp, const char *target, const char *message)
/* locate recipient's key and add it to the operation context. */
if ((ret = rnp_locate_key(pgp->context, "keyid", target, &key)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to locate recipient key: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to locate recipient key: %s\n", reason);
goto encrypt_finish;
}
if ((ret = rnp_op_encrypt_add_recipient(encrypt, key)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to add recipient: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to add recipient: %s\n", reason);
goto encrypt_finish;
}
rnp_key_handle_destroy(key);
@ -118,7 +118,7 @@ char *pgp__encrypt(struct t_pgp *pgp, const char *target, const char *message)
/* execute encryption operation */
if ((ret = rnp_op_encrypt_execute(encrypt)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: encryption failed: %s\n", reason);
weechat_printf(buffer, "[PGP]\tencryption failed: %s\n", reason);
goto encrypt_finish;
}
@ -138,7 +138,7 @@ encrypt_finish:
}
//"hQIMAzlgcSFDGLKEAQ//cGG3DFughC5xBF7xeXz1RdayOfhBAPfoZIq62MVuSnfS\nMfig65Zxz1LtAnnFq90TZY7hiHPBtVlYqg47AbSoYweMdpXsKgbUrd3NNf6k2nsZ\nUkChCtyGuHi8pTzclfle7gT0nNXJ1WcLCZ4ORZCrg3D5A+YTO9tdmE8GQsTT6TdV\nbbxF5yR4JF5SzFhuFL3ZoXPXrWylcwKXarYfoOTa6M2vSsCwApVIXQgJ/FI46sLT\nb0B/EVCjFvcvjkNr7+K7mQtth+x0a0pC4BtEhRvnIRAe/sdGp8NY+DP76clx4U+k\nIDG4H92F632pR6eEIoZttnBoaj0O4sTVAJCao5AoecR4w2FDqBWWtIyQp5vbo17/\nMtzungkk5vQP6Jhu36wa+JKpbHoxomVpHPZfAtIoyaY6pzQ0bUomIlSVpbZDvF68\nZKTlFd89Pm5x0JO5gsVYvf+N9Ed33d34n/0CFz5K5Tgu4Bk0v4LWEy3wtNsuQB4p\nkBSZJk7I2BakcRwP0zwld6rRHFIX1pb7zqThBPZGB9RkWPltiktUTibOII12tWhi\nksFpQJ8l1A8h9vM5kUXIeD6H2yP0CBUEIZF3Sf+jiSRZ/1/n3KoUrKEzkf/y4xgv\n1LA4pMjNLEr6J2fqGyYRFv4Bxv3PIvF17V5CwOtguxGRJHJXdIzm1BSHSqXxHezS\nYAFXMUb9fw3QX7Ed23KiyZjzd/LRsQBqMs9RsYyZB2PqF9x84lQYYbE8lErrryvK\nUEtmJKPw3Hvb7kgGox5vl5+KCg9q64EU9TgQpufYNShKtDz7Fsvc+ncgZoshDUeo\npw==\n=euIB"
char *pgp__decrypt(struct t_pgp *pgp, const char *ciphertext)
char *pgp__decrypt(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *ciphertext)
{
rnp_input_t input = NULL;
rnp_output_t output = NULL;
@ -156,19 +156,19 @@ char *pgp__decrypt(struct t_pgp *pgp, const char *ciphertext)
* message */
if ((ret = rnp_input_from_memory(&input, buf, buf_len, false)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create input object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create input object: %s\n", reason);
goto decrypt_finish;
}
if ((ret = rnp_output_to_memory(&output, 0)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create output object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create output object: %s\n", reason);
goto decrypt_finish;
}
if ((ret = rnp_decrypt(pgp->context, input, output)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: public-key decryption failed: %s\n", reason);
weechat_printf(buffer, "[PGP]\tpublic-key decryption failed: %s\n", reason);
goto decrypt_finish;
}
free(buf);
@ -185,7 +185,7 @@ decrypt_finish:
return result;
}
char *pgp__verify(struct t_pgp *pgp, const char *certificate)
char *pgp__verify(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *certificate)
{
rnp_op_verify_t verify = NULL;
rnp_input_t input = NULL;
@ -204,19 +204,19 @@ char *pgp__verify(struct t_pgp *pgp, const char *certificate)
/* create file input memory objects for the signed message and verified message */
if ((ret = rnp_input_from_memory(&input, buf, buf_len, false)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create input object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create input object: %s\n", reason);
goto verify_finish;
}
if ((ret = rnp_input_from_memory(&signature, buf, buf_len, false)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create input object: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create input object: %s\n", reason);
goto verify_finish;
}
if ((ret = rnp_op_verify_detached_create(&verify, pgp->context, input, signature)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to create verification context: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to create verification context: %s\n", reason);
goto verify_finish;
}
@ -226,14 +226,14 @@ char *pgp__verify(struct t_pgp *pgp, const char *certificate)
// ) != RNP_ERROR_SIGNATURE_INVALID)
// if (ret != RNP_ERROR_SIGNATURE_INVALID) {
// const char *reason = rnp_result_to_string(ret);
// weechat_printf(NULL, "pgp: failed to execute verification operation: %s\n", reason);
// weechat_printf(buffer, "[PGP]\tfailed to execute verification operation: %s\n", reason);
// goto verify_finish;
// }
/* now check signatures and get some info about them */
if ((ret = rnp_op_verify_get_signature_count(verify, &sigcount)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to get signature count: %s\n", reason);
weechat_printf(buffer, "[PGP]\tfailed to get signature count: %s\n", reason);
goto verify_finish;
}
@ -244,23 +244,33 @@ char *pgp__verify(struct t_pgp *pgp, const char *certificate)
if ((ret = rnp_op_verify_get_signature_at(verify, i, &sig)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to get signature %d: %s\n", (int)i, reason);
weechat_printf(buffer, "[PGP]\tfailed to get signature %d: %s\n", (int)i, reason);
goto verify_finish;
}
if ((ret = rnp_op_verify_signature_get_key(sig, &key)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to get signature's %d key: %s\n", (int)i, reason);
weechat_printf(buffer, "[PGP]\tfailed to get signature's %d key: %s\n", (int)i, reason);
goto verify_finish;
}
if ((ret = rnp_key_get_keyid(key, &keyid)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(NULL, "pgp: failed to get key id %d: %s\n", (int)i, reason);
weechat_printf(buffer, "[PGP]\tfailed to get key id %d: %s\n", (int)i, reason);
rnp_key_handle_destroy(key);
goto verify_finish;
}
rnp_signature_handle_t signature = NULL;
if ((ret = rnp_key_get_signature_at(key, 0, &signature)) == RNP_SUCCESS) {
if ((ret = rnp_signature_get_keyid(signature, &keyid)) != RNP_SUCCESS) {
const char *reason = rnp_result_to_string(ret);
weechat_printf(buffer, "[PGP]\tfailed to get key id: %s\n", reason);
rnp_key_handle_destroy(key);
goto verify_finish;
}
rnp_signature_handle_destroy(signature);
}
result = strdup(keyid);
rnp_buffer_destroy(keyid);

6
pgp.h
View File

@ -15,10 +15,10 @@ void pgp__init(struct t_pgp **pgp, const char *pub, const char *sec);
void pgp__free(struct t_pgp *pgp);
char *pgp__decrypt(struct t_pgp *pgp, const char *ciphertext);
char *pgp__decrypt(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *ciphertext);
char *pgp__encrypt(struct t_pgp *pgp, const char *target, const char *message);
char *pgp__encrypt(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *target, const char *message);
char *pgp__verify(struct t_pgp *pgp, const char *certificate);
char *pgp__verify(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *certificate);
#endif /*WEECHAT_XMPP_PGP_H*/