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.Runtime.InteropServices;
21 namespace Tizen.Security.SecureRepository
24 /// The class that represents a key.
26 /// <since_tizen> 3 </since_tizen>
30 /// A constructor of Key that takes the binary, its type, and optional password
33 /// <since_tizen> 3 </since_tizen>
34 /// <param name="binary">
35 /// The binary value of a key. This binary may be encrypted with binaryPassword.
37 /// <param name="type">The key's type.</param>
38 /// <param name="binaryPassword">
39 /// The password used to decrypt binary when binary is encrypted.
41 public Key(byte[] binary, KeyType type, string binaryPassword)
45 this.BinaryPassword = binaryPassword;
48 internal Key(IntPtr ptr)
50 if (ptr == IntPtr.Zero)
51 throw new ArgumentNullException("Returned ptr from CAPI cannot be null");
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;
60 // Refresh handle(IntPtr) always. Because C# layer
61 // properties(Binary, Type, BinaryPassword) could be changed.
62 internal IntPtr GetHandle()
64 IntPtr ptr = IntPtr.Zero;
68 Interop.CkmcTypes.KeyNew(
69 this.Binary, (UIntPtr)this.Binary.Length, (int)this.Type,
70 this.BinaryPassword, out ptr),
71 "Failed to create key");
77 if (ptr != IntPtr.Zero)
78 Interop.CkmcTypes.KeyFree(ptr);
85 /// The binary value of a key.
87 /// <since_tizen> 3 </since_tizen>
96 /// <since_tizen> 3 </since_tizen>
103 /// The password used to decrypt binary when binary is encrypted. It's optional.
105 /// <since_tizen> 3 </since_tizen>
106 public string BinaryPassword
111 internal CkmcKey ToCkmcKey()
113 return new Interop.CkmcKey(
114 (Binary == null) ? IntPtr.Zero : new PinnedObject(this.Binary),
115 (Binary == null) ? 0 : this.Binary.Length,
117 this.BinaryPassword);