NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_make_key_attachment()

Generate a public key attachment. More...

+ Collaboration diagram for pgp_make_key_attachment():

Functions

struct Bodypgp_gpgme_make_key_attachment (void)
 Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.
 
struct Bodypgp_class_make_key_attachment (void)
 Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.
 

Detailed Description

Generate a public key attachment.

Return values
ptrNew Body containing the attachment
NULLError

Function Documentation

◆ pgp_gpgme_make_key_attachment()

struct Body * pgp_gpgme_make_key_attachment ( void  )

Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 3724 of file crypt_gpgme.c.

3725{
3726 gpgme_ctx_t ctx = NULL;
3727 gpgme_key_t export_keys[2] = { 0 };
3728 gpgme_data_t keydata = NULL;
3729 struct Body *att = NULL;
3730 char buf[1024] = { 0 };
3731
3732 OptPgpCheckTrust = false;
3733
3734 struct CryptKeyInfo *key = crypt_ask_for_key(_("Please enter the key ID: "), NULL,
3736 if (!key)
3737 goto bail;
3738 export_keys[0] = key->kobj;
3739 export_keys[1] = NULL;
3740
3741 ctx = create_gpgme_context(false);
3742 gpgme_set_armor(ctx, 1);
3743 keydata = create_gpgme_data();
3744 gpgme_error_t err = gpgme_op_export_keys(ctx, export_keys, 0, keydata);
3745 if (err != GPG_ERR_NO_ERROR)
3746 {
3747 mutt_error(_("Error exporting key: %s"), gpgme_strerror(err));
3748 goto bail;
3749 }
3750
3751 char *tempf = data_object_to_tempfile(keydata, NULL);
3752 if (!tempf)
3753 goto bail;
3754
3755 att = mutt_body_new();
3756 /* tempf is a newly allocated string, so this is correct: */
3757 att->filename = tempf;
3758 att->unlink = true;
3759 att->use_disp = false;
3760 att->type = TYPE_APPLICATION;
3761 att->subtype = mutt_str_dup("pgp-keys");
3762 /* L10N: MIME description for exported (attached) keys.
3763 You can translate this entry to a non-ASCII string (it will be encoded),
3764 but it may be safer to keep it untranslated. */
3765 snprintf(buf, sizeof(buf), _("PGP Key 0x%s"), crypt_keyid(key));
3766 att->description = mutt_str_dup(buf);
3768
3769 att->length = mutt_file_get_size(tempf);
3770
3771bail:
3772 crypt_key_free(&key);
3773 gpgme_data_release(keydata);
3774 gpgme_release(ctx);
3775
3776 return att;
3777}
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:400
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:361
static char * data_object_to_tempfile(gpgme_data_t data, FILE **fp_ret)
Copy a data object to a temporary file.
Definition: crypt_gpgme.c:563
static struct CryptKeyInfo * crypt_ask_for_key(const char *tag, const char *whatfor, KeyFlags abilities, unsigned int app, bool *forced_valid)
Ask the user for a key.
Definition: crypt_gpgme.c:3423
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:138
static void crypt_key_free(struct CryptKeyInfo **keylist)
Release all the keys in a list.
Definition: crypt_gpgme.c:253
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:44
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1516
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition: globals.c:73
#define mutt_error(...)
Definition: logging2.h:92
@ TYPE_APPLICATION
Type: 'application/*'.
Definition: mime.h:33
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:126
void mutt_update_encoding(struct Body *b, struct ConfigSubset *sub)
Update the encoding type.
Definition: sendlib.c:421
The body of an email.
Definition: body.h:36
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:67
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
bool use_disp
Content-Disposition uses filename= ?
Definition: body.h:47
char * description
content-description
Definition: body.h:55
char * subtype
content-type subtype
Definition: body.h:60
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition: body.h:58
A stored PGP key.
Definition: crypt_gpgme.h:44
gpgme_key_t kobj
Definition: crypt_gpgme.h:46
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ Here is the call graph for this function:

◆ pgp_class_make_key_attachment()

struct Body * pgp_class_make_key_attachment ( void  )

Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 258 of file pgpkey.c.

259{
260 struct Body *att = NULL;
261 char buf[1024] = { 0 };
262 char tmp[256] = { 0 };
263 struct stat st = { 0 };
264 pid_t pid;
265 OptPgpCheckTrust = false;
266 struct Buffer *tempf = NULL;
267
268 struct PgpKeyInfo *key = pgp_ask_for_key(_("Please enter the key ID: "), NULL,
270
271 if (!key)
272 return NULL;
273
274 snprintf(tmp, sizeof(tmp), "0x%s", pgp_fpr_or_lkeyid(pgp_principal_key(key)));
275 pgp_key_free(&key);
276
277 tempf = buf_pool_get();
278 buf_mktemp(tempf);
279 FILE *fp_tmp = mutt_file_fopen(buf_string(tempf), "w");
280 if (!fp_tmp)
281 {
282 mutt_perror(_("Can't create temporary file"));
283 goto cleanup;
284 }
285
286 FILE *fp_null = mutt_file_fopen("/dev/null", "w");
287 if (!fp_null)
288 {
289 mutt_perror(_("Can't open /dev/null"));
290 mutt_file_fclose(&fp_tmp);
291 unlink(buf_string(tempf));
292 goto cleanup;
293 }
294
295 mutt_message(_("Invoking PGP..."));
296
297 pid = pgp_invoke_export(NULL, NULL, NULL, -1, fileno(fp_tmp), fileno(fp_null), tmp);
298 if (pid == -1)
299 {
300 mutt_perror(_("Can't create filter"));
301 unlink(buf_string(tempf));
302 mutt_file_fclose(&fp_tmp);
303 mutt_file_fclose(&fp_null);
304 goto cleanup;
305 }
306
307 filter_wait(pid);
308
309 mutt_file_fclose(&fp_tmp);
310 mutt_file_fclose(&fp_null);
311
312 att = mutt_body_new();
313 att->filename = buf_strdup(tempf);
314 att->unlink = true;
315 att->use_disp = false;
316 att->type = TYPE_APPLICATION;
317 att->subtype = mutt_str_dup("pgp-keys");
318 snprintf(buf, sizeof(buf), _("PGP Key %s"), tmp);
319 att->description = mutt_str_dup(buf);
321
322 stat(buf_string(tempf), &st);
323 att->length = st.st_size;
324
325cleanup:
326 buf_pool_release(&tempf);
327 return att;
328}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:570
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_perror(...)
Definition: logging2.h:93
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:234
pid_t pgp_invoke_export(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
Use PGP to export a key from the user's keyring.
Definition: pgpinvoke.c:442
struct PgpKeyInfo * pgp_ask_for_key(char *tag, const char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition: pgpkey.c:198
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:91
@ PGP_PUBRING
Public keys.
Definition: pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:201
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:36
Information about a PGP key.
Definition: pgplib.h:47
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function: