Fix warning related mandatory comments
[platform/core/csapi/security.git] / Tizen.Security / Tizen.Security.SecureRepository / SafeCertificateListHandle.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using static Interop;
21
22 namespace Tizen.Security.SecureRepository
23 {
24     internal class SafeCertificateListHandle : SafeHandle
25     {
26         private IEnumerable<Certificate> _certificates;
27
28         public SafeCertificateListHandle(IEnumerable<Certificate> certs) : base(IntPtr.Zero, true)
29         {
30             this.SetHandle(IntPtr.Zero);
31             _certificates = certs;
32         }
33
34         public SafeCertificateListHandle(IntPtr ptrCerts, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
35         {
36             this.SetHandle(ptrCerts);
37
38             List<Certificate> certs = new List<Certificate>();
39             IntPtr ptrCurr = handle;
40             while (ptrCurr != IntPtr.Zero)
41             {
42                 CkmcCertList ckmcCertList = (CkmcCertList)Marshal.PtrToStructure(ptrCurr, typeof(CkmcCertList));
43                 certs.Add(new Certificate(ckmcCertList.cert, false));
44                 ptrCurr = ckmcCertList.next;
45             }
46
47             _certificates = certs;
48         }
49
50         public IEnumerable<Certificate> Certificates
51         {
52             get { return _certificates; }
53         }
54
55         internal IntPtr ToCkmcCertificateListPtr()
56         {
57             if (_certificates == null)
58                 return IntPtr.Zero;
59
60             if (!IsInvalid)
61                 return handle;
62
63             IntPtr first = IntPtr.Zero;
64             IntPtr previous = IntPtr.Zero;
65
66             int ret;
67             foreach (Certificate cert in _certificates)
68             {
69                 IntPtr certPtr;
70                 ret = Interop.CkmcTypes.CertNew(cert.Binary, (uint)cert.Binary.Length, (int)cert.Format, out certPtr);
71                 Interop.CheckNThrowException(ret, "Failed to create new Certificate.");
72
73                 IntPtr outCertList;
74                 if (previous == IntPtr.Zero)
75                 {
76                     ret = Interop.CkmcTypes.CertListNew(certPtr, out outCertList);
77                     Interop.CheckNThrowException(ret, "Failed to create new CertificateList.");
78                     first = outCertList;
79                     previous = outCertList;
80                 }
81                 else
82                 {
83                     ret = Interop.CkmcTypes.CertListAdd(previous, certPtr, out outCertList);
84                     Interop.CheckNThrowException(ret, "Failed to add Certificate to CertificateList.");
85                     previous = outCertList;
86                 }
87             }
88
89             this.SetHandle(first);
90             return first;
91         }
92
93         /// <summary>
94         /// Gets a value that indicates whether the handle is invalid.
95         /// </summary>
96         public override bool IsInvalid
97         {
98             get { return (handle == IntPtr.Zero); }
99         }
100
101         /// <summary>
102         /// When overridden in a derived class, executes the code required to free the handle.
103         /// </summary>
104         /// <returns>true if the handle is released successfully</returns>
105         protected override bool ReleaseHandle()
106         {
107             if (handle == IntPtr.Zero) // do not release
108                 return true;
109
110             Interop.CkmcTypes.CertListAllFree(handle);
111             this.SetHandle(IntPtr.Zero);
112             return true;
113         }
114     }
115 }