GCHandle can only accept primitive type obj
authorKyungwook Tak <k.tak@samsung.com>
Wed, 2 Nov 2016 08:24:29 +0000 (17:24 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Wed, 2 Nov 2016 08:50:12 +0000 (17:50 +0900)
Convert structs to ptr by handle, not by PinnedObject.
PinnedObject can be used only for primitive types e.g., byte array.

Change-Id: I8bb10c57845554694a294cf59cfa4ace9a3d06d0
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/Tizen.Security.SecureRepository/Interop/Interop.CkmcTypes.cs
src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/Certificate.cs
src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/Key.cs
src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/Pkcs12.cs
src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/Pkcs12Manager.cs
src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/SafeCertificateListHandle.cs

index 98b87ac..6482a35 100644 (file)
@@ -111,12 +111,16 @@ internal static partial class Interop
 
     internal static partial class CkmcTypes
     {
+        [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_key_new", CallingConvention = CallingConvention.Cdecl)]
+        public static extern int KeyNew(byte[] rawKey, UIntPtr size, int keyType, string password, out IntPtr cert);
+        // int ckmc_key_new(unsigned char *raw_key, size_t key_size, ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey);
+        //
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_key_free", CallingConvention = CallingConvention.Cdecl)]
         public static extern void KeyFree(IntPtr buffer);
         // void ckmc_key_free(ckmc_key_s *key);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_buffer_new", CallingConvention = CallingConvention.Cdecl)]
-        public static extern int BufferNew(byte[] data, uint size, out IntPtr buffer);
+        public static extern int BufferNew(byte[] data, UIntPtr size, out IntPtr buffer);
         // int ckmc_buffer_new(unsigned char *data, size_t size, ckmc_raw_buffer_s** ppbuffer);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_buffer_free", CallingConvention = CallingConvention.Cdecl)]
@@ -124,7 +128,7 @@ internal static partial class Interop
         // void ckmc_buffer_free(ckmc_raw_buffer_s* buffer);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_cert_new", CallingConvention = CallingConvention.Cdecl)]
-        public static extern int CertNew(byte[] rawCert, uint size, int dataFormat, out IntPtr cert);
+        public static extern int CertNew(byte[] rawCert, UIntPtr size, int dataFormat, out IntPtr cert);
         // int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size, ckmc_data_format_e data_format, ckmc_cert_s** ppcert);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_cert_free", CallingConvention = CallingConvention.Cdecl)]
@@ -136,7 +140,7 @@ internal static partial class Interop
         // int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_pkcs12_new", CallingConvention = CallingConvention.Cdecl)]
-        public static extern int Pkcs12New(string filePath, out IntPtr cert);
+        public static extern int Pkcs12New(IntPtr key, IntPtr cert, IntPtr caCerts, out IntPtr p12_bundle);
         // int ckmc_pkcs12_new(ckmc_key_s *private_key, ckmc_cert_s* cert, ckmc_cert_list_s *ca_cert_list, ckmc_pkcs12_s** pkcs12_bundle);
 
         [DllImport(Libraries.KeyManager, EntryPoint = "ckmc_pkcs12_load", CallingConvention = CallingConvention.Cdecl)]
index 85adb54..2615689 100755 (executable)
@@ -63,6 +63,20 @@ namespace Tizen.Security.SecureRepository
             Format = (DataFormat)ckmcCert.dataFormat;
         }
 
+        internal IntPtr GetHandle()
+        {
+            if (this.handle == IntPtr.Zero)
+            {
+                int ret = Interop.CkmcTypes.CertNew(this.Binary,
+                                                    (UIntPtr)this.Binary.Length,
+                                                    (int)this.Format,
+                                                    out this.handle);
+                Interop.CheckNThrowException(ret, "Failed to create cert");
+            }
+
+            return this.handle;
+        }
+
         /// <summary>
         /// The binary value of a certificate.
         /// </summary>
index 3035f8e..83fcb41 100755 (executable)
@@ -50,6 +50,21 @@ namespace Tizen.Security.SecureRepository
             BinaryPassword = ckmcKey.password;
         }
 
+        internal IntPtr GetHandle()
+        {
+            if (this.handle == IntPtr.Zero)
+            {
+                int ret = Interop.CkmcTypes.KeyNew(this.Binary,
+                                                   (UIntPtr)this.Binary.Length,
+                                                   (int)this.Type,
+                                                   this.BinaryPassword,
+                                                   out this.handle);
+                Interop.CheckNThrowException(ret, "Failed to create key");
+            }
+
+            return this.handle;
+        }
+
         /// <summary>
         /// The binary value of a key.
         /// </summary>
index a03b442..e0ca65b 100755 (executable)
@@ -90,6 +90,28 @@ namespace Tizen.Security.SecureRepository
                 this.CaChain = new SafeCertificateListHandle(ckmcPkcs12.caChain, false).Certificates;
         }
 
