Skip to content

Commit

Permalink
Encryption settings from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Aug 27, 2020
1 parent 216a920 commit 6515d2f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 21 deletions.
11 changes: 11 additions & 0 deletions BUILD
Expand Up @@ -17,6 +17,7 @@ java_library(
"@maven//:org_bouncycastle_bcprov_jdk15on",
"@maven//:org_bitlet_weupnp",
"@maven//:io_netty_netty_handler",
"@com_google_protobuf//:protobuf_java_util",
":protolib",
],
)
Expand All @@ -29,6 +30,15 @@ java_binary(
],
)

java_binary(
name = "JsonTest",
main_class = "snowblossom.channels.JsonTest",
runtime_deps = [
":channelslib",
],
)


java_binary(
name = "ChannelIceLeaf",
main_class = "snowblossom.channels.iceleaf.ChannelIceLeaf",
Expand Down Expand Up @@ -147,6 +157,7 @@ java_test(
"@snowblossom//lib:lib",
"@snowblossom//client:client",
"@duckutil//:duckutil_lib",
"@com_google_protobuf//:protobuf_java_util",
":channelslib",
":protolib",
],
Expand Down
26 changes: 26 additions & 0 deletions protolib/channels.proto
Expand Up @@ -343,3 +343,29 @@ message DHTStrategy
int64 period = 3; // Might have meaning depending on mode
int64 period_mod = 4; // might have meaning depending on mode
}

message EncryptedChannelConfig
{
string protected_path = 1;
Offer offer = 2;


}
message Offer
{
enum OfferMode {
UNDEFINED = 0;
FOREVER_ACCESS = 1;
}
OfferMode offer_mode = 10;
string offer_id = 11;
map<string, OfferCurrency> offer_price = 12;

}

message OfferCurrency
{
double price = 1;
string address = 2;
}

15 changes: 1 addition & 14 deletions src/ChannelAccess.java
Expand Up @@ -124,20 +124,7 @@ public void createBlockForFiles(File base_path)
{
FileBlockImportSettings settings = new FileBlockImportSettings(ctx, base_path, node.getUserWalletDB(), null);

String key_id = ChannelCipherUtils.getCommonKeyID(ctx);

if (key_id != null)
{

SymmetricKey key = ChannelCipherUtils.getKeyFromChannel(ctx, key_id, node.getUserWalletDB().getKeys(0));
settings.setSymmetricKey(key);

settings.setEncryptPrefix("/prot/");


}


settings.setupEncrypt(ctx, node);

BlockGenUtils.createBlockForFiles(settings);
}
Expand Down
43 changes: 43 additions & 0 deletions src/FileBlockImportSettings.java
Expand Up @@ -4,6 +4,16 @@
import snowblossom.node.StatusInterface;
import snowblossom.proto.WalletDatabase;
import snowblossom.util.proto.SymmetricKey;
import snowblossom.lib.ValidationException;
import snowblossom.channels.proto.EncryptedChannelConfig;


import com.google.protobuf.util.JsonFormat;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;


public class FileBlockImportSettings
{
Expand Down Expand Up @@ -47,7 +57,40 @@ public boolean encrypt(String prefix)

if (prefix.startsWith(encrypt_prefix)) return true;
return false;
}

public void setupEncrypt(ChannelContext ctx, ChannelNode node)
throws ValidationException
{
File settings_file = new File(base_path, "encryption.json");

if (settings_file.exists())
{
try
{
EncryptedChannelConfig.Builder encrypted_config = EncryptedChannelConfig.newBuilder();

JsonFormat.Parser parser = JsonFormat.parser();
Reader input = new InputStreamReader(new FileInputStream(settings_file));
parser.merge(input, encrypted_config);

String key_id = ChannelCipherUtils.getCommonKeyID(ctx);
if (key_id == null)
{
ChannelCipherUtils.establishCommonKey(node, ctx);
key_id = ChannelCipherUtils.getCommonKeyID(ctx);
}

SymmetricKey key = ChannelCipherUtils.getKeyFromChannel(ctx, key_id, signer.getKeys(0));
setSymmetricKey(key);

setEncryptPrefix(encrypted_config.getProtectedPath());
}
catch(java.io.IOException e)
{
throw new ValidationException(e);
}
}

}

Expand Down
33 changes: 33 additions & 0 deletions src/JsonTest.java
@@ -0,0 +1,33 @@
package snowblossom.channels;

import com.google.protobuf.util.JsonFormat;

import snowblossom.channels.proto.EncryptedChannelConfig;
import snowblossom.channels.proto.OfferCurrency;
import snowblossom.channels.proto.Offer;


public class JsonTest
{
public static void main(String args[]) throws Exception
{

JsonFormat.Printer printer = JsonFormat.printer();

EncryptedChannelConfig.Builder conf = EncryptedChannelConfig.newBuilder();

conf.setProtectedPath("/prot/");

Offer.Builder offer = Offer.newBuilder();
offer.setOfferMode( Offer.OfferMode.FOREVER_ACCESS );
offer.putOfferPrice("SNOW", OfferCurrency.newBuilder().setPrice(1.0).setAddress("snow:x").build());

conf.setOffer(offer.build());

System.out.println(printer.print(conf.build() ));



}

}
14 changes: 9 additions & 5 deletions src/iceleaf/ChannelNodePanel.java
Expand Up @@ -313,11 +313,15 @@ public void threadActionPerformed(ActionEvent e)
String base_upload = ice_leaf_prefs.get("channel_upload_path", null);
File channel_upload_path = new File(base_upload, cid.asStringWithoutColon());

BlockGenUtils.createBlockForFiles( new FileBlockImportSettings(
node.getChannelSubscriber().openChannel(cid),
channel_upload_path,
node.getUserWalletDB(),
this));
FileBlockImportSettings file_settings = new FileBlockImportSettings(
node.getChannelSubscriber().openChannel(cid),
channel_upload_path,
node.getUserWalletDB(),
this);

file_settings.setupEncrypt(node.getChannelSubscriber().openChannel(cid), node);

BlockGenUtils.createBlockForFiles( file_settings );
}
catch(Throwable t)
{
Expand Down
26 changes: 24 additions & 2 deletions test/CipherChannelTest.java
Expand Up @@ -21,6 +21,10 @@
import snowblossom.lib.*;
import snowblossom.proto.WalletKeyPair;
import snowblossom.proto.AddressSpec;
import java.io.PrintStream;

import snowblossom.channels.proto.EncryptedChannelConfig;
import com.google.protobuf.util.JsonFormat;

public class CipherChannelTest
{
Expand All @@ -38,6 +42,9 @@ public static void loadProvider()
public static final int FILES_TO_SYNC=25;
public static final int MAX_FILE_SIZE=4000000;





@Test
public void testCipherChannel()
Expand Down Expand Up @@ -65,10 +72,25 @@ public void testCipherChannel()
ChannelContext ctx_b = node_b.getChannelSubscriber().openChannel(cid);
ChannelContext ctx_c = node_c.getChannelSubscriber().openChannel(cid);
ChannelContext ctx_d = node_c.getChannelSubscriber().openChannel(cid);

File file_dir = test_folder.newFolder();

{
JsonFormat.Printer printer = JsonFormat.printer();
EncryptedChannelConfig.Builder conf = EncryptedChannelConfig.newBuilder();
conf.setProtectedPath("/prot/");

PrintStream file_out = new PrintStream(new FileOutputStream(new File(file_dir, "encryption.json")));

file_out.println(printer.print(conf.build()));
file_out.close();
}


Assert.assertNull(ChannelCipherUtils.getCommonKeyID(ctx_a));

ChannelCipherUtils.establishCommonKey(node_a, ctx_a);
a_a.createBlockForFiles(file_dir);
//ChannelCipherUtils.establishCommonKey(node_a, ctx_a);

Assert.assertNotNull(ChannelCipherUtils.getCommonKeyID(ctx_a));

Expand All @@ -80,7 +102,6 @@ public void testCipherChannel()

String key_id = ChannelCipherUtils.getCommonKeyID(ctx_a);

File file_dir = test_folder.newFolder();

TreeMap<String, ChainHash> plain_file_map = new TreeMap<>();
TreeMap<String, ChainHash> prot_file_map = new TreeMap<>();
Expand Down Expand Up @@ -172,6 +193,7 @@ public void testCipherChannel()
ChainHash hash = prot_file_map.get(name);
Assert.assertEquals( hash, download(webport_a, cid, "prot/" + name));
Assert.assertEquals( hash, download(webport_b, cid, "prot/" + name));
//Assert.assertEquals( hash, download(webport_c, cid, "prot/" + name));
Assert.assertEquals( hash, download(webport_d, cid, "prot/" + name));
}

Expand Down

0 comments on commit 6515d2f

Please sign in to comment.