summary refs log tree commit diff
path: root/crypto/src/x509/store/X509StoreFactory.cs
blob: 96f22be3fb94e78ab376800a7ec9c19ea49ac8d3 (plain) (blame)
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
62
using System;
using System.Collections;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.X509.Store
{
	public sealed class X509StoreFactory
	{
		private X509StoreFactory()
		{
		}

		public static IX509Store Create(
			string					type,
			IX509StoreParameters	parameters)
		{
			if (type == null)
				throw new ArgumentNullException("type");

			string[] parts = Platform.ToUpperInvariant(type).Split('/');

            if (parts.Length < 2)
				throw new ArgumentException("type");

			if (parts[1] != "COLLECTION")
				throw new NoSuchStoreException("X.509 store type '" + type + "' not available.");

			X509CollectionStoreParameters p = (X509CollectionStoreParameters) parameters;
			ICollection coll = p.GetCollection();

			switch (parts[0])
			{
				case "ATTRIBUTECERTIFICATE":
					checkCorrectType(coll, typeof(IX509AttributeCertificate));
					break;
				case "CERTIFICATE":
					checkCorrectType(coll, typeof(X509Certificate));
					break;
				case "CERTIFICATEPAIR":
					checkCorrectType(coll, typeof(X509CertificatePair));
					break;
				case "CRL":
					checkCorrectType(coll, typeof(X509Crl));
					break;
				default:
					throw new NoSuchStoreException("X.509 store type '" + type + "' not available.");
			}

			return new X509CollectionStore(coll);
		}

		private static void checkCorrectType(ICollection coll, Type t)
		{
			foreach (object o in coll)
			{
				if (!t.IsInstanceOfType(o))
					throw new InvalidCastException("Can't cast object to type: " + t.FullName);
			}
		}
	}
}