gwenhywfar  4.99.8beta
setsignseq.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 
29 int setSignSeq(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
30  GWEN_DB_NODE *db;
31  const char *ttype;
32  const char *tname;
33  GWEN_CRYPT_TOKEN *ct;
34  unsigned int keyId;
35  unsigned int seq;
36  int rv;
37  const GWEN_ARGS args[]={
38  {
39  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
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 (0 for any)", /* short description */
47  "Key id (0 for any)" /* long description */
48  },
49  {
50  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
51  GWEN_ArgsType_Int, /* type */
52  "seq", /* name */
53  1, /* minnum */
54  1, /* maxnum */
55  "s", /* short option */
56  "seq", /* long option */
57  "New sequence counter value", /* short description */
58  "New sequence counter value" /* long description */
59  },
60  {
61  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
62  GWEN_ArgsType_Char, /* type */
63  "tokenType", /* name */
64  1, /* minnum */
65  1, /* maxnum */
66  "t", /* short option */
67  "ttype", /* long option */
68  "Specify the crypt token type", /* short description */
69  "Specify the crypt token type" /* long description */
70  },
71  {
72  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
73  GWEN_ArgsType_Char, /* type */
74  "tokenName", /* name */
75  0, /* minnum */
76  1, /* maxnum */
77  "n", /* short option */
78  "tname", /* long option */
79  "Specify the crypt token name", /* short description */
80  "Specify the crypt token name" /* long description */
81  },
82  {
84  GWEN_ArgsType_Int, /* type */
85  "help", /* name */
86  0, /* minnum */
87  0, /* maxnum */
88  "h", /* short option */
89  "help", /* long option */
90  "Show this help screen", /* short description */
91  "Show this help screen" /* long description */
92  }
93  };
94 
95  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
96  rv=GWEN_Args_Check(argc, argv, 1,
98  args,
99  db);
100  if (rv==GWEN_ARGS_RESULT_ERROR) {
101  fprintf(stderr, "ERROR: Could not parse arguments\n");
102  return 1;
103  }
104  else if (rv==GWEN_ARGS_RESULT_HELP) {
105  GWEN_BUFFER *ubuf;
106 
107  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
108  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
109  fprintf(stderr, "ERROR: Could not create help string\n");
110  return 1;
111  }
112  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
113  GWEN_Buffer_free(ubuf);
114  return 0;
115  }
116 
117  keyId=GWEN_DB_GetIntValue(db, "keyId", 0, 0);
118  seq=GWEN_DB_GetIntValue(db, "seq", 0, 0);
119 
120  ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
121  assert(ttype);
122 
123  tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
124 
125  /* get crypt token */
126  ct=getCryptToken(ttype, tname);
127  if (ct==0)
128  return 3;
129 
130  if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
132 
133  /* open crypt token for use */
134  rv=GWEN_Crypt_Token_Open(ct, 0, 0);
135  if (rv) {
136  DBG_ERROR(0, "Could not open token");
137  return 3;
138  }
139  else {
140  const GWEN_CRYPT_TOKEN_KEYINFO *ki;
141 
142  ki=GWEN_Crypt_Token_GetKeyInfo(ct, keyId,
144  0);
145  if (ki) {
147 
150  rv=GWEN_Crypt_Token_SetKeyInfo(ct, keyId, nki, 0);
152  if (rv) {
153  DBG_ERROR(GWEN_LOGDOMAIN, "Unable to set sign counter (%d)", rv);
154  return 4;
155  }
156  }
157  }
158 
159  /* close crypt token */
160  rv=GWEN_Crypt_Token_Close(ct, 0, 0);
161  if (rv) {
162  DBG_ERROR(0, "Could not close token");
163  return 3;
164  }
165 
166  return 0;
167 }
168 
169 
170 
#define GWEN_CRYPT_TOKEN_MODE_FORCE_PIN_ENTRY
Definition: ct.h:59
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
void GWEN_Crypt_Token_KeyInfo_free(GWEN_CRYPT_TOKEN_KEYINFO *p_struct)
Definition: ct_keyinfo.c:77
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
Definition: gcttool/main.c:69
int GWEN_Crypt_Token_Close(GWEN_CRYPT_TOKEN *ct, int abandon, uint32_t gid)
Definition: ct.c:247
int GWEN_Crypt_Token_SetKeyInfo(GWEN_CRYPT_TOKEN *ct, uint32_t id, const GWEN_CRYPT_TOKEN_KEYINFO *ki, uint32_t gid)
Definition: ct.c:319
#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:38
#define GWEN_CRYPT_TOKEN_KEYFLAGS_HASSIGNCOUNTER
Definition: ct_keyinfo.h:98
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
GWEN_CRYPT_TOKEN_KEYINFO * GWEN_Crypt_Token_KeyInfo_dup(const GWEN_CRYPT_TOKEN_KEYINFO *p_src)
Definition: ct_keyinfo.c:107
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
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:897
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1260
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
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:206
struct GWEN_CRYPT_TOKEN_KEYINFO GWEN_CRYPT_TOKEN_KEYINFO
Definition: ct_keyinfo.h:121
#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:1048
void GWEN_Crypt_Token_KeyInfo_SetSignCounter(GWEN_CRYPT_TOKEN_KEYINFO *p_struct, uint32_t p_src)
Definition: ct_keyinfo.c:355
int setSignSeq(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: setsignseq.c:29
#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:188
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:299