3 * $Id: key-flags.c,v 1.4 2004/04/02 01:03:49 mdw Exp $
5 * Reading and writing key flag strings
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: key-flags.c,v $
33 * Revision 1.4 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.3 2004/03/28 01:58:47 mdw
37 * Generate, store and retreive elliptic curve keys.
39 * Revision 1.2 2000/02/12 18:21:02 mdw
40 * Overhaul of key management (again).
42 * Revision 1.1 1999/12/22 15:47:48 mdw
43 * Major key-management revision.
47 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/bits.h>
53 #include <mLib/dstr.h>
57 /*----- Data structures ---------------------------------------------------*/
59 typedef struct key_flags
{
64 /*----- Flags table -------------------------------------------------------*/
66 typedef struct flagent
{
72 static const flagent flagtab
[] = {
74 /* --- Encoding types --- */
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
},
83 /* --- Classes of keys --- */
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
},
92 /* --- Other flags --- */
94 { "burn", KF_BURN
, KF_BURN
},
95 { "-burn", 0, KF_BURN
},
97 /* --- End marker --- */
102 /*----- Main code ---------------------------------------------------------*/
104 /* --- @key_readflags@ --- *
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
111 * Returns: Zero if all went well, nonzero if there was an error.
113 * Use: Reads a flag string.
116 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
118 unsigned f
= 0, m
= 0;
121 size_t sz
= strcspn(p
, ",:");
122 const flagent
*e
, *ee
= 0;
124 /* --- Look up the string in the flags table --- */
126 for (e
= flagtab
; e
->name
; e
++) {
127 if (strncmp(e
->name
, p
, sz
) == 0) {
128 if (e
->name
[sz
] == 0) {
132 return (KERR_BADFLAGS
);
138 return (KERR_BADFLAGS
);
140 /* --- Adjust the flag words --- *
142 * Ensure that the flags set are disjoint.
146 return (KERR_BADFLAGS
);
150 if (*p
== 0 || *p
== ':')
155 /* --- Report the results --- */
159 if (pp
) *pp
= (char *)p
;
163 /* --- @key_writeflags@ --- *
165 * Arguments: @unsigned f@ = flags to write
166 * @dstr *d@ = pointer to destination string
170 * Use: Emits a flags word as a string representation.
173 void key_writeflags(unsigned f
, dstr
*d
)
179 for (e
= flagtab
; e
->name
; e
++) {
180 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
190 /* --- @key_match@ --- *
192 * Arguments: @key_data *k@ = pointer to key data block
193 * @const key_filter *kf@ = pointer to filter block
195 * Returns: Nonzero if the key matches the filter.
197 * Use: Checks whether a key matches a filter.
200 int key_match(key_data
*k
, const key_filter
*kf
)
207 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
208 return ((k
->e
& kf
->m
) == kf
->f
);
210 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
211 if (key_match(&ks
->k
, kf
))
217 /*----- That's all, folks -------------------------------------------------*/