Merge pull request #498 from Unroll-Me/master
[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 #if FULL_AOT_RUNTIME
46                         return new System.Security.Cryptography.RSACryptoServiceProvider ();
47 #else
48                         return Create ("System.Security.Cryptography.RSA");
49 #endif
50                 }
51
52                 public static new RSA Create (string algName)
53                 {
54                         return (RSA) CryptoConfig.CreateFromName (algName);
55                 }
56
57                 protected RSA ()
58                 {
59                 }
60
61                 public abstract byte[] EncryptValue (byte[] rgb);
62
63                 public abstract byte[] DecryptValue (byte[] rgb);
64
65                 public abstract RSAParameters ExportParameters (bool includePrivateParameters);
66
67                 public abstract void ImportParameters (RSAParameters parameters);
68
69                 internal void ZeroizePrivateKey (RSAParameters parameters)
70                 {
71                         if (parameters.P != null)
72                                 Array.Clear (parameters.P, 0, parameters.P.Length);
73                         if (parameters.Q != null)
74                                 Array.Clear (parameters.Q, 0, parameters.Q.Length);
75                         if (parameters.DP != null)
76                                 Array.Clear (parameters.DP, 0, parameters.DP.Length);
77                         if (parameters.DQ != null)
78                                 Array.Clear (parameters.DQ, 0, parameters.DQ.Length);
79                         if (parameters.InverseQ != null)
80                                 Array.Clear (parameters.InverseQ, 0, parameters.InverseQ.Length);
81                         if (parameters.D != null)
82                                 Array.Clear (parameters.D, 0, parameters.D.Length);
83                 }
84
85                 public override void FromXmlString (string xmlString) 
86                 {
87                         if (xmlString == null)
88                                 throw new ArgumentNullException ("xmlString");
89
90                         RSAParameters rsaParams = new RSAParameters ();
91                         try {
92                                 rsaParams.P = GetNamedParam (xmlString, "P");
93                                 rsaParams.Q = GetNamedParam (xmlString, "Q");
94                                 rsaParams.D = GetNamedParam (xmlString, "D");
95                                 rsaParams.DP = GetNamedParam (xmlString, "DP");
96                                 rsaParams.DQ = GetNamedParam (xmlString, "DQ");
97                                 rsaParams.InverseQ = GetNamedParam (xmlString, "InverseQ");
98                                 rsaParams.Exponent = GetNamedParam (xmlString, "Exponent");
99                                 rsaParams.Modulus = GetNamedParam (xmlString, "Modulus");
100                                 ImportParameters (rsaParams);
101                         }
102                         catch (Exception e) {
103                                 ZeroizePrivateKey (rsaParams);
104                                 throw new CryptographicException (
105                                         Locale.GetText ("Couldn't decode XML"), e);
106                         }
107                         finally {
108                                 ZeroizePrivateKey (rsaParams);
109                         }
110                 }
111
112                 public override string ToXmlString (bool includePrivateParameters) 
113                 {
114                         StringBuilder sb = new StringBuilder ();
115                         RSAParameters rsaParams = ExportParameters (includePrivateParameters);
116                         try {
117                                 sb.Append ("<RSAKeyValue>");
118                                 
119                                 sb.Append ("<Modulus>");
120                                 sb.Append (Convert.ToBase64String (rsaParams.Modulus));
121                                 sb.Append ("</Modulus>");
122
123                                 sb.Append ("<Exponent>");
124                                 sb.Append (Convert.ToBase64String (rsaParams.Exponent));
125                                 sb.Append ("</Exponent>");
126
127                                 if (includePrivateParameters) {
128                                         // we want an ArgumentNullException is only the D is missing, but a
129                                         // CryptographicException if other parameters (CRT) are missings
130                                         if (rsaParams.D == null) {
131                                                 string msg = Locale.GetText ("Missing D parameter for the private key.");
132                                                 throw new ArgumentNullException (msg);
133                                         } else if ((rsaParams.P == null) || (rsaParams.Q == null) || (rsaParams.DP == null) ||
134                                                 (rsaParams.DQ == null) || (rsaParams.InverseQ == null)) {
135                                                 // note: we can import a private key, using FromXmlString,
136                                                 // without the CRT parameters but we export it using ToXmlString!
137                                                 string msg = Locale.GetText ("Missing some CRT parameters for the private key.");
138                                                 throw new CryptographicException (msg);
139                                         }
140
141                                         sb.Append ("<P>");
142                                         sb.Append (Convert.ToBase64String (rsaParams.P));
143                                         sb.Append ("</P>");
144
145                                         sb.Append ("<Q>");
146                                         sb.Append (Convert.ToBase64String (rsaParams.Q));
147                                         sb.Append ("</Q>");
148
149                                         sb.Append ("<DP>");
150                                         sb.Append (Convert.ToBase64String (rsaParams.DP));
151                                         sb.Append ("</DP>");
152
153                                         sb.Append ("<DQ>");
154                                         sb.Append (Convert.ToBase64String (rsaParams.DQ));
155                                         sb.Append ("</DQ>");
156
157                                         sb.Append ("<InverseQ>");
158                                         sb.Append (Convert.ToBase64String (rsaParams.InverseQ));
159                                         sb.Append ("</InverseQ>");
160
161                                         sb.Append ("<D>");
162                                         sb.Append (Convert.ToBase64String (rsaParams.D));
163                                         sb.Append ("</D>");
164                                 }
165                                 
166                                 sb.Append ("</RSAKeyValue>");
167                         }
168                         catch {
169                                 ZeroizePrivateKey (rsaParams);
170                                 throw;
171                         }
172                         
173                         return sb.ToString ();
174                 }
175         }
176 }