1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 namespace System.Security.Cryptography.X509Certificates.Tests
6 {
7     /// <summary>
8     /// A type to extend the Dispose on X509Chain to also dispose all of the X509Certificate objects
9     /// in the ChainElements structure.
10     /// </summary>
11     internal sealed class ChainHolder : IDisposable
12     {
13         private X509Chain _chain;
14 
ChainHolder()15         public ChainHolder()
16         {
17             _chain = new X509Chain();
18         }
19 
ChainHolder(IntPtr chainContext)20         public ChainHolder(IntPtr chainContext)
21         {
22             _chain = new X509Chain(chainContext);
23         }
24 
25         public X509Chain Chain => _chain;
26 
Dispose()27         public void Dispose()
28         {
29             DisposeChainElements();
30 
31             Chain.Dispose();
32         }
33 
DisposeChainElements()34         public void DisposeChainElements()
35         {
36             int count = Chain.ChainElements.Count;
37 
38             for (int i = 0; i < count; i++)
39             {
40                 Chain.ChainElements[i].Certificate.Dispose();
41             }
42         }
43     }
44 }
45