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 certificate.
26 /// <since_tizen> 3 </since_tizen>
27 public class Certificate
30 /// Loads Certificate from the given file path.
32 /// <since_tizen> 3 </since_tizen>
33 /// <param name="filePath">The path of certificate file to be loaded.</param>
34 /// <returns>Loaded certificate class instance.</returns>
35 /// <exception cref="ArgumentNullException">
36 /// The filePath should not be null
38 /// <exception cref="InvalidOperationException">
39 /// Invalid certificate file format. Provided file path does not exist or
40 /// cannot be accessed.
42 static public Certificate Load(string filePath)
45 throw new ArgumentNullException("filepath should not be null");
47 IntPtr ptr = IntPtr.Zero;
49 Interop.CheckNThrowException(
50 CkmcTypes.LoadCertFromFile(filePath, out ptr),
51 "Failed to load Certificate: " + filePath);
53 return new Certificate(ptr);
57 /// A constructor of Certificate that takes the binary and its format.
59 /// <since_tizen> 3 </since_tizen>
60 /// <param name="binary">The binary data of a certificate.</param>
61 /// <param name="format">The format of the binary data.</param>
62 public Certificate(byte[] binary, DataFormat format)
68 internal Certificate(IntPtr ptr)
70 if (ptr == IntPtr.Zero)
71 throw new ArgumentNullException("Returned ptr from CAPI cannot be null");
73 var ckmcCert = Marshal.PtrToStructure<CkmcCert>(ptr);
74 this.Binary = new byte[(int)ckmcCert.size];
75 Marshal.Copy(ckmcCert.rawCert, this.Binary, 0, this.Binary.Length);
76 this.Format = (DataFormat)ckmcCert.dataFormat;
79 // Refresh handle(IntPtr) always. Because C# layer
80 // properties(Binary, Format) could be changed.
81 internal IntPtr GetHandle()
83 IntPtr ptr = IntPtr.Zero;
88 this.Binary, (UIntPtr)this.Binary.Length, (int)this.Format,
90 "Failed to create cert");
96 if (ptr != IntPtr.Zero)
97 CkmcTypes.CertFree(ptr);
104 /// The binary value of a certificate.
106 /// <since_tizen> 3 </since_tizen>
113 /// The format of the binary value.
115 /// <since_tizen> 3 </since_tizen>
116 public DataFormat Format
121 internal CkmcCert ToCkmcCert()
123 return new Interop.CkmcCert(
124 (Binary == null) ? IntPtr.Zero : new PinnedObject(this.Binary),
125 (Binary == null) ? 0 : this.Binary.Length,