From ead50cfbb310feaaf6dfc7da6b59165428e4833a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 17:11:19 +0100 Subject: Create QuerySigner class --- src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java new file mode 100644 index 0000000..e5e561d --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -0,0 +1,4 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +public class QuerySigner { +} -- cgit v1.2.3 From da5df3957ba41d28256ab8183aae0dd3b6e0dcba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 21:05:28 +0100 Subject: Add query signer architecture --- .../mimuw/cloudatlas/agent/ApiImplementation.java | 4 +- .../cloudatlas/agent/NewApiImplementation.java | 4 +- src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java | 4 +- .../mimuw/cloudatlas/client/ClientController.java | 31 ++++-- .../pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 22 ++++ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 26 +++++ .../querysigner/QuerySignerApiImplementation.java | 112 +++++++++++++++++++++ .../cloudatlas/querysignerapi/QuerySignerApi.java | 17 ++++ 8 files changed, 203 insertions(+), 17 deletions(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java index d2e808a..fe3136d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java @@ -60,7 +60,7 @@ public class ApiImplementation implements Api { } } - public void installQuery(String name, String queryCode) throws RemoteException { + public void installQuery(String name, String queryCode, byte[] querySignature) throws RemoteException { Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*"); Matcher matcher = queryNamePattern.matcher(name); if (!matcher.matches()) { @@ -85,7 +85,7 @@ public class ApiImplementation implements Api { } } - public void uninstallQuery(String queryName) throws RemoteException { + public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException { uninstallQueryInHierarchy(root, new Attribute(queryName)); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index b293446..450382d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -79,7 +79,7 @@ public class NewApiImplementation implements Api { } } - public void installQuery(String name, String queryCode) throws RemoteException { + public void installQuery(String name, String queryCode, byte[] querySignature) throws RemoteException { Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*"); Matcher matcher = queryNamePattern.matcher(name); if (!matcher.matches()) { @@ -98,7 +98,7 @@ public class NewApiImplementation implements Api { } } - public void uninstallQuery(String queryName) throws RemoteException { + public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException { try { Attribute attributeName = new Attribute(queryName); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java index c62ee39..63c7f54 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java @@ -27,9 +27,9 @@ public interface Api extends Remote { public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException; - public void installQuery(String queryName, String query) throws RemoteException; + public void installQuery(String queryName, String query, byte[] querySignature) throws RemoteException; - public void uninstallQuery(String queryName) throws RemoteException; + public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException; public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java index 4019696..14f531e 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.model.*; +import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import java.net.InetAddress; import java.rmi.registry.LocateRegistry; @@ -32,17 +33,22 @@ import java.util.*; @Controller public class ClientController { - private Api api; - + private Api agentApi; + private QuerySignerApi querySignerApi; + private Map querySignatures; private Map attributes; private String currentZoneName; private static final int MAX_ENTRIES = 10; ClientController() { try { - String hostname = System.getProperty("agent_hostname"); - Registry registry = LocateRegistry.getRegistry(hostname); - this.api = (Api) registry.lookup("Api"); + String agentHostname = System.getProperty("agent_hostname"); + Registry registry = LocateRegistry.getRegistry(agentHostname); + this.agentApi = (Api) registry.lookup("Api"); + + String querySignerHostname = System.getProperty("querysigner_hostname"); + Registry querySignerRegistry = LocateRegistry.getRegistry(querySignerHostname); + this.querySignerApi = (QuerySignerApi) querySignerRegistry.lookup("QuerySignerApi"); } catch (Exception e) { System.err.println("Client exception:"); e.printStackTrace(); @@ -54,6 +60,7 @@ public class ClientController { } }; this.currentZoneName = System.getProperty("zone_path"); + this.querySignatures = new HashMap<>(); fetchAttributeData(); // fetch attribute data as early as possible } @@ -74,7 +81,9 @@ public class ClientController { boolean success = true; try { - this.api.installQuery(queryObject.getName(), queryObject.getValue()); + byte[] querySignature = this.querySignerApi.signQuery(queryObject.getName(), queryObject.getValue()); + querySignatures.put(queryObject.getName(), querySignature); + this.agentApi.installQuery(queryObject.getName(), queryObject.getValue(), querySignature); } catch (Exception e) { success = false; System.err.println("Client exception:"); @@ -99,7 +108,7 @@ public class ClientController { boolean success = true; try { - this.api.uninstallQuery(queryObject.getName()); + this.agentApi.uninstallQuery(queryObject.getName(), querySignatures.get(queryObject.getName())); } catch (Exception e) { success = false; System.err.println("Client exception:"); @@ -153,7 +162,7 @@ public class ClientController { try { contactObjects = parseContactsString(contactsObject); - this.api.setFallbackContacts(contactObjects); + this.agentApi.setFallbackContacts(contactObjects); } catch (Exception e) { success = false; System.err.println("Client exception:"); @@ -284,7 +293,7 @@ public class ClientController { try { attributeValue = parseAttributeValue(attributeObject); - api.setAttributeValue( + agentApi.setAttributeValue( attributeObject.getZoneName(), attributeObject.getAttributeName(), attributeValue); @@ -309,7 +318,7 @@ public class ClientController { String availableZonesString = ""; try { - availableZones = api.getZoneSet(); + availableZones = agentApi.getZoneSet(); availableZonesString = availableZones.toString().substring(1, availableZones.toString().length() - 1); } catch (Exception e) { success = false; @@ -336,7 +345,7 @@ public class ClientController { try { if (!this.currentZoneName.isEmpty()) { - attribData = api.getZoneAttributeValues(this.currentZoneName); + attribData = agentApi.getZoneAttributeValues(this.currentZoneName); currentTime = new ValueTime(System.currentTimeMillis()); this.attributes.put(currentTime, attribData); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index 82e1602..d203e99 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -15,6 +15,28 @@ public class ValueQuery extends Value { private String code; // Parsed query private Program query; + + public byte[] getSignature() { + return signature; + } + + public void setSignature(byte[] signature) { + this.signature = signature; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + // Query signature + private byte[] signature; + // Query signing timestamp + private long timestamp; + /** * Constructs a new ValueQuery object. * diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index e5e561d..69a25d7 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -1,4 +1,30 @@ package pl.edu.mimuw.cloudatlas.querysigner; +import pl.edu.mimuw.cloudatlas.agent.EventBus; +import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; + +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + public class QuerySigner { + + public static void runRegistry() { + try { + QuerySignerApiImplementation api = new QuerySignerApiImplementation(); + QuerySignerApi apiStub = + (QuerySignerApi) UnicastRemoteObject.exportObject(api, 0); + Registry registry = LocateRegistry.getRegistry(); + registry.rebind("QuerySignerApi", apiStub); + System.out.println("QuerySigner: api bound"); + } catch (Exception e) { + System.err.println("QuerySigner registry initialization exception:"); + e.printStackTrace(); + } + } + + public static void main(String[] args) { + runRegistry(); + } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java new file mode 100644 index 0000000..38a86c6 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -0,0 +1,112 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.rmi.RemoteException; +import java.security.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class QuerySignerApiImplementation implements QuerySignerApi { + private PublicKey publicKey; + private PrivateKey privateKey; + private final static String ENCRYPTION_ALGORITHM = "RSA"; + private final static int NUM_KEY_BITS = 1024; + private Map queries; + private Set attribsSetByQueries; + + QuerySignerApiImplementation() { + this.queries = new HashMap<>(); + this.attribsSetByQueries = new HashSet<>(); + try { + generateKeys(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + + private String byteArrayToString(byte[] arr, int offset, int len) { + StringBuffer sb = new StringBuffer(); + for (int i = offset, n = Math.min(arr.length, offset + len); i < n; ++i) { + String hex = Integer.toHexString(0xFF & arr[i]); + if (hex.length() < 2) { + sb.append('0'); + } + sb.append(hex); + } + return sb.toString(); + } + + private void generateKeys() throws NoSuchAlgorithmException { + KeyPairGenerator keyGenerator = + KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM); + keyGenerator.initialize(NUM_KEY_BITS); + KeyPair keyPair = keyGenerator.generateKeyPair(); + this.privateKey = keyPair.getPrivate(); + this.publicKey = keyPair.getPublic(); + } + + private byte[] encryptQuery(String query) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher signCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); + signCipher.init(Cipher.ENCRYPT_MODE, privateKey); + byte[] encryptedBytes = signCipher.doFinal(query.getBytes()); + System.out.println( + "Bytes encrypted with " + ENCRYPTION_ALGORITHM + + ": " + byteArrayToString( + encryptedBytes, 0, encryptedBytes.length)); + return encryptedBytes; + } + + private String decryptQuery(byte[] encryptedQuery) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { + Cipher verifyCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); + verifyCipher.init(Cipher.DECRYPT_MODE, publicKey); + byte[] decryptedBytes = verifyCipher.doFinal(encryptedQuery); + System.out.println( + "Bytes decrypted with " + ENCRYPTION_ALGORITHM + + ": " + byteArrayToString( + decryptedBytes, 0, decryptedBytes.length)); + return new String(decryptedBytes); + } + + @Override + public byte[] signQuery(String queryName, String queryCode) throws RemoteException { + try { + return encryptQuery(queryName + queryCode); + } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { + e.printStackTrace(); + throw new RemoteException(e.getLocalizedMessage()); + } + } + + @Override + public String checkQuery(byte[] encryptedQuery, String queryName, String queryCode) throws RemoteException { + try { + return decryptQuery(encryptedQuery); + } catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) { + e.printStackTrace(); + throw new RemoteException(e.getLocalizedMessage()); + } + } + + @Override + public PublicKey getPublicKey() throws RemoteException { + return publicKey; + } + + @Override + public void setPublicKey(PublicKey publicKey) throws RemoteException { + this.publicKey = publicKey; + } + + @Override + public byte[] getQuerySignature(String queryName) throws RemoteException { + return queries.get(queryName).getSignature(); + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java new file mode 100644 index 0000000..3c77c0a --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java @@ -0,0 +1,17 @@ +package pl.edu.mimuw.cloudatlas.querysignerapi; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.security.PublicKey; + +public interface QuerySignerApi extends Remote { + public byte[] signQuery(String queryName, String queryCode) throws RemoteException; + + public String checkQuery(byte[] encryptedQuery, String queryName, String queryCode) throws RemoteException; + + public PublicKey getPublicKey() throws RemoteException; + + public void setPublicKey(PublicKey publicKey) throws RemoteException; + + public byte[] getQuerySignature(String queryName) throws RemoteException; +} -- cgit v1.2.3 From edc0b9821f2c95b13eb04036c28a2cc8312f69c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:55:54 +0100 Subject: Make byte serializer class --- .../pl/edu/mimuw/cloudatlas/ByteSerializer.java | 180 +++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java b/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java new file mode 100644 index 0000000..0a86c0d --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java @@ -0,0 +1,180 @@ +package pl.edu.mimuw.cloudatlas; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import pl.edu.mimuw.cloudatlas.agent.messages.*; +import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; +import pl.edu.mimuw.cloudatlas.agent.modules.RecursiveScheduledTask; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; +import pl.edu.mimuw.cloudatlas.model.*; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.rmi.Remote; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.HashMap; +import java.util.LinkedHashMap; + +/** + * Serializes classes to and from byte arrays + */ +// TODO remove udupserializer +public class ByteSerializer { + private Kryo kryo; + + public ByteSerializer() { + kryo = new Kryo(); + kryo.setReferences(true); + kryo.setRegistrationRequired(true); + registerClasses(); + } + + private void registerClasses() { + + kryo.register(Inet4Address.class, new Serializer() { + + @Override + public void write(Kryo kryo, Output output, Object object) { + InetAddress ia = (InetAddress) object; + kryo.writeObject(output, ia.getAddress()); + } + + @Override + public Object read(Kryo kryo, Input input, Class type) { + try { + byte[] buf = kryo.readObject(input, byte[].class); + InetAddress addr = Inet4Address.getByAddress(buf); + return addr; + } catch (UnknownHostException e) { + System.out.println("Custom InetAddress read failed"); + e.printStackTrace(); + return null; + } + } + }); + + kryo.register(PathName.class, new Serializer() { + + @Override + public void write(Kryo kryo, Output output, Object object) { + PathName pn = (PathName) object; + kryo.writeObject(output, pn.getName()); + } + + @Override + public Object read(Kryo kryo, Input input, Class type) { + String addr = input.readString(); + return new PathName(addr); + } + }); + + kryo.register(ValueList.class, new Serializer() { + @Override + public void write(Kryo kryo, Output output, Object object) { + ValueList vl = (ValueList) object; + kryo.writeObject(output, ((TypeCollection) vl.getType()).getElementType()); + kryo.writeObject(output, vl.getValue()); + } + + @Override + public Object read(Kryo kryo, Input input, Class type) { + Type t = kryo.readObject(input, Type.class); + ArrayList list = kryo.readObject(input, ArrayList.class); + return new ValueList(list, t); + } + }); + + kryo.register(ValueSet.class, new Serializer() { + @Override + public void write(Kryo kryo, Output output, Object object) { + ValueSet vs = (ValueSet) object; + kryo.writeObject(output, ((TypeCollection) vs.getType()).getElementType()); + kryo.writeObject(output, vs.getValue()); + } + + @Override + public Object read(Kryo kryo, Input input, Class type) { + Type t = kryo.readObject(input, Type.class); + HashSet set = kryo.readObject(input, HashSet.class); + return new ValueSet(set, t); + } + }); + + // model + kryo.register(Value.class); + kryo.register(ValueBoolean.class); + kryo.register(ValueContact.class); + kryo.register(ValueDuration.class); + kryo.register(ValueInt.class); + kryo.register(ValueNull.class); + kryo.register(ValueQuery.class); + kryo.register(ValueSet.class); + kryo.register(ValueString.class); + kryo.register(ValueTime.class); + kryo.register(ValueUtils.class); + kryo.register(ZMI.class); + + kryo.register(Attribute.class); + kryo.register(AttributesMap.class); + kryo.register(AttributesUtil.class); + + kryo.register(Type.class); + kryo.register(TypeCollection.class); + kryo.register(TypePrimitive.class); + + // messages in chronological order so it's easier to keep track + kryo.register(AgentMessage.class); + kryo.register(AttributesMessage.class); + kryo.register(GetStateMessage.class); + kryo.register(HejkaMessage.class); + kryo.register(NoCoTamMessage.class); + kryo.register(QueryMessage.class); + kryo.register(QurnikMessage.class); + kryo.register(RemikMessage.class); + kryo.register(RemoveZMIMessage.class); + kryo.register(RequestStateMessage.class); + kryo.register(ResponseMessage.class); + kryo.register(RunQueriesMessage.class); + kryo.register(SetAttributeMessage.class); + kryo.register(StanikMessage.Type.class); + kryo.register(StanikMessage.class); + kryo.register(TimerSchedulerMessage.class); + kryo.register(UDUPMessage.class); + kryo.register(UpdateAttributesMessage.class); + kryo.register(UpdateQueriesMessage.class); + kryo.register(GossipGirlMessage.class); + kryo.register(GossipGirlMessage.Type.class); + kryo.register(RemoteGossipGirlMessage.class); + + // modules + kryo.register(TimerScheduledTask.class); + kryo.register(RecursiveScheduledTask.class); + + // other + kryo.register(byte[].class); + kryo.register(LinkedHashMap.class); + kryo.register(HashMap.class); + kryo.register(ModuleType.class); + } + + public Object deserialize(byte[] packetData, Class objClass) { + ByteArrayInputStream in = new ByteArrayInputStream(packetData); + Input kryoInput = new Input(in); + return kryo.readObject(kryoInput, objClass); + } + + public byte[] serialize(Object obj) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Output kryoOut = new Output(out); + kryo.writeObject(kryoOut, obj); + kryoOut.flush(); + kryoOut.close(); + return out.toByteArray(); + } +} -- cgit v1.2.3 From a813ba681dc3fe4853da14ee6d1e61c495f79104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:56:40 +0100 Subject: Make key generator skeleton --- .../java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java new file mode 100644 index 0000000..8581280 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java @@ -0,0 +1,38 @@ +package pl.edu.mimuw.cloudatlas; + +import java.security.*; + +public class KeyGenerator { + private final static int NUM_KEY_BITS = 1024; + private final static String ENCRYPTION_ALGORITHM = "RSA"; + + // TODO + // TODO read key values from files + // TODO gradlew + private static void printKeyToFile(byte[] keyValue, String fileName) { + + } + + private static void generateKeys() throws NoSuchAlgorithmException { + PublicKey publicKey; + PrivateKey privateKey; + + KeyPairGenerator keyGenerator = + KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM); + keyGenerator.initialize(NUM_KEY_BITS); + KeyPair keyPair = keyGenerator.generateKeyPair(); + + privateKey = keyPair.getPrivate(); + printKeyToFile(privateKey.getEncoded(), "query_signer"); + publicKey = keyPair.getPublic(); + printKeyToFile(publicKey.getEncoded(), "query_signer.pub"); + } + + public static void main() { + try { + generateKeys(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } +} -- cgit v1.2.3 From f6ada2aae355781e5e16534d3d2e7959cbac69c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:56:51 +0100 Subject: Separate query utils --- .../pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java new file mode 100644 index 0000000..6ec62f4 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java @@ -0,0 +1,16 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +import java.rmi.RemoteException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class QueryUtils { + + public static void validateQueryName(String queryName) throws RemoteException { + Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*"); + Matcher matcher = queryNamePattern.matcher(queryName); + if (!matcher.matches()) { + throw new RemoteException("Invalid query identifier"); + } + } +} -- cgit v1.2.3 From c97540a2640bc2d8ecffedb0e85f2d1e3d517eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:57:26 +0100 Subject: Change agent rmi api --- .../pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java | 13 +++++-------- .../pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java | 13 +++++-------- src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java | 5 +++-- 3 files changed, 13 insertions(+), 18 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java index fe3136d..e9dbb7e 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java @@ -28,6 +28,7 @@ import pl.edu.mimuw.cloudatlas.model.Type; import pl.edu.mimuw.cloudatlas.model.TypePrimitive; import pl.edu.mimuw.cloudatlas.model.ZMI; import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils; public class ApiImplementation implements Api { ZMI root; @@ -60,14 +61,9 @@ public class ApiImplementation implements Api { } } - public void installQuery(String name, String queryCode, byte[] querySignature) throws RemoteException { - Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*"); - Matcher matcher = queryNamePattern.matcher(name); - if (!matcher.matches()) { - throw new RemoteException("Invalid query identifier"); - } + public void installQuery(String name, ValueQuery query) throws RemoteException { + QueryUtils.validateQueryName(name); try { - ValueQuery query = new ValueQuery(queryCode); Attribute attributeName = new Attribute(name); installQueryInHierarchy(root, attributeName, query); executeAllQueries(root); @@ -85,7 +81,8 @@ public class ApiImplementation implements Api { } } - public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException { + public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException { + QueryUtils.validateQueryName(queryName); uninstallQueryInHierarchy(root, new Attribute(queryName)); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index 450382d..bf75210 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -22,6 +22,7 @@ import pl.edu.mimuw.cloudatlas.interpreter.Main; import pl.edu.mimuw.cloudatlas.interpreter.QueryResult; import pl.edu.mimuw.cloudatlas.model.*; import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils; public class NewApiImplementation implements Api { private EventBus eventBus; @@ -79,14 +80,9 @@ public class NewApiImplementation implements Api { } } - public void installQuery(String name, String queryCode, byte[] querySignature) throws RemoteException { - Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*"); - Matcher matcher = queryNamePattern.matcher(name); - if (!matcher.matches()) { - throw new RemoteException("Invalid query identifier"); - } + public void installQuery(String name, ValueQuery query) throws RemoteException { + QueryUtils.validateQueryName(name); try { - ValueQuery query = new ValueQuery(queryCode); Attribute attributeName = new Attribute(name); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); Map> queries = new HashMap(); @@ -98,7 +94,8 @@ public class NewApiImplementation implements Api { } } - public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException { + public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException { + QueryUtils.validateQueryName(queryName); try { Attribute attributeName = new Attribute(queryName); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java index 63c7f54..2936b13 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java @@ -6,6 +6,7 @@ import java.rmi.RemoteException; import pl.edu.mimuw.cloudatlas.model.Value; import pl.edu.mimuw.cloudatlas.model.ValueContact; import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; /** * @@ -27,9 +28,9 @@ public interface Api extends Remote { public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException; - public void installQuery(String queryName, String query, byte[] querySignature) throws RemoteException; + public void installQuery(String queryName, ValueQuery query) throws RemoteException; - public void uninstallQuery(String queryName, byte[] querySignature) throws RemoteException; + public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException; public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException; -- cgit v1.2.3 From 36cf47fd63352c67a5fdeea7a922c16f0856e9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:57:43 +0100 Subject: Add new fields to value query --- .../pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 39 ++++++++++++---------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index d203e99..26a5fbb 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -15,23 +15,6 @@ public class ValueQuery extends Value { private String code; // Parsed query private Program query; - - public byte[] getSignature() { - return signature; - } - - public void setSignature(byte[] signature) { - this.signature = signature; - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - // Query signature private byte[] signature; // Query signing timestamp @@ -47,17 +30,39 @@ public class ValueQuery extends Value { this.code = query; Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); this.query = (new parser(lex)).pProgram(); + this.signature = null; + this.timestamp = System.currentTimeMillis(); + } + + public ValueQuery(String query, byte[] querySignature) throws Exception { + this.code = query; + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + this.query = (new parser(lex)).pProgram(); + this.signature = querySignature; + this.timestamp = System.currentTimeMillis(); } private ValueQuery() { this.code = null; this.query = null; + this.signature = null; + this.timestamp = System.currentTimeMillis(); } public Program getQuery() { return query; } + public byte[] getSignature() { return signature; } + + public long getTimestamp() { return timestamp; } + + public void setTimestamp(long timestamp) { this.timestamp = timestamp; } + + public String getCode() { + return code; + } + @Override public Type getType() { return TypePrimitive.QUERY; -- cgit v1.2.3 From c48ec1604744ab330d18af1f55256c35dc5c34c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 22:58:08 +0100 Subject: Improve query signer and its api --- .../mimuw/cloudatlas/client/ClientController.java | 10 ++- .../mimuw/cloudatlas/querysigner/QuerySigner.java | 12 +++- .../querysigner/QuerySignerApiImplementation.java | 84 +++++++++++++++------- .../cloudatlas/querysignerapi/QuerySignerApi.java | 10 ++- 4 files changed, 78 insertions(+), 38 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java index 14f531e..56b478c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -35,7 +35,6 @@ import java.util.*; public class ClientController { private Api agentApi; private QuerySignerApi querySignerApi; - private Map querySignatures; private Map attributes; private String currentZoneName; private static final int MAX_ENTRIES = 10; @@ -60,7 +59,6 @@ public class ClientController { } }; this.currentZoneName = System.getProperty("zone_path"); - this.querySignatures = new HashMap<>(); fetchAttributeData(); // fetch attribute data as early as possible } @@ -81,9 +79,8 @@ public class ClientController { boolean success = true; try { - byte[] querySignature = this.querySignerApi.signQuery(queryObject.getName(), queryObject.getValue()); - querySignatures.put(queryObject.getName(), querySignature); - this.agentApi.installQuery(queryObject.getName(), queryObject.getValue(), querySignature); + ValueQuery query = this.querySignerApi.signInstallQuery(queryObject.getName(), queryObject.getValue()); + this.agentApi.installQuery(queryObject.getName(), query); } catch (Exception e) { success = false; System.err.println("Client exception:"); @@ -108,7 +105,8 @@ public class ClientController { boolean success = true; try { - this.agentApi.uninstallQuery(queryObject.getName(), querySignatures.get(queryObject.getName())); + ValueQuery query = querySignerApi.signUninstallQuery(queryObject.getName()); + this.agentApi.uninstallQuery(queryObject.getName(), query); } catch (Exception e) { success = false; System.err.println("Client exception:"); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index 69a25d7..90a86b7 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -1,7 +1,5 @@ package pl.edu.mimuw.cloudatlas.querysigner; -import pl.edu.mimuw.cloudatlas.agent.EventBus; -import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import java.rmi.registry.LocateRegistry; @@ -9,10 +7,18 @@ import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class QuerySigner { + public static class InvalidQueryException extends Exception { + InvalidQueryException() { + super("Query invalid"); + } + } public static void runRegistry() { try { - QuerySignerApiImplementation api = new QuerySignerApiImplementation(); + // TODO reading from files + String publicKey = System.getProperty("public_key"); + String privateKey = System.getProperty("private_key"); + QuerySignerApiImplementation api = new QuerySignerApiImplementation(publicKey.getBytes(), privateKey.getBytes()); QuerySignerApi apiStub = (QuerySignerApi) UnicastRemoteObject.exportObject(api, 0); Registry registry = LocateRegistry.getRegistry(); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index 38a86c6..d1c0e7c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -1,5 +1,6 @@ package pl.edu.mimuw.cloudatlas.querysigner; +import pl.edu.mimuw.cloudatlas.ByteSerializer; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; @@ -9,27 +10,27 @@ import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.rmi.RemoteException; import java.security.*; +import java.security.interfaces.RSAPrivateCrtKey; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class QuerySignerApiImplementation implements QuerySignerApi { + private final static String ENCRYPTION_ALGORITHM = "RSA"; + private final static String DIGEST_ALGORITHM = "SHA-256"; private PublicKey publicKey; private PrivateKey privateKey; - private final static String ENCRYPTION_ALGORITHM = "RSA"; - private final static int NUM_KEY_BITS = 1024; private Map queries; private Set attribsSetByQueries; + private ByteSerializer byteSerializer; - QuerySignerApiImplementation() { + QuerySignerApiImplementation(byte[] serializedPublicKey, byte[] serializedPrivateKey) { + this.byteSerializer = new ByteSerializer(); + this.publicKey = (PublicKey) byteSerializer.deserialize(serializedPublicKey, PublicKey.class); + this.privateKey = (PrivateKey) byteSerializer.deserialize(serializedPrivateKey, PrivateKey.class); this.queries = new HashMap<>(); this.attribsSetByQueries = new HashSet<>(); - try { - generateKeys(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } } private String byteArrayToString(byte[] arr, int offset, int len) { @@ -44,19 +45,10 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return sb.toString(); } - private void generateKeys() throws NoSuchAlgorithmException { - KeyPairGenerator keyGenerator = - KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM); - keyGenerator.initialize(NUM_KEY_BITS); - KeyPair keyPair = keyGenerator.generateKeyPair(); - this.privateKey = keyPair.getPrivate(); - this.publicKey = keyPair.getPublic(); - } - - private byte[] encryptQuery(String query) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + private byte[] encryptQuery(byte[] query) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher signCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); signCipher.init(Cipher.ENCRYPT_MODE, privateKey); - byte[] encryptedBytes = signCipher.doFinal(query.getBytes()); + byte[] encryptedBytes = signCipher.doFinal(query); System.out.println( "Bytes encrypted with " + ENCRYPTION_ALGORITHM + ": " + byteArrayToString( @@ -64,7 +56,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return encryptedBytes; } - private String decryptQuery(byte[] encryptedQuery) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { + private byte[] decryptQuery(byte[] encryptedQuery) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { Cipher verifyCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); verifyCipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] decryptedBytes = verifyCipher.doFinal(encryptedQuery); @@ -72,29 +64,67 @@ public class QuerySignerApiImplementation implements QuerySignerApi { "Bytes decrypted with " + ENCRYPTION_ALGORITHM + ": " + byteArrayToString( decryptedBytes, 0, decryptedBytes.length)); - return new String(decryptedBytes); + return decryptedBytes; + } + + private byte[] cryptographicHash(byte[] serializedQuery) throws NoSuchAlgorithmException { + MessageDigest digestGenerator = + MessageDigest.getInstance(DIGEST_ALGORITHM); + byte[] digest = digestGenerator.digest(serializedQuery); + System.out.println( + DIGEST_ALGORITHM + " digest: " + + byteArrayToString( + digest, 0, digest.length)); + return digest; + } + + // TODO + private byte[] serializeQuery(String queryName, String queryCode) { + return byteSerializer.serialize(queryName + queryCode); } @Override - public byte[] signQuery(String queryName, String queryCode) throws RemoteException { + public ValueQuery signInstallQuery(String queryName, String queryCode) throws RemoteException { + QueryUtils.validateQueryName(queryName); try { - return encryptQuery(queryName + queryCode); - } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { + byte[] serializedQuery = serializeQuery(queryName, queryCode); + byte[] hashedQuery = cryptographicHash(serializedQuery); + byte[] querySignature = encryptQuery(hashedQuery); + return new ValueQuery(queryCode, querySignature); + } catch (Exception e) { e.printStackTrace(); throw new RemoteException(e.getLocalizedMessage()); } } @Override - public String checkQuery(byte[] encryptedQuery, String queryName, String queryCode) throws RemoteException { + public void validateInstallQuery(String queryName, ValueQuery query) throws RemoteException { + QueryUtils.validateQueryName(queryName); try { - return decryptQuery(encryptedQuery); - } catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) { + byte[] decryptedQuery = decryptQuery(query.getSignature()); + byte[] serializedQuery = serializeQuery(queryName, query.getCode()); + byte[] hashedSerializedQuery = cryptographicHash(serializedQuery); + if (hashedSerializedQuery != decryptedQuery) { + throw new QuerySigner.InvalidQueryException(); + } + } catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException | QuerySigner.InvalidQueryException e) { e.printStackTrace(); throw new RemoteException(e.getLocalizedMessage()); } } + // TODO + @Override + public ValueQuery signUninstallQuery(String queryName) throws RemoteException { + return null; + } + + // TODO + @Override + public void validateUninstallQuery(String queryName, ValueQuery query) throws RemoteException { + + } + @Override public PublicKey getPublicKey() throws RemoteException { return publicKey; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java index 3c77c0a..fa46da3 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java @@ -1,13 +1,19 @@ package pl.edu.mimuw.cloudatlas.querysignerapi; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; + import java.rmi.Remote; import java.rmi.RemoteException; import java.security.PublicKey; public interface QuerySignerApi extends Remote { - public byte[] signQuery(String queryName, String queryCode) throws RemoteException; + public ValueQuery signInstallQuery(String queryName, String queryCode) throws RemoteException; + + public ValueQuery signUninstallQuery(String queryName) throws RemoteException; + + public void validateInstallQuery(String queryName, ValueQuery query) throws RemoteException; - public String checkQuery(byte[] encryptedQuery, String queryName, String queryCode) throws RemoteException; + public void validateUninstallQuery(String queryName, ValueQuery query) throws RemoteException; public PublicKey getPublicKey() throws RemoteException; -- cgit v1.2.3 From cc901690a8e3e41a1958ae33d95462031fba8e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 12 Jan 2020 23:07:32 +0100 Subject: Fix query value typo --- src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index c5d4b54..6839e4c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -23,7 +23,6 @@ public class ValueQuery extends Value { /** * Constructs a new ValueQuery object. * - * @param name the name of the query * @param query the code of the query */ public ValueQuery(String query) throws Exception { @@ -61,10 +60,6 @@ public class ValueQuery extends Value { public void setTimestamp(long timestamp) { this.timestamp = timestamp; } - public String getCode() { - return code; - } - @Override public Type getType() { return TypePrimitive.QUERY; -- cgit v1.2.3 From 81d1abbbad1f1e9c7774c7162b23fc804f7989a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 20:56:01 +0100 Subject: Add reading keys from file --- .../java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 59 ++++++++++++++++++++-- .../mimuw/cloudatlas/querysigner/QueryData.java | 4 ++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java index 8581280..8a474ac 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java @@ -1,16 +1,30 @@ package pl.edu.mimuw.cloudatlas; +import java.io.*; import java.security.*; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; public class KeyGenerator { private final static int NUM_KEY_BITS = 1024; private final static String ENCRYPTION_ALGORITHM = "RSA"; // TODO - // TODO read key values from files - // TODO gradlew private static void printKeyToFile(byte[] keyValue, String fileName) { + System.out.println("\n BEGIN KEY"); + for (byte k : keyValue) { + System.out.print(k); + } + System.out.println("\n EOF KEY"); + String keyString = new String(keyValue); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); + writer.write(keyString); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } private static void generateKeys() throws NoSuchAlgorithmException { @@ -22,13 +36,48 @@ public class KeyGenerator { keyGenerator.initialize(NUM_KEY_BITS); KeyPair keyPair = keyGenerator.generateKeyPair(); + String publicKeyFile = System.getProperty("public_key_file"); + String privateKeyFile = System.getProperty("private_key_file"); + privateKey = keyPair.getPrivate(); - printKeyToFile(privateKey.getEncoded(), "query_signer"); + printKeyToFile(privateKey.getEncoded(), privateKeyFile); publicKey = keyPair.getPublic(); - printKeyToFile(publicKey.getEncoded(), "query_signer.pub"); + printKeyToFile(publicKey.getEncoded(), publicKeyFile); + } + + public static PublicKey getPublicKey(String key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePublic(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static PrivateKey getPrivateKey(String key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePrivate(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String readKeyFromFile(String filename) throws IOException { + String key; + BufferedReader reader = new BufferedReader(new FileReader(filename)); + key = reader.readLine(); + reader.close(); + return key; } - public static void main() { + public static void main(String[] args) { try { generateKeys(); } catch (NoSuchAlgorithmException e) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java new file mode 100644 index 0000000..a09c823 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -0,0 +1,4 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +public class QueryData { +} -- cgit v1.2.3 From 23b8714b29816b608cb3aa330b6e93610e107126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 20:56:41 +0100 Subject: Create simple query data structure so that RMI doesn't blow up --- .../mimuw/cloudatlas/agent/ApiImplementation.java | 7 +++-- .../cloudatlas/agent/NewApiImplementation.java | 7 +++-- src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java | 5 ++-- .../mimuw/cloudatlas/client/ClientController.java | 5 ++-- .../pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 9 ++++++ .../mimuw/cloudatlas/querysigner/QueryData.java | 26 +++++++++++++++++ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 20 ++++++++++--- .../querysigner/QuerySignerApiImplementation.java | 34 +++++++--------------- .../cloudatlas/querysignerapi/QuerySignerApi.java | 17 ++++------- 9 files changed, 80 insertions(+), 50 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java index e9dbb7e..90e7789 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java @@ -28,6 +28,7 @@ import pl.edu.mimuw.cloudatlas.model.Type; import pl.edu.mimuw.cloudatlas.model.TypePrimitive; import pl.edu.mimuw.cloudatlas.model.ZMI; import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils; public class ApiImplementation implements Api { @@ -61,11 +62,11 @@ public class ApiImplementation implements Api { } } - public void installQuery(String name, ValueQuery query) throws RemoteException { + public void installQuery(String name, QueryData query) throws RemoteException { QueryUtils.validateQueryName(name); try { Attribute attributeName = new Attribute(name); - installQueryInHierarchy(root, attributeName, query); + installQueryInHierarchy(root, attributeName, new ValueQuery(query)); executeAllQueries(root); } catch (Exception e) { throw new RemoteException("Failed to install query", e); @@ -81,7 +82,7 @@ public class ApiImplementation implements Api { } } - public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException { + public void uninstallQuery(String queryName, QueryData query) throws RemoteException { QueryUtils.validateQueryName(queryName); uninstallQueryInHierarchy(root, new Attribute(queryName)); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index bf75210..0bf4338 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -22,6 +22,7 @@ import pl.edu.mimuw.cloudatlas.interpreter.Main; import pl.edu.mimuw.cloudatlas.interpreter.QueryResult; import pl.edu.mimuw.cloudatlas.model.*; import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils; public class NewApiImplementation implements Api { @@ -80,13 +81,13 @@ public class NewApiImplementation implements Api { } } - public void installQuery(String name, ValueQuery query) throws RemoteException { + public void installQuery(String name, QueryData query) throws RemoteException { QueryUtils.validateQueryName(name); try { Attribute attributeName = new Attribute(name); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); Map> queries = new HashMap(); - queries.put(attributeName, new SimpleImmutableEntry(query, timestamp)); + queries.put(attributeName, new SimpleImmutableEntry(new ValueQuery(query), timestamp)); UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries); eventBus.addMessage(message); } catch (Exception e) { @@ -94,7 +95,7 @@ public class NewApiImplementation implements Api { } } - public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException { + public void uninstallQuery(String queryName, QueryData query) throws RemoteException { QueryUtils.validateQueryName(queryName); try { Attribute attributeName = new Attribute(queryName); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java index 2936b13..7cc629d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/api/Api.java @@ -7,6 +7,7 @@ import pl.edu.mimuw.cloudatlas.model.Value; import pl.edu.mimuw.cloudatlas.model.ValueContact; import pl.edu.mimuw.cloudatlas.model.AttributesMap; import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; /** * @@ -28,9 +29,9 @@ public interface Api extends Remote { public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException; - public void installQuery(String queryName, ValueQuery query) throws RemoteException; + public void installQuery(String queryName, QueryData query) throws RemoteException; - public void uninstallQuery(String queryName, ValueQuery query) throws RemoteException; + public void uninstallQuery(String queryName, QueryData query) throws RemoteException; public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java index 56b478c..5f34fe9 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.model.*; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import java.net.InetAddress; @@ -79,7 +80,7 @@ public class ClientController { boolean success = true; try { - ValueQuery query = this.querySignerApi.signInstallQuery(queryObject.getName(), queryObject.getValue()); + QueryData query = this.querySignerApi.signInstallQuery(queryObject.getName(), queryObject.getValue()); this.agentApi.installQuery(queryObject.getName(), query); } catch (Exception e) { success = false; @@ -105,7 +106,7 @@ public class ClientController { boolean success = true; try { - ValueQuery query = querySignerApi.signUninstallQuery(queryObject.getName()); + QueryData query = querySignerApi.signUninstallQuery(queryObject.getName()); this.agentApi.uninstallQuery(queryObject.getName(), query); } catch (Exception e) { success = false; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index 6839e4c..ece50b5 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -6,6 +6,7 @@ import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program; import pl.edu.mimuw.cloudatlas.interpreter.query.parser; import pl.edu.mimuw.cloudatlas.interpreter.query.Yylex; import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; /** * A class that holds a CloudAtlas query. @@ -41,6 +42,14 @@ public class ValueQuery extends Value { this.timestamp = System.currentTimeMillis(); } + public ValueQuery(QueryData queryData) throws Exception { + this.code = queryData.getCode(); + Yylex lex = new Yylex(new ByteArrayInputStream(queryData.getCode().getBytes())); + this.query = (new parser(lex)).pProgram(); + this.signature = queryData.getSignature(); + this.timestamp = System.currentTimeMillis(); + } + private ValueQuery() { this.code = null; this.query = null; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java index a09c823..7a85d7c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -1,4 +1,30 @@ package pl.edu.mimuw.cloudatlas.querysigner; +import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program; + public class QueryData { + // Original source code + private String code; + // Query signature + private byte[] signature; + // Query signing timestamp + private long timestamp; + + public QueryData(String code, byte[] signature) { + this.code = code; + this.signature = signature; + this.timestamp = System.currentTimeMillis();; + } + + public String getCode() { + return code; + } + + public byte[] getSignature() { + return signature; + } + + public long getTimestamp() { + return timestamp; + } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index 90a86b7..d60a75d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -1,10 +1,16 @@ package pl.edu.mimuw.cloudatlas.querysigner; +import pl.edu.mimuw.cloudatlas.KeyGenerator; +import pl.edu.mimuw.cloudatlas.agent.NewApiImplementation; +import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; +import java.io.IOException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; +import java.security.PrivateKey; +import java.security.PublicKey; public class QuerySigner { public static class InvalidQueryException extends Exception { @@ -13,12 +19,18 @@ public class QuerySigner { } } + private static QuerySignerApiImplementation initApi() throws IOException { + String publicKeyFile = System.getProperty("public_key_file"); + String privateKeyFile = System.getProperty("private_key_file"); + PublicKey publicKey = null; //KeyGenerator.getPublicKey(KeyGenerator.readKeyFromFile(publicKeyFile)); + PrivateKey privateKey = null; // KeyGenerator.getPrivateKey(KeyGenerator.readKeyFromFile(privateKeyFile)); + return new QuerySignerApiImplementation(publicKey, privateKey); + } + public static void runRegistry() { try { - // TODO reading from files - String publicKey = System.getProperty("public_key"); - String privateKey = System.getProperty("private_key"); - QuerySignerApiImplementation api = new QuerySignerApiImplementation(publicKey.getBytes(), privateKey.getBytes()); + QuerySignerApiImplementation api = initApi(); +// NewApiImplementation apii = new NewApiImplementation(null); QuerySignerApi apiStub = (QuerySignerApi) UnicastRemoteObject.exportObject(api, 0); Registry registry = LocateRegistry.getRegistry(); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index d1c0e7c..32bc634 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -10,7 +10,6 @@ import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.rmi.RemoteException; import java.security.*; -import java.security.interfaces.RSAPrivateCrtKey; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -25,10 +24,12 @@ public class QuerySignerApiImplementation implements QuerySignerApi { private Set attribsSetByQueries; private ByteSerializer byteSerializer; - QuerySignerApiImplementation(byte[] serializedPublicKey, byte[] serializedPrivateKey) { + QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) {// (byte[] serializedPublicKey, byte[] serializedPrivateKey) { this.byteSerializer = new ByteSerializer(); - this.publicKey = (PublicKey) byteSerializer.deserialize(serializedPublicKey, PublicKey.class); - this.privateKey = (PrivateKey) byteSerializer.deserialize(serializedPrivateKey, PrivateKey.class); +// this.publicKey = (PublicKey) byteSerializer.deserialize(serializedPublicKey, PublicKey.class); +// this.privateKey = (PrivateKey) byteSerializer.deserialize(serializedPrivateKey, PrivateKey.class); + this.publicKey = publicKey; + this.privateKey = privateKey; this.queries = new HashMap<>(); this.attribsSetByQueries = new HashSet<>(); } @@ -84,13 +85,13 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } @Override - public ValueQuery signInstallQuery(String queryName, String queryCode) throws RemoteException { + public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException { QueryUtils.validateQueryName(queryName); try { byte[] serializedQuery = serializeQuery(queryName, queryCode); byte[] hashedQuery = cryptographicHash(serializedQuery); byte[] querySignature = encryptQuery(hashedQuery); - return new ValueQuery(queryCode, querySignature); + return new QueryData(queryCode, querySignature); } catch (Exception e) { e.printStackTrace(); throw new RemoteException(e.getLocalizedMessage()); @@ -98,7 +99,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } @Override - public void validateInstallQuery(String queryName, ValueQuery query) throws RemoteException { + public void validateInstallQuery(String queryName, QueryData query) throws RemoteException { QueryUtils.validateQueryName(queryName); try { byte[] decryptedQuery = decryptQuery(query.getSignature()); @@ -115,28 +116,13 @@ public class QuerySignerApiImplementation implements QuerySignerApi { // TODO @Override - public ValueQuery signUninstallQuery(String queryName) throws RemoteException { + public QueryData signUninstallQuery(String queryName) throws RemoteException { return null; } // TODO @Override - public void validateUninstallQuery(String queryName, ValueQuery query) throws RemoteException { - - } - - @Override - public PublicKey getPublicKey() throws RemoteException { - return publicKey; - } + public void validateUninstallQuery(String queryName, QueryData query) throws RemoteException { - @Override - public void setPublicKey(PublicKey publicKey) throws RemoteException { - this.publicKey = publicKey; - } - - @Override - public byte[] getQuerySignature(String queryName) throws RemoteException { - return queries.get(queryName).getSignature(); } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java index fa46da3..6b42d32 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java @@ -1,23 +1,16 @@ package pl.edu.mimuw.cloudatlas.querysignerapi; -import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; import java.rmi.Remote; import java.rmi.RemoteException; -import java.security.PublicKey; public interface QuerySignerApi extends Remote { - public ValueQuery signInstallQuery(String queryName, String queryCode) throws RemoteException; + public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException; - public ValueQuery signUninstallQuery(String queryName) throws RemoteException; + public QueryData signUninstallQuery(String queryName) throws RemoteException; - public void validateInstallQuery(String queryName, ValueQuery query) throws RemoteException; + public void validateInstallQuery(String queryName, QueryData query) throws RemoteException; - public void validateUninstallQuery(String queryName, ValueQuery query) throws RemoteException; - - public PublicKey getPublicKey() throws RemoteException; - - public void setPublicKey(PublicKey publicKey) throws RemoteException; - - public byte[] getQuerySignature(String queryName) throws RemoteException; + public void validateUninstallQuery(String queryName, QueryData query) throws RemoteException; } -- cgit v1.2.3 From 8103eb7e174c7c0bb33316ae9499bbc0fbb62ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 21:54:45 +0100 Subject: Fix key generation and reading --- .../java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 87 ---------------------- .../edu/mimuw/cloudatlas/querysigner/KeyUtils.java | 35 +++++++++ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 8 +- 3 files changed, 37 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java deleted file mode 100644 index 8a474ac..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java +++ /dev/null @@ -1,87 +0,0 @@ -package pl.edu.mimuw.cloudatlas; - -import java.io.*; -import java.security.*; -import java.security.spec.X509EncodedKeySpec; -import java.util.Base64; - -public class KeyGenerator { - private final static int NUM_KEY_BITS = 1024; - private final static String ENCRYPTION_ALGORITHM = "RSA"; - - // TODO - private static void printKeyToFile(byte[] keyValue, String fileName) { - System.out.println("\n BEGIN KEY"); - for (byte k : keyValue) { - System.out.print(k); - } - System.out.println("\n EOF KEY"); - String keyString = new String(keyValue); - - try { - BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); - writer.write(keyString); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void generateKeys() throws NoSuchAlgorithmException { - PublicKey publicKey; - PrivateKey privateKey; - - KeyPairGenerator keyGenerator = - KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM); - keyGenerator.initialize(NUM_KEY_BITS); - KeyPair keyPair = keyGenerator.generateKeyPair(); - - String publicKeyFile = System.getProperty("public_key_file"); - String privateKeyFile = System.getProperty("private_key_file"); - - privateKey = keyPair.getPrivate(); - printKeyToFile(privateKey.getEncoded(), privateKeyFile); - publicKey = keyPair.getPublic(); - printKeyToFile(publicKey.getEncoded(), publicKeyFile); - } - - public static PublicKey getPublicKey(String key){ - try { - byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); - X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); - KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); - return kf.generatePublic(X509publicKey); - } catch(Exception e) { - e.printStackTrace(); - } - return null; - } - - public static PrivateKey getPrivateKey(String key){ - try { - byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); - X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); - KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); - return kf.generatePrivate(X509publicKey); - } catch(Exception e) { - e.printStackTrace(); - } - return null; - } - - public static String readKeyFromFile(String filename) throws IOException { - String key; - BufferedReader reader = new BufferedReader(new FileReader(filename)); - key = reader.readLine(); - reader.close(); - return key; - } - - public static void main(String[] args) { - try { - generateKeys(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - } -} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java new file mode 100644 index 0000000..7a543ba --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java @@ -0,0 +1,35 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.*; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +public class KeyUtils { + private final static String ENCRYPTION_ALGORITHM = "RSA"; + + public static PublicKey getPublicKey(String filename){ + try { + byte[] byteKey = Files.readAllBytes(Paths.get(filename)); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePublic(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static PrivateKey getPrivateKey(String filename){ + try { + byte[] byteKey = Files.readAllBytes(Paths.get(filename)); + PKCS8EncodedKeySpec PKCS8privateKey = new PKCS8EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePrivate(PKCS8privateKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index d60a75d..735601d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -1,8 +1,5 @@ package pl.edu.mimuw.cloudatlas.querysigner; -import pl.edu.mimuw.cloudatlas.KeyGenerator; -import pl.edu.mimuw.cloudatlas.agent.NewApiImplementation; -import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import java.io.IOException; @@ -22,15 +19,14 @@ public class QuerySigner { private static QuerySignerApiImplementation initApi() throws IOException { String publicKeyFile = System.getProperty("public_key_file"); String privateKeyFile = System.getProperty("private_key_file"); - PublicKey publicKey = null; //KeyGenerator.getPublicKey(KeyGenerator.readKeyFromFile(publicKeyFile)); - PrivateKey privateKey = null; // KeyGenerator.getPrivateKey(KeyGenerator.readKeyFromFile(privateKeyFile)); + PublicKey publicKey = KeyUtils.getPublicKey(publicKeyFile); + PrivateKey privateKey = KeyUtils.getPrivateKey(privateKeyFile); return new QuerySignerApiImplementation(publicKey, privateKey); } public static void runRegistry() { try { QuerySignerApiImplementation api = initApi(); -// NewApiImplementation apii = new NewApiImplementation(null); QuerySignerApi apiStub = (QuerySignerApi) UnicastRemoteObject.exportObject(api, 0); Registry registry = LocateRegistry.getRegistry(); -- cgit v1.2.3 From 12175874524e19d999880302c66b7242de47e482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 22:11:22 +0100 Subject: Make query data serializable --- src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java | 2 ++ src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java b/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java index 0a86c0d..ee7a6f0 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/ByteSerializer.java @@ -9,6 +9,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; import pl.edu.mimuw.cloudatlas.agent.modules.RecursiveScheduledTask; import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; import pl.edu.mimuw.cloudatlas.model.*; +import pl.edu.mimuw.cloudatlas.querysigner.QueryData; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -161,6 +162,7 @@ public class ByteSerializer { kryo.register(LinkedHashMap.class); kryo.register(HashMap.class); kryo.register(ModuleType.class); + kryo.register(QueryData.class); } public Object deserialize(byte[] packetData, Class objClass) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java index 7a85d7c..7801a28 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -1,8 +1,8 @@ package pl.edu.mimuw.cloudatlas.querysigner; -import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program; +import java.io.Serializable; -public class QueryData { +public class QueryData implements Serializable { // Original source code private String code; // Query signature -- cgit v1.2.3 From 27b84d48029e90d816653e2b718aa8f629094611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 22:40:43 +0100 Subject: Small fixes --- .../mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index 32bc634..37264bf 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -24,10 +24,8 @@ public class QuerySignerApiImplementation implements QuerySignerApi { private Set attribsSetByQueries; private ByteSerializer byteSerializer; - QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) {// (byte[] serializedPublicKey, byte[] serializedPrivateKey) { + QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) { this.byteSerializer = new ByteSerializer(); -// this.publicKey = (PublicKey) byteSerializer.deserialize(serializedPublicKey, PublicKey.class); -// this.privateKey = (PrivateKey) byteSerializer.deserialize(serializedPrivateKey, PrivateKey.class); this.publicKey = publicKey; this.privateKey = privateKey; this.queries = new HashMap<>(); @@ -79,7 +77,6 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return digest; } - // TODO private byte[] serializeQuery(String queryName, String queryCode) { return byteSerializer.serialize(queryName + queryCode); } -- cgit v1.2.3 From 3159df80ee02f6f65fe8e8ae1bae8fecb8fad070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 15:10:44 +0100 Subject: Debug install validation --- .../cloudatlas/agent/NewApiImplementation.java | 18 +++++------ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 2 +- .../querysigner/QuerySignerApiImplementation.java | 36 +++++++++------------- .../cloudatlas/querysignerapi/QuerySignerApi.java | 4 --- 4 files changed, 23 insertions(+), 37 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index 0bf4338..a2ac358 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -1,9 +1,8 @@ package pl.edu.mimuw.cloudatlas.agent; -import java.io.PrintStream; - import java.rmi.RemoteException; +import java.security.PublicKey; import java.util.concurrent.CompletableFuture; import java.util.List; import java.util.AbstractMap.SimpleImmutableEntry; @@ -12,24 +11,20 @@ import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.regex.Pattern; -import java.util.regex.Matcher; import pl.edu.mimuw.cloudatlas.agent.messages.*; -import pl.edu.mimuw.cloudatlas.interpreter.Interpreter; -import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException; -import pl.edu.mimuw.cloudatlas.interpreter.Main; -import pl.edu.mimuw.cloudatlas.interpreter.QueryResult; import pl.edu.mimuw.cloudatlas.model.*; import pl.edu.mimuw.cloudatlas.api.Api; -import pl.edu.mimuw.cloudatlas.querysigner.QueryData; -import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils; +import pl.edu.mimuw.cloudatlas.querysigner.*; public class NewApiImplementation implements Api { private EventBus eventBus; + private PublicKey publicKey; public NewApiImplementation(EventBus eventBus) { this.eventBus = eventBus; + String publicKeyFile = System.getProperty("public_key_file"); + publicKey = KeyUtils.getPublicKey(publicKeyFile); } public Set getZoneSet() throws RemoteException { @@ -82,8 +77,9 @@ public class NewApiImplementation implements Api { } public void installQuery(String name, QueryData query) throws RemoteException { - QueryUtils.validateQueryName(name); try { + QueryUtils.validateQueryName(name); + QuerySignerApiImplementation.validateInstallQuery(name, query, this.publicKey); Attribute attributeName = new Attribute(name); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); Map> queries = new HashMap(); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index 735601d..b2f426e 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -16,7 +16,7 @@ public class QuerySigner { } } - private static QuerySignerApiImplementation initApi() throws IOException { + public static QuerySignerApiImplementation initApi() throws IOException { String publicKeyFile = System.getProperty("public_key_file"); String privateKeyFile = System.getProperty("private_key_file"); PublicKey publicKey = KeyUtils.getPublicKey(publicKeyFile); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index 37264bf..3acaacf 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -22,17 +22,15 @@ public class QuerySignerApiImplementation implements QuerySignerApi { private PrivateKey privateKey; private Map queries; private Set attribsSetByQueries; - private ByteSerializer byteSerializer; - QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) { - this.byteSerializer = new ByteSerializer(); + public QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; this.queries = new HashMap<>(); this.attribsSetByQueries = new HashSet<>(); } - private String byteArrayToString(byte[] arr, int offset, int len) { + private static String byteArrayToString(byte[] arr, int offset, int len) { StringBuffer sb = new StringBuffer(); for (int i = offset, n = Math.min(arr.length, offset + len); i < n; ++i) { String hex = Integer.toHexString(0xFF & arr[i]); @@ -55,7 +53,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return encryptedBytes; } - private byte[] decryptQuery(byte[] encryptedQuery) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { + private static byte[] decryptQuery(byte[] encryptedQuery, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { Cipher verifyCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); verifyCipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] decryptedBytes = verifyCipher.doFinal(encryptedQuery); @@ -66,7 +64,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return decryptedBytes; } - private byte[] cryptographicHash(byte[] serializedQuery) throws NoSuchAlgorithmException { + private static byte[] cryptographicHash(byte[] serializedQuery) throws NoSuchAlgorithmException { MessageDigest digestGenerator = MessageDigest.getInstance(DIGEST_ALGORITHM); byte[] digest = digestGenerator.digest(serializedQuery); @@ -77,7 +75,8 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return digest; } - private byte[] serializeQuery(String queryName, String queryCode) { + private static byte[] serializeQuery(String queryName, String queryCode) { + ByteSerializer byteSerializer = new ByteSerializer(); return byteSerializer.serialize(queryName + queryCode); } @@ -95,19 +94,15 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } } - @Override - public void validateInstallQuery(String queryName, QueryData query) throws RemoteException { + public static void validateInstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException,IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, QuerySigner.InvalidQueryException { QueryUtils.validateQueryName(queryName); - try { - byte[] decryptedQuery = decryptQuery(query.getSignature()); - byte[] serializedQuery = serializeQuery(queryName, query.getCode()); - byte[] hashedSerializedQuery = cryptographicHash(serializedQuery); - if (hashedSerializedQuery != decryptedQuery) { - throw new QuerySigner.InvalidQueryException(); - } - } catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException | QuerySigner.InvalidQueryException e) { - e.printStackTrace(); - throw new RemoteException(e.getLocalizedMessage()); + byte[] decryptedQuery = decryptQuery(query.getSignature(), publicKey); + byte[] serializedQuery = serializeQuery(queryName, query.getCode()); + byte[] hashedSerializedQuery = cryptographicHash(serializedQuery); + String decryptedQueryString = byteArrayToString(decryptedQuery, 0, decryptedQuery.length); + String hashedSerializedQueryString = byteArrayToString(hashedSerializedQuery, 0, hashedSerializedQuery.length); + if (!decryptedQueryString.equals(hashedSerializedQueryString)) { + throw new QuerySigner.InvalidQueryException(); } } @@ -118,8 +113,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } // TODO - @Override - public void validateUninstallQuery(String queryName, QueryData query) throws RemoteException { + public static void validateUninstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException { } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java index 6b42d32..55f4d04 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysignerapi/QuerySignerApi.java @@ -9,8 +9,4 @@ public interface QuerySignerApi extends Remote { public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException; public QueryData signUninstallQuery(String queryName) throws RemoteException; - - public void validateInstallQuery(String queryName, QueryData query) throws RemoteException; - - public void validateUninstallQuery(String queryName, QueryData query) throws RemoteException; } -- cgit v1.2.3 From 99b7023aba5c09f069badc35d05ea3ef53f3a6d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 20:30:22 +0100 Subject: Add query uninstall calls --- .../cloudatlas/agent/NewApiImplementation.java | 5 ++-- .../querysigner/QuerySignerApiImplementation.java | 32 ++++++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index a2ac358..affad86 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -38,7 +38,7 @@ public class NewApiImplementation implements Api { StateMessage stateMessage = (StateMessage) response; Set zones = new HashSet(); collectZoneNames(stateMessage.getZMI(), zones); - return zones; + return zones; } else { System.out.println("ERROR: getZoneSet didn't receive a StateMessage"); throw new Exception("Failed to retrieve zone set"); @@ -92,8 +92,9 @@ public class NewApiImplementation implements Api { } public void uninstallQuery(String queryName, QueryData query) throws RemoteException { - QueryUtils.validateQueryName(queryName); try { + QueryUtils.validateQueryName(queryName); + QuerySignerApiImplementation.validateUninstallQuery(queryName, query, this.publicKey); Attribute attributeName = new Attribute(queryName); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); Map> queries = new HashMap(); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index 3acaacf..3ab2931 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -75,16 +75,19 @@ public class QuerySignerApiImplementation implements QuerySignerApi { return digest; } - private static byte[] serializeQuery(String queryName, String queryCode) { + private static byte[] serializeQuery(String queryName, String queryCode, Boolean install) { ByteSerializer byteSerializer = new ByteSerializer(); - return byteSerializer.serialize(queryName + queryCode); + if (install) { + return byteSerializer.serialize(queryName + queryCode + install.toString()); + } else { + return byteSerializer.serialize(queryName + install.toString()); + } } - @Override - public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException { + private QueryData signQuery(String queryName, String queryCode, Boolean install) throws RemoteException { QueryUtils.validateQueryName(queryName); try { - byte[] serializedQuery = serializeQuery(queryName, queryCode); + byte[] serializedQuery = serializeQuery(queryName, queryCode, install); byte[] hashedQuery = cryptographicHash(serializedQuery); byte[] querySignature = encryptQuery(hashedQuery); return new QueryData(queryCode, querySignature); @@ -94,10 +97,19 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } } + @Override + public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException { + return signQuery(queryName, queryCode, true); + } + public static void validateInstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException,IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, QuerySigner.InvalidQueryException { + validateQuery(queryName, query, publicKey, true); + } + + public static void validateQuery(String queryName, QueryData query, PublicKey publicKey, boolean install) throws RemoteException,IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, QuerySigner.InvalidQueryException { QueryUtils.validateQueryName(queryName); byte[] decryptedQuery = decryptQuery(query.getSignature(), publicKey); - byte[] serializedQuery = serializeQuery(queryName, query.getCode()); + byte[] serializedQuery = serializeQuery(queryName, query.getCode(), install); byte[] hashedSerializedQuery = cryptographicHash(serializedQuery); String decryptedQueryString = byteArrayToString(decryptedQuery, 0, decryptedQuery.length); String hashedSerializedQueryString = byteArrayToString(hashedSerializedQuery, 0, hashedSerializedQuery.length); @@ -106,14 +118,12 @@ public class QuerySignerApiImplementation implements QuerySignerApi { } } - // TODO @Override public QueryData signUninstallQuery(String queryName) throws RemoteException { - return null; + return signQuery(queryName, "", false); } - // TODO - public static void validateUninstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException { - + public static void validateUninstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, QuerySigner.InvalidQueryException, NoSuchPaddingException, InvalidKeyException { + validateQuery(queryName, query, publicKey, false); } } -- cgit v1.2.3 From 83feb6ad13187af9be55cc71408611f67d280955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 21:28:10 +0100 Subject: Fix timestamps and ValueQuery --- .../mimuw/cloudatlas/agent/NewApiImplementation.java | 8 ++++---- .../cloudatlas/agent/messages/StateMessage.java | 6 +++--- .../agent/messages/UpdateQueriesMessage.java | 6 +++--- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 10 +++++----- .../cloudatlas/agent/modules/GossipGirlState.java | 10 +++++----- .../edu/mimuw/cloudatlas/agent/modules/Qurnik.java | 5 +++-- .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 20 +++++++++++--------- .../pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 18 ++++++++++++------ 8 files changed, 46 insertions(+), 37 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index affad86..bd3f524 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -82,8 +82,8 @@ public class NewApiImplementation implements Api { QuerySignerApiImplementation.validateInstallQuery(name, query, this.publicKey); Attribute attributeName = new Attribute(name); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); - Map> queries = new HashMap(); - queries.put(attributeName, new SimpleImmutableEntry(new ValueQuery(query), timestamp)); + Map queries = new HashMap(); + queries.put(attributeName, new ValueQuery(query)); UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries); eventBus.addMessage(message); } catch (Exception e) { @@ -97,8 +97,8 @@ public class NewApiImplementation implements Api { QuerySignerApiImplementation.validateUninstallQuery(queryName, query, this.publicKey); Attribute attributeName = new Attribute(queryName); ValueTime timestamp = new ValueTime(System.currentTimeMillis()); - Map> queries = new HashMap(); - queries.put(attributeName, new SimpleImmutableEntry(null, timestamp)); + Map queries = new HashMap(); + queries.put(attributeName, new ValueQuery(query)); UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries); eventBus.addMessage(message); } catch (Exception e) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java index 806d41f..c70f215 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java @@ -13,10 +13,10 @@ import pl.edu.mimuw.cloudatlas.model.ZMI; public class StateMessage extends ResponseMessage { private ZMI zmi; - private Map> queries; + private Map queries; private Set contacts; - public StateMessage(String messageId, ModuleType destinationModule, long timestamp, long requestId, ZMI zmi, Map> queries, Set contacts) { + public StateMessage(String messageId, ModuleType destinationModule, long timestamp, long requestId, ZMI zmi, Map queries, Set contacts) { super(messageId, destinationModule, timestamp, Type.STATE, requestId); this.zmi = zmi; this.queries = queries; @@ -29,7 +29,7 @@ public class StateMessage extends ResponseMessage { return zmi; } - public Map> getQueries() { + public Map getQueries() { return queries; } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/UpdateQueriesMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/UpdateQueriesMessage.java index 4b0b9c8..7f156df 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/UpdateQueriesMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/UpdateQueriesMessage.java @@ -8,16 +8,16 @@ import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueTime; public class UpdateQueriesMessage extends StanikMessage { - private Map> queries; + private Map queries; - public UpdateQueriesMessage(String messageId, long timestamp, Map> queries) { + public UpdateQueriesMessage(String messageId, long timestamp, Map queries) { super(messageId, timestamp, Type.UPDATE_QUERIES); this.queries = queries; } public UpdateQueriesMessage() {} - public Map> getQueries() { + public Map getQueries() { return queries; } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index 5199e82..b6087e6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -200,11 +200,11 @@ public class GossipGirl extends Module { System.out.println("INFO: handling Query in " + Long.toString(message.getReceiverGossipId())); state.setLastAction(); state.gotQuery(message); - Map> queries = new HashMap(); - queries.put( - message.getName(), - new SimpleImmutableEntry(message.getQuery(), state.getTheirQueryTimestamp(message.getName())) - ); + Map queries = new HashMap(); + ValueQuery vq = message.getQuery(); + ValueTime timestamp = state.getTheirQueryTimestamp(message.getName()); + vq.setTimestamp(timestamp.getValue()); + queries.put(message.getName(), vq); UpdateQueriesMessage updateMessage = new UpdateQueriesMessage("", 0, queries); System.out.println("INFO: GossipGirl sending UpdateQueriesMessage"); sendMessage(updateMessage); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java index cfaf560..4709eb1 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java @@ -47,7 +47,7 @@ public class GossipGirlState { public long timeOffest; public State state; public ZMI hierarchy; - public Map> queries; + public Map queries; public ValueTime hejkaSendTimestamp; public ValueTime hejkaReceiveTimestamp; public ValueTime noCoTamSendTimestamp; @@ -79,7 +79,7 @@ public class GossipGirlState { lastAction = ValueUtils.currentTime(); } - public void setState(ZMI hierarchy, Map> queries) { + public void setState(ZMI hierarchy, Map queries) { switch (state) { case WAIT_FOR_STATE_INITIALIZER: this.hierarchy = hierarchy; @@ -211,8 +211,8 @@ public class GossipGirlState { public Map getQueryTimestampsToSend() { Map queryTimestamps= new HashMap(); - for (Entry> query : queries.entrySet()) { - queryTimestamps.put(query.getKey(), query.getValue().getValue()); + for (Entry query : queries.entrySet()) { + queryTimestamps.put(query.getKey(), new ValueTime(query.getValue().getTimestamp())); } return queryTimestamps; @@ -259,7 +259,7 @@ public class GossipGirlState { queryList.add( new SimpleImmutableEntry( name, - queries.get(name).getKey() + queries.get(name) ) ); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java index 2119653..9a544d9 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java @@ -59,13 +59,14 @@ public class Qurnik extends Module { private void runQueriesOnState(StateMessage message) throws InterruptedException { List queries = new LinkedList(); - for (Entry timestampedQuery : message.getQueries().values()) { - queries.add(timestampedQuery.getKey()); + for (ValueQuery timestampedQuery : message.getQueries().values()) { + queries.add(timestampedQuery); } executeAllQueries(message.getZMI(), queries, PathName.ROOT); } private void executeAllQueries(ZMI zmi, List queries, PathName currentPath) throws InterruptedException { + System.out.println("INFO: Qurnik executing all queries " + queries); if(!zmi.getSons().isEmpty()) { for(ZMI son : zmi.getSons()) { Value sonName = son.getAttributes().getOrNull("name"); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 6e7d4dc..9e2a603 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -16,7 +16,7 @@ public class Stanik extends Module { } private ZMI hierarchy; - private HashMap> queries; + private HashMap queries; private long freshnessPeriod; private Set contacts; private ValueTime contactsTimestamp; @@ -26,7 +26,7 @@ public class Stanik extends Module { super(ModuleType.STATE); this.ourPath = ourPath; hierarchy = new ZMI(); - queries = new HashMap>(); + queries = new HashMap(); hierarchy.getAttributes().add("timestamp", new ValueTime(0l)); this.freshnessPeriod = freshnessPeriod; this.contactsTimestamp = ValueUtils.currentTime(); @@ -45,7 +45,7 @@ public class Stanik extends Module { private void setDefaultQuery(String name, String query) { try { ValueQuery queryValue = new ValueQuery(query); - queries.put(new Attribute(name), new SimpleImmutableEntry(queryValue, new ValueTime(0l))); + queries.put(new Attribute(name), queryValue); } catch (Exception e) { System.out.println("ERROR: failed to compile default query"); } @@ -89,7 +89,7 @@ public class Stanik extends Module { 0, message.getRequestId(), hierarchy.clone(), - (HashMap>) queries.clone(), + (HashMap) queries.clone(), contacts ); sendMessage(response); @@ -210,11 +210,13 @@ public class Stanik extends Module { } public void handleUpdateQueries(UpdateQueriesMessage message) { - for (Entry> entry : message.getQueries().entrySet()) { + System.out.println("INFO: Stanik handles update queries"); + for (Entry entry : message.getQueries().entrySet()) { Attribute attribute = entry.getKey(); - ValueTime timestamp = entry.getValue().getValue(); - Entry currentTimestampedQuery = queries.get(attribute); - if (currentTimestampedQuery == null || ValueUtils.valueLower(currentTimestampedQuery.getValue(), timestamp)) { + ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); + ValueQuery currentTimestampedQuery = queries.get(attribute); + ValueTime currentQueryTimestamp = new ValueTime(currentTimestampedQuery.getTimestamp()); + if (currentQueryTimestamp == null || ValueUtils.valueLower(currentQueryTimestamp, timestamp)) { queries.put(entry.getKey(), entry.getValue()); } } @@ -273,7 +275,7 @@ public class Stanik extends Module { return hierarchy; } - public HashMap> getQueries() { + public HashMap getQueries() { return queries; } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index ece50b5..3edda69 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -28,24 +28,30 @@ public class ValueQuery extends Value { */ public ValueQuery(String query) throws Exception { this.code = query; - Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); - this.query = (new parser(lex)).pProgram(); + if (!query.isEmpty()) { + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + this.query = (new parser(lex)).pProgram(); + } this.signature = null; this.timestamp = System.currentTimeMillis(); } public ValueQuery(String query, byte[] querySignature) throws Exception { this.code = query; - Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); - this.query = (new parser(lex)).pProgram(); + if (!query.isEmpty()) { + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + this.query = (new parser(lex)).pProgram(); + } this.signature = querySignature; this.timestamp = System.currentTimeMillis(); } public ValueQuery(QueryData queryData) throws Exception { this.code = queryData.getCode(); - Yylex lex = new Yylex(new ByteArrayInputStream(queryData.getCode().getBytes())); - this.query = (new parser(lex)).pProgram(); + if (!queryData.getCode().isEmpty()) { + Yylex lex = new Yylex(new ByteArrayInputStream(queryData.getCode().getBytes())); + this.query = (new parser(lex)).pProgram(); + } this.signature = queryData.getSignature(); this.timestamp = System.currentTimeMillis(); } -- cgit v1.2.3 From c4513b4817ec623d026fa35682b4934a211e1b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 21:54:11 +0100 Subject: Some small fixes --- .../java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java | 4 ++-- src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 9e2a603..79728b5 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -215,8 +215,8 @@ public class Stanik extends Module { Attribute attribute = entry.getKey(); ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); ValueQuery currentTimestampedQuery = queries.get(attribute); - ValueTime currentQueryTimestamp = new ValueTime(currentTimestampedQuery.getTimestamp()); - if (currentQueryTimestamp == null || ValueUtils.valueLower(currentQueryTimestamp, timestamp)) { + if (currentTimestampedQuery == null || + ValueUtils.valueLower(new ValueTime(currentTimestampedQuery.getTimestamp()), timestamp)) { queries.put(entry.getKey(), entry.getValue()); } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index 3edda69..f049603 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -56,6 +56,16 @@ public class ValueQuery extends Value { this.timestamp = System.currentTimeMillis(); } + public ValueQuery(String query, long timestamp) throws Exception { + this.code = query; + if (!query.isEmpty()) { + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + this.query = (new parser(lex)).pProgram(); + } + this.signature = null; + this.timestamp = timestamp; + } + private ValueQuery() { this.code = null; this.query = null; -- cgit v1.2.3 From 9681d76b15cc68f2f8246972ae8266522b92fefe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 22:33:17 +0100 Subject: Add query installation check --- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 2 -- .../edu/mimuw/cloudatlas/agent/modules/Qurnik.java | 3 +-- .../pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 11 +++++++++++ .../mimuw/cloudatlas/querysigner/QueryData.java | 9 ++++++++- .../querysigner/QuerySignerApiImplementation.java | 22 +++++++++++++++++----- 5 files changed, 37 insertions(+), 10 deletions(-) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index b6087e6..5137e0b 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -202,8 +202,6 @@ public class GossipGirl extends Module { state.gotQuery(message); Map queries = new HashMap(); ValueQuery vq = message.getQuery(); - ValueTime timestamp = state.getTheirQueryTimestamp(message.getName()); - vq.setTimestamp(timestamp.getValue()); queries.put(message.getName(), vq); UpdateQueriesMessage updateMessage = new UpdateQueriesMessage("", 0, queries); System.out.println("INFO: GossipGirl sending UpdateQueriesMessage"); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java index 9a544d9..c94a87d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java @@ -66,7 +66,6 @@ public class Qurnik extends Module { } private void executeAllQueries(ZMI zmi, List queries, PathName currentPath) throws InterruptedException { - System.out.println("INFO: Qurnik executing all queries " + queries); if(!zmi.getSons().isEmpty()) { for(ZMI son : zmi.getSons()) { Value sonName = son.getAttributes().getOrNull("name"); @@ -81,7 +80,7 @@ public class Qurnik extends Module { Interpreter interpreter = new Interpreter(zmi); AttributesMap newAttributes = new AttributesMap(); for (ValueQuery query : queries) { - if (query != null) { + if (query != null && query.isInstalled()) { try { List result = interpreter.interpretProgram(query.getQuery()); for(QueryResult r : result) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index f049603..95f826a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -20,6 +20,8 @@ public class ValueQuery extends Value { private byte[] signature; // Query signing timestamp private long timestamp; + // Query installation status + private boolean installed; /** * Constructs a new ValueQuery object. @@ -34,6 +36,7 @@ public class ValueQuery extends Value { } this.signature = null; this.timestamp = System.currentTimeMillis(); + this.installed = true; } public ValueQuery(String query, byte[] querySignature) throws Exception { @@ -44,6 +47,7 @@ public class ValueQuery extends Value { } this.signature = querySignature; this.timestamp = System.currentTimeMillis(); + this.installed = true; } public ValueQuery(QueryData queryData) throws Exception { @@ -54,6 +58,7 @@ public class ValueQuery extends Value { } this.signature = queryData.getSignature(); this.timestamp = System.currentTimeMillis(); + this.installed = queryData.isInstalled(); } public ValueQuery(String query, long timestamp) throws Exception { @@ -64,6 +69,7 @@ public class ValueQuery extends Value { } this.signature = null; this.timestamp = timestamp; + this.installed = true; } private ValueQuery() { @@ -71,6 +77,7 @@ public class ValueQuery extends Value { this.query = null; this.signature = null; this.timestamp = System.currentTimeMillis(); + this.installed = true; } public String getCode() { return code; } @@ -85,6 +92,10 @@ public class ValueQuery extends Value { public void setTimestamp(long timestamp) { this.timestamp = timestamp; } + public boolean isInstalled() { return installed; } + + public void setInstalled(boolean installed) { this.installed = installed; } + @Override public Type getType() { return TypePrimitive.QUERY; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java index 7801a28..87b965c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -10,10 +10,13 @@ public class QueryData implements Serializable { // Query signing timestamp private long timestamp; + private boolean installed; + public QueryData(String code, byte[] signature) { this.code = code; this.signature = signature; - this.timestamp = System.currentTimeMillis();; + this.timestamp = System.currentTimeMillis(); + this.installed = true; } public String getCode() { @@ -27,4 +30,8 @@ public class QueryData implements Serializable { public long getTimestamp() { return timestamp; } + + public boolean isInstalled() { return installed; } + + public void setInstalled(boolean installed) { this.installed = installed; } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java index 3ab2931..a6233d3 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java @@ -1,7 +1,6 @@ package pl.edu.mimuw.cloudatlas.querysigner; import pl.edu.mimuw.cloudatlas.ByteSerializer; -import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import javax.crypto.BadPaddingException; @@ -20,7 +19,7 @@ public class QuerySignerApiImplementation implements QuerySignerApi { private final static String DIGEST_ALGORITHM = "SHA-256"; private PublicKey publicKey; private PrivateKey privateKey; - private Map queries; + private Map queries; private Set attribsSetByQueries; public QuerySignerApiImplementation(PublicKey publicKey, PrivateKey privateKey) { @@ -90,7 +89,10 @@ public class QuerySignerApiImplementation implements QuerySignerApi { byte[] serializedQuery = serializeQuery(queryName, queryCode, install); byte[] hashedQuery = cryptographicHash(serializedQuery); byte[] querySignature = encryptQuery(hashedQuery); - return new QueryData(queryCode, querySignature); + QueryData newQuery = new QueryData(queryCode, querySignature); + newQuery.setInstalled(install); + this.queries.put(queryName, newQuery); + return newQuery; } catch (Exception e) { e.printStackTrace(); throw new RemoteException(e.getLocalizedMessage()); @@ -99,7 +101,12 @@ public class QuerySignerApiImplementation implements QuerySignerApi { @Override public QueryData signInstallQuery(String queryName, String queryCode) throws RemoteException { - return signQuery(queryName, queryCode, true); + QueryUtils.validateQueryName(queryName); + if (this.queries.containsKey(queryName) && this.queries.get(queryName).isInstalled()) { + throw new RemoteException("Query already installed"); + } else { + return signQuery(queryName, queryCode, true); + } } public static void validateInstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException,IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, QuerySigner.InvalidQueryException { @@ -120,7 +127,12 @@ public class QuerySignerApiImplementation implements QuerySignerApi { @Override public QueryData signUninstallQuery(String queryName) throws RemoteException { - return signQuery(queryName, "", false); + QueryUtils.validateQueryName(queryName); + if (this.queries.containsKey(queryName) && this.queries.get(queryName).isInstalled()) { + return signQuery(queryName, "", false); + } else { + throw new RemoteException("Query is not installed"); + } } public static void validateUninstallQuery(String queryName, QueryData query, PublicKey publicKey) throws RemoteException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, QuerySigner.InvalidQueryException, NoSuchPaddingException, InvalidKeyException { -- cgit v1.2.3 From 93cafafbc6fe53628be1b4ef08b6d9ca48ec6bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 22:50:10 +0100 Subject: Add validation in Stanik --- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 1 + .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 31 ++++++++++++++++++++++ .../mimuw/cloudatlas/querysigner/QueryData.java | 7 +++++ .../mimuw/cloudatlas/querysigner/QueryUtils.java | 11 ++++++++ 4 files changed, 50 insertions(+) (limited to 'src/main/java/pl/edu') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index 5137e0b..a952274 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -26,6 +26,7 @@ import pl.edu.mimuw.cloudatlas.model.ValueContact; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueTime; import pl.edu.mimuw.cloudatlas.model.ZMI; +import pl.edu.mimuw.cloudatlas.querysigner.QuerySignerApiImplementation; public class GossipGirl extends Module { private long nextGossipId = 0; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 79728b5..efc5605 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -1,12 +1,21 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import java.nio.file.Path; +import java.rmi.RemoteException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; import java.util.*; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.*; import pl.edu.mimuw.cloudatlas.model.*; +import pl.edu.mimuw.cloudatlas.querysigner.*; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; public class Stanik extends Module { private class InvalidUpdateAttributesMessage extends Exception { @@ -21,6 +30,7 @@ public class Stanik extends Module { private Set contacts; private ValueTime contactsTimestamp; private PathName ourPath; + private PublicKey publicKey; public Stanik(PathName ourPath, long freshnessPeriod) { super(ModuleType.STATE); @@ -31,6 +41,8 @@ public class Stanik extends Module { this.freshnessPeriod = freshnessPeriod; this.contactsTimestamp = ValueUtils.currentTime(); this.contacts = new HashSet<>(); + String publicKeyFile = System.getProperty("public_key_file"); + this.publicKey = KeyUtils.getPublicKey(publicKeyFile); setDefaultQueries(); } @@ -213,6 +225,25 @@ public class Stanik extends Module { System.out.println("INFO: Stanik handles update queries"); for (Entry entry : message.getQueries().entrySet()) { Attribute attribute = entry.getKey(); + ValueQuery query = entry.getValue(); + try { + if (query.isInstalled()) { + QuerySignerApiImplementation.validateInstallQuery( + attribute.getName(), + QueryUtils.constructQueryData(query), + this.publicKey); + + } else { + QuerySignerApiImplementation.validateUninstallQuery( + attribute.getName(), + QueryUtils.constructQueryData(query), + this.publicKey); + } + } catch (RemoteException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | QuerySigner.InvalidQueryException e) { + System.out.println("ERROR: Query " + attribute.getName() + " was not updated in Stanik with error message " + e.getMessage()); + e.printStackTrace(); + continue; + } ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); ValueQuery currentTimestampedQuery = queries.get(attribute); if (currentTimestampedQuery == null || diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java index 87b965c..a9e039e 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -19,6 +19,13 @@ public class QueryData implements Serializable { this.installed = true; } + public QueryData(String code, byte[] signature, long timestamp, boolean installed) { + this.code = code; + this.signature = signature; + this.timestamp = timestamp; + this.installed = installed; + } + public String getCode() { return code; } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java index 6ec62f4..c46e32d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java @@ -1,5 +1,7 @@ package pl.edu.mimuw.cloudatlas.querysigner; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; + import java.rmi.RemoteException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -13,4 +15,13 @@ public class QueryUtils { throw new RemoteException("Invalid query identifier"); } } + + public static QueryData constructQueryData(ValueQuery valueQuery) { + return new QueryData( + valueQuery.getCode(), + valueQuery.getSignature(), + valueQuery.getTimestamp(), + valueQuery.isInstalled() + ); + } } -- cgit v1.2.3