Welcome to mirror list, hosted at ThFree Co, Russian Federation.

btls-cert-sync.cs « btls « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fcdc708e0ab56fb5cba3f56b43bc2912e8d9ee54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using MNS = Mono.Net.Security;

namespace Mono.Btls
{
	static class BtlsCertSync
	{
		static void Main (string[] args)
		{
			if (!MNS.MonoTlsProviderFactory.IsBtlsSupported ()) {
				Console.Error.WriteLine ("BTLS is not supported in this runtime!");
				Environment.Exit (255);
			}

			var configPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
			configPath = Path.Combine (configPath, ".mono");

			var oldStorePath = Path.Combine (configPath, "certs", "Trust");
			var newStorePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);

			if (!Directory.Exists (oldStorePath)) {
				Console.WriteLine ("Old trust store {0} does not exist.");
				Environment.Exit (255);
			}

			if (Directory.Exists (newStorePath))
				Directory.Delete (newStorePath, true);
			Directory.CreateDirectory (newStorePath);

			var oldfiles = Directory.GetFiles (oldStorePath, "*.cer");
			Console.WriteLine ("Found {0} files in the old store.", oldfiles.Length);

			foreach (var file in oldfiles) {
				Console.WriteLine ("Converting {0}.", file);
				var data = File.ReadAllBytes (file);
				using (var x509 = MonoBtlsX509.LoadFromData (data, MonoBtlsX509Format.DER)) {
					ConvertToNewFormat (newStorePath, x509);
				}
			}
		}

		static void ConvertToNewFormat (string root, MonoBtlsX509 x509)
		{
			long hash = x509.GetSubjectNameHash ();

			string newName;
			int index = 0;
			do {
				newName = Path.Combine (root, string.Format ("{0:x8}.{1}", hash, index++));
			} while (File.Exists (newName));
			Console.WriteLine ("  new name: {0}", newName);

			using (var stream = new FileStream (newName, FileMode.Create))
			using (var bio = MonoBtlsBio.CreateMonoStream (stream))
                                x509.ExportAsPEM (bio, true);
		}
	}
}