public ProtectedMemorySecretFactory(IConfiguration configuration)
        {
            Debug.WriteLine("ProtectedMemorySecretFactory ctor");
            lock (allocatorLock)
            {
                this.configuration = configuration;
                if (allocator != null)
                {
                    refCount++;
                    Debug.WriteLine($"ProtectedMemorySecretFactory: Using existing allocator refCount: {refCount}");
                    return;
                }

                allocator = DetectViaRuntimeInformation(configuration)
                            ?? DetectViaOsVersionPlatform(configuration)
                            ?? DetectOsDescription(configuration);

                if (allocator == null)
                {
                    throw new PlatformNotSupportedException("Could not detect supported platform for protected memory");
                }

                Debug.WriteLine("ProtectedMemorySecretFactory: Created new allocator");
                refCount++;
                Debug.WriteLine($"ProtectedMemorySecretFactory: Using new allocator refCount: {refCount}");
            }
        }
Ejemplo n.º 2
0
        private static void Release(IProtectedMemoryAllocator pm, ref IntPtr ptr, ulong len)
        {
            #pragma warning disable 162
            if (Debug.On)
            {
                // TODO Add/uncomment this when we refactor logging to use static creation
                // log.LogDebug("closing: {pointer}", ptr);
            }
            #pragma warning restore 162

            IntPtr oldPtr = Interlocked.Exchange(ref ptr, IntPtr.Zero);
            if (oldPtr != IntPtr.Zero)
            {
                try
                {
                    pm.SetReadWriteAccess(oldPtr, len);

                    pm.ZeroMemory(oldPtr, len);
                }
                finally
                {
                    pm.Free(oldPtr, len);
                }
            }
        }
        public void Dispose()
        {
            Debug.WriteLine("ProtectedMemorySecretFactory: Dispose");
            lock (allocatorLock)
            {
                if (allocator == null)
                {
                    throw new Exception("ProtectedMemorySecretFactory.Dispose: Allocator is null!");
                }

                Debug.WriteLine("ProtectedMemorySecretFactory: Allocator is not null");
                refCount--;
                if (refCount == 0)
                {
                    Debug.WriteLine("ProtectedMemorySecretFactory: refCount is zero, disposing");
                    allocator.Dispose();
                    Debug.WriteLine("ProtectedMemorySecretFactory: Setting allocator to null");
                    allocator = null;
                }
                else
                {
                    Debug.WriteLine($"ProtectedMemorySecretFactory: New refCount is {refCount}");
                }
            }
        }
Ejemplo n.º 4
0
 private void TestNullConfiguration(IProtectedMemoryAllocator protectedMemoryAllocator)
 {
     Debug.WriteLine("TestNullConfiguration");
     using (var secret = new ProtectedMemorySecret(new byte[] { 0, 1 }, protectedMemoryAllocator, null))
     {
     }
 }
Ejemplo n.º 5
0
        internal ProtectedMemorySecret(byte[] sourceBytes, IProtectedMemoryAllocator allocator)
        {
            this.allocator = allocator;

            length  = (ulong)sourceBytes.Length;
            pointer = this.allocator.Alloc((ulong)sourceBytes.Length);

            if (pointer == IntPtr.Zero)
            {
                throw new ProtectedMemoryAllocationFailedException("Protected memory allocation failed");
            }

            try
            {
                Marshal.Copy(sourceBytes, 0, pointer, (int)length);
                this.allocator.SetNoAccess(pointer, length);
            }
            catch
            {
                // Shouldn't happen, but need to free memory to avoid leak if it does
                IntPtr oldPtr = Interlocked.Exchange(ref pointer, IntPtr.Zero);
                if (oldPtr != IntPtr.Zero)
                {
                    this.allocator.SetReadWriteAccess(oldPtr, length);
                    this.allocator.Free(oldPtr, length);
                }

                throw;
            }

            // Only clear the client's source buffer if we're successful
            SecureZeroMemory(sourceBytes);
        }
Ejemplo n.º 6
0
        private void TestWithSecretBytesWithClosedSecretShouldFail(IProtectedMemoryAllocator protectedMemoryAllocator)
        {
            Debug.WriteLine("TestWithSecretBytesWithClosedSecretShouldFail");
            byte[] secretBytes           = { 0, 1 };
            ProtectedMemorySecret secret =
                new ProtectedMemorySecret((byte[])secretBytes.Clone(), protectedMemoryAllocator, configuration);

            secret.Close();
            Assert.Throws <InvalidOperationException>(() => { secret.WithSecretBytes(decryptedBytes => true); });
        }
Ejemplo n.º 7
0
        private void TestWithSecretUtf8CharsWithClosedSecretShouldFail(IProtectedMemoryAllocator protectedMemoryAllocator)
        {
            Debug.WriteLine("TestWithSecretUtf8CharsWithClosedSecretShouldFail");
            char[] secretChars           = { 'a', 'b' };
            ProtectedMemorySecret secret =
                ProtectedMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration);

            secret.Close();
            Assert.Throws <InvalidOperationException>(() => { secret.WithSecretUtf8Chars(decryptedChars => true); });
        }
Ejemplo n.º 8
0
 public ProtectedMemorySecretTest()
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         protectedMemoryAllocatorController = new MacOSProtectedMemoryAllocatorLP64();
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         protectedMemoryAllocatorController = new LinuxProtectedMemoryAllocatorLP64();
     }
 }
Ejemplo n.º 9
0
 internal static ProtectedMemorySecret FromCharArray(char[] sourceChars, IProtectedMemoryAllocator allocator)
 {
     byte[] sourceBytes = Encoding.UTF8.GetBytes(sourceChars);
     try
     {
         return(new ProtectedMemorySecret(sourceBytes, allocator));
     }
     finally
     {
         SecureZeroMemory(sourceBytes);
     }
 }
Ejemplo n.º 10
0
 private void TestWithSecretUtf8CharsAction(IProtectedMemoryAllocator protectedMemoryAllocator)
 {
     Debug.WriteLine("TestWithSecretUtf8CharsAction");
     char[] secretChars = { 'a', 'b' };
     using (ProtectedMemorySecret secret =
                ProtectedMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration))
     {
         secret.WithSecretUtf8Chars(decryptedChars =>
         {
             Assert.Equal(secretChars, decryptedChars);
         });
     }
 }
Ejemplo n.º 11
0
 private void TestWithSecretBytesAction(IProtectedMemoryAllocator protectedMemoryAllocator)
 {
     Debug.WriteLine("TestWithSecretBytesAction");
     byte[] secretBytes = { 0, 1 };
     using (ProtectedMemorySecret secret =
                new ProtectedMemorySecret((byte[])secretBytes.Clone(), protectedMemoryAllocator, configuration))
     {
         secret.WithSecretBytes(decryptedBytes =>
         {
             Assert.Equal(secretBytes, decryptedBytes);
         });
     }
 }
Ejemplo n.º 12
0
        internal ProtectedMemorySecret(byte[] sourceBytes, IProtectedMemoryAllocator allocator, IConfiguration configuration)
        {
            Debug.WriteLine("ProtectedMemorySecret ctor");
            this.allocator     = allocator;
            this.configuration = configuration;

            if (configuration != null)
            {
                if (configuration["debugSecrets"] == "true")
                {
                    creationStackTrace = Environment.StackTrace;
                }

                if (configuration["requireSecretDisposal"] == "true")
                {
                    requireSecretDisposal = true;
                }
            }

            length  = (ulong)sourceBytes.Length;
            pointer = this.allocator.Alloc((ulong)sourceBytes.Length);

            if (pointer == IntPtr.Zero)
            {
                throw new ProtectedMemoryAllocationFailedException("Protected memory allocation failed");
            }

            try
            {
                Marshal.Copy(sourceBytes, 0, pointer, (int)length);
                this.allocator.SetNoAccess(pointer, length);
            }
            catch
            {
                try
                {
                    this.allocator.SetReadWriteAccess(pointer, length);
                }
                finally
                {
                    this.allocator.Free(pointer, length);
                    pointer = IntPtr.Zero;
                }

                throw;
            }

            // Only clear the client's source buffer if we're successful
            SecureZeroMemory(sourceBytes);
        }
