/// <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); }
/// <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(); } } } }