2 // PublicKey.cs - System.Security.Cryptography.PublicKey
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Tim Coleman (tim@timcoleman.com)
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)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 extern alias MonoSecurity;
36 using MonoSecurity::Mono.Security;
37 using MonoSecurity::Mono.Security.Cryptography;
38 using MSX = MonoSecurity::Mono.Security.X509;
40 namespace System.Security.Cryptography.X509Certificates {
42 public sealed class PublicKey {
44 private const string rsaOid = "1.2.840.113549.1.1.1";
45 private const string dsaOid = "1.2.840.10040.4.1";
47 private AsymmetricAlgorithm _key;
48 private AsnEncodedData _keyValue;
49 private AsnEncodedData _params;
52 static byte[] Empty = new byte [0];
54 public PublicKey (Oid oid, AsnEncodedData parameters, AsnEncodedData keyValue)
57 throw new ArgumentNullException ("oid");
58 if (parameters == null)
59 throw new ArgumentNullException ("parameters");
61 throw new ArgumentNullException ("keyValue");
64 _params = new AsnEncodedData (parameters);
65 _keyValue = new AsnEncodedData (keyValue);
68 internal PublicKey (MSX.X509Certificate certificate)
70 // note: _key MUSTonly contains the public part of the key
71 bool export_required = true;
73 if (certificate.KeyAlgorithm == rsaOid) {
74 // shortcut export/import in the case the private key isn't available
75 RSACryptoServiceProvider rcsp = (certificate.RSA as RSACryptoServiceProvider);
76 if ((rcsp != null) && rcsp.PublicOnly) {
77 _key = certificate.RSA;
78 export_required = false;
81 RSAManaged rsam = (certificate.RSA as RSAManaged);
82 if ((rsam != null) && rsam.PublicOnly) {
83 _key = certificate.RSA;
84 export_required = false;
88 if (export_required) {
89 RSAParameters rsap = certificate.RSA.ExportParameters (false);
91 (_key as RSA).ImportParameters (rsap);
94 // shortcut export/import in the case the private key isn't available
95 DSACryptoServiceProvider dcsp = (certificate.DSA as DSACryptoServiceProvider);
96 if ((dcsp != null) && dcsp.PublicOnly) {
97 _key = certificate.DSA;
98 export_required = false;
100 // note: DSAManaged isn't available in Mono.Security due to a bug in Fx 1.x
102 if (export_required) {
103 DSAParameters rsap = certificate.DSA.ExportParameters (false);
104 _key = DSA.Create ();
105 (_key as DSA).ImportParameters (rsap);
109 _oid = new Oid (certificate.KeyAlgorithm);
110 _keyValue = new AsnEncodedData (_oid, certificate.PublicKey);
111 _params = new AsnEncodedData (_oid, certificate.KeyAlgorithmParameters ?? Empty);
116 public AsnEncodedData EncodedKeyValue {
117 get { return _keyValue; }
120 public AsnEncodedData EncodedParameters {
121 get { return _params; }
124 public AsymmetricAlgorithm Key {
127 switch (_oid.Value) {
129 _key = DecodeRSA (_keyValue.RawData);
132 _key = DecodeDSA (_keyValue.RawData, _params.RawData);
135 string msg = Locale.GetText ("Cannot decode public key from unknown OID '{0}'.", _oid.Value);
136 throw new NotSupportedException (msg);
149 static private byte[] GetUnsignedBigInteger (byte[] integer)
151 if (integer [0] != 0x00)
154 // this first byte is added so we're sure it's an unsigned integer
155 // however we can't feed it into RSAParameters or DSAParameters
156 int length = integer.Length - 1;
157 byte[] uinteger = new byte [length];
158 Buffer.BlockCopy (integer, 1, uinteger, 0, length);
162 static internal DSA DecodeDSA (byte[] rawPublicKey, byte[] rawParameters)
164 DSAParameters dsaParams = new DSAParameters ();
166 // for DSA rawPublicKey contains 1 ASN.1 integer - Y
167 ASN1 pubkey = new ASN1 (rawPublicKey);
168 if (pubkey.Tag != 0x02)
169 throw new CryptographicException (Locale.GetText ("Missing DSA Y integer."));
170 dsaParams.Y = GetUnsignedBigInteger (pubkey.Value);
172 ASN1 param = new ASN1 (rawParameters);
173 if ((param == null) || (param.Tag != 0x30) || (param.Count < 3))
174 throw new CryptographicException (Locale.GetText ("Missing DSA parameters."));
175 if ((param [0].Tag != 0x02) || (param [1].Tag != 0x02) || (param [2].Tag != 0x02))
176 throw new CryptographicException (Locale.GetText ("Invalid DSA parameters."));
178 dsaParams.P = GetUnsignedBigInteger (param [0].Value);
179 dsaParams.Q = GetUnsignedBigInteger (param [1].Value);
180 dsaParams.G = GetUnsignedBigInteger (param [2].Value);
182 catch (Exception e) {
183 string msg = Locale.GetText ("Error decoding the ASN.1 structure.");
184 throw new CryptographicException (msg, e);
187 DSA dsa = (DSA) new DSACryptoServiceProvider (dsaParams.Y.Length << 3);
188 dsa.ImportParameters (dsaParams);
192 static internal RSA DecodeRSA (byte[] rawPublicKey)
194 RSAParameters rsaParams = new RSAParameters ();
196 // for RSA rawPublicKey contains 2 ASN.1 integers
197 // the modulus and the public exponent
198 ASN1 pubkey = new ASN1 (rawPublicKey);
199 if (pubkey.Count == 0)
200 throw new CryptographicException (Locale.GetText ("Missing RSA modulus and exponent."));
201 ASN1 modulus = pubkey [0];
202 if ((modulus == null) || (modulus.Tag != 0x02))
203 throw new CryptographicException (Locale.GetText ("Missing RSA modulus."));
204 ASN1 exponent = pubkey [1];
205 if (exponent.Tag != 0x02)
206 throw new CryptographicException (Locale.GetText ("Missing RSA public exponent."));
208 rsaParams.Modulus = GetUnsignedBigInteger (modulus.Value);
209 rsaParams.Exponent = exponent.Value;
211 catch (Exception e) {
212 string msg = Locale.GetText ("Error decoding the ASN.1 structure.");
213 throw new CryptographicException (msg, e);
216 int keySize = (rsaParams.Modulus.Length << 3);
217 RSA rsa = (RSA) new RSACryptoServiceProvider (keySize);
218 rsa.ImportParameters (rsaParams);