chiark / gitweb /
server/keyexch.c: Prefix crypto-details trace messages correctly.
[tripe] / server / keymgmt.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Key loading and storing
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
31/*----- Global variables --------------------------------------------------*/
32
52c03a2a 33group *gg;
34mp *kpriv;
9317aa92 35ge *kpub;
b5c45da1 36algswitch algs;
de7bd20b 37size_t indexsz;
410c8acf 38
39/*----- Static variables --------------------------------------------------*/
40
41static key_file *kf_pub;
42static const char *kr_priv, *kr_pub, *tag_priv;
43static fwatch w_priv, w_pub;
44
52c03a2a 45/*----- Key groups --------------------------------------------------------*/
46
47typedef struct kgops {
48 const char *ty;
4d36660a
MW
49 int (*loadpriv)(key_data *, group **, mp **, dstr *, dstr *);
50 int (*loadpub)(key_data *, group **, ge **, dstr *, dstr *);
52c03a2a 51} kgops;
52
53/* --- Diffie-Hellman --- */
54
4d36660a 55static int kgdh_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
52c03a2a 56{
57 key_packstruct kps[DH_PRIVFETCHSZ];
58 key_packdef *kp;
59 dh_priv dp;
52c03a2a 60 int rc;
61
62 kp = key_fetchinit(dh_privfetch, kps, &dp);
63 if ((rc = key_unpack(kp, kd, t)) != 0) {
4d36660a
MW
64 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
65 goto fail_0;
52c03a2a 66 }
67 *g = group_prime(&dp.dp);
68 *x = MP_COPY(dp.x);
4d36660a
MW
69 rc = 0;
70 goto done;
71fail_0:
72 rc = -1;
52c03a2a 73done:
74 key_fetchdone(kp);
4d36660a 75 return (rc);
52c03a2a 76}
77
4d36660a 78static int kgdh_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
52c03a2a 79{
80 key_packstruct kps[DH_PUBFETCHSZ];
81 key_packdef *kp;
82 dh_pub dp;
52c03a2a 83 int rc;
84
85 kp = key_fetchinit(dh_pubfetch, kps, &dp);
86 if ((rc = key_unpack(kp, kd, t)) != 0) {
4d36660a
MW
87 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
88 goto fail_0;
52c03a2a 89 }
90 *g = group_prime(&dp.dp);
91 *p = G_CREATE(*g);
92 if (G_FROMINT(*g, *p, dp.y)) {
4d36660a
MW
93 a_format(e, "bad-public-vector", A_END);
94 goto fail_1;
52c03a2a 95 }
4d36660a
MW
96 rc = 0;
97 goto done;
98fail_1:
99 G_DESTROY(*g, *p);
100 G_DESTROYGROUP(*g);
101fail_0:
102 rc = -1;
52c03a2a 103done:
104 key_fetchdone(kp);
4d36660a 105 return (rc);
52c03a2a 106}
107
fc5f4823 108static const kgops kgdh_ops = { "dh", kgdh_priv, kgdh_pub };
52c03a2a 109
110/* --- Elliptic curve --- */
111
4d36660a 112static int kgec_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
52c03a2a 113{
114 key_packstruct kps[EC_PRIVFETCHSZ];
115 key_packdef *kp;
116 ec_priv ep;
117 ec_info ei;
4d36660a 118 const char *err;
52c03a2a 119 int rc;
120
121 kp = key_fetchinit(ec_privfetch, kps, &ep);
122 if ((rc = key_unpack(kp, kd, t)) != 0) {
4d36660a
MW
123 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
124 goto fail_0;
125 }
126 if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
127 a_format(e, "decode-failed", "%s", err, A_END);
128 goto fail_0;
52c03a2a 129 }
52c03a2a 130 *g = group_ec(&ei);
131 *x = MP_COPY(ep.x);
4d36660a
MW
132 rc = 0;
133 goto done;
134fail_0:
135 rc = -1;
52c03a2a 136done:
137 key_fetchdone(kp);
4d36660a 138 return (rc);
52c03a2a 139}
140
4d36660a 141static int kgec_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
52c03a2a 142{
143 key_packstruct kps[EC_PUBFETCHSZ];
144 key_packdef *kp;
145 ec_pub ep;
146 ec_info ei;
4d36660a 147 const char *err;
52c03a2a 148 int rc;
149
150 kp = key_fetchinit(ec_pubfetch, kps, &ep);
151 if ((rc = key_unpack(kp, kd, t)) != 0) {
4d36660a
MW
152 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
153 goto fail_0;
154 }
155 if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
156 a_format(e, "decode-failed", "%s", err, A_END);
157 goto fail_0;
52c03a2a 158 }
52c03a2a 159 *g = group_ec(&ei);
160 *p = G_CREATE(*g);
161 if (G_FROMEC(*g, *p, &ep.p)) {
4d36660a
MW
162 a_format(e, "bad-public-vector", A_END);
163 goto fail_1;
52c03a2a 164 }
4d36660a
MW
165 rc = 0;
166 goto done;
167fail_1:
168 G_DESTROY(*g, *p);
169 G_DESTROYGROUP(*g);
170fail_0:
171 rc = -1;
52c03a2a 172done:
173 key_fetchdone(kp);
4d36660a 174 return (rc);
52c03a2a 175}
176
fc5f4823 177static const kgops kgec_ops = { "ec", kgec_priv, kgec_pub };
52c03a2a 178
179/* --- Table of supported key types --- */
180
181static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
182
b5c45da1 183/*----- Algswitch stuff ---------------------------------------------------*/
184
185/* --- @algs_get@ --- *
186 *
187 * Arguments: @algswitch *a@ = where to put the algorithms
4d36660a
MW
188 * @dstr *e@ = where to write errror tokens
189 * @key_file *kf@ = key file
b5c45da1 190 * @key *k@ = key to inspect
191 *
4d36660a 192 * Returns: Zero if OK; nonzero on error.
b5c45da1 193 *
194 * Use: Extracts an algorithm choice from a key.
195 */
196
4d36660a 197static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
b5c45da1 198{
199 const char *p;
4d36660a 200 char *q, *qq;
b5c45da1 201 dstr d = DSTR_INIT;
4d36660a 202 int rc = -1;
b5c45da1 203
4d36660a 204 /* --- Symmetric encryption for bulk data --- */
b5c45da1 205
4d36660a
MW
206 if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc";
207 if ((a->c = gcipher_byname(p)) == 0) {
208 a_format(e, "unknown-cipher", "%s", p, A_END);
209 goto done;
210 }
211
212 /* --- Hash function --- */
213
214 if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
215 if ((a->h = ghash_byname(p)) == 0) {
216 a_format(e, "unknown-hash", "%s", p, A_END);
217 goto done;
218 }
b5c45da1 219
4d36660a 220 /* --- Symmetric encryption for key derivation --- */
b5c45da1 221
74ee77cb 222 if ((p = key_getattr(kf, k, "mgf")) == 0) {
b5c45da1 223 dstr_reset(&d);
74ee77cb 224 dstr_putf(&d, "%s-mgf", a->h->name);
b5c45da1 225 p = d.buf;
226 }
4d36660a
MW
227 if ((a->mgf = gcipher_byname(p)) == 0) {
228 a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
229 goto done;
230 }
231
232 /* --- Message authentication for bulk data --- */
b5c45da1 233
234 if ((p = key_getattr(kf, k, "mac")) != 0) {
235 dstr_reset(&d);
236 dstr_puts(&d, p);
237 if ((q = strchr(d.buf, '/')) != 0)
238 *q++ = 0;
4d36660a
MW
239 if ((a->m = gmac_byname(d.buf)) == 0) {
240 a_format(e, "unknown-mac", "%s", d.buf, A_END);
241 goto done;
242 }
b5c45da1 243 if (!q)
244 a->tagsz = a->m->hashsz;
245 else {
4d36660a
MW
246 unsigned long n = strtoul(q, &qq, 0);
247 if (*qq) {
248 a_format(e, "bad-tag-length-string", "%s", q, A_END);
249 goto done;
250 }
251 if (n%8 || n/8 > a->m->hashsz) {
252 a_format(e, "bad-tag-length", "%lu", n, A_END);
253 goto done;
254 }
b5c45da1 255 a->tagsz = n/8;
256 }
257 } else {
258 dstr_reset(&d);
259 dstr_putf(&d, "%s-hmac", a->h->name);
4d36660a
MW
260 if ((a->m = gmac_byname(d.buf)) == 0) {
261 a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
262 goto done;
263 }
b5c45da1 264 a->tagsz = a->h->hashsz/2;
265 }
266
4d36660a 267 rc = 0;
b5c45da1 268done:
269 dstr_destroy(&d);
4d36660a 270 return (rc);
b5c45da1 271}
272
273/* --- @algs_check@ --- *
274 *
275 * Arguments: @algswitch *a@ = a choice of algorithms
4d36660a 276 * @dstr *e@ = where to write error tokens
b5c45da1 277 * @const group *g@ = the group we're working in
278 *
4d36660a 279 * Returns: Zero if OK; nonzero on error.
b5c45da1 280 *
281 * Use: Checks an algorithm choice for sensibleness. This also
282 * derives some useful information from the choices, and you
283 * must call this before committing the algorithm selection
284 * for use by @keyset@ functions.
285 */
286
4d36660a 287static int algs_check(algswitch *a, dstr *e, const group *g)
b5c45da1 288{
289 /* --- Derive the key sizes --- *
290 *
291 * Must ensure that we have non-empty keys. This isn't ideal, but it
383a9d71
MW
292 * provides a handy sanity check. Also must be based on a 64- or 128-bit
293 * block cipher or we can't do the data expiry properly.
b5c45da1 294 */
295
296 a->hashsz = a->h->hashsz;
4d36660a
MW
297 if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) {
298 a_format(e, "cipher", "%s", a->c->name,
299 "no-key-size", "%lu", (unsigned long)a->hashsz,
300 A_END);
301 return (-1);
302 }
303 if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) {
304 a_format(e, "mac", "%s", a->m->name,
305 "no-key-size", "%lu", (unsigned long)a->hashsz,
306 A_END);
307 return (-1);
308 }
b5c45da1 309
383a9d71
MW
310 /* --- Derive the data limit --- */
311
312 if (a->c->blksz < 16) a->expsz = MEG(64);
313 else a->expsz = MEG(2048);
314
b5c45da1 315 /* --- Ensure the MGF accepts hashes as keys --- */
316
4d36660a
MW
317 if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
318 a_format(e, "mgf", "%s", a->mgf->name,
319 "restrictive-key-schedule",
320 A_END);
321 return (-1);
322 }
b5c45da1 323
324 /* --- All ship-shape and Bristol-fashion --- */
325
326 return (0);
327}
328
329/* --- @algs_samep@ --- *
330 *
331 * Arguments: @const algswitch *a, *aa@ = two algorithm selections
332 *
333 * Returns: Nonzero if the two selections are the same.
334 *
335 * Use: Checks sameness of algorithm selections: used to ensure that
336 * peers are using sensible algorithms.
337 */
338
339static int algs_samep(const algswitch *a, const algswitch *aa)
340{
341 return (a->c == aa->c && a->mgf == aa->mgf && a->h == aa->h &&
342 a->m == aa->m && a->tagsz == aa->tagsz);
343}
344
410c8acf 345/*----- Main code ---------------------------------------------------------*/
346
347/* --- @keymoan@ --- *
348 *
56814747 349 * Arguments: @const char *file@ = name of the file
e04c2d50
MW
350 * @int line@ = line number in file
351 * @const char *msg@ = error message
4d36660a 352 * @void *p@ = argument pointer (indicates which keyring)
410c8acf 353 *
e04c2d50 354 * Returns: ---
410c8acf 355 *
e04c2d50 356 * Use: Reports an error message about loading a key file.
410c8acf 357 */
358
359static void keymoan(const char *file, int line, const char *msg, void *p)
f43df819 360{
4d36660a
MW
361 const char *kind = p;
362
363 if (!line) {
364 a_warn("KEYMGMT", "%s-keyring", kind, "%s", file,
365 "io-error", "?ERRNO", A_END);
366 } else {
367 a_warn("KEYMGMT", "%s-keyring", kind, "%s", file, "line", "%d", line,
368 "%s", msg, A_END);
369 }
f43df819 370}
410c8acf 371
fc5f4823
MW
372/* --- @keykg@ --- *
373 *
374 * Arguments: @key_file *kf@ = pointer to key file
375 * @key *k@ = pointer to key
376 * @const char **tyr@ = where to put the type string
377 *
378 * Returns: Pointer to indicated key-group options, or null.
379 *
380 * Use: Looks up a key's group indicator and tries to find a matching
381 * table entry.
382 */
383
384static const kgops *keykg(key_file *kf, key *k, const char **tyr)
385{
386 const char *ty;
387 const kgops **ko;
388
389 /* --- Look up the key type in the table --- *
390 *
391 * There are several places to look for this. The most obvious is the
392 * `kx-group' key attribute. But there's also the key type itself.
393 */
394
395 ty = key_getattr(kf, k, "kx-group");
396 if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
397 if (!ty) ty = "dh";
398 if (tyr) *tyr = ty;
399
400 for (ko = kgtab; *ko; ko++) {
401 if (strcmp((*ko)->ty, ty) == 0)
402 return (*ko);
403 }
404 return (0);
405}
406
410c8acf 407/* --- @loadpriv@ --- *
408 *
409 * Arguments: @dstr *d@ = string to write errors in
410c8acf 410 *
411 * Returns: Zero if OK, nonzero on error.
412 *
413 * Use: Loads the private key from its keyfile.
414 */
415
4d36660a 416static int loadpriv(void)
410c8acf 417{
418 key_file kf;
52c03a2a 419 key *k;
d59d4fe9 420 key_data **kd;
52c03a2a 421 dstr t = DSTR_INIT;
4d36660a 422 dstr e = DSTR_INIT;
52c03a2a 423 group *g = 0;
424 mp *x = 0;
425 int rc = -1;
fc5f4823 426 const kgops *ko;
4d36660a 427 const char *err, *tag, *ty;
b5c45da1 428 algswitch a;
52c03a2a 429
430 /* --- Open the private key file --- */
410c8acf 431
4d36660a 432 if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, "private"))
52c03a2a 433 goto done_0;
52c03a2a 434
435 /* --- Find the private key --- */
436
fc5f4823
MW
437 if (tag_priv ?
438 key_qtag(&kf, tag = tag_priv, &t, &k, &kd) :
439 key_qtag(&kf, tag = "tripe", &t, &k, &kd) &&
e2fe2afd 440 key_qtag(&kf, tag = "tripe-dh", &t, &k, &kd)) {
4d36660a
MW
441 a_warn("KEYMGMT", "private-keyring", "%s", kr_priv,
442 "key-not-found", "%s", tag, A_END);
52c03a2a 443 goto done_1;
444 }
445
446 /* --- Look up the key type in the table --- */
447
fc5f4823 448 if ((ko = keykg(&kf, k, &ty)) == 0) {
4d36660a
MW
449 a_warn("KEYMGMT", "private-keyring",
450 "%s", kr_priv, "key", "%s", t.buf,
451 "unknown-group-type", "%s", ty, A_END);
fc5f4823 452 goto done_1;
52c03a2a 453 }
52c03a2a 454
455 /* --- Load the key --- */
456
4d36660a
MW
457 if (ko->loadpriv(*kd, &g, &x, &t, &e)) {
458 a_warn("KEYMGMT", "private-keyring",
459 "%s", kr_priv, "key", "%s", t.buf,
460 "*%s", e.buf, A_END);
52c03a2a 461 goto done_1;
462 }
463
464 /* --- Check that the key is sensible --- */
465
4d36660a
MW
466 if ((err = G_CHECK(g, &rand_global)) != 0) {
467 a_warn("KEYMGMT", "private-keyring",
468 "%s", kr_priv, "key", "%s", t.buf,
469 "bad-group", "%s", err, A_END);
52c03a2a 470 goto done_1;
471 }
472
b5c45da1 473 /* --- Collect the algorithms --- */
474
4d36660a
MW
475 if (algs_get(&a, &e, &kf, k) ||
476 algs_check(&a, &e, g)) {
477 a_warn("KEYMGMT", "private-keyring",
478 "%s", kr_priv, "key", "%s", t.buf,
479 "*%s", e.buf, A_END);
b5c45da1 480 goto done_1;
481 }
482
52c03a2a 483 /* --- Good, we're happy --- *
484 *
e04c2d50 485 * Dodginess! We change the group over here, but don't free any old group
52c03a2a 486 * elements. This assumes that the new group is basically the same as the
487 * old one, and will happily adopt the existing elements. If it isn't,
488 * then we lose badly. Check this, then.
489 */
490
491 if (gg) {
492 if (!group_samep(g, gg)) {
4d36660a
MW
493 a_warn("KEYMGMT", "private-keyring",
494 "%s", kr_priv, "key", "%s", t.buf,
495 "changed-group", A_END);
52c03a2a 496 goto done_1;
410c8acf 497 }
52c03a2a 498 G_DESTROYGROUP(gg);
410c8acf 499 }
52c03a2a 500 if (kpriv)
501 mp_drop(kpriv);
502
9317aa92
MW
503 if (kpub)
504 G_DESTROY(g, kpub);
505 kpub = G_CREATE(g);
506 G_EXP(g, kpub, g->g, x);
de7bd20b 507 indexsz = mp_octets(g->r);
9317aa92 508
52c03a2a 509 /* --- Dump out the group --- */
510
511 IF_TRACING(T_KEYMGMT, {
512 trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", t.buf);
513 IF_TRACING(T_CRYPTO, {
514 trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
515 trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
516 trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
b5c45da1 517 trace(T_CRYPTO, "crypto: cipher = %s", a.c->name);
518 trace(T_CRYPTO, "crypto: mgf = %s", a.mgf->name);
519 trace(T_CRYPTO, "crypto: hash = %s", a.h->name);
520 trace(T_CRYPTO, "crypto: mac = %s/%lu",
521 a.m->name, (unsigned long)a.tagsz * 8);
52c03a2a 522 })
523 })
524
525 /* --- Success! --- */
526
527 gg = g; g = 0;
b5c45da1 528 algs = a;
52c03a2a 529 kpriv = x; x = 0;
530 rc = 0;
531
532 /* --- Tidy up --- */
533
534done_1:
535 key_close(&kf);
536done_0:
537 dstr_destroy(&t);
4d36660a 538 dstr_destroy(&e);
52c03a2a 539 if (x) mp_drop(x);
540 if (g) G_DESTROYGROUP(g);
410c8acf 541 return (rc);
542}
543
544/* --- @loadpub@ --- *
545 *
546 * Arguments: @dstr *d@ = string to write errors to
547 *
548 * Returns: Zero if OK, nonzero on error.
549 *
550 * Use: Reloads the public keyring.
551 */
552
4d36660a 553static int loadpub(void)
410c8acf 554{
555 key_file *kf = CREATE(key_file);
556
4d36660a 557 if (key_open(kf, kr_pub, KOPEN_READ, keymoan, "public")) {
410c8acf 558 DESTROY(kf);
559 return (-1);
560 }
561 kf_pub = kf;
562 T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
563 return (0);
564}
565
de014da6 566/* --- @km_reload@ --- *
410c8acf 567 *
568 * Arguments: ---
569 *
570 * Returns: Zero if OK, nonzero to force reloading of keys.
571 *
de014da6 572 * Use: Checks the keyrings to see if they need reloading.
410c8acf 573 */
574
de014da6 575int km_reload(void)
410c8acf 576{
410c8acf 577 key_file *kf;
578 int reload = 0;
579
580 /* --- Check the private key first --- */
581
582 if (fwatch_update(&w_priv, kr_priv)) {
583 T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
4d36660a 584 if (!loadpriv())
410c8acf 585 reload = 1;
410c8acf 586 }
587
588 /* --- Now check the public keys --- */
589
590 if (fwatch_update(&w_pub, kr_pub)) {
591 T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
592 kf = kf_pub;
4d36660a 593 if (!loadpub()) {
410c8acf 594 reload = 1;
595 key_close(kf);
596 DESTROY(kf);
597 }
598 }
599
600 /* --- Done --- */
601
602 return (reload);
603}
604
605/* --- @km_init@ --- *
606 *
607 * Arguments: @const char *priv@ = private keyring file
608 * @const char *pub@ = public keyring file
609 * @const char *tag@ = tag to load
610 *
611 * Returns: ---
612 *
613 * Use: Initializes, and loads the private key.
614 */
615
616void km_init(const char *priv, const char *pub, const char *tag)
617{
b5c45da1 618 const gchash *const *hh;
410c8acf 619
620 kr_priv = priv;
621 kr_pub = pub;
622 tag_priv = tag;
623 fwatch_init(&w_priv, kr_priv);
624 fwatch_init(&w_pub, kr_pub);
625
b5c45da1 626 for (hh = ghashtab; *hh; hh++) {
627 if ((*hh)->hashsz > MAXHASHSZ) {
628 die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
629 (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
630 }
631 }
632
4d36660a
MW
633 if (loadpriv() || loadpub())
634 exit(EXIT_FAILURE);
410c8acf 635}
636
637/* --- @km_getpubkey@ --- *
638 *
639 * Arguments: @const char *tag@ = public key tag to load
52c03a2a 640 * @ge *kpub@ = where to put the public key
00e64b67 641 * @time_t *t_exp@ = where to put the expiry time
410c8acf 642 *
643 * Returns: Zero if OK, nonzero if it failed.
644 *
645 * Use: Fetches a public key from the keyring.
646 */
647
52c03a2a 648int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
410c8acf 649{
00e64b67 650 key *k;
d59d4fe9 651 key_data **kd;
52c03a2a 652 dstr t = DSTR_INIT;
4d36660a 653 dstr e = DSTR_INIT;
fc5f4823 654 const kgops *ko;
4d36660a 655 const char *ty;
52c03a2a 656 group *g = 0;
657 ge *p = 0;
b5c45da1 658 algswitch a;
52c03a2a 659 int rc = -1;
660
661 /* --- Find the key --- */
662
663 if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
4d36660a
MW
664 a_warn("KEYMGMT", "public-keyring", "%s", kr_pub,
665 "key-not-found", "%s", tag, A_END);
52c03a2a 666 goto done;
667 }
410c8acf 668
52c03a2a 669 /* --- Look up the key type in the table --- */
670
fc5f4823 671 if ((ko = keykg(kf_pub, k, &ty)) == 0) {
4d36660a
MW
672 a_warn("KEYMGMT", "public-keyring",
673 "%s", kr_pub, "key", "%s", t.buf,
674 "unknown-group-type", "%s", ty, A_END);
fc5f4823 675 goto done;
52c03a2a 676 }
52c03a2a 677
678 /* --- Load the key --- */
679
4d36660a
MW
680 if (ko->loadpub(*kd, &g, &p, &t, &e)) {
681 a_warn("KEYMGMT", "public-keyring",
682 "%s", kr_pub, "key", "%s", t.buf,
683 "*%s", e.buf, A_END);
52c03a2a 684 goto done;
685 }
686
687 /* --- Ensure that the group is correct --- *
688 *
e04c2d50 689 * Dodginess! We assume that if this works, our global group is willing to
52c03a2a 690 * adopt this public element. Probably reasonable.
691 */
692
693 if (!group_samep(gg, g)) {
4d36660a
MW
694 a_warn("KEYMGMT", "public-keyring",
695 "%s", kr_pub, "key", "%s", t.buf,
696 "*%s", e.buf, A_END);
52c03a2a 697 goto done;
698 }
699
700 /* --- Check the public group element --- */
701
702 if (group_check(gg, p)) {
4d36660a
MW
703 a_warn("KEYMGMT", "public-keyring",
704 "%s", kr_pub, "key", "%s", t.buf,
f43df819
MW
705 "bad-public-group-element",
706 A_END);
52c03a2a 707 goto done;
410c8acf 708 }
52c03a2a 709
b5c45da1 710 /* --- Check the algorithms --- */
711
4d36660a
MW
712 if (algs_get(&a, &e, kf_pub, k)) {
713 a_warn("KEYMGMT", "public-keyring",
714 "%s", kr_pub, "key", "%s", t.buf,
715 "*%s", e.buf, A_END);
b5c45da1 716 goto done;
717 }
3cdc3f3a 718 if (!algs_samep(&a, &algs)) {
4d36660a
MW
719 a_warn("KEYMGMT", "public-keyring",
720 "%s", kr_pub, "key", "%s", t.buf,
721 "algorithm-mismatch", A_END);
b5c45da1 722 goto done;
723 }
724
52c03a2a 725 /* --- Dump the public key --- */
726
410c8acf 727 IF_TRACING(T_KEYMGMT, {
52c03a2a 728 trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
729 trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
410c8acf 730 })
52c03a2a 731
732 /* --- OK, accept the public key --- */
733
00e64b67 734 *t_exp = k->exp;
52c03a2a 735 G_COPY(gg, kpub, p);
736 rc = 0;
737
738 /* --- Tidy up --- */
739
740done:
741 if (p) G_DESTROY(g, p);
742 if (g) G_DESTROYGROUP(g);
743 dstr_destroy(&t);
4d36660a 744 dstr_destroy(&e);
52c03a2a 745 return (rc);
410c8acf 746}
747
748/*----- That's all, folks -------------------------------------------------*/