gwenhywfar 5.10.1
showkey.c
Go to the documentation of this file.
1/***************************************************************************
2 $RCSfile$
3 -------------------
4 cvs : $Id: create.c 892 2005-11-03 00:20:45Z aquamaniac $
5 begin : Tue May 03 2005
6 copyright : (C) 2005 by Martin Preuss
7 email : martin@libchipcard.de
8
9 ***************************************************************************
10 * Please see toplevel file COPYING for license details *
11 ***************************************************************************/
12
13#ifdef HAVE_CONFIG_H
14# include <config.h>
15#endif
16
17#include "globals.h"
18
19#include <gwenhywfar/debug.h>
20#include <gwenhywfar/ct.h>
21#include <gwenhywfar/ctplugin.h>
22#include <gwenhywfar/text.h>
23
24
25
26
27
28
29int showKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
30{
31 GWEN_DB_NODE *db;
32 const char *ttype;
33 const char *tname;
35 unsigned int keyId;
36 int shown=0;
37 int rv;
38 const GWEN_ARGS args[]= {
39 {
41 GWEN_ArgsType_Int, /* type */
42 "keyId", /* name */
43 0, /* minnum */
44 1, /* maxnum */
45 "k", /* short option */
46 "key", /* long option */
47 "Key id (0 for any)", /* short description */
48 "Key id (0 for any)" /* long description */
49 },
50 {
52 GWEN_ArgsType_Char, /* type */
53 "tokenType", /* name */
54 1, /* minnum */
55 1, /* maxnum */
56 "t", /* short option */
57 "ttype", /* long option */
58 "Specify the crypt token type", /* short description */
59 "Specify the crypt token type" /* long description */
60 },
61 {
63 GWEN_ArgsType_Char, /* type */
64 "tokenName", /* name */
65 0, /* minnum */
66 1, /* maxnum */
67 "n", /* short option */
68 "tname", /* long option */
69 "Specify the crypt token name", /* short description */
70 "Specify the crypt token name" /* long description */
71 },
72 {
74 GWEN_ArgsType_Int, /* type */
75 "help", /* name */
76 0, /* minnum */
77 0, /* maxnum */
78 "h", /* short option */
79 "help", /* long option */
80 "Show this help screen", /* short description */
81 "Show this help screen" /* long description */
82 }
83 };
84
85 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
86 rv=GWEN_Args_Check(argc, argv, 1,
88 args,
89 db);
90 if (rv==GWEN_ARGS_RESULT_ERROR) {
91 fprintf(stderr, "ERROR: Could not parse arguments\n");
92 return 1;
93 }
94 else if (rv==GWEN_ARGS_RESULT_HELP) {
95 GWEN_BUFFER *ubuf;
96
97 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
98 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
99 fprintf(stderr, "ERROR: Could not create help string\n");
100 return 1;
101 }
102 fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
103 GWEN_Buffer_free(ubuf);
104 return 0;
105 }
106
107 keyId=GWEN_DB_GetIntValue(db, "keyId", 0, 0);
108
109 ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
110 assert(ttype);
111
112 tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
113
114 /* get crypt token */
115 ct=getCryptToken(ttype, tname);
116 if (ct==0)
117 return 3;
118
119 if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
121
122 /* open crypt token for use */
123 rv=GWEN_Crypt_Token_Open(ct, 0, 0);
124 if (rv) {
125 DBG_ERROR(0, "Could not open token");
126 return 3;
127 }
128 else {
129 uint32_t keyIds[64];
130 uint32_t keyCount;
131 uint32_t i;
132
133 keyCount=64;
134 rv=GWEN_Crypt_Token_GetKeyIdList(ct, keyIds, &keyCount, 0);
135 if (rv<0) {
136 DBG_ERROR(0, "Error filling key list");
137 GWEN_Crypt_Token_Close(ct, 0, 0);
138 return 3;
139 }
140 for (i=0; i<keyCount; i++) {
141 if (keyId==0 || keyId==keyIds[i]) {
142 const GWEN_CRYPT_TOKEN_KEYINFO *ki;
143 uint32_t flags;
144 const char *s;
145
146 ki=GWEN_Crypt_Token_GetKeyInfo(ct, keyIds[i], 0xffffffff, 0);
147 if (ki) {
148 fprintf(stdout, "-------------------------------------------------\n");
149 fprintf(stdout, "Key %08x\n",
150 (unsigned int)GWEN_Crypt_Token_KeyInfo_GetId(ki));
151
153 if (s)
154 fprintf(stdout, "Key Descr : %s\n", s);
155
156 fprintf(stdout, "Crypt Algo : %s\n",
158 fprintf(stdout, "Key Size : %d\n", GWEN_Crypt_Token_KeyInfo_GetKeySize(ki));
159
160 fprintf(stdout, "Key Flags :");
163 fprintf(stdout, " STATUS");
165 fprintf(stdout, " MODULUS");
167 fprintf(stdout, " EXPONENT");
169 fprintf(stdout, " KEYVERSION");
171 fprintf(stdout, " KEYNUMBER");
173 fprintf(stdout, " SIGNCOUNTER");
176 fprintf(stdout, " SIGN");
178 fprintf(stdout, " VERIFY");
180 fprintf(stdout, " ENCIPHER");
182 fprintf(stdout, " DECIPHER");
183 }
184 fprintf(stdout, "\n");
185
187 fprintf(stdout, "Key Number : %d\n", GWEN_Crypt_Token_KeyInfo_GetKeyNumber(ki));
189 fprintf(stdout, "Key Version: %d\n", GWEN_Crypt_Token_KeyInfo_GetKeyVersion(ki));
191 fprintf(stdout, "Sign Cnt : %d\n", GWEN_Crypt_Token_KeyInfo_GetSignCounter(ki));
193 GWEN_BUFFER *tbuf;
194 const uint8_t *p;
195 uint32_t len;
196 int nbits;
197
198 tbuf=GWEN_Buffer_new(0, 256, 0, 1);
199 p=(const uint8_t *)GWEN_Crypt_Token_KeyInfo_GetModulusData(ki);
201
202 nbits=len*8;
203 while (len && *p==0) {
204 p++;
205 len--;
206 nbits-=8;
207 }
208 if (len) {
209 int i;
210 uint8_t mask=0x80;
211 uint8_t b=*p;
212
213 for (i=0; i<8; i++) {
214 if (b & mask)
215 break;
216 nbits--;
217 mask>>=1;
218 }
219 }
220
221 fprintf(stdout, "Modulus : (%d bits)\n", nbits);
222
223 while (len) {
224 uint32_t rl;
225
226 rl=(len>16)?16:len;
227 GWEN_Text_ToHexBuffer((const char *)p, rl, tbuf, 2, ' ', 0);
228 fprintf(stdout, " %s\n", GWEN_Buffer_GetStart(tbuf));
229 GWEN_Buffer_Reset(tbuf);
230 p+=rl;
231 len-=rl;
232 }
233 GWEN_Buffer_free(tbuf);
234 }
235
237 GWEN_BUFFER *tbuf;
238 const char *p;
239 uint32_t len;
240
241 tbuf=GWEN_Buffer_new(0, 256, 0, 1);
242 fprintf(stdout, "Exponent : \n");
245 while (len) {
246 uint32_t rl;
247
248 rl=(len>16)?16:len;
249 GWEN_Text_ToHexBuffer(p, rl, tbuf, 2, ' ', 0);
250 fprintf(stdout, " %s\n", GWEN_Buffer_GetStart(tbuf));
251 GWEN_Buffer_Reset(tbuf);
252 p+=rl;
253 len-=rl;
254 }
255 GWEN_Buffer_free(tbuf);
256 }
257
258 shown++;
259 }
260 }
261 }
262 }
263
264 /* close crypt token */
265 rv=GWEN_Crypt_Token_Close(ct, 0, 0);
266 if (rv) {
267 DBG_ERROR(0, "Could not close token");
268 return 3;
269 }
270
271 if (!shown) {
272 if (keyId==0) {
273 DBG_ERROR(0, "No key found");
274 }
275 else {
276 DBG_ERROR(0, "Key %u not found", keyId);
277 }
278 return 1;
279 }
280
281 return 0;
282}
283
284
285
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition: buffer.c:650
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
const char * GWEN_Crypt_CryptAlgoId_toString(GWEN_CRYPT_CRYPTALGOID a)
Definition: cryptalgo.c:53
const GWEN_CRYPT_TOKEN_KEYINFO * GWEN_Crypt_Token_GetKeyInfo(GWEN_CRYPT_TOKEN *ct, uint32_t id, uint32_t flags, uint32_t gid)
Definition: ct.c:320
int GWEN_Crypt_Token_GetKeyIdList(GWEN_CRYPT_TOKEN *ct, uint32_t *pIdList, uint32_t *pCount, uint32_t gid)
Definition: ct.c:301
int GWEN_Crypt_Token_Open(GWEN_CRYPT_TOKEN *ct, int admin, uint32_t gid)
Definition: ct.c:222
int GWEN_Crypt_Token_Close(GWEN_CRYPT_TOKEN *ct, int abandon, uint32_t gid)
Definition: ct.c:265
void GWEN_Crypt_Token_AddModes(GWEN_CRYPT_TOKEN *ct, uint32_t f)
Definition: ct.c:202
#define GWEN_CRYPT_TOKEN_MODE_FORCE_PIN_ENTRY
Definition: ct.h:59
struct GWEN_CRYPT_TOKEN GWEN_CRYPT_TOKEN
Definition: ct.h:19
uint32_t GWEN_Crypt_Token_KeyInfo_GetSignCounter(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:296
uint32_t GWEN_Crypt_Token_KeyInfo_GetId(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:254
uint32_t GWEN_Crypt_Token_KeyInfo_GetFlags(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:266
uint32_t GWEN_Crypt_Token_KeyInfo_GetModulusLen(const GWEN_CRYPT_TOKEN_KEYINFO *st)
Definition: ct_keyinfo.c:851
uint32_t GWEN_Crypt_Token_KeyInfo_GetKeyNumber(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:284
const uint8_t * GWEN_Crypt_Token_KeyInfo_GetExponentData(const GWEN_CRYPT_TOKEN_KEYINFO *st)
Definition: ct_keyinfo.c:871
int GWEN_Crypt_Token_KeyInfo_GetKeySize(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:272
const char * GWEN_Crypt_Token_KeyInfo_GetKeyDescr(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:260
uint32_t GWEN_Crypt_Token_KeyInfo_GetKeyVersion(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:290
int GWEN_Crypt_Token_KeyInfo_GetCryptAlgoId(const GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:278
const uint8_t * GWEN_Crypt_Token_KeyInfo_GetModulusData(const GWEN_CRYPT_TOKEN_KEYINFO *st)
Definition: ct_keyinfo.c:843
uint32_t GWEN_Crypt_Token_KeyInfo_GetExponentLen(const GWEN_CRYPT_TOKEN_KEYINFO *st)
Definition: ct_keyinfo.c:879
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASEXPONENT
Definition: ct_keyinfo.h:101
#define GWEN_CRYPT_TOKEN_KEYFLAGS_CANSIGN
Definition: ct_keyinfo.h:108
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASMODULUS
Definition: ct_keyinfo.h:100
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASACTIONFLAGS
Definition: ct_keyinfo.h:102
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASKEYNUMBER
Definition: ct_keyinfo.h:105
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASSTATUS
Definition: ct_keyinfo.h:99
#define GWEN_CRYPT_TOKEN_KEYFLAGS_CANDECIPHER
Definition: ct_keyinfo.h:111
#define GWEN_CRYPT_TOKEN_KEYFLAGS_CANENCIPHER
Definition: ct_keyinfo.h:110
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASKEYVERSION
Definition: ct_keyinfo.h:103
#define GWEN_CRYPT_TOKEN_KEYFLAGS_CANVERIFY
Definition: ct_keyinfo.h:109
struct GWEN_CRYPT_TOKEN_KEYINFO GWEN_CRYPT_TOKEN_KEYINFO
Definition: ct_keyinfo.h:127
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASSIGNCOUNTER
Definition: ct_keyinfo.h:104
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:971
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1381
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1163
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
Definition: gcttool/main.c:71
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
int showKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: showkey.c:29
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
Definition: src/base/args.c:45
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
@ GWEN_ArgsOutType_Txt
Definition: src/base/args.h:68
@ GWEN_ArgsType_Int
Definition: src/base/args.h:63
@ GWEN_ArgsType_Char
Definition: src/base/args.h:62
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
int GWEN_Text_ToHexBuffer(const char *src, unsigned l, GWEN_BUFFER *buf, unsigned int groupsize, char delimiter, int skipLeadingZeroes)
Definition: text.c:777