[cert-sync] Don't import into BTLS store if BTLS is not available (#4475)
[mono.git] / mcs / tools / security / cert-sync.cs
1 //
2 // cert-sync.cs: Import the root certificates from a certificate store into Mono
3 //
4 // Authors:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //      Jo Shields <jo.shields@xamarin.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2014 Xamarin, Inc (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Net;
35 using System.Reflection;
36 using System.Security.Cryptography;
37 using System.Text;
38
39 using Mono.Security.X509;
40
41 [assembly: AssemblyTitle ("Mono Certificate Store Sync")]
42 [assembly: AssemblyDescription ("Populate Mono certificate store from a concatenated list of certificates.")]
43
44 namespace Mono.Tools
45 {
46
47         class CertSync
48         {
49         
50                 static string inputFile;
51                 static bool quiet;
52                 static bool userStore;
53
54                 static X509Certificate DecodeCertificate (string s)
55                 {
56                         byte[] rawdata = Convert.FromBase64String (s);
57                         return new X509Certificate (rawdata);
58                 }
59
60                 static Stream GetFile ()
61                 {
62                         try {
63                                 if (inputFile != null) {
64                                         return File.OpenRead (inputFile);
65                                 } else {
66                                         return null;
67                                 }
68                         } catch {
69                                 return null;
70                         }
71                 }
72
73                 static X509CertificateCollection DecodeCollection ()
74                 {
75                         X509CertificateCollection roots = new X509CertificateCollection ();
76                         StringBuilder sb = new StringBuilder ();
77                         bool processing = false;
78
79                         using (Stream s = GetFile ()) {
80                                 if (s == null) {
81                                         WriteLine ("Couldn't retrieve the file using the supplied information.");
82                                         return null;
83                                 }
84
85                                 StreamReader sr = new StreamReader (s);
86                                 while (true) {
87                                         string line = sr.ReadLine ();
88                                         if (line == null)
89                                                 break;
90
91                                         if (processing) {
92                                                 if (line.StartsWith ("-----END CERTIFICATE-----")) {
93                                                         processing = false;
94                                                         X509Certificate root = DecodeCertificate (sb.ToString ());
95                                                         roots.Add (root);
96
97                                                         sb = new StringBuilder ();
98                                                         continue;
99                                                 }
100                                                 sb.Append (line);
101                                         } else {
102                                                 processing = line.StartsWith ("-----BEGIN CERTIFICATE-----");
103                                         }
104                                 }
105                                 return roots;
106                         }
107                 }
108
109                 static int Process ()
110                 {
111                         X509CertificateCollection roots = DecodeCollection ();
112                         if (roots == null) {
113                                 return 1;
114                         } else if (roots.Count == 0) {
115                                 WriteLine ("No certificates were found.");
116                                 return 0;
117                         }
118
119                         if (userStore) {
120                                 WriteLine ("Importing into legacy user store:");
121                                 ImportToStore (roots, X509StoreManager.CurrentUser.TrustedRoot);
122                                 if (Mono.Security.Interface.MonoTlsProviderFactory.IsProviderSupported ("btls")) {
123                                         WriteLine ("");
124                                         WriteLine ("Importing into BTLS user store:");
125                                         ImportToStore (roots, X509StoreManager.NewCurrentUser.TrustedRoot);
126                                 }
127                         } else {
128                                 WriteLine ("Importing into legacy system store:");
129                                 ImportToStore (roots, X509StoreManager.LocalMachine.TrustedRoot);
130                                 if (Mono.Security.Interface.MonoTlsProviderFactory.IsProviderSupported ("btls")) {
131                                         WriteLine ("");
132                                         WriteLine ("Importing into BTLS system store:");
133                                         ImportToStore (roots, X509StoreManager.NewLocalMachine.TrustedRoot);
134                                 }
135                         }
136
137                         return 0;
138                 }
139
140                 static void ImportToStore (X509CertificateCollection roots, X509Store store)
141                 {
142                         X509CertificateCollection trusted = store.Certificates;
143                         int additions = 0;
144                         WriteLine ("I already trust {0}, your new list has {1}", trusted.Count, roots.Count);
145                         foreach (X509Certificate root in roots) {
146                                 if (!trusted.Contains (root)) {
147                                         try {
148                                                 store.Import (root);
149                                                 WriteLine ("Certificate added: {0}", root.SubjectName);
150                                                 additions++;
151                                         } catch (Exception e) {
152                                                 WriteLine ("Warning: Could not import {0}", root.SubjectName);
153                                                 WriteLine (e.ToString ());
154                                         }
155                                 }
156                         }
157                         if (additions > 0)
158                                 WriteLine ("{0} new root certificates were added to your trust store.", additions);
159
160                         X509CertificateCollection removed = new X509CertificateCollection ();
161                         foreach (X509Certificate trust in trusted) {
162                                 if (!roots.Contains (trust)) {
163                                         removed.Add (trust);
164                                 }
165                         }
166                         if (removed.Count > 0) {
167                                 WriteLine ("{0} previously trusted certificates were removed.", removed.Count);
168
169                                 foreach (X509Certificate old in removed) {
170                                         store.Remove (old);
171                                         WriteLine ("Certificate removed: {0}", old.SubjectName);
172                                 }
173                         }
174                         WriteLine ("Import process completed.");
175                 }
176
177                 static string Thumbprint (string algorithm, X509Certificate certificate)
178                 {
179                         HashAlgorithm hash = HashAlgorithm.Create (algorithm);
180                         byte[] digest = hash.ComputeHash (certificate.RawData);
181                         return BitConverter.ToString (digest);
182                 }
183
184                 static bool ParseOptions (string[] args)
185                 {
186                         if (args.Length < 1)
187                                 return false;
188
189                         for (int i = 0; i < args.Length - 1; i++) {
190                                 switch (args [i]) {
191                                 case "--quiet":
192                                         quiet = true;
193                                         break;
194                                 case "--user":
195                                         userStore = true;
196                                         break;
197                                 case "--btls": // we always import to the btls store too now, keep for compat
198                                         break;
199                                 default:
200                                         WriteLine ("Unknown option '{0}'.", args[i]);
201                                         return false;
202                                 }
203                         }
204                         inputFile = args [args.Length - 1];
205                         if (!File.Exists (inputFile)) {
206                                 WriteLine ("Unknown option or file not found '{0}'.", inputFile);
207                                 return false;
208                         }
209                         return true;
210                 }
211
212                 static void Header ()
213                 {
214                         Console.WriteLine (new AssemblyInfo ().ToString ());
215                 }
216
217                 static void Help ()
218                 {
219                         Console.WriteLine ("Usage: cert-sync [--quiet] [--user] system-ca-bundle.crt");
220                         Console.WriteLine ("Where system-ca-bundle.crt is in PEM format");
221                 }
222
223                 static void WriteLine (string str)
224                 {
225                         if (!quiet)
226                                 Console.WriteLine (str);
227                 }
228
229                 static void WriteLine (string format, params object[] args)
230                 {
231                         if (!quiet)
232                                 Console.WriteLine (format, args);
233                 }
234
235                 static int Main (string[] args)
236                 {
237                         try {
238                                 if (!ParseOptions (args)) {
239                                         Header ();
240                                         Help ();
241                                         return 1;
242                                 }
243                                 if (!quiet) {
244                                         Header ();
245                                 }
246                                 return Process ();
247                         } catch (Exception e) {
248                                 // ignore quiet on exception
249                                 Console.WriteLine ("Error: {0}", e);
250                                 return 1;
251                         }
252                 }
253         }
254 }