NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 **ptr)
149{
150 if (!ptr || !*ptr)
151 return;
152
153 struct PgpKeyInfo *key = *ptr;
154
155 pgp_uid_free(&key->address);
156 FREE(&key->keyid);
157 FREE(&key->fingerprint);
158
159 FREE(ptr);
160}
161
168struct PgpKeyInfo *pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
169{
170 if (!klist || !*klist || !key)
171 return NULL;
172
173 struct PgpKeyInfo **last = NULL;
174 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
175
176 if (key->parent && (key->parent != key))
177 key = key->parent;
178
179 last = klist;
180 for (p = *klist; p && p != key; p = p->next)
181 last = &p->next;
182
183 if (!p)
184 return NULL;
185
186 for (q = p->next, r = p; q && q->parent == p; q = q->next)
187 r = q;
188
189 if (r)
190 r->next = NULL;
191
192 *last = q;
193 return q;
194}
195
200void pgp_key_free(struct PgpKeyInfo **kpp)
201{
202 if (!kpp || !*kpp)
203 return;
204
205 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
206
207 if ((*kpp)->parent && ((*kpp)->parent != *kpp))
208 *kpp = (*kpp)->parent;
209
210 /* Order is important here:
211 *
212 * - First free all children.
213 * - If we are an orphan (i.e., our parent was not in the key list),
214 * free our parent.
215 * - free ourselves. */
216
217 for (p = *kpp; p; p = q)
218 {
219 for (q = p->next; q && q->parent == p; q = r)
220 {
221 r = q->next;
222 key_free(&q);
223 }
224
225 key_free(&p->parent);
226 key_free(&p);
227 }
228
229 *kpp = NULL;
230}
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:45
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
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:200
struct PgpKeyInfo * pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
Remove a PGP key from a list.
Definition: pgplib.c:168
bool pgp_cansign(unsigned char type)
Does this algorithm ID support signing?
Definition: pgplib.c:86
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
static void key_free(struct PgpKeyInfo **ptr)
Free a PGP Key info.
Definition: pgplib.c:148
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