2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
22 namespace Tizen.Security.SecureRepository
24 internal class SafeCertificateListHandle
26 private IEnumerable<Certificate> _certificates;
28 public SafeCertificateListHandle(IEnumerable<Certificate> certs)
30 _certificates = certs;
33 public SafeCertificateListHandle(IntPtr ptr)
36 var certs = new List<Certificate>();
37 while (cur != IntPtr.Zero)
39 var ckmcCertList = Marshal.PtrToStructure<CkmcCertList>(cur);
40 certs.Add(new Certificate(ckmcCertList.cert));
41 cur = ckmcCertList.next;
44 this._certificates = certs;
47 public IEnumerable<Certificate> Certificates
49 get { return _certificates; }
52 internal IntPtr GetHandle()
54 if (_certificates == null)
57 IntPtr first = IntPtr.Zero;
58 IntPtr previous = IntPtr.Zero;
59 IntPtr certPtr = IntPtr.Zero;
63 foreach (var cert in this._certificates)
65 // initialize local variables to release memory safely for
66 // in case of exception occured
67 certPtr = IntPtr.Zero;
69 certPtr = cert.GetHandle();
72 if (previous == IntPtr.Zero)
74 Interop.CheckNThrowException(
75 Interop.CkmcTypes.CertListNew(certPtr, out outCertList),
76 "Failed to create new CertificateList.");
81 Interop.CheckNThrowException(
82 Interop.CkmcTypes.CertListAdd(previous, certPtr,
84 "Failed to add Certificate to CertificateList.");
86 previous = outCertList;
93 if (first != IntPtr.Zero)
94 Interop.CkmcTypes.CertListAllFree(first);
95 if (certPtr != IntPtr.Zero)
96 Interop.CkmcTypes.CertFree(certPtr);