NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
expando_gpgme.c
Go to the documentation of this file.
1
29#include <gpgme.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <time.h>
33#include "private.h"
34#include "mutt/lib.h"
35#include "core/lib.h"
36#include "expando_gpgme.h"
37#include "lib.h"
38#include "expando/lib.h"
39#include "crypt_gpgme.h"
40#include "pgplib.h"
41
49static char *crypt_key_abilities(KeyFlags flags)
50{
51 static char buf[3];
52
53 if (!(flags & KEYFLAG_CANENCRYPT))
54 buf[0] = '-';
55 else if (flags & KEYFLAG_PREFER_SIGNING)
56 buf[0] = '.';
57 else
58 buf[0] = 'e';
59
60 if (!(flags & KEYFLAG_CANSIGN))
61 buf[1] = '-';
62 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
63 buf[1] = '.';
64 else
65 buf[1] = 's';
66
67 buf[2] = '\0';
68
69 return buf;
70}
71
79static char *crypt_flags(KeyFlags flags)
80{
81 if (flags & KEYFLAG_REVOKED)
82 return "R";
83 if (flags & KEYFLAG_EXPIRED)
84 return "X";
85 if (flags & KEYFLAG_DISABLED)
86 return "d";
87 if (flags & KEYFLAG_CRITICAL)
88 return "c";
89
90 return " ";
91}
92
96static long gpgme_entry_number_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
97{
98 const struct CryptEntry *entry = data;
99 return entry->num;
100}
101
105static void gpgme_entry_trust(const struct ExpandoNode *node, void *data,
106 MuttFormatFlags flags, struct Buffer *buf)
107{
108 const struct CryptEntry *entry = data;
109 const struct CryptKeyInfo *key = entry->key;
110
111 const char *s = "";
112 if ((key->flags & KEYFLAG_ISX509))
113 {
114 s = "x";
115 }
116 else
117 {
118 switch (key->validity)
119 {
120 case GPGME_VALIDITY_FULL:
121 s = "f";
122 break;
123 case GPGME_VALIDITY_MARGINAL:
124 s = "m";
125 break;
126 case GPGME_VALIDITY_NEVER:
127 s = "n";
128 break;
129 case GPGME_VALIDITY_ULTIMATE:
130 s = "u";
131 break;
132 case GPGME_VALIDITY_UNDEFINED:
133 s = "q";
134 break;
135 case GPGME_VALIDITY_UNKNOWN:
136 default:
137 s = "?";
138 break;
139 }
140 }
141
142 buf_strcpy(buf, s);
143}
144
148static void gpgme_entry_user_id(const struct ExpandoNode *node, void *data,
149 MuttFormatFlags flags, struct Buffer *buf)
150{
151 const struct CryptEntry *entry = data;
152 const struct CryptKeyInfo *key = entry->key;
153
154 const char *s = key->uid;
155 buf_strcpy(buf, s);
156}
157
161static void gpgme_key_algorithm(const struct ExpandoNode *node, void *data,
162 MuttFormatFlags flags, struct Buffer *buf)
163{
164 const struct CryptEntry *entry = data;
165 const struct CryptKeyInfo *key = entry->key;
166
167 const char *s = NULL;
168 if (key->kobj->subkeys)
169 s = gpgme_pubkey_algo_name(key->kobj->subkeys->pubkey_algo);
170 else
171 s = "?";
172
173 buf_strcpy(buf, s);
174}
175
179static void gpgme_key_capabilities(const struct ExpandoNode *node, void *data,
180 MuttFormatFlags flags, struct Buffer *buf)
181{
182 const struct CryptEntry *entry = data;
183 const struct CryptKeyInfo *key = entry->key;
184
185 const char *s = crypt_key_abilities(key->flags);
186 buf_strcpy(buf, s);
187}
188
192static void gpgme_key_date(const struct ExpandoNode *node, void *data,
193 MuttFormatFlags flags, struct Buffer *buf)
194{
195 const struct CryptEntry *entry = data;
196 const struct CryptKeyInfo *key = entry->key;
197
198 const char *text = node->text;
199 bool use_c_locale = false;
200 if (*text == '!')
201 {
202 use_c_locale = true;
203 text++;
204 }
205
206 struct tm tm = { 0 };
207 if (key->kobj->subkeys && (key->kobj->subkeys->timestamp > 0))
208 {
209 tm = mutt_date_localtime(key->kobj->subkeys->timestamp);
210 }
211 else
212 {
213 tm = mutt_date_localtime(0); // Default to 1970-01-01
214 }
215
216 char tmp[128] = { 0 };
217 if (use_c_locale)
218 {
219 strftime_l(tmp, sizeof(tmp), text, &tm, NeoMutt->time_c_locale);
220 }
221 else
222 {
223 strftime(tmp, sizeof(tmp), text, &tm);
224 }
225
226 buf_strcpy(buf, tmp);
227}
228
232static long gpgme_key_date_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
233{
234 const struct CryptEntry *entry = data;
235 const struct CryptKeyInfo *key = entry->key;
236 return key->kobj->subkeys->timestamp;
237}
238
242static void gpgme_key_fingerprint(const struct ExpandoNode *node, void *data,
243 MuttFormatFlags flags, struct Buffer *buf)
244{
245 const struct CryptEntry *entry = data;
246 struct CryptKeyInfo *key = entry->key;
247
248 /* fixme: we need a way to distinguish between main and subkeys.
249 * Store the idx in entry? */
250 const char *s = crypt_fpr_or_lkeyid(key);
251 buf_strcpy(buf, s);
252}
253
257static void gpgme_key_flags(const struct ExpandoNode *node, void *data,
258 MuttFormatFlags flags, struct Buffer *buf)
259{
260 const struct CryptEntry *entry = data;
261 const struct CryptKeyInfo *key = entry->key;
262
263 const char *s = crypt_flags(key->flags);
264 buf_strcpy(buf, s);
265}
266
270static void gpgme_key_id(const struct ExpandoNode *node, void *data,
271 MuttFormatFlags flags, struct Buffer *buf)
272{
273 const struct CryptEntry *entry = data;
274 struct CryptKeyInfo *key = entry->key;
275
276 /* fixme: we need a way to distinguish between main and subkeys.
277 * Store the idx in entry? */
278 const char *s = crypt_keyid(key);
279 buf_strcpy(buf, s);
280}
281
285static long gpgme_key_length_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
286{
287 const struct CryptEntry *entry = data;
288 const struct CryptKeyInfo *key = entry->key;
289
290 return key->kobj->subkeys ? key->kobj->subkeys->length : 0;
291}
292
296static void gpgme_key_protocol(const struct ExpandoNode *node, void *data,
297 MuttFormatFlags flags, struct Buffer *buf)
298{
299 const struct CryptEntry *entry = data;
300 const struct CryptKeyInfo *key = entry->key;
301
302 const char *s = gpgme_get_protocol_name(key->kobj->protocol);
303 buf_strcpy(buf, s);
304}
305
312 // clang-format off
331 { -1, -1, NULL, NULL },
332 // clang-format on
333};
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.
Definition: crypt_gpgme.c:214
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:138
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.
Definition: expando_gpgme.c:49
const struct ExpandoRenderCallback PgpEntryGpgmeRenderCallbacks[]
Callbacks for GPGME Key Expandos.
static char * crypt_flags(KeyFlags flags)
Parse the key flags into a single character.
Definition: expando_gpgme.c:79
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 -.
Definition: expando_gpgme.c:96
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:906
Convenience wrapper for the library headers.
#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
@ 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
GUI display the mailboxes in a side panel.
Key value store.
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:42
locale_t time_c_locale
Current locale but LC_TIME=C.
Definition: neomutt.h:48