gwenhywfar  5.10.1
verify.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sat Jun 25 2011
3  copyright : (C) 2011 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/sar.h>
18 #include <gwenhywfar/cryptmgrkeys.h>
19 #include <gwenhywfar/cryptkeyrsa.h>
20 
21 
22 
23 
24 int verifyArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
25 {
26  GWEN_DB_NODE *db;
27  const char *aname;
28  const char *keyFile;
29  const char *signer;
30  GWEN_DB_NODE *dbKey;
31  GWEN_CRYPT_KEY *key;
32  GWEN_SAR *sr;
33  int rv;
34  const GWEN_ARGS args[]= {
35  {
36  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
37  GWEN_ArgsType_Char, /* type */
38  "archive", /* name */
39  1, /* minnum */
40  1, /* maxnum */
41  "a", /* short option */
42  "archive", /* long option */
43  "Specify the archive file name", /* short description */
44  "Specify the archive file name" /* long description */
45  },
46  {
47  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
48  GWEN_ArgsType_Char, /* type */
49  "keyfile", /* name */
50  1, /* minnum */
51  1, /* maxnum */
52  "k", /* short option */
53  "keyfile", /* long option */
54  "Specify the keyfile to use", /* short description */
55  "Specify the keyfile to use" /* long description */
56  },
57  {
58  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
59  GWEN_ArgsType_Char, /* type */
60  "signer", /* name */
61  0, /* minnum */
62  1, /* maxnum */
63  "s", /* short option */
64  "signer", /* long option */
65  "Specify the signer", /* short description */
66  "Specify the signer" /* long description */
67  },
68  {
70  GWEN_ArgsType_Int, /* type */
71  "help", /* name */
72  0, /* minnum */
73  0, /* maxnum */
74  "h", /* short option */
75  "help", /* long option */
76  "Show this help screen", /* short description */
77  "Show this help screen" /* long description */
78  }
79  };
80 
81  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
82  rv=GWEN_Args_Check(argc, argv, 1,
84  args,
85  db);
86  if (rv==GWEN_ARGS_RESULT_ERROR) {
87  fprintf(stderr, "ERROR: Could not parse arguments\n");
88  return 1;
89  }
90  else if (rv==GWEN_ARGS_RESULT_HELP) {
91  GWEN_BUFFER *ubuf;
92 
93  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
94  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
95  fprintf(stderr, "ERROR: Could not create help string\n");
96  return 1;
97  }
98  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
99  GWEN_Buffer_free(ubuf);
100  return 0;
101  }
102 
103  aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
104  assert(aname);
105 
106  keyFile=GWEN_DB_GetCharValue(db, "keyFile", 0, NULL);
107  assert(keyFile);
108 
109  signer=GWEN_DB_GetCharValue(db, "Signer", 0, "Signer");
110 
111  dbKey=GWEN_DB_Group_new("keyfile");
112  rv=GWEN_DB_ReadFile(dbKey, keyFile, GWEN_DB_FLAGS_DEFAULT);
113  if (rv<0) {
114  fprintf(stderr, "ERROR: Error reading keyfile [%s] (%d)\n", keyFile, rv);
115  return 2;
116  }
117  key=GWEN_Crypt_KeyRsa_fromDb(dbKey);
118  if (key==NULL) {
119  fprintf(stderr, "ERROR: Error decoding keyfile [%s] (%d)\n", keyFile, rv);
120  return 2;
121  }
123 
124  sr=GWEN_Sar_new();
125  rv=GWEN_Sar_OpenArchive(sr, aname,
128  if (rv<0) {
129  fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
130  return 2;
131  }
132  else {
133  GWEN_CRYPTMGR *cm;
134 
135  cm=GWEN_CryptMgrKeys_new(NULL, NULL, signer, key, 1);
136 
137  /* verify */
138  rv=GWEN_Sar_Verify(sr, cm);
139  if (rv<0) {
140  fprintf(stderr, "ERROR: Error verifying archive (%d)\n", rv);
141  GWEN_CryptMgr_free(cm);
142  GWEN_Sar_CloseArchive(sr, 1);
143  GWEN_Sar_free(sr);
144  return 2;
145  }
146  GWEN_CryptMgr_free(cm);
147 
148  /* close archive */
149  rv=GWEN_Sar_CloseArchive(sr, 0);
150  if (rv<0) {
151  fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
152  GWEN_Sar_CloseArchive(sr, 1);
153  GWEN_Sar_free(sr);
154  return 2;
155  }
156 
157  fprintf(stdout, "Signature is valid.\n");
158  return 0;
159  }
160 }
161 
162 
163 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define GWEN_SYNCIO_FILE_FLAGS_READ
Definition: syncio_file.h:53
GWEN_SAR * GWEN_Sar_new(void)
Definition: sar.c:50
int GWEN_Sar_CloseArchive(GWEN_SAR *sr, int abandon)
Definition: sar.c:181
#define NULL
Definition: binreloc.c:300
#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
void GWEN_Crypt_KeyRsa_AddFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
Definition: cryptkeyrsa.c:1018
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromDb(GWEN_DB_NODE *db)
Definition: cryptkeyrsa.c:648
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
int GWEN_Sar_Verify(GWEN_SAR *sr, GWEN_CRYPTMGR *cm)
Definition: sar.c:2128
#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
void GWEN_Sar_free(GWEN_SAR *sr)
Definition: sar.c:73
struct GWEN_CRYPT_KEY GWEN_CRYPT_KEY
Definition: cryptkey.h:26
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
GWEN_CRYPTMGR * GWEN_CryptMgrKeys_new(const char *localName, GWEN_CRYPT_KEY *localKey, const char *peerName, GWEN_CRYPT_KEY *peerKey, int ownKeys)
Definition: cryptmgrkeys.c:33
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
void GWEN_CryptMgr_free(GWEN_CRYPTMGR *cm)
Definition: cryptmgr.c:49
#define GWEN_CRYPT_KEYRSA_FLAGS_DIRECTSIGN
Definition: cryptkeyrsa.h:22
int GWEN_Sar_OpenArchive(GWEN_SAR *sr, const char *aname, GWEN_SYNCIO_FILE_CREATIONMODE cm, uint32_t acc)
Definition: sar.c:134
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
GWENHYWFAR_API int GWEN_DB_ReadFile(GWEN_DB_NODE *n, const char *fname, uint32_t dbflags)
Definition: dbrw.c:1003
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
Definition: db.c:173
struct GWEN_CRYPTMGR GWEN_CRYPTMGR
Definition: cryptmgr.h:64
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
struct GWEN_SAR GWEN_SAR
Definition: sar.h:37
int verifyArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: verify.c:24