]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Added softhsm.h to share code in softhsm detection
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 21 Dec 2014 11:16:17 +0000 (13:16 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 21 Dec 2014 17:37:50 +0000 (19:37 +0200)
tests/suite/pkcs11-chainverify.c
tests/suite/pkcs11-combo.c
tests/suite/pkcs11-get-issuer.c
tests/suite/pkcs11-is-known.c
tests/suite/pkcs11-privkey.c
tests/suite/softhsm.h [new file with mode: 0644]
tests/suite/testpkcs11.softhsm

index 43493b8efc6f42c5437defd44cc36ad49266c952..1e5e065957e9f4a93d797119c6fdc95d3c352c66 100644 (file)
@@ -33,6 +33,7 @@
 #include <gnutls/x509.h>
 
 #include "../utils.h"
+#include "softhsm.h"
 #include "../test-chains.h"
 
 #define URL "pkcs11:model=SoftHSM;manufacturer=SoftHSM;serial=1;token=test"
@@ -69,17 +70,15 @@ int pin_func(void* userdata, int attempt, const char* url, const char *label,
        return -1;
 }
 
-#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
-#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
-
 void doit(void)
 {
        int exit_val = 0;
        size_t i;
        int ret;
        FILE *fp;
-       const char *lib;
+       const char *lib, *bin;
        gnutls_typed_vdata_st vdata[2];
+       char buf[128];
 
        unsetenv("SOFTHSM_CONF");
        /* The overloading of time() seems to work in linux (ELF?)
@@ -89,20 +88,9 @@ void doit(void)
        exit(77);
 #endif
 
-       if (access("/usr/bin/softhsm", X_OK) < 0) {
-               fprintf(stderr, "cannot find softhsm binary\n");
-               exit(77);
-       }
-
-       if (access(LIB1, R_OK) == 0) {
-               lib = LIB1;
-       } else if (access(LIB2, R_OK) == 0) {
-               lib = LIB2;
-       } else {
-               fprintf(stderr, "cannot find softhsm module\n");
-               exit(77);
-       }
+       bin = softhsm_bin();
 
+       lib = softhsm_lib();
 
        ret = global_init();
        if (ret != 0) {
@@ -127,7 +115,8 @@ void doit(void)
 
        setenv("SOFTHSM_CONF", CONFIG, 0);
 
-       system("softhsm --init-token --slot 0 --label test --so-pin 1234 --pin 1234");
+       snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin 1234 --pin 1234", bin);
+       system(buf);
 
        ret = gnutls_pkcs11_add_provider(lib, "trusted");
        if (ret < 0) {
index 14990087b74736b9950d91a258e54d9a6cae0ba8..afe59e566d217dd2199d2f62292bc11beae09dc9 100644 (file)
@@ -39,6 +39,7 @@
 
 #include "../utils.h"
 #include "../test-chains.h"
+#include "softhsm.h"
 
 #define URL "pkcs11:model=SoftHSM;manufacturer=SoftHSM;serial=1;token=test"
 #define NAME "softhsm-combo"
@@ -253,16 +254,14 @@ int pin_func(void* userdata, int attempt, const char* url, const char *label,
        return -1;
 }
 
-#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
-#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
-
 void doit(void)
 {
+       char buf[128];
        int exit_val = 0;
        int ret;
        unsigned j;
        FILE *fp;
-       const char *lib;
+       const char *lib, *bin;
        gnutls_x509_crt_t issuer = NULL;
        gnutls_x509_trust_list_t tl;
        gnutls_x509_crt_t certs[MAX_CHAIN];
@@ -277,20 +276,9 @@ void doit(void)
 #ifdef _WIN32
        exit(77);
 #endif
-       if (access("/usr/bin/softhsm", X_OK) < 0) {
-               fprintf(stderr, "cannot find softhsm binary\n");
-               exit(77);
-       }
-
-       if (access(LIB1, R_OK) == 0) {
-               lib = LIB1;
-       } else if (access(LIB2, R_OK) == 0) {
-               lib = LIB2;
-       } else {
-               fprintf(stderr, "cannot find softhsm module\n");
-               exit(77);
-       }
+       bin = softhsm_bin();
 
+       lib = softhsm_lib();
 
        ret = global_init();
        if (ret != 0) {
@@ -316,7 +304,8 @@ void doit(void)
 
        setenv("SOFTHSM_CONF", CONFIG, 0);
 
-       system("/usr/bin/softhsm --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN);
+       snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
+       system(buf);
 
        ret = gnutls_pkcs11_add_provider(lib, "trusted");
        if (ret < 0) {
index a134905af371071c9cb2860c8cfb84e7b9f71248..1614aa406dd84147f163bb2fbfa7e9ba34172688 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "../utils.h"
 #include "../test-chains.h"
+#include "softhsm.h"
 
 #define URL "pkcs11:model=SoftHSM;manufacturer=SoftHSM;serial=1;token=test"
 #define CONFIG "softhsm-issuer.config"
@@ -71,16 +72,14 @@ int pin_func(void* userdata, int attempt, const char* url, const char *label,
        return -1;
 }
 
-#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
-#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
-
 void doit(void)
 {
+       char buf[128];
        int exit_val = 0;
        int ret;
        unsigned j;
        FILE *fp;
-       const char *lib;
+       const char *lib, *bin;
        gnutls_x509_crt_t issuer = NULL;
        gnutls_x509_trust_list_t tl;
        gnutls_x509_crt_t certs[MAX_CHAIN];
@@ -109,20 +108,9 @@ void doit(void)
                exit(1);
        }
 
-       if (access("/usr/bin/softhsm", X_OK) < 0) {
-               fprintf(stderr, "cannot find softhsm binary\n");
-               exit(77);
-       }
-
-       if (access(LIB1, R_OK) == 0) {
-               lib = LIB1;
-       } else if (access(LIB2, R_OK) == 0) {
-               lib = LIB2;
-       } else {
-               fprintf(stderr, "cannot find softhsm module\n");
-               exit(77);
-       }
+       bin = softhsm_bin();
 
+       lib = softhsm_lib();
 
        ret = global_init();
        if (ret != 0) {
@@ -148,7 +136,8 @@ void doit(void)
 
        setenv("SOFTHSM_CONF", CONFIG, 0);
 
-       system("/usr/bin/softhsm --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN);
+       snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
+       system(buf);
 
        ret = gnutls_pkcs11_add_provider(lib, "trusted");
        if (ret < 0) {
index c1617b1d08d87ee555e7326da894793a48b1ac4d..86226884ac30d2e1f98fb4d7fe36bbfd60174248 100644 (file)
@@ -34,6 +34,7 @@
 #include <gnutls/x509-ext.h>
 
 #include "../utils.h"
+#include "softhsm.h"
 
 #define MAX_CHAIN 16
 
@@ -249,16 +250,14 @@ int pin_func(void* userdata, int attempt, const char* url, const char *label,
        return -1;
 }
 
-#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
-#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
-
 void doit(void)
 {
+       char buf[128];
        int exit_val = 0;
        int ret;
        unsigned j;
        FILE *fp;
-       const char *lib;
+       const char *lib, *bin;
        gnutls_x509_crt_t issuer = NULL;
        gnutls_x509_trust_list_t tl;
        gnutls_x509_crt_t certs[MAX_CHAIN];
@@ -272,20 +271,9 @@ void doit(void)
 #ifdef _WIN32
        exit(77);
 #endif
-       if (access("/usr/bin/softhsm", X_OK) < 0) {
-               fprintf(stderr, "cannot find softhsm binary\n");
-               exit(77);
-       }
-
-       if (access(LIB1, R_OK) == 0) {
-               lib = LIB1;
-       } else if (access(LIB2, R_OK) == 0) {
-               lib = LIB2;
-       } else {
-               fprintf(stderr, "cannot find softhsm module\n");
-               exit(77);
-       }
+       bin = softhsm_bin();
 
+       lib = softhsm_lib();
 
        ret = global_init();
        if (ret != 0) {
@@ -311,7 +299,8 @@ void doit(void)
 
        setenv("SOFTHSM_CONF", CONFIG, 0);
 
-       system("/usr/bin/softhsm --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN);
+       snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
+       system(buf);
 
        ret = gnutls_pkcs11_add_provider(lib, "trusted");
        if (ret < 0) {
index f1a5ff83a1e5d06c1bafb4f7537f4c31902d6f43..8e7567f4eab24a114d4b22b0fddea2d7e8a75db3 100644 (file)
@@ -34,6 +34,7 @@
 #include <gnutls/x509-ext.h>
 
 #include "../utils.h"
+#include "softhsm.h"
 
 /* Tests whether gnutls_certificate_set_x509_key_file2() will utilize
  * the provided password as PIN when PKCS #11 keys are imported */
@@ -118,15 +119,13 @@ int pin_func(void* userdata, int attempt, const char* url, const char *label,
        return -1;
 }
 
-#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
-#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
-
 void doit(void)
 {
+       char buf[128];
        int exit_val = 0;
        int ret;
        FILE *fp;
-       const char *lib;
+       const char *lib, *bin;
        gnutls_x509_crt_t crt;
        gnutls_x509_privkey_t key;
        gnutls_certificate_credentials_t cred;
@@ -139,20 +138,9 @@ void doit(void)
 #ifdef _WIN32
        exit(77);
 #endif
-       if (access("/usr/bin/softhsm", X_OK) < 0) {
-               fprintf(stderr, "cannot find softhsm binary\n");
-               exit(77);
-       }
-
-       if (access(LIB1, R_OK) == 0) {
-               lib = LIB1;
-       } else if (access(LIB2, R_OK) == 0) {
-               lib = LIB2;
-       } else {
-               fprintf(stderr, "cannot find softhsm module\n");
-               exit(77);
-       }
+       bin = softhsm_bin();
 
+       lib = softhsm_lib();
 
        ret = global_init();
        if (ret != 0) {
@@ -178,7 +166,8 @@ void doit(void)
 
        setenv("SOFTHSM_CONF", CONFIG, 0);
 
-       system("/usr/bin/softhsm --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN);
+       snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
+       system(buf);
 
        ret = gnutls_pkcs11_add_provider(lib, "trusted");
        if (ret < 0) {
diff --git a/tests/suite/softhsm.h b/tests/suite/softhsm.h
new file mode 100644 (file)
index 0000000..bb46736
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2014 Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GnuTLS; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#ifndef SOFTHSM_H
+# define SOFTHSM_H
+
+#define LIB1 "/usr/lib64/softhsm/libsofthsm.so"
+#define LIB2 "/usr/lib/softhsm/libsofthsm.so"
+#define LIB3 "/usr/local/lib/softhsm/libsofthsm.so"
+
+inline static const char *softhsm_lib(void) 
+{
+       const char *lib;
+
+       if (access(LIB1, R_OK) == 0) {
+               lib = LIB1;
+       } else if (access(LIB2, R_OK) == 0) {
+               lib = LIB2;
+       } else if (access(LIB3, R_OK) == 0) {
+               lib = LIB3;
+       } else {
+               fprintf(stderr, "cannot find softhsm module\n");
+               exit(77);
+       }
+
+       return lib;
+}
+
+inline static const char *softhsm_bin(void) 
+{
+       const char *bin;
+
+       if (access("/usr/bin/softhsm", X_OK) == 0) {
+               bin = "/usr/bin/softhsm";
+       } else if (access("/usr/local/bin/softhsm", X_OK) == 0) {
+               bin = "/usr/local/bin/softhsm";
+       } else {
+               fprintf(stderr, "cannot find softhsm module\n");
+               exit(77);
+       }
+
+       return bin;
+}
+
+#endif
index ddaaff4f7351e9c1b19bdf958edd3c22faaba784..63950a77f171332f53df6fe29a7915b45a8dd2ad 100755 (executable)
 
 export SOFTHSM_CONF="softhsm-testpkcs11.config"
 
-if test -f /usr/lib/softhsm/libsofthsm.so;then
-       ADDITIONAL_PARAM="--provider /usr/lib/softhsm/libsofthsm.so"
+if test -f /usr/local/lib/softhsm/libsofthsm.so;then
+       ADDITIONAL_PARAM="--provider /usr/local/lib/softhsm/libsofthsm.so"
 else
-       ADDITIONAL_PARAM="--provider /usr/lib64/softhsm/libsofthsm.so"
+       if test -f /usr/lib/softhsm/libsofthsm.so;then
+               ADDITIONAL_PARAM="--provider /usr/lib/softhsm/libsofthsm.so"
+       else
+               ADDITIONAL_PARAM="--provider /usr/lib64/softhsm/libsofthsm.so"
+       fi
 fi
 
 init_card () {