// EDMAURER in the event that the key is supplied as a file,
        // this type could get an instance member that caches the file
        // contents to avoid reading the file twice - once to get the
        // public key to establish the assembly name and another to do
        // the actual signing

        // internal for testing
        internal IClrStrongName GetStrongNameInterface()
        {
            IClrStrongName factoryCreated = TestStrongNameInterfaceFactory?.Invoke();

            if (factoryCreated != null)
            {
                return(factoryCreated);
            }

            try
            {
                return(ClrStrongName.GetInstance());
            }
            catch (MarshalDirectiveException) when(PathUtilities.IsUnixLikePlatform)
            {
                // CoreCLR, when not on Windows, doesn't support IClrStrongName (or COM in general).
                // This is really hard to detect/predict without false positives/negatives.
                // It turns out that CoreCLR throws a MarshalDirectiveException when attempting
                // to get the interface (Message "Cannot marshal 'return value': Unknown error."),
                // so just catch that and state that it's not supported.

                // We're deep in a try block that reports the exception's Message as part of a diagnostic.
                // This exception will skip through the IOException wrapping by `Sign` (in this class),
                // then caught by Compilation.SerializeToPeStream or DesktopStringNameProvider.CreateKeys
                throw new ClrStrongNameMissingException();
            }
        }
Example #2
0
        private static byte[] GetPublicKey([NotNull] byte[] keyFileContents)
        {
            var strongName = ClrStrongName.GetInstance();

            IntPtr keyBlob;
            int    keyBlobByteCount;

            unsafe
            {
                fixed(byte *p = keyFileContents)
                {
                    strongName.StrongNameGetPublicKey(null, (IntPtr)p, keyFileContents.Length, out keyBlob, out keyBlobByteCount);
                }
            }

            var publicKey = new byte[keyBlobByteCount];

            Marshal.Copy(keyBlob, publicKey, 0, keyBlobByteCount);
            strongName.StrongNameFreeBuffer(keyBlob);

            return(publicKey);
        }
        // EDMAURER in the event that the key is supplied as a file,
        // this type could get an instance member that caches the file
        // contents to avoid reading the file twice - once to get the
        // public key to establish the assembly name and another to do
        // the actual signing

        // internal for testing
        internal IClrStrongName GetStrongNameInterface()
        {
            return(TestStrongNameInterfaceFactory?.Invoke() ?? ClrStrongName.GetInstance());
        }