Use csapi-tizen for get error message API
authorKyungwook Tak <k.tak@samsung.com>
Thu, 18 Aug 2016 05:34:19 +0000 (14:34 +0900)
committerkyungwook tak <k.tak@samsung.com>
Thu, 18 Aug 2016 06:53:41 +0000 (15:53 +0900)
Change-Id: Ibf8e711d1df851d6fbcfcc64e2f588b76af3ba37
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
13 files changed:
Tizen.Security/Interop/Interop.CkmcErrors.cs
Tizen.Security/Interop/Interop.CkmcTypes.cs
Tizen.Security/Interop/Interop.Libraries.cs
Tizen.Security/Tizen.Security.SecureRepository/CertificateManager.cs
Tizen.Security/Tizen.Security.SecureRepository/Crypto/Cipher.cs
Tizen.Security/Tizen.Security.SecureRepository/Crypto/CipherParameters.cs
Tizen.Security/Tizen.Security.SecureRepository/Crypto/Signature.cs
Tizen.Security/Tizen.Security.SecureRepository/DataManager.cs
Tizen.Security/Tizen.Security.SecureRepository/KeyManager.cs
Tizen.Security/Tizen.Security.SecureRepository/Manager.cs
Tizen.Security/Tizen.Security.SecureRepository/Pkcs12.cs
Tizen.Security/Tizen.Security.SecureRepository/Pkcs12Manager.cs
Tizen.Security/Tizen.Security.SecureRepository/SafeCertificateListHandle.cs

index ce3fe3c..3e12547 100644 (file)
  */
 
 using System;
+using Tizen.Internals.Errors;
 
 internal static partial class Interop
 {
     private const int TizenErrorKeyManager = -0x01E10000;
+    private const string LogTag = "Tizen.Security.SecureRepository";
 
     internal enum KeyManagerError : int
     {
-        // TODO : Add reference to real Tizen project
-        //None = Tizen.Internals.Errors.ErrorCode.None,
-        //InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter
-        None = 0, // CKMC_ERROR_NONE
-        InvalidParameter = -22, // CKMC_ERROR_INVALID_PARAMETER
+        None = ErrorCode.None,
+        InvalidParameter = ErrorCode.InvalidParameter,
         VerificationFailed = TizenErrorKeyManager | 0x0D // CKMC_ERROR_VERIFICATION_FAILED
     };
 
-    internal class KeyManagerExceptionFactory
+    internal static void CheckNThrowException(int err, string msg)
     {
-        internal const string LogTag = "Tizen.Security.SecureRepository";
-
-        internal static void CheckNThrowException(int err, string msg)
+        switch (err)
         {
-            if (err == (int)KeyManagerError.None)
+            case (int)KeyManagerError.None:
                 return;
-
-            switch (err)
-            {
-                case (int)KeyManagerError.InvalidParameter:
-                    throw new ArgumentException(msg + ", error=" + Interop.GetErrorMessage(err));
-                default:
-                    throw new InvalidOperationException(msg + ", error=" + Interop.GetErrorMessage(err));
-            }
+            case (int)KeyManagerError.InvalidParameter:
+                throw new ArgumentException(string.Format("[{0}] {1}, error={2}",
+                    LogTag, msg, ErrorFacts.GetErrorMessage(err)));
+            default:
+                throw new InvalidOperationException(string.Format("[{0}] {1}, error={2}",
+                    LogTag, msg, ErrorFacts.GetErrorMessage(err)));
         }
     }
 }
index 484fac7..42900aa 100644 (file)
@@ -108,14 +108,6 @@ internal static partial class Interop
         public readonly IntPtr caChain;
     }
 
-
-    static public string GetErrorMessage(int errorCode)
-    {
-        IntPtr errorPtr = CkmcTypes.GetErrorMessage(errorCode);
-        return Marshal.PtrToStringAuto(errorPtr);
-    }
-
-
     internal static partial class CkmcTypes
     {
         [DllImport(Libraries.KeyManagerClient, EntryPoint = "ckmc_key_free", CallingConvention = CallingConvention.Cdecl)]
@@ -217,11 +209,5 @@ internal static partial class Interop
         [DllImport(Libraries.KeyManagerClient, EntryPoint = "ckmc_generate_new_params", CallingConvention = CallingConvention.Cdecl)]
         public static extern int CkmcGenerateNewParam(int algoType, out IntPtr paramList);
         // int ckmc_generate_new_params(ckmc_algo_type_e type, ckmc_param_list_h *pparams);
-
-        [DllImport(Libraries.TizenBaseCommon, EntryPoint = "get_error_message", CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr GetErrorMessage(int err);
-        // char *get_error_message(int err);
-
     }
 }
-
index a12b370..0405bb8 100644 (file)
@@ -20,6 +20,5 @@ internal static partial class Interop
     {
         public const string Privilege = "libprivilege-info.so.1";
         public const string KeyManagerClient = "libkey-manager-client.so.1";
-        public const string TizenBaseCommon = "libcapi-base-common.so.0";
     }
 }
