gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [libfints] branch master updated: Polishing keys import.


From: gnunet
Subject: [GNUnet-SVN] [libfints] branch master updated: Polishing keys import.
Date: Thu, 11 Oct 2018 12:59:18 +0200

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository libfints.

The following commit(s) were added to refs/heads/master by this push:
     new 725d142  Polishing keys import.
725d142 is described below

commit 725d142e68790de7f58578ab4470eedb4fd2d5a7
Author: Marcello Stanisci <address@hidden>
AuthorDate: Thu Oct 11 12:59:02 2018 +0200

    Polishing keys import.
---
 contrib/Makefile.am |  16 +++++--
 src/Makefile.am     |  13 ++++--
 src/libebics.c      | 129 ++++++++++++++++++++++++++++++++++++----------------
 src/tests.c         |   3 ++
 src/xmlproto.h      |   2 +-
 5 files changed, 117 insertions(+), 46 deletions(-)

diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index 008ccda..3caae57 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -7,6 +7,14 @@ xml_templates = \
   SignaturePubKeyOrderData.xml \
   ebicsNoPubKeyDigestsRequest.xml
 
+key_files = \
+  userAuthKey.pem \
+  userSigKey.pem \
+  userEncKey.pem \
+  bankAuthKey.pem \
+  bankSignKey.pem \
+  bankEncKey.pem
+
 $(xml_templates):
        ./genex.py ebics_schema/ebics_request_H004.xsd \
           ebicsRequest -o ebicsRequest.xml
@@ -19,10 +27,12 @@ $(xml_templates):
        ./genex.py ebics_schema/ebics_keymgmt_request_H004.xsd \
           ebicsNoPubKeyDigestsRequest -o ebicsNoPubKeyDigestsRequest.xml
 
-pkgdata_DATA = $(xml_templates)
+pkgdata_DATA = \
+  $(xml_templates) \
+  $(key_files)
 
-CLEANFILES = $(xml_templates)
+CLEANFILES = \
+  $(xml_templates)
 
 all: $(xml_templates)
        echo "Generating genex xml files"
-
diff --git a/src/Makefile.am b/src/Makefile.am
index a89fc2b..a7ea9cc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -31,12 +31,17 @@ libebics_CPPFLAGS = \
   $(GNUTLS_CFLAGS) \
   $(ZLIB_CFLAGS)
 
-check_PROGRAMS = tests
-TESTS = $(check_PROGRAMS)
+check_PROGRAMS = \
+  tests
+
+TESTS = \
+  $(check_PROGRAMS)
+
+tests_SOURCES = \
+  tests.c
 
-tests_SOURCES = tests.c
 tests_LDADD = \
-  -lebics \
+  libebics.la \
   -lgnunetutil
 
 tests_CPPFLAGS = \
diff --git a/src/libebics.c b/src/libebics.c
index 5cd17c7..210c88d 100644
--- a/src/libebics.c
+++ b/src/libebics.c
@@ -258,11 +258,14 @@ free_zlib ()
 
 
 /**
- * Load all keys.
+ * Load all keys.  For each filename given, the function
+ * will try to import a x509 both private and public key.
+ * However, it is not guaranteed that at the end of the
+ * import all keys are imported priv&pub.
  *
- * @param keyList
- * @param keyDir
- * @param keyFiles
+ * @param keyList Will contain the initialized keys.
+ * @param keyDir directory hosting the keys.
+ * @param keyFiles names of the files with key material.
  *
  * @returns EBICS_SUCCESS on success,
  *          EBICS_ERROR on non critical errors
@@ -273,77 +276,127 @@ EBICS_init_keymaterial (struct EBICS_Key keyList[],
                         char *keyDir,
                         const char *keyFiles[])
 {
-  LOG (EBICS_LOGLEVEL_DEBUG, "Initializing key material\n");
-
   int retv;
   int result = EBICS_SUCCESS;
+
+  LOG (EBICS_LOGLEVEL_DEBUG,
+       "Initializing key material\n");
   keyList[0].type = EBICS_KEY_NONE;
   
   for (int i = 0;
        i < EBICS_KEY_MAX_ENTRIES && keyFiles[i] != NULL;
        i++)
   {
-    size_t size = snprintf (NULL, 0,"%s/%s.pem", keyDir, keyFiles[i]) + 1;
-    assert(0 != size);
+    gnutls_x509_privkey_t privkey;
+    gnutls_pubkey_t pubkey;
+    size_t size;
+    size_t key_size;
+    struct EBICS_Key *key;
+    FILE *f;
+    char *key_content;
+
+    size = snprintf (NULL,
+                     0,
+                     "%s/%s.pem",
+                     keyDir,
+                     keyFiles[i]);
+    size++; /* account for 0-terminator */
+    assert (0 != size);
+
     char filepath[size];
-    size = snprintf (filepath, size,"%s/%s.pem", keyDir, keyFiles[i]);
+    size = snprintf (filepath,
+                     size,
+                     "%s/%s.pem",
+                     keyDir,
+                     keyFiles[i]);
+    /* no 0-terminator in the size count,  although
+     * string is 0-terminated  */
     assert (0 != size);
-    LOG (EBICS_LOGLEVEL_DEBUG, "Loading '%s' at position %d",filepath, i);
-    struct EBICS_Key *key = &keyList[i];
-    keyList[i+1].type = EBICS_KEY_NONE;
 
