2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
[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 // (C) 2004 Novell (http://www.novell.com)
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System.IO;
37 using System.Security.Cryptography;
38
39 using Mono.Security;
40 using Mono.Security.Cryptography;
41
42 namespace System.Reflection {
43
44 [Serializable]
45 public class StrongNameKeyPair 
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         public StrongNameKeyPair (byte[] keyPairArray) 
56         {
57                 if (keyPairArray == null)
58                         throw new ArgumentNullException ("keyPairArray");
59
60                 LoadKey (keyPairArray);
61                 GetRSA ();
62         }
63         
64         public StrongNameKeyPair (FileStream keyPairFile) 
65         {
66                 if (keyPairFile == null)
67                         throw new ArgumentNullException ("keyPairFile");
68
69                 byte[] input = new byte [keyPairFile.Length];
70                 keyPairFile.Read (input, 0, input.Length);
71                 LoadKey (input);
72                 GetRSA ();
73         }
74         
75         public StrongNameKeyPair (string keyPairContainer) 
76         {
77                 // named key container
78                 if (keyPairContainer == null)
79                         throw new ArgumentNullException ("keyPairContainer");
80
81                 _keyPairContainer = keyPairContainer;
82                 GetRSA ();
83         }
84         
85         private RSA GetRSA ()
86         {
87                 if (_rsa != null) return _rsa;
88                 
89                 if (_keyPairArray != null) {
90                         try {
91                                 _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
92                         }
93                         catch {
94                                 // exception is thrown when getting PublicKey
95                                 // to match MS implementation
96                                 _keyPairArray = null;
97                         }
98                 }
99                 else if (_keyPairContainer != null) {
100                         CspParameters csp = new CspParameters ();
101                         csp.KeyContainerName = _keyPairContainer;
102                         _rsa = new RSACryptoServiceProvider (csp);
103                 }
104                 return _rsa;
105         }
106
107         private void LoadKey (byte[] key) 
108         {
109                 try {
110                         // check for ECMA key
111                         if (key.Length == 16) {
112                                 int i = 0;
113                                 int sum = 0;
114                                 while (i < key.Length)
115                                         sum += key [i++];
116                                 if (sum == 4) {
117                                         // it is the ECMA key
118                                         _publicKey = (byte[]) key.Clone ();
119                                 }
120                         }
121                         else
122                                 _keyPairArray = key;
123                 }
124                 catch
125                 {
126                         // exception is thrown when getting PublicKey
127                         // to match MS implementation
128                 }
129         }
130
131         public byte[] PublicKey {
132                 get {
133                         if (_publicKey == null) {
134                                 RSA rsa = GetRSA ();
135                                 // ECMA "key" is valid but doesn't produce a RSA instance
136                                 if (rsa == null)
137                                         throw new ArgumentException ("invalid keypair");
138
139                                 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
140                                 _publicKey = new byte [blob.Length + 12];
141                                 // The first 12 bytes are documented at:\r
142                                 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp\r
143                                 // ALG_ID - Signature\r
144                                 _publicKey[0] = 0x00;\r
145                                 _publicKey[1] = 0x24;   \r
146                                 _publicKey[2] = 0x00;   \r
147                                 _publicKey[3] = 0x00;   \r
148                                 // ALG_ID - Hash\r
149                                 _publicKey[4] = 0x04;\r
150                                 _publicKey[5] = 0x80;\r
151                                 _publicKey[6] = 0x00;\r
152                                 _publicKey[7] = 0x00;\r
153                                 // Length of Public Key (in bytes)\r
154                                 int lastPart = blob.Length;\r
155                                 _publicKey[8] = (byte)(lastPart % 256);\r
156                                 _publicKey[9] = (byte)(lastPart / 256); // just in case\r
157                                 _publicKey[10] = 0x00;\r
158                                 _publicKey[11] = 0x00;\r
159
160                                 Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
161                         }
162                         return _publicKey;
163                 }
164         }
165
166         internal StrongName StrongName () 
167         {
168                 RSA rsa = GetRSA ();
169                 if (rsa != null)
170                         return new StrongName (rsa);
171                 if (_publicKey != null)
172                         return new StrongName (_publicKey);
173                 return null;
174         }
175 }
176
177 }