[Tizen.Security.TEEC] Check for TEE feature when creating context (#290)
authorIgor Kotrasiński <ikotrasinsk@gmail.com>
Wed, 12 Sep 2018 02:15:52 +0000 (04:15 +0200)
committerWonYoung Choi <wy80.choi@samsung.com>
Wed, 12 Sep 2018 02:15:52 +0000 (11:15 +0900)
Signed-off-by: Igor Kotrasinski <i.kotrasinsk@partner.samsung.com>
src/Tizen.Security.TEEC/Interop/Interop.Errors.cs
src/Tizen.Security.TEEC/Tizen.Security.TEEC.csproj
src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs

index eb86422..f79c033 100644 (file)
@@ -14,7 +14,9 @@ internal static partial class Interop
     internal enum LibteecError : uint
     {
         None = 0,
+        Generic = 0xFFFF0000,
         NotImplemented = 0xFFFF0009,
+        NotSupported = 0xFFFF000A,
         CommunicationFailed = 0xFFFF000E,
     };
 
@@ -25,9 +27,11 @@ internal static partial class Interop
             case (uint)LibteecError.None:
                 return ;
             case (uint)LibteecError.NotImplemented:
+            case (uint)LibteecError.NotSupported:
                 throw new NotSupportedException(string.Format("[{0}] {1} error=0x{2}",
                         LogTag, msg, err.ToString("X")));
             case (uint)LibteecError.CommunicationFailed:
+            case (uint)LibteecError.Generic:
                 throw new Exception(string.Format("[{0}] {1} error=0x{2}",
                         LogTag, msg, err.ToString("X")));
             default:
index 4c51c41..fbac841 100644 (file)
@@ -7,6 +7,7 @@
   <ItemGroup>
     <ProjectReference Include="..\Tizen\Tizen.csproj" />
     <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj" />
+    <ProjectReference Include="..\Tizen.System.Information\Tizen.System.Information.csproj" />
   </ItemGroup>
 
-</Project>
\ No newline at end of file
+</Project>
index 6965415..40719ee 100644 (file)
@@ -753,6 +753,20 @@ namespace Tizen.Security.TEEC
 
     };
 
+    static internal class TeeFeature
+    {
+        private static string name = "http://tizen.org/feature/security.tee";
+        public static bool IsEnabled() {
+            /* FIXME - System.Information does not provide a Try-less variant of GetValue */
+            if (!Tizen.System.Information.TryGetValue(name, out bool enabled)) {
+                unchecked {
+                    Interop.CheckNThrowException((int)Interop.LibteecError.Generic, "Failed to query for TEE feature");
+                }
+            }
+            return enabled;
+        }
+    }
+
     /// <summary>
     /// This type denotes a TEE Context, the main logical container linking a Client Application with a particular TEE.
     /// </summary>
@@ -778,12 +792,17 @@ namespace Tizen.Security.TEEC
         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
         public Context(string name)
         {
+            if (!TeeFeature.IsEnabled())
+                unchecked {
+                    Interop.CheckNThrowException((int)Interop.LibteecError.NotSupported, string.Format("InitializeContext('{0}')", name));
+                }
+
             context_imp = Marshal.AllocHGlobal(Marshal.SizeOf<Interop.TEEC_Context>());
             if (name != null && name.Length == 0)
                 name = null;
             try {
                 int ret = Interop.Libteec.InitializeContext(name, context_imp);
-                Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
+                Interop.CheckNThrowException(ret, string.Format("InitializeContext('{0}')", name));
                 initialized = true;
             }
             catch (global::System.DllNotFoundException e)