gwenhywfar 5.14.1
mkkey.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
24int 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 {
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 {
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,
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
#define NULL
Definition binreloc.c:300
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition buffer.c:42
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition buffer.c:89
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition buffer.c:235
void GWEN_Crypt_Key_free(GWEN_CRYPT_KEY *k)
Definition cryptkey.c:154
struct GWEN_CRYPT_KEY GWEN_CRYPT_KEY
Definition cryptkey.h:26
int GWEN_Crypt_KeyRsa_GeneratePair(unsigned int nbytes, int use65537e, GWEN_CRYPT_KEY **pPubKey, GWEN_CRYPT_KEY **pSecretKey)
int GWEN_Crypt_KeyRsa_toDb(const GWEN_CRYPT_KEY *k, GWEN_DB_NODE *db, int pub)
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_Group_new(const char *name)
Definition db.c:173
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition db.c:1381
void GWEN_DB_Group_free(GWEN_DB_NODE *n)
Definition db.c:421
GWENHYWFAR_API int GWEN_DB_WriteFile(GWEN_DB_NODE *n, const char *fname, uint32_t dbflags)
Definition dbrw.c:535
#define GWEN_DB_FLAGS_DEFAULT
Definition db.h:168
struct GWEN_DB_NODE GWEN_DB_NODE
Definition db.h:228
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition buffer.h:38
int mkArchiveKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition mkkey.c:24
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
#define GWEN_ARGS_RESULT_ERROR
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
#define GWEN_ARGS_FLAGS_HELP
#define GWEN_ARGS_FLAGS_LAST
@ GWEN_ArgsOutType_Txt
@ GWEN_ArgsType_Int
@ GWEN_ArgsType_Char
#define GWEN_ARGS_RESULT_HELP