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");
195 if ((p
= key_getattr(kf
, k
, "mgf")) == 0) {
197 dstr_putf(&d
, "%s-mgf", a
->h
->name
);
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)
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("no-hmac-for-hash");
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
)
312 /* --- @loadpriv@ --- *
314 * Arguments: @dstr *d@ = string to write errors in
316 * Returns: Zero if OK, nonzero on error.
318 * Use: Loads the private key from its keyfile.
321 static int loadpriv(dstr
*d
)
334 /* --- Open the private key file --- */
336 if (key_open(&kf
, kr_priv
, KOPEN_READ
, keymoan
, 0)) {
337 dstr_putf(d
, "error reading private keyring `%s': %s",
338 kr_priv
, strerror(errno
));
342 /* --- Find the private key --- */
344 if (key_qtag(&kf
, tag_priv
, &t
, &k
, &kd
)) {
345 dstr_putf(d
, "private key `%s' not found in keyring `%s'",
350 /* --- Look up the key type in the table --- */
352 for (ko
= kgtab
; *ko
; ko
++) {
353 if (strcmp((*ko
)->ty
, k
->type
) == 0)
356 dstr_putf(d
, "private key `%s' has unknown type `%s'", t
.buf
, k
->type
);
360 /* --- Load the key --- */
362 if ((e
= (*ko
)->loadpriv(*kd
, &g
, &x
, &t
)) != 0) {
363 dstr_putf(d
, "error reading private key `%s': %s", t
.buf
, e
);
367 /* --- Check that the key is sensible --- */
369 if ((e
= G_CHECK(g
, &rand_global
)) != 0) {
370 dstr_putf(d
, "bad group in private key `%s': %s", t
.buf
, e
);
374 /* --- Collect the algorithms --- */
376 if ((e
= algs_get(&a
, &kf
, k
)) != 0 ||
377 (e
= algs_check(&a
, g
)) != 0) {
378 dstr_putf(d
, "bad symmetric algorithm selection in private key `%s': %s",
383 /* --- Good, we're happy --- *
385 * Dodginess! We change the group over here, but don't free any old group
386 * elements. This assumes that the new group is basically the same as the
387 * old one, and will happily adopt the existing elements. If it isn't,
388 * then we lose badly. Check this, then.
392 if (!group_samep(g
, gg
)) {
393 dstr_putf(d
, "private key `%s' has different group", t
.buf
);
401 /* --- Dump out the group --- */
403 IF_TRACING(T_KEYMGMT
, {
404 trace(T_KEYMGMT
, "keymgmt: extracted private key `%s'", t
.buf
);
405 IF_TRACING(T_CRYPTO
, {
406 trace(T_CRYPTO
, "crypto: r = %s", mpstr(g
->r
));
407 trace(T_CRYPTO
, "crypto: h = %s", mpstr(g
->h
));
408 trace(T_CRYPTO
, "crypto: x = %s", mpstr(x
));
409 trace(T_CRYPTO
, "crypto: cipher = %s", a
.c
->name
);
410 trace(T_CRYPTO
, "crypto: mgf = %s", a
.mgf
->name
);
411 trace(T_CRYPTO
, "crypto: hash = %s", a
.h
->name
);
412 trace(T_CRYPTO
, "crypto: mac = %s/%lu",
413 a
.m
->name
, (unsigned long)a
.tagsz
* 8);
417 /* --- Success! --- */
424 /* --- Tidy up --- */
431 if (g
) G_DESTROYGROUP(g
);
435 /* --- @loadpub@ --- *
437 * Arguments: @dstr *d@ = string to write errors to
439 * Returns: Zero if OK, nonzero on error.
441 * Use: Reloads the public keyring.
444 static int loadpub(dstr
*d
)
446 key_file
*kf
= CREATE(key_file
);
448 if (key_open(kf
, kr_pub
, KOPEN_READ
, keymoan
, 0)) {
449 dstr_putf(d
, "error reading public keyring `%s': %s",
450 kr_pub
, strerror(errno
));
455 T( trace(T_KEYMGMT
, "keymgmt: loaded public keyring `%s'", kr_pub
); )
459 /* --- @km_reload@ --- *
463 * Returns: Zero if OK, nonzero to force reloading of keys.
465 * Use: Checks the keyrings to see if they need reloading.
474 /* --- Check the private key first --- */
476 if (fwatch_update(&w_priv
, kr_priv
)) {
477 T( trace(T_KEYMGMT
, "keymgmt: private keyring updated: reloading..."); )
480 a_warn("KEYMGMT", "bad-private-key", "%s", d
.buf
, A_END
);
485 /* --- Now check the public keys --- */
487 if (fwatch_update(&w_pub
, kr_pub
)) {
488 T( trace(T_KEYMGMT
, "keymgmt: public keyring updated: reloading..."); )
492 a_warn("KEYMGMT", "bad-public-keyring", "%s", d
.buf
, A_END
);
505 /* --- @km_init@ --- *
507 * Arguments: @const char *priv@ = private keyring file
508 * @const char *pub@ = public keyring file
509 * @const char *tag@ = tag to load
513 * Use: Initializes, and loads the private key.
516 void km_init(const char *priv
, const char *pub
, const char *tag
)
519 const gchash
*const *hh
;
524 fwatch_init(&w_priv
, kr_priv
);
525 fwatch_init(&w_pub
, kr_pub
);
527 for (hh
= ghashtab
; *hh
; hh
++) {
528 if ((*hh
)->hashsz
> MAXHASHSZ
) {
529 die(EXIT_FAILURE
, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
530 (*hh
)->name
, (unsigned long)(*hh
)->hashsz
, MAXHASHSZ
);
536 die(EXIT_FAILURE
, "%s", d
.buf
);
538 die(EXIT_FAILURE
, "%s", d
.buf
);
541 /* --- @km_getpubkey@ --- *
543 * Arguments: @const char *tag@ = public key tag to load
544 * @ge *kpub@ = where to put the public key
545 * @time_t *t_exp@ = where to put the expiry time
547 * Returns: Zero if OK, nonzero if it failed.
549 * Use: Fetches a public key from the keyring.
552 int km_getpubkey(const char *tag
, ge
*kpub
, time_t *t_exp
)
564 /* --- Find the key --- */
566 if (key_qtag(kf_pub
, tag
, &t
, &k
, &kd
)) {
567 a_warn("KEYMGMT", "public-key", "%s", tag
, "not-found", A_END
);
571 /* --- Look up the key type in the table --- */
573 for (ko
= kgtab
; *ko
; ko
++) {
574 if (strcmp((*ko
)->ty
, k
->type
) == 0)
578 "public-key", "%s", t
.buf
,
579 "unknown-type", "%s", k
->type
,
584 /* --- Load the key --- */
586 if ((e
= (*ko
)->loadpub(*kd
, &g
, &p
, &t
)) != 0) {
587 a_warn("KEYMGMT", "public-key", "%s", t
.buf
, "bad", "%s", e
, A_END
);
591 /* --- Ensure that the group is correct --- *
593 * Dodginess! We assume that if this works, our global group is willing to
594 * adopt this public element. Probably reasonable.
597 if (!group_samep(gg
, g
)) {
598 a_warn("KEYMGMT", "public-key", "%s", t
.buf
, "incorrect-group", A_END
);
602 /* --- Check the public group element --- */
604 if (group_check(gg
, p
)) {
606 "public-key", "%s", t
.buf
,
607 "bad-public-group-element",
612 /* --- Check the algorithms --- */
614 if ((e
= algs_get(&a
, kf_pub
, k
)) != 0) {
616 "public-key", "%s", t
.buf
,
617 "bad-algorithm-selection", e
,
621 if (!algs_samep(&a
, &algs
)) {
623 "public-key", "%s", t
.buf
,
624 "algorithm-mismatch",
629 /* --- Dump the public key --- */
631 IF_TRACING(T_KEYMGMT
, {
632 trace(T_KEYMGMT
, "keymgmt: extracted public key `%s'", t
.buf
);
633 trace(T_CRYPTO
, "crypto: p = %s", gestr(gg
, p
));
636 /* --- OK, accept the public key --- */
642 /* --- Tidy up --- */
645 if (p
) G_DESTROY(g
, p
);
646 if (g
) G_DESTROYGROUP(g
);
651 /*----- That's all, folks -------------------------------------------------*/