00ae581e3333cf0566333161f8d0335c3f25bb61
[platform/core/csapi/security.git] / Tizen.Security / Tizen.Security.SecureRepository / Key.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 using static Interop;
20
21 namespace Tizen.Security.SecureRepository
22 {
23     /// <summary>
24     /// Class that represents a key.
25     /// </summary>
26     public class Key : SafeHandle
27     {
28         /// <summary>
29         /// A constructor of Key that takes the binary, its type, and optional password of binary.
30         /// </summary>
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)
35         {
36             this.SetHandle(IntPtr.Zero);
37             Binary = binary;
38             Type = type;
39             BinaryPassword = binaryPassword;
40         }
41
42         internal Key(IntPtr ptr, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
43         {
44             base.SetHandle(ptr);
45
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);
51         }
52
53         /// <summary>
54         /// The binary value of a key.
55         /// </summary>
56         public byte[] Binary
57         {
58             get; set;
59         }
60
61         /// <summary>
62         /// The key's type.
63         /// </summary>
64         public KeyType Type
65         {
66             get; set;
67         }
68
69         /// <summary>
70         /// The password used to decrypt binary when binary is encrypted. It's optional.
71         /// </summary>
72         public string BinaryPassword
73         {
74             get; set;
75         }
76
77         internal CkmcKey ToCkmcKey()
78         {
79             return new Interop.CkmcKey(new PinnedObject(Binary),
80                                        Binary.Length,
81                                        (int)Type,
82                                        new PinnedObject(BinaryPassword));
83         }
84
85         /// <summary>
86         /// Gets a value that indicates whether the handle is invalid.
87         /// </summary>
88         public override bool IsInvalid
89         {
90             get { return handle == IntPtr.Zero; }
91         }
92
93         /// <summary>
94         /// When overridden in a derived class, executes the code required to free the handle.
95         /// </summary>
96         /// <returns>true if the handle is released successfully</returns>
97         protected override bool ReleaseHandle()
98         {
99             if (IsInvalid) // do not release
100                 return true;
101
102             Interop.CkmcTypes.KeyFree(handle);
103             this.SetHandle(IntPtr.Zero);
104             return true;
105         }
106     }
107 }