3 * $Id: keymgmt.c,v 1.6 2004/04/18 18:08:11 mdw Exp $
5 * Key loading and storing
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE 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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
39 /*----- Static variables --------------------------------------------------*/
41 static key_file
*kf_pub
;
42 static const char *kr_priv
, *kr_pub
, *tag_priv
;
43 static fwatch w_priv
, w_pub
;
45 /*----- Key groups --------------------------------------------------------*/
47 typedef struct kgops
{
49 const char *(*loadpriv
)(key_data
*, group
**, mp
**, dstr
*);
50 const char *(*loadpub
)(key_data
*, group
**, ge
**, dstr
*);
53 /* --- Diffie-Hellman --- */
55 static const char *kgdh_priv(key_data
*kd
, group
**g
, mp
**x
, dstr
*t
)
57 key_packstruct kps
[DH_PRIVFETCHSZ
];
63 kp
= key_fetchinit(dh_privfetch
, kps
, &dp
);
64 if ((rc
= key_unpack(kp
, kd
, t
)) != 0) {
68 *g
= group_prime(&dp
.dp
);
76 static const char *kgdh_pub(key_data
*kd
, group
**g
, ge
**p
, dstr
*t
)
78 key_packstruct kps
[DH_PUBFETCHSZ
];
84 kp
= key_fetchinit(dh_pubfetch
, kps
, &dp
);
85 if ((rc
= key_unpack(kp
, kd
, t
)) != 0) {
89 *g
= group_prime(&dp
.dp
);
91 if (G_FROMINT(*g
, *p
, dp
.y
)) {
92 e
= "bad public value";
101 static const kgops kgdh_ops
= { "tripe-dh", kgdh_priv
, kgdh_pub
};
103 /* --- Elliptic curve --- */
105 static const char *kgec_priv(key_data
*kd
, group
**g
, mp
**x
, dstr
*t
)
107 key_packstruct kps
[EC_PRIVFETCHSZ
];
114 kp
= key_fetchinit(ec_privfetch
, kps
, &ep
);
115 if ((rc
= key_unpack(kp
, kd
, t
)) != 0) {
116 e
= key_strerror(rc
);
119 if ((e
= ec_getinfo(&ei
, ep
.cstr
)) != 0)
129 static const char *kgec_pub(key_data
*kd
, group
**g
, ge
**p
, dstr
*t
)
131 key_packstruct kps
[EC_PUBFETCHSZ
];
138 kp
= key_fetchinit(ec_pubfetch
, kps
, &ep
);
139 if ((rc
= key_unpack(kp
, kd
, t
)) != 0) {
140 e
= key_strerror(rc
);
143 if ((e
= ec_getinfo(&ei
, ep
.cstr
)) != 0)
147 if (G_FROMEC(*g
, *p
, &ep
.p
)) {
148 e
= "bad public point";
157 static const kgops kgec_ops
= { "tripe-ec", kgec_priv
, kgec_pub
};
159 /* --- Table of supported key types --- */
161 static const kgops
*kgtab
[] = { &kgdh_ops
, &kgec_ops
, 0 };
163 /*----- Algswitch stuff ---------------------------------------------------*/
165 /* --- @algs_get@ --- *
167 * Arguments: @algswitch *a@ = where to put the algorithms
168 * @key_file *kf@ = key file (for some stupid reason)
169 * @key *k@ = key to inspect
171 * Returns: Null if OK, or an error message.
173 * Use: Extracts an algorithm choice from a key.
176 static const char *algs_get(algswitch
*a
, key_file
*kf
, key
*k
)
183 #define FAIL(msg) do { e = msg; goto done; } while (0)
185 if ((p
= key_getattr(kf
, k
, "cipher")) == 0)
187 if ((a
->c
= gcipher_byname(p
)) == 0)
188 FAIL("unknown cipher");
190 if ((p
= key_getattr(kf
, k
, "hash")) == 0)
192 if ((a
->h
= ghash_byname(p
)) == 0)
193 FAIL("unknown hash function");
195 if ((p
= key_getattr(kf
, k
, "mgf")) != 0) {
197 dstr_putf(&d
, "%s-mgf");
200 if ((a
->mgf
= gcipher_byname(p
)) == 0)
201 FAIL("unknown MGF cipher");
203 if ((p
= key_getattr(kf
, k
, "mac")) != 0) {
206 if ((q
= strchr(d
.buf
, '/')) != 0)
208 if ((a
->m
= gmac_byname(d
.buf
)) == 0)
209 FAIL("unknown message authentication code");
211 a
->tagsz
= a
->m
->hashsz
;
213 unsigned long n
= strtoul(q
, &q
, 0);
214 if (*q
) FAIL("bad tag length string");
215 if (n
%8 || n
> ~(size_t)0) FAIL("bad tag length");
220 dstr_putf(&d
, "%s-hmac", a
->h
->name
);
221 if ((a
->m
= gmac_byname(d
.buf
)) == 0)
222 FAIL("failed to derive HMAC from hash function");
223 a
->tagsz
= a
->h
->hashsz
/2;
232 /* --- @algs_check@ --- *
234 * Arguments: @algswitch *a@ = a choice of algorithms
235 * @const group *g@ = the group we're working in
237 * Returns: Null if OK, or an error message.
239 * Use: Checks an algorithm choice for sensibleness. This also
240 * derives some useful information from the choices, and you
241 * must call this before committing the algorithm selection
242 * for use by @keyset@ functions.
245 static const char *algs_check(algswitch
*a
, const group
*g
)
247 /* --- Derive the key sizes --- *
249 * Must ensure that we have non-empty keys. This isn't ideal, but it
250 * provides a handy sanity check.
253 a
->hashsz
= a
->h
->hashsz
;
254 if ((a
->cksz
= keysz(a
->hashsz
, a
->c
->keysz
)) == 0)
255 return ("no key size found for cipher");
256 if ((a
->mksz
= keysz(a
->hashsz
, a
->m
->keysz
)) == 0)
257 return ("no key size found for MAC");
259 /* --- Ensure that the tag size is sane --- */
261 if (a
->tagsz
> a
->m
->hashsz
) return ("tag length too large");
263 /* --- Ensure the MGF accepts hashes as keys --- */
265 if (keysz(a
->hashsz
, a
->mgf
->keysz
) != a
->hashsz
)
266 return ("MGF not suitable -- restrictive key schedule");
268 /* --- All ship-shape and Bristol-fashion --- */
273 /* --- @algs_samep@ --- *
275 * Arguments: @const algswitch *a, *aa@ = two algorithm selections
277 * Returns: Nonzero if the two selections are the same.
279 * Use: Checks sameness of algorithm selections: used to ensure that
280 * peers are using sensible algorithms.
283 static int algs_samep(const algswitch
*a
, const algswitch
*aa
)
285 return (a
->c
== aa
->c
&& a
->mgf
== aa
->mgf
&& a
->h
== aa
->h
&&
286 a
->m
== aa
->m
&& a
->tagsz
== aa
->tagsz
);
289 /*----- Main code ---------------------------------------------------------*/
291 /* --- @keymoan@ --- *
293 * Arguments: @const char *file@ = name of the file
294 * @int line@ = line number in file
295 * @const char *msg@ = error message
296 * @void *p@ = argument pointer
300 * Use: Reports an error message about loading a key file.
303 static void keymoan(const char *file
, int line
, const char *msg
, void *p
)
304 { a_warn("%s:%i: error: %s", file
, line
, msg
); }
306 /* --- @loadpriv@ --- *
308 * Arguments: @dstr *d@ = string to write errors in
310 * Returns: Zero if OK, nonzero on error.
312 * Use: Loads the private key from its keyfile.
315 static int loadpriv(dstr
*d
)
328 /* --- Open the private key file --- */
330 if (key_open(&kf
, kr_priv
, KOPEN_READ
, keymoan
, 0)) {
331 dstr_putf(d
, "error reading private keyring `%s': %s",
332 kr_priv
, strerror(errno
));
336 /* --- Find the private key --- */
338 if (key_qtag(&kf
, tag_priv
, &t
, &k
, &kd
)) {
339 dstr_putf(d
, "private key `%s' not found in keyring `%s'",
344 /* --- Look up the key type in the table --- */
346 for (ko
= kgtab
; *ko
; ko
++) {
347 if (strcmp((*ko
)->ty
, k
->type
) == 0)
350 dstr_putf(d
, "private key `%s' has unknown type `%s'", t
.buf
, k
->type
);
354 /* --- Load the key --- */
356 if ((e
= (*ko
)->loadpriv(kd
, &g
, &x
, &t
)) != 0) {
357 dstr_putf(d
, "error reading private key `%s': %s", t
.buf
, e
);
361 /* --- Check that the key is sensible --- */
363 if ((e
= G_CHECK(g
, &rand_global
)) != 0) {
364 dstr_putf(d
, "bad group in private key `%s': %s", t
.buf
, e
);
368 /* --- Collect the algorithms --- */
370 if ((e
= algs_get(&a
, &kf
, k
)) != 0 ||
371 (e
= algs_check(&a
, g
)) != 0) {
372 dstr_putf(d
, "bad symmetric algorithm selection in private key `%s': %s",
377 /* --- Good, we're happy --- *
379 * Dodginess! We change the group over here, but don't free any old group
380 * elements. This assumes that the new group is basically the same as the
381 * old one, and will happily adopt the existing elements. If it isn't,
382 * then we lose badly. Check this, then.
386 if (!group_samep(g
, gg
)) {
387 dstr_putf(d
, "private key `%s' has different group", t
.buf
);
395 /* --- Dump out the group --- */
397 IF_TRACING(T_KEYMGMT
, {
398 trace(T_KEYMGMT
, "keymgmt: extracted private key `%s'", t
.buf
);
399 IF_TRACING(T_CRYPTO
, {
400 trace(T_CRYPTO
, "crypto: r = %s", mpstr(g
->r
));
401 trace(T_CRYPTO
, "crypto: h = %s", mpstr(g
->h
));
402 trace(T_CRYPTO
, "crypto: x = %s", mpstr(x
));
403 trace(T_CRYPTO
, "crypto: cipher = %s", a
.c
->name
);
404 trace(T_CRYPTO
, "crypto: mgf = %s", a
.mgf
->name
);
405 trace(T_CRYPTO
, "crypto: hash = %s", a
.h
->name
);
406 trace(T_CRYPTO
, "crypto: mac = %s/%lu",
407 a
.m
->name
, (unsigned long)a
.tagsz
* 8);
411 /* --- Success! --- */
418 /* --- Tidy up --- */
425 if (g
) G_DESTROYGROUP(g
);
429 /* --- @loadpub@ --- *
431 * Arguments: @dstr *d@ = string to write errors to
433 * Returns: Zero if OK, nonzero on error.
435 * Use: Reloads the public keyring.
438 static int loadpub(dstr
*d
)
440 key_file
*kf
= CREATE(key_file
);
442 if (key_open(kf
, kr_pub
, KOPEN_READ
, keymoan
, 0)) {
443 dstr_putf(d
, "error reading public keyring `%s': %s",
444 kr_pub
, strerror(errno
));
449 T( trace(T_KEYMGMT
, "keymgmt: loaded public keyring `%s'", kr_pub
); )
453 /* --- @km_interval@ --- *
457 * Returns: Zero if OK, nonzero to force reloading of keys.
459 * Use: Called on the interval timer to perform various useful jobs.
462 int km_interval(void)
468 /* --- Check the private key first --- */
470 if (fwatch_update(&w_priv
, kr_priv
)) {
471 T( trace(T_KEYMGMT
, "keymgmt: private keyring updated: reloading..."); )
474 a_warn("%s -- ignoring changes", d
.buf
);
479 /* --- Now check the public keys --- */
481 if (fwatch_update(&w_pub
, kr_pub
)) {
482 T( trace(T_KEYMGMT
, "keymgmt: public keyring updated: reloading..."); )
486 a_warn("%s -- ignoring changes", d
.buf
);
499 /* --- @km_init@ --- *
501 * Arguments: @const char *priv@ = private keyring file
502 * @const char *pub@ = public keyring file
503 * @const char *tag@ = tag to load
507 * Use: Initializes, and loads the private key.
510 void km_init(const char *priv
, const char *pub
, const char *tag
)
513 const gchash
*const *hh
;
518 fwatch_init(&w_priv
, kr_priv
);
519 fwatch_init(&w_pub
, kr_pub
);
521 for (hh
= ghashtab
; *hh
; hh
++) {
522 if ((*hh
)->hashsz
> MAXHASHSZ
) {
523 die(EXIT_FAILURE
, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
524 (*hh
)->name
, (unsigned long)(*hh
)->hashsz
, MAXHASHSZ
);
530 die(EXIT_FAILURE
, "%s", d
.buf
);
532 die(EXIT_FAILURE
, "%s", d
.buf
);
535 /* --- @km_getpubkey@ --- *
537 * Arguments: @const char *tag@ = public key tag to load
538 * @ge *kpub@ = where to put the public key
539 * @time_t *t_exp@ = where to put the expiry time
541 * Returns: Zero if OK, nonzero if it failed.
543 * Use: Fetches a public key from the keyring.
546 int km_getpubkey(const char *tag
, ge
*kpub
, time_t *t_exp
)
558 /* --- Find the key --- */
560 if (key_qtag(kf_pub
, tag
, &t
, &k
, &kd
)) {
561 a_warn("public key `%s' not found in keyring `%s'", tag
, kr_pub
);
565 /* --- Look up the key type in the table --- */
567 for (ko
= kgtab
; *ko
; ko
++) {
568 if (strcmp((*ko
)->ty
, k
->type
) == 0)
571 a_warn("public key `%s' has unknown type `%s'", t
.buf
, k
->type
);
575 /* --- Load the key --- */
577 if ((e
= (*ko
)->loadpub(kd
, &g
, &p
, &t
)) != 0) {
578 a_warn("error reading public key `%s': %s", t
.buf
, e
);
582 /* --- Ensure that the group is correct --- *
584 * Dodginess! We assume that if this works, our global group is willing to
585 * adopt this public element. Probably reasonable.
588 if (!group_samep(gg
, g
)) {
589 a_warn("public key `%s' has incorrect group", t
.buf
);
593 /* --- Check the public group element --- */
595 if (group_check(gg
, p
)) {
596 a_warn("public key `%s' has bad public group element", t
.buf
);
600 /* --- Check the algorithms --- */
602 if ((e
= algs_get(&a
, kf_pub
, k
)) != 0) {
603 a_warn("public key `%s' has bad algorithm selection: %s", t
.buf
, e
);
606 if (!algs_samep(&a
, &algs
)) {
607 a_warn("public key `%s' specifies different algorithms", t
.buf
);
611 /* --- Dump the public key --- */
613 IF_TRACING(T_KEYMGMT
, {
614 trace(T_KEYMGMT
, "keymgmt: extracted public key `%s'", t
.buf
);
615 trace(T_CRYPTO
, "crypto: p = %s", gestr(gg
, p
));
618 /* --- OK, accept the public key --- */
624 /* --- Tidy up --- */
627 if (p
) G_DESTROY(g
, p
);
628 if (g
) G_DESTROYGROUP(g
);
633 /*----- That's all, folks -------------------------------------------------*/