Fix warning related mandatory comments
[platform/core/csapi/security.git] / Tizen.Security.SecureRepository / Tizen.Security.SecureRepository / Certificate.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.Runtime.InteropServices;
19 using static Interop;
20
21 namespace Tizen.Security.SecureRepository
22 {
23     /// <summary>
24     /// Class that represents a certificate.
25     /// </summary>
26     public class Certificate : SafeHandle
27     {
28         /// <summary>
29         /// A constructor of Certificate that takes the binary and its format.
30         /// </summary>
31         /// <param name="binary">The binary data of a certificate.</param>
32         /// <param name="format">The format of the binary data.</param>
33         public Certificate(byte[] binary, DataFormat format) : base(IntPtr.Zero, true)
34         {
35             this.SetHandle(IntPtr.Zero);
36             Binary = binary;
37             Format = format;
38         }
39
40         internal Certificate(IntPtr ptrCkmcCert, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
41         {
42             base.SetHandle(ptrCkmcCert);
43
44             CkmcCert ckmcCert = (CkmcCert)Marshal.PtrToStructure(ptrCkmcCert, typeof(CkmcCert));
45             Binary = new byte[ckmcCert.size];
46             Marshal.Copy(ckmcCert.rawCert, Binary, 0, Binary.Length);
47             Format = (DataFormat)ckmcCert.dataFormat;
48         }
49
50         /// <summary>
51         /// The binary value of a certificate.
52         /// </summary>
53         public byte[] Binary
54         {
55             get; set;
56         }
57
58         /// <summary>
59         /// The format of the binary value.
60         /// </summary>
61         public DataFormat Format
62         {
63             get; set;
64         }
65
66         internal CkmcCert ToCkmcCert()
67         {
68             return new Interop.CkmcCert(new PinnedObject(Binary), Binary.Length, (int)Format);
69         }
70
71         /// <summary>
72         /// Gets a value that indicates whether the handle is invalid.
73         /// </summary>
74         public override bool IsInvalid
75         {
76             get { return handle == IntPtr.Zero; }
77         }
78
79         /// <summary>
80         /// When overridden in a derived class, executes the code required to free the handle.
81         /// </summary>
82         /// <returns>true if the handle is released successfully</returns>
83         protected override bool ReleaseHandle()
84         {
85             if (IsInvalid) // do not release
86                 return true;
87             Interop.CkmcTypes.CertFree(handle);
88             this.SetHandle(IntPtr.Zero);
89             return true;
90         }
91     }
92 }