index 6d1e7aa..6b19f00 100644 (file)
@@ -37,7 +37,7 @@ namespace Tizen.Security.SecureRepository
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcManager.CkmcGetCert(alias, password, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
 
             return new Certificate(ptr);
         }
@@ -50,7 +50,7 @@ namespace Tizen.Security.SecureRepository
         {
             IntPtr ptr = new IntPtr();
             int ret = Interop.CkmcManager.CkmcGetCertAliasList(out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate aliases.");
+            Interop.CheckNThrowException(ret, "Failed to get certificate aliases.");
 
             return new SafeAliasListHandle(ptr).Aliases;
         }
@@ -64,7 +64,7 @@ namespace Tizen.Security.SecureRepository
         static public void SaveCertificate(string alias, Certificate cert, Policy policy)
         {
             int ret = Interop.CkmcManager.CkmcSaveCert(alias, cert.ToCkmcCert(), policy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save certificate. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to save certificate. alias=" + alias);
         }
 
         /// <summary>
@@ -83,7 +83,7 @@ namespace Tizen.Security.SecureRepository
 
             int ret = Interop.CkmcManager.CkmcGetCertChain(new PinnedObject(certificate.ToCkmcCert()),
                                                         untrustedCerts.ToCkmcCertificateListPtr(), out ptrCertChain);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain");
+            Interop.CheckNThrowException(ret, "Failed to get certificate chain");
 
             SafeCertificateListHandle certChain = new SafeCertificateListHandle(ptrCertChain);
             return certChain.Certificates;
@@ -109,7 +109,7 @@ namespace Tizen.Security.SecureRepository
             int ret = Interop.CkmcManager.CkmcGetCertChainWithTrustedCerts(new PinnedObject(certificate.ToCkmcCert()),
                             untrustedCerts.ToCkmcCertificateListPtr(), trustedCerts.ToCkmcCertificateListPtr(), useTrustedSystemCertificates,
                             out ptrCertChain);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
+            Interop.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
 
             SafeCertificateListHandle certChain = new SafeCertificateListHandle(ptrCertChain);
 
@@ -126,7 +126,7 @@ namespace Tizen.Security.SecureRepository
             int ocspStatus = (int)OcspStatus.Good;
             SafeCertificateListHandle certChain = new SafeCertificateListHandle(certificateChain);
             int ret = Interop.CkmcManager.CkmcOcspCheck(certChain.ToCkmcCertificateListPtr(), ref ocspStatus);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
+            Interop.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
             return (OcspStatus)ocspStatus;
         }
     }
index b559f73..b92e0ff 100644 (file)
@@ -58,7 +58,7 @@ namespace Tizen.Security.SecureRepository.Crypto
             Interop.CkmcRawBuffer cipherTextBuff = new Interop.CkmcRawBuffer(new PinnedObject(cipherText), cipherText.Length);
 
             int ret = Interop.CkmcManager.CkmcDecryptData(Parameters.PtrCkmcParamList, keyAlias, password, cipherTextBuff, out ptrPlainText);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to decrypt data");
+            Interop.CheckNThrowException(ret, "Failed to decrypt data");
 
             return new SafeRawBufferHandle(ptrPlainText).Data;
         }
@@ -80,7 +80,7 @@ namespace Tizen.Security.SecureRepository.Crypto
             Interop.CkmcRawBuffer plainTextBuff = new Interop.CkmcRawBuffer(new PinnedObject(plainText), plainText.Length);
 
             int ret = Interop.CkmcManager.CkmcEncryptData(Parameters.PtrCkmcParamList, keyAlias, password, plainTextBuff, out ptrCipherText);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to encrypt data");
+            Interop.CheckNThrowException(ret, "Failed to encrypt data");
 
             return new SafeRawBufferHandle(ptrCipherText).Data;
         }
