NeoMutt  2023-11-03-85-g512e01
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 3649 of file crypt_gpgme.c.

3650{
3651 gpgme_ctx_t context = NULL;
3652 gpgme_key_t export_keys[2] = { 0 };
3653 gpgme_data_t keydata = NULL;
3654 gpgme_error_t err;
3655 struct Body *att = NULL;
3656 char buf[1024] = { 0 };
3657
3658 OptPgpCheckTrust = false;
3659
3660 struct CryptKeyInfo *key = crypt_ask_for_key(_("Please enter the key ID: "), NULL,
3662 if (!key)
3663 goto bail;
3664 export_keys[0] = key->kobj;
3665 export_keys[1] = NULL;
3666
3667 context = create_gpgme_context(false);
3668 gpgme_set_armor(context, 1);
3669 keydata = create_gpgme_data();
3670 err = gpgme_op_export_keys(context, export_keys, 0, keydata);
3671 if (err != GPG_ERR_NO_ERROR)
3672 {
3673 mutt_error(_("Error exporting key: %s"), gpgme_strerror(err));
3674 goto bail;
3675 }
3676
3677 char *tempf = data_object_to_tempfile(keydata, NULL);
3678 if (!tempf)
3679 goto bail;
3680
3681 att = mutt_body_new();
3682 /* tempf is a newly allocated string, so this is correct: */
3683 att->filename = tempf;
3684 att->unlink = true;
3685 att->use_disp = false;
3686 att->type = TYPE_APPLICATION;
3687 att->subtype = mutt_str_dup("pgp-keys");
3688 /* L10N: MIME description for exported (attached) keys.
3689 You can translate this entry to a non-ASCII string (it will be encoded),
3690 but it may be safer to keep it untranslated. */
3691 snprintf(buf, sizeof(buf), _("PGP Key 0x%s"), crypt_keyid(key));
3692 att->description = mutt_str_dup(buf);
3694
3695 att->length = mutt_file_get_size(tempf);
3696
3697bail:
3698 crypt_key_free(&key);
3699 gpgme_data_release(keydata);
3700 gpgme_release(context);
3701
3702 return att;
3703}
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:399
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:360
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:560
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:3348
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:137
static void crypt_key_free(struct CryptKeyInfo **keylist)
Release all the keys in a list.
Definition: crypt_gpgme.c:252
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:1560
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition: globals.c:78
#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:251
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:91
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:127
void mutt_update_encoding(struct Body *b, struct ConfigSubset *sub)
Update the encoding type.
Definition: sendlib.c:420
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 240 of file pgpkey.c.

241{
242 struct Body *att = NULL;
243 char buf[1024] = { 0 };
244 char tmp[256] = { 0 };
245 struct stat st = { 0 };
246 pid_t pid;
247 OptPgpCheckTrust = false;
248 struct Buffer *tempf = NULL;
249
250 struct PgpKeyInfo *key = pgp_ask_for_key(_("Please enter the key ID: "), NULL,
252
253 if (!key)
254 return NULL;
255
256 snprintf(tmp, sizeof(tmp), "0x%s", pgp_fpr_or_lkeyid(pgp_principal_key(key)));
257 pgp_key_free(&key);
258
259 tempf = buf_pool_get();
260 buf_mktemp(tempf);
261 FILE *fp_tmp = mutt_file_fopen(buf_string(tempf), "w");
262 if (!fp_tmp)
263 {
264 mutt_perror(_("Can't create temporary file"));
265 goto cleanup;
266 }
267
268 FILE *fp_null = fopen("/dev/null", "w");
269 if (!fp_null)
270 {
271 mutt_perror(_("Can't open /dev/null"));
272 mutt_file_fclose(&fp_tmp);
273 unlink(buf_string(tempf));
274 goto cleanup;
275 }
276
277 mutt_message(_("Invoking PGP..."));
278
279 pid = pgp_invoke_export(NULL, NULL, NULL, -1, fileno(fp_tmp), fileno(fp_null), tmp);
280 if (pid == -1)
281 {
282 mutt_perror(_("Can't create filter"));
283 unlink(buf_string(tempf));
284 mutt_file_fclose(&fp_tmp);
285 mutt_file_fclose(&fp_null);
286 goto cleanup;
287 }
288
289 filter_wait(pid);
290
291 mutt_file_fclose(&fp_tmp);
292 mutt_file_fclose(&fp_null);
293
294 att = mutt_body_new();
295 att->filename = buf_strdup(tempf);
296 att->unlink = true;
297 att->use_disp = false;
298 att->type = TYPE_APPLICATION;
299 att->subtype = mutt_str_dup("pgp-keys");
300 snprintf(buf, sizeof(buf), _("PGP Key %s"), tmp);
301 att->description = mutt_str_dup(buf);
303
304 stat(buf_string(tempf), &st);
305 att->length = st.st_size;
306
307cleanup:
308 buf_pool_release(&tempf);
309 return att;
310}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:218
#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:235
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:492
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:180
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:89
@ PGP_PUBRING
Public keys.
Definition: pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:200
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:34
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: