**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Reflection / StrongNameKeyPair.cs
blobdd13075a47b2b1ba805ba70049051943089a4338
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)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
36 using System.IO;
37 using System.Security.Cryptography;
39 using Mono.Security;
40 using Mono.Security.Cryptography;
42 namespace System.Reflection {
44 [Serializable]
45 public class StrongNameKeyPair
47 private byte[] _publicKey;
48 private string _keyPairContainer;
49 private bool _keyPairExported;
50 private byte[] _keyPairArray;
52 [NonSerialized]
53 private RSA _rsa;
55 public StrongNameKeyPair (byte[] keyPairArray)
57 if (keyPairArray == null)
58 throw new ArgumentNullException ("keyPairArray");
60 LoadKey (keyPairArray);
61 GetRSA ();
64 public StrongNameKeyPair (FileStream keyPairFile)
66 if (keyPairFile == null)
67 throw new ArgumentNullException ("keyPairFile");
69 byte[] input = new byte [keyPairFile.Length];
70 keyPairFile.Read (input, 0, input.Length);
71 LoadKey (input);
72 GetRSA ();
75 public StrongNameKeyPair (string keyPairContainer)
77 // named key container
78 if (keyPairContainer == null)
79 throw new ArgumentNullException ("keyPairContainer");
81 _keyPairContainer = keyPairContainer;
82 GetRSA ();
85 private RSA GetRSA ()
87 if (_rsa != null) return _rsa;
89 if (_keyPairArray != null) {
90 try {
91 _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
93 catch {
94 // exception is thrown when getting PublicKey
95 // to match MS implementation
96 _keyPairArray = null;
99 else if (_keyPairContainer != null) {
100 CspParameters csp = new CspParameters ();
101 csp.KeyContainerName = _keyPairContainer;
102 _rsa = new RSACryptoServiceProvider (csp);
104 return _rsa;
107 private void LoadKey (byte[] key)
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 ();
121 else
122 _keyPairArray = key;
124 catch
126 // exception is thrown when getting PublicKey
127 // to match MS implementation
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");
139 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
140 _publicKey = new byte [blob.Length + 12];
141 // The first 12 bytes are documented at:
142 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
143 // ALG_ID - Signature
144 _publicKey[0] = 0x00;
145 _publicKey[1] = 0x24;
146 _publicKey[2] = 0x00;
147 _publicKey[3] = 0x00;
148 // ALG_ID - Hash
149 _publicKey[4] = 0x04;
150 _publicKey[5] = 0x80;
151 _publicKey[6] = 0x00;
152 _publicKey[7] = 0x00;
153 // Length of Public Key (in bytes)
154 int lastPart = blob.Length;
155 _publicKey[8] = (byte)(lastPart % 256);
156 _publicKey[9] = (byte)(lastPart / 256); // just in case
157 _publicKey[10] = 0x00;
158 _publicKey[11] = 0x00;
160 Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
162 return _publicKey;
166 internal StrongName StrongName ()
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;