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
mkArchiveKey(GWEN_DB_NODE * dbArgs,int argc,char ** argv)24 int mkArchiveKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
25 {
26 GWEN_DB_NODE *db;
27 const char *keyFile;
28 GWEN_DB_NODE *dbKey;
29 GWEN_CRYPT_KEY *pubKey;
30 GWEN_CRYPT_KEY *privKey;
31 int rv;
32 const GWEN_ARGS args[]= {
33 {
34 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
35 GWEN_ArgsType_Char, /* type */
36 "keyfile", /* name */
37 1, /* minnum */
38 1, /* maxnum */
39 "k", /* short option */
40 "keyfile", /* long option */
41 "Specify the keyfile to use", /* short description */
42 "Specify the keyfile to use" /* long description */
43 },
44 {
45 GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
46 GWEN_ArgsType_Int, /* type */
47 "help", /* name */
48 0, /* minnum */
49 0, /* maxnum */
50 "h", /* short option */
51 "help", /* long option */
52 "Show this help screen", /* short description */
53 "Show this help screen" /* long description */
54 }
55 };
56
57 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
58 rv=GWEN_Args_Check(argc, argv, 1,
59 GWEN_ARGS_MODE_ALLOW_FREEPARAM,
60 args,
61 db);
62 if (rv==GWEN_ARGS_RESULT_ERROR) {
63 fprintf(stderr, "ERROR: Could not parse arguments\n");
64 return 1;
65 }
66 else if (rv==GWEN_ARGS_RESULT_HELP) {
67 GWEN_BUFFER *ubuf;
68
69 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
70 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
71 fprintf(stderr, "ERROR: Could not create help string\n");
72 return 1;
73 }
74 fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
75 GWEN_Buffer_free(ubuf);
76 return 0;
77 }
78
79 keyFile=GWEN_DB_GetCharValue(db, "keyFile", 0, NULL);
80 assert(keyFile);
81
82 /* create key */
83 rv=GWEN_Crypt_KeyRsa_GeneratePair(512, 1, &pubKey, &privKey);
84 if (rv<0) {
85 fprintf(stderr, "ERROR: Error generating key pair (%d)\n", rv);
86 return rv;
87 }
88
89 dbKey=GWEN_DB_Group_new("keyfile");
90 rv=GWEN_Crypt_KeyRsa_toDb(privKey, dbKey, 0);
91 if (rv<0) {
92 fprintf(stderr, "ERROR: Error encoding keyfile [%s] (%d)\n", keyFile, rv);
93 return 2;
94 }
95
96 rv=GWEN_DB_WriteFile(dbKey, keyFile, GWEN_DB_FLAGS_DEFAULT);
97 if (rv<0) {
98 fprintf(stderr, "ERROR: Error writing keyfile [%s] (%d)\n", keyFile, rv);
99 return 2;
100 }
101
102 GWEN_DB_Group_free(dbKey);
103 GWEN_Crypt_Key_free(privKey);
104 GWEN_Crypt_Key_free(pubKey);
105
106 return 0;
107 }
108
109
110
111