5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2010 Jb Evain
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Reflection;
32
using System.Security.Cryptography;
36
#if !SILVERLIGHT && !CF
37
using System.Runtime.Serialization;
38
using Mono.Security.Cryptography;
43
namespace Mono.Cecil {
45
// Most of this code has been adapted
46
// from Jeroen Frijters' fantastic work
47
// in IKVM.Reflection.Emit. Thanks!
49
static class CryptoService {
51
#if !SILVERLIGHT && !CF
52
public static void StrongName (Stream stream, ImageWriter writer, StrongNameKeyPair key_pair)
54
int strong_name_pointer;
56
var strong_name = CreateStrongName (key_pair, HashStream (stream, writer, out strong_name_pointer));
57
PatchStrongName (stream, strong_name_pointer, strong_name);
60
static void PatchStrongName (Stream stream, int strong_name_pointer, byte [] strong_name)
62
stream.Seek (strong_name_pointer, SeekOrigin.Begin);
63
stream.Write (strong_name, 0, strong_name.Length);
66
static byte [] CreateStrongName (StrongNameKeyPair key_pair, byte [] hash)
68
const string hash_algo = "SHA1";
70
using (var rsa = key_pair.CreateRSA ()) {
71
var formatter = new RSAPKCS1SignatureFormatter (rsa);
72
formatter.SetHashAlgorithm (hash_algo);
74
byte [] signature = formatter.CreateSignature (hash);
75
Array.Reverse (signature);
81
static byte [] HashStream (Stream stream, ImageWriter writer, out int strong_name_pointer)
83
const int buffer_size = 8192;
85
var text = writer.text;
86
var header_size = (int) writer.GetHeaderSize ();
87
var text_section_pointer = (int) text.PointerToRawData;
88
var strong_name_directory = writer.GetStrongNameSignatureDirectory ();
90
if (strong_name_directory.Size == 0)
91
throw new InvalidOperationException ();
93
strong_name_pointer = (int) (text_section_pointer
94
+ (strong_name_directory.VirtualAddress - text.VirtualAddress));
95
var strong_name_length = (int) strong_name_directory.Size;
97
var sha1 = new SHA1Managed ();
98
var buffer = new byte [buffer_size];
99
using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) {
101
stream.Seek (0, SeekOrigin.Begin);
102
CopyStreamChunk (stream, crypto_stream, buffer, header_size);
104
stream.Seek (text_section_pointer, SeekOrigin.Begin);
105
CopyStreamChunk (stream, crypto_stream, buffer, (int) strong_name_pointer - text_section_pointer);
107
stream.Seek (strong_name_length, SeekOrigin.Current);
108
CopyStreamChunk (stream, crypto_stream, buffer, (int) (stream.Length - (strong_name_pointer + strong_name_length)));
114
static void CopyStreamChunk (Stream stream, Stream dest_stream, byte [] buffer, int length)
117
int read = stream.Read (buffer, 0, System.Math.Min (buffer.Length, length));
118
dest_stream.Write (buffer, 0, read);
123
public static byte [] ComputeHash (string file)
125
if (!File.Exists (file))
126
return Empty<byte>.Array;
128
const int buffer_size = 8192;
130
var sha1 = new SHA1Managed ();
132
using (var stream = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read)) {
134
var buffer = new byte [buffer_size];
136
using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write))
137
CopyStreamChunk (stream, crypto_stream, buffer, (int) stream.Length);
144
#if !SILVERLIGHT && !CF
145
static partial class Mixin {
147
public static RSA CreateRSA (this StrongNameKeyPair key_pair)
150
string key_container;
152
if (!TryGetKeyContainer (key_pair, out key, out key_container))
153
return CryptoConvert.FromCapiKeyBlob (key);
155
var parameters = new CspParameters {
156
Flags = CspProviderFlags.UseMachineKeyStore,
157
KeyContainerName = key_container,
161
return new RSACryptoServiceProvider (parameters);
164
static bool TryGetKeyContainer (ISerializable key_pair, out byte [] key, out string key_container)
166
var info = new SerializationInfo (typeof (StrongNameKeyPair), new FormatterConverter ());
167
key_pair.GetObjectData (info, new StreamingContext ());
169
key = (byte []) info.GetValue ("_keyPairArray", typeof (byte []));
170
key_container = info.GetString ("_keyPairContainer");
171
return key_container != null;