Merge pull request #3676 from chamons/SignalHandlerAPI_XM45
[mono.git] / mcs / tools / btls / btls-cert-sync.cs
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Security.Cryptography.X509Certificates;
5 using Mono.Btls;
6
7 namespace Mono.Btls
8 {
9         static class BtlsCertSync
10         {
11                 static void Main (string[] args)
12                 {
13                         if (!MonoBtlsProvider.IsSupported ()) {
14                                 Console.Error.WriteLine ("BTLS is not supported in this runtime!");
15                                 Environment.Exit (255);
16                         }
17
18                         var configPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
19                         configPath = Path.Combine (configPath, ".mono");
20
21                         var oldStorePath = Path.Combine (configPath, "certs", "Trust");
22                         var newStorePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
23
24                         if (!Directory.Exists (oldStorePath)) {
25                                 Console.WriteLine ("Old trust store {0} does not exist.");
26                                 Environment.Exit (255);
27                         }
28
29                         if (Directory.Exists (newStorePath))
30                                 Directory.Delete (newStorePath, true);
31                         Directory.CreateDirectory (newStorePath);
32
33                         var oldfiles = Directory.GetFiles (oldStorePath, "*.cer");
34                         Console.WriteLine ("Found {0} files in the old store.", oldfiles.Length);
35
36                         foreach (var file in oldfiles) {
37                                 Console.WriteLine ("Converting {0}.", file);
38                                 var data = File.ReadAllBytes (file);
39                                 using (var x509 = MonoBtlsX509.LoadFromData (data, MonoBtlsX509Format.DER)) {
40                                         ConvertToNewFormat (newStorePath, x509);
41                                 }
42                         }
43                 }
44
45                 static void ConvertToNewFormat (string root, MonoBtlsX509 x509)
46                 {
47                         long hash = x509.GetSubjectNameHash ();
48
49                         string newName;
50                         int index = 0;
51                         do {
52                                 newName = Path.Combine (root, string.Format ("{0:x8}.{1}", hash, index++));
53                         } while (File.Exists (newName));
54                         Console.WriteLine ("  new name: {0}", newName);
55
56                         using (var stream = new FileStream (newName, FileMode.Create))
57                         using (var bio = MonoBtlsBio.CreateMonoStream (stream))
58                                 x509.ExportAsPEM (bio, true);
59                 }
60         }
61 }