2005-11-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / DiffieHellman.cs
1 //
2 // DiffieHellman.cs: Defines a base class from which all Diffie-Hellman implementations inherit
3 //
4 // Author:
5 //      Pieter Philippaerts (Pieter@mentalis.org)
6 //
7 // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Text;
33 using System.Security;
34 using System.Security.Cryptography;
35 using Mono.Xml;
36 using Mono.Math;
37
38 namespace Mono.Security.Cryptography {
39         /// <summary>
40         /// Defines a base class from which all Diffie-Hellman implementations inherit.
41         /// </summary>
42         public abstract class DiffieHellman : AsymmetricAlgorithm {
43                 /// <summary>
44                 /// Creates an instance of the default implementation of the <see cref="DiffieHellman"/> algorithm.
45                 /// </summary>
46                 /// <returns>A new instance of the default implementation of DiffieHellman.</returns>
47                 public static new DiffieHellman Create () {
48                         return Create ("Mono.Security.Cryptography.DiffieHellman");
49                 }
50                 /// <summary>
51                 /// Creates an instance of the specified implementation of <see cref="DiffieHellman"/>.
52                 /// </summary>
53                 /// <param name="algName">The name of the implementation of DiffieHellman to use.</param>
54                 /// <returns>A new instance of the specified implementation of DiffieHellman.</returns>
55                 public static new DiffieHellman Create (string algName) {
56                         return (DiffieHellman) CryptoConfig.CreateFromName (algName);
57                 }
58
59                 /// <summary>
60                 /// When overridden in a derived class, creates the key exchange data. 
61                 /// </summary>
62                 /// <returns>The key exchange data to be sent to the intended recipient.</returns>
63                 public abstract byte[] CreateKeyExchange();
64                 /// <summary>
65                 /// When overridden in a derived class, extracts secret information from the key exchange data.
66                 /// </summary>
67                 /// <param name="keyex">The key exchange data within which the secret information is hidden.</param>
68                 /// <returns>The secret information derived from the key exchange data.</returns>
69                 public abstract byte[] DecryptKeyExchange (byte[] keyex);
70
71                 /// <summary>
72                 /// When overridden in a derived class, exports the <see cref="DHParameters"/>.
73                 /// </summary>
74                 /// <param name="includePrivate"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
75                 /// <returns>The parameters for Diffie-Hellman.</returns>
76                 public abstract DHParameters ExportParameters (bool includePrivate);
77                 /// <summary>
78                 /// When overridden in a derived class, imports the specified <see cref="DHParameters"/>.
79                 /// </summary>
80                 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
81                 public abstract void ImportParameters (DHParameters parameters);
82
83                 private byte[] GetNamedParam(SecurityElement se, string param) {
84                         SecurityElement sep = se.SearchForChildByTag(param);
85                         if (sep == null)
86                                 return null;
87                         return Convert.FromBase64String(sep.Text);
88                 }
89                 /// <summary>
90                 /// Reconstructs a <see cref="DiffieHellman"/> object from an XML string.
91                 /// </summary>
92                 /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
93                 /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
94                 public override void FromXmlString (string xmlString) {
95                         if (xmlString == null)
96                                 throw new ArgumentNullException ("xmlString");
97
98                         DHParameters dhParams = new DHParameters();
99                         try {
100                                 SecurityParser sp = new SecurityParser();
101                                 sp.LoadXml(xmlString);
102                                 SecurityElement se = sp.ToXml();
103                                 if (se.Tag != "DHKeyValue")
104                                         throw new CryptographicException();
105                                 dhParams.P = GetNamedParam(se, "P");
106                                 dhParams.G = GetNamedParam(se, "G");
107                                 dhParams.X = GetNamedParam(se, "X");
108                                 ImportParameters(dhParams);
109                         } finally {
110                                 if (dhParams.P != null)
111                                         Array.Clear(dhParams.P, 0, dhParams.P.Length);
112                                 if (dhParams.G != null)
113                                         Array.Clear(dhParams.G, 0, dhParams.G.Length);
114                                 if (dhParams.X != null)
115                                         Array.Clear(dhParams.X, 0, dhParams.X.Length);
116                         }
117                 }
118                 /// <summary>
119                 /// Creates and returns an XML string representation of the current <see cref="DiffieHellman"/> object.
120                 /// </summary>
121                 /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
122                 /// <returns>An XML string encoding of the current DiffieHellman object.</returns>
123                 public override string ToXmlString (bool includePrivateParameters) {
124                         StringBuilder sb = new StringBuilder ();
125                         DHParameters dhParams = ExportParameters(includePrivateParameters);
126                         try {
127                                 sb.Append ("<DHKeyValue>");
128                                 
129                                 sb.Append ("<P>");
130                                 sb.Append (Convert.ToBase64String (dhParams.P));
131                                 sb.Append ("</P>");
132
133                                 sb.Append ("<G>");
134                                 sb.Append (Convert.ToBase64String (dhParams.G));
135                                 sb.Append ("</G>");
136
137                                 if (includePrivateParameters) {
138                                         sb.Append ("<X>");
139                                         sb.Append (Convert.ToBase64String (dhParams.X));
140                                         sb.Append ("</X>");
141                                 }
142                                 
143                                 sb.Append ("</DHKeyValue>");
144                         } finally {
145                                 Array.Clear(dhParams.P, 0, dhParams.P.Length);
146                                 Array.Clear(dhParams.G, 0, dhParams.G.Length);
147                                 if (dhParams.X != null)
148                                         Array.Clear(dhParams.X, 0, dhParams.X.Length);
149                         }
150                         return sb.ToString ();
151                 }
152         }
153 }