Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / 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     /// <since_tizen> 3 </since_tizen>
27     public class Key
28     {
29         /// <summary>
30         /// A constructor of Key that takes the binary, its type, and optional password
31         /// of binary.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         /// <param name="binary">
35         /// The binary value of a key. This binary may be encrypted with binaryPassword.
36         /// </param>
37         /// <param name="type">The key's type.</param>
38         /// <param name="binaryPassword">
39         /// The password used to decrypt binary when binary is encrypted.
40         /// </param>
41         public Key(byte[] binary, KeyType type, string binaryPassword)
42         {
43             this.Binary = binary;
44             this.Type = type;
45             this.BinaryPassword = binaryPassword;
46         }
47
48         internal Key(IntPtr ptr)
49         {
50             if (ptr == IntPtr.Zero)
51                 throw new ArgumentNullException("Returned ptr from CAPI cannot be null");
52
53             var ckmcKey = Marshal.PtrToStructure<CkmcKey>(ptr);
54             this.Binary = new byte[(int)ckmcKey.size];
55             Marshal.Copy(ckmcKey.rawKey, this.Binary, 0, this.Binary.Length);
56             this.Type = (KeyType)ckmcKey.keyType;
57             this.BinaryPassword = ckmcKey.password;
58         }
59
60         // Refresh handle(IntPtr) always. Because C# layer
61         // properties(Binary, Type, BinaryPassword) could be changed.
62         internal IntPtr GetHandle()
63         {
64             IntPtr ptr = IntPtr.Zero;
65             try
66             {
67                 CheckNThrowException(
68                     Interop.CkmcTypes.KeyNew(
69                         this.Binary, (UIntPtr)this.Binary.Length, (int)this.Type,
70                         this.BinaryPassword, out ptr),
71                     "Failed to create key");
72
73                 return ptr;
74             }
75             catch
76             {
77                 if (ptr != IntPtr.Zero)
78                     Interop.CkmcTypes.KeyFree(ptr);
79
80                 throw;
81             }
82         }
83
84         /// <summary>
85         /// The binary value of a key.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88         public byte[] Binary
89         {
90             get; set;
91         }
92
93         /// <summary>
94         /// The key's type.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         public KeyType Type
98         {
99             get; set;
100         }
101
102         /// <summary>
103         /// The password used to decrypt binary when binary is encrypted. It's optional.
104         /// </summary>
105         /// <since_tizen> 3 </since_tizen>
106         public string BinaryPassword
107         {
108             get; set;
109         }
110
111         internal CkmcKey ToCkmcKey()
112         {
113             return new Interop.CkmcKey(
114                 (Binary == null) ? IntPtr.Zero : new PinnedObject(this.Binary),
115                 (Binary == null) ? 0 : this.Binary.Length,
116                 (int)this.Type,
117                 this.BinaryPassword);
118         }
119     }
120 }