NtSecureConnectPort

INT64 __fastcall NtSecureConnectPort(
        VOID **PortHandle,
        _UNICODE_STRING *PortName,
        _SECURITY_QUALITY_OF_SERVICE *SecurityQos,
        _PORT_VIEW *ClientView,
        VOID *RequiredServerSid,
        _REMOTE_PORT_VIEW *ServerView,
        UINT64 *MaxMessageLength,
        VOID *ConnectionInformation,
        UINT64 *ConnectionInformationLength){
  PVOID *v11; 
  _ETHREAD *CurrentThread; 
  INT8 v13; 
  __int64 v14; 
  __int64 v15; 
  __int64 v16; 
  __int64 v17; 
  __int64 v18; 
  __int64 v19; 
  __int64 v20; 
  __int64 v21; 
  int v22; 
  PADAPTER_OBJECT v23; 
  int v24; 
  int v25; 
  _KALPC_MESSAGE *v26; 
  unsigned int v27; 
  int v28; 
  unsigned int v29; 
  INT8 RequestorMode; 
  NTSTATUS v32; 
  UINT CapturedLength; 
  PVOID CapturedSid; 
  volatile void *Address; 
  PADAPTER_OBJECT DmaAdapter; 
  ULONG_PTR v37; 
  HANDLE Handle; 
  _UNICODE_STRING *ConnectionPortName; 
  ULONG_PTR BugCheckParameter2; 
  VOID **v41; 
  __m128i v42; 
  __int128 v43; 
  __int128 v44; 
  UINT64 *v45; 
  UINT64 *v46; 
  _REMOTE_PORT_VIEW v47; 
  __int128 v48; 
  __int128 v49; 
  __int64 v50; 
  _ALPC_DISPATCH_CONTEXT result; 
  __int64 v52; 
  int v53; 
  ConnectionPortName = PortName;
  v41 = PortHandle;
  v46 = MaxMessageLength;
  Address = ConnectionInformation;
  v45 = ConnectionInformationLength;
  DmaAdapter = 0i64;
  memset((INT64)&result, 0i64);
  Handle = 0i64;
  CapturedSid = 0i64;
  BugCheckParameter2 = 0i64;
  v48 = 0i64;
  v49 = 0i64;
  v50 = 0i64;
  CapturedLength = 0;
  v52 = 0i64;
  v53 = 0;
  v37 = 0i64;
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  --*((_WORD *)CurrentThread + 242);
  memset(&v47, 0, sizeof(v47));
  v42 = 0i64;
  v43 = 0i64;
  v44 = 0i64;
  v13 = *((_BYTE *)KeGetCurrentThread() + 562);
  RequestorMode = v13;
  if( !v13 )
  {
    if( ConnectionInformationLength )
      CapturedLength = *(_DWORD *)ConnectionInformationLength;
    if( ClientView )
    {
      v42 = *(__m128i *)&ClientView->Length;
      v43 = *(_OWORD *)&ClientView->SectionOffset;
      v44 = *(_OWORD *)&ClientView->ViewBase;
    }
    if( SecurityQos )
    {
      v52 = *(_QWORD *)&SecurityQos->Length;
      v53 = *(_DWORD *)&SecurityQos->ContextTrackingMode;
    }
    CapturedSid = RequiredServerSid;
    goto LABEL_39;
  }
  v14 = (__int64)v41;
  if( (unsigned __int64)v41 >= 0x7FFFFFFF0000i64 )
    v14 = 0x7FFFFFFF0000i64;
  *(_QWORD *)v14 = *(_QWORD *)v14;
  if( ConnectionInformationLength )
  {
    v15 = (__int64)ConnectionInformationLength;
    if( (unsigned __int64)ConnectionInformationLength >= 0x7FFFFFFF0000i64 )
      v15 = 0x7FFFFFFF0000i64;
    CapturedLength = *(_DWORD *)v15;
    ProbeForWrite((VOID *)Address, CapturedLength, 1ui64);
    v13 = RequestorMode;
  }
  if( ClientView )
  {
    v16 = (__int64)ClientView;
    if( (unsigned __int64)ClientView >= 0x7FFFFFFF0000i64 )
      v16 = 0x7FFFFFFF0000i64;
    v42 = *(__m128i *)v16;
    v43 = *(_OWORD *)(v16 + 16);
    v44 = *(_OWORD *)(v16 + 32);
    if( _mm_cvtsi128_si32(v42) != 48 )
    {
      v32 = -1073741811;
      goto LABEL_61;
    }
    if( ((unsigned __int8)ClientView & 3) != 0 )
      goto LABEL_37;
    v17 = (__int64)ClientView;
    if( (unsigned __int64)ClientView >= 0x7FFFFFFF0000i64 )
      v17 = 0x7FFFFFFF0000i64;
    *(_BYTE *)v17 = *(_BYTE *)v17;
    *(_BYTE *)(v17 + 47) = *(_BYTE *)(v17 + 47);
  }
  if( !ServerView )
    goto LABEL_24;
  v18 = (__int64)ServerView;
  if( (unsigned __int64)ServerView >= 0x7FFFFFFF0000i64 )
    v18 = 0x7FFFFFFF0000i64;
  if( *(_DWORD *)v18 != 24 )
  {
    v32 = -1073741811;
    goto LABEL_61;
  }
  if( ((unsigned __int8)ServerView & 3) != 0 )
LABEL_37:
    ExRaiseDatatypeMisalignment();
  v19 = (__int64)ServerView;
  if( (unsigned __int64)ServerView >= 0x7FFFFFFF0000i64 )
    v19 = 0x7FFFFFFF0000i64;
  *(_BYTE *)v19 = *(_BYTE *)v19;
  *(_BYTE *)(v19 + 23) = *(_BYTE *)(v19 + 23);
LABEL_24:
  if( MaxMessageLength )
  {
    v20 = (__int64)MaxMessageLength;
    if( (unsigned __int64)MaxMessageLength >= 0x7FFFFFFF0000i64 )
      v20 = 0x7FFFFFFF0000i64;
    *(_DWORD *)v20 = *(_DWORD *)v20;
  }
  if( SecurityQos )
  {
    v21 = (__int64)SecurityQos;
    if( (unsigned __int64)SecurityQos >= 0x7FFFFFFF0000i64 )
      v21 = 0x7FFFFFFF0000i64;
    v52 = *(_QWORD *)v21;
    v53 = *(_DWORD *)(v21 + 8);
  }
  CapturedSid = RequiredServerSid;
  if( !RequiredServerSid || (v32 = SeCaptureSid(RequiredServerSid, v13, 0, v11), v32 >= 0) )
  {
LABEL_39:
    v22 = AlpcpCreateClientPort(
            &Handle,
            (_ALPC_PORT **)&DmaAdapter,
            0i64,
            ConnectionPortName,
            0i64,
            0i64,
            0i64,
            0i64,
            CapturedSid,
            (_SECURITY_QUALITY_OF_SERVICE *)((unsigned __int64)&v52 & -(__int64)(SecurityQos != 0i64)),
            1u);
    v32 = v22;
    if( CapturedSid != RequiredServerSid )
      SeReleaseSid(CapturedSid, RequestorMode, 1u);
    if( v22 >= 0 )
    {
      LOWORD(v48) = CapturedLength;
      ConnectionPortName = (_UNICODE_STRING *)((unsigned __int64)&v42 & -(__int64)(ClientView != 0i64));
      v23 = DmaAdapter;
      AlpcpFormatConnectionRequest((_KEVENT *)&BugCheckParameter2, NotificationEvent, (UINT8)DmaAdapter);
      v25 = v24;
      v32 = v24;
      if( v24 >= 0 )
      {
        v26 = (_KALPC_MESSAGE *)BugCheckParameter2;
        v27 = *(_DWORD *)(BugCheckParameter2 + 264);
        if( AlpcpLogEnabled )
          AlpcpLogConnectRequest(BugCheckParameter2);
        result.PortObject = (_ALPC_PORT *)v23;
        result.Message = v26;
        result.Flags = 0x20000;
        v28 = AlpcpDispatchConnectionRequest((INT64)&result);
        v25 = v28;
        v32 = v28;
        if( v28 < 0 )
        {
          if( AlpcpLogEnabled )
            AlpcpLogConnectFail(v27, (unsigned int)v28);
          AlpcpUnlockMessage((ULONG_PTR)v26);
        }
        else
        {
          v29 = AlpcpReceiveLegacyConnectionReply(
                  &result,
                  (VOID *)Address,
                  &CapturedLength,
                  (_KALPC_VIEW *)v37,
                  (_PORT_VIEW *)ConnectionPortName,
                  (_REMOTE_PORT_VIEW *)((unsigned __int64)&v47 & -(__int64)(ServerView != 0i64)));
          v25 = v29;
          v32 = v29;
          if( v29 )
          {
            if( AlpcpLogEnabled )
              AlpcpLogConnectFail(v27, v29);
          }
          else
          {
            if( AlpcpLogEnabled )
              AlpcpLogConnectSuccess(v27);
            *v41 = Handle;
            if( ClientView )
            {
              *(__m128i *)&ClientView->Length = v42;
              *(_OWORD *)&ClientView->SectionOffset = v43;
              *(_OWORD *)&ClientView->ViewBase = v44;
            }
            if( ServerView )
              *ServerView = v47;
            if( v45 )
              *(_DWORD *)v45 = CapturedLength;
            if( v46 )
              *(_DWORD *)v46 = *(_DWORD *)&v23[17].Version;
          }
        }
      }
      HalPutDmaAdapter(v23);
      if( v37 )
        AlpcpDereferenceBlobEx((PVOID)v37, 1i64);
      if( v25 )
        NtClose(Handle);
    }
  }
LABEL_61:
  KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
  return(unsigned int)v32;
}

Referenced by:

IopConnectLinkTrackingPort
NtConnectPort