e56dff91 |
1 | /* -*-c-*- |
2 | * |
ef13e9a4 |
3 | * $Id$ |
e56dff91 |
4 | * |
5 | * Packing and unpacking key data |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
e56dff91 |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
45c0fd36 |
18 | * |
e56dff91 |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
45c0fd36 |
23 | * |
e56dff91 |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
e56dff91 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <mLib/dstr.h> |
33 | |
e56dff91 |
34 | #include "key-data.h" |
35 | |
36 | /*----- Generic packing and unpacking -------------------------------------*/ |
37 | |
38 | /* --- @key_pack@ --- * |
39 | * |
40 | * Arguments: @key_packdef *kp@ = pointer to packing structure |
ef13e9a4 |
41 | * @key_data **kd@ = where to put the key data pointer |
e56dff91 |
42 | * @dstr *d@ = pointer to tag string for the key data |
43 | * |
44 | * Returns: Error code, or zero. |
45 | * |
46 | * Use: Packs a key from a data structure. |
47 | */ |
48 | |
ef13e9a4 |
49 | int key_pack(key_packdef *kp, key_data **kd, dstr *d) |
e56dff91 |
50 | { |
ef13e9a4 |
51 | switch (kp->e & KF_ENCMASK) { |
e56dff91 |
52 | |
53 | /* --- Binary and integer keys are easy --- */ |
54 | |
ef13e9a4 |
55 | case KENC_BINARY: { |
56 | key_bin *b = kp->p; |
57 | *kd = key_newbinary(kp->e, b->k, b->sz); |
e56dff91 |
58 | return (0); |
ef13e9a4 |
59 | } |
e56dff91 |
60 | case KENC_MP: |
ef13e9a4 |
61 | *kd = key_newmp(kp->e, *(mp **)kp->p); |
e56dff91 |
62 | return (0); |
1ba83484 |
63 | case KENC_STRING: |
ef13e9a4 |
64 | *kd = key_newstring(kp->e, *(char **)kp->p); |
1ba83484 |
65 | return (0); |
66 | case KENC_EC: |
ef13e9a4 |
67 | *kd = key_newec(kp->e, (ec *)kp->p); |
1ba83484 |
68 | return (0); |
e56dff91 |
69 | |
70 | /* --- Encrypted keys are a little tricky --- * |
71 | * |
72 | * This works rather differently to unpacking. |
73 | */ |
74 | |
75 | case KENC_ENCRYPT: { |
ef13e9a4 |
76 | key_data *kkd; |
e56dff91 |
77 | int err = key_pack(kp->p, &kkd, d); |
78 | if (!err) { |
ef13e9a4 |
79 | err = key_plock(kd, kkd, d->buf); |
80 | key_drop(kkd); |
e56dff91 |
81 | } |
82 | return (err); |
83 | } |
84 | |
85 | /* --- Structured keys, as ever, are a nuisance --- */ |
86 | |
87 | case KENC_STRUCT: { |
88 | int err; |
89 | key_packstruct *p; |
90 | size_t l = d->len; |
91 | |
ef13e9a4 |
92 | *kd = key_newstruct(); |
e56dff91 |
93 | DPUTC(d, '.'); |
94 | for (p = kp->p; p->name; p++) { |
95 | key_data *kkd; |
96 | d->len = l + 1; |
97 | DPUTS(d, p->name); |
ef13e9a4 |
98 | if ((err = key_pack(&p->kp, &kkd, d)) != 0) { |
99 | key_drop(*kd); |
e56dff91 |
100 | return (err); |
101 | } |
ef13e9a4 |
102 | key_structsteal(*kd, p->name, kkd); |
e56dff91 |
103 | } |
104 | d->len = l; |
105 | d->buf[l] = 0; |
106 | return (0); |
107 | } |
ef13e9a4 |
108 | default: |
109 | abort(); |
e56dff91 |
110 | } |
e56dff91 |
111 | } |
112 | |
113 | /* --- @key_unpack@ --- * |
114 | * |
115 | * Arguments: @key_packdef *kp@ = pointer to packing structure |
116 | * @key_data *kd@ = pointer to source key data |
117 | * @dstr *d@ = pointer to tag string for the key data |
118 | * |
119 | * Returns: Error code, or zero. |
120 | * |
121 | * Use: Unpacks a key into an appropriate data structure. |
122 | */ |
123 | |
124 | int key_unpack(key_packdef *kp, key_data *kd, dstr *d) |
125 | { |
ef13e9a4 |
126 | unsigned e = kp->e & KF_ENCMASK; |
e56dff91 |
127 | int err; |
128 | |
129 | /* --- Decrypt the encrypted key --- */ |
130 | |
ef13e9a4 |
131 | if ((kd->e & KF_ENCMASK) == KENC_ENCRYPT) { |
132 | if ((err = key_punlock(&kp->kd, kd, d->buf)) != 0) |
e56dff91 |
133 | goto fail; |
ef13e9a4 |
134 | kd = kp->kd; |
e56dff91 |
135 | } |
136 | |
137 | /* --- Ensure that the key has the right type --- */ |
138 | |
139 | if ((kd->e & KF_ENCMASK) != e) { |
f387fcb1 |
140 | err = KERR_WRONGTYPE; |
e56dff91 |
141 | goto fail; |
142 | } |
143 | |
144 | /* --- Unpack the key --- * |
145 | * |
146 | * Only three possibilities left now. |
147 | */ |
148 | |
149 | switch (e) { |
150 | |
151 | /* --- Binary and integer keys are easy --- */ |
152 | |
153 | case KENC_BINARY: |
154 | *(key_bin *)kp->p = kd->u.k; |
155 | break; |
156 | case KENC_MP: |
157 | *(mp **)kp->p = kd->u.m; |
158 | break; |
1ba83484 |
159 | case KENC_STRING: |
160 | *(char **)kp->p = kd->u.p; |
161 | break; |
162 | case KENC_EC: |
163 | *(ec *)kp->p = kd->u.e; |
164 | break; |
e56dff91 |
165 | |
166 | /* --- Structured keys take a little care --- */ |
167 | |
168 | case KENC_STRUCT: { |
169 | key_packstruct *p, *q; |
170 | size_t l = d->len; |
171 | |
172 | /* --- Iterate over the requested subparts --- */ |
173 | |
174 | DPUTC(d, '.'); |
175 | for (p = kp->p; p->name; p++) { |
176 | key_data *kkd; |
177 | |
178 | /* --- Build the name --- */ |
179 | |
180 | d->len = l + 1; |
181 | DPUTS(d, p->name); |
182 | |
183 | /* --- Find and unpack the subkey --- */ |
184 | |
185 | if ((kkd = key_structfind(kd, p->name)) == 0) { |
ef13e9a4 |
186 | if (!(p->kp.e & KF_OPT)) { |
e56dff91 |
187 | err = KERR_NOTFOUND; |
188 | goto tidy; |
189 | } |
190 | } else if ((err = key_unpack(&p->kp, kkd, d)) != 0) { |
191 | p++; |
192 | goto tidy; |
193 | } |
194 | } |
195 | |
196 | /* --- Done --- */ |
197 | |
198 | d->len = l; |
199 | d->buf[l] = 0; |
200 | break; |
201 | |
202 | /* --- Tidy up if something went wrong --- */ |
203 | |
204 | tidy: |
205 | for (q = kp->p; q < p; q++) |
206 | key_unpackdone(&q->kp); |
207 | goto fail; |
208 | } |
ef13e9a4 |
209 | |
210 | default: |
211 | abort(); |
e56dff91 |
212 | } |
213 | |
214 | return (0); |
215 | |
216 | /* --- Something went wrong --- */ |
217 | |
218 | fail: |
ef13e9a4 |
219 | if (kp->kd) { |
220 | key_drop(kp->kd); |
221 | kp->kd = 0; |
222 | } |
e56dff91 |
223 | return (err); |
224 | } |
225 | |
226 | /* --- @key_unpackdone@ --- * |
227 | * |
228 | * Arguments: @key_packdef *kp@ = pointer to packing definition |
229 | * |
230 | * Returns: --- |
231 | * |
232 | * Use: Frees the key components contained within a packing |
233 | * definition, created during key unpacking. |
234 | */ |
235 | |
236 | void key_unpackdone(key_packdef *kp) |
237 | { |
ef13e9a4 |
238 | if (kp->kd) { |
239 | key_drop(kp->kd); |
240 | kp->kd = 0; |
241 | } |
242 | if ((kp->e & KF_ENCMASK) == KENC_STRUCT) { |
e56dff91 |
243 | key_packstruct *p; |
244 | for (p = kp->p; p->name; p++) |
245 | key_unpackdone(&p->kp); |
246 | } |
247 | } |
248 | |
249 | /*----- That's all, folks -------------------------------------------------*/ |