Ejemplo n.º 13
0
 private void TestWithSecretIntPtrActionSuccess(IProtectedMemoryAllocator protectedMemoryAllocator)
 {
     Debug.WriteLine("TestWithSecretIntPtrActionSuccess");
     char[] secretChars = { 'a', 'b' };
     using (ProtectedMemorySecret secret =
                ProtectedMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration))
     {
         secret.WithSecretIntPtr((ptr, len) =>
         {
             Assert.NotEqual(ptr, IntPtr.Zero);
             Assert.True(len == 2);
         });
     }
 }
Ejemplo n.º 14
0
        private void TestWithSecretIntPtrDisposed(IProtectedMemoryAllocator protectedMemoryAllocator)
        {
            Debug.WriteLine("TestWithSecretIntPtrDisposed");
            char[] secretChars           = { 'a', 'b' };
            ProtectedMemorySecret secret =
                ProtectedMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration);

            secret.Dispose();
            Assert.Throws <InvalidOperationException>(() =>
            {
                secret.WithSecretIntPtr((ptr, len) =>
                {
                    return(true);
                });
            });
        }
Ejemplo n.º 15
0
        public ProtectedMemoryAllocatorTest()
        {
            Trace.Listeners.Clear();
            var consoleListener = new ConsoleTraceListener();

            Trace.Listeners.Add(consoleListener);

            configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "heapSize", "32000" },
                { "minimumAllocationSize", "128" },
            }).Build();

            Debug.WriteLine("ProtectedMemoryAllocatorTest ctor");
            protectedMemoryAllocator = GetPlatformAllocator(configuration);
        }
Ejemplo n.º 16
0
        private void TestAllocatorSetNoDumpFailure()
        {
            Debug.WriteLine("TestAllocatorSetNoDumpFailure");
            byte[] secretBytes = { 0, 1 };
            IProtectedMemoryAllocator allocator = null;

            // TODO : Need to determine if we can stub out the protectedMemoryAllocatorMock.
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                Mock <MacOSProtectedMemoryAllocatorLP64> protectedMemoryAllocatorMacOSMock =
                    new Mock <MacOSProtectedMemoryAllocatorLP64> {
                    CallBase = true
                };

                protectedMemoryAllocatorMacOSMock.Setup(x => x.SetNoDump(It.IsAny <IntPtr>(), It.IsAny <ulong>()))
                .Throws(new Exception());

                allocator = protectedMemoryAllocatorMacOSMock.Object;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Mock <LinuxOpenSSL11ProtectedMemoryAllocatorLP64> protectedMemoryAllocatorLinuxMock =
                    new Mock <LinuxOpenSSL11ProtectedMemoryAllocatorLP64>(configuration)
                {
                    CallBase = true
                };

                protectedMemoryAllocatorLinuxMock.Setup(x => x.SetNoDump(It.IsAny <IntPtr>(), It.IsAny <ulong>()))
                .Throws(new Exception());

                allocator = protectedMemoryAllocatorLinuxMock.Object;
            }
            else
            {
                return;
            }

            Assert.Throws <Exception>(() =>
            {
                ProtectedMemorySecret secret =
                    new ProtectedMemorySecret(secretBytes, allocator, configuration);

                secret.Close();
            });
        }
 public ProtectedMemoryAllocatorTest()
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         protectedMemoryAllocator = new LinuxProtectedMemoryAllocatorLP64();
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         protectedMemoryAllocator = new MacOSProtectedMemoryAllocatorLP64();
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         protectedMemoryAllocator = new WindowsProtectedMemoryAllocatorVirtualAlloc();
     }
     else
     {
         throw new NotSupportedException("Cannot determine platform for testing");
     }
 }
Ejemplo n.º 18
0
        private static void Release(IProtectedMemoryAllocator pm, ref IntPtr ptr, ulong len)
        {
#if DEBUG
            // TODO Add/uncomment this when we refactor logging to use static creation
            // log.LogDebug("closing: {pointer}", ptr);
#endif
            IntPtr oldPtr = Interlocked.Exchange(ref ptr, IntPtr.Zero);
            if (oldPtr != IntPtr.Zero)
            {
                try
                {
                    pm.SetReadWriteAccess(oldPtr, len);
                }
                finally
                {
                    pm.Free(oldPtr, len);
                }
            }
        }
Ejemplo n.º 19
0
 public ProtectedMemorySecretFactory()
 {
     allocator = GetAllocator();
 }