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