Merge pull request #3749 from BrzVlad/fix-mips-fix
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509LookupMonoCollection.cs
1 //
2 // MonoBtlsX509LookupMonoCollection.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26 #if SECURITY_DEP
27 using System;
28 using System.IO;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31 using System.Security.Cryptography.X509Certificates;
32
33 #if MONOTOUCH
34 using MonoTouch;
35 #endif
36
37 namespace Mono.Btls
38 {
39         internal class MonoBtlsX509LookupMonoCollection : MonoBtlsX509LookupMono
40         {
41                 long[] hashes;
42                 MonoBtlsX509[] certificates;
43                 X509CertificateCollection collection;
44                 MonoBtlsX509TrustKind trust;
45
46                 internal MonoBtlsX509LookupMonoCollection (X509CertificateCollection collection, MonoBtlsX509TrustKind trust)
47                 {
48                         this.collection = collection;
49                         this.trust = trust;
50                 }
51
52                 void Initialize ()
53                 {
54                         if (certificates != null)
55                                 return;
56
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
61                                 // trust settings.
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 ();
66                         }
67                 }
68
69                 protected override MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name)
70                 {
71                         Console.WriteLine ("COLLECTION LOOKUP: {0:x} - {1}", name.GetHash (), name.GetString ());
72                         Initialize ();
73
74                         var hash = name.GetHash ();
75                         for (int i = 0; i < certificates.Length; i++) {
76                                 if (hashes [i] == hash)
77                                         return certificates [i];
78                         }
79
80                         return null;
81                 }
82
83                 protected override void Close ()
84                 {
85                         try {
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;
91                                                 }
92                                         }
93                                         certificates = null;
94                                         hashes = null;
95                                 }
96                         } finally {
97                                 base.Close ();
98                         }
99                 }
100         }
101 }
102 #endif