Merge pull request #131 from txdv/master
[mono.git] / mcs / class / corlib / System.Reflection / StrongNameKeyPair.cs
1 //
2 // System.Reflection.StrongNameKeyPair.cs
3 //
4 // Authors:
5 //      Kevin Winchester (kwin@ns.sympatico.ca)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) 2002 Kevin Winchester
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-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 using System.IO;
33 using System.Security.Cryptography;
34 using System.Security.Permissions;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37
38 using Mono.Security;
39 using Mono.Security.Cryptography;
40
41 namespace System.Reflection {
42
43         [ComVisible (true)]
44 [Serializable]
45 public class StrongNameKeyPair : ISerializable, IDeserializationCallback
46 {               
47         private byte[] _publicKey;
48         private string _keyPairContainer;
49         private bool _keyPairExported;
50         private byte[] _keyPairArray;
51         
52         [NonSerialized]
53         private RSA _rsa;
54
55         // note: we ask for UnmanagedCode because we do not want everyone
56         // to be able to generate strongnamed assemblies
57
58         [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
59         public StrongNameKeyPair (byte[] keyPairArray) 
60         {
61                 if (keyPairArray == null)
62                         throw new ArgumentNullException ("keyPairArray");
63
64                 LoadKey (keyPairArray);
65                 GetRSA ();
66         }
67         
68         [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
69         public StrongNameKeyPair (FileStream keyPairFile) 
70         {
71                 if (keyPairFile == null)
72                         throw new ArgumentNullException ("keyPairFile");
73
74                 byte[] input = new byte [keyPairFile.Length];
75                 keyPairFile.Read (input, 0, input.Length);
76                 LoadKey (input);
77                 GetRSA ();
78         }
79         
80         [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
81         public StrongNameKeyPair (string keyPairContainer) 
82         {
83                 // named key container
84                 if (keyPairContainer == null)
85                         throw new ArgumentNullException ("keyPairContainer");
86
87                 _keyPairContainer = keyPairContainer;
88                 GetRSA ();
89         }
90         protected StrongNameKeyPair (SerializationInfo info, StreamingContext context)
91         {
92                 _publicKey = (byte []) info.GetValue ("_publicKey", typeof (byte []));
93                 _keyPairContainer = info.GetString ("_keyPairContainer");
94                 _keyPairExported = info.GetBoolean ("_keyPairExported");
95                 _keyPairArray = (byte []) info.GetValue ("_keyPairArray", typeof (byte []));
96         }
97
98         void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
99         {
100                 info.AddValue ("_publicKey", _publicKey, typeof (byte []));
101                 info.AddValue ("_keyPairContainer", _keyPairContainer);
102                 info.AddValue ("_keyPairExported", _keyPairExported);
103                 info.AddValue ("_keyPairArray", _keyPairArray, typeof (byte []));
104         }
105
106         void IDeserializationCallback.OnDeserialization (object sender)
107         {
108         }
109
110         private RSA GetRSA ()
111         {
112                 if (_rsa != null) return _rsa;
113                 
114                 if (_keyPairArray != null) {
115                         try {
116                                 _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
117                         }
118                         catch {
119                                 // exception is thrown when getting PublicKey
120                                 // to match MS implementation
121                                 _keyPairArray = null;
122                         }
123                 }
124 #if !NET_2_1
125                 else if (_keyPairContainer != null) {
126                         CspParameters csp = new CspParameters ();
127                         csp.KeyContainerName = _keyPairContainer;
128                         _rsa = new RSACryptoServiceProvider (csp);
129                 }
130 #endif
131                 return _rsa;
132         }
133
134         private void LoadKey (byte[] key) 
135         {
136                 try {
137                         // check for ECMA key
138                         if (key.Length == 16) {
139                                 int i = 0;
140                                 int sum = 0;
141                                 while (i < key.Length)
142                                         sum += key [i++];
143                                 if (sum == 4) {
144                                         // it is the ECMA key
145                                         _publicKey = (byte[]) key.Clone ();
146                                 }
147                         }
148                         else
149                                 _keyPairArray = key;
150                 }
151                 catch
152                 {
153                         // exception is thrown when getting PublicKey
154                         // to match MS implementation
155                 }
156         }
157
158         public byte[] PublicKey {
159                 get {
160                         if (_publicKey == null) {
161                                 RSA rsa = GetRSA ();
162                                 // ECMA "key" is valid but doesn't produce a RSA instance
163                                 if (rsa == null)
164                                         throw new ArgumentException ("invalid keypair");
165
166                                 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
167                                 _publicKey = new byte [blob.Length + 12];
168                                 // The first 12 bytes are documented at:
169                                 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
170                                 // ALG_ID - Signature
171                                 _publicKey[0] = 0x00;
172                                 _publicKey[1] = 0x24;   
173                                 _publicKey[2] = 0x00;   
174                                 _publicKey[3] = 0x00;   
175                                 // ALG_ID - Hash
176                                 _publicKey[4] = 0x04;
177                                 _publicKey[5] = 0x80;
178                                 _publicKey[6] = 0x00;
179                                 _publicKey[7] = 0x00;
180                                 // Length of Public Key (in bytes)
181                                 int lastPart = blob.Length;
182                                 _publicKey[8] = (byte)(lastPart % 256);
183                                 _publicKey[9] = (byte)(lastPart / 256); // just in case
184                                 _publicKey[10] = 0x00;
185                                 _publicKey[11] = 0x00;
186
187                                 Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
188                         }
189                         return _publicKey;
190                 }
191         }
192
193         internal StrongName StrongName () 
194         {
195                 RSA rsa = GetRSA ();
196                 if (rsa != null)
197                         return new StrongName (rsa);
198                 if (_publicKey != null)
199                         return new StrongName (_publicKey);
200                 return null;
201         }
202 }
203
204 }