Remove unused code
[apps/madmutt.git] / lib-crypt / pgplib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1997-2000 Thomas Roessler <roessler@does-not-exist.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 /* Generally useful, pgp-related functions. */
11
12 #include <lib-lib/lib-lib.h>
13
14 #include "lib.h"
15 #include "pgplib.h"
16
17 const char *pgp_pkalgbytype (unsigned char type)
18 {
19   switch (type) {
20   case 1:
21     return "RSA";
22   case 2:
23     return "RSA";
24   case 3:
25     return "RSA";
26   case 16:
27     return "ElG";
28   case 17:
29     return "DSA";
30   case 20:
31     return "ElG";
32   default:
33     return "unk";
34   }
35 }
36
37 short pgp_canencrypt (unsigned char type)
38 {
39   switch (type) {
40   case 1:
41   case 2:
42   case 16:
43   case 20:
44     return 1;
45   default:
46     return 0;
47   }
48 }
49
50 short pgp_cansign (unsigned char type)
51 {
52   switch (type) {
53   case 1:
54   case 3:
55   case 17:
56   case 20:
57     return 1;
58   default:
59     return 0;
60   }
61 }
62
63 /* return values: 
64
65  * 1 = sign only
66  * 2 = encrypt only
67  * 3 = both
68  */
69
70 short pgp_get_abilities (unsigned char type)
71 {
72   return (pgp_canencrypt (type) << 1) | pgp_cansign (type);
73 }
74
75 static void pgp_free_sig (pgp_sig_t ** sigp)
76 {
77   pgp_sig_t *sp, *q;
78
79   if (!sigp || !*sigp)
80     return;
81
82   for (sp = *sigp; sp; sp = q) {
83     q = sp->next;
84     p_delete(&sp);
85   }
86
87   *sigp = NULL;
88 }
89
90 static void pgp_free_uid (pgp_uid_t ** upp)
91 {
92   pgp_uid_t *up, *q;
93
94   if (!upp || !*upp)
95     return;
96   for (up = *upp; up; up = q) {
97     q = up->next;
98     pgp_free_sig (&up->sigs);
99     p_delete(&up->addr);
100     p_delete(&up);
101   }
102
103   *upp = NULL;
104 }
105
106 pgp_uid_t *pgp_copy_uids (pgp_uid_t * up, pgp_key_t parent)
107 {
108   pgp_uid_t *l = NULL;
109   pgp_uid_t **lp = &l;
110
111   for (; up; up = up->next) {
112     *lp = p_new(pgp_uid_t, 1);
113     (*lp)->trust = up->trust;
114     (*lp)->flags = up->flags;
115     (*lp)->addr = m_strdup(up->addr);
116     (*lp)->parent = parent;
117     lp = &(*lp)->next;
118   }
119
120   return l;
121 }
122
123 static void _pgp_free_key (pgp_key_t * kpp)
124 {
125   pgp_key_t kp;
126
127   if (!kpp || !*kpp)
128     return;
129
130   kp = *kpp;
131
132   pgp_free_uid (&kp->address);
133   p_delete(&kp->keyid);
134   p_delete(kpp);
135 }
136
137 pgp_key_t pgp_remove_key (pgp_key_t * klist, pgp_key_t key)
138 {
139   pgp_key_t *last;
140   pgp_key_t p, q, r;
141
142   if (!klist || !*klist || !key)
143     return NULL;
144
145   if (key->parent && key->parent != key)
146     key = key->parent;
147
148   last = klist;
149   for (p = *klist; p && p != key; p = p->next)
150     last = &p->next;
151
152   if (!p)
153     return NULL;
154
155   for (q = p->next, r = p; q && q->parent == p; q = q->next)
156     r = q;
157
158   if (r)
159     r->next = NULL;
160
161   *last = q;
162   return q;
163 }
164
165 void pgp_free_key (pgp_key_t * kpp)
166 {
167   pgp_key_t p, q, r;
168
169   if (!kpp || !*kpp)
170     return;
171
172   if ((*kpp)->parent && (*kpp)->parent != *kpp)
173     *kpp = (*kpp)->parent;
174
175   /* Order is important here:
176    *
177    * - First free all children.
178    * - If we are an orphan (i.e., our parent was not in the key list),
179    *   free our parent.
180    * - free ourselves.
181    */
182
183   for (p = *kpp; p; p = q) {
184     for (q = p->next; q && q->parent == p; q = r) {
185       r = q->next;
186       _pgp_free_key (&q);
187     }
188     if (p->parent)
189       _pgp_free_key (&p->parent);
190
191     _pgp_free_key (&p);
192   }
193
194   *kpp = NULL;
195 }