[pkg-opensc-commit] [libp11] 16/27: Error reporting fixes

Eric Dorland eric at moszumanska.debian.org
Mon Aug 7 19:48:09 UTC 2017


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

eric pushed a commit to branch master
in repository libp11.

commit 4f0fce46fb459eec5aeac395689ace8ebadd079c
Author: Michał Trojnara <Michal.Trojnara at stunnel.org>
Date:   Mon Jun 12 21:38:42 2017 +0200

    Error reporting fixes
---
 src/eng_err.c    |  2 ++
 src/libp11-int.h |  2 +-
 src/libp11.h     |  9 ++++-----
 src/p11_ckr.c    | 60 ++++++++++++++++++++++++++++++++++++++++++++------------
 src/p11_err.c    |  2 ++
 src/p11_load.c   |  4 ++--
 6 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/src/eng_err.c b/src/eng_err.c
index 3b26d2a..bbeb2fe 100644
--- a/src/eng_err.c
+++ b/src/eng_err.c
@@ -12,6 +12,8 @@
 #include <openssl/err.h>
 #include "eng_err.h"
 
+#define ENG_LIB_NAME "pkcs11 engine"
+
 /* BEGIN ERROR CODES */
 #ifndef OPENSSL_NO_ERR
 
diff --git a/src/libp11-int.h b/src/libp11-int.h
index 040f9ce..a60aae5 100644
--- a/src/libp11-int.h
+++ b/src/libp11-int.h
@@ -128,7 +128,7 @@ typedef struct pkcs11_cert_private {
 	} while (0)
 #define CRYPTOKI_call(ctx, func_and_args) \
 	PRIVCTX(ctx)->method->func_and_args
-extern void ERR_load_CKR_strings(void);
+extern int ERR_load_CKR_strings(void);
 
 /* Memory allocation */
 #define PKCS11_DUP(s) \
diff --git a/src/libp11.h b/src/libp11.h
index 36676f7..ac10477 100644
--- a/src/libp11.h
+++ b/src/libp11.h
@@ -35,11 +35,10 @@
 extern "C" {
 #endif
 
-/* Get some structures for local code to handle PKCS#11 data readily */
-#define ERR_LIB_CKR	ERR_LIB_USER
-
-#define CKRerr(f,r) \
-	ERR_PUT_error(ERR_LIB_CKR,(f),(r),__FILE__,__LINE__)
+int ERR_load_CKR_strings(void);
+void ERR_unload_CKR_strings(void);
+void ERR_CKR_error(int function, int reason, char *file, int line);
+# define CKRerr(f,r) ERR_CKR_error((f),(r),__FILE__,__LINE__)
 
 /*
  * The purpose of this library is to provide a simple PKCS11
diff --git a/src/p11_ckr.c b/src/p11_ckr.c
index 2af546a..1870ddd 100644
--- a/src/p11_ckr.c
+++ b/src/p11_ckr.c
@@ -19,12 +19,10 @@
 #include "libp11.h"
 #include "libp11-int.h"
 
+#define CKR_LIB_NAME "PKCS#11 module"
+
 /* BEGIN ERROR CODES */
 #ifndef NO_ERR
-static ERR_STRING_DATA CKR_str_library[] = {
-	{ERR_PACK(ERR_LIB_CKR, 0, 0), "PKCS11 module"},
-	{0, NULL}
-};
 
 # define ERR_FUNC(func) ERR_PACK(0,func,0)
 # define ERR_REASON(reason) ERR_PACK(0,0,reason)
@@ -150,18 +148,56 @@ static ERR_STRING_DATA CKR_str_reasons[] = {
 };
 #endif
 
-void ERR_load_CKR_strings(void)
+#ifdef CKR_LIB_NAME
+static ERR_STRING_DATA CKR_lib_name[] = {
+	{0, CKR_LIB_NAME},
+	{0, NULL}
+};
+#endif
+
+static int CKR_lib_error_code = 0;
+static int CKR_error_init = 1;
+
+int ERR_load_CKR_strings(void)
 {
-	static int init = 1;
+	if (CKR_lib_error_code == 0)
+		CKR_lib_error_code = ERR_get_next_error_library();
 
-	if (init) {
-		init = 0;
-#ifndef NO_ERR
-		ERR_load_strings(0, CKR_str_library);
-		ERR_load_strings(ERR_LIB_CKR, CKR_str_functs);
-		ERR_load_strings(ERR_LIB_CKR, CKR_str_reasons);
+	if (CKR_error_init) {
+		CKR_error_init = 0;
+#ifndef OPENSSL_NO_ERR
+		ERR_load_strings(CKR_lib_error_code, CKR_str_functs);
+		ERR_load_strings(CKR_lib_error_code, CKR_str_reasons);
+#endif
+
+#ifdef CKR_LIB_NAME
+		CKR_lib_name->error = ERR_PACK(CKR_lib_error_code, 0, 0);
+		ERR_load_strings(0, CKR_lib_name);
 #endif
 	}
+	return 1;
+}
+
+void ERR_unload_CKR_strings(void)
+{
+	if (CKR_error_init == 0) {
+#ifndef OPENSSL_NO_ERR
+		ERR_unload_strings(CKR_lib_error_code, CKR_str_functs);
+		ERR_unload_strings(CKR_lib_error_code, CKR_str_reasons);
+#endif
+
+#ifdef CKR_LIB_NAME
+		ERR_unload_strings(0, CKR_lib_name);
+#endif
+		CKR_error_init = 1;
+	}
+}
+
+void ERR_CKR_error(int function, int reason, char *file, int line)
+{
+	if (CKR_lib_error_code == 0)
+		CKR_lib_error_code = ERR_get_next_error_library();
+	ERR_PUT_error(CKR_lib_error_code, function, reason, file, line);
 }
 
 /* vim: set noexpandtab: */
diff --git a/src/p11_err.c b/src/p11_err.c
index 2c563c1..0bbbe0f 100644
--- a/src/p11_err.c
+++ b/src/p11_err.c
@@ -12,6 +12,8 @@
 #include <openssl/err.h>
 #include "p11_err.h"
 
+#define P11_LIB_NAME "libp11"
+
 /* BEGIN ERROR CODES */
 #ifndef OPENSSL_NO_ERR
 
diff --git a/src/p11_load.c b/src/p11_load.c
index a95f94b..37266f7 100644
--- a/src/p11_load.c
+++ b/src/p11_load.c
@@ -85,7 +85,7 @@ int pkcs11_CTX_load(PKCS11_CTX *ctx, const char *name)
 	args.pReserved = cpriv->init_args;
 	rv = cpriv->method->C_Initialize(&args);
 	if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
-		P11err(P11_F_PKCS11_CTX_LOAD, rv);
+		CKRerr(P11_F_PKCS11_CTX_LOAD, rv);
 		return -1;
 	}
 
@@ -120,7 +120,7 @@ int pkcs11_CTX_reload(PKCS11_CTX *ctx)
 	}
 	rv = cpriv->method->C_Initialize(args);
 	if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
-		P11err(P11_F_PKCS11_CTX_RELOAD, rv);
+		CKRerr(P11_F_PKCS11_CTX_RELOAD, rv);
 		return -1;
 	}
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opensc/libp11.git



More information about the pkg-opensc-commit mailing list