2 Copyright (C) 2009-2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
26 using System.Security.Cryptography;
28 namespace IKVM.Reflection
30 public sealed class StrongNameKeyPair
32 private readonly byte[] keyPairArray;
33 private readonly string keyPairContainer;
35 public StrongNameKeyPair(string keyPairContainer)
37 if (keyPairContainer == null)
39 throw new ArgumentNullException("keyPairContainer");
41 if (Universe.MonoRuntime && Environment.OSVersion.Platform == PlatformID.Win32NT)
43 throw new NotSupportedException("IKVM.Reflection does not support key containers when running on Mono");
45 this.keyPairContainer = keyPairContainer;
48 public StrongNameKeyPair(byte[] keyPairArray)
50 if (keyPairArray == null)
52 throw new ArgumentNullException("keyPairArray");
54 this.keyPairArray = (byte[])keyPairArray.Clone();
57 public StrongNameKeyPair(FileStream keyPairFile)
58 : this(ReadAllBytes(keyPairFile))
62 private static byte[] ReadAllBytes(FileStream keyPairFile)
64 if (keyPairFile == null)
66 throw new ArgumentNullException("keyPairFile");
68 byte[] buf = new byte[keyPairFile.Length - keyPairFile.Position];
69 keyPairFile.Read(buf, 0, buf.Length);
73 public byte[] PublicKey
77 if (Universe.MonoRuntime)
79 // MONOBUG workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5299
80 return MonoGetPublicKey();
82 using (RSACryptoServiceProvider rsa = CreateRSA())
84 byte[] cspBlob = rsa.ExportCspBlob(false);
85 byte[] publicKey = new byte[12 + cspBlob.Length];
86 Buffer.BlockCopy(cspBlob, 0, publicKey, 12, cspBlob.Length);
90 publicKey[8] = (byte)(cspBlob.Length >> 0);
91 publicKey[9] = (byte)(cspBlob.Length >> 8);
92 publicKey[10] = (byte)(cspBlob.Length >> 16);
93 publicKey[11] = (byte)(cspBlob.Length >> 24);
99 internal RSACryptoServiceProvider CreateRSA()
103 if (keyPairArray != null)
105 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
106 rsa.ImportCspBlob(keyPairArray);
111 CspParameters parm = new CspParameters();
112 parm.KeyContainerName = keyPairContainer;
113 // MONOBUG Mono doesn't like it when Flags or KeyNumber are set
114 if (!Universe.MonoRuntime)
116 parm.Flags = CspProviderFlags.UseMachineKeyStore | CspProviderFlags.UseExistingKey;
117 parm.KeyNumber = 2; // Signature
119 return new RSACryptoServiceProvider(parm);
124 throw new ArgumentException("Unable to obtain public key for StrongNameKeyPair.");
128 [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
129 private byte[] MonoGetPublicKey()
131 return keyPairArray != null
132 ? new System.Reflection.StrongNameKeyPair(keyPairArray).PublicKey
133 : new System.Reflection.StrongNameKeyPair(keyPairContainer).PublicKey;