4 using System.Security.Cryptography.X509Certificates;
9 static class BtlsCertSync
11 static void Main (string[] args)
13 if (!MonoBtlsProvider.IsSupported ()) {
14 Console.Error.WriteLine ("BTLS is not supported in this runtime!");
15 Environment.Exit (255);
18 var configPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
19 configPath = Path.Combine (configPath, ".mono");
21 var oldStorePath = Path.Combine (configPath, "certs", "Trust");
22 var newStorePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
24 if (!Directory.Exists (oldStorePath)) {
25 Console.WriteLine ("Old trust store {0} does not exist.");
26 Environment.Exit (255);
29 if (Directory.Exists (newStorePath))
30 Directory.Delete (newStorePath, true);
31 Directory.CreateDirectory (newStorePath);
33 var oldfiles = Directory.GetFiles (oldStorePath, "*.cer");
34 Console.WriteLine ("Found {0} files in the old store.", oldfiles.Length);
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);
45 static void ConvertToNewFormat (string root, MonoBtlsX509 x509)
47 long hash = x509.GetSubjectNameHash ();
52 newName = Path.Combine (root, string.Format ("{0:x8}.{1}", hash, index++));
53 } while (File.Exists (newName));
54 Console.WriteLine (" new name: {0}", newName);
56 using (var stream = new FileStream (newName, FileMode.Create))
57 using (var bio = MonoBtlsBio.CreateMonoStream (stream))
58 x509.ExportAsPEM (bio, true);