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/mimuw/cloudatlas/querysigner') 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/querysigner/QuerySigner.java | 26 +++++ .../querysigner/QuerySignerApiImplementation.java | 112 +++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySignerApiImplementation.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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(); + } +} -- 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/mimuw/cloudatlas/querysigner') 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 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/querysigner/QuerySigner.java | 12 +++- .../querysigner/QuerySignerApiImplementation.java | 84 +++++++++++++++------- 2 files changed, 66 insertions(+), 30 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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; -- 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 --- src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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/querysigner/QueryData.java | 26 +++++++++++++++++ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 20 ++++++++++--- .../querysigner/QuerySignerApiImplementation.java | 34 +++++++--------------- 3 files changed, 52 insertions(+), 28 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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(); } } -- 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 --- .../edu/mimuw/cloudatlas/querysigner/KeyUtils.java | 35 ++++++++++++++++++++++ .../mimuw/cloudatlas/querysigner/QuerySigner.java | 8 ++--- 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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/querysigner/QueryData.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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/mimuw/cloudatlas/querysigner') 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 --- .../mimuw/cloudatlas/querysigner/QuerySigner.java | 2 +- .../querysigner/QuerySignerApiImplementation.java | 36 +++++++++------------- 2 files changed, 16 insertions(+), 22 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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 { } } -- 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 --- .../querysigner/QuerySignerApiImplementation.java | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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 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/querysigner/QueryData.java | 9 ++++++++- .../querysigner/QuerySignerApiImplementation.java | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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 --- .../java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java | 7 +++++++ .../java/pl/edu/mimuw/cloudatlas/querysigner/QueryUtils.java | 11 +++++++++++ 2 files changed, 18 insertions(+) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner') 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