From: Nikos Mavrogiannopoulos Date: Sun, 21 Dec 2014 11:16:17 +0000 (+0200) Subject: Added softhsm.h to share code in softhsm detection X-Git-Tag: gnutls_3_4_0~451 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fad565e7fc2ec52dd557c25119ab35f4f236916e;p=thirdparty%2Fgnutls.git Added softhsm.h to share code in softhsm detection --- diff --git a/tests/suite/pkcs11-chainverify.c b/tests/suite/pkcs11-chainverify.c index 43493b8efc..1e5e065957 100644 --- a/tests/suite/pkcs11-chainverify.c +++ b/tests/suite/pkcs11-chainverify.c @@ -33,6 +33,7 @@ #include #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) { diff --git a/tests/suite/pkcs11-combo.c b/tests/suite/pkcs11-combo.c index 14990087b7..afe59e566d 100644 --- a/tests/suite/pkcs11-combo.c +++ b/tests/suite/pkcs11-combo.c @@ -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) { diff --git a/tests/suite/pkcs11-get-issuer.c b/tests/suite/pkcs11-get-issuer.c index a134905af3..1614aa406d 100644 --- a/tests/suite/pkcs11-get-issuer.c +++ b/tests/suite/pkcs11-get-issuer.c @@ -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) { diff --git a/tests/suite/pkcs11-is-known.c b/tests/suite/pkcs11-is-known.c index c1617b1d08..86226884ac 100644 --- a/tests/suite/pkcs11-is-known.c +++ b/tests/suite/pkcs11-is-known.c @@ -34,6 +34,7 @@ #include #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) { diff --git a/tests/suite/pkcs11-privkey.c b/tests/suite/pkcs11-privkey.c index f1a5ff83a1..8e7567f4ea 100644 --- a/tests/suite/pkcs11-privkey.c +++ b/tests/suite/pkcs11-privkey.c @@ -34,6 +34,7 @@ #include #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 index 0000000000..bb46736cff --- /dev/null +++ b/tests/suite/softhsm.h @@ -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 diff --git a/tests/suite/testpkcs11.softhsm b/tests/suite/testpkcs11.softhsm index ddaaff4f73..63950a77f1 100755 --- a/tests/suite/testpkcs11.softhsm +++ b/tests/suite/testpkcs11.softhsm @@ -20,10 +20,14 @@ 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 () {