Add API for CKM return code descriptions 29/200229/4
authorTomasz Swierczek <t.swierczek@samsung.com>
Wed, 20 Feb 2019 09:08:14 +0000 (10:08 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Tue, 26 Feb 2019 12:29:18 +0000 (13:29 +0100)
In rare case when DB tool was used for db inspection,
and db could not be opened, the commandline interface
returned raw error code, without any explanation.

Change-Id: If7a29842ae5a7fc2e99a2d991545539704647f3c

src/include/ckm/ckm-error.h
src/manager/CMakeLists.txt
src/manager/common/ckm-error.cpp [new file with mode: 0644]
tools/ckm_db_tool/CMakeLists.txt
tools/ckm_db_tool/ckm_db_tool.cpp

index 372bd98..788eaef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,9 +20,7 @@
 #ifndef _CKM_ERROR_H_
 #define _CKM_ERROR_H_
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include <ckm/ckm-type.h>
 
 /**
  * \name Return Codes
@@ -110,8 +108,11 @@ extern "C" {
 #define CKM_API_ERROR_UNKNOWN -255
 /** @}*/
 
-#ifdef __cplusplus
-}
-#endif
+namespace CKM {
+
+/*! \brief   returns stringified name of return code/status constant */
+KEY_MANAGER_API const char * APICodeToString(int error);
+
+} // namespace CKM
 
 #endif
index 53c572c..d4f67b6 100644 (file)
@@ -26,6 +26,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/common/pkcs12-impl.cpp
     ${COMMON_PATH}/common/log-setup.cpp
     ${COMMON_PATH}/common/ckm-zero-memory.cpp
+    ${COMMON_PATH}/common/ckm-error.cpp
     ${COMMON_PATH}/dpl/log/src/abstract_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/log.cpp
diff --git a/src/manager/common/ckm-error.cpp b/src/manager/common/ckm-error.cpp
new file mode 100644 (file)
index 0000000..520bcbe
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file       ckm-zero-memory.cpp
+ * @author     Tomasz Swierczek (t.swierczek@samsung.com)
+ * @version    1.0
+ */
+
+#include <ckm/ckm-error.h>
+
+namespace CKM {
+
+#define CKM_CODE_DESCRIBE(name) case name: return #name
+const char * APICodeToString(int error) {
+    switch (error) {
+        CKM_CODE_DESCRIBE(CKM_API_SUCCESS);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_SOCKET);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_BAD_REQUEST);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_BAD_RESPONSE);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_SEND_FAILED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_RECV_FAILED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_AUTHENTICATION_FAILED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_INPUT_PARAM);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_BUFFER_TOO_SMALL);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_OUT_OF_MEMORY);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_ACCESS_DENIED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_SERVER_ERROR);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_DB_LOCKED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_DB_ERROR);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_DB_ALIAS_EXISTS);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_DB_ALIAS_UNKNOWN);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_VERIFICATION_FAILED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_INVALID_FORMAT);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_FILE_ACCESS_DENIED);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_NOT_EXPORTABLE);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_FILE_SYSTEM);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_NOT_SUPPORTED);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_GOOD);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_UNSUPPORTED);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_UNKNOWN);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_REVOKED);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_NET_ERROR);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_INVALID_URL);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_INVALID_RESPONSE);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_REMOTE_ERROR);
+        CKM_CODE_DESCRIBE(CKM_API_OCSP_STATUS_INTERNAL_ERROR);
+        CKM_CODE_DESCRIBE(CKM_API_ERROR_UNKNOWN);
+        default: return "Code not defined";
+    }
+}
+#undef CKM_CODE_DESCRIBE
+
+} // namespace CKM
index 33fa991..a25e497 100644 (file)
@@ -41,6 +41,7 @@ SET(CKM_DB_TOOLS_SOURCES
     ${PROJECT_SOURCE_DIR}/tools/ckm_db_tool/db-crypto-ext.cpp
     ${PROJECT_SOURCE_DIR}/tools/ckm_db_tool/ckm-logic-ext.cpp
     ${PROJECT_SOURCE_DIR}/tools/ckm_db_tool/db-wrapper.cpp
+    ${KEY_MANAGER_PATH}/common/ckm-error.cpp
     ${KEY_MANAGER_PATH}/crypto/platform/decider.cpp
     ${KEY_MANAGER_PATH}/crypto/sw-backend/internals.cpp
     ${KEY_MANAGER_PATH}/crypto/sw-backend/obj.cpp
index 2435ee3..0de240d 100644 (file)
@@ -104,7 +104,7 @@ int main(int argc, char *argv[])
                int retCode;
 
                if (CKM_API_SUCCESS != (retCode = dbw.unlock())) {
-                       cerr << "Unlocking database failed: " << retCode << endl;
+                       cerr << "Unlocking database failed: " << APICodeToString(retCode) << endl;
                        return -1;
                }