Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / 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
25     {
26         private IEnumerable<Certificate> _certificates;
27
28         public SafeCertificateListHandle(IEnumerable<Certificate> certs)
29         {
30             _certificates = certs;
31         }
32
33         public SafeCertificateListHandle(IntPtr ptr)
34         {
35             var cur = ptr;
36             var certs = new List<Certificate>();
37             while (cur != IntPtr.Zero)
38             {
39                 var ckmcCertList = Marshal.PtrToStructure<CkmcCertList>(cur);
40                 certs.Add(new Certificate(ckmcCertList.cert));
41                 cur = ckmcCertList.next;
42             }
43
44             this._certificates = certs;
45         }
46
47         public IEnumerable<Certificate> Certificates
48         {
49             get { return _certificates; }
50         }
51
52         internal IntPtr GetHandle()
53         {
54             if (_certificates == null)
55                 return IntPtr.Zero;
56
57             IntPtr first = IntPtr.Zero;
58             IntPtr previous = IntPtr.Zero;
59             IntPtr certPtr = IntPtr.Zero;
60
61             try
62             {
63                 foreach (var cert in this._certificates)
64                 {
65                     // initialize local variables to release memory safely for
66                     // in case of exception occured
67                     certPtr = IntPtr.Zero;
68
69                     certPtr = cert.GetHandle();
70
71                     IntPtr outCertList;
72                     if (previous == IntPtr.Zero)
73                     {
74                         Interop.CheckNThrowException(
75                             Interop.CkmcTypes.CertListNew(certPtr, out outCertList),
76                             "Failed to create new CertificateList.");
77                         first = outCertList;
78                     }
79                     else
80                     {
81                         Interop.CheckNThrowException(
82                             Interop.CkmcTypes.CertListAdd(previous, certPtr,
83                                                           out outCertList),
84                             "Failed to add Certificate to CertificateList.");
85                     }
86                     previous = outCertList;
87                 }
88
89                 return first;
90             }
91             catch
92             {
93                 if (first != IntPtr.Zero)
94                     Interop.CkmcTypes.CertListAllFree(first);
95                 if (certPtr != IntPtr.Zero)
96                     Interop.CkmcTypes.CertFree(certPtr);
97
98                 throw;
99             }
100         }
101     }
102 }