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