public void CreateSignedFile() { StrongNameKey snkIn = StrongNameKey.LoadFrom(SnkFile); string file; using (FileStream f = File.Create(file = TmpPath)) using (AssuredStream s = new AssuredStream(f, snkIn, "TestPackage")) using (BinaryWriter bw = new BinaryWriter(s)) { Assert.That(s.Position, Is.EqualTo(0L), "Position 0 within signed file"); bw.Write("Test String"); Assert.That(s.Position, Is.Not.EqualTo(0L), "Position 0 within signed file"); } using (FileStream f = File.OpenRead(file)) using (AssuredStream s = new AssuredStream(f, VerificationMode.Full)) using (BinaryReader br = new BinaryReader(s)) { Assert.That(s.PublicKeyToken, Is.EqualTo(snkIn.PublicKeyToken), "Public key matches"); Assert.That(s.Position, Is.EqualTo(0L), "Position 0 within signed file"); Assert.That(br.ReadString(), Is.EqualTo("Test String")); Assert.That(s.Position, Is.Not.EqualTo(0L), "Position not 0 within signed file"); Assert.That(s.HashString, Is.Not.Null, "Hash is set"); } }
/*public AssuredStreamHeader(Stream source) * : this(source, VerificationMode.Full) * { * }*/ public AssuredStreamHeader(Stream source, VerificationMode mode) { QQnBinaryReader br = new QQnBinaryReader(source); uint vFileSignature = br.ReadUInt32(); if (vFileSignature != FileSignature) { throw new FormatException(); } _fileType = br.ReadString(); _hashType = (HashType)br.ReadByte(); _fileHash = br.ReadByteArray(); _hashSignature = br.ReadByteArray(); byte[] publicKey = br.ReadByteArray(); if (publicKey.Length > 0) { _snk = StrongNameKey.LoadFrom(publicKey); if (mode != VerificationMode.None) { if (!_snk.VerifyHash(_fileHash, _hashSignature)) { throw new CryptographicException("Stream hash verification failed"); } } } _guid = new Guid(br.ReadBytes(16)); _bodyLength = br.ReadInt64(); _hashPosition = source.Position; }
public void CreateSignedSubFile() { // Tests whether a stream within a signed stream behaves as an outer stream StrongNameKey snkIn = StrongNameKey.LoadFrom(SnkFile); string file; using (FileStream f = File.Create(file = TmpPath)) using (AssuredStream s = new AssuredStream(f, snkIn, "TestPackage")) using (AssuredSubStream s2 = new AssuredSubStream(s, VerificationMode.Full)) using (BinaryWriter bw = new BinaryWriter(s2)) { Assert.That(s2.Position, Is.EqualTo(0L), "Position 0 within signed file"); bw.Write("Test String"); Assert.That(s2.Position, Is.Not.EqualTo(0L), "Position 0 within signed file"); } using (FileStream f = File.OpenRead(file)) using (AssuredStream s = new AssuredStream(f, VerificationMode.Full)) using (AssuredSubStream s2 = new AssuredSubStream(s, VerificationMode.Full)) using (BinaryReader br = new BinaryReader(s2)) { Assert.That(s2.Position, Is.EqualTo(0L), "Position 0 within signed file"); Assert.That(br.ReadString(), Is.EqualTo("Test String")); Assert.That(s2.Position, Is.Not.EqualTo(0L), "Position not 0 within signed file"); Assert.That(s.HashString, Is.Not.Null, "Hash is set"); } }
public void CreateZipAndTpz() { using (ZipFile zf = ZipFile.Create(QQnPath.Combine(TmpPath, "TestZip.zip"))) { AddSomeFiles(zf); } AssuredStreamCreateArgs assuredArgs = new AssuredStreamCreateArgs(); assuredArgs.StrongNameKey = StrongNameKey.LoadFrom(SnkFile); assuredArgs.FileType = "TPZ-Test"; MultipleStreamCreateArgs mutlArgs = new MultipleStreamCreateArgs(); mutlArgs.VerificationMode = VerificationMode.Full; using (FileStream fileStream = File.Create(QQnPath.Combine(TmpPath, "TestTpz.zip"))) using (AssuredStream assuredStream = new AssuredStream(fileStream, assuredArgs)) using (MultipleStreamWriter msw = new MultipleStreamWriter(assuredStream, mutlArgs)) { using (Stream s = msw.CreateStream()) { s.WriteByte(255); } using (Stream s = msw.CreateStream()) using (ZipFile zf = ZipFile.Create(s)) { AddSomeFiles(zf); } } }
public void TestMyHash() { StrongNameKey snkIn = StrongNameKey.LoadFrom(SnkFile); byte[] data = new byte[20]; byte[] signature = snkIn.SignHash(data); byte[] publicKeyData = snkIn.GetPublicKeyData(); StrongNameKey snkVerify = StrongNameKey.LoadFrom(publicKeyData); Assert.That(snkVerify.VerifyHash(data, signature), Is.True, "Verification completed"); }
public Pack CreateDefinition(TBLogFile file) { BuildOrigin myOrigin = null; foreach (BuildOrigin bo in BuildOrigins) { if (bo.LogFile == file) { myOrigin = bo; break; } } Pack p = new Pack(); TBLogConfiguration config = file.Configurations[0]; TBLogTarget target = config.Target; TBLogAssembly asm = config.Assembly; if (!string.IsNullOrEmpty(target.KeySrc)) { p.StrongNameKey = StrongNameKey.LoadFrom(QQnPath.Combine(file.ProjectPath, target.KeySrc)); } else if (!string.IsNullOrEmpty(target.KeyContainer)) { p.StrongNameKey = StrongNameKey.LoadFromContainer(target.KeyContainer, false); } if (asm != null && !string.IsNullOrEmpty(asm.AssemblyName)) { AssemblyName name = new AssemblyName(asm.AssemblyName); p.Version = name.Version; } PackContainer po = p.Containers.AddItem("#ProjectOutput"); po.ContainerDir = config.OutputPath; po.BaseDir = QQnPath.Combine(file.ProjectPath); foreach (TBLogItem item in file.AllProjectOutput) { if (item.IsShared) { continue; } PackFile pf = po.Files.AddItem(QQnPath.EnsureRelativePath(po.BaseDir, item.FullSrc)); pf.StreamName = item.Src; } PackContainer ct = p.Containers.AddItem("#Content"); ct.ContainerDir = ""; po.BaseDir = file.ProjectPath; foreach (TBLogItem item in file.AllContents) { if (item.IsShared) { continue; } PackFile pf = po.Files.AddItem(QQnPath.EnsureRelativePath(po.BaseDir, item.FullSrc)); } myOrigin.Pack = p; return(p); }
public void TestTokens() { StrongNameKey snkIn = StrongNameKey.LoadFrom(SnkFile); Assert.That(snkIn.PublicKeyToken, Is.EqualTo(QQnCryptoHelpers.HashString(typeof(QQnCryptoHelpers).Assembly.GetName().GetPublicKeyToken())), "Our public-token api matches the .Net one"); }
/// <summary> /// Tries to create a pack. /// </summary> /// <param name="project">The project.</param> /// <param name="pack">The pack.</param> /// <returns></returns> public static bool TryCreatePack(TBLogFile project, out Pack pack) { if (project == null) { throw new ArgumentNullException("project"); } TBLogFile log = project; string projectDir = project.Project.Path; Pack p = new Pack(); p.BaseDir = projectDir; PackContainer projectOutput = p.Containers.AddItem("#ProjectOutput"); TBLogConfiguration config = log.Configurations[0]; if (!string.IsNullOrEmpty(config.OutputPath)) { projectOutput.ContainerDir = config.OutputPath; projectOutput.BaseDir = config.OutputPath; } foreach (TBLogItem item in config.ProjectOutput.Items) { if (item.IsShared) { continue; } PackFile pf = projectOutput.Files.AddItem(QQnPath.MakeRelativePath(projectOutput.BaseDir, QQnPath.Combine(projectDir, item.Src))); pf.StreamName = item.Src; } PackContainer projectContent = p.Containers.AddItem("#ProjectContent"); if (!string.IsNullOrEmpty(config.OutputPath)) { projectContent.ContainerDir = "content/" + log.Project.Name; projectContent.BaseDir = log.ProjectPath; } foreach (TBLogItem item in config.Content.Items) { PackFile pf = projectContent.Files.AddItem(QQnPath.MakeRelativePath(projectContent.BaseDir, QQnPath.Combine(projectDir, item.Src))); pf.StreamName = item.Src; } PackContainer projectScripts = p.Containers.AddItem("#ProjectScripts"); if (!string.IsNullOrEmpty(config.OutputPath)) { projectScripts.ContainerDir = "scripts/" + log.Project.Name; projectScripts.BaseDir = log.Project.Path; } foreach (TBLogItem item in config.Content.Items) { PackFile pf = projectContent.Files.AddItem(QQnPath.MakeRelativePath(projectContent.BaseDir, QQnPath.Combine(projectDir, item.Src))); pf.StreamName = item.Src; } if (config.Target.KeySrc != null) { p.StrongNameKey = StrongNameKey.LoadFrom(QQnPath.Combine(log.Project.Path, config.Target.KeySrc)); } else if (config.Target.KeyContainer != null) { p.StrongNameKey = StrongNameKey.LoadFromContainer(config.Target.KeyContainer, false); } foreach (PackContainer pc in p.Containers) { foreach (PackFile pf in pc.Files) { VerifyUtils.UpdateFile(pf.BaseDir, pf); } } pack = p; return(true); }