Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / Tizen.Security.SecureRepository / KeyManager.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.Collections.Generic;
19
20 namespace Tizen.Security.SecureRepository
21 {
22     /// <summary>
23     /// This class provides the methods storing, retrieving, and creating keys.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class KeyManager : Manager
27     {
28         /// <summary>
29         /// Gets a key from secure repository.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <param name="alias">The name of a key to retrieve.</param>
33         /// <param name="password">
34         /// The password used in decrypting a key value.
35         /// If password of policy is provided in SaveKey(), the same password should
36         /// be provided.
37         /// </param>
38         /// <returns>A key specified by alias.</returns>
39         /// <exception cref="ArgumentNullException">
40         /// Alias argument is null.
41         /// </exception>
42         /// <exception cref="ArgumentException">
43         /// Alias argument is invalid format.
44         /// </exception>
45         /// <exception cref="InvalidOperationException">
46         /// Key does not exist with the alias or key-protecting password isn't matched.
47         /// </exception>
48         static public Key Get(string alias, string password)
49         {
50             if (alias == null)
51                 throw new ArgumentNullException("alias cannot be null");
52
53             IntPtr ptr = IntPtr.Zero;
54
55             try
56             {
57                 Interop.CheckNThrowException(
58                     Interop.CkmcManager.GetKey(alias, password, out ptr),
59                     "Failed to get key. alias=" + alias);
60                 return new Key(ptr);
61             }
62             finally
63             {
64                 if (ptr != IntPtr.Zero)
65                     Interop.CkmcTypes.KeyFree(ptr);
66             }
67         }
68
69         /// <summary>
70         /// Gets all alias of keys which the client can access.
71         /// </summary>
72         /// <since_tizen> 3 </since_tizen>
73         /// <returns>All alias of keys which the client can access.</returns>
74         /// <exception cref="ArgumentException">No alias to get.</exception>
75         static public IEnumerable<string> GetAliases()
76         {
77             IntPtr ptr = IntPtr.Zero;
78
79             try
80             {
81                 Interop.CheckNThrowException(
82                     Interop.CkmcManager.GetKeyAliasList(out ptr),
83                     "Failed to get key aliases.");
84                 return new SafeAliasListHandle(ptr).Aliases;
85             }
86             finally
87             {
88                 if (ptr != IntPtr.Zero)
89                     Interop.CkmcTypes.AliasListAllFree(ptr);
90             }
91         }
92
93         /// <summary>
94         /// Stores a key inside secure repository based on the provided policy.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         /// <param name="alias">The name of a key to be stored.</param>
98         /// <param name="key">The key's binary value to be stored.</param>
99         /// <param name="policy">The policy about how to store a key securely.</param>
100         /// <exception cref="ArgumentNullException">
101         /// Any of argument is null.
102         /// </exception>
103         /// <exception cref="ArgumentException">
104         /// Alias argument is invalid format. key argument is invalid format.
105         /// </exception>
106         /// <exception cref="InvalidOperationException">
107         /// Key with alias does already exist.
108         /// </exception>
109         /// <remarks>
110         /// Type in key may be set to KeyType.None as an input.
111         /// Type is determined inside secure reposioty during storing keys.
112         /// </remarks>
113         /// <remarks>
114         /// If password in policy is provided, the key is additionally encrypted with
115         /// the password in policy.
116         /// </remarks>
117         static public void Save(string alias, Key key, Policy policy)
118         {
119             if (alias == null || key == null || policy == null)
120                 throw new ArgumentNullException("More than one of argument is null");
121
122             Interop.CheckNThrowException(
123                 Interop.CkmcManager.SaveKey(
124                     alias, key.ToCkmcKey(), policy.ToCkmcPolicy()),
125                 "Failed to save Key. alias=" + alias);
126         }
127
128         /// <summary>
129         /// Creates RSA private/public key pair and stores them inside secure repository
130         /// based on each policy.
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         /// <param name="size">
134         /// The size of key strength to be created. 1024, 2048, and 4096 are supported.
135         /// </param>
136         /// <param name="privateKeyAlias">The name of private key to be stored.</param>
137         /// <param name="publicKeyAlias">The name of public key to be stored.</param>
138         /// <param name="privateKeyPolicy">
139         /// The policy about how to store a private key securely.
140         /// </param>
141         /// <param name="publicKeyPolicy">
142         /// The policy about how to store a public key securely.
143         /// </param>
144         /// <exception cref="ArgumentNullException">
145         /// Any of argument is null.
146         /// </exception>
147         /// <exception cref="ArgumentException">
148         /// size is invalid. privateKeyAlias or publicKeyAlias is invalid format.
149         /// </exception>
150         /// <exception cref="InvalidOperationException">
151         /// Key with privateKeyAlias or publicKeyAlias does already exist.
152         /// </exception>
153         /// <remarks>
154         /// If password in policy is provided, the key is additionally encrypted with the
155         /// password in policy.
156         /// </remarks>
157         static public void CreateRsaKeyPair(
158             int size, string privateKeyAlias, string publicKeyAlias,
159             Policy privateKeyPolicy, Policy publicKeyPolicy)
160         {
161             if (size != 1024 && size != 2048 && size != 4096)
162                 throw new ArgumentException(string.Format("Invalid key size({0})", size));
163             else if (privateKeyAlias == null || publicKeyAlias == null ||
164                 privateKeyPolicy == null || publicKeyPolicy == null)
165                 throw new ArgumentNullException("alias and policy should not be null");
166
167             Interop.CheckNThrowException(
168                 Interop.CkmcManager.CreateKeyPairRsa(
169                     (UIntPtr)size, privateKeyAlias, publicKeyAlias,
170                     privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()),
171                 "Failed to Create RSA Key Pair");
172         }
173
174         /// <summary>
175         /// Creates DSA private/public key pair and stores them inside secure repository
176         /// based on each policy.
177         /// </summary>
178         /// <since_tizen> 3 </since_tizen>
179         /// <param name="size">
180         /// The size of key strength to be created. 1024, 2048, 3072, and 4096 are
181         /// supported.
182         /// </param>
183         /// <param name="privateKeyAlias">The name of private key to be stored.</param>
184         /// <param name="publicKeyAlias">The name of public key to be stored.</param>
185         /// <param name="privateKeyPolicy">
186         /// The policy about how to store a private key securely.
187         /// </param>
188         /// <param name="publicKeyPolicy">
189         /// The policy about how to store a public key securely.
190         /// </param>
191         /// <exception cref="ArgumentNullException">
192         /// Any of argument is null.
193         /// </exception>
194         /// <exception cref="ArgumentException">
195         /// size is invalid. privateKeyAlias or publicKeyAlias is invalid format.
196         /// </exception>
197         /// <exception cref="InvalidOperationException">
198         /// Key with privateKeyAlias or publicKeyAlias does already exist.
199         /// </exception>
200         /// <remarks>
201         /// If password in policy is provided, the key is additionally encrypted with
202         /// the password in policy.
203         /// </remarks>
204         static public void CreateDsaKeyPair(
205             int size, string privateKeyAlias, string publicKeyAlias,
206             Policy privateKeyPolicy, Policy publicKeyPolicy)
207         {
208             if (size != 1024 && size != 2048 && size != 3072 && size != 4096)
209                 throw new ArgumentException(string.Format("Invalid key size({0})", size));
210             else if (privateKeyAlias == null || publicKeyAlias == null ||
211                 privateKeyPolicy == null || publicKeyPolicy == null)
212                 throw new ArgumentNullException("alias and policy should not be null");
213
214             Interop.CheckNThrowException(
215                 Interop.CkmcManager.CreateKeyPairDsa(
216                     (UIntPtr)size, privateKeyAlias, publicKeyAlias,
217                     privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()),
218                 "Failed to Create DSA Key Pair");
219         }
220
221         /// <summary>
222         /// Creates ECDSA private/public key pair and stores them inside secure repository
223         /// based on each policy.
224         /// </summary>
225         /// <since_tizen> 3 </since_tizen>
226         /// <param name="type">The type of elliptic curve of ECDSA.</param>
227         /// <param name="privateKeyAlias">The name of private key to be stored.</param>
228         /// <param name="publicKeyAlias">The name of public key to be stored.</param>
229         /// <param name="privateKeyPolicy">
230         /// The policy about how to store a private key securely.
231         /// </param>
232         /// <param name="publicKeyPolicy">
233         /// The policy about how to store a public key securely.
234         /// </param>
235         /// <exception cref="ArgumentNullException">
236         /// Any of argument is null.
237         /// </exception>
238         /// <exception cref="ArgumentException">
239         /// Elliptic curve type is invalid. privateKeyAlias or publicKeyAlias is
240         /// invalid format.
241         /// </exception>
242         /// <exception cref="InvalidOperationException">
243         /// Key with privateKeyAlias or publicKeyAlias does already exist.
244         /// </exception>
245         /// <remarks>
246         /// If password in policy is provided, the key is additionally encrypted with
247         /// the password in policy.
248         /// </remarks>
249         static public void CreateEcdsaKeyPair(
250             EllipticCurveType type, string privateKeyAlias, string publicKeyAlias,
251             Policy privateKeyPolicy, Policy publicKeyPolicy)
252         {
253             if (privateKeyAlias == null || publicKeyAlias == null ||
254                 privateKeyPolicy == null || publicKeyPolicy == null)
255                 throw new ArgumentNullException("alias and policy should not be null");
256
257             Interop.CheckNThrowException(
258                 Interop.CkmcManager.CreateKeyPairEcdsa(
259                     (int)type, privateKeyAlias, publicKeyAlias,
260                     privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()),
261                 "Failed to Create ECDSA Key Pair");
262         }
263
264         /// <summary>
265         /// Creates AES key and stores it inside secure repository based on each policy.
266         /// </summary>
267         /// <since_tizen> 3 </since_tizen>
268         /// <param name="size">
269         /// The size of key strength to be created. 128, 192 and 256 are supported.
270         /// </param>
271         /// <param name="keyAlias">The name of key to be stored.</param>
272         /// <param name="policy">The policy about how to store the key securely.</param>
273         /// <exception cref="ArgumentNullException">
274         /// keyAlias or policy is null.
275         /// </exception>
276         /// <exception cref="ArgumentException">
277         /// Key size is invalid. keyAlias is invalid format.
278         /// </exception>
279         /// <exception cref="InvalidOperationException">
280         /// Key with privateKeyAlias or publicKeyAlias does already exist.
281         /// </exception>
282         /// <remarks>
283         /// If password in policy is provided, the key is additionally encrypted with
284         /// the password in policy.
285         /// </remarks>
286         static public void CreateAesKey(int size, string keyAlias, Policy policy)
287         {
288             if (size != 128 && size != 192 && size != 256)
289                 throw new ArgumentException(string.Format("Invalid key size({0})", size));
290             else if (keyAlias == null || policy == null)
291                 throw new ArgumentNullException("alias and policy should not be null");
292
293             Interop.CheckNThrowException(
294                 Interop.CkmcManager.CreateKeyAes(
295                     (UIntPtr)size, keyAlias, policy.ToCkmcPolicy()),
296                 "Failed to AES Key");
297         }
298
299         // to be static class safely
300         internal KeyManager()
301         {
302         }
303     }
304 }