scripts
This commit is contained in:
@@ -0,0 +1,289 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x0200001E RID: 30
|
||||
public class AuthenticodeBase
|
||||
{
|
||||
// Token: 0x0600012D RID: 301 RVA: 0x00007CF8 File Offset: 0x00005EF8
|
||||
public AuthenticodeBase()
|
||||
{
|
||||
this.fileblock = new byte[4096];
|
||||
}
|
||||
|
||||
// Token: 0x1700003E RID: 62
|
||||
// (get) Token: 0x0600012E RID: 302 RVA: 0x00007D10 File Offset: 0x00005F10
|
||||
internal int PEOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.blockNo < 1)
|
||||
{
|
||||
this.ReadFirstBlock();
|
||||
}
|
||||
return this.peOffset;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700003F RID: 63
|
||||
// (get) Token: 0x0600012F RID: 303 RVA: 0x00007D2C File Offset: 0x00005F2C
|
||||
internal int CoffSymbolTableOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.blockNo < 1)
|
||||
{
|
||||
this.ReadFirstBlock();
|
||||
}
|
||||
return this.coffSymbolTableOffset;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000040 RID: 64
|
||||
// (get) Token: 0x06000130 RID: 304 RVA: 0x00007D48 File Offset: 0x00005F48
|
||||
internal int SecurityOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.blockNo < 1)
|
||||
{
|
||||
this.ReadFirstBlock();
|
||||
}
|
||||
return this.dirSecurityOffset;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000131 RID: 305 RVA: 0x00007D64 File Offset: 0x00005F64
|
||||
internal void Open(string filename)
|
||||
{
|
||||
if (this.fs != null)
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
this.fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
this.blockNo = 0;
|
||||
}
|
||||
|
||||
// Token: 0x06000132 RID: 306 RVA: 0x00007D90 File Offset: 0x00005F90
|
||||
internal void Close()
|
||||
{
|
||||
if (this.fs != null)
|
||||
{
|
||||
this.fs.Close();
|
||||
this.fs = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000133 RID: 307 RVA: 0x00007DB0 File Offset: 0x00005FB0
|
||||
internal void ReadFirstBlock()
|
||||
{
|
||||
int num = this.ProcessFirstBlock();
|
||||
if (num != 0)
|
||||
{
|
||||
string text = Locale.GetText("Cannot sign non PE files, e.g. .CAB or .MSI files (error {0}).", new object[] { num });
|
||||
throw new NotSupportedException(text);
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000134 RID: 308 RVA: 0x00007DEC File Offset: 0x00005FEC
|
||||
internal int ProcessFirstBlock()
|
||||
{
|
||||
if (this.fs == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
this.fs.Position = 0L;
|
||||
this.blockLength = this.fs.Read(this.fileblock, 0, this.fileblock.Length);
|
||||
this.blockNo = 1;
|
||||
if (this.blockLength < 64)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if (BitConverterLE.ToUInt16(this.fileblock, 0) != 23117)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
this.peOffset = BitConverterLE.ToInt32(this.fileblock, 60);
|
||||
if (this.peOffset > this.fileblock.Length)
|
||||
{
|
||||
string text = string.Format(Locale.GetText("Header size too big (> {0} bytes)."), this.fileblock.Length);
|
||||
throw new NotSupportedException(text);
|
||||
}
|
||||
if ((long)this.peOffset > this.fs.Length)
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
if (BitConverterLE.ToUInt32(this.fileblock, this.peOffset) != 17744U)
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
this.dirSecurityOffset = BitConverterLE.ToInt32(this.fileblock, this.peOffset + 152);
|
||||
this.dirSecuritySize = BitConverterLE.ToInt32(this.fileblock, this.peOffset + 156);
|
||||
this.coffSymbolTableOffset = BitConverterLE.ToInt32(this.fileblock, this.peOffset + 12);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Token: 0x06000135 RID: 309 RVA: 0x00007F38 File Offset: 0x00006138
|
||||
internal byte[] GetSecurityEntry()
|
||||
{
|
||||
if (this.blockNo < 1)
|
||||
{
|
||||
this.ReadFirstBlock();
|
||||
}
|
||||
if (this.dirSecuritySize > 8)
|
||||
{
|
||||
byte[] array = new byte[this.dirSecuritySize - 8];
|
||||
this.fs.Position = (long)(this.dirSecurityOffset + 8);
|
||||
this.fs.Read(array, 0, array.Length);
|
||||
return array;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Token: 0x06000136 RID: 310 RVA: 0x00007F9C File Offset: 0x0000619C
|
||||
internal byte[] GetHash(HashAlgorithm hash)
|
||||
{
|
||||
if (this.blockNo < 1)
|
||||
{
|
||||
this.ReadFirstBlock();
|
||||
}
|
||||
this.fs.Position = (long)this.blockLength;
|
||||
int num = 0;
|
||||
long num2;
|
||||
if (this.dirSecurityOffset > 0)
|
||||
{
|
||||
if (this.dirSecurityOffset < this.blockLength)
|
||||
{
|
||||
this.blockLength = this.dirSecurityOffset;
|
||||
num2 = 0L;
|
||||
}
|
||||
else
|
||||
{
|
||||
num2 = (long)(this.dirSecurityOffset - this.blockLength);
|
||||
}
|
||||
}
|
||||
else if (this.coffSymbolTableOffset > 0)
|
||||
{
|
||||
this.fileblock[this.PEOffset + 12] = 0;
|
||||
this.fileblock[this.PEOffset + 13] = 0;
|
||||
this.fileblock[this.PEOffset + 14] = 0;
|
||||
this.fileblock[this.PEOffset + 15] = 0;
|
||||
this.fileblock[this.PEOffset + 16] = 0;
|
||||
this.fileblock[this.PEOffset + 17] = 0;
|
||||
this.fileblock[this.PEOffset + 18] = 0;
|
||||
this.fileblock[this.PEOffset + 19] = 0;
|
||||
if (this.coffSymbolTableOffset < this.blockLength)
|
||||
{
|
||||
this.blockLength = this.coffSymbolTableOffset;
|
||||
num2 = 0L;
|
||||
}
|
||||
else
|
||||
{
|
||||
num2 = (long)(this.coffSymbolTableOffset - this.blockLength);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
num = (int)(this.fs.Length & 7L);
|
||||
if (num > 0)
|
||||
{
|
||||
num = 8 - num;
|
||||
}
|
||||
num2 = this.fs.Length - (long)this.blockLength;
|
||||
}
|
||||
int num3 = this.peOffset + 88;
|
||||
hash.TransformBlock(this.fileblock, 0, num3, this.fileblock, 0);
|
||||
num3 += 4;
|
||||
hash.TransformBlock(this.fileblock, num3, 60, this.fileblock, num3);
|
||||
num3 += 68;
|
||||
if (num2 == 0L)
|
||||
{
|
||||
hash.TransformFinalBlock(this.fileblock, num3, this.blockLength - num3);
|
||||
}
|
||||
else
|
||||
{
|
||||
hash.TransformBlock(this.fileblock, num3, this.blockLength - num3, this.fileblock, num3);
|
||||
long num4 = num2 >> 12;
|
||||
int num5 = (int)(num2 - (num4 << 12));
|
||||
if (num5 == 0)
|
||||
{
|
||||
num4 -= 1L;
|
||||
num5 = 4096;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
long num6 = num4;
|
||||
num4 = num6 - 1L;
|
||||
if (num6 <= 0L)
|
||||
{
|
||||
break;
|
||||
}
|
||||
this.fs.Read(this.fileblock, 0, this.fileblock.Length);
|
||||
hash.TransformBlock(this.fileblock, 0, this.fileblock.Length, this.fileblock, 0);
|
||||
}
|
||||
if (this.fs.Read(this.fileblock, 0, num5) != num5)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (num > 0)
|
||||
{
|
||||
hash.TransformBlock(this.fileblock, 0, num5, this.fileblock, 0);
|
||||
hash.TransformFinalBlock(new byte[num], 0, num);
|
||||
}
|
||||
else
|
||||
{
|
||||
hash.TransformFinalBlock(this.fileblock, 0, num5);
|
||||
}
|
||||
}
|
||||
return hash.Hash;
|
||||
}
|
||||
|
||||
// Token: 0x06000137 RID: 311 RVA: 0x00008260 File Offset: 0x00006460
|
||||
protected byte[] HashFile(string fileName, string hashName)
|
||||
{
|
||||
byte[] array;
|
||||
try
|
||||
{
|
||||
this.Open(fileName);
|
||||
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName);
|
||||
byte[] hash = this.GetHash(hashAlgorithm);
|
||||
this.Close();
|
||||
array = hash;
|
||||
}
|
||||
catch
|
||||
{
|
||||
array = null;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// Token: 0x04000076 RID: 118
|
||||
public const string spcIndirectDataContext = "1.3.6.1.4.1.311.2.1.4";
|
||||
|
||||
// Token: 0x04000077 RID: 119
|
||||
private byte[] fileblock;
|
||||
|
||||
// Token: 0x04000078 RID: 120
|
||||
private FileStream fs;
|
||||
|
||||
// Token: 0x04000079 RID: 121
|
||||
private int blockNo;
|
||||
|
||||
// Token: 0x0400007A RID: 122
|
||||
private int blockLength;
|
||||
|
||||
// Token: 0x0400007B RID: 123
|
||||
private int peOffset;
|
||||
|
||||
// Token: 0x0400007C RID: 124
|
||||
private int dirSecurityOffset;
|
||||
|
||||
// Token: 0x0400007D RID: 125
|
||||
private int dirSecuritySize;
|
||||
|
||||
// Token: 0x0400007E RID: 126
|
||||
private int coffSymbolTableOffset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,486 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security;
|
||||
using System.Security.Cryptography;
|
||||
using Mono.Security.Cryptography;
|
||||
using Mono.Security.X509;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x0200001F RID: 31
|
||||
public class AuthenticodeDeformatter : AuthenticodeBase
|
||||
{
|
||||
// Token: 0x06000138 RID: 312 RVA: 0x000082C0 File Offset: 0x000064C0
|
||||
public AuthenticodeDeformatter()
|
||||
{
|
||||
this.reason = -1;
|
||||
this.signerChain = new X509Chain();
|
||||
this.timestampChain = new X509Chain();
|
||||
}
|
||||
|
||||
// Token: 0x06000139 RID: 313 RVA: 0x000082E8 File Offset: 0x000064E8
|
||||
public AuthenticodeDeformatter(string fileName)
|
||||
: this()
|
||||
{
|
||||
this.FileName = fileName;
|
||||
}
|
||||
|
||||
// Token: 0x17000041 RID: 65
|
||||
// (get) Token: 0x0600013A RID: 314 RVA: 0x000082F8 File Offset: 0x000064F8
|
||||
// (set) Token: 0x0600013B RID: 315 RVA: 0x00008300 File Offset: 0x00006500
|
||||
public string FileName
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.filename;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.Reset();
|
||||
try
|
||||
{
|
||||
this.CheckSignature(value);
|
||||
}
|
||||
catch (SecurityException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
this.reason = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000042 RID: 66
|
||||
// (get) Token: 0x0600013C RID: 316 RVA: 0x0000836C File Offset: 0x0000656C
|
||||
public byte[] Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.signedHash == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return (byte[])this.signedHash.Value.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000043 RID: 67
|
||||
// (get) Token: 0x0600013D RID: 317 RVA: 0x0000839C File Offset: 0x0000659C
|
||||
public int Reason
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.reason == -1)
|
||||
{
|
||||
this.IsTrusted();
|
||||
}
|
||||
return this.reason;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x0600013E RID: 318 RVA: 0x000083B8 File Offset: 0x000065B8
|
||||
public bool IsTrusted()
|
||||
{
|
||||
if (this.entry == null)
|
||||
{
|
||||
this.reason = 1;
|
||||
return false;
|
||||
}
|
||||
if (this.signingCertificate == null)
|
||||
{
|
||||
this.reason = 7;
|
||||
return false;
|
||||
}
|
||||
if (this.signerChain.Root == null || !this.trustedRoot)
|
||||
{
|
||||
this.reason = 6;
|
||||
return false;
|
||||
}
|
||||
if (this.timestamp != DateTime.MinValue)
|
||||
{
|
||||
if (this.timestampChain.Root == null || !this.trustedTimestampRoot)
|
||||
{
|
||||
this.reason = 6;
|
||||
return false;
|
||||
}
|
||||
if (!this.signingCertificate.WasCurrent(this.Timestamp))
|
||||
{
|
||||
this.reason = 4;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!this.signingCertificate.IsCurrent)
|
||||
{
|
||||
this.reason = 8;
|
||||
return false;
|
||||
}
|
||||
if (this.reason == -1)
|
||||
{
|
||||
this.reason = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Token: 0x17000044 RID: 68
|
||||
// (get) Token: 0x0600013F RID: 319 RVA: 0x0000849C File Offset: 0x0000669C
|
||||
public byte[] Signature
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.entry == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return (byte[])this.entry.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000045 RID: 69
|
||||
// (get) Token: 0x06000140 RID: 320 RVA: 0x000084BC File Offset: 0x000066BC
|
||||
public DateTime Timestamp
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000046 RID: 70
|
||||
// (get) Token: 0x06000141 RID: 321 RVA: 0x000084C4 File Offset: 0x000066C4
|
||||
public X509CertificateCollection Certificates
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.coll;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000047 RID: 71
|
||||
// (get) Token: 0x06000142 RID: 322 RVA: 0x000084CC File Offset: 0x000066CC
|
||||
public X509Certificate SigningCertificate
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.signingCertificate;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000143 RID: 323 RVA: 0x000084D4 File Offset: 0x000066D4
|
||||
private bool CheckSignature(string fileName)
|
||||
{
|
||||
this.filename = fileName;
|
||||
base.Open(this.filename);
|
||||
this.entry = base.GetSecurityEntry();
|
||||
if (this.entry == null)
|
||||
{
|
||||
this.reason = 1;
|
||||
base.Close();
|
||||
return false;
|
||||
}
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo(this.entry);
|
||||
if (contentInfo.ContentType != "1.2.840.113549.1.7.2")
|
||||
{
|
||||
base.Close();
|
||||
return false;
|
||||
}
|
||||
PKCS7.SignedData signedData = new PKCS7.SignedData(contentInfo.Content);
|
||||
if (signedData.ContentInfo.ContentType != "1.3.6.1.4.1.311.2.1.4")
|
||||
{
|
||||
base.Close();
|
||||
return false;
|
||||
}
|
||||
this.coll = signedData.Certificates;
|
||||
ASN1 content = signedData.ContentInfo.Content;
|
||||
this.signedHash = content[0][1][1];
|
||||
int length = this.signedHash.Length;
|
||||
HashAlgorithm hashAlgorithm;
|
||||
if (length != 16)
|
||||
{
|
||||
if (length != 20)
|
||||
{
|
||||
this.reason = 5;
|
||||
base.Close();
|
||||
return false;
|
||||
}
|
||||
hashAlgorithm = HashAlgorithm.Create("SHA1");
|
||||
this.hash = base.GetHash(hashAlgorithm);
|
||||
}
|
||||
else
|
||||
{
|
||||
hashAlgorithm = HashAlgorithm.Create("MD5");
|
||||
this.hash = base.GetHash(hashAlgorithm);
|
||||
}
|
||||
base.Close();
|
||||
if (!this.signedHash.CompareValue(this.hash))
|
||||
{
|
||||
this.reason = 2;
|
||||
}
|
||||
byte[] value = content[0].Value;
|
||||
hashAlgorithm.Initialize();
|
||||
byte[] array = hashAlgorithm.ComputeHash(value);
|
||||
bool flag = this.VerifySignature(signedData, array, hashAlgorithm);
|
||||
return flag && this.reason == 0;
|
||||
}
|
||||
|
||||
// Token: 0x06000144 RID: 324 RVA: 0x00008674 File Offset: 0x00006874
|
||||
private bool CompareIssuerSerial(string issuer, byte[] serial, X509Certificate x509)
|
||||
{
|
||||
if (issuer != x509.IssuerName)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (serial.Length != x509.SerialNumber.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int num = serial.Length;
|
||||
for (int i = 0; i < serial.Length; i++)
|
||||
{
|
||||
if (serial[i] != x509.SerialNumber[--num])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Token: 0x06000145 RID: 325 RVA: 0x000086D8 File Offset: 0x000068D8
|
||||
private bool VerifySignature(PKCS7.SignedData sd, byte[] calculatedMessageDigest, HashAlgorithm ha)
|
||||
{
|
||||
string text = null;
|
||||
ASN1 asn = null;
|
||||
int i = 0;
|
||||
while (i < sd.SignerInfo.AuthenticatedAttributes.Count)
|
||||
{
|
||||
ASN1 asn2 = (ASN1)sd.SignerInfo.AuthenticatedAttributes[i];
|
||||
string text2 = ASN1Convert.ToOid(asn2[0]);
|
||||
string text3 = text2;
|
||||
switch (text3)
|
||||
{
|
||||
case "1.2.840.113549.1.9.3":
|
||||
text = ASN1Convert.ToOid(asn2[1][0]);
|
||||
break;
|
||||
case "1.2.840.113549.1.9.4":
|
||||
asn = asn2[1][0];
|
||||
break;
|
||||
}
|
||||
IL_F1:
|
||||
i++;
|
||||
continue;
|
||||
goto IL_F1;
|
||||
}
|
||||
if (text != "1.3.6.1.4.1.311.2.1.4")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (asn == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!asn.CompareValue(calculatedMessageDigest))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string text4 = CryptoConfig.MapNameToOID(ha.ToString());
|
||||
ASN1 asn3 = new ASN1(49);
|
||||
foreach (object obj in sd.SignerInfo.AuthenticatedAttributes)
|
||||
{
|
||||
ASN1 asn4 = (ASN1)obj;
|
||||
asn3.Add(asn4);
|
||||
}
|
||||
ha.Initialize();
|
||||
byte[] array = ha.ComputeHash(asn3.GetBytes());
|
||||
byte[] signature = sd.SignerInfo.Signature;
|
||||
string issuerName = sd.SignerInfo.IssuerName;
|
||||
byte[] serialNumber = sd.SignerInfo.SerialNumber;
|
||||
foreach (X509Certificate x509Certificate in this.coll)
|
||||
{
|
||||
if (this.CompareIssuerSerial(issuerName, serialNumber, x509Certificate) && x509Certificate.PublicKey.Length > signature.Length >> 3)
|
||||
{
|
||||
this.signingCertificate = x509Certificate;
|
||||
RSACryptoServiceProvider rsacryptoServiceProvider = (RSACryptoServiceProvider)x509Certificate.RSA;
|
||||
if (rsacryptoServiceProvider.VerifyHash(array, text4, signature))
|
||||
{
|
||||
this.signerChain.LoadCertificates(this.coll);
|
||||
this.trustedRoot = this.signerChain.Build(x509Certificate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sd.SignerInfo.UnauthenticatedAttributes.Count == 0)
|
||||
{
|
||||
this.trustedTimestampRoot = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
int j = 0;
|
||||
while (j < sd.SignerInfo.UnauthenticatedAttributes.Count)
|
||||
{
|
||||
ASN1 asn5 = (ASN1)sd.SignerInfo.UnauthenticatedAttributes[j];
|
||||
string text5 = ASN1Convert.ToOid(asn5[0]);
|
||||
string text3 = text5;
|
||||
if (text3 != null)
|
||||
{
|
||||
if (AuthenticodeDeformatter.<>f__switch$map2 == null)
|
||||
{
|
||||
AuthenticodeDeformatter.<>f__switch$map2 = new Dictionary<string, int>(1) { { "1.2.840.113549.1.9.6", 0 } };
|
||||
}
|
||||
int num;
|
||||
if (AuthenticodeDeformatter.<>f__switch$map2.TryGetValue(text3, out num))
|
||||
{
|
||||
if (num == 0)
|
||||
{
|
||||
PKCS7.SignerInfo signerInfo = new PKCS7.SignerInfo(asn5[1]);
|
||||
this.trustedTimestampRoot = this.VerifyCounterSignature(signerInfo, signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
IL_35D:
|
||||
j++;
|
||||
continue;
|
||||
goto IL_35D;
|
||||
}
|
||||
}
|
||||
return this.trustedRoot && this.trustedTimestampRoot;
|
||||
}
|
||||
|
||||
// Token: 0x06000146 RID: 326 RVA: 0x00008AA8 File Offset: 0x00006CA8
|
||||
private bool VerifyCounterSignature(PKCS7.SignerInfo cs, byte[] signature)
|
||||
{
|
||||
if (cs.Version != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string text = null;
|
||||
ASN1 asn = null;
|
||||
int i = 0;
|
||||
while (i < cs.AuthenticatedAttributes.Count)
|
||||
{
|
||||
ASN1 asn2 = (ASN1)cs.AuthenticatedAttributes[i];
|
||||
string text2 = ASN1Convert.ToOid(asn2[0]);
|
||||
string text3 = text2;
|
||||
switch (text3)
|
||||
{
|
||||
case "1.2.840.113549.1.9.3":
|
||||
text = ASN1Convert.ToOid(asn2[1][0]);
|
||||
break;
|
||||
case "1.2.840.113549.1.9.4":
|
||||
asn = asn2[1][0];
|
||||
break;
|
||||
case "1.2.840.113549.1.9.5":
|
||||
this.timestamp = ASN1Convert.ToDateTime(asn2[1][0]);
|
||||
break;
|
||||
}
|
||||
IL_FC:
|
||||
i++;
|
||||
continue;
|
||||
goto IL_FC;
|
||||
}
|
||||
if (text != "1.2.840.113549.1.7.1")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (asn == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string text4 = null;
|
||||
int length = asn.Length;
|
||||
if (length != 16)
|
||||
{
|
||||
if (length == 20)
|
||||
{
|
||||
text4 = "SHA1";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
text4 = "MD5";
|
||||
}
|
||||
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(text4);
|
||||
if (!asn.CompareValue(hashAlgorithm.ComputeHash(signature)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
byte[] signature2 = cs.Signature;
|
||||
ASN1 asn3 = new ASN1(49);
|
||||
foreach (object obj in cs.AuthenticatedAttributes)
|
||||
{
|
||||
ASN1 asn4 = (ASN1)obj;
|
||||
asn3.Add(asn4);
|
||||
}
|
||||
byte[] array = hashAlgorithm.ComputeHash(asn3.GetBytes());
|
||||
string issuerName = cs.IssuerName;
|
||||
byte[] serialNumber = cs.SerialNumber;
|
||||
foreach (X509Certificate x509Certificate in this.coll)
|
||||
{
|
||||
if (this.CompareIssuerSerial(issuerName, serialNumber, x509Certificate) && x509Certificate.PublicKey.Length > signature2.Length)
|
||||
{
|
||||
RSACryptoServiceProvider rsacryptoServiceProvider = (RSACryptoServiceProvider)x509Certificate.RSA;
|
||||
RSAManaged rsamanaged = new RSAManaged();
|
||||
rsamanaged.ImportParameters(rsacryptoServiceProvider.ExportParameters(false));
|
||||
if (PKCS1.Verify_v15(rsamanaged, hashAlgorithm, array, signature2, true))
|
||||
{
|
||||
this.timestampChain.LoadCertificates(this.coll);
|
||||
return this.timestampChain.Build(x509Certificate);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Token: 0x06000147 RID: 327 RVA: 0x00008DB4 File Offset: 0x00006FB4
|
||||
private void Reset()
|
||||
{
|
||||
this.filename = null;
|
||||
this.entry = null;
|
||||
this.hash = null;
|
||||
this.signedHash = null;
|
||||
this.signingCertificate = null;
|
||||
this.reason = -1;
|
||||
this.trustedRoot = false;
|
||||
this.trustedTimestampRoot = false;
|
||||
this.signerChain.Reset();
|
||||
this.timestampChain.Reset();
|
||||
this.timestamp = DateTime.MinValue;
|
||||
}
|
||||
|
||||
// Token: 0x0400007F RID: 127
|
||||
private string filename;
|
||||
|
||||
// Token: 0x04000080 RID: 128
|
||||
private byte[] hash;
|
||||
|
||||
// Token: 0x04000081 RID: 129
|
||||
private X509CertificateCollection coll;
|
||||
|
||||
// Token: 0x04000082 RID: 130
|
||||
private ASN1 signedHash;
|
||||
|
||||
// Token: 0x04000083 RID: 131
|
||||
private DateTime timestamp;
|
||||
|
||||
// Token: 0x04000084 RID: 132
|
||||
private X509Certificate signingCertificate;
|
||||
|
||||
// Token: 0x04000085 RID: 133
|
||||
private int reason;
|
||||
|
||||
// Token: 0x04000086 RID: 134
|
||||
private bool trustedRoot;
|
||||
|
||||
// Token: 0x04000087 RID: 135
|
||||
private bool trustedTimestampRoot;
|
||||
|
||||
// Token: 0x04000088 RID: 136
|
||||
private byte[] entry;
|
||||
|
||||
// Token: 0x04000089 RID: 137
|
||||
private X509Chain signerChain;
|
||||
|
||||
// Token: 0x0400008A RID: 138
|
||||
private X509Chain timestampChain;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,464 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Mono.Security.X509;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x02000020 RID: 32
|
||||
public class AuthenticodeFormatter : AuthenticodeBase
|
||||
{
|
||||
// Token: 0x06000148 RID: 328 RVA: 0x00008E1C File Offset: 0x0000701C
|
||||
public AuthenticodeFormatter()
|
||||
{
|
||||
this.certs = new X509CertificateCollection();
|
||||
this.crls = new ArrayList();
|
||||
this.authority = Authority.Maximum;
|
||||
this.pkcs7 = new PKCS7.SignedData();
|
||||
}
|
||||
|
||||
// Token: 0x17000048 RID: 72
|
||||
// (get) Token: 0x0600014A RID: 330 RVA: 0x00008E74 File Offset: 0x00007074
|
||||
// (set) Token: 0x0600014B RID: 331 RVA: 0x00008E7C File Offset: 0x0000707C
|
||||
public Authority Authority
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.authority;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.authority = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000049 RID: 73
|
||||
// (get) Token: 0x0600014C RID: 332 RVA: 0x00008E88 File Offset: 0x00007088
|
||||
public X509CertificateCollection Certificates
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.certs;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004A RID: 74
|
||||
// (get) Token: 0x0600014D RID: 333 RVA: 0x00008E90 File Offset: 0x00007090
|
||||
public ArrayList Crl
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.crls;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004B RID: 75
|
||||
// (get) Token: 0x0600014E RID: 334 RVA: 0x00008E98 File Offset: 0x00007098
|
||||
// (set) Token: 0x0600014F RID: 335 RVA: 0x00008EB8 File Offset: 0x000070B8
|
||||
public string Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.hash == null)
|
||||
{
|
||||
this.hash = "MD5";
|
||||
}
|
||||
return this.hash;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException("Hash");
|
||||
}
|
||||
string text = value.ToUpper(CultureInfo.InvariantCulture);
|
||||
string text2 = text;
|
||||
if (text2 != null)
|
||||
{
|
||||
if (AuthenticodeFormatter.<>f__switch$map4 == null)
|
||||
{
|
||||
AuthenticodeFormatter.<>f__switch$map4 = new Dictionary<string, int>(2)
|
||||
{
|
||||
{ "MD5", 0 },
|
||||
{ "SHA1", 0 }
|
||||
};
|
||||
}
|
||||
int num;
|
||||
if (AuthenticodeFormatter.<>f__switch$map4.TryGetValue(text2, out num))
|
||||
{
|
||||
if (num == 0)
|
||||
{
|
||||
this.hash = text;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("Invalid Authenticode hash algorithm");
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004C RID: 76
|
||||
// (get) Token: 0x06000150 RID: 336 RVA: 0x00008F50 File Offset: 0x00007150
|
||||
// (set) Token: 0x06000151 RID: 337 RVA: 0x00008F58 File Offset: 0x00007158
|
||||
public RSA RSA
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.rsa;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.rsa = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004D RID: 77
|
||||
// (get) Token: 0x06000152 RID: 338 RVA: 0x00008F64 File Offset: 0x00007164
|
||||
// (set) Token: 0x06000153 RID: 339 RVA: 0x00008F6C File Offset: 0x0000716C
|
||||
public Uri TimestampUrl
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.timestamp;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.timestamp = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004E RID: 78
|
||||
// (get) Token: 0x06000154 RID: 340 RVA: 0x00008F78 File Offset: 0x00007178
|
||||
// (set) Token: 0x06000155 RID: 341 RVA: 0x00008F80 File Offset: 0x00007180
|
||||
public string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.description;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.description = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x1700004F RID: 79
|
||||
// (get) Token: 0x06000156 RID: 342 RVA: 0x00008F8C File Offset: 0x0000718C
|
||||
// (set) Token: 0x06000157 RID: 343 RVA: 0x00008F94 File Offset: 0x00007194
|
||||
public Uri Url
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.url;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.url = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000158 RID: 344 RVA: 0x00008FA0 File Offset: 0x000071A0
|
||||
private ASN1 AlgorithmIdentifier(string oid)
|
||||
{
|
||||
ASN1 asn = new ASN1(48);
|
||||
asn.Add(ASN1Convert.FromOid(oid));
|
||||
asn.Add(new ASN1(5));
|
||||
return asn;
|
||||
}
|
||||
|
||||
// Token: 0x06000159 RID: 345 RVA: 0x00008FD0 File Offset: 0x000071D0
|
||||
private ASN1 Attribute(string oid, ASN1 value)
|
||||
{
|
||||
ASN1 asn = new ASN1(48);
|
||||
asn.Add(ASN1Convert.FromOid(oid));
|
||||
ASN1 asn2 = asn.Add(new ASN1(49));
|
||||
asn2.Add(value);
|
||||
return asn;
|
||||
}
|
||||
|
||||
// Token: 0x0600015A RID: 346 RVA: 0x0000900C File Offset: 0x0000720C
|
||||
private ASN1 Opus(string description, string url)
|
||||
{
|
||||
ASN1 asn = new ASN1(48);
|
||||
if (description != null)
|
||||
{
|
||||
ASN1 asn2 = asn.Add(new ASN1(160));
|
||||
asn2.Add(new ASN1(128, Encoding.BigEndianUnicode.GetBytes(description)));
|
||||
}
|
||||
if (url != null)
|
||||
{
|
||||
ASN1 asn3 = asn.Add(new ASN1(161));
|
||||
asn3.Add(new ASN1(128, Encoding.ASCII.GetBytes(url)));
|
||||
}
|
||||
return asn;
|
||||
}
|
||||
|
||||
// Token: 0x0600015B RID: 347 RVA: 0x00009088 File Offset: 0x00007288
|
||||
private byte[] Header(byte[] fileHash, string hashAlgorithm)
|
||||
{
|
||||
string text = CryptoConfig.MapNameToOID(hashAlgorithm);
|
||||
ASN1 asn = new ASN1(48);
|
||||
ASN1 asn2 = asn.Add(new ASN1(48));
|
||||
asn2.Add(ASN1Convert.FromOid("1.3.6.1.4.1.311.2.1.15"));
|
||||
asn2.Add(new ASN1(48, AuthenticodeFormatter.obsolete));
|
||||
ASN1 asn3 = asn.Add(new ASN1(48));
|
||||
asn3.Add(this.AlgorithmIdentifier(text));
|
||||
asn3.Add(new ASN1(4, fileHash));
|
||||
this.pkcs7.HashName = hashAlgorithm;
|
||||
this.pkcs7.Certificates.AddRange(this.certs);
|
||||
this.pkcs7.ContentInfo.ContentType = "1.3.6.1.4.1.311.2.1.4";
|
||||
this.pkcs7.ContentInfo.Content.Add(asn);
|
||||
this.pkcs7.SignerInfo.Certificate = this.certs[0];
|
||||
this.pkcs7.SignerInfo.Key = this.rsa;
|
||||
ASN1 asn4;
|
||||
if (this.url == null)
|
||||
{
|
||||
asn4 = this.Attribute("1.3.6.1.4.1.311.2.1.12", this.Opus(this.description, null));
|
||||
}
|
||||
else
|
||||
{
|
||||
asn4 = this.Attribute("1.3.6.1.4.1.311.2.1.12", this.Opus(this.description, this.url.ToString()));
|
||||
}
|
||||
this.pkcs7.SignerInfo.AuthenticatedAttributes.Add(asn4);
|
||||
this.pkcs7.GetASN1();
|
||||
return this.pkcs7.SignerInfo.Signature;
|
||||
}
|
||||
|
||||
// Token: 0x0600015C RID: 348 RVA: 0x0000920C File Offset: 0x0000740C
|
||||
public ASN1 TimestampRequest(byte[] signature)
|
||||
{
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo("1.2.840.113549.1.7.1");
|
||||
contentInfo.Content.Add(new ASN1(4, signature));
|
||||
return PKCS7.AlgorithmIdentifier("1.3.6.1.4.1.311.3.2.1", contentInfo.ASN1);
|
||||
}
|
||||
|
||||
// Token: 0x0600015D RID: 349 RVA: 0x00009248 File Offset: 0x00007448
|
||||
public void ProcessTimestamp(byte[] response)
|
||||
{
|
||||
ASN1 asn = new ASN1(Convert.FromBase64String(Encoding.ASCII.GetString(response)));
|
||||
for (int i = 0; i < asn[1][0][3].Count; i++)
|
||||
{
|
||||
this.pkcs7.Certificates.Add(new X509Certificate(asn[1][0][3][i].GetBytes()));
|
||||
}
|
||||
this.pkcs7.SignerInfo.UnauthenticatedAttributes.Add(this.Attribute("1.2.840.113549.1.9.6", asn[1][0][4][0]));
|
||||
}
|
||||
|
||||
// Token: 0x0600015E RID: 350 RVA: 0x00009304 File Offset: 0x00007504
|
||||
private byte[] Timestamp(byte[] signature)
|
||||
{
|
||||
ASN1 asn = this.TimestampRequest(signature);
|
||||
WebClient webClient = new WebClient();
|
||||
webClient.Headers.Add("Content-Type", "application/octet-stream");
|
||||
webClient.Headers.Add("Accept", "application/octet-stream");
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(Convert.ToBase64String(asn.GetBytes()));
|
||||
return webClient.UploadData(this.timestamp.ToString(), bytes);
|
||||
}
|
||||
|
||||
// Token: 0x0600015F RID: 351 RVA: 0x00009374 File Offset: 0x00007574
|
||||
private bool Save(string fileName, byte[] asn)
|
||||
{
|
||||
File.Copy(fileName, fileName + ".bak", true);
|
||||
using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
|
||||
{
|
||||
int num;
|
||||
if (base.SecurityOffset > 0)
|
||||
{
|
||||
num = base.SecurityOffset;
|
||||
}
|
||||
else if (base.CoffSymbolTableOffset > 0)
|
||||
{
|
||||
fileStream.Seek((long)(base.PEOffset + 12), SeekOrigin.Begin);
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
fileStream.WriteByte(0);
|
||||
}
|
||||
num = base.CoffSymbolTableOffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
num = (int)fileStream.Length;
|
||||
}
|
||||
int num2 = num & 7;
|
||||
if (num2 > 0)
|
||||
{
|
||||
num2 = 8 - num2;
|
||||
}
|
||||
byte[] array = BitConverterLE.GetBytes(num + num2);
|
||||
fileStream.Seek((long)(base.PEOffset + 152), SeekOrigin.Begin);
|
||||
fileStream.Write(array, 0, 4);
|
||||
int num3 = asn.Length + 8;
|
||||
int num4 = num3 & 7;
|
||||
if (num4 > 0)
|
||||
{
|
||||
num4 = 8 - num4;
|
||||
}
|
||||
array = BitConverterLE.GetBytes(num3 + num4);
|
||||
fileStream.Seek((long)(base.PEOffset + 156), SeekOrigin.Begin);
|
||||
fileStream.Write(array, 0, 4);
|
||||
fileStream.Seek((long)num, SeekOrigin.Begin);
|
||||
if (num2 > 0)
|
||||
{
|
||||
byte[] array2 = new byte[num2];
|
||||
fileStream.Write(array2, 0, array2.Length);
|
||||
}
|
||||
fileStream.Write(array, 0, array.Length);
|
||||
array = BitConverterLE.GetBytes(131584);
|
||||
fileStream.Write(array, 0, array.Length);
|
||||
fileStream.Write(asn, 0, asn.Length);
|
||||
if (num4 > 0)
|
||||
{
|
||||
byte[] array3 = new byte[num4];
|
||||
fileStream.Write(array3, 0, array3.Length);
|
||||
}
|
||||
fileStream.Close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Token: 0x06000160 RID: 352 RVA: 0x00009528 File Offset: 0x00007728
|
||||
public bool Sign(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
base.Open(fileName);
|
||||
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(this.Hash);
|
||||
byte[] array = base.GetHash(hashAlgorithm);
|
||||
byte[] array2 = this.Header(array, this.Hash);
|
||||
if (this.timestamp != null)
|
||||
{
|
||||
byte[] array3 = this.Timestamp(array2);
|
||||
this.ProcessTimestamp(array3);
|
||||
}
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo("1.2.840.113549.1.7.2");
|
||||
contentInfo.Content.Add(this.pkcs7.ASN1);
|
||||
this.authenticode = contentInfo.ASN1;
|
||||
base.Close();
|
||||
return this.Save(fileName, this.authenticode.GetBytes());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Token: 0x06000161 RID: 353 RVA: 0x00009604 File Offset: 0x00007804
|
||||
public bool Timestamp(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
AuthenticodeDeformatter authenticodeDeformatter = new AuthenticodeDeformatter(fileName);
|
||||
byte[] signature = authenticodeDeformatter.Signature;
|
||||
if (signature != null)
|
||||
{
|
||||
base.Open(fileName);
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo(signature);
|
||||
this.pkcs7 = new PKCS7.SignedData(contentInfo.Content);
|
||||
byte[] array = this.Timestamp(this.pkcs7.SignerInfo.Signature);
|
||||
ASN1 asn = new ASN1(Convert.FromBase64String(Encoding.ASCII.GetString(array)));
|
||||
ASN1 asn2 = new ASN1(signature);
|
||||
ASN1 asn3 = asn2.Element(1, 160);
|
||||
if (asn3 == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ASN1 asn4 = asn3.Element(0, 48);
|
||||
if (asn4 == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ASN1 asn5 = asn4.Element(3, 160);
|
||||
if (asn5 == null)
|
||||
{
|
||||
asn5 = new ASN1(160);
|
||||
asn4.Add(asn5);
|
||||
}
|
||||
for (int i = 0; i < asn[1][0][3].Count; i++)
|
||||
{
|
||||
asn5.Add(asn[1][0][3][i]);
|
||||
}
|
||||
ASN1 asn6 = asn4[asn4.Count - 1];
|
||||
ASN1 asn7 = asn6[0];
|
||||
ASN1 asn8 = asn7[asn7.Count - 1];
|
||||
if (asn8.Tag != 161)
|
||||
{
|
||||
asn8 = new ASN1(161);
|
||||
asn7.Add(asn8);
|
||||
}
|
||||
asn8.Add(this.Attribute("1.2.840.113549.1.9.6", asn[1][0][4][0]));
|
||||
return this.Save(fileName, asn2.GetBytes());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Token: 0x0400008E RID: 142
|
||||
private const string signedData = "1.2.840.113549.1.7.2";
|
||||
|
||||
// Token: 0x0400008F RID: 143
|
||||
private const string countersignature = "1.2.840.113549.1.9.6";
|
||||
|
||||
// Token: 0x04000090 RID: 144
|
||||
private const string spcStatementType = "1.3.6.1.4.1.311.2.1.11";
|
||||
|
||||
// Token: 0x04000091 RID: 145
|
||||
private const string spcSpOpusInfo = "1.3.6.1.4.1.311.2.1.12";
|
||||
|
||||
// Token: 0x04000092 RID: 146
|
||||
private const string spcPelmageData = "1.3.6.1.4.1.311.2.1.15";
|
||||
|
||||
// Token: 0x04000093 RID: 147
|
||||
private const string commercialCodeSigning = "1.3.6.1.4.1.311.2.1.22";
|
||||
|
||||
// Token: 0x04000094 RID: 148
|
||||
private const string timestampCountersignature = "1.3.6.1.4.1.311.3.2.1";
|
||||
|
||||
// Token: 0x04000095 RID: 149
|
||||
private Authority authority;
|
||||
|
||||
// Token: 0x04000096 RID: 150
|
||||
private X509CertificateCollection certs;
|
||||
|
||||
// Token: 0x04000097 RID: 151
|
||||
private ArrayList crls;
|
||||
|
||||
// Token: 0x04000098 RID: 152
|
||||
private string hash;
|
||||
|
||||
// Token: 0x04000099 RID: 153
|
||||
private RSA rsa;
|
||||
|
||||
// Token: 0x0400009A RID: 154
|
||||
private Uri timestamp;
|
||||
|
||||
// Token: 0x0400009B RID: 155
|
||||
private ASN1 authenticode;
|
||||
|
||||
// Token: 0x0400009C RID: 156
|
||||
private PKCS7.SignedData pkcs7;
|
||||
|
||||
// Token: 0x0400009D RID: 157
|
||||
private string description;
|
||||
|
||||
// Token: 0x0400009E RID: 158
|
||||
private Uri url;
|
||||
|
||||
// Token: 0x0400009F RID: 159
|
||||
private static byte[] obsolete = new byte[]
|
||||
{
|
||||
3, 1, 0, 160, 32, 162, 30, 128, 28, 0,
|
||||
60, 0, 60, 0, 60, 0, 79, 0, 98, 0,
|
||||
115, 0, 111, 0, 108, 0, 101, 0, 116, 0,
|
||||
101, 0, 62, 0, 62, 0, 62
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x0200001D RID: 29
|
||||
public enum Authority
|
||||
{
|
||||
// Token: 0x04000073 RID: 115
|
||||
Individual,
|
||||
// Token: 0x04000074 RID: 116
|
||||
Commercial,
|
||||
// Token: 0x04000075 RID: 117
|
||||
Maximum
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Mono.Security.Cryptography;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x02000022 RID: 34
|
||||
public class PrivateKey
|
||||
{
|
||||
// Token: 0x06000169 RID: 361 RVA: 0x00009A30 File Offset: 0x00007C30
|
||||
public PrivateKey()
|
||||
{
|
||||
this.keyType = 2;
|
||||
}
|
||||
|
||||
// Token: 0x0600016A RID: 362 RVA: 0x00009A40 File Offset: 0x00007C40
|
||||
public PrivateKey(byte[] data, string password)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
throw new ArgumentNullException("data");
|
||||
}
|
||||
if (!this.Decode(data, password))
|
||||
{
|
||||
throw new CryptographicException(Locale.GetText("Invalid data and/or password"));
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000052 RID: 82
|
||||
// (get) Token: 0x0600016B RID: 363 RVA: 0x00009A84 File Offset: 0x00007C84
|
||||
public bool Encrypted
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.encrypted;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000053 RID: 83
|
||||
// (get) Token: 0x0600016C RID: 364 RVA: 0x00009A8C File Offset: 0x00007C8C
|
||||
// (set) Token: 0x0600016D RID: 365 RVA: 0x00009A94 File Offset: 0x00007C94
|
||||
public int KeyType
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.keyType;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.keyType = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000054 RID: 84
|
||||
// (get) Token: 0x0600016E RID: 366 RVA: 0x00009AA0 File Offset: 0x00007CA0
|
||||
// (set) Token: 0x0600016F RID: 367 RVA: 0x00009AA8 File Offset: 0x00007CA8
|
||||
public RSA RSA
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.rsa;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.rsa = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000055 RID: 85
|
||||
// (get) Token: 0x06000170 RID: 368 RVA: 0x00009AB4 File Offset: 0x00007CB4
|
||||
// (set) Token: 0x06000171 RID: 369 RVA: 0x00009AD0 File Offset: 0x00007CD0
|
||||
public bool Weak
|
||||
{
|
||||
get
|
||||
{
|
||||
return !this.encrypted || this.weak;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.weak = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000172 RID: 370 RVA: 0x00009ADC File Offset: 0x00007CDC
|
||||
private byte[] DeriveKey(byte[] salt, string password)
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(password);
|
||||
SHA1 sha = SHA1.Create();
|
||||
sha.TransformBlock(salt, 0, salt.Length, salt, 0);
|
||||
sha.TransformFinalBlock(bytes, 0, bytes.Length);
|
||||
byte[] array = new byte[16];
|
||||
Buffer.BlockCopy(sha.Hash, 0, array, 0, 16);
|
||||
sha.Clear();
|
||||
Array.Clear(bytes, 0, bytes.Length);
|
||||
return array;
|
||||
}
|
||||
|
||||
// Token: 0x06000173 RID: 371 RVA: 0x00009B40 File Offset: 0x00007D40
|
||||
private bool Decode(byte[] pvk, string password)
|
||||
{
|
||||
if (BitConverterLE.ToUInt32(pvk, 0) != 2964713758U)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (BitConverterLE.ToUInt32(pvk, 4) != 0U)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.keyType = BitConverterLE.ToInt32(pvk, 8);
|
||||
this.encrypted = BitConverterLE.ToUInt32(pvk, 12) == 1U;
|
||||
int num = BitConverterLE.ToInt32(pvk, 16);
|
||||
int num2 = BitConverterLE.ToInt32(pvk, 20);
|
||||
byte[] array = new byte[num2];
|
||||
Buffer.BlockCopy(pvk, 24 + num, array, 0, num2);
|
||||
if (num > 0)
|
||||
{
|
||||
if (password == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
byte[] array2 = new byte[num];
|
||||
Buffer.BlockCopy(pvk, 24, array2, 0, num);
|
||||
byte[] array3 = this.DeriveKey(array2, password);
|
||||
RC4 rc = RC4.Create();
|
||||
ICryptoTransform cryptoTransform = rc.CreateDecryptor(array3, null);
|
||||
cryptoTransform.TransformBlock(array, 8, array.Length - 8, array, 8);
|
||||
try
|
||||
{
|
||||
this.rsa = CryptoConvert.FromCapiPrivateKeyBlob(array);
|
||||
this.weak = false;
|
||||
}
|
||||
catch (CryptographicException)
|
||||
{
|
||||
this.weak = true;
|
||||
Buffer.BlockCopy(pvk, 24 + num, array, 0, num2);
|
||||
Array.Clear(array3, 5, 11);
|
||||
RC4 rc2 = RC4.Create();
|
||||
cryptoTransform = rc2.CreateDecryptor(array3, null);
|
||||
cryptoTransform.TransformBlock(array, 8, array.Length - 8, array, 8);
|
||||
this.rsa = CryptoConvert.FromCapiPrivateKeyBlob(array);
|
||||
}
|
||||
Array.Clear(array3, 0, array3.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.weak = true;
|
||||
this.rsa = CryptoConvert.FromCapiPrivateKeyBlob(array);
|
||||
Array.Clear(array, 0, array.Length);
|
||||
}
|
||||
Array.Clear(pvk, 0, pvk.Length);
|
||||
return this.rsa != null;
|
||||
}
|
||||
|
||||
// Token: 0x06000174 RID: 372 RVA: 0x00009CD0 File Offset: 0x00007ED0
|
||||
public void Save(string filename)
|
||||
{
|
||||
this.Save(filename, null);
|
||||
}
|
||||
|
||||
// Token: 0x06000175 RID: 373 RVA: 0x00009CDC File Offset: 0x00007EDC
|
||||
public void Save(string filename, string password)
|
||||
{
|
||||
if (filename == null)
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
byte[] array = null;
|
||||
FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write);
|
||||
try
|
||||
{
|
||||
byte[] array2 = new byte[4];
|
||||
byte[] array3 = BitConverterLE.GetBytes(2964713758U);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
fileStream.Write(array2, 0, 4);
|
||||
array3 = BitConverterLE.GetBytes(this.keyType);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
this.encrypted = password != null;
|
||||
array = CryptoConvert.ToCapiPrivateKeyBlob(this.rsa);
|
||||
if (this.encrypted)
|
||||
{
|
||||
array3 = BitConverterLE.GetBytes(1);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
array3 = BitConverterLE.GetBytes(16);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
array3 = BitConverterLE.GetBytes(array.Length);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
byte[] array4 = new byte[16];
|
||||
RC4 rc = RC4.Create();
|
||||
byte[] array5 = null;
|
||||
try
|
||||
{
|
||||
RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();
|
||||
randomNumberGenerator.GetBytes(array4);
|
||||
fileStream.Write(array4, 0, array4.Length);
|
||||
array5 = this.DeriveKey(array4, password);
|
||||
if (this.Weak)
|
||||
{
|
||||
Array.Clear(array5, 5, 11);
|
||||
}
|
||||
ICryptoTransform cryptoTransform = rc.CreateEncryptor(array5, null);
|
||||
cryptoTransform.TransformBlock(array, 8, array.Length - 8, array, 8);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Array.Clear(array4, 0, array4.Length);
|
||||
Array.Clear(array5, 0, array5.Length);
|
||||
rc.Clear();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fileStream.Write(array2, 0, 4);
|
||||
fileStream.Write(array2, 0, 4);
|
||||
array3 = BitConverterLE.GetBytes(array.Length);
|
||||
fileStream.Write(array3, 0, 4);
|
||||
}
|
||||
fileStream.Write(array, 0, array.Length);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Array.Clear(array, 0, array.Length);
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000176 RID: 374 RVA: 0x00009EA8 File Offset: 0x000080A8
|
||||
public static PrivateKey CreateFromFile(string filename)
|
||||
{
|
||||
return PrivateKey.CreateFromFile(filename, null);
|
||||
}
|
||||
|
||||
// Token: 0x06000177 RID: 375 RVA: 0x00009EB4 File Offset: 0x000080B4
|
||||
public static PrivateKey CreateFromFile(string filename, string password)
|
||||
{
|
||||
if (filename == null)
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
byte[] array = null;
|
||||
using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
array = new byte[fileStream.Length];
|
||||
fileStream.Read(array, 0, array.Length);
|
||||
fileStream.Close();
|
||||
}
|
||||
return new PrivateKey(array, password);
|
||||
}
|
||||
|
||||
// Token: 0x040000A4 RID: 164
|
||||
private const uint magic = 2964713758U;
|
||||
|
||||
// Token: 0x040000A5 RID: 165
|
||||
private bool encrypted;
|
||||
|
||||
// Token: 0x040000A6 RID: 166
|
||||
private RSA rsa;
|
||||
|
||||
// Token: 0x040000A7 RID: 167
|
||||
private bool weak;
|
||||
|
||||
// Token: 0x040000A8 RID: 168
|
||||
private int keyType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Mono.Security.X509;
|
||||
|
||||
namespace Mono.Security.Authenticode
|
||||
{
|
||||
// Token: 0x02000021 RID: 33
|
||||
public class SoftwarePublisherCertificate
|
||||
{
|
||||
// Token: 0x06000162 RID: 354 RVA: 0x000097FC File Offset: 0x000079FC
|
||||
public SoftwarePublisherCertificate()
|
||||
{
|
||||
this.pkcs7 = new PKCS7.SignedData();
|
||||
this.pkcs7.ContentInfo.ContentType = "1.2.840.113549.1.7.1";
|
||||
}
|
||||
|
||||
// Token: 0x06000163 RID: 355 RVA: 0x00009830 File Offset: 0x00007A30
|
||||
public SoftwarePublisherCertificate(byte[] data)
|
||||
: this()
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
throw new ArgumentNullException("data");
|
||||
}
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo(data);
|
||||
if (contentInfo.ContentType != "1.2.840.113549.1.7.2")
|
||||
{
|
||||
throw new ArgumentException(Locale.GetText("Unsupported ContentType"));
|
||||
}
|
||||
this.pkcs7 = new PKCS7.SignedData(contentInfo.Content);
|
||||
}
|
||||
|
||||
// Token: 0x17000050 RID: 80
|
||||
// (get) Token: 0x06000164 RID: 356 RVA: 0x00009894 File Offset: 0x00007A94
|
||||
public X509CertificateCollection Certificates
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.pkcs7.Certificates;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000051 RID: 81
|
||||
// (get) Token: 0x06000165 RID: 357 RVA: 0x000098A4 File Offset: 0x00007AA4
|
||||
public ArrayList Crls
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.pkcs7.Crls;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000166 RID: 358 RVA: 0x000098B4 File Offset: 0x00007AB4
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo("1.2.840.113549.1.7.2");
|
||||
contentInfo.Content.Add(this.pkcs7.ASN1);
|
||||
return contentInfo.GetBytes();
|
||||
}
|
||||
|
||||
// Token: 0x06000167 RID: 359 RVA: 0x000098EC File Offset: 0x00007AEC
|
||||
public static SoftwarePublisherCertificate CreateFromFile(string filename)
|
||||
{
|
||||
if (filename == null)
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
byte[] array = null;
|
||||
using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
array = new byte[fileStream.Length];
|
||||
fileStream.Read(array, 0, array.Length);
|
||||
fileStream.Close();
|
||||
}
|
||||
if (array.Length < 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (array[0] != 48)
|
||||
{
|
||||
try
|
||||
{
|
||||
array = SoftwarePublisherCertificate.PEM(array);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new CryptographicException("Invalid encoding", ex);
|
||||
}
|
||||
}
|
||||
return new SoftwarePublisherCertificate(array);
|
||||
}
|
||||
|
||||
// Token: 0x06000168 RID: 360 RVA: 0x000099B4 File Offset: 0x00007BB4
|
||||
private static byte[] PEM(byte[] data)
|
||||
{
|
||||
string text = ((data[1] != 0) ? Encoding.ASCII.GetString(data) : Encoding.Unicode.GetString(data));
|
||||
int num = text.IndexOf("-----BEGIN PKCS7-----") + "-----BEGIN PKCS7-----".Length;
|
||||
int num2 = text.IndexOf("-----END PKCS7-----", num);
|
||||
string text2 = ((num != -1 && num2 != -1) ? text.Substring(num, num2 - num) : text);
|
||||
return Convert.FromBase64String(text2);
|
||||
}
|
||||
|
||||
// Token: 0x040000A1 RID: 161
|
||||
private const string header = "-----BEGIN PKCS7-----";
|
||||
|
||||
// Token: 0x040000A2 RID: 162
|
||||
private const string footer = "-----END PKCS7-----";
|
||||
|
||||
// Token: 0x040000A3 RID: 163
|
||||
private PKCS7.SignedData pkcs7;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user