2 // System.Reflection.StrongNameKeyPair.cs
5 // Kevin Winchester (kwin@ns.sympatico.ca)
6 // Sebastien Pouliot (sebastien@ximian.com)
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)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Security.Cryptography;
34 using System.Security.Permissions;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
39 using Mono.Security.Cryptography;
41 namespace System.Reflection {
47 public class StrongNameKeyPair
49 : ISerializable, IDeserializationCallback
52 private byte[] _publicKey;
53 private string _keyPairContainer;
55 private bool _keyPairExported;
57 private byte[] _keyPairArray;
62 // note: we ask for UnmanagedCode because we do not want everyone
63 // to be able to generate strongnamed assemblies
65 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
66 public StrongNameKeyPair (byte[] keyPairArray)
68 if (keyPairArray == null)
69 throw new ArgumentNullException ("keyPairArray");
71 LoadKey (keyPairArray);
75 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
76 public StrongNameKeyPair (FileStream keyPairFile)
78 if (keyPairFile == null)
79 throw new ArgumentNullException ("keyPairFile");
81 byte[] input = new byte [keyPairFile.Length];
82 keyPairFile.Read (input, 0, input.Length);
87 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
88 public StrongNameKeyPair (string keyPairContainer)
90 // named key container
91 if (keyPairContainer == null)
92 throw new ArgumentNullException ("keyPairContainer");
94 _keyPairContainer = keyPairContainer;
98 protected StrongNameKeyPair (SerializationInfo info, StreamingContext context)
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 []));
106 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
108 info.AddValue ("_publicKey", _publicKey, typeof (byte []));
109 info.AddValue ("_keyPairContainer", _keyPairContainer);
110 info.AddValue ("_keyPairExported", _keyPairExported);
111 info.AddValue ("_keyPairArray", _keyPairArray, typeof (byte []));
114 void IDeserializationCallback.OnDeserialization (object sender)
118 private RSA GetRSA ()
120 if (_rsa != null) return _rsa;
122 if (_keyPairArray != null) {
124 _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
127 // exception is thrown when getting PublicKey
128 // to match MS implementation
129 _keyPairArray = null;
133 else if (_keyPairContainer != null) {
134 CspParameters csp = new CspParameters ();
135 csp.KeyContainerName = _keyPairContainer;
136 _rsa = new RSACryptoServiceProvider (csp);
142 private void LoadKey (byte[] key)
145 // check for ECMA key
146 if (key.Length == 16) {
149 while (i < key.Length)
152 // it is the ECMA key
153 _publicKey = (byte[]) key.Clone ();
161 // exception is thrown when getting PublicKey
162 // to match MS implementation
166 public byte[] PublicKey {
168 if (_publicKey == null) {
170 // ECMA "key" is valid but doesn't produce a RSA instance
172 throw new ArgumentException ("invalid keypair");
174 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
175 _publicKey = new byte [blob.Length + 12];
176 // The first 12 bytes are documented at:
177 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
178 // ALG_ID - Signature
179 _publicKey[0] = 0x00;
180 _publicKey[1] = 0x24;
181 _publicKey[2] = 0x00;
182 _publicKey[3] = 0x00;
184 _publicKey[4] = 0x04;
185 _publicKey[5] = 0x80;
186 _publicKey[6] = 0x00;
187 _publicKey[7] = 0x00;
188 // Length of Public Key (in bytes)
189 int lastPart = blob.Length;
190 _publicKey[8] = (byte)(lastPart % 256);
191 _publicKey[9] = (byte)(lastPart / 256); // just in case
192 _publicKey[10] = 0x00;
193 _publicKey[11] = 0x00;
195 Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
201 internal StrongName StrongName ()
205 return new StrongName (rsa);
206 if (_publicKey != null)
207 return new StrongName (_publicKey);