-- @path ANOMM=/InterComponentCommunicationModeling/MetaModels/ANO.ecore
-- @path ICCMM=/InterComponentCommunicationModeling/MetaModels/ICCMM.ecore

module PrivilegeEscalationInterApp;
create OUT : ANOMM from IN : ICCMM;


helper context OclAny def:getcontainerOfImplicitTargetComponent() :ICCMM!ImplicitCommunication =	
if self.oclIsTypeOf(ICCMM!ImplicitCommunication) then
 self
else
   self.refImmediateComposite().getcontainerOfImplicitTargetComponent()
endif;

helper context ICCMM!ComponentUsedPermission def:getAllAPIcallPermissions() : Sequence(ICCMM!APICallPermissions) =	
self.APICallPermissions.flatten();


helper context ICCMM!SenderApp def:notexistInUsesPermissions (permission_name :String) :Boolean =	
if not self.usesPermissions->select(c | c.Name=permission_name).notEmpty() then
 true
else
 false
endif;


helper context ICCMM!ImplicitTargetComponent def:notexistInComponentEnforcedPermissionsForImplicit (permission_name :String) :Boolean =	
if not self.component_enforcedPermissions->select(c | c.Name=permission_name).notEmpty() then
 true
else
 false
endif;

helper context ICCMM!ExplicitTargetComponent def:notexistInComponentEnforcedPermissionsForExplicit (permission_name :String) :Boolean =	
if self.component_enforcedPermissions->select(c | c.Name=permission_name).notEmpty() then
true
else
 false
endif;


		
helper context ICCMM!ExplicitCommunication def:getPrivilegeEscalationExplicitCommunications :Sequence(String) =	
let ExplicitCommunicationsOfBundle : Sequence(ICCMM!ExplicitCommunication) = self.refImmediateComposite().explicit_communications in
ExplicitCommunicationsOfBundle->iterate(n ; output : Sequence(String) = Sequence{} | 

if not self.sender_app.Name.oclIsUndefined() then
    if not self.receiver_app.Name.oclIsUndefined() then
        if not self.sender_app.Name.toString().endsWith(self.receiver_app.Name.toString()) then
            if self.intent.target_component.component_usedPermissions.notEmpty() then
                if not self.intent.target_component.component_enforcedPermissions.notEmpty() then
                    if not self.sender_app.usesPermissions.notEmpty() then
					    if self.receiver_app.Permission.oclIsUndefined() then
					      output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
					    else
					   	  output->reject(c | c.Name='')
					    endif
	                else
		                if self.intent.target_component.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.sender_app.notexistInUsesPermissions(a.Name))).notEmpty() then
		                 output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
	                    else
		   	             output->reject(c | c.Name='')
			            endif  
	                endif
                else
   	                if not self.sender_app.usesPermissions.notEmpty() then
	                 output->reject(c | c.Name='') 
	                else
	   	                if self.intent.target_component.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.intent.target_component.notexistInComponentEnforcedPermissionsForExplicit(a.Name))).notEmpty() then
						    if self.receiver_app.Permission.oclIsUndefined() then
		                     output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
						    else
						        if self.intent.target_component.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | not a.Name=self.receiver_app.Permission)).notEmpty() then
						          output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
						        else
						            output->reject(c | c.Name='')
								endif
							endif
	                    else
		                    if not self.intent.target_component.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.sender_app.notexistInUsesPermissions(a.Name))).notEmpty() then
		                     output->reject(c | c.Name='')
		                    else
		                    	if self.receiver_app.Permission.oclIsUndefined() then 
		   	                       output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
	                            else
	                            	if self.intent.target_component.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | not a.Name=self.receiver_app.Permission)).notEmpty() then
						                output->including(': This Bundle has Privilege Escalation vulnerability in the explicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Receiver Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
						            else
						            	output->reject(c | c.Name='')
									endif
								endif
						    endif  
	                    endif
                    endif
	            endif
            else
             output->reject(c | c.Name='')
            endif
        else
         output->reject(c | c.Name='')
        endif
    else
      output->reject(c | c.Name='')
     endif
else
output->reject(c | c.Name='')
endif
)->asSet()->asSequence();

