do not check order sequence if option /order was not used
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RSA.cs
1 //
2 // System.Security.Cryptography.RSA.cs
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) 2002
9 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2006,2008 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Text;
35
36 using Mono.Xml;
37
38 namespace System.Security.Cryptography {
39
40         [ComVisible (true)]
41         public abstract class RSA : AsymmetricAlgorithm {
42
43                 public static new RSA Create () 
44                 {
45                         return Create ("System.Security.Cryptography.RSA");
46                 }
47
48                 public static new RSA Create (string algName)
49                 {
50                         return (RSA) CryptoConfig.CreateFromName (algName);
51                 }
52
53                 protected RSA ()
54                 {
55                 }
56
57                 public abstract byte[] EncryptValue (byte[] rgb);
58
59                 public abstract byte[] DecryptValue (byte[] rgb);
60
61                 public abstract RSAParameters ExportParameters (bool includePrivateParameters);
62
63                 public abstract void ImportParameters (RSAParameters parameters);
64
65                 internal void ZeroizePrivateKey (RSAParameters parameters)
66                 {
67                         if (parameters.P != null)
68                                 Array.Clear (parameters.P, 0, parameters.P.Length);
69                         if (parameters.Q != null)
70                                 Array.Clear (parameters.Q, 0, parameters.Q.Length);
71                         if (parameters.DP != null)
72                                 Array.Clear (parameters.DP, 0, parameters.DP.Length);
73                         if (parameters.DQ != null)
74                                 Array.Clear (parameters.DQ, 0, parameters.DQ.Length);
75                         if (parameters.InverseQ != null)
76                                 Array.Clear (parameters.InverseQ, 0, parameters.InverseQ.Length);
77                         if (parameters.D != null)
78                                 Array.Clear (parameters.D, 0, parameters.D.Length);
79                 }
80
81                 public override void FromXmlString (string xmlString) 
82                 {
83                         if (xmlString == null)
84                                 throw new ArgumentNullException ("xmlString");
85
86                         RSAParameters rsaParams = new RSAParameters ();
87                         try {
88                                 rsaParams.P = GetNamedParam (xmlString, "P");
89                                 rsaParams.Q = GetNamedParam (xmlString, "Q");
90                                 rsaParams.D = GetNamedParam (xmlString, "D");
91                                 rsaParams.DP = GetNamedParam (xmlString, "DP");
92                                 rsaParams.DQ = GetNamedParam (xmlString, "DQ");
93                                 rsaParams.InverseQ = GetNamedParam (xmlString, "InverseQ");
94                                 rsaParams.Exponent = GetNamedParam (xmlString, "Exponent");
95                                 rsaParams.Modulus = GetNamedParam (xmlString, "Modulus");
96                                 ImportParameters (rsaParams);
97                         }
98                         catch (Exception e) {
99                                 ZeroizePrivateKey (rsaParams);
100                                 throw new CryptographicException (
101                                         Locale.GetText ("Couldn't decode XML"), e);
102                         }
103                         finally {
104                                 ZeroizePrivateKey (rsaParams);
105                         }
106                 }
107
108                 public override string ToXmlString (bool includePrivateParameters) 
109                 {
110                         StringBuilder sb = new StringBuilder ();
111                         RSAParameters rsaParams = ExportParameters (includePrivateParameters);
112                         try {
113                                 sb.Append ("<RSAKeyValue>");
114                                 
115                                 sb.Append ("<Modulus>");
116                                 sb.Append (Convert.ToBase64String (rsaParams.Modulus));
117                                 sb.Append ("</Modulus>");
118
119                                 sb.Append ("<Exponent>");
120                                 sb.Append (Convert.ToBase64String (rsaParams.Exponent));
121                                 sb.Append ("</Exponent>");
122
123                                 if (includePrivateParameters) {
124                                         // we want an ArgumentNullException is only the D is missing, but a
125                                         // CryptographicException if other parameters (CRT) are missings
126                                         if (rsaParams.D == null) {
127                                                 string msg = Locale.GetText ("Missing D parameter for the private key.");
128                                                 throw new ArgumentNullException (msg);
129                                         } else if ((rsaParams.P == null) || (rsaParams.Q == null) || (rsaParams.DP == null) ||
130                                                 (rsaParams.DQ == null) || (rsaParams.InverseQ == null)) {
131                                                 // note: we can import a private key, using FromXmlString,
132                                                 // without the CRT parameters but we export it using ToXmlString!
133                                                 string msg = Locale.GetText ("Missing some CRT parameters for the private key.");
134                                                 throw new CryptographicException (msg);
135                                         }
136
137                                         sb.Append ("<P>");
138                                         sb.Append (Convert.ToBase64String (rsaParams.P));
139                                         sb.Append ("</P>");
140
141                                         sb.Append ("<Q>");
142                                         sb.Append (Convert.ToBase64String (rsaParams.Q));
143                                         sb.Append ("</Q>");
144
145                                         sb.Append ("<DP>");
146                                         sb.Append (Convert.ToBase64String (rsaParams.DP));
147                                         sb.Append ("</DP>");
148
149                                         sb.Append ("<DQ>");
150                                         sb.Append (Convert.ToBase64String (rsaParams.DQ));
151                                         sb.Append ("</DQ>");
152
153                                         sb.Append ("<InverseQ>");
154                                         sb.Append (Convert.ToBase64String (rsaParams.InverseQ));
155                                         sb.Append ("</InverseQ>");
156
157                                         sb.Append ("<D>");
158                                         sb.Append (Convert.ToBase64String (rsaParams.D));
159                                         sb.Append ("</D>");
160                                 }
161                                 
162                                 sb.Append ("</RSAKeyValue>");
163                         }
164                         catch {
165                                 ZeroizePrivateKey (rsaParams);
166                                 throw;
167                         }
168                         
169                         return sb.ToString ();
170                 }
171         }
172 }