New test.
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / PublicKey.cs
1 //
2 // PublicKey.cs - System.Security.Cryptography.PublicKey
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) 2005 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 #if NET_2_0 && SECURITY_DEP
33
34 using Mono.Security;
35 using Mono.Security.Cryptography;
36 using MSX = Mono.Security.X509;
37
38 namespace System.Security.Cryptography.X509Certificates {
39
40         public sealed class PublicKey {
41
42                 private const string rsaOid = "1.2.840.113549.1.1.1";
43                 private const string dsaOid = "1.2.840.10040.4.1";
44
45                 private AsymmetricAlgorithm _key;
46                 private AsnEncodedData _keyValue;
47                 private AsnEncodedData _params;
48                 private Oid _oid;
49
50                 public PublicKey (Oid oid, AsnEncodedData parameters, AsnEncodedData keyValue)
51                 {
52                         if (oid == null)
53                                 throw new ArgumentNullException ("oid");
54                         if (parameters == null)
55                                 throw new ArgumentNullException ("parameters");
56                         if (keyValue == null)
57                                 throw new ArgumentNullException ("keyValue");
58
59                         _oid = new Oid (oid);
60                         _params = new AsnEncodedData (parameters);
61                         _keyValue = new AsnEncodedData (keyValue);
62                 }
63
64                 internal PublicKey (MSX.X509Certificate certificate)
65                 {
66                         // note: _key MUSTonly contains the public part of the key
67                         bool export_required = true;
68
69                         if (certificate.KeyAlgorithm == rsaOid) {
70                                 // shortcut export/import in the case the private key isn't available
71                                 RSACryptoServiceProvider rcsp = (certificate.RSA as RSACryptoServiceProvider);
72                                 if ((rcsp != null) && rcsp.PublicOnly) {
73                                         _key = certificate.RSA;
74                                         export_required = false;
75                                 } else {
76                                         RSAManaged rsam = (certificate.RSA as RSAManaged);
77                                         if ((rsam != null) && rsam.PublicOnly) {
78                                                 _key = certificate.RSA;
79                                                 export_required = false;
80                                         }
81                                 }
82
83                                 if (export_required) {
84                                         RSAParameters rsap = certificate.RSA.ExportParameters (false);
85                                         _key = RSA.Create ();
86                                         (_key as RSA).ImportParameters (rsap);
87                                 }
88                         } else {
89                                 // shortcut export/import in the case the private key isn't available
90                                 DSACryptoServiceProvider dcsp = (certificate.DSA as DSACryptoServiceProvider);
91                                 if ((dcsp != null) && dcsp.PublicOnly) {
92                                         _key = certificate.DSA;
93                                         export_required = false;
94                                 }
95                                 // note: DSAManaged isn't availablt in Mono.Security due to a bug in Fx 1.x
96
97                                 if (export_required) {
98                                         DSAParameters rsap = certificate.DSA.ExportParameters (false);
99                                         _key = DSA.Create ();
100                                         (_key as DSA).ImportParameters (rsap);
101                                 }
102                         }
103
104                         _oid = new Oid (certificate.KeyAlgorithm);
105                         _keyValue = new AsnEncodedData (_oid, certificate.PublicKey);
106                         _params = new AsnEncodedData (_oid, certificate.KeyAlgorithmParameters);
107                 }
108
109                 // properties
110
111                 public AsnEncodedData EncodedKeyValue {
112                         get { return _keyValue; }
113                 }
114
115                 public AsnEncodedData EncodedParameters {
116                         get { return _params; }
117                 }
118
119                 public AsymmetricAlgorithm Key {
120                         get {
121                                 if (_key == null) {
122                                         switch (_oid.Value) {
123                                         case rsaOid:
124                                                 _key = DecodeRSA (_keyValue.RawData);
125                                                 break;
126                                         case dsaOid:
127                                                 _key = DecodeDSA (_keyValue.RawData, _params.RawData);
128                                                 break;
129                                         default:
130                                                 string msg = Locale.GetText ("Cannot decode public key from unknown OID '{0}'.", _oid.Value);
131                                                 throw new NotSupportedException (msg);
132                                         }
133                                 }
134                                 return _key;
135                         }
136                 }
137
138                 public Oid Oid {
139                         get { return _oid; }
140                 }
141
142                 // private stuff
143
144                 static private byte[] GetUnsignedBigInteger (byte[] integer) 
145                 {
146                         if (integer [0] != 0x00)
147                                 return integer;
148
149                         // this first byte is added so we're sure it's an unsigned integer
150                         // however we can't feed it into RSAParameters or DSAParameters
151                         int length = integer.Length - 1;
152                         byte[] uinteger = new byte [length];
153                         Buffer.BlockCopy (integer, 1, uinteger, 0, length);
154                         return uinteger;
155                 }
156
157                 static internal DSA DecodeDSA (byte[] rawPublicKey, byte[] rawParameters)
158                 {
159                         DSAParameters dsaParams = new DSAParameters ();
160                         try {
161                                 // for DSA rawPublicKey contains 1 ASN.1 integer - Y
162                                 ASN1 pubkey = new ASN1 (rawPublicKey);
163                                 if (pubkey.Tag != 0x02)
164                                         throw new CryptographicException (Locale.GetText ("Missing DSA Y integer."));
165                                 dsaParams.Y = GetUnsignedBigInteger (pubkey.Value);
166
167                                 ASN1 param = new ASN1 (rawParameters);
168                                 if ((param == null) || (param.Tag != 0x30) || (param.Count < 3))
169                                         throw new CryptographicException (Locale.GetText ("Missing DSA parameters."));
170                                 if ((param [0].Tag != 0x02) || (param [1].Tag != 0x02) || (param [2].Tag != 0x02))
171                                         throw new CryptographicException (Locale.GetText ("Invalid DSA parameters."));
172
173                                 dsaParams.P = GetUnsignedBigInteger (param [0].Value);
174                                 dsaParams.Q = GetUnsignedBigInteger (param [1].Value);
175                                 dsaParams.G = GetUnsignedBigInteger (param [2].Value);
176                         }
177                         catch (Exception e) {
178                                 string msg = Locale.GetText ("Error decoding the ASN.1 structure.");
179                                 throw new CryptographicException (msg, e);
180                         }
181
182                         DSA dsa = (DSA) new DSACryptoServiceProvider (dsaParams.Y.Length << 3);
183                         dsa.ImportParameters (dsaParams);
184                         return dsa;
185                 }
186
187                 static internal RSA DecodeRSA (byte[] rawPublicKey)
188                 {
189                         RSAParameters rsaParams = new RSAParameters ();
190                         try {
191                                 // for RSA rawPublicKey contains 2 ASN.1 integers
192                                 // the modulus and the public exponent
193                                 ASN1 pubkey = new ASN1 (rawPublicKey);
194                                 if (pubkey.Count == 0)
195                                         throw new CryptographicException (Locale.GetText ("Missing RSA modulus and exponent."));
196                                 ASN1 modulus = pubkey [0];
197                                 if ((modulus == null) || (modulus.Tag != 0x02))
198                                         throw new CryptographicException (Locale.GetText ("Missing RSA modulus."));
199                                 ASN1 exponent = pubkey [1];
200                                 if (exponent.Tag != 0x02)
201                                         throw new CryptographicException (Locale.GetText ("Missing RSA public exponent."));
202
203                                 rsaParams.Modulus = GetUnsignedBigInteger (modulus.Value);
204                                 rsaParams.Exponent = exponent.Value;
205                         }
206                         catch (Exception e) {
207                                 string msg = Locale.GetText ("Error decoding the ASN.1 structure.");
208                                 throw new CryptographicException (msg, e);
209                         }
210
211                         int keySize = (rsaParams.Modulus.Length << 3);
212                         RSA rsa = (RSA) new RSACryptoServiceProvider (keySize);
213                         rsa.ImportParameters (rsaParams);
214                         return rsa;
215                 }
216         }
217 }
218
219 #endif