index d7d233a..61a3362 100644 (file)
@@ -43,7 +43,7 @@ namespace Tizen.Security.SecureRepository.Crypto
         protected void Add(CipherParameterName name, long value)
         {
             int ret = Interop.CkmcTypes.CkmcParamListSetInteger(PtrCkmcParamList, (int)name, value);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add parameter.");
+            Interop.CheckNThrowException(ret, "Failed to add parameter.");
         }
 
         /// <summary>
@@ -55,7 +55,7 @@ namespace Tizen.Security.SecureRepository.Crypto
         {
             Interop.CkmcRawBuffer rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(value), value.Length);
             int ret = Interop.CkmcTypes.CkmcParamListSetBuffer(PtrCkmcParamList, (int)name, new PinnedObject(rawBuff));
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add parameter.");
+            Interop.CheckNThrowException(ret, "Failed to add parameter.");
         }
 
         /// <summary>
@@ -66,7 +66,7 @@ namespace Tizen.Security.SecureRepository.Crypto
         {
             long value = 0;
             int ret = Interop.CkmcTypes.CkmcParamListGetInteger(PtrCkmcParamList, (int)name, out value);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get parameter.");
+            Interop.CheckNThrowException(ret, "Failed to get parameter.");
             return value;
         }
 
@@ -79,7 +79,7 @@ namespace Tizen.Security.SecureRepository.Crypto
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcTypes.CkmcParamListGetBuffer(PtrCkmcParamList, (int)name, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get parameter.");
+            Interop.CheckNThrowException(ret, "Failed to get parameter.");
 
             return new SafeRawBufferHandle(ptr).Data;
         }
index 9f2e2d4..cdb295b 100644 (file)
@@ -72,7 +72,7 @@ namespace Tizen.Security.SecureRepository.Crypto
 
             int ret = Interop.CkmcManager.CkmcCreateSignature(privateKeyAlias, password, messageBuff,
                                                             hash, rsaPadding, out ptrSignature);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to generate signature");
+            Interop.CheckNThrowException(ret, "Failed to generate signature");
 
             return new SafeRawBufferHandle(ptrSignature).Data;
         }
@@ -110,7 +110,7 @@ namespace Tizen.Security.SecureRepository.Crypto
                                                         signatureBuff, hash, rsaPadding);
             if (ret == (int)Interop.KeyManagerError.VerificationFailed)
                 return false;
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to verify signature");
+            Interop.CheckNThrowException(ret, "Failed to verify signature");
 
             return true;
         }
index 54f1fa7..5e317f6 100644 (file)
@@ -37,7 +37,7 @@ namespace Tizen.Security.SecureRepository
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcManager.CkmcGetData(alias, password, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
 
             return new SafeRawBufferHandle(ptr).Data;
         }
@@ -50,7 +50,7 @@ namespace Tizen.Security.SecureRepository
         {
             IntPtr ptr = new IntPtr();
             int ret = Interop.CkmcManager.CkmcGetDataAliasList(out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get data aliases");
+            Interop.CheckNThrowException(ret, "Failed to get data aliases");
 
             return new SafeAliasListHandle(ptr).Aliases;
         }
@@ -66,7 +66,7 @@ namespace Tizen.Security.SecureRepository
             Interop.CkmcRawBuffer rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(data), data.Length);
 
             int ret = Interop.CkmcManager.CkmcSaveData(alias, rawBuff, policy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save data. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to save data. alias=" + alias);
         }
     }
 }
index 0625be5..ede1173 100644 (file)
@@ -37,7 +37,7 @@ namespace Tizen.Security.SecureRepository
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcManager.CkmcGetKey(alias, password, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get key. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to get key. alias=" + alias);
 
             return new Key(ptr);
         }
