a9e7290b774b92b2ff9276206ee54c0fe5c4b4a2
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.TEEC / Tizen.Security.TEEC / Libteec.cs
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Security.TEEC
24 {
25     /// <summary>
26     /// This type denotes the Session Login Method used in OpenSession.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class LoginMethod
30     {
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;
43     }
44
45     /// <summary>
46     /// This type denotes the Value parameter.
47     /// </summary>
48     /// <since_tizen> 3 </since_tizen>
49     public enum TEFValueType : UInt32
50     {
51         /// <summary>The parameter is a ValueType tagged as the input.</summary>
52         Input  = 0x00000001,
53         /// <summary>The parameter is a ValueType tagged as the output.</summary>
54         Output = 0x00000002,
55         /// <summary>The parameter is a ValueType tagged as both the input and the output.</summary>
56         InOut  = 0x00000003,
57     }
58
59     /// <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.
62     /// </summary>
63     /// <since_tizen> 3 </since_tizen>
64     public enum TEFTempMemoryType : UInt32
65     {
66         /// <summary>The parameter is a TempMemoryType and is tagged as the input.</summary>
67         Input  = 0x00000005,
68         /// <summary>Same as the input, but the Memory Reference is tagged as the output.</summary>
69         Output = 0x00000006,
70         /// <summary>A Temporary Memory Reference tagged as both the input and the output.</summary>
71         InOut  = 0x00000007,
72     }
73
74     /// <summary>
75     /// This type denotes the SharedMemoryReference parameter.
76     /// </summary>
77     /// <since_tizen> 3 </since_tizen>
78     public enum TEFRegisteredMemoryType : UInt32
79     {
80         /// <summary>The parameter is a registered memory reference that refers to the entirety of its parent shared memory block.</summary>
81         Whole         = 0x0000000C,
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,
88     }
89
90     /// <summary>
91     /// This type denotes the SharedMemory access direction.
92     /// </summary>
93     /// <since_tizen> 3 </since_tizen>
94     [Flags]
95     public enum SharedMemoryFlags : UInt32
96     {
97         /// <summary>A flag indicates the shared memory can be read.</summary>
98         Input  = 0x00000001,
99         /// <summary>A flag indicates the shared memory can be written.</summary>
100         Output = 0x00000002,
101         /// <summary>A flag indicates the shared memory can be read and written.</summary>
102         InOut = Input | Output,
103     }
104
105     /// <summary>
106     /// This type denotes a shared memory block which has been either registered
107     /// with the implementation or allocated by it.
108     /// </summary>
109     /// <since_tizen> 3 </since_tizen>
110     public sealed class SharedMemory
111     {
112         internal Interop.TEEC_SharedMemory shm;
113         internal SharedMemory(Interop.TEEC_SharedMemory shm)
114         {
115             this.shm=shm;
116         }
117         /// <summary>
118         /// This property represents the shared memory size in bytes.
119         /// </summary>
120         /// <since_tizen> 3 </since_tizen>
121         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
122         /// <privlevel>partner</privlevel>
123         public UInt32 Size
124         {
125             get { return shm.size; }
126         }
127         /// <summary>
128         /// This property represents the start address of the shared memory block.
129         /// </summary>
130         /// <since_tizen> 3 </since_tizen>
131         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
132         /// <privlevel>partner</privlevel>
133         public IntPtr Address
134         {
135             get { return shm.buffer; }
136         }
137
138         /// <summary>
139         /// This function makes a copy and is designed for convenient operations on small buffers.
140         /// For large buffers, the direct address should be used.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
144         /// <privlevel>partner</privlevel>
145         /// <param name="data">The source data buffer to copy data from.</param>
146         /// <param name="dstOffs">The starting offset in the destination shared memory.</param>
147         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
148         public void SetData(byte[] data, int dstOffs)
149         {
150             if ((shm.flags & (uint)SharedMemoryFlags.Output) == 0) throw new InvalidOperationException("No write access");
151             Marshal.Copy(data, 0, shm.buffer + dstOffs, data.Length);
152         }
153         /// <summary>
154         /// This function makes a copy and is designed for convenient operations on small buffers.
155         /// For large buffers, the direct address should be used.
156         /// </summary>
157         /// <since_tizen> 3 </since_tizen>
158         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
159         /// <privlevel>partner</privlevel>
160         /// <param name="data">The destination data buffer to copy data into.</param>
161         /// <param name="srcOffs">The starting offset in the source shared memory.</param>
162         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
163         public void GetData(byte[] data, int srcOffs)
164         {
165             if ((shm.flags & (uint)SharedMemoryFlags.Input) == 0) throw new InvalidOperationException("No read access");
166             Marshal.Copy(shm.buffer + srcOffs, data, 0, data.Length);
167         }
168     };
169
170     /// <summary>
171     /// This type defines the payload of either an open session operation or an invoke command operation. It is
172     /// also used for cancelation of operations, which may be desirable even if no payload is passed.
173     /// Parameters are used to exchange data between CA and TA.
174     /// </summary>
175     /// <since_tizen> 3 </since_tizen>
176     public abstract class Parameter
177     {
178         internal Parameter(uint t)
179         {
180             this.NativeType = t;
181         }
182         internal uint NativeType { get; }
183     };
184
185     /// <summary>
186     /// This type defines a template for the parameter types.
187     /// </summary>
188     /// <since_tizen> 3 </since_tizen>
189     public abstract class BaseParameter<TEnum> : Parameter where TEnum : struct, IComparable, IFormattable, IConvertible // as close to Enum as possible
190     {
191         internal BaseParameter(TEnum t) : base((uint)(object)t)
192         {
193             Type = t;
194         }
195
196         /// <summary>
197         /// This property represents the access type to this parameter.
198         /// </summary>
199         /// <since_tizen> 3 </since_tizen>
200         public TEnum Type { get; }
201     }
202
203     /// <summary>
204     /// This type defines a temporary memory reference.
205     /// </summary>
206     /// <since_tizen> 3 </since_tizen>
207     public sealed class TempMemoryReference : BaseParameter<TEFTempMemoryType>
208     {
209         /// <summary>
210         /// Constructs a parameter object which holds information about the temporary memory copied to or from TA.
211         /// </summary>
212         /// <since_tizen> 3 </since_tizen>
213         /// <param name="buffer">The address of the allocated memory buffer.</param>
214         /// <param name="size">The size of the buffer.</param>
215         /// <param name="type">The kind of access allowed for TA <see cref="TEFTempMemoryType"/>.</param>
216         public TempMemoryReference(IntPtr buffer, uint size, TEFTempMemoryType type) :
217                 base(type)
218         {
219             this.Buffer = buffer;
220             this.Size = size;
221         }
222         /// <summary>
223         /// This property represents the memory address of the buffer.
224         /// </summary>
225         /// <since_tizen> 3 </since_tizen>
226         public IntPtr Buffer { get; }
227         /// <summary>
228         /// This property represents the size of the buffer.
229         /// </summary>
230         /// <since_tizen> 3 </since_tizen>
231         public uint Size { get; internal set; }
232     };
233
234     /// <summary>
235     /// This type defines a memory reference that uses a pre-registered or pre-allocated shared memory block.
236     /// </summary>
237     /// <since_tizen> 3 </since_tizen>
238     public sealed class RegisteredMemoryReference : BaseParameter<TEFRegisteredMemoryType>
239     {
240         /// <summary>
241         /// Constructs a parameter object which holds information about the registered memory shared with TA.
242         /// </summary>
243         /// <since_tizen> 3 </since_tizen>
244         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
245         /// <privlevel>partner</privlevel>
246         /// <param name="parent">The shared memory - registered or allocated.</param>
247         /// <param name="size">The size of the buffer part.</param>
248         /// <param name="offset">The offset of the buffer in the shared memory.</param>
249         /// <param name="type">The kind of access allowed for TA <see cref="TEFRegisteredMemoryType"/>.</param>
250         public RegisteredMemoryReference(SharedMemory parent, uint size, uint offset, TEFRegisteredMemoryType type) :
251                 base(type)
252         {
253             this.Parent = parent;
254             this.Size = size;
255             this.Offset = offset;
256         }
257         /// <summary>
258         /// This property represents the shared memory that is referred to.
259         /// </summary>
260         /// <since_tizen> 3 </since_tizen>
261         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
262         /// <privlevel>partner</privlevel>
263         public SharedMemory Parent { get; }
264         /// <summary>
265         /// This property represents the size (in bytes) of the shared memory.
266         /// </summary>
267         /// <since_tizen> 3 </since_tizen>
268         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
269         /// <privlevel>partner</privlevel>
270         public uint Size { get; internal set; }
271         /// <summary>
272         /// This property represents the offset (in bytes) from the start of the shared memory.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
276         /// <privlevel>partner</privlevel>
277         public uint Offset { get; }
278     };
279
280     /// <summary>
281     /// This type defines a parameter that is not referencing the shared memory, but carries instead small raw data
282     /// passed by a value.
283     /// </summary>
284     /// <since_tizen> 3 </since_tizen>
285     public sealed class Value : BaseParameter<TEFValueType>
286     {
287         /// <summary>
288         /// Constructs a parameter object which holds information about integer values copied to or from TA.
289         /// </summary>
290         /// <since_tizen> 3 </since_tizen>
291         /// <param name="a">User paramter A.</param>
292         /// <param name="b">User paramter B.</param>
293         /// <param name="type">The kind of access allowed for TA <see cref="TEFValueType"/>.</param>
294         public Value(uint a, uint b, TEFValueType type) :
295                 base(type)
296         {
297             this.A = a;
298             this.B = b;
299         }
300         /// <summary>
301         /// This property represents an unsigned integer A.
302         /// </summary>
303         /// <since_tizen> 3 </since_tizen>
304         public uint A { get; internal set; }
305         /// <summary>
306         /// This property represents an unsigned integer B.
307         /// </summary>
308         /// <since_tizen> 3 </since_tizen>
309         public uint B { get; internal set; }
310     };
311
312
313     /// <summary>
314     /// This type denotes a TEE Session, the logical container linking a client application with a particular trusted application.
315     /// </summary>
316     /// <since_tizen> 3 </since_tizen>
317     public sealed class Session
318     {
319         private Interop.TEEC_Context context;
320         private Interop.TEEC_Session session;
321
322         internal Session(Interop.TEEC_Context context) {
323             this.context = context;
324             this.session = new Interop.TEEC_Session();
325         }
326
327         /// <summary>
328         /// Destructor of the class.
329         /// </summary>
330         ~Session()
331         {
332             Close();
333         }
334
335         internal UInt32 InitParam(ref Interop.TEEC_Parameter dst, Parameter src)
336         {
337             switch (src.NativeType) {
338                case (int)TEFValueType.Input:
339                case (int)TEFValueType.Output:
340                case (int)TEFValueType.InOut:
341                    dst.value.a = ((Value)src).A;
342                    dst.value.b = ((Value)src).B;
343                    break;
344
345                case (int)TEFTempMemoryType.Input:
346                case (int)TEFTempMemoryType.Output:
347                case (int)TEFTempMemoryType.InOut:
348                    dst.tmpref.buffer = ((TempMemoryReference)src).Buffer;
349                    dst.tmpref.size = ((TempMemoryReference)src).Size;
350                    break;
351
352                case (int)TEFRegisteredMemoryType.Whole:
353                case (int)TEFRegisteredMemoryType.PartialInput:
354                case (int)TEFRegisteredMemoryType.PartialOutput:
355                case (int)TEFRegisteredMemoryType.PartialInOut:
356                    dst.memref.parent = ((RegisteredMemoryReference)src).Parent.shm;
357                    dst.memref.size = ((RegisteredMemoryReference)src).Size;
358                    dst.memref.offset = ((RegisteredMemoryReference)src).Offset;
359                    break;
360
361                default: return 0;
362             }
363             return src.NativeType;
364         }
365
366         internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter dst)
367         {
368             switch (dst.NativeType) {
369                case (int)TEFValueType.Input:
370                case (int)TEFValueType.Output:
371                case (int)TEFValueType.InOut:
372                    ((Value)dst).A = src.value.a;
373                    ((Value)dst).B = src.value.b;
374                    break;
375
376                case (int)TEFTempMemoryType.Input:
377                case (int)TEFTempMemoryType.Output:
378                case (int)TEFTempMemoryType.InOut:
379                    ((TempMemoryReference)dst).Size = src.tmpref.size;
380                    break;
381
382                case (int)TEFRegisteredMemoryType.Whole:
383                case (int)TEFRegisteredMemoryType.PartialInput:
384                case (int)TEFRegisteredMemoryType.PartialOutput:
385                case (int)TEFRegisteredMemoryType.PartialInOut:
386                    ((RegisteredMemoryReference)dst).Size = src.memref.size;
387                    break;
388
389                default: break;
390             }
391         }
392
393         internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
394         {
395             Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
396             Interop.TEEC_Operation op = new Interop.TEEC_Operation();
397
398             op.started=0;
399             op.paramTypes=0;
400             for (int i=0; i < 4 && i < paramlist.Length; ++i) {
401                 op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
402             }
403
404             uint ro;
405             int ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro);
406             //MAYBE map origin of return code to specyfic Exception
407             Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination));
408             for (int i=0; i < 4 && i < paramlist.Length; ++i) {
409                 UpdateParam(op.paramlist[i], ref paramlist[i]);
410             }
411         }
412
413         /// <summary>
414         /// This function closes a session which has been opened with a trusted application.
415         /// All commands within the session must be completed before this function can be called.
416         /// </summary>
417         /// <since_tizen> 3 </since_tizen>
418         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
419         /// <privlevel>partner</privlevel>
420         /// <feature>http://tizen.org/feature/security.tee</feature>
421         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
422         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
423         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
424         public void Close() {
425             Interop.Libteec.CloseSession(ref session);
426             //session = null;
427         }
428
429         /// <summary>
430         /// This function invokes a command within the specified session.
431         /// The parameter commandID is an identifier that is used to indicate which of the exposed trusted
432         /// application functions should be invoked. The supported command identifier values are defined by the
433         /// trusted application's protocol.
434         /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
435         /// </summary>
436         /// <since_tizen> 3 </since_tizen>
437         /// <param name="commandID">The command.</param>
438         /// <param name="paramlist">The array of parameters.</param>
439         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
440         /// <privlevel>partner</privlevel>
441         /// <feature>http://tizen.org/feature/security.tee</feature>
442         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
443         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
444         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
445         /// <exception cref="ArgumentException">The argument <paramref name="paramlist"/> is wrong.</exception>
446         public void InvokeCommand(uint commandID, Parameter[] paramlist)
447         {
448             Interop.TEEC_Operation op = new Interop.TEEC_Operation();
449             op.started=0;
450             op.paramTypes=0;
451             for (int i=0; i < 4 && i < paramlist.Length; ++i) {
452                 op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
453             }
454
455             uint ro;
456             int ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro);
457             //MAYBE map origin of return code to specific Exception
458             Interop.CheckNThrowException(ret, string.Format("InvokeCommand({0})", commandID));
459             for (int i=0; i < 4 && i < paramlist.Length; ++i) {
460                 UpdateParam(op.paramlist[i], ref paramlist[i]);
461             }
462         }
463
464         /// <summary>
465         /// The asynchronous version of the InvokeCommand.
466         /// </summary>
467         /// <since_tizen> 3 </since_tizen>
468         /// <param name="commandID">The command.</param>
469         /// <param name="paramlist">The array of parameters.</param>
470         /// <param name="token">The token for task manipulation.</param>
471         /// <returns>Returns a task executing an invoke command in the background.</returns>
472         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
473         /// <privlevel>partner</privlevel>
474         /// <feature>http://tizen.org/feature/security.tee</feature>
475         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
476         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
477         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
478         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
479         public async Task InvokeCommandAsync(uint commandID, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
480         {
481             await Task.Factory.StartNew(() => InvokeCommand(commandID, paramlist));
482         }
483
484     };
485
486     /// <summary>
487     /// This type denotes a TEE Context, the main logical container linking a Client Application with a particular TEE.
488     /// </summary>
489     /// <since_tizen> 3 </since_tizen>
490     public sealed class Context : IDisposable
491     {
492         private Interop.TEEC_Context context;
493
494         /// <summary>
495         /// This function (constructor) initializes a new TEE Context, forming a connection between this client application and the
496         /// TEE identified by the string identifier name (empty or null name denotes a default TEE).
497         /// </summary>
498         /// <since_tizen> 3 </since_tizen>
499         /// <param name="name">The TEE name.</param>
500         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
501         /// <privlevel>partner</privlevel>
502         /// <feature>http://tizen.org/feature/security.tee</feature>
503         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
504         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
505         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
506         public Context(string name)
507         {
508             context = new Interop.TEEC_Context();
509             if (name != null && name.Length == 0)
510                 name = null;
511             try {
512                 int ret = Interop.Libteec.InitializeContext(name, ref context);
513                 Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
514             }
515             catch (global::System.DllNotFoundException e)
516             {
517                 unchecked {
518                     Interop.CheckNThrowException((int)Interop.LibteecError.NotImplemented, "Not found: " + e.Message);
519                 }
520             }
521         }
522
523         /// <summary>
524         /// Destructor of the class.
525         /// </summary>
526         ~Context()
527         {
528             Dispose();
529         }
530
531         /// <summary>
532         /// This function implements the IDisposable interface.
533         /// </summary>
534         /// <since_tizen> 3 </since_tizen>
535         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
536         /// <privlevel>partner</privlevel>
537         /// <feature>http://tizen.org/feature/security.tee</feature>
538         public void Dispose() {
539             try {
540                 Interop.Libteec.FinalizeContext(ref context);
541             }
542             catch (global::System.DllNotFoundException) { }
543         }
544
545         /// <summary>
546         /// This function opens a new session between the client application and the specified trusted application.
547         /// The target trusted application is identified by an UUID passed in the parameter destination.
548         /// There can be up to four parameter objects given in the <paramref name="paramlist"/> array.
549         /// </summary>
550         /// <since_tizen> 3 </since_tizen>
551         /// <param name="destination">The UUID of the destination TA.</param>
552         /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
553         /// <param name="connectionData">The data to be verified by a given login method.</param>
554         /// <param name="paramlist">The parameters to be passed to TA open-session-callback.</param>
555         /// <returns>Returns opened session.</returns>
556         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
557         /// <privlevel>partner</privlevel>
558         /// <feature>http://tizen.org/feature/security.tee</feature>
559         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
560         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
561         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
562         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
563         public Session OpenSession(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
564         {
565             Session ses = new Session(context);
566             ses.Open(destination, loginMethod, connectionData, paramlist);
567             return ses;
568         }
569         /// <summary>
570         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
571         /// </summary>
572         /// <since_tizen> 3 </since_tizen>
573         /// <param name="destination">The UUID of the destination TA.</param>
574         /// <returns>Returns opened session.</returns>
575         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
576         /// <privlevel>partner</privlevel>
577         /// <feature>http://tizen.org/feature/security.tee</feature>
578         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
579         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
580         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
581         public Session OpenSession(Guid destination)
582         {
583             Session ses = new Session(context);
584             ses.Open(destination, LoginMethod.Public, null, null);
585             return ses;
586         }
587
588         /// <summary>
589         /// The asynchronous version of the OpenSession.
590         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
591         /// </summary>
592         /// <since_tizen> 3 </since_tizen>
593         /// <param name="destination">The UUID of the destination TA.</param>
594         /// <param name="loginMethod">The authentication algorithm <see cref="LoginMethod" />.</param>
595         /// <param name="connectionData">The data to be verified by a given login method.</param>
596         /// <param name="paramlist">The parameters to be passed to the TA open-session-callback.</param>
597         /// <param name="token">The token for the task manipulation.</param>
598         /// <returns>Returns a Task executing the session open in the background.</returns>
599         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
600         /// <privlevel>partner</privlevel>
601         /// <feature>http://tizen.org/feature/security.tee</feature>
602         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
603         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
604         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
605         /// <exception cref="ArgumentException">One of the arguments is wrong.</exception>
606         public async Task<Session> OpenSessionAsync(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token = default(CancellationToken))
607         {
608             Task<Session> task = Task<Session>.Factory.StartNew(() =>
609             {
610                 return OpenSession(destination, loginMethod, connectionData, paramlist);
611             });
612             return await task;
613         }
614         /// <summary>
615         /// The asynchronous version of the OpenSession.
616         /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
617         /// </summary>
618         /// <since_tizen> 3 </since_tizen>
619         /// <param name="destination">The UUID of the destination TA.</param>
620         /// <param name="token">The token for a task manipulation.</param>
621         /// <returns>Returns a task executing session open in the background.</returns>
622         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
623         /// <privlevel>partner</privlevel>
624         /// <feature>http://tizen.org/feature/security.tee</feature>
625         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
626         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
627         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
628         public async Task<Session> OpenSessionAsync(Guid destination, CancellationToken token = default(CancellationToken))
629         {
630             Task<Session> task = Task<Session>.Factory.StartNew(() =>
631             {
632                 return OpenSession(destination);
633             });
634             return await task;
635         }
636
637         /// <summary>
638         /// This function registers a block of the existing client application memory as a block of shared memory within
639         /// the scope of the specified context, in accordance with the parameters.
640         /// The input <paramref name="memaddr"/> must point to the shared memory region to register.
641         /// </summary>
642         /// <since_tizen> 3 </since_tizen>
643         /// <param name="memaddr">The address of the shared memory.</param>
644         /// <param name="size">The size of the shared memory.</param>
645         /// <param name="flags">The flags describing the access modes (Input and/or Output).</param>
646         /// <returns>Returns the SharedMemory handler.</returns>
647         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
648         /// <privlevel>partner</privlevel>
649         /// <feature>http://tizen.org/feature/security.tee</feature>
650         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
651         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
652         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
653         /// <exception cref="ArgumentException">The argument <paramref name="memaddr"/> is wrong.</exception>
654         public SharedMemory RegisterSharedMemory(IntPtr memaddr, UInt32 size, SharedMemoryFlags flags)
655         {
656             Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
657             shm.buffer = memaddr;
658             shm.size = size;
659             shm.flags = (UInt32)flags;
660             int ret = Interop.Libteec.RegisterSharedMemory(ref context, ref shm);
661             Interop.CheckNThrowException(ret, "RegisterSharedMemory");
662             return new SharedMemory(shm);
663         }
664
665         /// <summary>
666         /// This function allocates a new block of memory as a block of shared memory within the scope of the
667         /// specified context, in accordance with the parameters.
668         /// </summary>
669         /// <since_tizen> 3 </since_tizen>
670         /// <param name="size">The size of shared memory.</param>
671         /// <param name="flags">The flags describing access modes (Input and/or Output).</param>
672         /// <returns>Returns the Shared Memory handler.</returns>
673         /// <privilege>http://tizen.org/privilege/tee.client</privilege>
674         /// <privlevel>partner</privlevel>
675         /// <feature>http://tizen.org/feature/security.tee</feature>
676         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
677         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
678         /// <exception cref="InvalidOperationException">The operation is invalid.</exception>
679         public SharedMemory AllocateSharedMemory(UInt32 size, SharedMemoryFlags flags)
680         {
681             Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
682             shm.size = size;
683             shm.flags = (UInt32)flags;
684             int ret = Interop.Libteec.AllocateSharedMemory(ref context, ref shm);
685             Interop.CheckNThrowException(ret, "AllocateSharedMemory");
686             return new SharedMemory(shm);
687         }
688
689         /// <summary>
690         /// This function deregisters or deallocates a previously initialized block of the shared memory.
691         /// </summary>
692         /// <remarks>
693         /// For a memory buffer allocated using AllocateSharedMemory, the implementation must free the
694         /// underlying memory and the client application must not access this region after this function has been
695         /// called. In this case, the implementation must clear the buffer and size fields of the shared memory
696         /// structure before returning.
697         /// For memory registered using RegisterSharedMemory, the implementation must deregister the
698         /// underlying memory from the TEE, but the memory region will stay available to the client application for
699         /// other purposes as the memory is owned by it.
700         /// </remarks>
701         /// <since_tizen> 3 </since_tizen>
702         /// <param name="shm">The shared memory object returned by RegisterSharedMemory or AllocateSharedMemory.</param>
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         /// <exception cref="ArgumentException">The argument is wrong.</exception>
710         public void ReleaseSharedMemory(SharedMemory shm)
711         {
712             Interop.Libteec.ReleaseSharedMemory(ref shm.shm);
713         }
714     };
715 }