NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
pgplib.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include "mutt/lib.h"
32#ifdef CRYPT_BACKEND_CLASSIC_PGP
33#include "pgplib.h"
34#endif
35
41const char *pgp_pkalgbytype(unsigned char type)
42{
43 switch (type)
44 {
45 case 1:
46 return "RSA";
47 case 2:
48 return "RSA";
49 case 3:
50 return "RSA";
51 case 16:
52 return "ElG";
53 case 17:
54 return "DSA";
55 case 20:
56 return "ElG";
57 default:
58 return "unk";
59 }
60}
61
67bool pgp_canencrypt(unsigned char type)
68{
69 switch (type)
70 {
71 case 1:
72 case 2:
73 case 16:
74 case 20:
75 return true;
76 default:
77 return false;
78 }
79}
80
86bool pgp_cansign(unsigned char type)
87{
88 switch (type)
89 {
90 case 1:
91 case 3:
92 case 17:
93 case 20:
94 return true;
95 default:
96 return false;
97 }
98}
99
104static void pgp_uid_free(struct PgpUid **upp)
105{
106 struct PgpUid *up = NULL, *q = NULL;
107
108 if (!upp || !*upp)
109 return;
110 for (up = *upp; up; up = q)
111 {
112 q = up->next;
113 FREE(&up->addr);
114 FREE(&up);
115 }
116
117 *upp = NULL;
118}
119
126struct PgpUid *pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
127{
128 struct PgpUid *l = NULL;
129 struct PgpUid **lp = &l;
130
131 for (; up; up = up->next)
132 {
133 *lp = mutt_mem_calloc(1, sizeof(struct PgpUid));
134 (*lp)->trust = up->trust;
135 (*lp)->flags = up->flags;
136 (*lp)->addr = mutt_str_dup(up->addr);
137 (*lp)->parent = parent;
138 lp = &(*lp)->next;
139 }
140
141 return l;
142}
143
148static void key_free(struct PgpKeyInfo **kpp)
149{
150 if (!kpp || !*kpp)
151 return;
152
153 struct PgpKeyInfo *kp = *kpp;
154
155 pgp_uid_free(&kp->address);
156 FREE(&kp->keyid);
157 FREE(&kp->fingerprint);
158 FREE(kpp);
159}
160
167struct PgpKeyInfo *pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
168{
169 if (!klist || !*klist || !key)
170 return NULL;
171
172 struct PgpKeyInfo **last = NULL;
173 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
174
175 if (key->parent && (key->parent != key))
176 key = key->parent;
177
178 last = klist;
179 for (p = *klist; p && p != key; p = p->next)
180 last = &p->next;
181
182 if (!p)
183 return NULL;
184
185 for (q = p->next, r = p; q && q->parent == p; q = q->next)
186 r = q;
187
188 if (r)
189 r->next = NULL;
190
191 *last = q;
192 return q;
193}
194
199void pgp_key_free(struct PgpKeyInfo **kpp)
200{
201 if (!kpp || !*kpp)
202 return;
203
204 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
205
206 if ((*kpp)->parent && ((*kpp)->parent != *kpp))
207 *kpp = (*kpp)->parent;
208
209 /* Order is important here:
210 *
211 * - First free all children.
212 * - If we are an orphan (i.e., our parent was not in the key list),
213 * free our parent.
214 * - free ourselves. */
215
216 for (p = *kpp; p; p = q)
217 {
218 for (q = p->next; q && q->parent == p; q = r)
219 {
220 r = q->next;
221 key_free(&q);
222 }
223
224 key_free(&p->parent);
225 key_free(&p);
226 }
227
228 *kpp = NULL;
229}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
const char * pgp_pkalgbytype(unsigned char type)
Get the name of the algorithm from its ID.
Definition: pgplib.c:41
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:199
struct PgpKeyInfo * pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
Remove a PGP key from a list.
Definition: pgplib.c:167
bool pgp_cansign(unsigned char type)
Does this algorithm ID support signing?
Definition: pgplib.c:86
static void key_free(struct PgpKeyInfo **kpp)
Free a PGP Key info.
Definition: pgplib.c:148
struct PgpUid * pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
Copy a list of PGP UIDs.
Definition: pgplib.c:126
static void pgp_uid_free(struct PgpUid **upp)
Free a PGP UID.
Definition: pgplib.c:104
bool pgp_canencrypt(unsigned char type)
Does this algorithm ID support encryption?
Definition: pgplib.c:67
Misc PGP helper routines.
Information about a PGP key.
Definition: pgplib.h:47
char * keyid
Definition: pgplib.h:48
struct PgpKeyInfo * next
Definition: pgplib.h:57
struct PgpUid * address
Definition: pgplib.h:50
char * fingerprint
Definition: pgplib.h:49
struct PgpKeyInfo * parent
Definition: pgplib.h:56
PGP User ID.
Definition: pgplib.h:35
short trust
Definition: pgplib.h:37
struct PgpKeyInfo * parent
Parent key.
Definition: pgplib.h:39
int flags
Definition: pgplib.h:38
char * addr
Definition: pgplib.h:36
struct PgpUid * next
Linked list.
Definition: pgplib.h:40