NeoMutt  2022-04-29-323-g5fcc6c
Teaching an old dog new tricks
DOXYGEN
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)
 Implements CryptModuleSpecs::pgp_make_key_attachment() -. More...
 
struct Bodypgp_class_make_key_attachment (void)
 Implements CryptModuleSpecs::pgp_make_key_attachment() -. More...
 

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  )

Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 3626 of file crypt_gpgme.c.

3627{
3628 gpgme_ctx_t context = NULL;
3629 gpgme_key_t export_keys[2] = { 0 };
3630 gpgme_data_t keydata = NULL;
3631 gpgme_error_t err;
3632 struct Body *att = NULL;
3633 char buf[1024] = { 0 };
3634
3635 OptPgpCheckTrust = false;
3636
3637 struct CryptKeyInfo *key = crypt_ask_for_key(_("Please enter the key ID: "), NULL,
3639 if (!key)
3640 goto bail;
3641 export_keys[0] = key->kobj;
3642 export_keys[1] = NULL;
3643
3644 context = create_gpgme_context(false);
3645 gpgme_set_armor(context, 1);
3646 keydata = create_gpgme_data();
3647 err = gpgme_op_export_keys(context, export_keys, 0, keydata);
3648 if (err != GPG_ERR_NO_ERROR)
3649 {
3650 mutt_error(_("Error exporting key: %s"), gpgme_strerror(err));
3651 goto bail;
3652 }
3653
3654 char *tempf = data_object_to_tempfile(keydata, NULL);
3655 if (!tempf)
3656 goto bail;
3657
3658 att = mutt_body_new();
3659 /* tempf is a newly allocated string, so this is correct: */
3660 att->filename = tempf;
3661 att->unlink = true;
3662 att->use_disp = false;
3663 att->type = TYPE_APPLICATION;
3664 att->subtype = mutt_str_dup("pgp-keys");
3665 /* L10N: MIME description for exported (attached) keys.
3666 You can translate this entry to a non-ASCII string (it will be encoded),
3667 but it may be safer to keep it untranslated. */
3668 snprintf(buf, sizeof(buf), _("PGP Key 0x%s"), crypt_keyid(key));
3669 att->description = mutt_str_dup(buf);
3671
3672 att->length = mutt_file_get_size(tempf);
3673
3674bail:
3675 crypt_key_free(&key);
3676 gpgme_data_release(keydata);
3677 gpgme_release(context);
3678
3679 return att;
3680}
static struct CryptKeyInfo * crypt_ask_for_key(char *tag, char *whatfor, KeyFlags abilities, unsigned int app, bool *forced_valid)
Ask the user for a key.
Definition: crypt_gpgme.c:3330
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:397
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:358
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:556
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:134
static void crypt_key_free(struct CryptKeyInfo **keylist)
Release all the keys in a list.
Definition: crypt_gpgme.c:249
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:43
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1567
#define mutt_error(...)
Definition: logging.h:87
@ 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:250
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:126
bool OptPgpCheckTrust
(pseudo) used by dlg_select_pgp_key()
Definition: options.h:54
void mutt_update_encoding(struct Body *a, struct ConfigSubset *sub)
Update the encoding type.
Definition: sendlib.c:413
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:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:

◆ pgp_class_make_key_attachment()

struct Body * pgp_class_make_key_attachment ( void  )

Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 237 of file pgpkey.c.

238{
239 struct Body *att = NULL;
240 char buf[1024] = { 0 };
241 char tmp[256] = { 0 };
242 struct stat st = { 0 };
243 pid_t pid;
244 OptPgpCheckTrust = false;
245 struct Buffer *tempf = NULL;
246
247 struct PgpKeyInfo *key = pgp_ask_for_key(_("Please enter the key ID: "), NULL,
249
250 if (!key)
251 return NULL;
252
253 snprintf(tmp, sizeof(tmp), "0x%s", pgp_fpr_or_lkeyid(pgp_principal_key(key)));
254 pgp_key_free(&key);
255
256 tempf = mutt_buffer_pool_get();
257 mutt_buffer_mktemp(tempf);
258 FILE *fp_tmp = mutt_file_fopen(mutt_buffer_string(tempf), "w");
259 if (!fp_tmp)
260 {
261 mutt_perror(_("Can't create temporary file"));
262 goto cleanup;
263 }
264
265 FILE *fp_null = fopen("/dev/null", "w");
266 if (!fp_null)
267 {
268 mutt_perror(_("Can't open /dev/null"));
269 mutt_file_fclose(&fp_tmp);
270 unlink(mutt_buffer_string(tempf));
271 goto cleanup;
272 }
273
274 mutt_message(_("Invoking PGP..."));
275
276 pid = pgp_invoke_export(NULL, NULL, NULL, -1, fileno(fp_tmp), fileno(fp_null), tmp);
277 if (pid == -1)
278 {
279 mutt_perror(_("Can't create filter"));
280 unlink(mutt_buffer_string(tempf));
281 mutt_file_fclose(&fp_tmp);
282 mutt_file_fclose(&fp_null);
283 goto cleanup;
284 }
285
286 filter_wait(pid);
287
288 mutt_file_fclose(&fp_tmp);
289 mutt_file_fclose(&fp_null);
290
291 att = mutt_body_new();
292 att->filename = mutt_buffer_strdup(tempf);
293 att->unlink = true;
294 att->use_disp = false;
295 att->type = TYPE_APPLICATION;
296 att->subtype = mutt_str_dup("pgp-keys");
297 snprintf(buf, sizeof(buf), _("PGP Key %s"), tmp);
298 att->description = mutt_str_dup(buf);
300
301 stat(mutt_buffer_string(tempf), &st);
302 att->length = st.st_size;
303
304cleanup:
306 return att;
307}
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:447
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:151
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
#define mutt_message(...)
Definition: logging.h:86
#define mutt_perror(...)
Definition: logging.h:88
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
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:495
struct PgpKeyInfo * pgp_ask_for_key(char *tag, char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition: pgpkey.c:179
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:88
@ PGP_PUBRING
Public keys.
Definition: pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:199
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
String manipulation buffer.
Definition: buffer.h:34
Information about a PGP key.
Definition: pgplib.h:47
+ Here is the call graph for this function: