// 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(); } }
private static IClrStrongName GetClrStrongName() { return(clrStrongName ?? (clrStrongName = (IClrStrongName)RuntimeEnvironment.GetRuntimeInterfaceAsObject( new Guid("B79B0ACD-F5CD-409b-B5A5-A16244610B92"), typeof(IClrStrongName).GUID))); }
internal ImmutableArray <byte> GetPublicKey(string keyContainer) { IClrStrongName strongName = GetStrongNameInterface(); IntPtr keyBlob; int keyBlobByteCount; strongName.StrongNameGetPublicKey(keyContainer, pbKeyBlob: default, 0, out keyBlob, out keyBlobByteCount);
internal ImmutableArray <byte> GetPublicKey(string keyContainer) { IClrStrongName strongName = GetStrongNameInterface(); strongName.StrongNameGetPublicKey(keyContainer, default(IntPtr), 0, out IntPtr keyBlob, out int keyBlobByteCount); byte[] pubKey = new byte[keyBlobByteCount]; Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount); strongName.StrongNameFreeBuffer(keyBlob); return(pubKey.AsImmutableOrNull()); }
/// <exception cref="IOException"/> private void Sign(string filePath, string keyName) { try { IClrStrongName strongName = GetStrongNameInterface(); int unused; strongName.StrongNameSignatureGeneration(filePath, keyName, IntPtr.Zero, 0, null, out unused); } catch (Exception ex) { throw new IOException(ex.Message, ex); } }
/// <exception cref="IOException"/> private unsafe void Sign(string filePath, ImmutableArray <byte> keyPair) { try { IClrStrongName strongName = GetStrongNameInterface(); fixed(byte *pinned = keyPair.ToArray()) { int unused; strongName.StrongNameSignatureGeneration(filePath, null, (IntPtr)pinned, keyPair.Length, null, out unused); } } catch (Exception ex) { throw new IOException(ex.Message, ex); } }
/// <exception cref="IOException"/> private void Sign(string filePath, string keyName) { try { IClrStrongName strongName = GetStrongNameInterface(); strongName.StrongNameSignatureGeneration(filePath, keyName, IntPtr.Zero, 0, null, out int unused); } catch (ClrStrongNameMissingException) { // pipe it through so it's catchable directly by type throw; } catch (Exception ex) { throw new IOException(ex.Message, ex); } }
/// <exception cref="IOException"/> private unsafe void Sign(string filePath, ImmutableArray <byte> keyPair) { try { IClrStrongName strongName = GetStrongNameInterface(); fixed(byte *pinned = keyPair.ToArray()) { strongName.StrongNameSignatureGeneration(filePath, null, (IntPtr)pinned, keyPair.Length, null, out int unused); } } catch (ClrStrongNameMissingException) { // pipe it through so it's catchable directly by type throw; } catch (Exception ex) { throw new IOException(ex.Message, ex); } }
// internal for testing /// <exception cref="IOException"/> internal ImmutableArray <byte> GetPublicKey(byte[] keyFileContents) { try { var lastSeen = s_lastSeenKeyPair; if (lastSeen != null && ByteSequenceComparer.Equals(lastSeen.Item1, keyFileContents)) { return(lastSeen.Item2); } IClrStrongName strongName = GetStrongNameInterface(); IntPtr keyBlob; int keyBlobByteCount; //EDMAURER use marshal to be safe? unsafe { fixed(byte *p = keyFileContents) { strongName.StrongNameGetPublicKey(null, (IntPtr)p, keyFileContents.Length, out keyBlob, out keyBlobByteCount); } } byte[] pubKey = new byte[keyBlobByteCount]; Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount); strongName.StrongNameFreeBuffer(keyBlob); var result = pubKey.AsImmutableOrNull(); s_lastSeenKeyPair = Tuple.Create(keyFileContents, result); return(result); } catch (Exception ex) { throw new IOException(ex.Message); } }