From f9649f72cc5f7232a6b62bd4e13fee39580ab471 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Wed, 9 Nov 2016 17:02:54 -0800 Subject: [PATCH] Adding permission checks for Java; fixing type translations for return and parameter types for C++ in IoTCompiler --- iotjava/iotpolicy/IoTCompiler.java | 22 ++++++- iotjava/iotrmi/Java/IoTRMIObject.java | 16 +++++ .../Java/sample/CallBack_CBSkeleton.java | 12 ---- .../iotrmi/Java/sample/CallBack_CBStub.java | 12 ---- .../Java/sample/TestClass_Skeleton.java | 62 ++++++++++++------- .../iotrmi/Java/sample/TestClass_Stub.java | 11 +++- 6 files changed, 86 insertions(+), 49 deletions(-) diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 94029ca..90734e3 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -302,7 +302,8 @@ public class IoTCompiler { // Generate array of parameter types print("Class[] paramCls = new Class[] { "); for (int i = 0; i < methParams.size(); i++) { - print(getSimpleType(methPrmTypes.get(i)) + ".class"); + String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + print(getSimpleType(paramType) + ".class"); // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { print(", "); @@ -556,7 +557,8 @@ public class IoTCompiler { // Generate array of parameter types print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { - print("\"" + checkAndGetCplusType(methPrmTypes.get(i)) + "\""); + String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + print("\"" + getSimpleType(paramType) + "\""); // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { print(", "); @@ -1101,6 +1103,22 @@ public class IoTCompiler { } + // Detect array declaration, e.g. int A[], + // then generate type "int[]" + private String checkAndGetArray(String paramType, String param) { + + String paramTypeRet = null; + // Check for array declaration + if (param.contains("[]")) { + paramTypeRet = paramType + "[]"; + } else + // Just return it as is if it's not an array + paramTypeRet = paramType; + + return paramTypeRet; + } + + // Get simple types, e.g. HashSet for HashSet<...> // Basically strip off the "<...>" private String checkAndGetParamClass(String paramType, boolean needPtr) { diff --git a/iotjava/iotrmi/Java/IoTRMIObject.java b/iotjava/iotrmi/Java/IoTRMIObject.java index 90e5b54..6f52228 100644 --- a/iotjava/iotrmi/Java/IoTRMIObject.java +++ b/iotjava/iotrmi/Java/IoTRMIObject.java @@ -114,6 +114,22 @@ public class IoTRMIObject { } + /** + * static version of getMethodId() + */ + public static int getMethodId(byte[] methodBytes) { + + // Get method Id bytes + byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN]; + // Method Id is positioned after object Id in the byte array + System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN); + // Get method Id + int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes); + // Get method Id + return methodId; + } + + /** * getMethodParams() gets method params based on byte array received *

diff --git a/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java b/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java index 0e01eba..7eaf1ff 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java +++ b/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java @@ -8,11 +8,6 @@ import iotrmi.Java.IoTRMIObject; public class CallBack_CBSkeleton implements CallBackInterface { private int objectId = 0; // Default value is 0 - private final static String[] methodSignatures = { - - "intprintInt()", - "voidsetInt(int)" - }; private CallBackInterface cb; @@ -65,13 +60,6 @@ public class CallBack_CBSkeleton implements CallBackInterface { } - // Return method signatures - public static String[] getMethodSignatures() { - - return methodSignatures; - } - - public static void main(String[] args) throws Exception { } diff --git a/iotjava/iotrmi/Java/sample/CallBack_CBStub.java b/iotjava/iotrmi/Java/sample/CallBack_CBStub.java index 1278d6e..f438778 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_CBStub.java +++ b/iotjava/iotrmi/Java/sample/CallBack_CBStub.java @@ -11,11 +11,6 @@ public class CallBack_CBStub implements CallBackInterface { private IoTRMICall rmiCall; private int objectId = 0; // Default value is 0 - private final static String[] methodSignatures = { - - "intprintInt()", - "voidsetInt(int)" - }; /** * Constructors @@ -27,13 +22,6 @@ public class CallBack_CBStub implements CallBackInterface { } - // Return method signatures - public static String[] getMethodSignatures() { - - return methodSignatures; - } - - public int printInt() { int methodId = 0; diff --git a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java index c16b124..5958f1f 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java @@ -6,6 +6,10 @@ import java.util.Set; import java.util.Map; import java.util.HashMap; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + import iotrmi.Java.IoTRMIObject; import iotrmi.Java.IoTRMICall; @@ -14,7 +18,6 @@ public class TestClass_Skeleton implements TestClassInterface { /** * Class Constants */ - private int objectId = 0; // Default value is 0 private static int objIdCnt = 0; // Counter for callback object Ids private TestClassInterface tc; private int port; @@ -22,6 +25,11 @@ public class TestClass_Skeleton implements TestClassInterface { private IoTRMICall rmiCall; private CallBackInterface cbstub; + // Object permission + private int object0Id = 0; + private static Integer[] object0Permission = { 0, 1, 2, 3, 4, 5 }; + private List set0Allowed; + /** * Constructors @@ -33,6 +41,7 @@ public class TestClass_Skeleton implements TestClassInterface { tc = _tc; port = _port; rmiObj = new IoTRMIObject(_port); + set0Allowed = Arrays.asList(object0Permission); ___waitRequestInvokeMethod(); } @@ -290,30 +299,39 @@ public class TestClass_Skeleton implements TestClassInterface { rmiObj.getMethodBytes(); int _objectId = rmiObj.getObjectId(); - if (_objectId == objectId) { + int methodId = rmiObj.getMethodId(); + if (_objectId == object0Id) { // Multiplex based on object Id - int methodId = rmiObj.getMethodId(); - switch (methodId) { + // Complain if the method is not allowed + if (!set0Allowed.contains(methodId)) + throw new Error("TestClass_Skeleton: This object is not allowed to access method " + methodId); + // If we have more than 1 object Id... + //else if (_objectId == object1Id) { + + } else + throw new Error("TestClass_Skeleton: Unrecognizable object Id: " + _objectId); - case 0: ___setA(); break; - case 1: ___setB(); break; - case 2: ___setC(); break; - case 3: ___sumArray(); break; - case 4: ___setAndGetA(); break; - case 5: ___setACAndGetA(); break; - case 6: ___callBack(); break; - case 7: ___registerCallback(); break; - case 8: ____registerCallback(); break; - // Special option to register callback - case 9: ___regCB(); break; - // Struct handling (3 is the size of the struct) - case 10: ___handleStruct(structSize1); break; - case 11: structSize1 = ___structSize(); break; - case 12: ___handleEnum(); break; - default: - throw new Error("Method Id not recognized!"); - } + switch (methodId) { + + case 0: ___setA(); break; + case 1: ___setB(); break; + case 2: ___setC(); break; + case 3: ___sumArray(); break; + case 4: ___setAndGetA(); break; + case 5: ___setACAndGetA(); break; + case 6: ___callBack(); break; + case 7: ___registerCallback(); break; + case 8: ____registerCallback(); break; + // Special option to register callback + case 9: ___regCB(); break; + // Struct handling (3 is the size of the struct) + case 10: ___handleStruct(structSize1); break; + case 11: structSize1 = ___structSize(); break; + case 12: ___handleEnum(); break; + default: + throw new Error("Method Id not recognized!"); } + } } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index 9e2006d..bb6df9b 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -24,7 +24,9 @@ public class TestClass_Stub implements TestClassInterface { * Class Constants */ private int objectId = 0; // Default value is 0 - + // This is permission system for callback objects + private static Integer[] object0Permission = { 0, 1 }; // object0 is the callback object + private List set0Allowed; /** * Properties and constants for Callbacks! @@ -43,6 +45,8 @@ public class TestClass_Stub implements TestClassInterface { ports = _ports; rmiCall = new IoTRMICall(_port, _address, _rev); + set0Allowed = Arrays.asList(object0Permission); + // Only for callbacks!!! listCBObj = new ArrayList(); ___initCallBack(); @@ -69,6 +73,11 @@ public class TestClass_Stub implements TestClassInterface { Object retObj = null; while (true) { byte[] method = rmiObj.getMethodBytes(); + // Permission checking + int methId = IoTRMIObject.getMethodId(method); + if (!set0Allowed.contains(methId)) + throw new Error("CallBack_CBSkeleton: This object is not allowed to access method " + methId); + int objId = IoTRMIObject.getObjectId(method); CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId); if (skel != null) { -- 2.34.1