NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
expando_gpgme.c
Go to the documentation of this file.
1
22
28
29#include <gpgme.h>
30#include <stdbool.h>
31#include <time.h>
32#include "private.h"
33#include "mutt/lib.h"
34#include "core/lib.h"
35#include "expando_gpgme.h"
36#include "lib.h"
37#include "expando/lib.h"
38#include "crypt_gpgme.h"
39#include "pgplib.h"
40
48static char *crypt_key_abilities(KeyFlags flags)
49{
50 static char buf[3];
51
52 if (!(flags & KEYFLAG_CANENCRYPT))
53 buf[0] = '-';
54 else if (flags & KEYFLAG_PREFER_SIGNING)
55 buf[0] = '.';
56 else
57 buf[0] = 'e';
58
59 if (!(flags & KEYFLAG_CANSIGN))
60 buf[1] = '-';
61 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
62 buf[1] = '.';
63 else
64 buf[1] = 's';
65
66 buf[2] = '\0';
67
68 return buf;
69}
70
78static char *crypt_flags(KeyFlags flags)
79{
80 if (flags & KEYFLAG_REVOKED)
81 return "R";
82 if (flags & KEYFLAG_EXPIRED)
83 return "X";
84 if (flags & KEYFLAG_DISABLED)
85 return "d";
86 if (flags & KEYFLAG_CRITICAL)
87 return "c";
88
89 return " ";
90}
91
95static long gpgme_entry_number_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
96{
97 const struct CryptEntry *entry = data;
98 return entry->num;
99}
100
104static void gpgme_entry_trust(const struct ExpandoNode *node, void *data,
105 MuttFormatFlags flags, struct Buffer *buf)
106{
107 const struct CryptEntry *entry = data;
108 const struct CryptKeyInfo *key = entry->key;
109
110 const char *s = "";
111 if ((key->flags & KEYFLAG_ISX509))
112 {
113 s = "x";
114 }
115 else
116 {
117 switch (key->validity)
118 {
119 case GPGME_VALIDITY_FULL:
120 s = "f";
121 break;
122 case GPGME_VALIDITY_MARGINAL:
123 s = "m";
124 break;
125 case GPGME_VALIDITY_NEVER:
126 s = "n";
127 break;
128 case GPGME_VALIDITY_ULTIMATE:
129 s = "u";
130 break;
131 case GPGME_VALIDITY_UNDEFINED:
132 s = "q";
133 break;
134 case GPGME_VALIDITY_UNKNOWN:
135 default:
136 s = "?";
137 break;
138 }
139 }
140
141 buf_strcpy(buf, s);
142}
143
147static void gpgme_entry_user_id(const struct ExpandoNode *node, void *data,
148 MuttFormatFlags flags, struct Buffer *buf)
149{
150 const struct CryptEntry *entry = data;
151 const struct CryptKeyInfo *key = entry->key;
152
153 const char *s = key->uid;
154 buf_strcpy(buf, s);
155}
156
160static void gpgme_key_algorithm(const struct ExpandoNode *node, void *data,
161 MuttFormatFlags flags, struct Buffer *buf)
162{
163 const struct CryptEntry *entry = data;
164 const struct CryptKeyInfo *key = entry->key;
165
166 const char *s = NULL;
167 if (key->kobj->subkeys)
168 s = gpgme_pubkey_algo_name(key->kobj->subkeys->pubkey_algo);
169 else
170 s = "?";
171
172 buf_strcpy(buf, s);
173}
174
178static void gpgme_key_capabilities(const struct ExpandoNode *node, void *data,
179 MuttFormatFlags flags, struct Buffer *buf)
180{
181 const struct CryptEntry *entry = data;
182 const struct CryptKeyInfo *key = entry->key;
183
184 const char *s = crypt_key_abilities(key->flags);
185 buf_strcpy(buf, s);
186}
187
191static void gpgme_key_date(const struct ExpandoNode *node, void *data,
192 MuttFormatFlags flags, struct Buffer *buf)
193{
194 const struct CryptEntry *entry = data;
195 const struct CryptKeyInfo *key = entry->key;
196
197 const char *text = node->text;
198 bool use_c_locale = false;
199 if (*text == '!')
200 {
201 use_c_locale = true;
202 text++;
203 }
204
205 struct tm tm = { 0 };
206 if (key->kobj->subkeys && (key->kobj->subkeys->timestamp > 0))
207 {
208 tm = mutt_date_localtime(key->kobj->subkeys->timestamp);
209 }
210 else
211 {
212 tm = mutt_date_localtime(0); // Default to 1970-01-01
213 }
214
215 char tmp[128] = { 0 };
216 if (use_c_locale)
217 {
218 strftime_l(tmp, sizeof(tmp), text, &tm, NeoMutt->time_c_locale);
219 }
220 else
221 {
222 strftime(tmp, sizeof(tmp), text, &tm);
223 }
224
225 buf_strcpy(buf, tmp);
226}
227
231static long gpgme_key_date_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
232{
233 const struct CryptEntry *entry = data;
234 const struct CryptKeyInfo *key = entry->key;
235 return key->kobj->subkeys->timestamp;
236}
237
241static void gpgme_key_fingerprint(const struct ExpandoNode *node, void *data,
242 MuttFormatFlags flags, struct Buffer *buf)
243{
244 const struct CryptEntry *entry = data;
245 struct CryptKeyInfo *key = entry->key;
246
247 /* fixme: we need a way to distinguish between main and subkeys.
248 * Store the idx in entry? */
249 const char *s = crypt_fpr_or_lkeyid(key);
250 buf_strcpy(buf, s);
251}
252
256static void gpgme_key_flags(const struct ExpandoNode *node, void *data,
257 MuttFormatFlags flags, struct Buffer *buf)
258{
259 const struct CryptEntry *entry = data;
260 const struct CryptKeyInfo *key = entry->key;
261
262 const char *s = crypt_flags(key->flags);
263 buf_strcpy(buf, s);
264}
265
269static void gpgme_key_id(const struct ExpandoNode *node, void *data,
270 MuttFormatFlags flags, struct Buffer *buf)
271{
272 const struct CryptEntry *entry = data;
273 struct CryptKeyInfo *key = entry->key;
274
275 /* fixme: we need a way to distinguish between main and subkeys.
276 * Store the idx in entry? */
277 const char *s = crypt_keyid(key);
278 buf_strcpy(buf, s);
279}
280
284static long gpgme_key_length_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
285{
286 const struct CryptEntry *entry = data;
287 const struct CryptKeyInfo *key = entry->key;
288
289 return key->kobj->subkeys ? key->kobj->subkeys->length : 0;
290}
291
295static void gpgme_key_protocol(const struct ExpandoNode *node, void *data,
296 MuttFormatFlags flags, struct Buffer *buf)
297{
298 const struct CryptEntry *entry = data;
299 const struct CryptKeyInfo *key = entry->key;
300
301 const char *s = gpgme_get_protocol_name(key->kobj->protocol);
302 buf_strcpy(buf, s);
303}
304
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
Convenience wrapper for the core headers.
const char * crypt_fpr_or_lkeyid(struct CryptKeyInfo *k)
Find the fingerprint of a key.
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Wrapper for PGP/SMIME calls to GPGME.
@ ED_PGP
Pgp ED_PGP_ ExpandoDataPgp.
Definition domain.h:51
@ ED_PGP_KEY
Pgp_Key ED_PGK_ ExpandoDataPgpKey.
Definition domain.h:53
Parse Expando string.
static char * crypt_key_abilities(KeyFlags flags)
Parse key flags into a string.
const struct ExpandoRenderCallback PgpEntryGpgmeRenderCallbacks[]
Callbacks for GPGME Key Expandos.
static char * crypt_flags(KeyFlags flags)
Parse the key flags into a single character.
Ncrypt GPGME Expando definitions.
static long gpgme_key_length_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
GPGME: Key length - Implements get_number_t -.
static long gpgme_entry_number_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
GPGME: Index number - Implements get_number_t -.
static long gpgme_key_date_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
GPGME: Date of the key - Implements get_number_t -.
static void gpgme_key_flags(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Key Flags - Implements get_string_t -.
static void gpgme_key_protocol(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Protocol - Implements get_string_t -.
static void gpgme_entry_trust(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Trust/validity - Implements get_string_t -.
static void gpgme_key_capabilities(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Key Capabilities - Implements get_string_t -.
static void gpgme_key_algorithm(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Key Algorithm - Implements get_string_t -.
static void gpgme_key_date(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Date of the key - Implements get_string_t -.
static void gpgme_key_fingerprint(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Key fingerprint - Implements get_string_t -.
static void gpgme_entry_user_id(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: User id - Implements get_string_t -.
static void gpgme_key_id(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
GPGME: Key id - Implements get_string_t -.
struct tm mutt_date_localtime(time_t t)
Converts calendar time to a broken-down time structure expressed in user timezone.
Definition date.c:905
Convenience wrapper for the library headers.
API for encryption/signing of emails.
#define KEYFLAG_EXPIRED
Key is expired.
Definition lib.h:137
#define KEYFLAG_ISX509
Key is an X.509 key.
Definition lib.h:135
uint16_t KeyFlags
Flags describing PGP/SMIME keys, e.g. KEYFLAG_CANSIGN.
Definition lib.h:131
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition lib.h:134
#define KEYFLAG_PREFER_SIGNING
Key's owner prefers signing.
Definition lib.h:143
#define KEYFLAG_CRITICAL
Key is marked critical.
Definition lib.h:141
#define KEYFLAG_DISABLED
Key is marked disabled.
Definition lib.h:139
#define KEYFLAG_REVOKED
Key is revoked.
Definition lib.h:138
#define KEYFLAG_PREFER_ENCRYPTION
Key's owner prefers encryption.
Definition lib.h:142
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition lib.h:133
Shared constants/structs that are private to libconn.
@ ED_PGP_NUMBER
PgpEntry.num.
Definition private.h:51
@ ED_PGP_USER_ID
PgpUid.addr.
Definition private.h:53
@ ED_PGP_TRUST
PgpUid, TrustFlags.
Definition private.h:52
Misc PGP helper routines.
@ ED_PGK_KEY_CAPABILITIES
PgpKeyInfo.flags, pgp_key_abilities()
Definition pgplib.h:71
@ ED_PGK_KEY_FINGERPRINT
PgpKeyInfo.fingerprint.
Definition pgplib.h:72
@ ED_PGK_PKEY_LENGTH
pgp_principal_key(), PgpKeyInfo.keylen
Definition pgplib.h:81
@ ED_PGK_PKEY_ALGORITHM
pgp_principal_key(), PgpKeyInfo.algorithm
Definition pgplib.h:76
@ ED_PGK_DATE
PgpKeyInfo.gen_time.
Definition pgplib.h:69
@ ED_PGK_PKEY_FINGERPRINT
pgp_principal_key(), PgpKeyInfo.fingerprint
Definition pgplib.h:78
@ ED_PGK_KEY_ID
PgpKeyInfo, pgp_this_keyid()
Definition pgplib.h:74
@ ED_PGK_PROTOCOL
PgpKeyInfo.
Definition pgplib.h:82
@ ED_PGK_PKEY_CAPABILITIES
pgp_principal_key(), PgpKeyInfo.flags, pgp_key_abilities()
Definition pgplib.h:77
@ ED_PGK_KEY_FLAGS
PgpKeyInfo.kflags, pgp_flags()
Definition pgplib.h:73
@ ED_PGK_PKEY_ID
pgp_principal_key(), PgpKeyInfo, pgp_this_keyid()
Definition pgplib.h:80
@ ED_PGK_KEY_ALGORITHM
PgpKeyInfo.algorithm.
Definition pgplib.h:70
@ ED_PGK_KEY_LENGTH
PgpKeyInfo.keylen.
Definition pgplib.h:75
@ ED_PGK_PKEY_FLAGS
pgp_principal_key(), PgpKeyInfo.kflags, pgp_flags()
Definition pgplib.h:79
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition render.h:32
String manipulation buffer.
Definition buffer.h:36
An entry in the Select-Key menu.
Definition crypt_gpgme.h:86
struct CryptKeyInfo * key
Key.
Definition crypt_gpgme.h:88
size_t num
Index number.
Definition crypt_gpgme.h:87
A stored PGP key.
Definition crypt_gpgme.h:44
gpgme_validity_t validity
uid validity (cached for convenience)
Definition crypt_gpgme.h:50
KeyFlags flags
global and per uid flags (for convenience)
Definition crypt_gpgme.h:49
const char * uid
and for convenience point to this user ID
Definition crypt_gpgme.h:48
gpgme_key_t kobj
Definition crypt_gpgme.h:46
Basic Expando Node.
Definition node.h:67
const char * text
Node-specific text.
Definition node.h:73
Container for Accounts, Notifications.
Definition neomutt.h:43
locale_t time_c_locale
Current locale but LC_TIME=C.
Definition neomutt.h:49