/// </summary>
//TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_InitializeContext", CallingConvention = CallingConvention.Cdecl)]
- static public extern int InitializeContext(string name, ref TEEC_Context context);
+ static public extern int InitializeContext(string name, IntPtr context);
/// <summary>
/// This function destroys an initialized TEE Context, closing the connection between the client application
/// </summary>
//void TEEC_FinalizeContext(TEEC_Context *context);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_FinalizeContext", CallingConvention = CallingConvention.Cdecl)]
- static public extern void FinalizeContext(ref TEEC_Context context);
+ static public extern void FinalizeContext(IntPtr context);
/// <summary>
/// This function registers a block of existing client application memory as a block of shared memory within
/// </summary>
//EEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_RegisterSharedMemory", CallingConvention = CallingConvention.Cdecl)]
- static public extern int RegisterSharedMemory(ref TEEC_Context context, ref TEEC_SharedMemory sharedMem);
+ static public extern int RegisterSharedMemory(IntPtr context, ref TEEC_SharedMemory sharedMem);
/// <summary>
/// This function allocates a new block of memory as a block of shared memory within the scope of the
/// </summary>
//TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_AllocateSharedMemory", CallingConvention = CallingConvention.Cdecl)]
- static public extern int AllocateSharedMemory(ref TEEC_Context context, ref TEEC_SharedMemory sharedMem);
+ static public extern int AllocateSharedMemory(IntPtr context, ref TEEC_SharedMemory sharedMem);
/// <summary>
/// This function deregisters or deallocates a previously initialized block of the shared memory.
/// </summary>
//TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, const TEEC_UUID *destination, uint connectionMethod, const void *connectionData, TEEC_Operation *operation, uint *returnOrigin);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_OpenSession", CallingConvention = CallingConvention.Cdecl)]
- static public extern int OpenSession(ref TEEC_Context context, ref TEEC_Session session, ref TEEC_UUID destination, uint connectionMethod, byte[] connectionData, ref TEEC_Operation operation, out uint returnOrigin);
-
- [DllImport(Libraries.Libteec, EntryPoint = "TEEC_OpenSession", CallingConvention = CallingConvention.Cdecl)]
- static public extern int OpenSession(ref TEEC_Context context, ref TEEC_Session session, ref TEEC_UUID destination, uint connectionMethod, byte[] connectionData, IntPtr operation, out uint returnOrigin);
+ static public extern int OpenSession(IntPtr context, IntPtr session, ref TEEC_UUID destination, uint connectionMethod, byte[] connectionData, IntPtr operation, out uint returnOrigin);
/// <summary>
/// This function closes a session which has been opened with a trusted application.
/// </summary>
//void TEEC_CloseSession(TEEC_Session *session);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_CloseSession", CallingConvention = CallingConvention.Cdecl)]
- static public extern void CloseSession(ref TEEC_Session session);
+ static public extern void CloseSession(IntPtr session);
/// <summary>
/// This function invokes a command within the specified session.
/// </summary>
//TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint commandID, TEEC_Operation *operation, uint *returnOrigin);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_InvokeCommand", CallingConvention = CallingConvention.Cdecl)]
- static public extern int InvokeCommand(ref TEEC_Session session, uint commandID, ref TEEC_Operation operation, out uint returnOrigin);
-
- [DllImport(Libraries.Libteec, EntryPoint = "TEEC_InvokeCommand", CallingConvention = CallingConvention.Cdecl)]
- static public extern int InvokeCommand(ref TEEC_Session session, uint commandID, IntPtr operation, out uint returnOrigin);
+ static public extern int InvokeCommand(IntPtr session, uint commandID, IntPtr operation, out uint returnOrigin);
/// <summary>
/// This function requests the cancelation of a pending open session operation or a command invocation
/// </summary>
//void TEEC_RequestCancellation(TEEC_Operation *operation);
[DllImport(Libraries.Libteec, EntryPoint = "TEEC_RequestCancellation", CallingConvention = CallingConvention.Cdecl)]
- static public extern void RequestCancellation(ref TEEC_Operation operation);
+ static public extern void RequestCancellation(IntPtr operation);
}
}
/// <exception cref="InvalidOperationException">The operation is invalid.</exception>
public void SetData(byte[] data, int dstOffs)
{
- if ((shm.flags & (uint)SharedMemoryFlags.Output) == 0) throw new InvalidOperationException("No write access");
Marshal.Copy(data, 0, shm.buffer + dstOffs, data.Length);
}
/// <summary>
/// <exception cref="InvalidOperationException">The operation is invalid.</exception>
public void GetData(byte[] data, int srcOffs)
{
- if ((shm.flags & (uint)SharedMemoryFlags.Input) == 0) throw new InvalidOperationException("No read access");
Marshal.Copy(shm.buffer + srcOffs, data, 0, data.Length);
}
};
/// <since_tizen> 3 </since_tizen>
public sealed class Session
{
- private Interop.TEEC_Context context;
- private Interop.TEEC_Session session;
+ private Context context;
+ private IntPtr session_imp;
+ private IntPtr opptr;
+ private SharedMemory[] shm = new SharedMemory[4];
- internal Session(Interop.TEEC_Context context) {
+ internal Session(Context context) {
this.context = context;
- this.session = new Interop.TEEC_Session();
+ this.session_imp = Marshal.AllocHGlobal(Marshal.SizeOf<Interop.TEEC_Session>());
+ this.opptr = Marshal.AllocHGlobal(Marshal.SizeOf<Interop.TEEC_Operation64>());
+ for (int i=0; i < 4; ++i) shm[i] = null;
}
/// <summary>
~Session()
{
Close();
+ Marshal.FreeHGlobal(this.opptr);
+ Marshal.FreeHGlobal(this.session_imp);
}
- static internal UInt32 InitParam(ref Interop.TEEC_Parameter[] dst, int i, Parameter src)
+ internal UInt32 InitParam(ref Interop.TEEC_Parameter32[] dst, int i, Parameter src)
{
+ if (IntPtr.Size != 4) throw new Exception("wrong arch - not 32bit");
+ UInt32 rType = src.NativeType;
switch (src.NativeType) {
case (int)TEFValueType.Input:
case (int)TEFValueType.Output:
case (int)TEFTempMemoryType.Input:
case (int)TEFTempMemoryType.Output:
case (int)TEFTempMemoryType.InOut:
- dst[i].tmpref.buffer = ((TempMemoryReference)src).Buffer;
- dst[i].tmpref.size = new UIntPtr(((TempMemoryReference)src).Size);
+ byte[] mem = new byte[(uint)((TempMemoryReference)src).Size];
+ Marshal.Copy(((TempMemoryReference)src).Buffer, mem, 0, mem.Length);
+ shm[i] = context.AllocateSharedMemory((uint)mem.Length, SharedMemoryFlags.InOut);
+ Marshal.Copy(mem, 0, shm[i].shm.buffer, mem.Length);
+ dst[i].tmpref.size = (uint)mem.Length;
+ dst[i].tmpref.buffer = shm[i].shm.buffer.ToInt32();
break;
case (int)TEFRegisteredMemoryType.Whole:
+ RegisteredMemoryReference rmrw = (RegisteredMemoryReference)src;
+ rType = ((int)TEFRegisteredMemoryType.PartialInput & (int)TEFRegisteredMemoryType.PartialOutput) | rmrw.Parent.shm.flags;
+ dst[i].memref.parent = rmrw.Parent.shmptr.ToInt32();
+ dst[i].memref.size = rmrw.Size;
+ dst[i].memref.offset = rmrw.Offset;
+ break;
+
case (int)TEFRegisteredMemoryType.PartialInput:
case (int)TEFRegisteredMemoryType.PartialOutput:
case (int)TEFRegisteredMemoryType.PartialInOut:
RegisteredMemoryReference rmr = (RegisteredMemoryReference)src;
- dst[i].memref.parent = rmr.Parent.shmptr;
- dst[i].memref.size = new UIntPtr(rmr.Size);
- dst[i].memref.offset = new UIntPtr(rmr.Offset);
+ dst[i].memref.parent = rmr.Parent.shmptr.ToInt32();
+ dst[i].memref.size = rmr.Size;
+ dst[i].memref.offset = rmr.Offset;
break;
default: return 0;
}
- return src.NativeType;
+ return rType;
}
- static internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter[] dst, int i)
+ internal void UpdateParam(Interop.TEEC_Parameter32 src, ref Parameter[] dst, int i)
{
+ if (IntPtr.Size != 4) throw new Exception("wrong arch - not 32bit");
+ switch (dst[i].NativeType) {
+ case (int)TEFValueType.Input:
+ case (int)TEFValueType.Output:
+ case (int)TEFValueType.InOut:
+ ((Value)dst[i]).A = src.value.a;
+ ((Value)dst[i]).B = src.value.b;
+ break;
+
+ case (int)TEFTempMemoryType.Input:
+ case (int)TEFTempMemoryType.Output:
+ case (int)TEFTempMemoryType.InOut:
+ byte[] mem = new byte[src.tmpref.size];
+ Marshal.Copy(shm[i].shm.buffer, mem, 0, mem.Length);
+ Marshal.Copy(mem, 0, ((TempMemoryReference)dst[i]).Buffer, mem.Length);
+ ((TempMemoryReference)dst[i]).Size = src.tmpref.size;
+ break;
+
+ case (int)TEFRegisteredMemoryType.Whole:
+ case (int)TEFRegisteredMemoryType.PartialInput:
+ case (int)TEFRegisteredMemoryType.PartialOutput:
+ case (int)TEFRegisteredMemoryType.PartialInOut:
+ ((RegisteredMemoryReference)dst[i]).Size = src.memref.size;
+ break;
+
+ default: break;
+ }
+ }
+
+ static internal Interop.TEEC_Operation32 Create_TEEC_Operation32() {
+ Interop.TEEC_Operation32 op = new Interop.TEEC_Operation32();
+ op.started=0;
+ op.paramTypes=0;
+ op.paramlist = new Interop.TEEC_Parameter32[4];
+ return op;
+ }
+
+ internal UInt32 InitParam(ref Interop.TEEC_Parameter64[] dst, int i, Parameter src)
+ {
+ if (IntPtr.Size != 8) throw new Exception("wrong arch - not 64bit");
+ UInt32 rType = src.NativeType;
+ switch (src.NativeType) {
+ case (int)TEFValueType.Input:
+ case (int)TEFValueType.Output:
+ case (int)TEFValueType.InOut:
+ dst[i].value.a = ((Value)src).A;
+ dst[i].value.b = ((Value)src).B;
+ break;
+
+ case (int)TEFTempMemoryType.Input:
+ case (int)TEFTempMemoryType.Output:
+ case (int)TEFTempMemoryType.InOut:
+ byte[] mem = new byte[(uint)((TempMemoryReference)src).Size];
+ Marshal.Copy(((TempMemoryReference)src).Buffer, mem, 0, mem.Length);
+ shm[i] = context.AllocateSharedMemory((uint)mem.Length, SharedMemoryFlags.InOut);
+ Marshal.Copy(mem, 0, shm[i].shm.buffer, mem.Length);
+ dst[i].tmpref.size = (UInt64)mem.Length;
+ dst[i].tmpref.buffer = shm[i].shm.buffer.ToInt64();
+ break;
+
+ case (int)TEFRegisteredMemoryType.Whole:
+ RegisteredMemoryReference rmrw = (RegisteredMemoryReference)src;
+ rType = ((int)TEFRegisteredMemoryType.PartialInput & (int)TEFRegisteredMemoryType.PartialOutput) | rmrw.Parent.shm.flags;
+ dst[i].memref.parent = rmrw.Parent.shmptr.ToInt64();
+ dst[i].memref.size = rmrw.Size;
+ dst[i].memref.offset = rmrw.Offset;
+ break;
+
+ case (int)TEFRegisteredMemoryType.PartialInput:
+ case (int)TEFRegisteredMemoryType.PartialOutput:
+ case (int)TEFRegisteredMemoryType.PartialInOut:
+ RegisteredMemoryReference rmr = (RegisteredMemoryReference)src;
+ dst[i].memref.parent = rmr.Parent.shmptr.ToInt64();
+ dst[i].memref.size = rmr.Size;
+ dst[i].memref.offset = rmr.Offset;
+ break;
+
+ default:
+ break;
+ }
+ return rType;
+ }
+
+ internal void UpdateParam(Interop.TEEC_Parameter64 src, ref Parameter[] dst, int i)
+ {
+ if (IntPtr.Size != 8) throw new Exception("wrong arch - not 64bit");
switch (dst[i].NativeType) {
case (int)TEFValueType.Input:
case (int)TEFValueType.Output:
case (int)TEFTempMemoryType.Input:
case (int)TEFTempMemoryType.Output:
case (int)TEFTempMemoryType.InOut:
- ((TempMemoryReference)dst[i]).Size = src.tmpref.size.ToUInt32();
+ byte[] mem = new byte[src.tmpref.size];
+ Marshal.Copy(shm[i].shm.buffer, mem, 0, mem.Length);
+ Marshal.Copy(mem, 0, ((TempMemoryReference)dst[i]).Buffer, mem.Length);
+ ((TempMemoryReference)dst[i]).Size = (uint)src.tmpref.size;
break;
case (int)TEFRegisteredMemoryType.Whole:
case (int)TEFRegisteredMemoryType.PartialInput:
case (int)TEFRegisteredMemoryType.PartialOutput:
case (int)TEFRegisteredMemoryType.PartialInOut:
- ((RegisteredMemoryReference)dst[i]).Size = src.memref.size.ToUInt32();
+ ((RegisteredMemoryReference)dst[i]).Size = (uint)src.memref.size;
break;
default: break;
}
}
- static internal Interop.TEEC_Operation Create_TEEC_Operation() {
- Interop.TEEC_Operation op = new Interop.TEEC_Operation();
+ static internal Interop.TEEC_Operation64 Create_TEEC_Operation64() {
+ Interop.TEEC_Operation64 op = new Interop.TEEC_Operation64();
op.started=0;
op.paramTypes=0;
- op.paramlist = new Interop.TEEC_Parameter[4];
+ op.paramlist = new Interop.TEEC_Parameter64[4];
return op;
}
- internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
+ internal void Open32(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
+ {
+ Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
+
+ int ret;
+ uint ro;
+ if (paramlist != null) {
+ Interop.TEEC_Operation32 op = Create_TEEC_Operation32();
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (8*i);
+ }
+ Marshal.StructureToPtr(op, opptr, false);
+ ret = Interop.Libteec.OpenSession(context.context_imp, session_imp, ref uuid, loginMethod, connectionData, opptr, out ro);
+ op = Marshal.PtrToStructure<Interop.TEEC_Operation32>(opptr);
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ UpdateParam(op.paramlist[i], ref paramlist, i);
+ }
+ }
+ else {
+ ret = Interop.Libteec.OpenSession(context.context_imp, session_imp, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro);
+ }
+
+ for (int i=0; i < 4; ++i) {
+ if (shm[i] != null) {
+ context.ReleaseSharedMemory(shm[i]);
+ shm[i] = null;
+ }
+ }
+
+ //MAYBE map origin of return code to specyfic Exception
+ Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination));
+ }
+ internal void Open64(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
{
Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
int ret;
uint ro;
if (paramlist != null) {
- Interop.TEEC_Operation op = Create_TEEC_Operation();
+ Interop.TEEC_Operation64 op = Create_TEEC_Operation64();
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
- op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
+ op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (8*i);
}
- ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro);
+ Marshal.StructureToPtr(op, opptr, false);
+ ret = Interop.Libteec.OpenSession(context.context_imp, session_imp, ref uuid, loginMethod, connectionData, opptr, out ro);
+ op = Marshal.PtrToStructure<Interop.TEEC_Operation64>(opptr);
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
UpdateParam(op.paramlist[i], ref paramlist, i);
}
}
else {
- ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro);
+ ret = Interop.Libteec.OpenSession(context.context_imp, session_imp, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro);
+ }
+
+ for (int i=0; i < 4; ++i) {
+ if (shm[i] != null) {
+ context.ReleaseSharedMemory(shm[i]);
+ shm[i] = null;
+ }
}
//MAYBE map origin of return code to specyfic Exception
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <exception cref="InvalidOperationException">The operation is invalid.</exception>
public void Close() {
- Interop.Libteec.CloseSession(ref session);
+ Interop.Libteec.CloseSession(session_imp);
}
/// <summary>
int ret;
uint ro;
if (paramlist != null) {
- Interop.TEEC_Operation op = Create_TEEC_Operation();
- for (int i=0; i < 4 && i < paramlist.Length; ++i) {
- op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
+ if (IntPtr.Size == 4) {
+ Interop.TEEC_Operation32 op = Create_TEEC_Operation32();
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (8*i);
+ }
+ Marshal.StructureToPtr(op, opptr, false);
+ ret = Interop.Libteec.InvokeCommand(session_imp, commandID, opptr, out ro);
+ op = Marshal.PtrToStructure<Interop.TEEC_Operation32>(opptr);
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ UpdateParam(op.paramlist[i], ref paramlist, i);
+ }
}
- ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro);
- for (int i=0; i < 4 && i < paramlist.Length; ++i) {
- UpdateParam(op.paramlist[i], ref paramlist, i);
+ else {
+ Interop.TEEC_Operation64 op = Create_TEEC_Operation64();
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (8*i);
+ }
+ Marshal.StructureToPtr(op, opptr, false);
+ ret = Interop.Libteec.InvokeCommand(session_imp, commandID, opptr, out ro);
+ op = Marshal.PtrToStructure<Interop.TEEC_Operation64>(opptr);
+ for (int i=0; i < 4 && i < paramlist.Length; ++i) {
+ UpdateParam(op.paramlist[i], ref paramlist, i);
+ }
}
}
else {
- ret = Interop.Libteec.InvokeCommand(ref session, commandID, IntPtr.Zero, out ro);
+ ret = Interop.Libteec.InvokeCommand(session_imp, commandID, IntPtr.Zero, out ro);
+ }
+
+ for (int i=0; i < 4; ++i) {
+ if (shm[i] != null) {
+ context.ReleaseSharedMemory(shm[i]);
+ shm[i] = null;
+ }
}
//MAYBE map origin of return code to specific Exception
/// <since_tizen> 3 </since_tizen>
public sealed class Context : IDisposable
{
- private Interop.TEEC_Context context;
+ internal IntPtr context_imp;
/// <summary>
/// This function (constructor) initializes a new TEE Context, forming a connection between this client application and the
/// <exception cref="InvalidOperationException">The operation is invalid.</exception>
public Context(string name)
{
- context = new Interop.TEEC_Context();
+ context_imp = Marshal.AllocHGlobal(Marshal.SizeOf<Interop.TEEC_Context>());
if (name != null && name.Length == 0)
name = null;
try {
- int ret = Interop.Libteec.InitializeContext(name, ref context);
+ int ret = Interop.Libteec.InitializeContext(name, context_imp);
Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
}
catch (global::System.DllNotFoundException e)
/// <feature>http://tizen.org/feature/security.tee</feature>
public void Dispose() {
try {
- Interop.Libteec.FinalizeContext(ref context);
+ Interop.Libteec.FinalizeContext(context_imp);
}
catch (global::System.DllNotFoundException) { }
}
/// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
public Session OpenSession(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
{
- Session ses = new Session(context);
- ses.Open(destination, loginMethod, connectionData, paramlist);
+ Session ses = new Session(this);
+ if (IntPtr.Size == 4)
+ ses.Open32(destination, loginMethod, connectionData, paramlist);
+ else if (IntPtr.Size == 8)
+ ses.Open64(destination, loginMethod, connectionData, paramlist);
+ else throw new NotSupportedException("unsupported arch");
return ses;
}
/// <summary>
/// <exception cref="InvalidOperationException">The operation is invalid.</exception>
public Session OpenSession(Guid destination)
{
- Session ses = new Session(context);
- ses.Open(destination, LoginMethod.Public, null, null);
+ Session ses = new Session(this);
+ if (IntPtr.Size == 4)
+ ses.Open32(destination, LoginMethod.Public, null, null);
+ else if (IntPtr.Size == 8)
+ ses.Open64(destination, LoginMethod.Public, null, null);
+ else throw new NotSupportedException("unsupported arch");
return ses;
}
shm.buffer = memaddr;
shm.size = new UIntPtr(size);
shm.flags = (UInt32)flags;
- int ret = Interop.Libteec.RegisterSharedMemory(ref context, ref shm);
+ int ret = Interop.Libteec.RegisterSharedMemory(context_imp, ref shm);
Interop.CheckNThrowException(ret, "RegisterSharedMemory");
return new SharedMemory(ref shm);
}
Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
shm.size = new UIntPtr(size);
shm.flags = (UInt32)flags;
- int ret = Interop.Libteec.AllocateSharedMemory(ref context, ref shm);
+ int ret = Interop.Libteec.AllocateSharedMemory(context_imp, ref shm);
Interop.CheckNThrowException(ret, "AllocateSharedMemory");
return new SharedMemory(ref shm);
}