gwenhywfar  4.99.8beta
sign.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 signArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
25  GWEN_DB_NODE *db;
26  const char *aname;
27  const char *keyFile;
28  const char *signer;
29  GWEN_DB_NODE *dbKey;
30  GWEN_CRYPT_KEY *key;
31  GWEN_SAR *sr;
32  int rv;
33  const GWEN_ARGS args[]={
34  {
35  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
36  GWEN_ArgsType_Char, /* type */
37  "archive", /* name */
38  1, /* minnum */
39  1, /* maxnum */
40  "a", /* short option */
41  "archive", /* long option */
42  "Specify the archive file name", /* short description */
43  "Specify the archive file name" /* long description */
44  },
45  {
46  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
47  GWEN_ArgsType_Char, /* type */
48  "keyfile", /* name */
49  1, /* minnum */
50  1, /* maxnum */
51  "k", /* short option */
52  "keyfile", /* long option */
53  "Specify the keyfile to use", /* short description */
54  "Specify the keyfile to use" /* long description */
55  },
56  {
57  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
58  GWEN_ArgsType_Char, /* type */
59  "signer", /* name */
60  0, /* minnum */
61  1, /* maxnum */
62  "s", /* short option */
63  "signer", /* long option */
64  "Specify the signer", /* short description */
65  "Specify the signer" /* long description */
66  },
67  {
69  GWEN_ArgsType_Int, /* type */
70  "help", /* name */
71  0, /* minnum */
72  0, /* maxnum */
73  "h", /* short option */
74  "help", /* long option */
75  "Show this help screen", /* short description */
76  "Show this help screen" /* long description */
77  }
78  };
79 
80  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
81  rv=GWEN_Args_Check(argc, argv, 1,
83  args,
84  db);
85  if (rv==GWEN_ARGS_RESULT_ERROR) {
86  fprintf(stderr, "ERROR: Could not parse arguments\n");
87  return 1;
88  }
89  else if (rv==GWEN_ARGS_RESULT_HELP) {
90  GWEN_BUFFER *ubuf;
91 
92  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
93  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
94  fprintf(stderr, "ERROR: Could not create help string\n");
95  return 1;
96  }
97  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
98  GWEN_Buffer_free(ubuf);
99  return 0;
100  }
101 
102  aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
103  assert(aname);
104 
105  keyFile=GWEN_DB_GetCharValue(db, "keyFile", 0, NULL);
106  assert(keyFile);
107 
108  signer=GWEN_DB_GetCharValue(db, "signer", 0, "Signer");
109 
110  dbKey=GWEN_DB_Group_new("keyfile");
111  rv=GWEN_DB_ReadFile(dbKey, keyFile, GWEN_DB_FLAGS_DEFAULT);
112  if (rv<0) {
113  fprintf(stderr, "ERROR: Error reading keyfile [%s] (%d)\n", keyFile, rv);
114  return 2;
115  }
116  key=GWEN_Crypt_KeyRsa_fromDb(dbKey);
117  if (key==NULL) {
118  fprintf(stderr, "ERROR: Error decoding keyfile [%s] (%d)\n", keyFile, rv);
119  return 2;
120  }
122 
123  sr=GWEN_Sar_new();
124  rv=GWEN_Sar_OpenArchive(sr, aname,
127  if (rv<0) {
128  fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
129  return 2;
130  }
131  else {
132  GWEN_CRYPTMGR *cm;
133 
134  cm=GWEN_CryptMgrKeys_new(signer, key, NULL, NULL, 1);
135 
136  /* sign */
137  rv=GWEN_Sar_Sign(sr, cm);
138  if (rv<0) {
139  fprintf(stderr, "ERROR: Error signing archive (%d)\n", rv);
140  GWEN_CryptMgr_free(cm);
141  GWEN_Sar_CloseArchive(sr, 1);
142  GWEN_Sar_free(sr);
143  return 2;
144  }
145  GWEN_CryptMgr_free(cm);
146 
147  /* close archive */
148  rv=GWEN_Sar_CloseArchive(sr, 0);
149  if (rv<0) {
150  fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
151  GWEN_Sar_CloseArchive(sr, 1);
152  GWEN_Sar_free(sr);
153  return 2;
154  }
155 
156  return 0;
157  }
158 }
159 
160 
161 
int GWEN_Sar_Sign(GWEN_SAR *sr, GWEN_CRYPTMGR *cm)
Definition: sar.c:1906
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define GWEN_SYNCIO_FILE_FLAGS_WRITE
Definition: syncio_file.h:54
#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:176
#define NULL
Definition: binreloc.c:290
#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_ARGS_RESULT_HELP
Definition: src/base/args.h:58
void GWEN_Crypt_KeyRsa_AddFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
Definition: cryptkeyrsa.c:982
#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:622
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
void GWEN_Sar_free(GWEN_SAR *sr)
Definition: sar.c:71
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: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
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:48
#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:130
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 signArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: sign.c:24
GWENHYWFAR_API int GWEN_DB_ReadFile(GWEN_DB_NODE *n, const char *fname, uint32_t dbflags)
Definition: dbrw.c:990
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
Definition: db.c:131
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