Standard prioritiesval P_HIGH = 100;
val P_HIGH = 100;val P_NORMAL = 1000;
val P_NORMAL = 1000;val P_LOW = 10000;
val P_LOW = 10000;Standard declarationsMSGcolset MSG = string;DEVIDcolset DEVID = string;DEVKEYcolset DEVKEY = string;DEVPAIRDEVIDDEVKEYcolset DEVPAIR = product DEVID * DEVKEY;DHKEYcolset DHKEY = string;ENCMSGDHKEYMSGcolset ENCMSG = product DHKEY * MSG;SERVENCMSGDEVKEYENCMSGcolset SERVENCMSG = product DEVKEY * ENCMSG;SERVENCMSG2DEVKEYMSGcolset SERVENCMSG2 = product DEVKEY * MSG;SERVENCMSG3DEVKEYMSGENCMSGcolset SERVENCMSG3 = product DEVKEY * MSG * ENCMSG;SERVMSGDEVIDSERVENCMSGcolset SERVMSG = product DEVID * SERVENCMSG;DEVIDxENCMSGDEVIDENCMSGcolset DEVIDxENCMSG = product DEVID * ENCMSG;UNITcolset UNIT = unit;BOOLINTINTINFcolset INTINF = intinf;STRINGTIMEcolset TIME = time;REALcolset REAL = real;DEVIDdevidvar devid: DEVID;MSGmsgmsg1msg2var msg,msg1,msg2: MSG;DHKEYdhkeydhkeyI1dhkeyI2var dhkey,dhkeyI1,dhkeyI2 : DHKEY;DEVKEYdevkeyvar devkey : DEVKEY;DEVPAIRdevpairvar devpair : DEVPAIR;ENCMSGencmsgvar encmsg : ENCMSG;ENCMSGencmsg_from_serverencmsg_from_devencmsg_from_gwvar encmsg_from_server,encmsg_from_dev,encmsg_from_gw: ENCMSG;BOOLbooleanvar boolean : BOOL;SERVENCMSGservencmsgvar servencmsg : SERVENCMSG;SERVENCMSG2servencmsg2var servencmsg2 : SERVENCMSG2;SERVENCMSG3servencmsg3var servencmsg3 : SERVENCMSG3;SERVMSGservmsgvar servmsg : SERVMSG;STRINGstrvar str : STRING;M1ENCMSGDEV_KmDEVKEY1`"DEVKEY0001"M2SERVMSGMITM_not_checkedMSG1`"DETECT_MITM_NONCE"M3DEVIDxENCMSGReceived_DevIDDEVID1`"IOTDEV0001"DevIDDEVID1`"IOTDEV0001"M4ENCMSGMITM_TEST_RESULTSTRINGc6MSG1`"GW_IS_REAL"M5SERVENCMSG3M0_DEVDHKEYM0_GWDHKEYMITM_MESSAGEMSG1`"DETECT_MITM"c10DEVPAIR1`("IOTDEV0001","DEVKEY0001")++
2`("IOTDEV0002","DEVKEY0002")FakeDeviceSTRINGGW_IS_FAKEMSGGW_IS_REALMSGGW_VALIDATION_EXPECTED_MESSAGEMSG1`"GW_IS_REAL"Intruder DH Key
DistributionDHKEY1`"mitm_dhkey"EvesdropMSGDev_KeyDHKEYGW_KeyDHKEYM1_IENCMSGMITM Perform DHinput (dhkey)
output (dhkeyI1,dhkeyI2)
action
let
val res1 = "dhkey01"
val res2 = "dhkey01"
in
(res1,res2)
endIoT_DeviceIoT_GWIoT_ServerDecryptinput (dhkey,encmsg)
output (msg)
action
let
val res = #2(encmsg)
in
(res)
endEncryptinput (dhkey,msg)
output (encmsg)
action
let
val result = (dhkey,msg)
in
(result)
enddhkeyI2dhkeyI1dhkeydhkeymsgmsgencmsgdhkeyencmsgdhkeyI2dhkeyI1b5SERVMSGrecvMsgSERVENCMSG3Dev_Km1DEVKEYb4SERVENCMSGStart_MITM_CHECK_1UNITDEV_KsDHKEYb1ENCMSGb2MSGb3ENCMSGM1_IENCMSGM0_DEVDHKEYGW_VALIDATION_EXPECTED_MESSAGEMSG1`"GW_IS_REAL"M5SERVENCMSG3DevIDDEVID1`"IOTDEV0001"DEV_KmDEVKEY1`"DEVKEY0001"M2SERVMSGMITM_MESSAGEMSG1`"DETECT_MITM"GW_IS_REALMSGGW_IS_FAKEMSGConcatDevIDandEncryptedMsginput (devid,servencmsg)
output (servmsg)
action
let
val res = (devid,servencmsg)
in
(res)
endSend
ServerRecv
ServerEncrypt_Kminput (devkey,encmsg)
output (servencmsg)
action
let
val res = (devkey, encmsg)
in
(res)
endDecrypt[dhkey= #1(encmsg)]input (dhkey,encmsg)
output (msg)
action
let
val res = #2(encmsg)
in
(res)
endget DH DEVRecvCheck_MITM_msg[msg2=substring(msg,0,11)]falseCheck[devkey<> #1(servencmsg3) orelse msg<> #2(servencmsg3)]input (servencmsg3)
output (msg2)
action
let
val res = #2(servencmsg3)
in
(res)
endDecryptServEncMsg2[devkey= #1(servencmsg3)
andalso msg2= #2(servencmsg3)]input (servencmsg3)
output (msg)
action
let
val res = #2(servencmsg3)
in
(res)
endCheck_MITM_failcase[msg2<>substring(msg1,0,11)]input (msg1,msg2)
output (msg)
action
let
val res = (msg1)
in
(res)
enddhkey1`()dhkeyservmsgdevkeyservencmsg31`()servencmsg3msgencmsgmsgdevkeyencmsgencmsgservencmsgdevkeyservencmsg3encmsgservencmsgservmsgencmsgdhkeymsgservencmsg3deviddevkeyservmsgmsg2msg2msgmsg2msg1msg2msga4ENCMSGa5ENCMSGa2ENCMSGGW_KsDHKEYa1ENCMSGM1ENCMSGReceived_DevIDDEVID1`"IOTDEV0001"MITM_not_checkedMSG1`"DETECT_MITM"M3DEVIDxENCMSGM4ENCMSGM0_GWDHKEYMITM_TEST_RESULTSTRINGget DH GWSendCompare_MITM_Test_Msgsinput (encmsg,encmsg_from_server)
output (str)
action
let
val orig_key = #1(encmsg)
val orig_msg = #2(encmsg)
val recv_key = #1(encmsg_from_server)
val recv_msg = #2(encmsg_from_server)
in
if orig_key=recv_key andalso orig_msg=recv_msg
then "PASSED"
else "FAILED"
endRecvFromServerSetupSSL_with_ServerEncryptinput (dhkey,msg)
output (encmsg)
action
let
val result = (dhkey,msg)
in
(result)
endencmsgencmsgencmsgencmsgdhkeydhkeyencmsg_from_serverencmsgencmsgdevid(devid,encmsg)encmsgdhkeystrmsgencmsgencmsgc2ENCMSGc12DEVPAIRc11DEVKEYc8DEVIDc9SERVENCMSGc1SERVMSGc4ENCMSGc7SERVENCMSG3c5DEVPAIRc3DEVIDc10DEVPAIR1`("IOTDEV0001","DEVKEY0001")++
2`("IOTDEV0002","DEVKEY0002")M5SERVENCMSG3M3DEVIDxENCMSGM4ENCMSGc6MSG1`"GW_IS_REAL"FakeDeviceSTRINGM2SERVMSGdh_enc_msg_GWENCMSGdh_enc_msg_devENCMSGgw_encmsgENCMSGCopiedFakeDeviceSTRINGServEncMSG_Decryptinput (servencmsg, devkey)
output (encmsg,str)
action
let
val res = #2(servencmsg)
in
if devkey= #1(servencmsg) then (res,"0")
else (("0","0"),"WRONG DEVKEY")
endGW_RequestMatch[devid= #1(devpair)]findTheKey[devid= #1(devpair)]input (devpair)
output (devkey)
action
let
val res_devkey= #2(devpair)
in
(res_devkey)
endDisassemble SERVMSGinput (servmsg)
output (devid, servencmsg)
action
let
val res_devid = #1(servmsg)
val res_servencmsg = #2(servmsg)
in
(res_devid, res_servencmsg)
endRecvFromDevWait_for_SSLConnectionSend2GW_DevMsgSendRequestMatch_MsgEncrypt_K_DevIDinput (devpair,msg,encmsg,msg1)
output (servencmsg3)
action
let
val res_servencmsg3= (#2(devpair),msg,encmsg)
in
if msg1="MITM_DETECTED" then (#2(devpair),msg1,encmsg)
else (res_servencmsg3)
endcompare_dh_enc_msgsinput (encmsg_from_dev, encmsg_from_gw)
output (msg1)
action
let
val res = "MITM_DETECTED"
in
if encmsg_from_dev<> encmsg_from_gw then (res)
else ("NO_MITM_DETECTED")
enddevidservencmsg3devpairencmsgdevpairdevpairencmsgdevkeyservmsgencmsgdevkeyservencmsgdevidservencmsg3devidencmsgservmsgdevpairservencmsgdeviddevpairservencmsg3(devid,encmsg)encmsgmsgif str<>"0" then str else "DEVICE AUTHENTICATED"servmsgencmsgencmsgencmsg_from_devencmsg_from_gwmsg1encmsgencmsgmsg1msg1