/
DataManager.cs
130 lines (121 loc) · 4.84 KB
/
DataManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
using System;
using System.Collections.Generic;
using static Interop;
namespace Tizen.Security.SecureRepository
{
/// <summary>
/// This class provides the methods for storing and retrieving data.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class DataManager : Manager
{
/// <summary>
/// Gets data from the secure repository.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="alias">The name of a certificate to retrieve.</param>
/// <param name="password">
/// The password used in decrypting a data value.
/// If password of policy is provided in SaveData(), the same password should
/// be provided.
/// </param>
/// <returns>Data specified by alias.</returns>
/// <exception cref="ArgumentNullException">
/// The alias argument is null.
/// </exception>
/// <exception cref="ArgumentException">
/// The alias argument is in the invalid format.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Data does not exist with the alias or data-protecting password isn't matched.
/// </exception>
static public byte[] Get(string alias, string password)
{
if (alias == null)
throw new ArgumentNullException("alias cannot be null");
IntPtr ptr = IntPtr.Zero;
try
{
Interop.CheckNThrowException(
Interop.CkmcManager.GetData(alias, password, out ptr),
"Failed to get certificate. alias=" + alias);
return new SafeRawBufferHandle(ptr).Data;
}
finally
{
if (ptr != IntPtr.Zero)
Interop.CkmcTypes.BufferFree(ptr);
}
}
/// <summary>
/// Gets all aliases of data, which the client can access.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>All aliases of data, which the client can access.</returns>
/// <exception cref="ArgumentException">No alias to get.</exception>
static public IEnumerable<string> GetAliases()
{
IntPtr ptr = IntPtr.Zero;
try
{
Interop.CheckNThrowException(
Interop.CkmcManager.GetDataAliasList(out ptr),
"Failed to get data aliases");
return new SafeAliasListHandle(ptr).Aliases;
}
finally
{
if (ptr != IntPtr.Zero)
Interop.CkmcTypes.AliasListAllFree(ptr);
}
}
/// <summary>
/// Stores data inside the secure repository based on the provided policy.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="alias">The name of data to be stored.</param>
/// <param name="data">The binary value to be stored.</param>
/// <param name="policy">The policy about how to store data securely.</param>
/// <exception cref="ArgumentNullException">
/// Any of argument is null.
/// </exception>
/// <exception cref="ArgumentException">
/// The alias argument is in the invalid format. Data policy cannot be unextractable.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Data with alias already exist.
/// </exception>
static public void Save(string alias, byte[] data, Policy policy)
{
if (alias == null || data == null || policy == null)
throw new ArgumentNullException("alias and policy should be null");
else if (policy.Extractable == false)
throw new ArgumentException("Data should be extractable");
Interop.CheckNThrowException(
Interop.CkmcManager.SaveData(
alias,
new Interop.CkmcRawBuffer(new PinnedObject(data), data.Length),
policy.ToCkmcPolicy()),
"Failed to save data. alias=" + alias);
}
// to be static class safely
internal DataManager()
{
}
}
}