/// <summary> /// Compare the strong name signing state of the existing wrapper to the signing /// state we are requesting in this run of the task. Return true if they match (e.g. /// from a signing perspective, the wrapper is up-to-date) or false otherwise. /// </summary> private bool SigningRequirementsMatchExistingWrapper(ComReferenceWrapperInfo wrapperInfo) { StrongNameLevel desiredStrongNameLevel = StrongNameLevel.None; if (!string.IsNullOrEmpty(KeyFile) || !string.IsNullOrEmpty(KeyContainer)) { desiredStrongNameLevel = DelaySign ? StrongNameLevel.DelaySigned : StrongNameLevel.FullySigned; } // ...and see what we have already StrongNameLevel currentStrongNameLevel = StrongNameUtils.GetAssemblyStrongNameLevel(wrapperInfo.path); // if not matching, need to regenerate wrapper if (desiredStrongNameLevel != currentStrongNameLevel) { return(false); } // if the wrapper needs a strong name, see if the public keys match if (desiredStrongNameLevel == StrongNameLevel.DelaySigned || desiredStrongNameLevel == StrongNameLevel.FullySigned) { // get desired public key StrongNameUtils.GetStrongNameKey(Log, KeyFile, KeyContainer, out _, out byte[] desiredPublicKey); // get current public key AssemblyName assemblyName = AssemblyName.GetAssemblyName(wrapperInfo.path); if (assemblyName == null) { return(false); } byte[] currentPublicKey = assemblyName.GetPublicKey(); if (currentPublicKey.Length != desiredPublicKey.Length) { return(false); } // compare public keys byte by byte for (int i = 0; i < currentPublicKey.Length; i++) { if (currentPublicKey[i] != desiredPublicKey[i]) { return(false); } } } return(true); }
internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo) { wrapperInfo = null; StrongNameKeyPair keyPair = null; byte[] publicKey = null; StrongNameUtils.GetStrongNameKey(base.Log, base.KeyFile, base.KeyContainer, out keyPair, out publicKey); if (!base.DelaySign && (keyPair == null)) { if ((base.KeyContainer != null) && (base.KeyContainer.Length > 0)) { base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInContainer", new object[] { base.KeyContainer }); throw new StrongNameException(); } if ((base.KeyFile != null) && (base.KeyFile.Length > 0)) { base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInFile", new object[] { base.KeyFile }); throw new StrongNameException(); } } bool flag = true; this.ReferenceInfo.taskItem.GetMetadata("TlbReferenceName"); ResolveComReference.AxImp imp = new ResolveComReference.AxImp { ActiveXControlName = this.ReferenceInfo.typeLibPath, BuildEngine = base.BuildEngine, ToolPath = base.ToolPath, EnvironmentVariables = base.EnvironmentVariables, DelaySign = base.DelaySign, GenerateSource = false, KeyContainer = base.KeyContainer, KeyFile = base.KeyFile }; if (((this.ReferenceInfo != null) && (this.ReferenceInfo.primaryOfAxImpRef != null)) && ((this.ReferenceInfo.primaryOfAxImpRef.resolvedWrapper != null) && (this.ReferenceInfo.primaryOfAxImpRef.resolvedWrapper.path != null))) { imp.RuntimeCallableWrapperAssembly = this.ReferenceInfo.primaryOfAxImpRef.resolvedWrapper.path; } imp.OutputAssembly = Path.Combine(this.OutputDirectory, base.GetWrapperFileName()); flag = imp.Execute(); string wrapperPath = base.GetWrapperPath(); wrapperInfo = new ComReferenceWrapperInfo(); wrapperInfo.path = wrapperPath; wrapperInfo.assembly = Assembly.UnsafeLoadFrom(wrapperInfo.path); return(flag); }
/// <summary> /// Given our KeyFile, KeyContainer, and DelaySign parameters, generate the public / private /// key pair and validate that it exists to the extent needed. /// </summary> internal void GetAndValidateStrongNameKey(out StrongNameKeyPair keyPair, out byte[] publicKey) { keyPair = null; publicKey = null; // get key pair/public key StrongNameUtils.GetStrongNameKey(Log, KeyFile, KeyContainer, out keyPair, out publicKey); // make sure we give as much data to the typelib converter as necessary but not more, or we might end up // with something we didn't want if (DelaySign) { keyPair = null; if (publicKey == null) { Log.LogErrorWithCodeFromResources(null, ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "StrongNameUtils.NoPublicKeySpecified"); throw new StrongNameException(); } } else { publicKey = null; // If the user did not specify delay sign and we didn't get a public/private // key pair then we have an error since a public key by itself is not enough // to fully sign the assembly. (only if either KeyContainer or KeyFile was specified though) if (keyPair == null) { if (!string.IsNullOrEmpty(KeyContainer)) { Log.LogErrorWithCodeFromResources(null, ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInContainer", KeyContainer); throw new StrongNameException(); } if (!string.IsNullOrEmpty(KeyFile)) { Log.LogErrorWithCodeFromResources(null, ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInFile", KeyFile); throw new StrongNameException(); } } } }
internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo) { wrapperInfo = null; string typeLibName = this.ReferenceInfo.typeLibName; string wrapperPath = base.GetWrapperPath(); StrongNameKeyPair keyPair = null; byte[] publicKey = null; StrongNameUtils.GetStrongNameKey(base.Log, base.KeyFile, base.KeyContainer, out keyPair, out publicKey); if (base.DelaySign) { keyPair = null; if (publicKey == null) { base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "StrongNameUtils.NoPublicKeySpecified", new object[0]); throw new StrongNameException(); } } else { publicKey = null; if (keyPair == null) { if ((base.KeyContainer != null) && (base.KeyContainer.Length > 0)) { base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInContainer", new object[] { base.KeyContainer }); throw new StrongNameException(); } if ((base.KeyFile != null) && (base.KeyFile.Length > 0)) { base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInFile", new object[] { base.KeyFile }); throw new StrongNameException(); } } } bool flag = true; if (!base.ExecuteAsTool) { TypeLibConverter converter = new TypeLibConverter(); AssemblyBuilder assemblyBuilder = null; try { TypeLibImporterFlags flags = TypeLibImporterFlags.TransformDispRetVals | TypeLibImporterFlags.SafeArrayAsSystemArray; if (this.noClassMembers) { flags |= TypeLibImporterFlags.PreventClassMembers; } string str4 = this.targetProcessorArchitecture; if (str4 != null) { if (!(str4 == "MSIL")) { if (str4 == "AMD64") { goto Label_0323; } if (str4 == "IA64") { goto Label_032F; } if (str4 == "x86") { goto Label_033B; } } else { flags |= TypeLibImporterFlags.ImportAsAgnostic; } } goto Label_0345; Label_0323: flags |= TypeLibImporterFlags.ImportAsX64; goto Label_0345; Label_032F: flags |= TypeLibImporterFlags.ImportAsItanium; goto Label_0345; Label_033B: flags |= TypeLibImporterFlags.ImportAsX86; Label_0345: assemblyBuilder = converter.ConvertTypeLibToAssembly(this.ReferenceInfo.typeLibPointer, wrapperPath, flags, this, publicKey, keyPair, typeLibName, null); } catch (COMException exception) { base.Log.LogWarningWithCodeFromResources("ResolveComReference.ErrorCreatingWrapperAssembly", new object[] { this.ItemName, exception.Message }); throw new ComReferenceResolutionException(exception); } if (!this.HasTemporaryWrapper) { this.WriteWrapperToDisk(assemblyBuilder, wrapperPath); } wrapperInfo = new ComReferenceWrapperInfo(); wrapperInfo.path = this.HasTemporaryWrapper ? null : wrapperPath; wrapperInfo.assembly = assemblyBuilder; return(flag); } ResolveComReference.TlbImp imp = new ResolveComReference.TlbImp { BuildEngine = base.BuildEngine, EnvironmentVariables = base.EnvironmentVariables, DelaySign = base.DelaySign, KeyContainer = base.KeyContainer, KeyFile = base.KeyFile, OutputAssembly = wrapperPath, ToolPath = base.ToolPath, TypeLibName = this.ReferenceInfo.typeLibPath, AssemblyNamespace = typeLibName, AssemblyVersion = null, PreventClassMembers = this.noClassMembers, SafeArrayAsSystemArray = true, Transform = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals }; if (this.referenceFiles != null) { string fullPathToOutput = Path.GetFullPath(wrapperPath); imp.ReferenceFiles = (from rf in this.referenceFiles where string.Compare(fullPathToOutput, rf, StringComparison.OrdinalIgnoreCase) != 0 select rf).ToArray <string>(); } string targetProcessorArchitecture = this.targetProcessorArchitecture; if (targetProcessorArchitecture != null) { if (!(targetProcessorArchitecture == "MSIL")) { if (targetProcessorArchitecture == "AMD64") { imp.Machine = "X64"; } else if (targetProcessorArchitecture == "IA64") { imp.Machine = "Itanium"; } else if (targetProcessorArchitecture == "x86") { imp.Machine = "X86"; } else { imp.Machine = this.targetProcessorArchitecture; } } else { imp.Machine = "Agnostic"; } } flag = imp.Execute(); wrapperInfo = new ComReferenceWrapperInfo(); wrapperInfo.path = this.HasTemporaryWrapper ? null : wrapperPath; return(flag); }
private bool ValidateStrongNameParameters() { bool flag = false; bool flag2 = false; if (!string.IsNullOrEmpty(this.KeyFile)) { if (!File.Exists(this.KeyFile)) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.InvalidKeyFileSpecified", new object[] { this.KeyFile }); return(false); } flag = true; } if (!string.IsNullOrEmpty(this.KeyContainer)) { if (!File.Exists(this.KeyContainer)) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.InvalidKeyContainerSpecified", new object[] { this.KeyContainer }); return(false); } flag2 = true; } if (flag && flag2) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.CannotSpecifyBothKeyFileAndKeyContainer", new object[0]); return(false); } if (this.DelaySign) { if (!flag && !flag2) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.CannotSpecifyDelaySignWithoutEitherKeyFileOrKeyContainer", new object[0]); return(false); } } else if (flag || flag2) { StrongNameKeyPair keyPair = null; byte[] publicKey = null; try { StrongNameUtils.GetStrongNameKey(base.Log, this.KeyFile, this.KeyContainer, out keyPair, out publicKey); } catch (StrongNameException exception) { base.Log.LogErrorFromException(exception); keyPair = null; } if (keyPair == null) { if (!string.IsNullOrEmpty(this.KeyContainer)) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.StrongNameUtils.NoKeyPairInContainer", new object[] { this.KeyContainer }); return(false); } if (!string.IsNullOrEmpty(this.KeyFile)) { base.Log.LogErrorWithCodeFromResources("AxTlbBaseTask.StrongNameUtils.NoKeyPairInFile", new object[] { this.KeyFile }); return(false); } } } return(true); }
/// <summary> /// Validates the parameters passed to the task that involve strong name signing -- /// DelaySign, KeyContainer, and KeyFile /// </summary> /// <returns>true if the parameters are valid, false otherwise.</returns> private bool ValidateStrongNameParameters() { bool keyFileExists = false; bool keyContainerSpecified = false; // Make sure that if KeyFile is defined, it's a real file. if (!String.IsNullOrEmpty(KeyFile)) { if (File.Exists(KeyFile)) { keyFileExists = true; } else { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.InvalidKeyFileSpecified", KeyFile); return(false); } } // Check if KeyContainer name is specified keyContainerSpecified = !String.IsNullOrEmpty(KeyContainer); // Cannot define both KeyFile and KeyContainer if (keyFileExists && keyContainerSpecified) { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.CannotSpecifyBothKeyFileAndKeyContainer"); return(false); } // If this assembly is delay signed, either KeyFile or KeyContainer must be defined if (DelaySign && !keyFileExists && !keyContainerSpecified) { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.CannotSpecifyDelaySignWithoutEitherKeyFileOrKeyContainer"); return(false); } // If KeyFile or KeyContainer is specified, verify that a key pair exists (or if delay-signed, // even just a public key) if (keyFileExists || keyContainerSpecified) { StrongNameKeyPair keyPair = null; byte[] publicKey = null; try { StrongNameUtils.GetStrongNameKey(Log, KeyFile, KeyContainer, out keyPair, out publicKey); } catch (StrongNameException e) { Log.LogErrorFromException(e); keyPair = null; // don't return here -- let the appropriate error below get logged also. } if (DelaySign) { if (publicKey == null) { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.StrongNameUtils.NoPublicKeySpecified"); return(false); } else if (keyPair == null) { // record this so we know which switch to pass to the task _delaySigningAndKeyFileOnlyContainsPublicKey = true; } } else { if (keyPair == null) { if (!String.IsNullOrEmpty(KeyContainer)) { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.StrongNameUtils.NoKeyPairInContainer", KeyContainer); return(false); } else if (!String.IsNullOrEmpty(KeyFile)) { Log.LogErrorWithCodeFromResources("AxTlbBaseTask.StrongNameUtils.NoKeyPairInFile", KeyFile); return(false); } } } } return(true); }
protected virtual bool IsWrapperUpToDate(ComReferenceWrapperInfo wrapperInfo, DateTime componentTimestamp) { if ((this.ReferenceInfo.typeLibPath == null) || (this.ReferenceInfo.typeLibPath.Length == 0)) { throw new ComReferenceResolutionException(); } if (!File.Exists(wrapperInfo.path)) { return(false); } if (DateTime.Compare(File.GetLastWriteTime(this.ReferenceInfo.typeLibPath), componentTimestamp) != 0) { return(false); } StrongNameLevel none = StrongNameLevel.None; if (((this.KeyFile != null) && (this.KeyFile.Length > 0)) || ((this.KeyContainer != null) && (this.KeyContainer.Length > 0))) { if (this.DelaySign) { none = StrongNameLevel.DelaySigned; } else { none = StrongNameLevel.FullySigned; } } StrongNameLevel assemblyStrongNameLevel = StrongNameUtils.GetAssemblyStrongNameLevel(wrapperInfo.path); if (none != assemblyStrongNameLevel) { return(false); } if ((none == StrongNameLevel.DelaySigned) || (none == StrongNameLevel.FullySigned)) { StrongNameKeyPair pair; byte[] publicKey = null; StrongNameUtils.GetStrongNameKey(base.Log, this.KeyFile, this.KeyContainer, out pair, out publicKey); AssemblyName assemblyName = AssemblyName.GetAssemblyName(wrapperInfo.path); if (assemblyName == null) { return(false); } byte[] buffer2 = assemblyName.GetPublicKey(); if (buffer2.Length != publicKey.Length) { return(false); } for (int i = 0; i < buffer2.Length; i++) { if (buffer2[i] != publicKey[i]) { return(false); } } } try { wrapperInfo.assembly = Assembly.UnsafeLoadFrom(wrapperInfo.path); } catch (BadImageFormatException) { wrapperInfo.assembly = null; } return(wrapperInfo.assembly != null); }