2 // MonoBtlsX509LookupMonoCollection.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31 using System.Security.Cryptography.X509Certificates;
39 internal class MonoBtlsX509LookupMonoCollection : MonoBtlsX509LookupMono
42 MonoBtlsX509[] certificates;
43 X509CertificateCollection collection;
44 MonoBtlsX509TrustKind trust;
46 internal MonoBtlsX509LookupMonoCollection (X509CertificateCollection collection, MonoBtlsX509TrustKind trust)
48 this.collection = collection;
54 if (certificates != null)
57 hashes = new long [collection.Count];
58 certificates = new MonoBtlsX509 [collection.Count];
59 for (int i = 0; i < collection.Count; i++) {
60 // Create new 'X509 *' instance since we need to modify it to add the
62 var data = collection [i].GetRawCertData ();
63 certificates [i] = MonoBtlsX509.LoadFromData (data, MonoBtlsX509Format.DER);
64 certificates [i].AddExplicitTrust (trust);
65 hashes [i] = certificates [i].GetSubjectNameHash ();
69 protected override MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name)
71 Console.WriteLine ("COLLECTION LOOKUP: {0:x} - {1}", name.GetHash (), name.GetString ());
74 var hash = name.GetHash ();
75 for (int i = 0; i < certificates.Length; i++) {
76 if (hashes [i] == hash)
77 return certificates [i];
83 protected override void Close ()
86 if (certificates != null) {
87 for (int i = 0; i < certificates.Length; i++) {
88 if (certificates [i] != null) {
89 certificates [i].Dispose ();
90 certificates [i] = null;