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
|
/*
Copyright (C) 2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
#if !NO_AUTHENTICODE
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
namespace IKVM.Reflection.Reader
{
// This code is based on trial-and-error and some inspiration from the Mono.Security library.
// It almost certainly has bugs and/or design flaws.
static class Authenticode
{
private const ushort IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
private const ushort IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
private const int WIN_CERT_REVISION_2_0 = 0x0200;
private const int WIN_CERT_TYPE_PKCS_SIGNED_DATA = 0x0002;
internal static X509Certificate GetSignerCertificate(Stream stream)
{
stream.Seek(60, SeekOrigin.Begin);
BinaryReader br = new BinaryReader(stream);
int peSignatureOffset = br.ReadInt32();
int checksumOffset = peSignatureOffset + 24 + 64;
// seek to the IMAGE_OPTIONAL_HEADER
stream.Seek(peSignatureOffset + 24, SeekOrigin.Begin);
int certificateTableDataDirectoryOffset;
switch (br.ReadUInt16())
{
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
certificateTableDataDirectoryOffset = peSignatureOffset + 24 + (64 + 4 * 8) + 8 * 4;
break;
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
certificateTableDataDirectoryOffset = peSignatureOffset + 24 + (64 + 4 * 8 + 16) + 8 * 4;
break;
default:
throw new BadImageFormatException();
}
stream.Seek(certificateTableDataDirectoryOffset, SeekOrigin.Begin);
int certificateTableOffset = br.ReadInt32();
int certificateTableLength = br.ReadInt32();
stream.Seek(certificateTableOffset, SeekOrigin.Begin);
int dwLength = br.ReadInt32();
short wRevision = br.ReadInt16();
short wCertificateType = br.ReadInt16();
if (wRevision != WIN_CERT_REVISION_2_0)
{
return null;
}
if (wCertificateType != WIN_CERT_TYPE_PKCS_SIGNED_DATA)
{
return null;
}
byte[] buf = new byte[certificateTableLength - 8];
stream.Read(buf, 0, buf.Length);
SignedCms cms = new SignedCms();
try
{
cms.Decode(buf);
cms.CheckSignature(false);
}
catch (CryptographicException)
{
return null;
}
SignerInfo signerInfo = cms.SignerInfos[0];
int[] offsets = new int[] { checksumOffset, certificateTableDataDirectoryOffset, certificateTableOffset };
int[] lengths = new int[] { 4, 8, certificateTableLength };
byte[] actualHash = ComputeHashWithSkip(stream, signerInfo.DigestAlgorithm.FriendlyName, offsets, lengths);
byte[] requiredHash = DecodeASN1(cms.ContentInfo.Content, 0, 1, 1);
if (requiredHash == null || actualHash.Length != requiredHash.Length)
{
return null;
}
for (int i = 0; i < actualHash.Length; i++)
{
if (actualHash[i] != requiredHash[i])
{
return null;
}
}
return signerInfo.Certificate;
}
private static byte[] ComputeHashWithSkip(Stream stream, string hashAlgorithm, int[] skipOffsets, int[] skipLengths)
{
using (HashAlgorithm hash = HashAlgorithm.Create(hashAlgorithm))
{
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, skipOffsets[0]);
stream.Seek(skipLengths[0], SeekOrigin.Current);
for (int i = 1; i < skipOffsets.Length; i++)
{
HashChunk(stream, cs, buf, skipOffsets[i] - (skipOffsets[i - 1] + skipLengths[i - 1]));
stream.Seek(skipLengths[i], SeekOrigin.Current);
}
HashChunk(stream, cs, buf, (int)stream.Length - (skipOffsets[skipOffsets.Length - 1] + skipLengths[skipLengths.Length - 1]));
}
return hash.Hash;
}
}
private static void HashChunk(Stream stream, CryptoStream cs, byte[] buf, int length)
{
while (length > 0)
{
int read = stream.Read(buf, 0, Math.Min(buf.Length, length));
cs.Write(buf, 0, read);
length -= read;
}
}
private static byte[] DecodeASN1(byte[] buf, params int[] indexes)
{
return DecodeASN1(buf, 0, buf.Length, 0, indexes);
}
private static byte[] DecodeASN1(byte[] buf, int pos, int end, int depth, int[] indexes)
{
for (int index = 0; pos < end; index++)
{
int tag = buf[pos++];
int length = buf[pos++];
if (length > 128)
{
int lenlen = length & 0x7F;
length = 0;
for (int i = 0; i < lenlen; i++)
{
length = length * 256 + buf[pos++];
}
}
if (indexes[depth] == index)
{
if (depth == indexes.Length - 1)
{
byte[] data = new byte[length];
Buffer.BlockCopy(buf, pos, data, 0, length);
return data;
}
if ((tag & 0x20) == 0)
{
return null;
}
return DecodeASN1(buf, pos, pos + length, depth + 1, indexes);
}
pos += length;
}
return null;
}
}
}
#endif // !NO_AUTHENTICODE
|