diff options
author | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-14 20:30:22 +0100 |
---|---|---|
committer | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-14 20:30:22 +0100 |
commit | 99b7023aba5c09f069badc35d05ea3ef53f3a6d2 (patch) | |
tree | 5e4559a79f5823d3927439c928c2d424fdb1ff70 | |
parent | 3159df80ee02f6f65fe8e8ae1bae8fecb8fad070 (diff) |
Add query uninstall calls
3 files changed, 41 insertions, 27 deletions
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<String> zones = new HashSet<String>(); 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<Attribute, Entry<ValueQuery, ValueTime>> 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); } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/QuerySignerApiImplementationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/QuerySignerApiImplementationTest.java index 70aad63..29d53d2 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/QuerySignerApiImplementationTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/QuerySignerApiImplementationTest.java @@ -18,7 +18,7 @@ import java.security.PublicKey; public class QuerySignerApiImplementationTest { @Test - public void testQueryVerification() { + public void testQueryInstallVerification() { QuerySignerApiImplementation queryApi; try { @@ -29,21 +29,24 @@ public class QuerySignerApiImplementationTest { queryApi = new QuerySignerApiImplementation(publicKey, privateKey); QueryData signedQuery = queryApi.signInstallQuery("&a", "SELECT 1 AS ONE"); QuerySignerApiImplementation.validateInstallQuery("&a", signedQuery, publicKey); - } catch (IOException e) { - e.printStackTrace(); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - } catch (BadPaddingException e) { - e.printStackTrace(); - } catch (QuerySigner.InvalidQueryException e) { + } catch (IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | QuerySigner.InvalidQueryException e) { e.printStackTrace(); } + } + @Test + public void testQueryUninstallVerification() { + QuerySignerApiImplementation queryApi; + try { + String publicKeyFile = "build/tmp/query_signer.pub"; + String privateKeyFile = "build/tmp/query_signer"; + PublicKey publicKey = KeyUtils.getPublicKey(publicKeyFile); + PrivateKey privateKey = KeyUtils.getPrivateKey(privateKeyFile); + queryApi = new QuerySignerApiImplementation(publicKey, privateKey); + QueryData signedQuery = queryApi.signUninstallQuery("&a"); + QuerySignerApiImplementation.validateUninstallQuery("&a", signedQuery, publicKey); + } catch (IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | QuerySigner.InvalidQueryException e) { + e.printStackTrace(); + } } } |