3 * $Id: key-flags.c,v 1.3 2004/03/28 01:58:47 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.3 2004/03/28 01:58:47 mdw
34 * Generate, store and retreive elliptic curve keys.
36 * Revision 1.2 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
39 * Revision 1.1 1999/12/22 15:47:48 mdw
40 * Major key-management revision.
44 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
50 #include <mLib/dstr.h>
54 /*----- Data structures ---------------------------------------------------*/
56 typedef struct key_flags
{
61 /*----- Flags table -------------------------------------------------------*/
63 typedef struct flagent
{
69 static flagent flagtab
[] = {
71 /* --- Encoding types --- */
73 { "binary", KENC_BINARY
, KF_ENCMASK
},
74 { "integer", KENC_MP
, KF_ENCMASK
},
75 { "struct", KENC_STRUCT
, KF_ENCMASK
},
76 { "encrypt", KENC_ENCRYPT
, KF_ENCMASK
},
77 { "string", KENC_STRING
, KF_ENCMASK
},
78 { "ec", KENC_EC
, KF_ENCMASK
},
80 /* --- Classes of keys --- */
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
},
89 /* --- Other flags --- */
91 { "burn", KF_BURN
, KF_BURN
},
92 { "-burn", 0, KF_BURN
},
94 /* --- End marker --- */
99 /*----- Main code ---------------------------------------------------------*/
101 /* --- @key_readflags@ --- *
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
108 * Returns: Zero if all went well, nonzero if there was an error.
110 * Use: Reads a flag string.
113 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
115 unsigned f
= 0, m
= 0;
118 size_t sz
= strcspn(p
, ",:");
121 /* --- Look up the string in the flags table --- */
123 for (e
= flagtab
; e
->name
; e
++) {
124 if (strncmp(e
->name
, p
, sz
) == 0) {
125 if (e
->name
[sz
] == 0) {
129 return (KERR_BADFLAGS
);
135 return (KERR_BADFLAGS
);
137 /* --- Adjust the flag words --- *
139 * Ensure that the flags set are disjoint.
143 return (KERR_BADFLAGS
);
147 if (*p
== 0 || *p
== ':')
152 /* --- Report the results --- */
156 if (pp
) *pp
= (char *)p
;
160 /* --- @key_writeflags@ --- *
162 * Arguments: @unsigned f@ = flags to write
163 * @dstr *d@ = pointer to destination string
167 * Use: Emits a flags word as a string representation.
170 void key_writeflags(unsigned f
, dstr
*d
)
176 for (e
= flagtab
; e
->name
; e
++) {
177 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
187 /* --- @key_match@ --- *
189 * Arguments: @key_data *k@ = pointer to key data block
190 * @const key_filter *kf@ = pointer to filter block
192 * Returns: Nonzero if the key matches the filter.
194 * Use: Checks whether a key matches a filter.
197 int key_match(key_data
*k
, const key_filter
*kf
)
204 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
205 return ((k
->e
& kf
->m
) == kf
->f
);
207 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
208 if (key_match(&ks
->k
, kf
))
214 /*----- That's all, folks -------------------------------------------------*/