NeoMutt  2024-02-01-25-ga71e95
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgplib.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include "mutt/lib.h"
33#ifdef CRYPT_BACKEND_CLASSIC_PGP
34#include "pgplib.h"
35#endif
36
42const char *pgp_pkalgbytype(unsigned char type)
43{
44 switch (type)
45 {
46 case 1:
47 return "RSA";
48 case 2:
49 return "RSA";
50 case 3:
51 return "RSA";
52 case 16:
53 return "ElG";
54 case 17:
55 return "DSA";
56 case 20:
57 return "ElG";
58 default:
59 return "unk";
60 }
61}
62
68bool pgp_canencrypt(unsigned char type)
69{
70 switch (type)
71 {
72 case 1:
73 case 2:
74 case 16:
75 case 20:
76 return true;
77 default:
78 return false;
79 }
80}
81
87bool pgp_cansign(unsigned char type)
88{
89 switch (type)
90 {
91 case 1:
92 case 3:
93 case 17:
94 case 20:
95 return true;
96 default:
97 return false;
98 }
99}
100
105static void pgp_uid_free(struct PgpUid **upp)
106{
107 struct PgpUid *up = NULL, *q = NULL;
108
109 if (!upp || !*upp)
110 return;
111 for (up = *upp; up; up = q)
112 {
113 q = up->next;
114 FREE(&up->addr);
115 FREE(&up);
116 }
117
118 *upp = NULL;
119}
120
127struct PgpUid *pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
128{
129 struct PgpUid *l = NULL;
130 struct PgpUid **lp = &l;
131
132 for (; up; up = up->next)
133 {
134 *lp = mutt_mem_calloc(1, sizeof(struct PgpUid));
135 (*lp)->trust = up->trust;
136 (*lp)->flags = up->flags;
137 (*lp)->addr = mutt_str_dup(up->addr);
138 (*lp)->parent = parent;
139 lp = &(*lp)->next;
140 }
141
142 return l;
143}
144
149static void key_free(struct PgpKeyInfo **ptr)
150{
151 if (!ptr || !*ptr)
152 return;
153
154 struct PgpKeyInfo *key = *ptr;
155
156 pgp_uid_free(&key->address);
157 FREE(&key->keyid);
158 FREE(&key->fingerprint);
159
160 FREE(ptr);
161}
162
169struct PgpKeyInfo *pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
170{
171 if (!klist || !*klist || !key)
172 return NULL;
173
174 struct PgpKeyInfo **last = NULL;
175 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
176
177 if (key->parent && (key->parent != key))
178 key = key->parent;
179
180 last = klist;
181 for (p = *klist; p && p != key; p = p->next)
182 last = &p->next;
183
184 if (!p)
185 return NULL;
186
187 for (q = p->next, r = p; q && q->parent == p; q = q->next)
188 r = q;
189
190 if (r)
191 r->next = NULL;
192
193 *last = q;
194 return q;
195}
196
201void pgp_key_free(struct PgpKeyInfo **kpp)
202{
203 if (!kpp || !*kpp)
204 return;
205
206 struct PgpKeyInfo *p = NULL, *q = NULL, *r = NULL;
207
208 if ((*kpp)->parent && ((*kpp)->parent != *kpp))
209 *kpp = (*kpp)->parent;
210
211 /* Order is important here:
212 *
213 * - First free all children.
214 * - If we are an orphan (i.e., our parent was not in the key list),
215 * free our parent.
216 * - free ourselves. */
217
218 for (p = *kpp; p; p = q)
219 {
220 for (q = p->next; q && q->parent == p; q = r)
221 {
222 r = q->next;
223 key_free(&q);
224 }
225
226 key_free(&p->parent);
227 key_free(&p);
228 }
229
230 *kpp = NULL;
231}
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:253
const char * pgp_pkalgbytype(unsigned char type)
Get the name of the algorithm from its ID.
Definition: pgplib.c:42
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:201
struct PgpKeyInfo * pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
Remove a PGP key from a list.
Definition: pgplib.c:169
bool pgp_cansign(unsigned char type)
Does this algorithm ID support signing?
Definition: pgplib.c:87
struct PgpUid * pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
Copy a list of PGP UIDs.
Definition: pgplib.c:127
static void pgp_uid_free(struct PgpUid **upp)
Free a PGP UID.
Definition: pgplib.c:105
bool pgp_canencrypt(unsigned char type)
Does this algorithm ID support encryption?
Definition: pgplib.c:68
static void key_free(struct PgpKeyInfo **ptr)
Free a PGP Key info.
Definition: pgplib.c:149
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