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;
21 namespace Tizen.Security.SecureRepository
24 /// Class that represents a key.
26 public class Key : SafeHandle
29 /// A constructor of Key that takes the binary, its type, and optional password of binary.
31 /// <param name="binary">The binary value of a key. This binary may be encrypted with binaryPassword.</param>
32 /// <param name="type">The key's type.</param>
33 /// <param name="binaryPassword">The password used to decrypt binary when binary is encrypted.</param>
34 public Key(byte[] binary, KeyType type, string binaryPassword) : base(IntPtr.Zero, true)
36 this.SetHandle(IntPtr.Zero);
39 BinaryPassword = binaryPassword;
42 internal Key(IntPtr ptr, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
46 CkmcKey ckmcKey = (CkmcKey)Marshal.PtrToStructure(handle, typeof(CkmcKey));
47 Binary = new byte[ckmcKey.size];
48 Marshal.Copy(ckmcKey.rawKey, Binary, 0, Binary.Length);
49 Type = (KeyType)ckmcKey.keyType;
50 BinaryPassword = Marshal.PtrToStringAuto(ckmcKey.password);
54 /// The binary value of a key.
70 /// The password used to decrypt binary when binary is encrypted. It's optional.
72 public string BinaryPassword
77 internal CkmcKey ToCkmcKey()
79 return new Interop.CkmcKey(new PinnedObject(Binary),
82 new PinnedObject(BinaryPassword));
86 /// Gets a value that indicates whether the handle is invalid.
88 public override bool IsInvalid
90 get { return handle == IntPtr.Zero; }
94 /// When overridden in a derived class, executes the code required to free the handle.
96 /// <returns>true if the handle is released successfully</returns>
97 protected override bool ReleaseHandle()
99 if (IsInvalid) // do not release
102 Interop.CkmcTypes.KeyFree(handle);
103 this.SetHandle(IntPtr.Zero);