NeoMutt
2025-12-11-1009-ga75d9e
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
Loading...
Searching...
No Matches
pgplib.c
Go to the documentation of this file.
1
23
29
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
42
const
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
68
bool
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
87
bool
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
105
static
void
pgp_uid_free
(
struct
PgpUid
**upp)
106
{
107
struct
PgpUid
*up = NULL;
108
struct
PgpUid
*q = NULL;
109
110
if
(!upp || !*upp)
111
return
;
112
for
(up = *upp; up; up = q)
113
{
114
q = up->
next
;
115
FREE
(&up->
addr
);
116
FREE
(&up);
117
}
118
119
*upp = NULL;
120
}
121
128
struct
PgpUid
*
pgp_copy_uids
(
struct
PgpUid
*up,
struct
PgpKeyInfo
*
parent
)
129
{
130
struct
PgpUid
*l = NULL;
131
struct
PgpUid
**lp = &l;
132
133
for
(; up; up = up->
next
)
134
{
135
*lp =
MUTT_MEM_CALLOC
(1,
struct
PgpUid
);
136
(*lp)->trust = up->
trust
;
137
(*lp)->flags = up->
flags
;
138
(*lp)->addr =
mutt_str_dup
(up->
addr
);
139
(*lp)->parent =
parent
;
140
lp = &(*lp)->
next
;
141
}
142
143
return
l;
144
}
145
150
static
void
key_free
(
struct
PgpKeyInfo
**ptr)
151
{
152
if
(!ptr || !*ptr)
153
return
;
154
155
struct
PgpKeyInfo
*key = *ptr;
156
157
pgp_uid_free
(&key->
address
);
158
FREE
(&key->
keyid
);
159
FREE
(&key->
fingerprint
);
160
161
FREE
(ptr);
162
}
163
170
struct
PgpKeyInfo
*
pgp_remove_key
(
struct
PgpKeyInfo
**klist,
struct
PgpKeyInfo
*key)
171
{
172
if
(!klist || !*klist || !key)
173
return
NULL;
174
175
struct
PgpKeyInfo
**last = NULL;
176
struct
PgpKeyInfo
*p = NULL;
177
struct
PgpKeyInfo
*q = NULL;
178
struct
PgpKeyInfo
*r = NULL;
179
180
if
(key->
parent
&& (key->
parent
!= key))
181
key = key->
parent
;
182
183
last = klist;
184
for
(p = *klist; p && p != key; p = p->
next
)
185
last = &p->
next
;
186
187
if
(!p)
188
return
NULL;
189
190
for
(q = p->
next
, r = p; q && q->
parent
== p; q = q->
next
)
191
r = q;
192
193
if
(r)
194
r->
next
= NULL;
195
196
*last = q;
197
return
q;
198
}
199
204
void
pgp_key_free
(
struct
PgpKeyInfo
**kpp)
205
{
206
if
(!kpp || !*kpp)
207
return
;
208
209
struct
PgpKeyInfo
*p = NULL;
210
struct
PgpKeyInfo
*q = NULL;
211
struct
PgpKeyInfo
*r = NULL;
212
213
if
((*kpp)->parent && ((*kpp)->parent != *kpp))
214
*kpp = (*kpp)->
parent
;
215
216
/* Order is important here:
217
*
218
* - First free all children.
219
* - If we are an orphan (i.e., our parent was not in the key list),
220
* free our parent.
221
* - free ourselves. */
222
223
for
(p = *kpp; p; p = q)
224
{
225
for
(q = p->
next
; q && q->
parent
== p; q = r)
226
{
227
r = q->
next
;
228
key_free
(&q);
229
}
230
231
key_free
(&p->
parent
);
232
key_free
(&p);
233
}
234
235
*kpp = NULL;
236
}
FREE
#define FREE(x)
Free memory and set the pointer to NULL.
Definition
memory.h:68
MUTT_MEM_CALLOC
#define MUTT_MEM_CALLOC(n, type)
Definition
memory.h:52
lib.h
Convenience wrapper for the library headers.
mutt_str_dup
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition
string.c:257
pgp_pkalgbytype
const char * pgp_pkalgbytype(unsigned char type)
Get the name of the algorithm from its ID.
Definition
pgplib.c:42
pgp_key_free
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition
pgplib.c:204
pgp_remove_key
struct PgpKeyInfo * pgp_remove_key(struct PgpKeyInfo **klist, struct PgpKeyInfo *key)
Remove a PGP key from a list.
Definition
pgplib.c:170
pgp_cansign
bool pgp_cansign(unsigned char type)
Does this algorithm ID support signing?
Definition
pgplib.c:87
pgp_copy_uids
struct PgpUid * pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
Copy a list of PGP UIDs.
Definition
pgplib.c:128
pgp_uid_free
static void pgp_uid_free(struct PgpUid **upp)
Free a PGP UID.
Definition
pgplib.c:105
pgp_canencrypt
bool pgp_canencrypt(unsigned char type)
Does this algorithm ID support encryption?
Definition
pgplib.c:68
key_free
static void key_free(struct PgpKeyInfo **ptr)
Free a PGP Key info.
Definition
pgplib.c:150
pgplib.h
Misc PGP helper routines.
PgpKeyInfo
Information about a PGP key.
Definition
pgplib.h:49
PgpKeyInfo::keyid
char * keyid
Key ID.
Definition
pgplib.h:50
PgpKeyInfo::next
struct PgpKeyInfo * next
Linked list.
Definition
pgplib.h:59
PgpKeyInfo::address
struct PgpUid * address
User IDs.
Definition
pgplib.h:52
PgpKeyInfo::fingerprint
char * fingerprint
Key fingerprint.
Definition
pgplib.h:51
PgpKeyInfo::parent
struct PgpKeyInfo * parent
Parent key.
Definition
pgplib.h:58
PgpUid
PGP User ID.
Definition
pgplib.h:36
PgpUid::trust
short trust
Trust level.
Definition
pgplib.h:38
PgpUid::parent
struct PgpKeyInfo * parent
Parent key.
Definition
pgplib.h:40
PgpUid::flags
int flags
Flags for this UID.
Definition
pgplib.h:39
PgpUid::addr
char * addr
Email address.
Definition
pgplib.h:37
PgpUid::next
struct PgpUid * next
Linked list.
Definition
pgplib.h:41