IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Authenticode.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 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*/
24using System;
25using System.IO;
26using System.Security.Cryptography;
27using System.Security.Cryptography.Pkcs;
28using System.Security.Cryptography.X509Certificates;
29
31{
32
33 // This code is based on trial-and-error and some inspiration from the Mono.Security library.
34 // It almost certainly has bugs and/or design flaws.
35 static class Authenticode
36 {
37
38 const ushort IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
39 const ushort IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
40 const int WIN_CERT_REVISION_2_0 = 0x0200;
41 const int WIN_CERT_TYPE_PKCS_SIGNED_DATA = 0x0002;
42
43 internal static X509Certificate GetSignerCertificate(Stream stream)
44 {
45 stream.Seek(60, SeekOrigin.Begin);
46 var br = new BinaryReader(stream);
47 var peSignatureOffset = br.ReadInt32();
48 var checksumOffset = peSignatureOffset + 24 + 64;
49
50 // seek to the IMAGE_OPTIONAL_HEADER
51 stream.Seek(peSignatureOffset + 24, SeekOrigin.Begin);
52 var certificateTableDataDirectoryOffset = br.ReadUInt16() switch
53 {
54 IMAGE_NT_OPTIONAL_HDR32_MAGIC => peSignatureOffset + 24 + (64 + 4 * 8) + 8 * 4,
55 IMAGE_NT_OPTIONAL_HDR64_MAGIC => peSignatureOffset + 24 + (64 + 4 * 8 + 16) + 8 * 4,
56 _ => throw new BadImageFormatException(),
57 };
58
59 stream.Seek(certificateTableDataDirectoryOffset, SeekOrigin.Begin);
60 var certificateTableOffset = br.ReadInt32();
61 var certificateTableLength = br.ReadInt32();
62
63 stream.Seek(certificateTableOffset, SeekOrigin.Begin);
64 var dwLength = br.ReadInt32();
65 var wRevision = br.ReadInt16();
66 var wCertificateType = br.ReadInt16();
67 if (wRevision != WIN_CERT_REVISION_2_0)
68 return null;
69 if (wCertificateType != WIN_CERT_TYPE_PKCS_SIGNED_DATA)
70 return null;
71
72 var buf = br.ReadBytes(certificateTableLength - 8);
73 var cms = new SignedCms();
74 try
75 {
76 cms.Decode(buf);
77 cms.CheckSignature(false);
78 }
79 catch (CryptographicException)
80 {
81 return null;
82 }
83
84 var signerInfo = cms.SignerInfos[0];
85
86 var offsets = new int[] { checksumOffset, certificateTableDataDirectoryOffset, certificateTableOffset };
87 var lengths = new int[] { 4, 8, certificateTableLength };
88 var actualHash = ComputeHashWithSkip(stream, signerInfo.DigestAlgorithm.FriendlyName, offsets, lengths);
89 var requiredHash = DecodeASN1(cms.ContentInfo.Content, 0, 1, 1);
90
91 if (requiredHash == null || actualHash.Length != requiredHash.Length)
92 return null;
93
94 for (int i = 0; i < actualHash.Length; i++)
95 if (actualHash[i] != requiredHash[i])
96 return null;
97
98 return signerInfo.Certificate;
99 }
100
101 static byte[] ComputeHashWithSkip(Stream stream, string hashAlgorithm, int[] skipOffsets, int[] skipLengths)
102 {
103 stream.Position = 0;
104 for (int i = skipOffsets.Length - 1; i >= 0; i--)
105 {
106 stream = new IKVM.Reflection.Writer.SkipStream(stream, skipOffsets[i], skipLengths[i]);
107 }
108 using (HashAlgorithm hash = HashAlgorithm.Create(hashAlgorithm))
109 {
110 return hash.ComputeHash(stream);
111 }
112 }
113
114 static byte[] DecodeASN1(byte[] buf, params int[] indexes)
115 {
116 return DecodeASN1(buf, 0, buf.Length, 0, indexes);
117 }
118
119 static byte[] DecodeASN1(byte[] buf, int pos, int end, int depth, int[] indexes)
120 {
121 for (var index = 0; pos < end; index++)
122 {
123 var tag = buf[pos++];
124 var length = (int)buf[pos++];
125 if (length > 128)
126 {
127 var lenlen = length & 0x7F;
128 length = 0;
129 for (var i = 0; i < lenlen; i++)
130 length = length * 256 + buf[pos++];
131 }
132 if (indexes[depth] == index)
133 {
134 if (depth == indexes.Length - 1)
135 {
136 var data = new byte[length];
137 Buffer.BlockCopy(buf, pos, data, 0, length);
138 return data;
139 }
140
141 if ((tag & 0x20) == 0)
142 return null;
143
144 return DecodeASN1(buf, pos, pos + length, depth + 1, indexes);
145 }
146 pos += length;
147 }
148
149 return null;
150 }
151
152 }
153
154}
Read-only stream that presents a windowed view of another stream.
Definition SkipStream.cs:34