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.Runtime.InteropServices;
20 namespace Tizen.Security.SecureRepository.Crypto
22 internal class SafeCipherParametersHandle : SafeHandle
24 internal SafeCipherParametersHandle(CipherAlgorithmType algorithm) :
25 base(IntPtr.Zero, true)
27 IntPtr ptr = IntPtr.Zero;
28 Interop.CkmcTypes.GenerateNewParam((int)algorithm, out ptr);
32 internal void SetInteger(CipherParameterName name, long value)
34 Interop.CheckNThrowException(
35 Interop.CkmcTypes.ParamListSetInteger(
36 Ptr, (int)name, value),
37 "Failed to add parameter.");
40 internal void SetBuffer(CipherParameterName name, byte[] value)
42 var rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(value), value.Length);
43 IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rawBuff));
46 Marshal.StructureToPtr<Interop.CkmcRawBuffer>(rawBuff, ptr, false);
47 Interop.CheckNThrowException(
48 Interop.CkmcTypes.ParamListSetBuffer(
50 "Failed to add parameter.");
54 Marshal.FreeHGlobal(ptr);
58 internal long GetInteger(CipherParameterName name)
61 Interop.CheckNThrowException(
62 Interop.CkmcTypes.ParamListGetInteger(
63 Ptr, (int)name, out value),
64 "Failed to get parameter.");
68 internal byte[] GetBuffer(CipherParameterName name)
70 IntPtr ptr = IntPtr.Zero;
74 Interop.CheckNThrowException(
75 Interop.CkmcTypes.ParamListGetBuffer(
76 Ptr, (int)name, out ptr),
77 "Failed to get parameter.");
78 return new SafeRawBufferHandle(ptr).Data;
82 if (ptr != IntPtr.Zero)
83 Interop.CkmcTypes.BufferFree(ptr);
87 internal CipherAlgorithmType Algorithm
91 return (CipherAlgorithmType)GetInteger(
92 CipherParameterName.AlgorithmType);
98 get { return this.handle; }
101 public override bool IsInvalid
103 get { return this.handle == IntPtr.Zero; }
106 protected override bool ReleaseHandle()
108 Interop.CkmcTypes.ParamListFree(this.handle);
109 this.SetHandle(IntPtr.Zero);