Commit 119e0633 authored by garciay's avatar garciay
Browse files

STF507 week#9:

.  Implement additional TPs
.  Review of the PICS, TSS&TPs & PIXITs documents
.  Start validation using simulation
.      Minor bug fixed in TA
TODO: Logs should be commented later
parent bf42b34b
......@@ -72,7 +72,7 @@ public class PcapMultiplexer implements Runnable {
}
device = alldevs.get(ifaceIndex);
// System.out.println("Listening: " + device.getName());
System.out.println("Listening: " + device.getName());
}
/**
......@@ -84,7 +84,7 @@ public class PcapMultiplexer implements Runnable {
}
public synchronized void register(Layer client, byte[] macAddress, short frameType) {
// System.out.println(">>>PcapMultiplexer.registering: " + frameType);
System.out.println(">>>PcapMultiplexer.registering: " + frameType);
if(clientsToMacs.isEmpty()) {
// Open interface
......@@ -103,7 +103,7 @@ public class PcapMultiplexer implements Runnable {
filter = "";
}
else {
// System.out.println("Another Client !");
System.out.println("Another Client !");
filter = filter + " and ";
}
......@@ -114,7 +114,7 @@ public class PcapMultiplexer implements Runnable {
}
filter = filter + "not ether src " + strMacAddress;
// System.out.println("New filter: " + filter);
System.out.println("New filter: " + filter);
// Apply filter
PcapBpfProgram bpfFilter = new PcapBpfProgram();
......@@ -122,7 +122,7 @@ public class PcapMultiplexer implements Runnable {
int netmask = 0;
int r = pcap.compile(bpfFilter, filter, optimize, netmask);
if (r != Pcap.OK) {
// System.out.println("Filter error: " + pcap.getErr());
System.out.println("Filter error: " + pcap.getErr());
}
pcap.setFilter(bpfFilter);
......
......@@ -24,87 +24,87 @@ public class EthernetLayer extends Layer {
/**
* Well-known Ethernet broadcast address
*/
public static byte[] MAC_BROADCAST = new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
public static byte[] MAC_BROADCAST = new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
/**
* Parameter name for Link-Layer destination
*/
public static final String LINK_LAYER_DESTINATION = "LinkLayerDestination";
/**
/**
* Constructor
* @param management Layer management instance
* @param lowerStack Lower protocol stack
*/
public EthernetLayer(IManagementLayers management, Stack<String> lowerStack) {
super(management, lowerStack);
public EthernetLayer(IManagementLayers management, Stack<String> lowerStack) {
super(management, lowerStack);
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#register(org.etsi.its.adapter.layers.Layer)
*/
@Override
public void register(Layer upperLayer) {
if(registeredUpperLayer == null) {
super.register(upperLayer);
try {
Method getEthernetType = registeredUpperLayer.getClass().getMethod("getEthernetType", (Class<?>[])null);
if (getEthernetType != null) {
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#register(org.etsi.its.adapter.layers.Layer)
*/
@Override
public void register(Layer upperLayer) {
if(registeredUpperLayer == null) {
super.register(upperLayer);
try {
Method getEthernetType = registeredUpperLayer.getClass().getMethod("getEthernetType", (Class<?>[])null);
if (getEthernetType != null) {
upperLayerFrameType = (Short) getEthernetType.invoke(registeredUpperLayer, (Object[]) null);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
localMacAddress = management.getLinkLayerAddress();
PcapMultiplexer.getInstance().register(this, localMacAddress, upperLayerFrameType);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
localMacAddress = management.getLinkLayerAddress();
PcapMultiplexer.getInstance().register(this, localMacAddress, upperLayerFrameType);
}
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#send(byte[], java.util.Map)
*/
@Override
public boolean send(byte[] message, Map<String, Object> params) {
byte[] dst = (byte[])params.get(LINK_LAYER_DESTINATION);
if(dst == null) {
dst = MAC_BROADCAST;
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#send(byte[], java.util.Map)
*/
@Override
public boolean send(byte[] message, Map<String, Object> params) {
byte[] dst = (byte[])params.get(LINK_LAYER_DESTINATION);
if(dst == null) {
dst = MAC_BROADCAST;
}
byte[] sent = PcapMultiplexer.getInstance().sendPacket(this, dst, message);
byte[] sent = PcapMultiplexer.getInstance().sendPacket(this, dst, message);
return super.send(sent, params);
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#unregister(org.etsi.its.adapter.layers.Layer)
*/
@Override
public void unregister(Layer upperLayer) {
PcapMultiplexer.getInstance().unregister(this);
return super.send(sent, params);
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.layers.Layer#unregister(org.etsi.its.adapter.layers.Layer)
*/
@Override
public void unregister(Layer upperLayer) {
PcapMultiplexer.getInstance().unregister(this);
}
/**
* Local Ethernet address
*/
private byte[] localMacAddress;
/**
* Upper layer's frame type
*/
private short upperLayerFrameType;
/**
* Local Ethernet address
*/
private byte[] localMacAddress;
/**
* Upper layer's frame type
*/
private short upperLayerFrameType;
}
......@@ -643,6 +643,12 @@ public class GnLayer extends Layer implements Runnable, IEthernetSpecific {
long curtime = System.currentTimeMillis();
byte[] generationTime = ByteHelper.longToByteArray((long)(curtime - 1072915200000L) * 1000L, Long.SIZE / Byte.SIZE); // In microseconds
System.out.println("GnLayer.createSecuredMessage: generationTime=" + ByteHelper.byteArrayToString(generationTime));
byte[] threeDLocation = ByteHelper.concat(
management.getLatitude(),
management.getLongitude(),
new byte[] { 0x00, 0x00 }
);
System.out.println("GnLayer.createSecuredMessage: threeDLocation=" + ByteHelper.byteArrayToString(threeDLocation));
// Build the payload to be signed
byte[] headersField = ByteHelper.concat(
ByteHelper.concat( // SecuredMessage HeaderFields
......@@ -654,7 +660,11 @@ public class GnLayer extends Layer implements Runnable, IEthernetSpecific {
new byte[] {
(byte)0x00, // generationTime
},
generationTime // Time64 value
generationTime, // Time64 value
new byte[] {
(byte)0x03, // generationLocation
},
threeDLocation // ThreeDLocation
)
);
if ((commonHdr[0] & 0xF0) == 0x01) { // Next header = Btp-A
......
......@@ -98,17 +98,17 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
long currentTime = System.currentTimeMillis();
byte[] tst = ByteHelper.intToByteArray((int)(((currentTime - 1072915200000L) % modulo) - 3000), 4);
System.arraycopy(tst, 0, beaconHeader, 20, 4);
// TODO Uncomment to get secured beacon if (!management.isSecuredModeSet()) { // Secure mode disabled
/* TODO Uncomment to get secured beacon*/if (!management.isSecuredModeSet()) { // Secure mode disabled
send(beaconHeader, params);
/* TODO Uncomment to get secured beacon
/* TODO Uncomment to get secured beacon*/
} else { // Send a secured beacon
byte[] securedBeaconHeader = buildSecuredBeacon(beaconHeader, tst, currentTime, false);
// triggerTime = currentTime;
// Send the secured beacon
System.out.println("GnPort: Call send " + ByteHelper.byteArrayToString(securedBeaconHeader));
//System.out.println("GnPort: Call send " + ByteHelper.byteArrayToString(securedBeaconHeader));
send(securedBeaconHeader, params);
}
TODO Uncomment to get secured beacon*/
/* TODO Uncomment to get secured beacon*/
try {
Thread.sleep(management.getGnBeaconInterval());
} catch (InterruptedException e) {
......@@ -119,7 +119,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
}
private byte[] buildSecuredBeacon(final byte[] p_beacon, byte[] p_tst, final long p_currentTime, final boolean p_sendDigest) {
System.out.println("GnPort.buildSecuredBeacon (1): " + ByteHelper.byteArrayToString(p_beacon));
//System.out.println("GnPort.buildSecuredBeacon (1): " + ByteHelper.byteArrayToString(p_beacon));
byte[] basicHeader = ByteHelper.extract(p_beacon, 0, 4);
basicHeader[0] &= 0xFE;
......@@ -131,10 +131,10 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
p_tst, // Update the timestamp field
ByteHelper.extract(p_beacon, 20, p_beacon.length - 4 - 20) // Add remaining bytes
);
System.out.println("GnPort.buildSecuredBeacon (2): " + ByteHelper.byteArrayToString(beacon));
//System.out.println("GnPort.buildSecuredBeacon (2): " + ByteHelper.byteArrayToString(beacon));
// Prepare the message to be signed
byte[] toBeSignedData = buildToBeSignedData(beacon, p_currentTime, p_sendDigest);
System.out.println("GnPort.buildSecuredBeacon: toBeSignedData " + ByteHelper.byteArrayToString(toBeSignedData));
//System.out.println("GnPort.buildSecuredBeacon: toBeSignedData " + ByteHelper.byteArrayToString(toBeSignedData));
// Sign the message
byte[] securedBeaconHeader = signSecuredMessage(toBeSignedData);
......@@ -143,7 +143,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
}
private byte[] buildToBeSignedData(final byte[] p_beacon, final long p_currentTime, final boolean p_sendDigest) {
System.out.println("GnPort.buildToBeSignedData: " + ByteHelper.byteArrayToString(p_beacon));
//System.out.println("GnPort.buildToBeSignedData: " + ByteHelper.byteArrayToString(p_beacon));
// Build the SignerInfo field
byte[] signerInfo = null;
......@@ -167,7 +167,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
// Build the generation time value
byte[] generationTime = ByteHelper.longToByteArray((long)(p_currentTime - 1072915200000L) * 1000L, Long.SIZE / Byte.SIZE); // In microseconds
System.out.println("GnPort.buildToBeSignedData: generationTime=" + ByteHelper.byteArrayToString(generationTime));
//System.out.println("GnPort.buildToBeSignedData: generationTime=" + ByteHelper.byteArrayToString(generationTime));
byte[] headersField = ByteHelper.concat(
ByteHelper.concat( // SecuredMessage HeaderFields
signerInfo, // signerInfo
......@@ -203,7 +203,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
b
);
byte[] headersFieldLength = SecurityHelper.getInstance().size2tls(headersField.length);
System.out.println("GnPort.buildToBeSignedData: headersField=" + ByteHelper.byteArrayToString(headersField));
//System.out.println("GnPort.buildToBeSignedData: headersField=" + ByteHelper.byteArrayToString(headersField));
byte[] toBeSignedData = ByteHelper.concat(
new byte[] { // SecuredMessage version
(byte)0x02 // version
......@@ -218,23 +218,23 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
new byte[] { (byte)0x43 }, // Signature length
new byte[] { (byte)0x01 } // Signature
);
System.out.println("GnPort.buildToBeSignedData: toBeSignedData=" + ByteHelper.byteArrayToString(toBeSignedData));
//System.out.println("GnPort.buildToBeSignedData: toBeSignedData=" + ByteHelper.byteArrayToString(toBeSignedData));
return toBeSignedData;
}
private byte[] signSecuredMessage(final byte[] p_toBeSignedData) {
System.out.println("GnPort.signSecuredMessage: toBeSignedData: " + ByteHelper.byteArrayToString(p_toBeSignedData));
//System.out.println("GnPort.signSecuredMessage: toBeSignedData: " + ByteHelper.byteArrayToString(p_toBeSignedData));
// Calculate the hash
byte[] hash = CryptoLib.hashWithSha256(p_toBeSignedData);
System.out.println("GnPort.signSecuredMessage: hash=" + ByteHelper.byteArrayToString(hash));
//System.out.println("GnPort.signSecuredMessage: hash=" + ByteHelper.byteArrayToString(hash));
byte[] securedBeaconHeader = null;
// Signed the hash
byte[] signatureBytes;
try {
signatureBytes = CryptoLib.signWithEcdsaNistp256WithSha256(hash, management.getSigningPrivateKey());
System.out.println("GnPort.signSecuredMessage: signatureBytes=" + ByteHelper.byteArrayToString(signatureBytes));
//System.out.println("GnPort.signSecuredMessage: signatureBytes=" + ByteHelper.byteArrayToString(signatureBytes));
// Add signature
securedBeaconHeader = ByteHelper.concat(
p_toBeSignedData,
......@@ -247,11 +247,11 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
/* boolean result = CryptoLib.verifyWithEcdsaNistp256WithSha256(p_toBeSignedData, signatureBytes, management.getSigningPublicKeyX(), management.getSigningPublicKeyY());
System.out.println("GnPort.signSecuredMessage: Verify signature: pubX" + ByteHelper.byteArrayToString(management.getSigningPublicKeyX()));
System.out.println("GnPort.signSecuredMessage: Verify signature: pubY" + ByteHelper.byteArrayToString(management.getSigningPublicKeyY()));
System.out.println("GnPort.signSecuredMessage: Verify signature: " + new Boolean(result));*/
//System.out.println("GnPort.signSecuredMessage: Verify signature: pubX" + ByteHelper.byteArrayToString(management.getSigningPublicKeyX()));
//System.out.println("GnPort.signSecuredMessage: Verify signature: pubY" + ByteHelper.byteArrayToString(management.getSigningPublicKeyY()));
//System.out.println("GnPort.signSecuredMessage: Verify signature: " + new Boolean(result));*/
System.out.println("<<< GnPort.signSecuredMessage: sendBeacon: " + ByteHelper.byteArrayToString(securedBeaconHeader));
//System.out.println("<<< GnPort.signSecuredMessage: sendBeacon: " + ByteHelper.byteArrayToString(securedBeaconHeader));
return securedBeaconHeader;
} catch (Exception e) {
e.printStackTrace();
......@@ -265,7 +265,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
*/
@Override
public boolean send(byte[] message) {
System.out.println(">>> GnPort.send: " + ByteHelper.byteArrayToString(message));
//System.out.println(">>> GnPort.send: " + ByteHelper.byteArrayToString(message));
HashMap<String, Object> params = new HashMap<String, Object>();
......@@ -277,7 +277,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
}
// ByteHelper.dump("GnPort.send", message);
System.out.println("\n\n");
//System.out.println("\n\n");
return send(message, params);
}
......@@ -286,7 +286,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
*/
@Override
public void receive(byte[] message, Map<String, Object> lowerInfo) {
System.out.println(">>> GnPort.receive: " + ByteHelper.byteArrayToString(message));
//System.out.println(">>> GnPort.receive: " + ByteHelper.byteArrayToString(message));
byte[] basicHdr = new byte[4];
System.arraycopy(message, 0, basicHdr, 0, 4);
......@@ -309,10 +309,10 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
} else if ((basicHdr[0] & 0x0f) == 0x02) { // Secured tag
byte[] payload = SecurityHelper.getInstance().checkSecuredProfileAndExtractPayload(message, basicHdr.length, management.isEnforceSecuredModeSet(), management.getItsAidOther());
if (payload != null) {
// System.out.println("GnPort.receive: payload=" + ByteHelper.byteArrayToString(payload));
//System.out.println("GnPort.receive: payload=" + ByteHelper.byteArrayToString(payload));
byte[] commonHdr = new byte[8];
System.arraycopy(payload, 0, commonHdr, 0, 8);
// System.out.println("GnPort.receive: commonHdr=" + ByteHelper.byteArrayToString(commonHdr));
//System.out.println("GnPort.receive: commonHdr=" + ByteHelper.byteArrayToString(commonHdr));
byte[] htHst = new byte[1];
System.arraycopy(commonHdr, 1, htHst, 0, 1);
headerType = (int)(htHst[0] >> 4);
......@@ -333,7 +333,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
System.err.println("GnPort.receive: Invalid basic header type");
return;
}
System.out.println("GnPort.receive: sopv=" + ByteHelper.byteArrayToString(sopv));
//System.out.println("GnPort.receive: sopv=" + ByteHelper.byteArrayToString(sopv));
byte[] gn = new byte[8];
System.arraycopy(sopv, 0, gn, 0, 8);
......@@ -378,7 +378,7 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
}
private byte[] createSecuredMessage(final byte[] message) {
System.out.println(">>> GnPort.createSecuredMessage: " + ByteHelper.byteArrayToString(message));
//System.out.println(">>> GnPort.createSecuredMessage: " + ByteHelper.byteArrayToString(message));
// Extract and update the basicHeader
byte[] basicHdr = ByteHelper.extract(message, 0, 4);
......@@ -388,11 +388,11 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
// Set nextHeader to secured
basicHdr[0] &= 0xFE;
basicHdr[0] |= 0x02;
System.out.println("GnPort.createSecuredMessage: basicHdr=" + ByteHelper.byteArrayToString(basicHdr));
//System.out.println("GnPort.createSecuredMessage: basicHdr=" + ByteHelper.byteArrayToString(basicHdr));
// Extract and update the basicHeader
byte[] commonHdr = ByteHelper.extract(message, 4, 8);
System.out.println("GnPort.createSecuredMessage: commonHdr=" + ByteHelper.byteArrayToString(commonHdr));
//System.out.println("GnPort.createSecuredMessage: commonHdr=" + ByteHelper.byteArrayToString(commonHdr));
// Extract SecuredMessage payload
byte[] securedPayload = ByteHelper.extract(message, 4, message.length - 4);
......@@ -477,16 +477,16 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
new byte[] { (byte)0x01 }, // Signature
new byte[] { (byte)0x43 } // Signature length
);
System.out.println("GnPort.createSecuredMessage: toBeSignedData=" + ByteHelper.byteArrayToString(toBeSignedData));
//System.out.println("GnPort.createSecuredMessage: toBeSignedData=" + ByteHelper.byteArrayToString(toBeSignedData));
byte[] toBeSent = null;
try {
// Calculate the hash
byte[] hash = CryptoLib.hashWithSha256(toBeSignedData);
System.out.println("GnPort.createSecuredMessage: hash=" + ByteHelper.byteArrayToString(hash));
//System.out.println("GnPort.createSecuredMessage: hash=" + ByteHelper.byteArrayToString(hash));
// Signed the hash
byte[] signatureBytes = CryptoLib.signWithEcdsaNistp256WithSha256(hash, management.getSigningPrivateKey());
System.out.println("GnPort.createSecuredMessage: signatureBytes=" + ByteHelper.byteArrayToString(signatureBytes));
//System.out.println("GnPort.createSecuredMessage: signatureBytes=" + ByteHelper.byteArrayToString(signatureBytes));
// Add signature
toBeSent = ByteHelper.concat(
basicHdr,
......@@ -498,8 +498,8 @@ public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific
e.printStackTrace();
}
System.out.println("GnPort.createSecuredMessage: toBeSent=" + ByteHelper.byteArrayToString(toBeSent));
System.out.println("\n\n");
//System.out.println("GnPort.createSecuredMessage: toBeSent=" + ByteHelper.byteArrayToString(toBeSent));
//System.out.println("\n\n");
return toBeSent;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment