Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from google/pr/2
Browse files Browse the repository at this point in the history
Merge dolph's file permission change, plus a commit to make it clearer.
  • Loading branch information
divegeek committed Feb 28, 2015
2 parents 40d2f6f + dccf895 commit 3e662ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion java/code/src/org/keyczar/GenericKeyczar.java
Expand Up @@ -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
Expand All @@ -307,6 +307,16 @@ 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
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();
} catch (IOException e) {
Expand Down
14 changes: 12 additions & 2 deletions java/code/src/org/keyczar/KeyczarTool.java
Expand Up @@ -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
Expand Down Expand Up @@ -367,6 +367,16 @@ private static void create(String locationFlag, String nameFlag,
}
try {
FileOutputStream metaOutput = new FileOutputStream(file);

// only allow the file owner to read/write the file
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();
} catch (IOException e) {
Expand Down

0 comments on commit 3e662ad

Please sign in to comment.