From 3eff20eeb26572da5bd183f1dec8477444800f97 Mon Sep 17 00:00:00 2001 From: Dolph Mathews Date: Fri, 13 Feb 2015 00:37:10 +0000 Subject: [PATCH 1/2] Create metadata and key files in mode 0600 (owner-only read/write) Fixes issue 157 --- java/code/src/org/keyczar/GenericKeyczar.java | 10 +++++++++- java/code/src/org/keyczar/KeyczarTool.java | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/java/code/src/org/keyczar/GenericKeyczar.java b/java/code/src/org/keyczar/GenericKeyczar.java index 8369c81c..0ebbe143 100644 --- a/java/code/src/org/keyczar/GenericKeyczar.java +++ b/java/code/src/org/keyczar/GenericKeyczar.java @@ -296,7 +296,7 @@ void writeEncrypted(String location, Encrypter encrypter) } /** - * Utility function to write given data to a file at given location. + * Utility function to securely write given data to a file at given location. * * @param data String data to be written * @param location String pathname of destination file @@ -307,6 +307,14 @@ void writeFile(String data, String location) File outputFile = new File(location); try { FileWriter writer = new FileWriter(outputFile); + + // only allow the file owner to read/write the file + outputFile.setReadable(false, false); + outputFile.setReadable(true, true); + outputFile.setWritable(false, false); + outputFile.setWritable(true, true); + outputFile.setExecutable(false, false); + writer.write(data); writer.close(); } catch (IOException e) { diff --git a/java/code/src/org/keyczar/KeyczarTool.java b/java/code/src/org/keyczar/KeyczarTool.java index 0c6dac9f..9000324e 100644 --- a/java/code/src/org/keyczar/KeyczarTool.java +++ b/java/code/src/org/keyczar/KeyczarTool.java @@ -304,8 +304,8 @@ private static RsaPadding getPadding(String paddingFlag) throws KeyczarException /** * Creates a new KeyMetadata object, deciding its name, purpose and type - * based on command line flags. Outputs its JSON representation in a file - * named meta in the directory given by the location flag. + * based on command line flags. Outputs its JSON representation in a secure + * file named meta in the directory given by the location flag. * @param asymmetricFlag * @param purposeFlag * @param nameFlag @@ -367,6 +367,14 @@ private static void create(String locationFlag, String nameFlag, } try { FileOutputStream metaOutput = new FileOutputStream(file); + + // only allow the file owner to read/write the file + file.setReadable(false, false); + file.setReadable(true, true); + file.setWritable(false, false); + file.setWritable(true, true); + file.setExecutable(false, false); + metaOutput.write(kmd.toString().getBytes(Keyczar.DEFAULT_ENCODING)); metaOutput.close(); } catch (IOException e) { From dccf8953e9efff71a34ddaa432eaa9784bc64f60 Mon Sep 17 00:00:00 2001 From: Shawn Willden Date: Sat, 28 Feb 2015 08:48:33 -0700 Subject: [PATCH 2/2] Explain file permission args Added some named variables to clarify the meaning of the file permission method arguments. --- java/code/src/org/keyczar/GenericKeyczar.java | 12 +++++++----- java/code/src/org/keyczar/KeyczarTool.java | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/java/code/src/org/keyczar/GenericKeyczar.java b/java/code/src/org/keyczar/GenericKeyczar.java index 0ebbe143..cdda603f 100644 --- a/java/code/src/org/keyczar/GenericKeyczar.java +++ b/java/code/src/org/keyczar/GenericKeyczar.java @@ -309,11 +309,13 @@ void writeFile(String data, String location) FileWriter writer = new FileWriter(outputFile); // only allow the file owner to read/write the file - outputFile.setReadable(false, false); - outputFile.setReadable(true, true); - outputFile.setWritable(false, false); - outputFile.setWritable(true, true); - outputFile.setExecutable(false, false); + final boolean appliesToAll = false; + final boolean appliesToOwner = true; + outputFile.setReadable(false, appliesToAll); + outputFile.setReadable(true, appliesToOwner); + outputFile.setWritable(false, appliesToAll); + outputFile.setWritable(true, appliesToOwner); + outputFile.setExecutable(false, appliesToAll); writer.write(data); writer.close(); diff --git a/java/code/src/org/keyczar/KeyczarTool.java b/java/code/src/org/keyczar/KeyczarTool.java index 9000324e..f7256afa 100644 --- a/java/code/src/org/keyczar/KeyczarTool.java +++ b/java/code/src/org/keyczar/KeyczarTool.java @@ -369,11 +369,13 @@ private static void create(String locationFlag, String nameFlag, FileOutputStream metaOutput = new FileOutputStream(file); // only allow the file owner to read/write the file - file.setReadable(false, false); - file.setReadable(true, true); - file.setWritable(false, false); - file.setWritable(true, true); - file.setExecutable(false, false); + final boolean appliesToAll = false; + final boolean appliesToOwner = true; + file.setReadable(false, appliesToAll); + file.setReadable(true, appliesToOwner); + file.setWritable(false, appliesToAll); + file.setWritable(true, appliesToOwner); + file.setExecutable(false, appliesToAll); metaOutput.write(kmd.toString().getBytes(Keyczar.DEFAULT_ENCODING)); metaOutput.close();