Miscellaneous constification.
[u/mdw/catacomb] / key-flags.c
1 /* -*-c-*-
2 *
3 * $Id: key-flags.c,v 1.4 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Reading and writing key flag strings
6 *
7 * (c) 1999 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: key-flags.c,v $
33 * Revision 1.4 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.3 2004/03/28 01:58:47 mdw
37 * Generate, store and retreive elliptic curve keys.
38 *
39 * Revision 1.2 2000/02/12 18:21:02 mdw
40 * Overhaul of key management (again).
41 *
42 * Revision 1.1 1999/12/22 15:47:48 mdw
43 * Major key-management revision.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <mLib/bits.h>
53 #include <mLib/dstr.h>
54
55 #include "key.h"
56
57 /*----- Data structures ---------------------------------------------------*/
58
59 typedef struct key_flags {
60 unsigned f;
61 unsigned m;
62 } key_flags;
63
64 /*----- Flags table -------------------------------------------------------*/
65
66 typedef struct flagent {
67 const char *name;
68 unsigned f;
69 unsigned m;
70 } flagent;
71
72 static const flagent flagtab[] = {
73
74 /* --- Encoding types --- */
75
76 { "binary", KENC_BINARY, KF_ENCMASK },
77 { "integer", KENC_MP, KF_ENCMASK },
78 { "struct", KENC_STRUCT, KF_ENCMASK },
79 { "encrypt", KENC_ENCRYPT, KF_ENCMASK },
80 { "string", KENC_STRING, KF_ENCMASK },
81 { "ec", KENC_EC, KF_ENCMASK },
82
83 /* --- Classes of keys --- */
84
85 { "shared", KCAT_SHARE, KF_CATMASK },
86 { "public", KCAT_PUB, KF_CATMASK },
87 { "private", KCAT_PRIV, KF_CATMASK },
88 { "symmetric", KCAT_SYMM, KF_CATMASK },
89 { "secret", 0, KF_NONSECRET },
90 { "-secret", KF_NONSECRET, KF_NONSECRET },
91
92 /* --- Other flags --- */
93
94 { "burn", KF_BURN, KF_BURN },
95 { "-burn", 0, KF_BURN },
96
97 /* --- End marker --- */
98
99 { 0, 0, 0 }
100 };
101
102 /*----- Main code ---------------------------------------------------------*/
103
104 /* --- @key_readflags@ --- *
105 *
106 * Arguments: @const char *p@ = pointer to string to read
107 * @char **pp@ = where to store the end pointer
108 * @unsigned *ff@ = where to store the flags
109 * @unsigned *mm@ = where to store the mask
110 *
111 * Returns: Zero if all went well, nonzero if there was an error.
112 *
113 * Use: Reads a flag string.
114 */
115
116 int key_readflags(const char *p, char **pp, unsigned *ff, unsigned *mm)
117 {
118 unsigned f = 0, m = 0;
119
120 for (;;) {
121 size_t sz = strcspn(p, ",:");
122 const flagent *e, *ee = 0;
123
124 /* --- Look up the string in the flags table --- */
125
126 for (e = flagtab; e->name; e++) {
127 if (strncmp(e->name, p, sz) == 0) {
128 if (e->name[sz] == 0) {
129 ee = e;
130 break;
131 } else if (ee)
132 return (KERR_BADFLAGS);
133 else
134 ee = e;
135 }
136 }
137 if (!ee)
138 return (KERR_BADFLAGS);
139
140 /* --- Adjust the flag words --- *
141 *
142 * Ensure that the flags set are disjoint.
143 */
144
145 if (m & ee->m)
146 return (KERR_BADFLAGS);
147 m |= ee->m;
148 f |= ee->f;
149 p += sz;
150 if (*p == 0 || *p == ':')
151 break;
152 p++;
153 }
154
155 /* --- Report the results --- */
156
157 if (ff) *ff = f;
158 if (mm) *mm = m;
159 if (pp) *pp = (char *)p;
160 return (0);
161 }
162
163 /* --- @key_writeflags@ --- *
164 *
165 * Arguments: @unsigned f@ = flags to write
166 * @dstr *d@ = pointer to destination string
167 *
168 * Returns: ---
169 *
170 * Use: Emits a flags word as a string representation.
171 */
172
173 void key_writeflags(unsigned f, dstr *d)
174 {
175 int del = 0;
176 const flagent *e;
177 unsigned m = 0;
178
179 for (e = flagtab; e->name; e++) {
180 if (m & e->m || e->name[0] == '-' || (f & e->m) != e->f)
181 continue;
182 if (del)
183 DPUTC(d, ',');
184 DPUTS(d, e->name);
185 m |= e->m;
186 del = 1;
187 }
188 }
189
190 /* --- @key_match@ --- *
191 *
192 * Arguments: @key_data *k@ = pointer to key data block
193 * @const key_filter *kf@ = pointer to filter block
194 *
195 * Returns: Nonzero if the key matches the filter.
196 *
197 * Use: Checks whether a key matches a filter.
198 */
199
200 int key_match(key_data *k, const key_filter *kf)
201 {
202 sym_iter i;
203 key_struct *ks;
204
205 if (!kf)
206 return (1);
207 if ((k->e & KF_ENCMASK) != KENC_STRUCT)
208 return ((k->e & kf->m) == kf->f);
209
210 for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
211 if (key_match(&ks->k, kf))
212 return (1);
213 }
214 return (0);
215 }
216
217 /*----- That's all, folks -------------------------------------------------*/