diff --git a/src/main/java/tmltranslator/toavatar/TML2Avatar.java b/src/main/java/tmltranslator/toavatar/TML2Avatar.java index ab18a9565a7749bbef40f870b0ed23199bb4ba04..fd1fe54a3324c512eebf0e3ca0c8dcb77fa0facb 100644 --- a/src/main/java/tmltranslator/toavatar/TML2Avatar.java +++ b/src/main/java/tmltranslator/toavatar/TML2Avatar.java @@ -656,6 +656,7 @@ public class TML2Avatar { elementList.add(as); elementList.add(tran); if (security && ae.securityPattern!=null){ + //If encryption if (ae.securityPattern!=null && ae.getName().contains("encrypt")){ secPatterns.add(ae.securityPattern); if (ae.securityPattern.type.equals("Advanced")){ @@ -843,91 +844,110 @@ public class TML2Avatar { } else if (ae.securityPattern!=null && ae.getName().contains("decrypt")){ + //Decryption action block.addAttribute(new AvatarAttribute(ae.securityPattern.name, AvatarType.INTEGER, block, null)); block.addAttribute(new AvatarAttribute(ae.securityPattern.name+"_encrypted", AvatarType.INTEGER, block, null)); if (ae.securityPattern.type.equals("Symmetric Encryption")){ - if (ae.securityPattern.key.isEmpty()){ + if (!ae.securityPattern.key.isEmpty()){ + //Decrypting a key + //Add sdecrypt method + AvatarMethod sdecrypt = new AvatarMethod("sdecrypt", ae); + block.addAttribute(new AvatarAttribute("key_"+ae.securityPattern.key, AvatarType.INTEGER, block,null)); + block.addAttribute(new AvatarAttribute("key_"+ae.securityPattern.name, AvatarType.INTEGER, block,null)); + block.addAttribute(new AvatarAttribute("encryptedKey_"+ae.securityPattern.key, AvatarType.INTEGER, block,null)); + + sdecrypt.addParameter(block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)); + sdecrypt.addParameter(block.getAvatarAttributeWithName("key_"+ae.securityPattern.name)); + sdecrypt.addReturnParameter(block.getAvatarAttributeWithName("key_"+ae.securityPattern.key)); + if (block.getAvatarAttributeWithName("key_"+ae.securityPattern.key)!=null && block.getAvatarAttributeWithName("key_"+ae.securityPattern.name)!=null && block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)!=null){ + block.addMethod(sdecrypt); + tran.addAction(ae.securityPattern.key+" = sdecrypt(encryptedKey_"+ae.securityPattern.key+", key_"+ae.securityPattern.name+")"); + } + } + else { + //Decrypting data AvatarMethod sdecrypt = new AvatarMethod("sdecrypt", ae); block.addAttribute(new AvatarAttribute(ae.securityPattern.name, AvatarType.INTEGER, block,null)); + block.addAttribute(new AvatarAttribute("key_"+ae.securityPattern.name, AvatarType.INTEGER, block,null)); + block.addAttribute(new AvatarAttribute(ae.securityPattern.name+"_encrypted", AvatarType.INTEGER, block,null)); + sdecrypt.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")); sdecrypt.addParameter(block.getAvatarAttributeWithName("key_"+ae.securityPattern.name)); sdecrypt.addReturnParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); if (block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")!=null && block.getAvatarAttributeWithName("key_"+ae.securityPattern.name)!=null && block.getAvatarAttributeWithName(ae.securityPattern.name)!=null){ block.addMethod(sdecrypt); + tran.addAction(ae.securityPattern.name+" = sdecrypt("+ae.securityPattern.name+"_encrypted, key_"+ae.securityPattern.name+")"); } - tran.addAction(ae.securityPattern.name+" = sdecrypt("+ae.securityPattern.name+"_encrypted, key_"+ae.securityPattern.name+")"); } - else { - AvatarMethod sdecrypt = new AvatarMethod("sdecrypt", ae); - sdecrypt.addParameter(block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)); - block.addAttribute(new AvatarAttribute("key_"+ae.securityPattern.key, AvatarType.INTEGER, block,null)); - sdecrypt.addParameter(block.getAvatarAttributeWithName("key_"+ae.securityPattern.key)); - sdecrypt.addReturnParameter(block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)); - if (block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)!=null && block.getAvatarAttributeWithName("key_"+ae.securityPattern.name)!=null && block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.name)!=null){ - block.addMethod(sdecrypt); - } - tran.addAction(ae.securityPattern.key+" = sdecrypt(encryptedKey_"+ae.securityPattern.key+", key_"+ae.securityPattern.name+")"); - } - // elementList.add(as); - // elementList.add(tran); - // as.addNext(tran); - if (!ae.securityPattern.nonce.isEmpty()){ - block.addAttribute(new AvatarAttribute("testnonce_"+ae.securityPattern.nonce, AvatarType.INTEGER, block, null)); - AvatarMethod get2 = new AvatarMethod("get2",ae); - get2.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); - get2.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); - get2.addParameter(block.getAvatarAttributeWithName("testnonce_"+ae.securityPattern.nonce)); - if (block.getAvatarAttributeWithName(ae.securityPattern.name)!=null && block.getAvatarAttributeWithName("testnonce_"+ae.securityPattern.nonce)!=null) { - System.out.println("adding get2"); - block.addMethod(get2); + if (!ae.securityPattern.nonce.isEmpty()){ + //Separate out the nonce + block.addAttribute(new AvatarAttribute("testnonce_"+ae.securityPattern.nonce, AvatarType.INTEGER, block, null)); + //Add get2 method + AvatarMethod get2 = new AvatarMethod("get2",ae); + get2.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); + get2.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); + get2.addParameter(block.getAvatarAttributeWithName("testnonce_"+ae.securityPattern.nonce)); + if (block.getAvatarAttributeWithName(ae.securityPattern.name)!=null && block.getAvatarAttributeWithName("testnonce_"+ae.securityPattern.nonce)!=null) { + block.addMethod(get2); + tran.addAction("get2("+ae.securityPattern.name + ","+ae.securityPattern.name+",testnonce_"+ae.securityPattern.nonce+")"); + } + + //Add state after get2 statement + AvatarState guardState = new AvatarState(ae.getName().replaceAll(" ","")+"_guarded", ae.getReferenceObject()); + tran.addNext(guardState); + tran=new AvatarTransition(block, "__guard_"+ae.getName(), ae.getReferenceObject()); + guardState.addNext(tran); + elementList.add(guardState); + elementList.add(tran); + + //Guard transition to determine if nonce matches + tran.setGuard("testnonce_"+ae.securityPattern.nonce+"==" + ae.securityPattern.nonce); } - tran.addAction("get2("+ae.securityPattern.name + ","+ae.securityPattern.name+",testnonce_"+ae.securityPattern.nonce+")"); - AvatarState guardState = new AvatarState(ae.getName().replaceAll(" ","")+"_guarded", ae.getReferenceObject()); - tran.addNext(guardState); - tran=new AvatarTransition(block, "__guard_"+ae.getName(), ae.getReferenceObject()); - guardState.addNext(tran); - tran.setGuard("testnonce_"+ae.securityPattern.nonce+"==" + ae.securityPattern.nonce); - } - AvatarState dummy = new AvatarState(ae.getName().replaceAll(" ","")+"_dummy", ae.getReferenceObject()); - ae.securityPattern.state2=dummy; - tran.addNext(dummy); + //Add a dummy state afterwards for authenticity after decrypting the data + AvatarState dummy = new AvatarState(ae.getName().replaceAll(" ","")+"_dummy", ae.getReferenceObject()); + ae.securityPattern.state2=dummy; + tran.addNext(dummy); tran = new AvatarTransition(block, "__after_"+ae.getName(), ae.getReferenceObject()); - dummy.addNext(tran); + dummy.addNext(tran); elementList.add(dummy); elementList.add(tran); - if (ae.securityPattern.nonce.isEmpty()){ - AvatarAttributeState authDest = new AvatarAttributeState(block.getName()+"."+dummy.getName()+"."+ae.securityPattern.name,ae.getReferenceObject(),block.getAvatarAttributeWithName(ae.securityPattern.name), dummy); - signalAuthDestMap.put(ae.securityPattern.name, authDest); - } - else { AvatarAttributeState authDest = new AvatarAttributeState(block.getName()+"."+dummy.getName()+"."+ae.securityPattern.name,ae.getReferenceObject(),block.getAvatarAttributeWithName(ae.securityPattern.name), dummy); signalAuthDestMap.put(ae.securityPattern.name, authDest); } - } - else if (ae.securityPattern.type.equals("Asymmetric Encryption")){ - AvatarMethod adecrypt = new AvatarMethod("adecrypt", ae); - if (ae.securityPattern.key.isEmpty()){ - block.addAttribute(new AvatarAttribute(ae.securityPattern.name+"_encrypted",AvatarType.INTEGER,block,null)); - block.addAttribute(new AvatarAttribute(ae.securityPattern.name,AvatarType.INTEGER,block,null)); - adecrypt.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")); - adecrypt.addParameter(block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)); - adecrypt.addReturnParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); - if (block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")!=null && block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)!= null){ - block.addMethod(adecrypt); - } - tran.addAction(ae.securityPattern.name+" = adecrypt("+ae.securityPattern.name+"_encrypted, privKey_"+ae.securityPattern.name+")"); - } - else { - block.addAttribute(new AvatarAttribute("encryptedKey_"+ae.securityPattern.key,AvatarType.INTEGER,block,null)); - adecrypt.addParameter(block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)); - adecrypt.addParameter(block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)); - block.addAttribute(new AvatarAttribute("privKey_"+ae.securityPattern.key, AvatarType.INTEGER, block,null)); + else if (ae.securityPattern.type.equals("Asymmetric Encryption")){ + AvatarMethod adecrypt = new AvatarMethod("adecrypt", ae); - if (block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)!=null && block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)!= null){ - block.addMethod(adecrypt); + if (!ae.securityPattern.key.isEmpty()){ + //Decrypting key + block.addAttribute(new AvatarAttribute("encryptedKey_"+ae.securityPattern.key,AvatarType.INTEGER,block,null)); + block.addAttribute(new AvatarAttribute("privKey_"+ae.securityPattern.name, AvatarType.INTEGER, block,null)); + block.addAttribute(new AvatarAttribute("key_"+ae.securityPattern.key, AvatarType.INTEGER, block,null)); + + adecrypt.addParameter(block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)); + adecrypt.addParameter(block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)); + adecrypt.addReturnParameter(block.getAvatarAttributeWithName("key_"+ae.securityPattern.key)); + + if (block.getAvatarAttributeWithName("encryptedKey_"+ae.securityPattern.key)!=null && block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)!= null && block.getAvatarAttributeWithName("key_"+ae.securityPattern.key)!=null){ + block.addMethod(adecrypt); + tran.addAction("key_"+ae.securityPattern.key+" = adecrypt(encryptedKey_"+ae.securityPattern.key+", privKey_"+ae.securityPattern.name+")"); + } + } + else { + //Decrypting data + + //Add adecrypt method + block.addAttribute(new AvatarAttribute(ae.securityPattern.name+"_encrypted",AvatarType.INTEGER,block,null)); + block.addAttribute(new AvatarAttribute(ae.securityPattern.name,AvatarType.INTEGER,block,null)); + block.addAttribute(new AvatarAttribute("privKey_"+ae.securityPattern.name,AvatarType.INTEGER,block,null)); + + adecrypt.addParameter(block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")); + adecrypt.addParameter(block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)); + adecrypt.addReturnParameter(block.getAvatarAttributeWithName(ae.securityPattern.name)); + if (block.getAvatarAttributeWithName(ae.securityPattern.name+"_encrypted")!=null && block.getAvatarAttributeWithName("privKey_"+ae.securityPattern.name)!= null && block.getAvatarAttributeWithName(ae.securityPattern.name)!=null){ + block.addMethod(adecrypt); + tran.addAction(ae.securityPattern.name+" = adecrypt("+ae.securityPattern.name+"_encrypted, privKey_"+ae.securityPattern.name+")"); + } } - tran.addAction("key_"+ae.securityPattern.key+" = adecrypt(encryptedKey_"+ae.securityPattern.key+", privKey_"+ae.securityPattern.name+")"); - } //elementList.add(as); // elementList.add(tran); //as.addNext(tran); @@ -1061,6 +1081,7 @@ public class TML2Avatar { AvatarState signalState = new AvatarState("signalstate_"+ae.getName().replaceAll(" ","")+"_"+ch.getName(),ae.getReferenceObject(), checkAcc); AvatarTransition signalTran = new AvatarTransition(block, "__after_signalstate_"+ae.getName()+"_"+ch.getName(), ae.getReferenceObject()); if (ae instanceof TMLReadChannel){ + //Create signal if it does not already exist if (!signalInMap.containsKey(ch.getName())){ sig = new AvatarSignal(getName(ch.getName()), AvatarSignal.IN, ch.getReferenceObject()); signals.add(sig); @@ -1078,16 +1099,19 @@ public class TML2Avatar { AvatarActionOnSignal as = new AvatarActionOnSignal(ae.getName(), sig, ae.getReferenceObject()); if (ae.securityPattern!=null){ + //If nonce if (ae.securityPattern.type.equals("Nonce")){ block.addAttribute(new AvatarAttribute(ae.securityPattern.name, AvatarType.INTEGER, block,null)); as.addValue(ae.securityPattern.name); } + //Send the encrypted key else if (!ae.securityPattern.key.isEmpty()){ as.addValue("encryptedKey_"+ae.securityPattern.key); AvatarAttribute data= new AvatarAttribute("encryptedKey_"+ae.securityPattern.key, AvatarType.INTEGER, block, null); block.addAttribute(data); } else { + //Send the encrypted data if (!secChannelMap.containsKey(ae.securityPattern.name)){ List<String> tmp=new ArrayList<String>(); secChannelMap.put(ae.securityPattern.name,tmp); @@ -1131,6 +1155,7 @@ public class TML2Avatar { //WriteChannel if (!signalOutMap.containsKey(ch.getName())){ + //Add signal if it does not exist sig = new AvatarSignal(getName(ch.getName()), AvatarSignal.OUT, ch.getReferenceObject()); signals.add(sig); block.addSignal(sig); @@ -1165,15 +1190,18 @@ public class TML2Avatar { AvatarActionOnSignal as = new AvatarActionOnSignal(ae.getName(), sig, ae.getReferenceObject()); if (ae.securityPattern!=null){ + //send nonce if (ae.securityPattern.type.equals("Nonce")){ as.addValue(ae.securityPattern.name); } + //send encrypted key else if (!ae.securityPattern.key.isEmpty()){ as.addValue("encryptedKey_"+ae.securityPattern.key); AvatarAttribute data= new AvatarAttribute("encryptedKey_"+ae.securityPattern.key, AvatarType.INTEGER, block, null); block.addAttribute(data); } else { + //send encrypted data as.addValue(ae.securityPattern.name+"_encrypted"); AvatarAttribute data= new AvatarAttribute(ae.securityPattern.name+"_encrypted", AvatarType.INTEGER, block, null); block.addAttribute(data); diff --git a/src/main/java/ui/avatarbd/AvatarBDPerformancePragma.java b/src/main/java/ui/avatarbd/AvatarBDPerformancePragma.java index 09a95a53bef5975c3ac694cb930bb76595cf8e9b..aa5b5c0ab0f133b562e6c833ef86270aec7a5c68 100644 --- a/src/main/java/ui/avatarbd/AvatarBDPerformancePragma.java +++ b/src/main/java/ui/avatarbd/AvatarBDPerformancePragma.java @@ -196,7 +196,7 @@ public class AvatarBDPerformancePragma extends TGCScalableWithoutInternalCompone int i = 1; Font heading = new Font("heading", Font.BOLD, 14); g.setFont(heading); - g.drawString("Performance Pragma", x+textX, y+textY + currentFontSize); + g.drawString("Performance Pragmas", x+textX, y+textY + currentFontSize); g.setFont(fold); for (String s: properties){ g.drawString(s, x + textX, y + textY + (i+1)* currentFontSize); @@ -252,7 +252,7 @@ public class AvatarBDPerformancePragma extends TGCScalableWithoutInternalCompone public boolean editOndoubleClick(JFrame frame) { String oldValue = value; - JDialogPerformancePragma jdn = new JDialogPerformancePragma(frame, "Setting the performance pragma", value); + JDialogPerformancePragma jdn = new JDialogPerformancePragma(frame, "Setting the performance pragmas", value); //jdn.setLocation(200, 150); GraphicLib.centerOnParent(jdn); AvatarBDPanel abdp = (AvatarBDPanel) tdp; diff --git a/src/main/java/ui/avatarbd/AvatarBDPragma.java b/src/main/java/ui/avatarbd/AvatarBDPragma.java index 5a031223c8e00cc98c7c661ae07005f543fad62a..c29e8ac500c4329c7f00c3b9bce4e0f457b81fe2 100755 --- a/src/main/java/ui/avatarbd/AvatarBDPragma.java +++ b/src/main/java/ui/avatarbd/AvatarBDPragma.java @@ -188,7 +188,7 @@ public class AvatarBDPragma extends TGCScalableWithoutInternalComponent { * from the Graphics object. * Thus we use a saved FontMetrics object in TDiagramPanel that only changes when zoom changes. */ - int desiredWidth = Math.max(this.minWidth, 2*this.tdp.stringWidth(g, "Property Pragma") + marginX+ textX); + int desiredWidth = Math.max(this.minWidth, 2*this.tdp.stringWidth(g, "Property Pragmas") + marginX+ textX); for(int i=0; i< values.length; i++) desiredWidth = Math.max(desiredWidth, this.tdp.stringWidth(g, values[i]) + marginX+textX); @@ -227,7 +227,7 @@ public class AvatarBDPragma extends TGCScalableWithoutInternalComponent { int i = 1; Font heading = new Font("heading", Font.BOLD, this.tdp.getFontSize ()*7/6); g.setFont(heading); - g.drawString("Model Pragma", x+textX, y+textY + currentFontSize); + g.drawString("Model Pragmas", x+textX, y+textY + currentFontSize); g.setFont(fold); for (String s: models){ g.drawString(s, x + textX, y + textY + (i+1)* currentFontSize); @@ -246,7 +246,7 @@ public class AvatarBDPragma extends TGCScalableWithoutInternalComponent { i++; g.drawLine(x, y+textY/2+i*currentFontSize, x+width, y+textY/2+i*currentFontSize); g.setFont(heading); - g.drawString("Property Pragma", x+textX, y+textY+(i+1)* currentFontSize); + g.drawString("Property Pragmas", x+textX, y+textY+(i+1)* currentFontSize); g.setFont(fold); i++; // System.out.println("syntax errors " + syntaxErrors.toString()); @@ -383,7 +383,7 @@ public class AvatarBDPragma extends TGCScalableWithoutInternalComponent { public boolean editOndoubleClick(JFrame frame) { String oldValue = value; - JDialogPragma jdn = new JDialogPragma(frame, "Setting the pragma", value); + JDialogPragma jdn = new JDialogPragma(frame, "Setting the security pragmas", value); //jdn.setLocation(200, 150); AvatarBDPanel abdp = (AvatarBDPanel) tdp; jdn.blockAttributeMap = abdp.getBlockStrings(true,false,false); diff --git a/src/main/java/ui/avatarbd/AvatarBDSafetyPragma.java b/src/main/java/ui/avatarbd/AvatarBDSafetyPragma.java index 8f580c1b75691c79006c5eb0a88923f25822cbed..71dd744e8fc1a3ca12cb34ca2b4c40ae6f230d85 100644 --- a/src/main/java/ui/avatarbd/AvatarBDSafetyPragma.java +++ b/src/main/java/ui/avatarbd/AvatarBDSafetyPragma.java @@ -160,7 +160,7 @@ public class AvatarBDSafetyPragma extends TGCScalableWithoutInternalComponent { Color c = g.getColor(); int desiredWidth = minWidth; - desiredWidth = Math.max(desiredWidth, 2*g.getFontMetrics().stringWidth("Safety Pragma") + marginX+ textX); + desiredWidth = Math.max(desiredWidth, 2*g.getFontMetrics().stringWidth("Safety Pragmas") + marginX+ textX); for(int i=0; i< values.length; i++) { desiredWidth = Math.max(desiredWidth, g.getFontMetrics().stringWidth(values[i]) + marginX+textX); @@ -199,7 +199,7 @@ public class AvatarBDSafetyPragma extends TGCScalableWithoutInternalComponent { int i = 1; Font heading = new Font("heading", Font.BOLD, 14); g.setFont(heading); - g.drawString("Safety Pragma", x+textX, y+textY + currentFontSize); + g.drawString("Safety Pragmas", x+textX, y+textY + currentFontSize); g.setFont(fold); for (String s: properties){ g.drawString(s, x + textX, y + textY + (i+1)* currentFontSize); @@ -258,7 +258,7 @@ public class AvatarBDSafetyPragma extends TGCScalableWithoutInternalComponent { public boolean editOndoubleClick(JFrame frame) { String oldValue = value; - JDialogSafetyPragma jdn = new JDialogSafetyPragma(frame, "Setting the safety pragma", value); + JDialogSafetyPragma jdn = new JDialogSafetyPragma(frame, "Setting the safety pragmas", value); //jdn.setLocation(200, 150); GraphicLib.centerOnParent(jdn); AvatarBDPanel abdp = (AvatarBDPanel) tdp;