* FileSystemInfo.cs: corrected COM visibility of UTC properties
[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 using System;
11 using System.Text;
12 using System.Security;
13 using System.Security.Cryptography;
14 using Mono.Xml;
15 using Mono.Math;
16
17 namespace Mono.Security.Cryptography {
18         /// <summary>
19         /// Defines a base class from which all Diffie-Hellman implementations inherit.
20         /// </summary>
21         public abstract class DiffieHellman : AsymmetricAlgorithm {
22                 /// <summary>
23                 /// Creates an instance of the default implementation of the <see cref="DiffieHellman"/> algorithm.
24                 /// </summary>
25                 /// <returns>A new instance of the default implementation of DiffieHellman.</returns>
26                 public static new DiffieHellman Create () {
27                         return Create ("Mono.Security.Cryptography.DiffieHellman");
28                 }
29                 /// <summary>
30                 /// Creates an instance of the specified implementation of <see cref="DiffieHellman"/>.
31                 /// </summary>
32                 /// <param name="algName">The name of the implementation of DiffieHellman to use.</param>
33                 /// <returns>A new instance of the specified implementation of DiffieHellman.</returns>
34                 public static new DiffieHellman Create (string algName) {
35                         return (DiffieHellman) CryptoConfig.CreateFromName (algName);
36                 }
37
38                 /// <summary>
39                 /// Initializes a new <see cref="DiffieHellman"/> instance.
40                 /// </summary>
41                 public DiffieHellman() {}
42
43                 /// <summary>
44                 /// When overridden in a derived class, creates the key exchange data. 
45                 /// </summary>
46                 /// <returns>The key exchange data to be sent to the intended recipient.</returns>
47                 public abstract byte[] CreateKeyExchange();
48                 /// <summary>
49                 /// When overridden in a derived class, extracts secret information from the key exchange data.
50                 /// </summary>
51                 /// <param name="keyEx">The key exchange data within which the secret information is hidden.</param>
52                 /// <returns>The secret information derived from the key exchange data.</returns>
53                 public abstract byte[] DecryptKeyExchange(byte[] keyEx);
54
55                 /// <summary>
56                 /// When overridden in a derived class, exports the <see cref="DHParameters"/>.
57                 /// </summary>
58                 /// <param name="includePrivate"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
59                 /// <returns>The parameters for Diffie-Hellman.</returns>
60                 public abstract DHParameters ExportParameters (bool includePrivate);
61                 /// <summary>
62                 /// When overridden in a derived class, imports the specified <see cref="DHParameters"/>.
63                 /// </summary>
64                 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
65                 public abstract void ImportParameters (DHParameters parameters);
66
67                 private byte[] GetNamedParam(SecurityElement se, string param) {
68                         SecurityElement sep = se.SearchForChildByTag(param);
69                         if (sep == null)
70                                 return null;
71                         return Convert.FromBase64String(sep.Text);
72                 }
73                 /// <summary>
74                 /// Reconstructs a <see cref="DiffieHellman"/> object from an XML string.
75                 /// </summary>
76                 /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
77                 /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
78                 public override void FromXmlString (string xmlString) {
79                         if (xmlString == null)
80                                 throw new ArgumentNullException ("xmlString");
81
82                         DHParameters dhParams = new DHParameters();
83                         try {
84                                 SecurityParser sp = new SecurityParser();
85                                 sp.LoadXml(xmlString);
86                                 SecurityElement se = sp.ToXml();
87                                 if (se.Tag != "DHKeyValue")
88                                         throw new CryptographicException();
89                                 dhParams.P = GetNamedParam(se, "P");
90                                 dhParams.G = GetNamedParam(se, "G");
91                                 dhParams.X = GetNamedParam(se, "X");
92                                 ImportParameters(dhParams);
93                         } finally {
94                                 if (dhParams.P != null)
95                                         Array.Clear(dhParams.P, 0, dhParams.P.Length);
96                                 if (dhParams.G != null)
97                                         Array.Clear(dhParams.G, 0, dhParams.G.Length);
98                                 if (dhParams.X != null)
99                                         Array.Clear(dhParams.X, 0, dhParams.X.Length);
100                         }
101                 }
102                 /// <summary>
103                 /// Creates and returns an XML string representation of the current <see cref="DiffieHellman"/> object.
104                 /// </summary>
105                 /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
106                 /// <returns>An XML string encoding of the current DiffieHellman object.</returns>
107                 public override string ToXmlString (bool includePrivateParameters) {
108                         StringBuilder sb = new StringBuilder ();
109                         DHParameters dhParams = ExportParameters(includePrivateParameters);
110                         try {
111                                 sb.Append ("<DHKeyValue>");
112                                 
113                                 sb.Append ("<P>");
114                                 sb.Append (Convert.ToBase64String (dhParams.P));
115                                 sb.Append ("</P>");
116
117                                 sb.Append ("<G>");
118                                 sb.Append (Convert.ToBase64String (dhParams.G));
119                                 sb.Append ("</G>");
120
121                                 if (includePrivateParameters) {
122                                         sb.Append ("<X>");
123                                         sb.Append (Convert.ToBase64String (dhParams.X));
124                                         sb.Append ("</X>");
125                                 }
126                                 
127                                 sb.Append ("</DHKeyValue>");
128                         } finally {
129                                 Array.Clear(dhParams.P, 0, dhParams.P.Length);
130                                 Array.Clear(dhParams.G, 0, dhParams.G.Length);
131                                 if (dhParams.X != null)
132                                         Array.Clear(dhParams.X, 0, dhParams.X.Length);
133                         }
134                         return sb.ToString ();
135                 }
136         }
137 }