OX: Announce public key on PEP

src/pgp/gpg.c:p_ox_gpg_readkey

Used to read a public key from a file. The function will return the fingerprint
of the file and the base64 encoded key.

src/xmpp/ox.[hc]

ox_announce_public_key(const char* const filename) can be called from the /ox
announce <filename> command. The key within the file will be pushed on PEP and
the Metadata node will be set.

Issue: #1331
This commit is contained in:
DebXWoody
2020-07-05 09:28:51 +02:00
committed by Michael Vetter
parent 5e87b0dc51
commit 5a17957253
7 changed files with 396 additions and 2 deletions

View File

@@ -1190,4 +1190,107 @@ p_ox_gpg_decrypt(char* base64)
return result;
}
/*!
* \brief Read public key from file.
*
* This function is used the read a public key from a file.
*
* This function is used to read a key and push it on PEP. There are some checks
* in this function:
*
* Key is not
* - gkey->revoked
* - gkey->expired
* - gkey->disabled
* - gkey->invalid
* - gkey->secret
*
* Only one key in the file.
*
* \param filename filname to read the file.
* \param key result with base64 encode key or NULL
* \param fp result with the fingerprint or NULL
*
*/
void
p_ox_gpg_readkey(const char* const filename, char** key, char** fp){
log_info("Read OpenPGP Key from file %s", filename);
GError* error = NULL;
gchar* data = NULL;
gsize size = -1;
gboolean success = g_file_get_contents (filename,
&data,
&size,
&error);
if ( success ) {
setlocale (LC_ALL, "");
gpgme_check_version (NULL);
gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
gpgme_ctx_t ctx;
gpgme_error_t error = gpgme_new (&ctx);
if(GPG_ERR_NO_ERROR != error ) {
log_error("Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error));
return;
}
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
if( error != GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error));
return;
}
gpgme_set_armor(ctx,0);
gpgme_set_textmode(ctx,0);
gpgme_set_offline(ctx,1);
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
gpgme_data_t gpgme_data = NULL;
error = gpgme_data_new (&gpgme_data);
if ( error != GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: gpgme_data_new %s", gpgme_strerror(error));
return;
}
error = gpgme_data_new_from_mem(&gpgme_data, (char*)data, size,0);
if ( error != GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: gpgme_data_new_from_mem %s", gpgme_strerror(error));
return;
}
error = gpgme_op_keylist_from_data_start ( ctx, gpgme_data, 0);
if ( error != GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: gpgme_op_keylist_from_data_start %s", gpgme_strerror(error));
return;
}
gpgme_key_t gkey;
error = gpgme_op_keylist_next (ctx, &gkey);
if ( error != GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: gpgme_op_keylist_next %s", gpgme_strerror(error));
return;
}
gpgme_key_t end;
error = gpgme_op_keylist_next (ctx, &end);
if( error == GPG_ERR_NO_ERROR ) {
log_error("Read OpenPGP key from file: ambiguous key");
return;
}
if(gkey->revoked || gkey->expired || gkey->disabled || gkey->invalid || gkey->secret ) {
log_error("Read OpenPGP key from file: Key is not valid");
return;
}
gchar* keybase64 = g_base64_encode( (const guchar*) data, size );
*key = strdup(keybase64);
*fp = strdup(gkey->fpr);
} else {
log_error("Read OpenPGP key from file: Unable to read file: %s", error->message);
}
}

View File

@@ -76,6 +76,8 @@ char* p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const rec
char* p_ox_gpg_decrypt(char* base64);
void p_ox_gpg_readkey(const char* const filename, char** key, char** fp);
/*!
* \brief List of public keys with xmpp-URI.
*