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 SharedMemory(Interop.TEEC_SharedMemory shm)
118 /// This property represents the shared memory size in bytes.
120 /// <since_tizen> 3 </since_tizen>
121 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
122 /// <privlevel>partner</privlevel>
125 get { return shm.size; }
128 /// This property represents the start address of the shared memory block.
130 /// <since_tizen> 3 </since_tizen>
131 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
132 /// <privlevel>partner</privlevel>
133 public IntPtr Address
135 get { return shm.buffer; }
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.
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)
150 if ((shm.flags & (uint)SharedMemoryFlags.Output) == 0) throw new InvalidOperationException("No write access");
151 Marshal.Copy(data, 0, shm.buffer + dstOffs, data.Length);
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.
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)
165 if ((shm.flags & (uint)SharedMemoryFlags.Input) == 0) throw new InvalidOperationException("No read access");
166 Marshal.Copy(shm.buffer + srcOffs, data, 0, data.Length);
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.
175 /// <since_tizen> 3 </since_tizen>
176 public abstract class Parameter
178 internal Parameter(uint t)
182 internal uint NativeType { get; }
186 /// This type defines a template for the parameter types.
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
191 internal BaseParameter(TEnum t) : base((uint)(object)t)
197 /// This property represents the access type to this parameter.
199 /// <since_tizen> 3 </since_tizen>
200 public TEnum Type { get; }
204 /// This type defines a temporary memory reference.
206 /// <since_tizen> 3 </since_tizen>
207 public sealed class TempMemoryReference : BaseParameter<TEFTempMemoryType>
210 /// Constructs a parameter object which holds information about the temporary memory copied to or from TA.
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) :
219 this.Buffer = buffer;
223 /// This property represents the memory address of the buffer.
225 /// <since_tizen> 3 </since_tizen>
226 public IntPtr Buffer { get; }
228 /// This property represents the size of the buffer.
230 /// <since_tizen> 3 </since_tizen>
231 public uint Size { get; internal set; }
235 /// This type defines a memory reference that uses a pre-registered or pre-allocated shared memory block.
237 /// <since_tizen> 3 </since_tizen>
238 public sealed class RegisteredMemoryReference : BaseParameter<TEFRegisteredMemoryType>
241 /// Constructs a parameter object which holds information about the registered memory shared with TA.
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) :
253 this.Parent = parent;
255 this.Offset = offset;
258 /// This property represents the shared memory that is referred to.
260 /// <since_tizen> 3 </since_tizen>
261 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
262 /// <privlevel>partner</privlevel>
263 public SharedMemory Parent { get; }
265 /// This property represents the size (in bytes) of the shared memory.
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; }
272 /// This property represents the offset (in bytes) from the start of the shared memory.
274 /// <since_tizen> 3 </since_tizen>
275 /// <privilege>http://tizen.org/privilege/tee.client</privilege>
276 /// <privlevel>partner</privlevel>
277 public uint Offset { get; }
281 /// This type defines a parameter that is not referencing the shared memory, but carries instead small raw data
282 /// passed by a value.
284 /// <since_tizen> 3 </since_tizen>
285 public sealed class Value : BaseParameter<TEFValueType>
288 /// Constructs a parameter object which holds information about integer values copied to or from TA.
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) :
301 /// This property represents an unsigned integer A.
303 /// <since_tizen> 3 </since_tizen>
304 public uint A { get; internal set; }
306 /// This property represents an unsigned integer B.
308 /// <since_tizen> 3 </since_tizen>
309 public uint B { get; internal set; }
314 /// This type denotes a TEE Session, the logical container linking a client application with a particular trusted application.
316 /// <since_tizen> 3 </since_tizen>
317 public sealed class Session
319 private Interop.TEEC_Context context;
320 private Interop.TEEC_Session session;
322 internal Session(Interop.TEEC_Context context) {
323 this.context = context;
324 this.session = new Interop.TEEC_Session();
328 /// Destructor of the class.
335 internal UInt32 InitParam(ref Interop.TEEC_Parameter dst, Parameter src)
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;
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;
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;
363 return src.NativeType;
366 internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter dst)
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;
376 case (int)TEFTempMemoryType.Input:
377 case (int)TEFTempMemoryType.Output:
378 case (int)TEFTempMemoryType.InOut:
379 ((TempMemoryReference)dst).Size = src.tmpref.size;
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;
393 internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
395 Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
396 Interop.TEEC_Operation op = new Interop.TEEC_Operation();
400 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
401 op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
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]);
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.
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);
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.
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)
448 Interop.TEEC_Operation op = new Interop.TEEC_Operation();
451 for (int i=0; i < 4 && i < paramlist.Length; ++i) {
452 op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
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]);
465 /// The asynchronous version of the InvokeCommand.
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))
481 await Task.Factory.StartNew(() => InvokeCommand(commandID, paramlist));
487 /// This type denotes a TEE Context, the main logical container linking a Client Application with a particular TEE.
489 /// <since_tizen> 3 </since_tizen>
490 public sealed class Context : IDisposable
492 private Interop.TEEC_Context context;
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).
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)
508 context = new Interop.TEEC_Context();
509 if (name != null && name.Length == 0)
512 int ret = Interop.Libteec.InitializeContext(name, ref context);
513 Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
515 catch (global::System.DllNotFoundException e)
518 Interop.CheckNThrowException((int)Interop.LibteecError.NotImplemented, "Not found: " + e.Message);
524 /// Destructor of the class.
532 /// This function implements the IDisposable interface.
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() {
540 Interop.Libteec.FinalizeContext(ref context);
542 catch (global::System.DllNotFoundException) { }
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.
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)
565 Session ses = new Session(context);
566 ses.Open(destination, loginMethod, connectionData, paramlist);
570 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
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)
583 Session ses = new Session(context);
584 ses.Open(destination, LoginMethod.Public, null, null);
589 /// The asynchronous version of the OpenSession.
590 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
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))
608 Task<Session> task = Task<Session>.Factory.StartNew(() =>
610 return OpenSession(destination, loginMethod, connectionData, paramlist);
615 /// The asynchronous version of the OpenSession.
616 /// @see OpenSession (Guid destination, uint connectionMethod, byte[] connectionData, Parameter[] paramlist, CancellationToken token).
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))
630 Task<Session> task = Task<Session>.Factory.StartNew(() =>
632 return OpenSession(destination);
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.
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)
656 Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
657 shm.buffer = memaddr;
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);
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.
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)
681 Interop.TEEC_SharedMemory shm = new Interop.TEEC_SharedMemory();
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);
690 /// This function deregisters or deallocates a previously initialized block of the shared memory.
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.
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)
712 Interop.Libteec.ReleaseSharedMemory(ref shm.shm);