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

AsymmetricAlgorithm.cs « System.Security.Cryptography « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb2986470dc27ac2526c5d157d1a76e21dd4beb7 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// System.Security.Cryptography.AsymmetricAlgorithm Class implementation
//
// Authors:
//	Thomas Neidhart (tome@sbox.tugraz.at)
//	Sebastien Pouliot (spouliot@motus.com)
//
// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//

using System;

namespace System.Security.Cryptography {

	// to import keypairs parameters using MiniParser
	internal class AsymmetricParameters : MiniParser.IReader {
		private string xml;
		private int pos;

		public AsymmetricParameters (string xml) 
		{
			this.xml = xml;
			pos = 0;
		}

		public int Read () 
		{
			try {
				return (int) xml [pos++];
			}
			catch {
				return -1;
			}
		}
	}

	/// <summary>
	/// Abstract base class for all cryptographic asymmetric algorithms.
	/// Available algorithms include:
	/// RSA, DSA
	/// </summary>
	public abstract class AsymmetricAlgorithm : IDisposable	{

		protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits. 
		protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm. 

		/// <summary>
		/// Called from constructor of derived class.
		/// </summary>
		protected AsymmetricAlgorithm () {}
		
		/// <summary>
		/// Gets the key exchange algorithm
		/// </summary>
		public abstract string KeyExchangeAlgorithm {get;}
		
		/// <summary>
		/// Gets or sets the actual key size
		/// </summary>
		public virtual int KeySize {
			get { return this.KeySizeValue; }
			set {
				if (!IsLegalKeySize (this.LegalKeySizesValue, value))
					throw new CryptographicException("key size not supported by algorithm");
				
				this.KeySizeValue = value;
			}
		}

		/// <summary>
		/// Gets all legal key sizes
		/// </summary>
		public virtual KeySizes[] LegalKeySizes {
			get { return this.LegalKeySizesValue; }
		}

		/// <summary>
		/// Gets the signature algorithm
		/// </summary>
		public abstract string SignatureAlgorithm {get;}

		void IDisposable.Dispose () 
		{
			Dispose (true);
			GC.SuppressFinalize (this);  // Finalization is now unnecessary
		}

		public void Clear () 
		{
			Dispose (false);
		}

		protected abstract void Dispose (bool disposing);

		/// <summary>
		/// Reconstructs the AsymmetricAlgorithm Object from an XML-string
		/// </summary>
		public abstract void FromXmlString (string xmlString);
		
		/// <summary>
		/// Returns an XML string representation the current AsymmetricAlgorithm object
		/// </summary>
		public abstract string ToXmlString (bool includePrivateParameters);		
		
		private bool IsLegalKeySize (KeySizes[] LegalKeys, int Size) 
		{
			foreach (KeySizes LegalKeySize in LegalKeys) {
				for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
					if (i == Size)
						return true;
				}
			}
			return false;
		}
		
		/// <summary>
		/// Creates the default implementation of the default asymmetric algorithm (RSA).
		/// </summary>
		public static AsymmetricAlgorithm Create () 
		{
			return Create ("System.Security.Cryptography.AsymmetricAlgorithm");
		}
	
		/// <summary>
		/// Creates a specific implementation of the given asymmetric algorithm.
		/// </summary>
		/// <param name="algo">Specifies which derived class to create</param>
		public static AsymmetricAlgorithm Create (string algName) 
		{
			return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
		}
	}
}