2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading;
21 using System.Threading.Tasks;
23 namespace Tizen.Security.TEEC
26 /// This type denotes the Session Login Method used in OpenSession.
28 /// <since_tizen> 3 </since_tizen>
29 public class LoginMethod
31 /// <summary>No login data is provided.</summary>
32 /// <since_tizen> 3 </since_tizen>
33 public const uint Public = 0x00000000;
34 /// <summary>Login data about the user running the client application process is provided.</summary>
35 /// <since_tizen> 3 </since_tizen>
36 public const uint User = 0x00000001;
37 /// <summary>Login data about the group running the client application process is provided.</summary>
38 /// <since_tizen> 3 </since_tizen>
39 public const uint Group = 0x00000002;
40 /// <summary>Login data about the running client application itself is provided.</summary>
41 /// <since_tizen> 3 </since_tizen>
42 public const uint Application = 0x00000003;
46 /// This type denotes the Value parameter.
48 /// <since_tizen> 3 </since_tizen>
49 public enum TEFValueType : UInt32
51 /// <summary>The parameter is a ValueType tagged as the input.</summary>
53 /// <summary>The parameter is a ValueType tagged as the output.</summary>
55 /// <summary>The parameter is a ValueType tagged as both the input and the output.</summary>
60 /// This type denotes the TempMemoryReference parameter
61 /// describing a region of memory which needs to be temporarily registered for the duration of the operation.
63 /// <since_tizen> 3 </since_tizen>
64 public enum TEFTempMemoryType : UInt32
66 /// <summary>The parameter is a TempMemoryType and is tagged as the input.</summary>
68 /// <summary>Same as the input, but the Memory Reference is tagged as the output.</summary>
70 /// <summary>A Temporary Memory Reference tagged as both the input and the output.</summary>
75 /// This type denotes the SharedMemoryReference parameter.
77 /// <since_tizen> 3 </since_tizen>
78 public enum TEFRegisteredMemoryType : UInt32
80 /// <summary>The parameter is a registered memory reference that refers to the entirety of its parent shared memory block.</summary>
82 /// <summary>A registered memory reference structure that refers to a partial region of its parent shared mMemory block and is tagged as the input.</summary>
83 PartialInput = 0x0000000D,
84 /// <summary>A registered memory reference structure that refers to a partial region of its parent shared memory block and is tagged as the output.</summary>
85 PartialOutput = 0x0000000E,
86 /// <summary>A registered memory reference structure that refers to a partial region of its parent shared memory block and is tagged as both the input and the output.</summary>
87 PartialInOut = 0x0000000F,
91 /// This type denotes the SharedMemory access direction.
93 /// <since_tizen> 3 </since_tizen>
95 public enum SharedMemoryFlags : UInt32
97 /// <summary>A flag indicates the shared memory can be read.</summary>
99 /// <summary>A flag indicates the shared memory can be written.</summary>
101 /// <summary>A flag indicates the shared memory can be read and written.</summary>
102 InOut = Input | Output,
106 /// This type denotes a shared memory block which has been either registered
107 /// with the implementation or allocated by it.
109 /// <since_tizen> 3 </since_tizen>
110 public sealed class SharedMemory
112 internal Interop.TEEC_SharedMemory shm;
113 internal IntPtr shmptr;
114 internal SharedMemory(ref Interop.TEEC_SharedMemory shm)
117 shmptr = Marshal.AllocHGlobal(Marshal.SizeOf(shm));
118 Marshal.StructureToPtr(shm, shmptr, false);
122 Marshal.FreeHGlobal(shmptr);
125 /// This property represents the shared memory size in bytes.
127 /// <since_tizen> 3 </since_tizen>
128 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
129 /// <privlevel>partner</privlevel>
132 get { return shm.size.ToUInt32(); }
135 /// This property represents the start address of the shared memory block.
137 /// <since_tizen> 3 </since_tizen>
138 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
139 /// <privlevel>partner</privlevel>
140 public IntPtr Address
142 get { return shm.buffer; }
146 /// This function makes a copy and is designed for convenient operations on small buffers.
147 /// For large buffers, the direct address should be used.
149 /// <since_tizen> 3 </since_tizen>
150 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
151 /// <privlevel>partner</privlevel>
152 /// <param name="data">The source data buffer to copy data from.</param>
153 /// <param name="dstOffs">The starting offset in the destination shared memory.</param>
154 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
155 public void SetData(byte[] data, int dstOffs)
157 if ((shm.flags & (uint)SharedMemoryFlags.Output) == 0) throw new InvalidOperationException("No write access");
158 Marshal.Copy(data, 0, shm.buffer + dstOffs, data.Length);
161 /// This function makes a copy and is designed for convenient operations on small buffers.
162 /// For large buffers, the direct address should be used.
164 /// <since_tizen> 3 </since_tizen>
165 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
166 /// <privlevel>partner</privlevel>
167 /// <param name="data">The destination data buffer to copy data into.</param>
168 /// <param name="srcOffs">The starting offset in the source shared memory.</param>
169 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
170 public void GetData(byte[] data, int srcOffs)
172 if ((shm.flags & (uint)SharedMemoryFlags.Input) == 0) throw new InvalidOperationException("No read access");
173 Marshal.Copy(shm.buffer + srcOffs, data, 0, data.Length);
178 /// This type defines the payload of either an open session operation or an invoke command operation. It is
179 /// also used for cancelation of operations, which may be desirable even if no payload is passed.
180 /// Parameters are used to exchange data between CA and TA.
182 /// <since_tizen> 3 </since_tizen>
183 public abstract class Parameter
185 internal Parameter(uint t)
189 internal uint NativeType { get; }
193 /// This type defines a template for the parameter types.
195 /// <since_tizen> 3 </since_tizen>
196 public abstract class BaseParameter<TEnum> : Parameter where TEnum : struct, IComparable, IFormattable, IConvertible // as close to Enum as possible
198 internal BaseParameter(TEnum t) : base((uint)(object)t)
204 /// This property represents the access type to this parameter.
206 /// <since_tizen> 3 </since_tizen>
207 public TEnum Type { get; }
211 /// This type defines a temporary memory reference.
213 /// <since_tizen> 3 </since_tizen>
214 public sealed class TempMemoryReference : BaseParameter<TEFTempMemoryType>
217 /// Constructs a parameter object which holds information about the temporary memory copied to or from TA.
219 /// <since_tizen> 3 </since_tizen>
220 /// <param name="buffer">The address of the allocated memory buffer.</param>
221 /// <param name="size">The size of the buffer.</param>
222 /// <param name="type">The kind of access allowed for TA <see cref="TEFTempMemoryType"/>.</param>
223 public TempMemoryReference(IntPtr buffer, uint size, TEFTempMemoryType type) :
226 this.Buffer = buffer;
230 /// This property represents the memory address of the buffer.
232 /// <since_tizen> 3 </since_tizen>
233 public IntPtr Buffer { get; }
235 /// This property represents the size of the buffer.
237 /// <since_tizen> 3 </since_tizen>
238 public uint Size { get; internal set; }
242 /// This type defines a memory reference that uses a pre-registered or pre-allocated shared memory block.
244 /// <since_tizen> 3 </since_tizen>
245 public sealed class RegisteredMemoryReference : BaseParameter<TEFRegisteredMemoryType>
248 /// Constructs a parameter object which holds information about the registered memory shared with TA.
250 /// <since_tizen> 3 </since_tizen>
251 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
252 /// <privlevel>partner</privlevel>
253 /// <param name="parent">The shared memory - registered or allocated.</param>
254 /// <param name="size">The size of the buffer part.</param>
255 /// <param name="offset">The offset of the buffer in the shared memory.</param>
256 /// <param name="type">The kind of access allowed for TA <see cref="TEFRegisteredMemoryType"/>.</param>
257 public RegisteredMemoryReference(SharedMemory parent, uint size, uint offset, TEFRegisteredMemoryType type) :
260 this.Parent = parent;
261 if (type == TEFRegisteredMemoryType.Whole) {
262 this.Size = parent.Size;
267 this.Offset = offset;
272 /// This property represents the shared memory that is referred to.
274 /// <since_tizen> 3 </since_tizen>
275 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
276 /// <privlevel>partner</privlevel>
277 public SharedMemory Parent { get; }
279 /// This property represents the size (in bytes) of the shared memory.
281 /// <since_tizen> 3 </since_tizen>
282 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
283 /// <privlevel>partner</privlevel>
284 public uint Size { get; internal set; }
286 /// This property represents the offset (in bytes) from the start of the shared memory.
288 /// <since_tizen> 3 </since_tizen>
289 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
290 /// <privlevel>partner</privlevel>
291 public uint Offset { get; }
295 /// This type defines a parameter that is not referencing the shared memory, but carries instead small raw data
296 /// passed by a value.
298 /// <since_tizen> 3 </since_tizen>
299 public sealed class Value : BaseParameter<TEFValueType>
302 /// Constructs a parameter object which holds information about integer values copied to or from TA.
304 /// <since_tizen> 3 </since_tizen>
305 /// <param name="a">User paramter A.</param>
306 /// <param name="b">User paramter B.</param>
307 /// <param name="type">The kind of access allowed for TA <see cref="TEFValueType"/>.</param>
308 public Value(uint a, uint b, TEFValueType type) :
315 /// This property represents an unsigned integer A.
317 /// <since_tizen> 3 </since_tizen>
318 public uint A { get; internal set; }
320 /// This property represents an unsigned integer B.
322 /// <since_tizen> 3 </since_tizen>
323 public uint B { get; internal set; }
328 /// This type denotes a TEE Session, the logical container linking a client application with a particular trusted application.
330 /// <since_tizen> 3 </since_tizen>
331 public sealed class Session
333 private Interop.TEEC_Context context;
334 private Interop.TEEC_Session session;
336 internal Session(Interop.TEEC_Context context) {
337 this.context = context;
338 this.session = new Interop.TEEC_Session();
342 /// Destructor of the class.
349 static internal UInt32 InitParam(ref Interop.TEEC_Parameter[] dst, int i, Parameter src)
351 switch (src.NativeType) {
352 case (int)TEFValueType.Input:
353 case (int)TEFValueType.Output:
354 case (int)TEFValueType.InOut:
355 dst[i].value.a = ((Value)src).A;
356 dst[i].value.b = ((Value)src).B;
359 case (int)TEFTempMemoryType.Input:
360 case (int)TEFTempMemoryType.Output:
361 case (int)TEFTempMemoryType.InOut:
362 dst[i].tmpref.buffer = ((TempMemoryReference)src).Buffer;
363 dst[i].tmpref.size = new UIntPtr(((TempMemoryReference)src).Size);
366 case (int)TEFRegisteredMemoryType.Whole:
367 case (int)TEFRegisteredMemoryType.PartialInput:
368 case (int)TEFRegisteredMemoryType.PartialOutput:
369 case (int)TEFRegisteredMemoryType.PartialInOut:
370 RegisteredMemoryReference rmr = (RegisteredMemoryReference)src;
371 dst[i].memref.parent = rmr.Parent.shmptr;
372 dst[i].memref.size = new UIntPtr(rmr.Size);
373 dst[i].memref.offset = new UIntPtr(rmr.Offset);
378 return src.NativeType;
381 static internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter[] dst, int i)
383 switch (dst[i].NativeType) {
384 case (int)TEFValueType.Input:
385 case (int)TEFValueType.Output:
386 case (int)TEFValueType.InOut:
387 ((Value)dst[i]).A = src.value.a;
388 ((Value)dst[i]).B = src.value.b;
391 case (int)TEFTempMemoryType.Input:
392 case (int)TEFTempMemoryType.Output:
393 case (int)TEFTempMemoryType.InOut:
394 ((TempMemoryReference)dst[i]).Size = src.tmpref.size.ToUInt32();
397 case (int)TEFRegisteredMemoryType.Whole:
398 case (int)TEFRegisteredMemoryType.PartialInput:
399 case (int)TEFRegisteredMemoryType.PartialOutput:
400 case (int)TEFRegisteredMemoryType.PartialInOut:
401 ((RegisteredMemoryReference)dst[i]).Size = src.memref.size.ToUInt32();
408 static internal Interop.TEEC_Operation Create_TEEC_Operation() {
409 Interop.TEEC_Operation op = new Interop.TEEC_Operation();
412 op.paramlist = new Interop.TEEC_Parameter[4];
416 internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
418 Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
422 if (paramlist != null) {
423 Interop.TEEC_Operation op = Create_TEEC_Operation();
424 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
425 op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
427 ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro);
428 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
429 UpdateParam(op.paramlist[i], ref paramlist, i);
433 ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, IntPtr.Zero, out ro);
436 //MAYBE map origin of return code to specyfic Exception
437 Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination));
441 /// This function closes a session which has been opened with a trusted application.
442 /// All commands within the session must be completed before this function can be called.
444 /// <since_tizen> 3 </since_tizen>
445 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
446 /// <privlevel>partner</privlevel>
447 /// <feature>http://tizen.org/feature/security.tee</feature>
448 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
449 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
450 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
451 public void Close() {
452 Interop.Libteec.CloseSession(ref session);
456 /// This function invokes a command within the specified session.
457 /// The parameter commandID is an identifier that is used to indicate which of the exposed trusted
458 /// application functions should be invoked. The supported command identifier values are defined by the
459 /// trusted application's protocol.
460 /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
462 /// <since_tizen> 3 </since_tizen>
463 /// <param name="commandID">The command.</param>
464 /// <param name="paramlist">The array of parameters.</param>
465 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
466 /// <privlevel>partner</privlevel>
467 /// <feature>http://tizen.org/feature/security.tee</feature>
468 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
469 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
470 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
471 /// <exception cref="ArgumentException">The argument <paramref name="paramlist"/> is wrong.</exception>
472 public void InvokeCommand(uint commandID, Parameter[] paramlist)
476 if (paramlist != null) {
477 Interop.TEEC_Operation op = Create_TEEC_Operation();
478 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
479 op.paramTypes |= InitParam(ref op.paramlist, i, paramlist[i]) << (4*i);
481 ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro);
482 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
483 UpdateParam(op.paramlist[i], ref paramlist, i);
487 ret = Interop.Libteec.InvokeCommand(ref session, commandID, IntPtr.Zero, out ro);
490 //MAYBE map origin of return code to specific Exception
491 Interop.CheckNThrowException(ret, string.Format("InvokeCommand({0})", commandID));
495 /// The asynchronous version of the InvokeCommand.
497 /// <since_tizen> 3 </since_tizen>
498 /// <param name="commandID">The command.</param>
499 /// <param name="paramlist">The array of parameters.</param>
500 /// <param name="token">The token for task manipulation.</param>
501 /// <returns>Returns a task executing an invoke command in the background.</returns>
502 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
503 /// <privlevel>partner</privlevel>
504 /// <feature>http://tizen.org/feature/security.tee</feature>
505 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
506 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
507 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
508 /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
509 public async Task InvokeCommandAsync(uint commandID, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
511 await Task.Factory.StartNew(() => InvokeCommand(commandID, paramlist));
517 /// This type denotes a TEE Context, the main logical container linking a Client Application with a particular TEE.
519 /// <since_tizen> 3 </since_tizen>
520 public sealed class Context : IDisposable
522 private Interop.TEEC_Context context;
525 /// This function (constructor) initializes a new TEE Context, forming a connection between this client application and the
526 /// TEE identified by the string identifier name (empty or null name denotes a default TEE).
528 /// <since_tizen> 3 </since_tizen>
529 /// <param name="name">The TEE name.</param>
530 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
531 /// <privlevel>partner</privlevel>
532 /// <feature>http://tizen.org/feature/security.tee</feature>
533 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
534 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
535 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
536 public Context(string name)
538 context = new Interop.TEEC_Context();
539 if (name != null && name.Length == 0)
542 int ret = Interop.Libteec.InitializeContext(name, ref context);
543 Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
545 catch (global::System.DllNotFoundException e)
548 Interop.CheckNThrowException((int)Interop.LibteecError.NotImplemented, "Not found: " + e.Message);
554 /// Destructor of the class.
562 /// This function implements the IDisposable interface.
564 /// <since_tizen> 3 </since_tizen>
565 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
566 /// <privlevel>partner</privlevel>
567 /// <feature>http://tizen.org/feature/security.tee</feature>
568 public void Dispose() {
570 Interop.Libteec.FinalizeContext(ref context);
572 catch (global::System.DllNotFoundException) { }
576 /// This function opens a new session between the client application and the specified trusted application.
577 /// The target trusted application is identified by an UUID passed in the parameter destination.
578 /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
580 /// <since_tizen> 3 </since_tizen>
581 /// <param name="destination">The UUID of the destination TA.</param>
582 /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
583 /// <param name="connectionData">The data to be verified by a given login method.</param>
584 /// <param name="paramlist">The parameters to be passed to TA open-session-callback.</param>
585 /// <returns>Returns opened session.</returns>
586 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
587 /// <privlevel>partner</privlevel>
588 /// <feature>http://tizen.org/feature/security.tee</feature>
589 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
590 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
591 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
592 /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
593 public Session OpenSession(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
595 Session ses = new Session(context);
596 ses.Open(destination, loginMethod, connectionData, paramlist);
600 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
602 /// <since_tizen> 3 </since_tizen>
603 /// <param name="destination">The UUID of the destination TA.</param>
604 /// <returns>Returns opened session.</returns>
605 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
606 /// <privlevel>partner</privlevel>
607 /// <feature>http://tizen.org/feature/security.tee</feature>
608 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
609 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
610 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
611 public Session OpenSession(Guid destination)
613 Session ses = new Session(context);
614 ses.Open(destination, LoginMethod.Public, null, null);
619 /// The asynchronous version of the OpenSession.
620 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
622 /// <since_tizen> 3 </since_tizen>
623 /// <param name="destination">The UUID of the destination TA.</param>
624 /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
625 /// <param name="connectionData">The data to be verified by a given login method.</param>
626 /// <param name="paramlist">The parameters to be passed to the TA open-session-callback.</param>
627 /// <param name="token">The token for the task manipulation.</param>
628 /// <returns>Returns a Task executing the session open in the background.</returns>
629 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
630 /// <privlevel>partner</privlevel>
631 /// <feature>http://tizen.org/feature/security.tee</feature>
632 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
633 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
634 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
635 /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
636 public async Task<Session> OpenSessionAsync(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
638 Task<Session> task = Task<Session>.Factory.StartNew(() =>
640 return OpenSession(destination, loginMethod, connectionData, paramlist);
645 /// The asynchronous version of the OpenSession.
646 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
648 /// <since_tizen> 3 </since_tizen>
649 /// <param name="destination">The UUID of the destination TA.</param>
650 /// <param name="token">The token for a task manipulation.</param>
651 /// <returns>Returns a task executing session open in the background.</returns>
652 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
653 /// <privlevel>partner</privlevel>
654 /// <feature>http://tizen.org/feature/security.tee</feature>
655 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
656 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
657 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
658 public async Task<Session> OpenSessionAsync(Guid destination, CancellationToken token = default(CancellationToken))
660 Task<Session> task = Task<Session>.Factory.StartNew(() =>
662 return OpenSession(destination);
668 /// This function registers a block of the existing client application memory as a block of shared memory within
669 /// the scope of the specified context, in accordance with the parameters.
670 /// The input <paramref name="memaddr"/> must point to the shared memory region to register.
672 /// <since_tizen> 3 </since_tizen>
673 /// <param name="memaddr">The address of the shared memory.</param>
674 /// <param name="size">The size of the shared memory.</param>
675 /// <param name="flags">The flags describing the access modes (Input and/or Output).</param>
676 /// <returns>Returns the SharedMemory handler.</returns>
677 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
678 /// <privlevel>partner</privlevel>
679 /// <feature>http://tizen.org/feature/security.tee</feature>
680 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
681 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
682 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
683 /// <exception cref="ArgumentException">The argument <paramref name="memaddr"/> is wrong.</exception>
684 public SharedMemory RegisterSharedMemory(IntPtr memaddr, UInt32 size, SharedMemoryFlags flags)
686 Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
687 shm.buffer = memaddr;
688 shm.size = new UIntPtr(size);
689 shm.flags = (UInt32)flags;
690 int ret = Interop.Libteec.RegisterSharedMemory(ref context, ref shm);
691 Interop.CheckNThrowException(ret, "RegisterSharedMemory");
692 return new SharedMemory(ref shm);
696 /// This function allocates a new block of memory as a block of shared memory within the scope of the
697 /// specified context, in accordance with the parameters.
699 /// <since_tizen> 3 </since_tizen>
700 /// <param name="size">The size of shared memory.</param>
701 /// <param name="flags">The flags describing access modes (Input and/or Output).</param>
702 /// <returns>Returns the Shared Memory handler.</returns>
703 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
704 /// <privlevel>partner</privlevel>
705 /// <feature>http://tizen.org/feature/security.tee</feature>
706 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
707 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
708 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
709 public SharedMemory AllocateSharedMemory(UInt32 size, SharedMemoryFlags flags)
711 Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
712 shm.size = new UIntPtr(size);
713 shm.flags = (UInt32)flags;
714 int ret = Interop.Libteec.AllocateSharedMemory(ref context, ref shm);
715 Interop.CheckNThrowException(ret, "AllocateSharedMemory");
716 return new SharedMemory(ref shm);
720 /// This function deregisters or deallocates a previously initialized block of the shared memory.
723 /// For a memory buffer allocated using AllocateSharedMemory, the implementation must free the
724 /// underlying memory and the client application must not access this region after this function has been
725 /// called. In this case, the implementation must clear the buffer and size fields of the shared memory
726 /// structure before returning.
727 /// For memory registered using RegisterSharedMemory, the implementation must deregister the
728 /// underlying memory from the TEE, but the memory region will stay available to the client application for
729 /// other purposes as the memory is owned by it.
731 /// <since_tizen> 3 </since_tizen>
732 /// <param name="shm">The shared memory object returned by RegisterSharedMemory or AllocateSharedMemory.</param>
733 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
734 /// <privlevel>partner</privlevel>
735 /// <feature>http://tizen.org/feature/security.tee</feature>
736 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
737 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
738 /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
739 /// <exception cref="ArgumentException">The argument is wrong.</exception>
740 public void ReleaseSharedMemory(SharedMemory shm)
742 Interop.Libteec.ReleaseSharedMemory(ref shm.shm);