Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / IKVM.Reflection / StrongNameKeyPair.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
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.
7
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:
11
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.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.IO;
26 using System.Security.Cryptography;
27
28 namespace IKVM.Reflection
29 {
30         public sealed class StrongNameKeyPair
31         {
32                 private readonly byte[] keyPairArray;
33                 private readonly string keyPairContainer;
34
35                 public StrongNameKeyPair(string keyPairContainer)
36                 {
37                         if (keyPairContainer == null)
38                         {
39                                 throw new ArgumentNullException("keyPairContainer");
40                         }
41                         if (Universe.MonoRuntime && Environment.OSVersion.Platform == PlatformID.Win32NT)
42                         {
43                                 throw new NotSupportedException("IKVM.Reflection does not support key containers when running on Mono");
44                         }
45                         this.keyPairContainer = keyPairContainer;
46                 }
47
48                 public StrongNameKeyPair(byte[] keyPairArray)
49                 {
50                         if (keyPairArray == null)
51                         {
52                                 throw new ArgumentNullException("keyPairArray");
53                         }
54                         this.keyPairArray = (byte[])keyPairArray.Clone();
55                 }
56
57                 public StrongNameKeyPair(FileStream keyPairFile)
58                         : this(ReadAllBytes(keyPairFile))
59                 {
60                 }
61
62                 private static byte[] ReadAllBytes(FileStream keyPairFile)
63                 {
64                         if (keyPairFile == null)
65                         {
66                                 throw new ArgumentNullException("keyPairFile");
67                         }
68                         byte[] buf = new byte[keyPairFile.Length - keyPairFile.Position];
69                         keyPairFile.Read(buf, 0, buf.Length);
70                         return buf;
71                 }
72
73                 public byte[] PublicKey
74                 {
75                         get
76                         {
77                                 if (Universe.MonoRuntime)
78                                 {
79                                         // MONOBUG workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5299
80                                         return MonoGetPublicKey();
81                                 }
82                                 using (RSACryptoServiceProvider rsa = CreateRSA())
83                                 {
84                                         byte[] cspBlob = rsa.ExportCspBlob(false);
85                                         byte[] publicKey = new byte[12 + cspBlob.Length];
86                                         Buffer.BlockCopy(cspBlob, 0, publicKey, 12, cspBlob.Length);
87                                         publicKey[1] = 36;
88                                         publicKey[4] = 4;
89                                         publicKey[5] = 128;
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);
94                                         return publicKey;
95                                 }
96                         }
97                 }
98
99                 internal RSACryptoServiceProvider CreateRSA()
100                 {
101                         try
102                         {
103                                 if (keyPairArray != null)
104                                 {
105                                         RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
106                                         rsa.ImportCspBlob(keyPairArray);
107                                         return rsa;
108                                 }
109                                 else
110                                 {
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)
115                                         {
116                                                 parm.Flags = CspProviderFlags.UseMachineKeyStore | CspProviderFlags.UseExistingKey;
117                                                 parm.KeyNumber = 2;     // Signature
118                                         }
119                                         return new RSACryptoServiceProvider(parm);
120                                 }
121                         }
122                         catch
123                         {
124                                 throw new ArgumentException("Unable to obtain public key for StrongNameKeyPair.");
125                         }
126                 }
127
128                 [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
129                 private byte[] MonoGetPublicKey()
130                 {
131                         return keyPairArray != null
132                                 ? new System.Reflection.StrongNameKeyPair(keyPairArray).PublicKey
133                                 : new System.Reflection.StrongNameKeyPair(keyPairContainer).PublicKey;
134                 }
135         }
136 }