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 // (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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
37 using System
.Security
.Cryptography
;
40 using Mono
.Security
.Cryptography
;
42 namespace System
.Reflection
{
45 public class StrongNameKeyPair
47 private byte[] _publicKey
;
48 private string _keyPairContainer
;
49 private bool _keyPairExported
;
50 private byte[] _keyPairArray
;
55 public StrongNameKeyPair (byte[] keyPairArray
)
57 if (keyPairArray
== null)
58 throw new ArgumentNullException ("keyPairArray");
60 LoadKey (keyPairArray
);
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
);
75 public StrongNameKeyPair (string keyPairContainer
)
77 // named key container
78 if (keyPairContainer
== null)
79 throw new ArgumentNullException ("keyPairContainer");
81 _keyPairContainer
= keyPairContainer
;
87 if (_rsa
!= null) return _rsa
;
89 if (_keyPairArray
!= null) {
91 _rsa
= CryptoConvert
.FromCapiKeyBlob (_keyPairArray
);
94 // exception is thrown when getting PublicKey
95 // to match MS implementation
99 else if (_keyPairContainer
!= null) {
100 CspParameters csp
= new CspParameters ();
101 csp
.KeyContainerName
= _keyPairContainer
;
102 _rsa
= new RSACryptoServiceProvider (csp
);
107 private void LoadKey (byte[] key
)
110 // check for ECMA key
111 if (key
.Length
== 16) {
114 while (i
< key
.Length
)
117 // it is the ECMA key
118 _publicKey
= (byte[]) key
.Clone ();
126 // exception is thrown when getting PublicKey
127 // to match MS implementation
131 public byte[] PublicKey
{
133 if (_publicKey
== null) {
135 // ECMA "key" is valid but doesn't produce a RSA instance
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;
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
);
166 internal StrongName
StrongName ()
170 return new StrongName (rsa
);
171 if (_publicKey
!= null)
172 return new StrongName (_publicKey
);