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;
38 using Mono.Security.Cryptography;
40 namespace System.Reflection {
46 public class StrongNameKeyPair
48 private byte[] _publicKey;
49 private string _keyPairContainer;
50 private bool _keyPairExported;
51 private byte[] _keyPairArray;
56 // note: we ask for UnmanagedCode because we do not want everyone
57 // to be able to generate strongnamed assemblies
59 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
60 public StrongNameKeyPair (byte[] keyPairArray)
62 if (keyPairArray == null)
63 throw new ArgumentNullException ("keyPairArray");
65 LoadKey (keyPairArray);
69 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
70 public StrongNameKeyPair (FileStream keyPairFile)
72 if (keyPairFile == null)
73 throw new ArgumentNullException ("keyPairFile");
75 byte[] input = new byte [keyPairFile.Length];
76 keyPairFile.Read (input, 0, input.Length);
81 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
82 public StrongNameKeyPair (string keyPairContainer)
84 // named key container
85 if (keyPairContainer == null)
86 throw new ArgumentNullException ("keyPairContainer");
88 _keyPairContainer = keyPairContainer;
94 if (_rsa != null) return _rsa;
96 if (_keyPairArray != null) {
98 _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
101 // exception is thrown when getting PublicKey
102 // to match MS implementation
103 _keyPairArray = null;
106 else if (_keyPairContainer != null) {
107 CspParameters csp = new CspParameters ();
108 csp.KeyContainerName = _keyPairContainer;
109 _rsa = new RSACryptoServiceProvider (csp);
114 private void LoadKey (byte[] key)
117 // check for ECMA key
118 if (key.Length == 16) {
121 while (i < key.Length)
124 // it is the ECMA key
125 _publicKey = (byte[]) key.Clone ();
133 // exception is thrown when getting PublicKey
134 // to match MS implementation
138 public byte[] PublicKey {
140 if (_publicKey == null) {
142 // ECMA "key" is valid but doesn't produce a RSA instance
144 throw new ArgumentException ("invalid keypair");
146 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
147 _publicKey = new byte [blob.Length + 12];
148 // The first 12 bytes are documented at:
\r
149 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
\r
150 // ALG_ID - Signature
\r
151 _publicKey[0] = 0x00;
\r
152 _publicKey[1] = 0x24;
\r
153 _publicKey[2] = 0x00;
\r
154 _publicKey[3] = 0x00;
\r
156 _publicKey[4] = 0x04;
\r
157 _publicKey[5] = 0x80;
\r
158 _publicKey[6] = 0x00;
\r
159 _publicKey[7] = 0x00;
\r
160 // Length of Public Key (in bytes)
\r
161 int lastPart = blob.Length;
\r
162 _publicKey[8] = (byte)(lastPart % 256);
\r
163 _publicKey[9] = (byte)(lastPart / 256); // just in case
\r
164 _publicKey[10] = 0x00;
\r
165 _publicKey[11] = 0x00;
\r
167 Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
173 internal StrongName StrongName ()
177 return new StrongName (rsa);
178 if (_publicKey != null)
179 return new StrongName (_publicKey);