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.Collections.Generic;
21 namespace Tizen.Security.SecureRepository
24 /// This class provides the methods for storing and retrieving data.
26 /// <since_tizen> 3 </since_tizen>
27 public class DataManager : Manager
30 /// Gets data from the secure repository.
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
39 /// <returns>Data specified by alias.</returns>
40 /// <exception cref="ArgumentNullException">
41 /// The alias argument is null.
43 /// <exception cref="ArgumentException">
44 /// The alias argument is in the invalid format.
46 /// <exception cref="InvalidOperationException">
47 /// Data does not exist with the alias or data-protecting password isn't matched.
49 static public byte[] Get(string alias, string password)
52 throw new ArgumentNullException("alias cannot be null");
54 IntPtr ptr = IntPtr.Zero;
58 Interop.CheckNThrowException(
59 Interop.CkmcManager.GetData(alias, password, out ptr),
60 "Failed to get certificate. alias=" + alias);
61 return new SafeRawBufferHandle(ptr).Data;
65 if (ptr != IntPtr.Zero)
66 Interop.CkmcTypes.BufferFree(ptr);
71 /// Gets all aliases of data, which the client can access.
73 /// <since_tizen> 3 </since_tizen>
74 /// <returns>All aliases of data, which the client can access.</returns>
75 /// <exception cref="ArgumentException">No alias to get.</exception>
76 static public IEnumerable<string> GetAliases()
78 IntPtr ptr = IntPtr.Zero;
82 Interop.CheckNThrowException(
83 Interop.CkmcManager.GetDataAliasList(out ptr),
84 "Failed to get data aliases");
85 return new SafeAliasListHandle(ptr).Aliases;
89 if (ptr != IntPtr.Zero)
90 Interop.CkmcTypes.AliasListAllFree(ptr);
95 /// Stores data inside the secure repository based on the provided policy.
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.
104 /// <exception cref="ArgumentException">
105 /// The alias argument is in the invalid format. Data policy cannot be unextractable.
107 /// <exception cref="InvalidOperationException">
108 /// Data with alias already exist.
110 static public void Save(string alias, byte[] data, Policy policy)
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");
117 Interop.CheckNThrowException(
118 Interop.CkmcManager.SaveData(
120 new Interop.CkmcRawBuffer(new PinnedObject(data), data.Length),
121 policy.ToCkmcPolicy()),
122 "Failed to save data. alias=" + alias);
125 // to be static class safely
126 internal DataManager()