Imported Upstream version 6.6.0.108

Former-commit-id: 00c6fff7917ab7dddc0c67044b046850c2283096
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-10-04 09:01:11 +00:00
parent 4c26f54a2f
commit 9050bbfc26
51 changed files with 188 additions and 118 deletions

View File

@@ -402,13 +402,16 @@ namespace IKVM.Reflection
{
return publicKey;
}
byte[] hash = new SHA1Managed().ComputeHash(publicKey);
byte[] token = new byte[8];
for (int i = 0; i < token.Length; i++)
using (var sha1 = SHA1.Create())
{
token[i] = hash[hash.Length - 1 - i];
byte[] hash = sha1.ComputeHash(publicKey);
byte[] token = new byte[8];
for (int i = 0; i < token.Length; i++)
{
token[i] = hash[hash.Length - 1 - i];
}
return token;
}
return token;
}
internal static string ComputePublicKeyToken(string publicKey)

View File

@@ -472,21 +472,23 @@ namespace IKVM.Reflection.Emit
private int AddFile(ModuleBuilder manifestModule, string fileName, int flags)
{
SHA1Managed hash = new SHA1Managed();
string fullPath = fileName;
if (dir != null)
using (var hash = SHA1.Create())
{
fullPath = Path.Combine(dir, fileName);
}
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
string fullPath = fileName;
if (dir != null)
{
byte[] buf = new byte[8192];
ModuleWriter.HashChunk(fs, cs, buf, (int)fs.Length);
fullPath = Path.Combine(dir, fileName);
}
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
{
byte[] buf = new byte[8192];
ModuleWriter.HashChunk(fs, cs, buf, (int)fs.Length);
}
}
return manifestModule.__AddModule(flags, Path.GetFileName(fileName), hash.Hash);
}
return manifestModule.__AddModule(flags, Path.GetFileName(fileName), hash.Hash);
}
public void AddResourceFile(string name, string fileName)

View File

@@ -382,53 +382,55 @@ namespace IKVM.Reflection.Writer
private static void StrongName(Stream stream, StrongNameKeyPair keyPair, uint headerLength, uint textSectionFileOffset, uint strongNameSignatureFileOffset, uint strongNameSignatureLength)
{
SHA1Managed hash = new SHA1Managed();
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
using (var hash = SHA1.Create())
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, (int)headerLength);
stream.Seek(textSectionFileOffset, SeekOrigin.Begin);
HashChunk(stream, cs, buf, (int)(strongNameSignatureFileOffset - textSectionFileOffset));
stream.Seek(strongNameSignatureLength, SeekOrigin.Current);
HashChunk(stream, cs, buf, (int)(stream.Length - (strongNameSignatureFileOffset + strongNameSignatureLength)));
}
using (RSA rsa = keyPair.CreateRSA())
{
RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter(rsa);
byte[] signature = sign.CreateSignature(hash);
Array.Reverse(signature);
if (signature.Length != strongNameSignatureLength)
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
{
throw new InvalidOperationException("Signature length mismatch");
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, (int)headerLength);
stream.Seek(textSectionFileOffset, SeekOrigin.Begin);
HashChunk(stream, cs, buf, (int)(strongNameSignatureFileOffset - textSectionFileOffset));
stream.Seek(strongNameSignatureLength, SeekOrigin.Current);
HashChunk(stream, cs, buf, (int)(stream.Length - (strongNameSignatureFileOffset + strongNameSignatureLength)));
}
using (RSA rsa = keyPair.CreateRSA())
{
RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter(rsa);
byte[] signature = sign.CreateSignature(hash);
Array.Reverse(signature);
if (signature.Length != strongNameSignatureLength)
{
throw new InvalidOperationException("Signature length mismatch");
}
stream.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
stream.Write(signature, 0, signature.Length);
}
stream.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
stream.Write(signature, 0, signature.Length);
}
// compute the PE checksum
stream.Seek(0, SeekOrigin.Begin);
int count = (int)stream.Length / 4;
BinaryReader br = new BinaryReader(stream);
long sum = 0;
for (int i = 0; i < count; i++)
{
sum += br.ReadUInt32();
int carry = (int)(sum >> 32);
sum &= 0xFFFFFFFFU;
sum += carry;
}
while ((sum >> 16) != 0)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
sum += stream.Length;
// compute the PE checksum
stream.Seek(0, SeekOrigin.Begin);
int count = (int)stream.Length / 4;
BinaryReader br = new BinaryReader(stream);
long sum = 0;
for (int i = 0; i < count; i++)
{
sum += br.ReadUInt32();
int carry = (int)(sum >> 32);
sum &= 0xFFFFFFFFU;
sum += carry;
}
while ((sum >> 16) != 0)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
sum += stream.Length;
// write the PE checksum, note that it is always at offset 0xD8 in the file
ByteBuffer bb = new ByteBuffer(4);
bb.Write((int)sum);
stream.Seek(0xD8, SeekOrigin.Begin);
bb.WriteTo(stream);
// write the PE checksum, note that it is always at offset 0xD8 in the file
ByteBuffer bb = new ByteBuffer(4);
bb.Write((int)sum);
stream.Seek(0xD8, SeekOrigin.Begin);
bb.WriteTo(stream);
}
}
internal static void HashChunk(Stream stream, CryptoStream cs, byte[] buf, int length)
@@ -443,21 +445,23 @@ namespace IKVM.Reflection.Writer
private static Guid GenerateModuleVersionId(Stream stream)
{
SHA1Managed hash = new SHA1Managed();
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
using (var hash = SHA1.Create())
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, (int)stream.Length);
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, (int)stream.Length);
}
byte[] bytes = new byte[16];
Buffer.BlockCopy(hash.Hash, 0, bytes, 0, bytes.Length);
// set GUID type to "version 4" (random)
bytes[7] &= 0x0F;
bytes[7] |= 0x40;
bytes[8] &= 0x3F;
bytes[8] |= 0x80;
return new Guid(bytes);
}
byte[] bytes = new byte[16];
Buffer.BlockCopy(hash.Hash, 0, bytes, 0, bytes.Length);
// set GUID type to "version 4" (random)
bytes[7] &= 0x0F;
bytes[7] |= 0x40;
bytes[8] &= 0x3F;
bytes[8] |= 0x80;
return new Guid(bytes);
}
}
}