chiark / gitweb /
Add consistency checking for public keys.
[catacomb] / keycheck.c
1 /* -*-c-*-
2  *
3  * $Id: keycheck.c,v 1.1 2001/02/03 16:08:24 mdw Exp $
4  *
5  * Framework for checking consistency of keys
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
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.
18  * 
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.
23  * 
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
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: keycheck.c,v $
33  * Revision 1.1  2001/02/03 16:08:24  mdw
34  * Add consistency checking for public keys.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <stdarg.h>
41
42 #include <mLib/dstr.h>
43
44 #include "keycheck.h"
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 /* --- @keycheck_report@ --- *
49  *
50  * Arguments:   @keycheck *kc@ = keycheck state
51  *              @unsigned sev@ = severity of this report
52  *              @const char *msg@ = message to send along
53  *              @...@ = things to fill the message in with
54  *
55  * Returns:     Zero to continue, or nonzero to stop and give up.
56  *
57  * Use:         Reports a message to the user function.
58  */
59
60 int keycheck_report(keycheck *kc, unsigned sev, const char *msg, ...)
61 {
62   int rc;
63   va_list ap;
64   dstr d = DSTR_INIT;
65
66   kc->sev[sev]++;
67   va_start(ap, msg);
68   dstr_vputf(&d, msg, ap);
69   va_end(ap);
70   rc = kc->func ? kc->func(sev, d.buf, kc->p) : 0;
71   dstr_destroy(&d);
72   return (rc);  
73 }
74
75 /* --- @keycheck_init@ --- *
76  *
77  * Arguments:   @keycheck *kc@ = pointer to block to initialize
78  *              @int (*func)(unsigned sev, const char *msg, void *p)@ =
79  *                      handler function for problems
80  *              @void *p@ = pointer to give to handler
81  *
82  * Returns:     ---
83  *
84  * Use:         Initializes a key checking context.
85  */
86
87 void keycheck_init(keycheck *kc,
88                    int (*func)(unsigned /*sev*/,
89                                const char */*msg*/,
90                                void */*p*/),
91                    void *p)
92 {
93   unsigned i;
94   kc->func = func;
95   kc->p = p;
96   for (i = 0; i < KCSEV_MAX; i++)
97     kc->sev[i] = 0;
98 }
99
100 /* --- @keycheck_allclear@ --- *
101  *
102  * Arguments:   @keycheck *kc@ = pointer to keycheck context
103  *              @unsigned sev@ = minimum severity to care about
104  *
105  * Returns:     Nonzero if no problems of @sev@ or above were noticed.
106  */
107
108 int keycheck_allclear(keycheck *kc, unsigned sev)
109 {
110   while (sev < KCSEV_MAX) {
111     if (kc->sev[sev])
112       return (0);
113     sev++;
114   }
115   return (1);
116 }
117
118 /*----- That's all, folks -------------------------------------------------*/