Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / Tizen.Security.SecureRepository / DataManager.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 using static Interop;
20
21 namespace Tizen.Security.SecureRepository
22 {
23     /// <summary>
24     /// This class provides the methods storing and retrieving data.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class DataManager : Manager
28     {
29         /// <summary>
30         /// Gets data from secure repository.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         /// <param name="alias">The name of a certificate to retrieve.</param>
34         /// <param name="password">
35         /// The password used in decrypting a data value.
36         /// If password of policy is provided in SaveData(), the same password should
37         /// be provided.
38         /// </param>
39         /// <returns>Data specified by alias.</returns>
40         /// <exception cref="ArgumentNullException">
41         /// Alias argument is null.
42         /// </exception>
43         /// <exception cref="ArgumentException">
44         /// Alias argument is invalid format.
45         /// </exception>
46         /// <exception cref="InvalidOperationException">
47         /// Data does not exist with the alias or data-protecting password isn't matched.
48         /// </exception>
49         static public byte[] Get(string alias, string password)
50         {
51             if (alias == null)
52                 throw new ArgumentNullException("alias cannot be null");
53
54             IntPtr ptr = IntPtr.Zero;
55
56             try
57             {
58                 Interop.CheckNThrowException(
59                     Interop.CkmcManager.GetData(alias, password, out ptr),
60                     "Failed to get certificate. alias=" + alias);
61                 return new SafeRawBufferHandle(ptr).Data;
62             }
63             finally
64             {
65                 if (ptr != IntPtr.Zero)
66                     Interop.CkmcTypes.BufferFree(ptr);
67             }
68         }
69
70         /// <summary>
71         /// Gets all alias of data which the client can access.
72         /// </summary>
73         /// <since_tizen> 3 </since_tizen>
74         /// <returns>All alias of data which the client can access.</returns>
75         /// <exception cref="ArgumentException">No alias to get.</exception>
76         static public IEnumerable<string> GetAliases()
77         {
78             IntPtr ptr = IntPtr.Zero;
79
80             try
81             {
82                 Interop.CheckNThrowException(
83                     Interop.CkmcManager.GetDataAliasList(out ptr),
84                     "Failed to get data aliases");
85                 return new SafeAliasListHandle(ptr).Aliases;
86             }
87             finally
88             {
89                 if (ptr != IntPtr.Zero)
90                     Interop.CkmcTypes.AliasListAllFree(ptr);
91             }
92         }
93
94         /// <summary>
95         /// Stores data inside secure repository based on the provided policy.
96         /// </summary>
97         /// <since_tizen> 3 </since_tizen>
98         /// <param name="alias">The name of data to be stored.</param>
99         /// <param name="data">The binary value to be stored.</param>
100         /// <param name="policy">The policy about how to store data securely.</param>
101         /// <exception cref="ArgumentNullException">
102         /// Any of argument is null.
103         /// </exception>
104         /// <exception cref="ArgumentException">
105         /// Alias argument is invalid format. Data policy cannot be unextractable.
106         /// </exception>
107         /// <exception cref="InvalidOperationException">
108         /// Data with alias does already exist.
109         /// </exception>
110         static public void Save(string alias, byte[] data, Policy policy)
111         {
112             if (alias == null || data == null || policy == null)
113                 throw new ArgumentNullException("alias and policy should be null");
114             else if (policy.Extractable == false)
115                 throw new ArgumentException("Data should be extractable");
116
117             Interop.CheckNThrowException(
118                 Interop.CkmcManager.SaveData(
119                     alias,
120                     new Interop.CkmcRawBuffer(new PinnedObject(data), data.Length),
121                     policy.ToCkmcPolicy()),
122                 "Failed to save data. alias=" + alias);
123         }
124
125         // to be static class safely
126         internal DataManager()
127         {
128         }
129     }
130 }