+        internal IntPtr GetHandle()
+        {
+            if (this.PrivateKey == null)
+                return IntPtr.Zero;
+
+            IntPtr keyPtr = this.PrivateKey.GetHandle();
+            IntPtr certPtr = this.Certificate != null ?
+                    this.Certificate.GetHandle() : IntPtr.Zero;
+
+            if (this.handle == IntPtr.Zero)
+            {
+                var caCerts = new SafeCertificateListHandle(this.CaChain);
+                int ret = Interop.CkmcTypes.Pkcs12New(keyPtr,
+                                                      certPtr,
+                                                      caCerts.ToCkmcCertificateListPtr(),
+                                                      out this.handle);
+                Interop.CheckNThrowException(ret, "Failed to create pkcs12");
+            }
+
+            return this.handle;
+        }
+
         /// <summary>
         /// A private key.
         /// </summary>
@@ -116,15 +138,11 @@ namespace Tizen.Security.SecureRepository
 
         internal CkmcPkcs12 ToCkmcPkcs12()
         {
-            Interop.CkmcKey ckmcKey = (PrivateKey != null) ?
-                                            PrivateKey.ToCkmcKey() : new Interop.CkmcKey(IntPtr.Zero, 0, 0, null);
-            Interop.CkmcCert ckmcCert = (Certificate != null) ?
-                                            Certificate.ToCkmcCert() : new Interop.CkmcCert(IntPtr.Zero, 0, 0);
             SafeCertificateListHandle ckmcCaCerts = new SafeCertificateListHandle(CaChain);
 
-            return new Interop.CkmcPkcs12(new PinnedObject(ckmcKey),
-                                            new PinnedObject(ckmcCert),
-                                            ckmcCaCerts.ToCkmcCertificateListPtr());
+            return new Interop.CkmcPkcs12(PrivateKey.GetHandle(),
+                                          Certificate.GetHandle(),
+                                          ckmcCaCerts.ToCkmcCertificateListPtr());
         }
 
         /// <summary>
index e1c66df..8241e3a 100644 (file)
@@ -63,7 +63,7 @@ namespace Tizen.Security.SecureRepository
         static public void Save(string alias, Pkcs12 pkcs12, Policy keyPolicy, Policy certificatePolicy)
         {
             int ret = Interop.CkmcManager.SavePkcs12(alias,
-                                                     new PinnedObject(pkcs12.ToCkmcPkcs12()),
+                                                     pkcs12.GetHandle(),
                                                      keyPolicy.ToCkmcPolicy(),
                                                      certificatePolicy.ToCkmcPolicy());
             Interop.CheckNThrowException(ret, "Failed to save PKCS12. alias=" + alias);
index 9f87d46..8c7bea9 100755 (executable)
@@ -67,7 +67,7 @@ namespace Tizen.Security.SecureRepository
             foreach (Certificate cert in _certificates)
             {
                 IntPtr certPtr;
-                ret = Interop.CkmcTypes.CertNew(cert.Binary, (uint)cert.Binary.Length, (int)cert.Format, out certPtr);
+                ret = Interop.CkmcTypes.CertNew(cert.Binary, (UIntPtr)cert.Binary.Length, (int)cert.Format, out certPtr);
                 Interop.CheckNThrowException(ret, "Failed to create new Certificate.");
 
                 IntPtr outCertList;