private bool ReplaceModuleReference(DllToSign module, DllReference reference) { bool changed; ModuleDefinition md_ref; ModuleDefinition md_sign; md_ref = reference.Module; md_sign = module.Module; changed = false; for (int i = 0; i < md_sign.AssemblyReferences.Count; i++) { // Find any references to our 'reference' DLLs and update them if (md_sign.AssemblyReferences[i].Name == Path.GetFileNameWithoutExtension(md_ref.Name)) { if (Debug) { Console.WriteLine("Debug: Replacing reference to {0}, [{1}, {2}] with [{3}, {4}] in module {5}", md_sign.AssemblyReferences[i].Name, md_sign.AssemblyReferences[i].Version, PrintHex(md_sign.AssemblyReferences[i].PublicKeyToken), md_ref.Assembly.Name.Version, PrintHex(md_ref.Assembly.Name.PublicKeyToken), Path.GetFileNameWithoutExtension(module.Filename) ); } // We have a match - we need to replace they token and version md_sign.AssemblyReferences[i].PublicKeyToken = md_ref.Assembly.Name.PublicKeyToken; md_sign.AssemblyReferences[i].Version = md_ref.Assembly.Name.Version; module.ReferenceFixed = true; } // Find any references to our 'to sign' DLLs and replace the key token with the one we'll use to sign for (int j = 0; j < signdlls.Count; j++) { if (md_sign.AssemblyReferences[i].Name == Path.GetFileNameWithoutExtension(signdlls[j].Module.Name)) { if (Debug) { Console.WriteLine("Debug: Swapping key token for {0} from {1} to {2} in module {3}", Path.GetFileNameWithoutExtension(signdlls[j].Module.Name), PrintHex(md_sign.AssemblyReferences[i].PublicKeyToken), PrintHex(PublicKeyTokenGenerator.Token(keys[signdlls[j].Key].KeyPair)), md_sign.Name ); } md_sign.AssemblyReferences[i].PublicKeyToken = PublicKeyTokenGenerator.Token(keys[signdlls[j].Key].KeyPair); } } } return(changed); }
private bool ReadConfiguration(out string error) { XmlNamespaceManager ns_mgr; XmlDocument doc; XmlNodeList nodes; XmlNode node; SigningKey key; DllReference reference; DllToSign sign; ResignAssemblyResolver resolver; error = string.Empty; resolver = new ResignAssemblyResolver(this); try { doc = new XmlDocument(); doc.Load(configuration_file); ns_mgr = new XmlNamespaceManager(doc.NameTable); node = doc.SelectSingleNode("/resign/sign"); if (node != null) { reference_dir = GetAttribute(node, "referencedir", string.Empty); } nodes = doc.SelectNodes("/resign/signingkey"); foreach (XmlNode key_node in nodes) { key = new SigningKey(); key.Name = GetAttribute(key_node, "name", string.Empty); key.Filename = GetAttribute(key_node, "file", string.Empty); key.Container = GetAttribute(key_node, "container", string.Empty); if (!string.IsNullOrEmpty(key.Filename) && !File.Exists(key.Filename)) { error = "signingkey tag '" + key.Name + "' references non-existing file '" + key.Filename + "'"; return(false); } keys.Add(key.Name, key); } nodes = doc.SelectNodes("/resign/references/dll"); foreach (XmlNode ref_node in nodes) { reference = new DllReference(); reference.Name = GetAttribute(ref_node, "name", string.Empty); reference.Filename = GetAttribute(ref_node, "path", string.Empty); if (!File.Exists(reference.Filename)) { error = "references/dll tag '" + reference.Name + "' references non-existing file '" + reference.Filename + "'"; return(false); } references.Add(Path.GetFileNameWithoutExtension(reference.Name), reference); } nodes = doc.SelectNodes("/resign/sign/dll"); foreach (XmlNode dll_node in nodes) { sign = new DllToSign(resolver); sign.Filename = GetAttribute(dll_node, "path", string.Empty); if (!File.Exists(sign.Filename)) { error = "dll tag references non-existing path '" + sign.Filename + "'"; return(false); } sign.Key = GetAttribute(dll_node, "key", string.Empty); if (!keys.Keys.Contains(sign.Key)) { error = "dll tag '" + sign.Filename + "' references undefined key '" + sign.Key; return(false); } signdlls.Add(sign); } node = doc.SelectSingleNode("/resign/sign"); output_dir = GetAttribute(node, "outputdir", string.Empty); only_sign_on_match = GetAttribute(node, "changedonly", true); return(true); } catch (Exception ex) { error = ex.ToString(); return(false); } }