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