Release 4.0.0-preview1-00051
[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     /// <since_tizen> 3 </since_tizen>
27     public class Certificate
28     {
29         /// <summary>
30         /// Load Certificate from the given file path.
31         /// </summary>
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         /// filePath should not be null
37         /// </exception>
38         /// <exception cref="InvalidOperationException">
39         /// Invalid certificate file format. Provided file path does not exist or
40         /// cannot be accessed.
41         /// </exception>
42         static public Certificate Load(string filePath)
43         {
44             if (filePath == null)
45                 throw new ArgumentNullException("filepath should not be null");
46
47             IntPtr ptr = IntPtr.Zero;
48
49             Interop.CheckNThrowException(
50                 CkmcTypes.LoadCertFromFile(filePath, out ptr),
51                 "Failed to load Certificate: " + filePath);
52
53             return new Certificate(ptr);
54         }
55
56         /// <summary>
57         /// A constructor of Certificate that takes the binary and its format.
58         /// </summary>
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)
63         {
64             this.Binary = binary;
65             this.Format = format;
66         }
67
68         internal Certificate(IntPtr ptr)
69         {
70             if (ptr == IntPtr.Zero)
71                 throw new ArgumentNullException("Returned ptr from CAPI cannot be null");
72
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;
77         }
78
79         // Refresh handle(IntPtr) always. Because C# layer
80         // properties(Binary, Format) could be changed.
81         internal IntPtr GetHandle()
82         {
83             IntPtr ptr = IntPtr.Zero;
84             try
85             {
86                 CheckNThrowException(
87                     CkmcTypes.CertNew(
88                         this.Binary, (UIntPtr)this.Binary.Length, (int)this.Format,
89                         out ptr),
90                     "Failed to create cert");
91
92                 return ptr;
93             }
94             catch
95             {
96                 if (ptr != IntPtr.Zero)
97                     CkmcTypes.CertFree(ptr);
98
99                 throw;
100             }
101         }
102
103         /// <summary>
104         /// The binary value of a certificate.
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         public byte[] Binary
108         {
109             get; set;
110         }
111
112         /// <summary>
113         /// The format of the binary value.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         public DataFormat Format
117         {
118             get; set;
119         }
120
121         internal CkmcCert ToCkmcCert()
122         {
123             return new Interop.CkmcCert(
124                 (Binary == null) ? IntPtr.Zero : new PinnedObject(this.Binary),
125                 (Binary == null) ? 0 : this.Binary.Length,
126                 (int)Format);
127         }
128     }
129 }