helper context ICCMM!ImplicitTargetComponent def:getPrivilegeEscalationImplicitCommunications :Sequence(String) =	
let ImplicitTargetComponentsOfCommunication : Sequence(ICCMM!ImplicitTargetComponent) = self.refImmediateComposite().target_components in
ImplicitTargetComponentsOfCommunication->iterate(n ; output : Sequence(String) = Sequence{} | 

if not self.receiver_app.Name.toString().endsWith(self.refImmediateComposite().refImmediateComposite().sender_app.Name.toString()) then
    if self.component_usedPermissions.notEmpty() then
        if not self.component_enforcedPermissions.notEmpty() then
            if not self.getcontainerOfImplicitTargetComponent().sender_app.usesPermissions.notEmpty() then
			    if self.receiver_app.Permission.oclIsUndefined() then
	               output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
				else
					output->reject(c | c.Name='')
				endif
  	        else
  	            if self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.getcontainerOfImplicitTargetComponent().sender_app.notexistInUsesPermissions(a.Name))).notEmpty() then
				    if self.receiver_app.Permission.oclIsUndefined() then
		              output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					else
						if self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | not a.Name=self.receiver_app.Permission)).notEmpty() then
						    output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					    else
					    	output->reject(c | c.Name='')
						endif
					endif
  	            else
		   	    output->reject(c | c.Name='')
			    endif
	        endif
        else
   	        if not self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.notexistInComponentEnforcedPermissionsForImplicit(a.Name))).notEmpty() then
	          output->reject(c | c.Name='')
	        else
	   	        if not self.getcontainerOfImplicitTargetComponent().sender_app.usesPermissions.notEmpty() then
				    if self.receiver_app.Permission.oclIsUndefined() then 
		   	             output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
  	                else
	                    if self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | not a.Name=self.receiver_app.Permission)).notEmpty() then
						  output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
  	                    else
						   output->reject(c | c.Name='')
					    endif
				    endif
				else
  	                if self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | self.getcontainerOfImplicitTargetComponent().sender_app.notexistInUsesPermissions(a.Name))).notEmpty() then
		             output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
  	                else
		   	            if self.receiver_app.Permission.oclIsUndefined() then 
		   	             output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
  	                    else
	                        if self.component_usedPermissions->select(c | c.getAllAPIcallPermissions()->exists(a | not a.Name=self.receiver_app.Permission)).notEmpty() then
						      output->including(': This Bundle has Privilege Escalation vulnerability in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.refImmediateComposite().SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.refImmediateComposite().refImmediateComposite().sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.Name + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
  	                        else
						      output->reject(c | c.Name='')
					        endif
				        endif
			        endif
	            endif
            endif
	    endif
    else
    output->reject(c | c.Name='')
    endif
else
output->reject(c | c.Name='')
endif
	
)->asSet()->asSequence();

rule ExplicitCommunications2PrivilegeEscalationCommunications
 {
	from
	   s:ICCMM!ExplicitCommunication
	  (not s.intent.target_component.oclIsUndefined() )
	  	--and not s.receiver_app.oclIsUndefined() and not s.sender_app.Name.toString() = s.receiver_app.Name.toString())
	to
	   t:ANOMM!PrivilegeEscalation
	   (             
	 description <-  
	 --'nn'
	 s.getPrivilegeEscalationExplicitCommunications->iterate(up; output: String = '' | output->concat(up)+'\n')     
	 
	   )
	  do {
	t.trace<-Sequence{s};
         }
  }

rule ImplicitCommunications2PrivilegeEscalationCommunications
 {
	from
	   s:ICCMM!ImplicitTargetComponent
	  
	to
	   t:ANOMM!PrivilegeEscalation
	   (
	                 
	 description <- -- 'mm'
	 
	 s.getPrivilegeEscalationImplicitCommunications->iterate(up; output: String = '' | output->concat(up)+'\n')      
	   )
	  do {
	t.trace<-Sequence{s};
         }
  }

