Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / Tizen.Security.SecureRepository / Crypto / SafeCipherParametersHandle.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.Runtime.InteropServices;
19
20 namespace Tizen.Security.SecureRepository.Crypto
21 {
22     internal class SafeCipherParametersHandle : SafeHandle
23     {
24         internal SafeCipherParametersHandle(CipherAlgorithmType algorithm) :
25             base(IntPtr.Zero, true)
26         {
27             IntPtr ptr = IntPtr.Zero;
28             Interop.CkmcTypes.GenerateNewParam((int)algorithm, out ptr);
29             this.SetHandle(ptr);
30         }
31
32         internal void SetInteger(CipherParameterName name, long value)
33         {
34             Interop.CheckNThrowException(
35                 Interop.CkmcTypes.ParamListSetInteger(
36                     Ptr, (int)name, value),
37                 "Failed to add parameter.");
38         }
39
40         internal void SetBuffer(CipherParameterName name, byte[] value)
41         {
42             var rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(value), value.Length);
43             IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rawBuff));
44             try
45             {
46                 Marshal.StructureToPtr<Interop.CkmcRawBuffer>(rawBuff, ptr, false);
47                 Interop.CheckNThrowException(
48                     Interop.CkmcTypes.ParamListSetBuffer(
49                         Ptr, (int)name, ptr),
50                     "Failed to add parameter.");
51             }
52             finally
53             {
54                 Marshal.FreeHGlobal(ptr);
55             }
56         }
57
58         internal long GetInteger(CipherParameterName name)
59         {
60             long value = 0;
61             Interop.CheckNThrowException(
62                 Interop.CkmcTypes.ParamListGetInteger(
63                     Ptr, (int)name, out value),
64                 "Failed to get parameter.");
65             return value;
66         }
67
68         internal byte[] GetBuffer(CipherParameterName name)
69         {
70             IntPtr ptr = IntPtr.Zero;
71
72             try
73             {
74                 Interop.CheckNThrowException(
75                     Interop.CkmcTypes.ParamListGetBuffer(
76                         Ptr, (int)name, out ptr),
77                     "Failed to get parameter.");
78                 return new SafeRawBufferHandle(ptr).Data;
79             }
80             finally
81             {
82                 if (ptr != IntPtr.Zero)
83                     Interop.CkmcTypes.BufferFree(ptr);
84             }
85         }
86
87         internal CipherAlgorithmType Algorithm
88         {
89             get
90             {
91                 return (CipherAlgorithmType)GetInteger(
92                     CipherParameterName.AlgorithmType);
93             }
94         }
95
96         internal IntPtr Ptr
97         {
98             get { return this.handle; }
99         }
100
101         public override bool IsInvalid
102         {
103             get { return this.handle == IntPtr.Zero; }
104         }
105
106         protected override bool ReleaseHandle()
107         {
108             Interop.CkmcTypes.ParamListFree(this.handle);
109             this.SetHandle(IntPtr.Zero);
110             return true;
111         }
112     }
113 }