-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Program.cs
211 lines (183 loc) · 7.17 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
namespace FakeSign
{
/// <summary>
/// Takes a delay-signed assembly and flips its CLI header "strong-name signed" bit without
/// adding a correct signature. This creates an assembly that can be loaded in full trust
/// without registering for verification skipping. The assembly cannot be installed to the
/// GAC.
/// </summary>
/// <remarks>
/// This code is taken largely from the Microsoft.BuildTools project and their OSS signing
/// process.
///
/// https://github.com/dotnet/buildtools/blob/master/src/Microsoft.DotNet.Build.Tasks/OpenSourceSign.cs
/// </remarks>
internal static class Program
{
/// <summary>
/// The number of bytes from the start of the <see cref="CorHeader"/> to its <see cref="CorFlags"/>.
/// </summary>
private const int OffsetFromStartOfCorHeaderToFlags =
sizeof(Int32) // byte count
+ sizeof(Int16) // major version
+ sizeof(Int16) // minor version
+ sizeof(Int64); // metadata directory
private static bool ExecuteCore(string assemblyPath, bool unSign = false, bool force = false)
{
if (Directory.Exists(assemblyPath))
{
Console.Error.WriteLine($"Expected file, not a directory: {assemblyPath}");
return false;
}
if (!File.Exists(assemblyPath))
{
Console.Error.WriteLine($"File not found: {assemblyPath}");
return false;
}
using (var stream = OpenFile(assemblyPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
using (var reader = new PEReader(stream))
using (var writer = new BinaryWriter(stream))
{
var mdReader = ValidateManagedAssemblyAndGetMetadataReader(reader);
if (mdReader == null)
{
Console.Error.WriteLine($"Cannot {(unSign ? "un-sign" : "sign")} {assemblyPath}.");
return false;
}
if (!force && !Validate(reader, mdReader, unSign))
{
Console.Error.WriteLine($"Use the -f (force) option to {(unSign ? "un-sign" : "sign")} {assemblyPath} anyway.");
return false;
}
stream.Position = reader.PEHeaders.CorHeaderStartOffset + OffsetFromStartOfCorHeaderToFlags;
var flags = reader.PEHeaders.CorHeader.Flags;
if (unSign)
{
flags &= ~CorFlags.StrongNameSigned;
}
else
{
flags |= CorFlags.StrongNameSigned;
}
writer.Write((UInt32)flags);
}
return true;
}
private static MetadataReader ValidateManagedAssemblyAndGetMetadataReader(PEReader peReader)
{
if (!peReader.HasMetadata)
{
Console.Error.WriteLine("PE file is not a managed module.");
return null;
}
var mdReader = peReader.GetMetadataReader();
if (!mdReader.IsAssembly)
{
Console.Error.WriteLine("PE file is not an assembly.");
return null;
}
return mdReader;
}
/// <summary>
/// Returns true if the PE file meets all of the pre-conditions to be Open Source Signed.
/// Returns false and logs msbuild errors otherwise.
/// </summary>
private static bool Validate(PEReader peReader, MetadataReader mdReader, bool unSign)
{
CorHeader header = peReader.PEHeaders.CorHeader;
var expectedStrongNameFlag = unSign ? CorFlags.StrongNameSigned : 0;
var actualStrongNameFlag = header.Flags & CorFlags.StrongNameSigned;
if (expectedStrongNameFlag != actualStrongNameFlag)
{
Console.Error.WriteLine($"PE file is {(unSign ? "not" : "already")} strong-name signed.");
return false;
}
if ((header.StrongNameSignatureDirectory.Size <= 0) || mdReader.GetAssemblyDefinition().PublicKey.IsNil)
{
Console.Error.WriteLine("PE file is not a delay-signed assembly.");
return false;
}
return true;
}
/// <summary>
/// Wraps FileStream constructor to normalize all unpreventable exceptions to IOException.
/// </summary>
private static FileStream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
{
try
{
return new FileStream(path, mode, access, share);
}
catch (ArgumentException ex)
{
throw new IOException(ex.Message, ex);
}
catch (UnauthorizedAccessException ex)
{
throw new IOException(ex.Message, ex);
}
catch (NotSupportedException ex)
{
throw new IOException(ex.Message, ex);
}
}
internal static int Main(string[] args)
{
string file = null;
bool unSign = false;
bool force = false;
foreach (string arg in args)
{
if (arg.Length >= 2 && (arg[0] == '-' || arg[0] == '/'))
{
switch (arg[1])
{
case '?':
goto Help;
case 'u':
case 'U':
unSign = true;
break;
case 'f':
case 'F':
force = true;
break;
default:
Console.Error.WriteLine($"Unrecognized switch {arg}");
goto Help;
}
}
else if (file != null)
{
Console.Error.WriteLine("Too many arguments.");
goto Help;
}
else
{
file = arg;
}
}
if (file == null)
{
Console.Error.WriteLine("Missing assemblyPath.");
goto Help;
}
return ExecuteCore(file, unSign, force) ? 0 : 1;
Help:
Console.Error.Write(
@"Sets or removes the ""strong name signed"" flag in a managed assembly. This
creates an assembly that can be loaded in full trust without registering for
verification skipping.
FakeSign [-u] [-f] assemblyPath
-u (unsign) Clears the strong name flag (default is to set the flag).
-f (force) Updates even if nothing would change.
");
return 1;
}
}
}