gwenhywfar  5.10.1
activatekey.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Thu Jun 24 2010
3  copyright : (C) 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 
26 int activateKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
27 {
28  GWEN_DB_NODE *db;
29  const char *ttype;
30  const char *tname;
31  GWEN_CRYPT_TOKEN *ct;
32  unsigned int keyId;
33  int rv;
34  const char *s;
35  const GWEN_ARGS args[]= {
36  {
37  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
38  GWEN_ArgsType_Int, /* type */
39  "keyId", /* name */
40  1, /* minnum */
41  1, /* maxnum */
42  "k", /* short option */
43  "key", /* long option */
44  "Key id", /* short description */
45  "Key id" /* long description */
46  },
47  {
48  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
49  GWEN_ArgsType_Char, /* type */
50  "tokenType", /* name */
51  1, /* minnum */
52  1, /* maxnum */
53  "t", /* short option */
54  "ttype", /* long option */
55  "Specify the crypt token type", /* short description */
56  "Specify the crypt token type" /* long description */
57  },
58  {
59  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
60  GWEN_ArgsType_Char, /* type */
61  "tokenName", /* name */
62  0, /* minnum */
63  1, /* maxnum */
64  "n", /* short option */
65  "tname", /* long option */
66  "Specify the crypt token name", /* short description */
67  "Specify the crypt token name" /* long description */
68  },
69  {
71  GWEN_ArgsType_Int, /* type */
72  "help", /* name */
73  0, /* minnum */
74  0, /* maxnum */
75  "h", /* short option */
76  "help", /* long option */
77  "Show this help screen", /* short description */
78  "Show this help screen" /* long description */
79  }
80  };
81 
82  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
83  rv=GWEN_Args_Check(argc, argv, 1,
85  args,
86  db);
87  if (rv==GWEN_ARGS_RESULT_ERROR) {
88  fprintf(stderr, "ERROR: Could not parse arguments\n");
89  return 1;
90  }
91  else if (rv==GWEN_ARGS_RESULT_HELP) {
92  GWEN_BUFFER *ubuf;
93 
94  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
95  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
96  fprintf(stderr, "ERROR: Could not create help string\n");
97  return 1;
98  }
99  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
100  GWEN_Buffer_free(ubuf);
101  return 0;
102  }
103 
104  keyId=GWEN_DB_GetIntValue(db, "keyId", 0, 0);
105  if (keyId==0) {
106  DBG_ERROR(0, "Key Id must not be zero");
107  return 1;
108  }
109 
110  s=GWEN_DB_GetCharValue(db, "algo", 0, "rsa");
111  if (!s) {
112  DBG_ERROR(0, "Algo id missing");
113  return 1;
114  }
115  ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
116  assert(ttype);
117 
118  tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
119 
120  /* get crypt token */
121  ct=getCryptToken(ttype, tname);
122  if (ct==0)
123  return 3;
124 
125  if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
127 
128  /* open crypt token for use */
129  rv=GWEN_Crypt_Token_Open(ct, 1, 0);
130  if (rv) {
131  DBG_ERROR(0, "Could not open token");
132  return 3;
133  }
134  else {
135  /* activate key */
136  rv=GWEN_Crypt_Token_ActivateKey(ct, keyId, 0);
137  if (rv) {
139  "Error activating key (%d)", rv);
140  return 3;
141  }
142  }
143 
144  /* close crypt token */
145  rv=GWEN_Crypt_Token_Close(ct, 0, 0);
146  if (rv) {
147  DBG_ERROR(0, "Could not close token");
148  return 3;
149  }
150 
151  fprintf(stderr, "Key %d successfully activated.\n", keyId);
152 
153  return 0;
154 }
155 
156 
157 
#define GWEN_CRYPT_TOKEN_MODE_FORCE_PIN_ENTRY
Definition: ct.h:59
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
Definition: gcttool/main.c:71
int GWEN_Crypt_Token_Close(GWEN_CRYPT_TOKEN *ct, int abandon, uint32_t gid)
Definition: ct.c:265
#define GWEN_LOGDOMAIN
Definition: logger.h:35
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
int activateKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: activatekey.c:26
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
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
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
struct GWEN_CRYPT_TOKEN GWEN_CRYPT_TOKEN
Definition: ct.h:19
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
int GWEN_Crypt_Token_Open(GWEN_CRYPT_TOKEN *ct, int admin, uint32_t gid)
Definition: ct.c:222
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
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_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1163
int GWEN_Crypt_Token_ActivateKey(GWEN_CRYPT_TOKEN *ct, uint32_t id, uint32_t gid)
Definition: ct.c:550
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
void GWEN_Crypt_Token_AddModes(GWEN_CRYPT_TOKEN *ct, uint32_t f)
Definition: ct.c:202