gwenhywfar 5.14.1
genkey.c
Go to the documentation of this file.
1/***************************************************************************
2 begin : Tue May 03 2005
3 copyright : (C) 2005-2010 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * Please see toplevel file COPYING for license details *
8 ***************************************************************************/
9
10#ifdef HAVE_CONFIG_H
11# include <config.h>
12#endif
13
14#include "globals.h"
15
16#include <gwenhywfar/debug.h>
17#include <gwenhywfar/ct.h>
18#include <gwenhywfar/ctplugin.h>
19#include <gwenhywfar/text.h>
20
21
22
23
24
25
26int genKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
27{
28 GWEN_DB_NODE *db;
29 const char *ttype;
30 const char *tname;
32 unsigned int keyId;
33 unsigned int keySize;
35 int rv;
36 const char *s;
37 const GWEN_ARGS args[]= {
38 {
40 GWEN_ArgsType_Int, /* type */
41 "keyId", /* name */
42 1, /* minnum */
43 1, /* maxnum */
44 "k", /* short option */
45 "key", /* long option */
46 "Key id", /* short description */
47 "Key id" /* long description */
48 },
49 {
51 GWEN_ArgsType_Char, /* type */
52 "tokenType", /* name */
53 1, /* minnum */
54 1, /* maxnum */
55 "t", /* short option */
56 "ttype", /* long option */
57 "Specify the crypt token type", /* short description */
58 "Specify the crypt token type" /* long description */
59 },
60 {
62 GWEN_ArgsType_Char, /* type */
63 "tokenName", /* name */
64 0, /* minnum */
65 1, /* maxnum */
66 "n", /* short option */
67 "tname", /* long option */
68 "Specify the crypt token name", /* short description */
69 "Specify the crypt token name" /* long description */
70 },
71 {
73 GWEN_ArgsType_Char, /* type */
74 "algo", /* name */
75 0, /* minnum */
76 1, /* maxnum */
77 "a", /* short option */
78 "algo", /* long option */
79 "Specify the algorithm", /* short description */
80 "Specify the algorithm" /* long description */
81 },
82 {
84 GWEN_ArgsType_Int, /* type */
85 "keysize", /* name */
86 0, /* minnum */
87 1, /* maxnum */
88 "s", /* short option */
89 "keysize", /* long option */
90 "Key size in bytes", /* short description */
91 "Key size in bytes" /* long description */
92 },
93 {
94 0, /* flags */
95 GWEN_ArgsType_Int, /* type */
96 "exp65537", /* name */
97 0, /* minnum */
98 1, /* maxnum */
99 "e", /* short option */
100 "exp65537", /* long option */
101 "Use default exponent of 65537", /* short description */
102 "Use default exponent of 65537" /* long description */
103 },
104 {
106 GWEN_ArgsType_Int, /* type */
107 "help", /* name */
108 0, /* minnum */
109 0, /* maxnum */
110 "h", /* short option */
111 "help", /* long option */
112 "Show this help screen", /* short description */
113 "Show this help screen" /* long description */
114 }
115 };
116
117 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
118 rv=GWEN_Args_Check(argc, argv, 1,
120 args,
121 db);
122 if (rv==GWEN_ARGS_RESULT_ERROR) {
123 fprintf(stderr, "ERROR: Could not parse arguments\n");
124 return 1;
125 }
126 else if (rv==GWEN_ARGS_RESULT_HELP) {
127 GWEN_BUFFER *ubuf;
128
129 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
130 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
131 fprintf(stderr, "ERROR: Could not create help string\n");
132 return 1;
133 }
134 fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
135 GWEN_Buffer_free(ubuf);
136 return 0;
137 }
138
139 keyId=GWEN_DB_GetIntValue(db, "keyId", 0, 0);
140 if (keyId==0) {
141 DBG_ERROR(0, "Key Id must not be zero");
142 return 1;
143 }
144
145 s=GWEN_DB_GetCharValue(db, "algo", 0, "rsa");
146 if (!s) {
147 DBG_ERROR(0, "Algo id missing");
148 return 1;
149 }
151 if (algoId==GWEN_Crypt_CryptAlgoId_Unknown) {
152 DBG_ERROR(0, "Bad algo id [%s]", s);
153 return 1;
154 }
155
156 keySize=GWEN_DB_GetIntValue(db, "keySize", 0, 96);
157 if (keySize==0) {
158 DBG_ERROR(0, "Invalid key size %d", keySize);
159 return 1;
160 }
161
162 ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
163 assert(ttype);
164
165 tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
166
167 /* get crypt token */
168 ct=getCryptToken(ttype, tname);
169 if (ct==0)
170 return 3;
171
172 if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
174
175 if (GWEN_DB_GetIntValue(db, "exp65537", 0, 0))
177
178 /* open crypt token for use */
179 rv=GWEN_Crypt_Token_Open(ct, 1, 0);
180 if (rv) {
181 DBG_ERROR(0, "Could not open token");
182 return 3;
183 }
184 else {
186
189
190 /* generate key */
191 rv=GWEN_Crypt_Token_GenerateKey(ct, keyId, algo, 0);
192 if (rv) {
194 "Error generating key (%d)", rv);
196 return 3;
197 }
198 }
199
200 /* close crypt token */
201 rv=GWEN_Crypt_Token_Close(ct, 0, 0);
202 if (rv) {
203 DBG_ERROR(0, "Could not close token");
204 return 3;
205 }
206
207 fprintf(stderr, "Key %d (%s, %d bytes) successfully generated.\n",
208 keyId, GWEN_Crypt_CryptAlgoId_toString(algoId), keySize);
209
210 return 0;
211}
212
213
214
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition buffer.c:42
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
void GWEN_Crypt_CryptAlgo_SetChunkSize(GWEN_CRYPT_CRYPTALGO *a, int s)
Definition cryptalgo.c:327
void GWEN_Crypt_CryptAlgo_free(GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:234
GWEN_CRYPT_CRYPTALGO * GWEN_Crypt_CryptAlgo_new(GWEN_CRYPT_CRYPTALGOID id, GWEN_CRYPT_CRYPTMODE m)
Definition cryptalgo.c:113
GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgoId_fromString(const char *s)
Definition cryptalgo.c:28
GWEN_CRYPT_CRYPTALGOID
Definition cryptalgo.h:52
struct GWEN_CRYPT_CRYPTALGO GWEN_CRYPT_CRYPTALGO
Definition cryptalgo.h:20
@ GWEN_Crypt_CryptMode_None
Definition cryptalgo.h:57
int GWEN_Crypt_Token_GenerateKey(GWEN_CRYPT_TOKEN *ct, uint32_t keyId, const GWEN_CRYPT_CRYPTALGO *a, uint32_t gid)
Definition ct.c:515
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
#define GWEN_CRYPT_TOKEN_MODE_EXP_65537
Definition ct.h:62
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,...)
Definition debug.h:97
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
int genKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition genkey.c:26
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition buffer.h:38
#define GWEN_LOGDOMAIN
Definition logger.h:32
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
#define GWEN_ARGS_RESULT_ERROR
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
#define GWEN_ARGS_FLAGS_HELP
#define GWEN_ARGS_FLAGS_LAST
@ GWEN_ArgsOutType_Txt
@ GWEN_ArgsType_Int
@ GWEN_ArgsType_Char
#define GWEN_ARGS_RESULT_HELP