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