0dfaae28b70afaac81f283c58d2a485c26dbb990
[platform/core/csapi/tizenfx.git] / src / 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         /// Load Certificate from the given file path.
30         /// </summary>
31         /// <param name="filePath">The path of certificate file to be loaded.</param>
32         /// <returns>Loaded certificate class instance.</returns>
33         /// <exception cref="InvalidOperationException">Invalid certificate file format. Provided file path does not exist or cannot be accessed.</exception>
34         static public Certificate Load(string filePath)
35         {
36             IntPtr ptr = new IntPtr();
37
38             int ret = Interop.CkmcTypes.LoadCertFromFile(filePath, out ptr);
39             Interop.CheckNThrowException(ret, "Failed to load Certificate. file=" + filePath);
40
41             return new Certificate(ptr);
42         }
43
44         /// <summary>
45         /// A constructor of Certificate that takes the binary and its format.
46         /// </summary>
47         /// <param name="binary">The binary data of a certificate.</param>
48         /// <param name="format">The format of the binary data.</param>
49         public Certificate(byte[] binary, DataFormat format) : base(IntPtr.Zero, true)
50         {
51             this.SetHandle(IntPtr.Zero);
52             Binary = binary;
53             Format = format;
54         }
55
56         internal Certificate(IntPtr ptrCkmcCert, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
57         {
58             base.SetHandle(ptrCkmcCert);
59
60             CkmcCert ckmcCert = Marshal.PtrToStructure<CkmcCert>(ptrCkmcCert);
61             Binary = new byte[ckmcCert.size];
62             Marshal.Copy(ckmcCert.rawCert, Binary, 0, Binary.Length);
63             Format = (DataFormat)ckmcCert.dataFormat;
64         }
65
66         /// <summary>
67         /// The binary value of a certificate.
68         /// </summary>
69         public byte[] Binary
70         {
71             get; set;
72         }
73
74         /// <summary>
75         /// The format of the binary value.
76         /// </summary>
77         public DataFormat Format
78         {
79             get; set;
80         }
81
82         internal CkmcCert ToCkmcCert()
83         {
84             byte[] bin = (Binary != null) ? Binary : new byte[0];
85             return new Interop.CkmcCert(new PinnedObject(bin), bin.Length, (int)Format);
86         }
87
88         /// <summary>
89         /// Gets a value that indicates whether the handle is invalid.
90         /// </summary>
91         public override bool IsInvalid
92         {
93             get { return handle == IntPtr.Zero; }
94         }
95
96         /// <summary>
97         /// When overridden in a derived class, executes the code required to free the handle.
98         /// </summary>
99         /// <returns>true if the handle is released successfully.</returns>
100         protected override bool ReleaseHandle()
101         {
102             if (IsInvalid) // do not release
103                 return true;
104             Interop.CkmcTypes.CertFree(handle);
105             this.SetHandle(IntPtr.Zero);
106             return true;
107         }
108     }
109 }