+    LOG (EBICS_LOGLEVEL_DEBUG,
+         "Loading '%s' at position %d",
+         filepath,
+         i);
+
+    key = &keyList[i];
+    keyList[i+1].type = EBICS_KEY_NONE;
 
-    /* TODO: Load actual keys and parse them? */
-    FILE *f = fopen(filepath, "rb");
+    f = fopen (filepath, "rb"); /* rb: Read Binary */
     if (NULL == f)
     {
-      LOG (EBICS_LOGLEVEL_ERROR, "Could not load %s",filepath);
+      LOG (EBICS_LOGLEVEL_ERROR,
+           "Could not load %s",
+           filepath);
       key->type = EBICS_KEY_NONE;
       continue;
     }
 
-    fseek(f, 0, SEEK_END);
-    size = ftell(f);
-    fseek(f, 0, SEEK_SET);
-    char *result = (char *)malloc(size);
-    size_t res = fread(result, sizeof(char), size, f);
-    assert(0 != res);
-    fclose(f);
-    LOG (EBICS_LOGLEVEL_DEBUG, "Size: %u", size);
+    fseek (f, 0, SEEK_END);
+    size = ftell (f);
+    /* bring the position again at the beginning  */
+    fseek (f, 0, SEEK_SET);
 
-    gnutls_datum_t rawkey = {result, size};
-    gnutls_x509_privkey_t privkey;
-    gnutls_x509_privkey_init(&privkey);
-    gnutls_pubkey_t pubkey;
+    key_content = (char *) malloc (size);
+
+    key_size = fread (key_content,
+                      sizeof (char),
+                      size,
+                      f);
+
+    assert (0 != key_size);
+    fclose (f);
+    LOG (EBICS_LOGLEVEL_DEBUG,
+         "Size: %u",
+         key_size);
+
+    gnutls_datum_t rawkey = {
+      key_content,
+      size };
+
+    gnutls_x509_privkey_init (&privkey);
     gnutls_pubkey_init (&pubkey);
 
-    //retv = gnutls_privkey_import_x509_raw (privkey, &rawkey, 
GNUTLS_X509_FMT_PEM, NULL, 0);
-    retv = gnutls_x509_privkey_import (privkey, &rawkey, GNUTLS_X509_FMT_PEM);
+    retv = gnutls_x509_privkey_import (privkey,
+                                       &rawkey,
+                                       GNUTLS_X509_FMT_PEM);
     if (GNUTLS_E_SUCCESS != retv)
-    {
-      LOG (EBICS_LOGLEVEL_ERROR, "Could not import a privatekey. GnuTLS Error: 
%s", gnutls_strerror (retv));
-    }
+      LOG (EBICS_LOGLEVEL_ERROR,
+           "Could not import a privatekey. GnuTLS Error: %s",
+           gnutls_strerror (retv));
     else
     {
-      LOG (EBICS_LOGLEVEL_DEBUG,"Found private key in %s!",filepath);
+      LOG (EBICS_LOGLEVEL_DEBUG,
+           "Found private key in %s!",
+           filepath);
+
       key->privatekey = privkey;
       key->type |= EBICS_KEY_RSA_PRIVATE;
     }
 
-    retv = gnutls_pubkey_import (pubkey, &rawkey, GNUTLS_X509_FMT_PEM);
+    retv = gnutls_pubkey_import (pubkey,
+                                 &rawkey,
+                                 GNUTLS_X509_FMT_PEM);
+
     if (GNUTLS_E_SUCCESS != retv)
     {
-      LOG (EBICS_LOGLEVEL_ERROR, "Could not import a publickey. GnuTLS Error: 
%s", gnutls_strerror (retv));
+      LOG (EBICS_LOGLEVEL_ERROR,
+      "Could not import the publickey. GnuTLS Error: %s",
+      gnutls_strerror (retv));
     }
     else
     {
-      LOG (EBICS_LOGLEVEL_DEBUG,"Found public key in %s!",filepath);
+      LOG (EBICS_LOGLEVEL_DEBUG,
+           "Found public key in %s!",
+           filepath);
       key->publickey = pubkey;
       key->type |= EBICS_KEY_RSA_PUBLIC;
     }
 
-    strncpy (key->name, keyFiles[i], EBICS_KEY_MAX_NAME);
-    free(result);
+    strncpy (key->name,
+             keyFiles[i],
+             EBICS_KEY_MAX_NAME);
+
+    GNUNET_free (key_content);
   }
 }
 
diff --git a/src/tests.c b/src/tests.c
index f4edeec..d40c49d 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -38,6 +38,9 @@
 int
 main (int argc, char **argv)
 {
+  unsetenv ("XDG_DATA_HOME");
+  unsetenv ("XDG_CONFIG_HOME");
+
   if (EBICS_SUCCESS != EBICS_init_library ()) 
   {
     LOG (EBICS_FATAL,
diff --git a/src/xmlproto.h b/src/xmlproto.h
index 862318a..f952499 100644
--- a/src/xmlproto.h
+++ b/src/xmlproto.h
@@ -88,7 +88,7 @@ enum EBICS_KEY_HashAlgorithm
 };
 
 /**
- * Stores a key of type EBICS_KeyType
+ * Store key(s).
  */
 struct EBICS_Key
 {

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]