-
Notifications
You must be signed in to change notification settings - Fork 75
Secure keystores with filesystem acl, use pkcs12 #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.attribute.PosixFilePermission; | ||
| import java.nio.file.attribute.PosixFilePermissions; | ||
| import java.util.EnumSet; | ||
| import java.util.Set; | ||
|
|
||
| import com.sun.jna.Platform; | ||
|
|
||
| public class FilePermissionUtil { | ||
|
|
||
| private static final Set<PosixFilePermission> OWNER_ONLY = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE); | ||
|
|
||
| private FilePermissionUtil() {} | ||
|
|
||
| /* | ||
| * Creates a file that only the account the server runs as, and whoever administers the machine, | ||
| * may read or write. | ||
| */ | ||
| public static void createOwnerOnlyFile(File file) throws IOException { | ||
| Path path = file.toPath(); | ||
| File parent = file.getParentFile(); | ||
|
|
||
| if (parent != null) { | ||
| Files.createDirectories(parent.toPath()); | ||
| } | ||
|
|
||
| if (Platform.isWindows()) { | ||
| WindowsFilePermissionUtil.createRestrictedFile(path); | ||
| } else { | ||
| Files.createFile(path, PosixFilePermissions.asFileAttribute(OWNER_ONLY)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.FileAlreadyExistsException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import com.sun.jna.Native; | ||
| import com.sun.jna.Pointer; | ||
| import com.sun.jna.platform.win32.Kernel32; | ||
| import com.sun.jna.platform.win32.Kernel32Util; | ||
| import com.sun.jna.platform.win32.WinBase; | ||
| import com.sun.jna.platform.win32.WinError; | ||
| import com.sun.jna.platform.win32.WinNT; | ||
| import com.sun.jna.platform.win32.WinNT.HANDLE; | ||
| import com.sun.jna.ptr.IntByReference; | ||
| import com.sun.jna.ptr.PointerByReference; | ||
| import com.sun.jna.win32.StdCallLibrary; | ||
| import com.sun.jna.win32.W32APIOptions; | ||
| import com.sun.security.auth.module.NTSystem; | ||
|
|
||
| /* | ||
| * The Windows half of FilePermissionUtil, kept apart so that nothing here is loaded on a platform | ||
| * that has no Win32 API to call. | ||
| */ | ||
| class WindowsFilePermissionUtil { | ||
|
|
||
| /* | ||
| * Full control to creator, LocalSystem and the local administrators. Matches | ||
| * user profile directories. | ||
| */ | ||
| private static final String SDDL = "D:P(A;;FA;;;{CURRENTUSERSID})(A;;FA;;;SY)(A;;FA;;;BA)"; | ||
|
|
||
| private static final int SDDL_REVISION_1 = 1; | ||
|
|
||
| /* | ||
| * The SDDL conversions are not part of JNA's Advapi32 mapping. | ||
| */ | ||
| private interface Advapi32Sddl extends StdCallLibrary { | ||
| Advapi32Sddl INSTANCE = Native.loadLibrary("Advapi32", Advapi32Sddl.class, W32APIOptions.DEFAULT_OPTIONS); | ||
|
|
||
| boolean ConvertStringSecurityDescriptorToSecurityDescriptor(String sddl, int revision, PointerByReference securityDescriptor, IntByReference size); | ||
| } | ||
|
|
||
| private WindowsFilePermissionUtil() {} | ||
|
|
||
| /* | ||
| * Creates the file with a DACL that only lets the account the server runs as and the machine's | ||
| * administrators near it. The DACL is handed to CreateFile rather than applied afterwards, so | ||
| * the file is never briefly readable by anyone else. | ||
| */ | ||
| static void createRestrictedFile(Path path) throws IOException { | ||
| Pointer securityDescriptor = buildSecurityDescriptor(); | ||
|
|
||
| try { | ||
| createFile(path, securityDescriptor); | ||
| } finally { | ||
| Kernel32.INSTANCE.LocalFree(securityDescriptor); | ||
| } | ||
| } | ||
|
|
||
| private static Pointer buildSecurityDescriptor() throws IOException { | ||
| PointerByReference securityDescriptor = new PointerByReference(); | ||
|
|
||
| String sddl = SDDL.replace("{CURRENTUSERSID}", new NTSystem().getUserSID()); | ||
| if (!Advapi32Sddl.INSTANCE.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, SDDL_REVISION_1, securityDescriptor, null)) { | ||
| throw lastError("Could not build a security descriptor from \"" + sddl + "\""); | ||
| } | ||
|
|
||
| return securityDescriptor.getValue(); | ||
| } | ||
|
|
||
| private static void createFile(Path path, Pointer securityDescriptor) throws IOException { | ||
| WinBase.SECURITY_ATTRIBUTES securityAttributes = new WinBase.SECURITY_ATTRIBUTES(); | ||
| securityAttributes.lpSecurityDescriptor = securityDescriptor; | ||
| securityAttributes.bInheritHandle = false; | ||
|
|
||
| HANDLE handle = Kernel32.INSTANCE.CreateFile(path.toAbsolutePath().toString(), WinNT.GENERIC_WRITE, 0, securityAttributes, WinNT.CREATE_NEW, WinNT.FILE_ATTRIBUTE_NORMAL, null); | ||
|
|
||
| if (WinBase.INVALID_HANDLE_VALUE.equals(handle)) { | ||
| // reported the same way as Files.createFile, so that callers need not care which is which | ||
| if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_FILE_EXISTS) { | ||
| throw new FileAlreadyExistsException(path.toString()); | ||
| } | ||
|
|
||
| throw lastError("Could not create " + path); | ||
| } | ||
|
|
||
| Kernel32.INSTANCE.CloseHandle(handle); | ||
| } | ||
|
|
||
| private static IOException lastError(String message) { | ||
| return new IOException(message + ": " + Kernel32Util.formatMessage(Kernel32.INSTANCE.GetLastError())); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assume.assumeFalse; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.attribute.PosixFilePermissions; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.commons.io.IOUtils; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| import com.sun.jna.Platform; | ||
|
|
||
| public class FilePermissionUtilTest { | ||
|
|
||
| @Rule | ||
| public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
|
||
|
|
||
| @Test | ||
| public void testPosixPermissions() throws Exception { | ||
| assumeFalse("Test only applies on POSIX filesystems", Platform.isWindows()); | ||
| File file = new File(temporaryFolder.getRoot(), "keystore.p12"); | ||
|
|
||
| FilePermissionUtil.createOwnerOnlyFile(file); | ||
|
|
||
| assertEquals("rw-------", PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath()))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWindowsPermissions() throws Exception { | ||
| assumeTrue("Test only applies on windows", Platform.isWindows()); | ||
| assumeTrue("Test assumes english locale", System.getProperty("user.language").equals("en")); | ||
| File file = new File(temporaryFolder.getRoot(), "keystore.p12"); | ||
| String path = file.getAbsolutePath(); | ||
|
|
||
| FilePermissionUtil.createOwnerOnlyFile(file); | ||
| var icaclsOutput = system("icacls", path); | ||
| assertContains(icaclsOutput, System.getProperty("user.name") + ":(F)"); | ||
| assertContains(icaclsOutput, "BUILTIN\\Administrators:(F)"); | ||
| assertContains(icaclsOutput, "NT AUTHORITY\\SYSTEM:(F)"); | ||
| assertNotContains(icaclsOutput, "Everyone:"); | ||
| assertNotContains(icaclsOutput, "Users:"); | ||
| assertNotContains(icaclsOutput, "(I)" /* inherited permissions */); | ||
| } | ||
|
|
||
| private void assertContains(String output, String expected) { | ||
| assertTrue("Expected output to contain '" + expected + "' but was '" + output + "'", output.contains(expected)); | ||
| } | ||
|
|
||
| private void assertNotContains(String output, String expected) { | ||
| assertFalse("Expected output to not contain '" + expected + "' but was '" + output + "'", output.contains(expected)); | ||
| } | ||
|
|
||
| private String system(String command, String... arguments) throws Exception { | ||
| List<String> commandLine = new ArrayList<String>(); | ||
| commandLine.add(command); | ||
| commandLine.addAll(Arrays.asList(arguments)); | ||
|
|
||
| Process process = new ProcessBuilder(commandLine).redirectErrorStream(true).start(); | ||
| String output = IOUtils.toString(process.getInputStream(), Charset.defaultCharset()); | ||
|
|
||
| assertEquals(output, 0, process.waitFor()); | ||
| return output; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,10 @@ password.reuselimit = 0 | |
| version = 4.6.0 | ||
|
|
||
| # keystore | ||
| keystore.path = ${dir.appdata}/keystore.jks | ||
| keystore.storepass = 81uWxplDtB | ||
| keystore.keypass = 81uWxplDtB | ||
| keystore.type = JCEKS | ||
| keystore.path = ${dir.appdata}/keystore.pfx | ||
| keystore.storepass = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per Tonys comments from chat - add a comment in the props file explaining how this is generated if its blank.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did on line 28. With pkcs12, there is no need to have a password, so the blank is not a sentinel value - there just is no password.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry. I was off: this is a test resource, not a published asset. |
||
| keystore.keypass = | ||
| keystore.type = PKCS12 | ||
|
|
||
| # server | ||
| http.contextpath = / | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non blocking comment - I checked if file utils does this for us. It does not. I dont live introducing a util class but its appropriate here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
touchto create the file? But then you still gotta mess with perms. https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#touch(java.io.File)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see an alternative here for any major portion.