@@ -50,7 +50,7 @@ namespace Tizen.Security.SecureRepository
         {
             IntPtr ptr = new IntPtr();
             int ret = Interop.CkmcManager.CkmcGetKeyAliasList(out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get key aliases.");
+            Interop.CheckNThrowException(ret, "Failed to get key aliases.");
 
             return new SafeAliasListHandle(ptr).Aliases;
         }
@@ -66,7 +66,7 @@ namespace Tizen.Security.SecureRepository
         static public void SaveKey(string alias, Key key, Policy policy)
         {
             int ret = Interop.CkmcManager.CkmcSaveKey(alias, key.ToCkmcKey(), policy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save Key. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to save Key. alias=" + alias);
         }
 
         /// <summary>
@@ -83,7 +83,7 @@ namespace Tizen.Security.SecureRepository
         {
             int ret = Interop.CkmcManager.CkmcCreateKeyPairRsa(size, privateKeyAlias, publicKeyAlias,
                                         privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create RSA Key Pair");
+            Interop.CheckNThrowException(ret, "Failed to Create RSA Key Pair");
         }
 
         /// <summary>
@@ -100,7 +100,7 @@ namespace Tizen.Security.SecureRepository
         {
             int ret = Interop.CkmcManager.CkmcCreateKeyPairDsa(size, privateKeyAlias, publicKeyAlias,
                                         privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create DSA Key Pair");
+            Interop.CheckNThrowException(ret, "Failed to Create DSA Key Pair");
         }
 
         /// <summary>
@@ -117,7 +117,7 @@ namespace Tizen.Security.SecureRepository
         {
             int ret = Interop.CkmcManager.CkmcCreateKeyPairEcdsa((int)type, privateKeyAlias, publicKeyAlias,
                                         privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create ECDSA Key Pair");
+            Interop.CheckNThrowException(ret, "Failed to Create ECDSA Key Pair");
         }
 
         /// <summary>
@@ -130,7 +130,7 @@ namespace Tizen.Security.SecureRepository
         static public void CreateKeyAes(int size, string keyAlias, Policy policy)
         {
             int ret = Interop.CkmcManager.CkmcCreateKeyAes(size, keyAlias, policy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to AES Key");
+            Interop.CheckNThrowException(ret, "Failed to AES Key");
         }
     }
 }
index 2b37acc..1a4bb3f 100644 (file)
@@ -54,7 +54,7 @@ namespace Tizen.Security.SecureRepository
         static public void RemoveAlias(string alias)
         {
             int ret = Interop.CkmcManager.CkmcRemoveAlias(alias);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to remove alias. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to remove alias. alias=" + alias);
         }
 
         /// <summary>
@@ -68,7 +68,7 @@ namespace Tizen.Security.SecureRepository
         static public void SetPermission(string alias, string otherPackageId, int permissions)
         {
             int ret = Interop.CkmcManager.CkmcSetPermission(alias, otherPackageId, permissions);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to set permission. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to set permission. alias=" + alias);
         }
     }
 }
index b32e111..43036e4 100644 (file)
@@ -38,7 +38,7 @@ namespace Tizen.Security.SecureRepository
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcTypes.CkmcPkcs12Load(filePath, filePassword, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to load PKCS12. file=" + filePath);
+            Interop.CheckNThrowException(ret, "Failed to load PKCS12. file=" + filePath);
 
             return new Pkcs12(ptr);
         }
index 5470253..f18585f 100644 (file)
@@ -39,7 +39,7 @@ namespace Tizen.Security.SecureRepository
             IntPtr ptr = new IntPtr();
 
             int ret = Interop.CkmcManager.CkmcGetPkcs12(alias, keyPassword, cerificatePassword, out ptr);
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get PKCS12. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to get PKCS12. alias=" + alias);
 
             return new Pkcs12(ptr);
         }
@@ -58,7 +58,7 @@ namespace Tizen.Security.SecureRepository
                                                     new PinnedObject(pkcs12.ToCkmcPkcs12()),
                                                     keyPolicy.ToCkmcPolicy(),
                                                     certificatePolicy.ToCkmcPolicy());
-            Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save PKCS12. alias=" + alias);
+            Interop.CheckNThrowException(ret, "Failed to save PKCS12. alias=" + alias);
         }
     }
 }
index b346f25..2676759 100644 (file)
@@ -68,20 +68,20 @@ namespace Tizen.Security.SecureRepository
             {
                 IntPtr certPtr;
                 ret = Interop.CkmcTypes.CkmcCertNew(cert.Binary, (uint)cert.Binary.Length, (int)cert.Format, out certPtr);
-                Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to create new Certificate.");
+                Interop.CheckNThrowException(ret, "Failed to create new Certificate.");
 
                 IntPtr outCertList;
                 if (previous == IntPtr.Zero)
                 {
                     ret = Interop.CkmcTypes.CkmcCertListNew(certPtr, out outCertList);
-                    Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to create new CertificateList.");
+                    Interop.CheckNThrowException(ret, "Failed to create new CertificateList.");
                     first = outCertList;
                     previous = outCertList;
                 }
                 else
                 {
                     ret = Interop.CkmcTypes.CkmcCertListAdd(previous, certPtr, out outCertList);
-                    Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add Certificate to CertificateList.");
+                    Interop.CheckNThrowException(ret, "Failed to add Certificate to CertificateList.");
                     previous = outCertList;
                 }
             }