[mono-config] use right type for result of strlen
[mono.git] / mcs / tools / security / cert-sync.cs
1 //
2 // cert-sync.cs: Import the root certificates from Linux SSL 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 ("Linux Cert Store Sync")]
42 [assembly: AssemblyDescription ("Synchronize local certs with certs from local Linux trust store.")]
43
44 namespace Mono.Tools
45 {
46
47         class CertSync
48         {
49         
50                 static string inputFile;
51                 static bool quiet;
52
53                 static X509Certificate DecodeCertificate (string s)
54                 {
55                         byte[] rawdata = Convert.FromBase64String (s);
56                         return new X509Certificate (rawdata);
57                 }
58
59                 static Stream GetFile ()
60                 {
61                         try {
62                                 if (inputFile != null) {
63                                         return File.OpenRead (inputFile);
64                                 } else {
65                                         return null;
66                                 }
67                         } catch {
68                                 return null;
69                         }
70                 }
71
72                 static X509CertificateCollection DecodeCollection ()
73                 {
74                         X509CertificateCollection roots = new X509CertificateCollection ();
75                         StringBuilder sb = new StringBuilder ();
76                         bool processing = false;
77
78                         using (Stream s = GetFile ()) {
79                                 if (s == null) {
80                                         WriteLine ("Couldn't retrieve the file using the supplied information.");
81                                         return null;
82                                 }
83
84                                 StreamReader sr = new StreamReader (s);
85                                 while (true) {
86                                         string line = sr.ReadLine ();
87                                         if (line == null)
88                                                 break;
89
90                                         if (processing) {
91                                                 if (line.StartsWith ("-----END CERTIFICATE-----")) {
92                                                         processing = false;
93                                                         X509Certificate root = DecodeCertificate (sb.ToString ());
94                                                         roots.Add (root);
95
96                                                         sb = new StringBuilder ();
97                                                         continue;
98                                                 }
99                                                 sb.Append (line);
100                                         } else {
101                                                 processing = line.StartsWith ("-----BEGIN CERTIFICATE-----");
102                                         }
103                                 }
104                                 return roots;
105                         }
106                 }
107
108                 static int Process ()
109                 {
110                         X509CertificateCollection roots = DecodeCollection ();
111                         if (roots == null) {
112                                 return 1;
113                         } else if (roots.Count == 0) {
114                                 WriteLine ("No certificates were found.");
115                                 return 0;
116                         }
117                                 
118                         X509Stores stores = (X509StoreManager.LocalMachine);
119                         X509CertificateCollection trusted = stores.TrustedRoot.Certificates;
120                         int additions = 0;
121                         WriteLine ("I already trust {0}, your new list has {1}", trusted.Count, roots.Count);
122                         foreach (X509Certificate root in roots) {
123                                 if (!trusted.Contains (root)) {
124                                         try {
125                                                 stores.TrustedRoot.Import (root);
126                                                 WriteLine ("Certificate added: {0}", root.SubjectName);
127                                         } catch {
128                                                 WriteLine ("Warning: Could not import {0}");
129                                         }
130                                         additions++;
131                                 }
132                         }
133                         if (additions > 0)
134                                 WriteLine ("{0} new root certificates were added to your trust store.", additions);
135
136                         X509CertificateCollection removed = new X509CertificateCollection ();
137                         foreach (X509Certificate trust in trusted) {
138                                 if (!roots.Contains (trust)) {
139                                         removed.Add (trust);
140                                 }
141                         }
142                         if (removed.Count > 0) {
143                                 WriteLine ("{0} previously trusted certificates were removed.", removed.Count);
144
145                                 foreach (X509Certificate old in removed) {
146                                         stores.TrustedRoot.Remove (old);
147                                         WriteLine ("Certificate removed: {0}", old.SubjectName);
148                                 }
149                         }
150                         WriteLine ("Import process completed.");
151                         return 0;
152                 }
153
154                 static string Thumbprint (string algorithm, X509Certificate certificate)
155                 {
156                         HashAlgorithm hash = HashAlgorithm.Create (algorithm);
157                         byte[] digest = hash.ComputeHash (certificate.RawData);
158                         return BitConverter.ToString (digest);
159                 }
160
161                 static bool ParseOptions (string[] args)
162                 {
163                         if (args.Length < 1)
164                                 return false;
165
166                         for (int i = 0; i < args.Length - 1; i++) {
167                                 switch (args [i]) {
168                                 case "--quiet":
169                                         quiet = true;
170                                         break;
171                                 default:
172                                         WriteLine ("Unknown option '{0}'.");
173                                         return false;
174                                 }
175                         }
176                         inputFile = args [args.Length - 1];
177                         if (!File.Exists (inputFile)) {
178                                 WriteLine ("Unknown option or file not found '{0}'.");
179                                 return false;
180                         }
181                         return true;
182                 }
183
184                 static void Header ()
185                 {
186                         Console.WriteLine (new AssemblyInfo ().ToString ());
187                 }
188
189                 static void Help ()
190                 {
191                         Console.WriteLine ("Usage: cert-sync [--quiet] system-ca-bundle.crt");
192                         Console.WriteLine ("Where system-ca-bundle.crt is in PEM format");
193                 }
194
195                 static void WriteLine (string str)
196                 {
197                         if (!quiet)
198                                 Console.WriteLine (str);
199                 }
200
201                 static void WriteLine (string format, params object[] args)
202                 {
203                         if (!quiet)
204                                 Console.WriteLine (format, args);
205                 }
206
207                 static int Main (string[] args)
208                 {
209                         try {
210                                 if (!ParseOptions (args)) {
211                                         Header ();
212                                         Help ();
213                                         return 1;
214                                 }
215                                 if (!quiet) {
216                                         Header ();
217                                 }
218                                 return Process ();
219                         } catch (Exception e) {
220                                 // ignore quiet on exception
221                                 Console.WriteLine ("Error: {0}", e);
222                                 return 1;
223                         }
224                 }
225         }
226 }