Ejemplo n.º 1
0
        int encodeUserData(byte[] buffer, int bufPos, byte[] payload, int payloadLength, bool encode, byte contextId)
        {
            int encodedDataSetLength = 3; /* presentation-selector */

            /* presentation-data */
            encodedDataSetLength += payloadLength + 1;
            encodedDataSetLength += IsoUtil.BerEncoder_determineLengthSize((uint)payloadLength);

            int fullyEncodedDataLength = encodedDataSetLength;

            fullyEncodedDataLength += IsoUtil.BerEncoder_determineLengthSize((uint)encodedDataSetLength) + 1;

            if (encode)
            {
                /* fully-encoded-data */
                bufPos = IsoUtil.BerEncoder_encodeTL(0x61, (uint)fullyEncodedDataLength, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x30, (uint)encodedDataSetLength, buffer, bufPos);

                /* presentation-selector acse */
                bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
                buffer[bufPos++] = contextId;

                /* presentation-data (= acse payload) */
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa0, (uint)payloadLength, buffer, bufPos);

                return(bufPos);
            }
            else
            {
                int encodedUserDataLength = fullyEncodedDataLength + 1;
                encodedUserDataLength += IsoUtil.BerEncoder_determineLengthSize((uint)fullyEncodedDataLength);

                return(encodedUserDataLength);
            }
        }
Ejemplo n.º 2
0
        int parsePresentationContextDefinitionList(byte[] buffer, int totalLength, int bufPos)
        {
            int endPos = bufPos + totalLength;

            while (bufPos < endPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, endPos);

                switch (tag)
                {
                case 0x30:
                    iecs.logger.LogDebug("PRES: parse pcd entry");
                    bufPos = parsePCDLEntry(buffer, len, bufPos);
                    if (bufPos < 0)
                    {
                        return(-1);
                    }
                    break;

                default:
                    iecs.logger.LogDebug("PRES: unknown tag in presentation-context-definition-list");
                    bufPos += len;
                    break;
                }
            }

            return(bufPos);
        }
Ejemplo n.º 3
0
        public int createUserDataACSE(byte[] buffer, byte[] payload, int payloadLength)
        {
            int bufPos = 0;

            int userDataLengthFieldSize = IsoUtil.BerEncoder_determineLengthSize((uint)payloadLength);

            int pdvListLength = payloadLength + (userDataLengthFieldSize + 4);

            int pdvListLengthFieldSize = IsoUtil.BerEncoder_determineLengthSize((uint)pdvListLength);
            int presentationLength     = pdvListLength + (pdvListLengthFieldSize + 1);

            bufPos = IsoUtil.BerEncoder_encodeTL(0x61, (uint)presentationLength, buffer, bufPos);

            bufPos = IsoUtil.BerEncoder_encodeTL(0x30, (uint)pdvListLength, buffer, bufPos);

            buffer[bufPos++] = (byte)0x02;
            buffer[bufPos++] = (byte)0x01;
            buffer[bufPos++] = (byte)acseContextId; /* ACSE context id */

            bufPos = IsoUtil.BerEncoder_encodeTL(0xa0, (uint)payloadLength, buffer, bufPos);

            /*writeBuffer->partLength = bufPos;
             * writeBuffer->length = bufPos + payloadLength;
             * writeBuffer->nextPart = payload;*/

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);
            return(bufPos + payloadLength);
        }
Ejemplo n.º 4
0
        int parseUserInformation(byte[] buffer, int bufPos, int maxBufPos, ref bool userInfoValid)
        {
            iecs.logger.LogDebug(String.Format("ACSE: parseUserInformation {0} {1}", bufPos, maxBufPos));

            bool hasindirectReference = false;
            bool isDataValid          = false;

            while (bufPos < maxBufPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                switch (tag)
                {
                case 0x02:     /* indirect-reference */
                    nextReference        = IsoUtil.BerDecoder_decodeUint32(buffer, len, bufPos);
                    bufPos              += len;
                    hasindirectReference = true;
                    break;

                case 0xa0:     /* encoding */
                    isDataValid = true;

                    userDataBufferSize  = len;
                    userDataBufferIndex = bufPos;

                    bufPos += len;

                    break;

                default:     /* ignore unknown tag */
                    bufPos += len;
                    break;
                }
            }


            if (!hasindirectReference)
            {
                iecs.logger.LogDebug("ACSE: User data has no indirect reference!");
            }

            if (!isDataValid)
            {
                iecs.logger.LogDebug("ACSE: No valid user data");
            }

            if (hasindirectReference && isDataValid)
            {
                userInfoValid = true;
            }
            else
            {
                userInfoValid = false;
            }

            return(bufPos);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses Iso presentation accept message
        /// </summary>
        /// <param name="buffer">Data buffer</param>
        /// <param name="offset">Index of the first message byte</param>
        /// <param name="length">Length of the buffer from offset to end</param>
        /// <returns>Index to the user data (payload) in the absolute numbering (from the buffer index 0)</returns>
        public int parseAcceptMessage(byte[] buffer, int offset, int length)
        {
            int maxBufPos = offset + length;

            int bufPos = offset;

            byte cpTag = buffer[bufPos++];

            if (cpTag != 0x31)
            {
                iecs.logger.LogDebug("PRES: not a CPA message\n");
                return(0);
            }

            int len = 0;

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

            while (bufPos < maxBufPos)
            {
                byte tag = buffer[bufPos++];

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                if (bufPos < 0)
                {
                    iecs.logger.LogDebug("PRES: wrong parameter length\n");
                    return(0);
                }

                switch (tag)
                {
                case 0xa0:         /* mode-selector */
                    bufPos += len; /* ignore content since only normal mode is allowed */
                    break;

                case 0xa2:     /* normal-mode-parameters */
                    bufPos = parseNormalModeParameters(buffer, len, bufPos);

                    if (bufPos < 0)
                    {
                        iecs.logger.LogDebug("PRES: error parsing normal-mode-parameters");
                        return(0);
                    }

                    break;

                default:
                    iecs.logger.LogDebug(String.Format("PRES: CPA unknown tag {0}", tag));
                    bufPos += len;
                    break;
                }
            }

            return(bufPos);
        }
Ejemplo n.º 6
0
        int parseNormalModeParameters(byte[] buffer, int totalLength, int bufPos)
        {
            int endPos = bufPos + totalLength;

            while (bufPos < endPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, endPos);

                if (bufPos < 0)
                {
                    iecs.logger.LogDebug("PRES: wrong parameter length");
                    return(-1);
                }

                switch (tag)
                {
                case 0x81:     /* calling-presentation-selector */
                    iecs.logger.LogDebug("PRES: calling-pres-sel");
                    bufPos += len;
                    break;

                case 0x82:     /* calling-presentation-selector */
                    iecs.logger.LogDebug("PRES: calling-pres-sel");
                    bufPos += len;
                    break;

                case 0xa4:     /* presentation-context-definition list */
                    iecs.logger.LogDebug("PRES: pcd list");
                    bufPos = parsePresentationContextDefinitionList(buffer, len, bufPos);
                    break;

                case 0x61:     /* user data */
                    iecs.logger.LogDebug("PRES: user-data");

                    bufPos = parseFullyEncodedData(buffer, len, bufPos);

                    if (bufPos < 0)
                    {
                        return(-1);
                    }

                    break;

                default:
                    iecs.logger.LogDebug("PRES: unknown tag in normal-mode");
                    bufPos += len;
                    break;
                }
            }

            return(bufPos);
        }
Ejemplo n.º 7
0
        int encodeAcceptBer(byte[] buffer, int bufPos)
        {
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x30, 7, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x80, 1, buffer, bufPos);
            buffer[bufPos++] = 0;
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x81, 2, buffer, bufPos);
            buffer[bufPos++] = 0x51;
            buffer[bufPos++] = 0x01;

            return(bufPos);
        }
Ejemplo n.º 8
0
        public int createCpaMessage(byte[] buffer, byte[] payload, int payloadLength)
        {
            int contentLength = 0;

            /* mode-selector */
            contentLength += 5;

            int normalModeLength = 0;

            normalModeLength += 6;  /* responding-presentation-selector */

            normalModeLength += 20; /* context-definition-result-list */

            normalModeLength += encodeUserData(null, 0, payload, payloadLength, false, acseContextId);

            contentLength += normalModeLength;

            contentLength += IsoUtil.BerEncoder_determineLengthSize((uint)normalModeLength) + 1;

            int bufPos = 0;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x31, (uint)contentLength, buffer, bufPos);

            /* mode-selector */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0xa0, 3, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x80, 1, buffer, bufPos);
            buffer[bufPos++] = 1; /* 1 = normal-mode */

            /* normal-mode-parameters */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa2, (uint)normalModeLength, buffer, bufPos);

            /* responding-presentation-selector */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x83, 4, buffer, bufPos);
            //memcpy(buffer + bufPos, calledPresentationSelector, 4);
            def_calledPresentationSelector.CopyTo(buffer, bufPos);
            bufPos += 4;

            /* context-definition-result-list */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa5, 18, buffer, bufPos);
            bufPos = encodeAcceptBer(buffer, bufPos); /* accept for acse */
            bufPos = encodeAcceptBer(buffer, bufPos); /* accept for mms */

            /* encode user data */
            bufPos = encodeUserData(buffer, bufPos, payload, payloadLength, true, acseContextId);

            /*writeBuffer->partLength = bufPos;
             * writeBuffer->length = bufPos + payload->length;
             * writeBuffer->nextPart = payload;*/

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);
            return(bufPos + payloadLength);
        }
Ejemplo n.º 9
0
        public AcseIndication parseMessage(byte[] buffer, int offset, int length)
        {
            AcseIndication indication;

            int messageSize = offset + length;

            int bufPos = offset;

            byte messageType = buffer[bufPos++];

            int len = 0;

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, messageSize);

            if (bufPos < 0)
            {
                iecs.logger.LogDebug("ACSE: AcseConnection_parseMessage: invalid ACSE message!");

                return(AcseIndication.ACSE_ERROR);
            }

            switch (messageType)
            {
            case 0x60:
                indication = parseAarqPdu(buffer, bufPos, messageSize);
                break;

            case 0x61:
                indication = parseAarePdu(buffer, bufPos, messageSize);
                break;

            case 0x62:     /* A_RELEASE.request RLRQ-apdu */
                indication = AcseIndication.ACSE_RELEASE_REQUEST;
                break;

            case 0x63:     /* A_RELEASE.response RLRE-apdu */
                indication = AcseIndication.ACSE_RELEASE_RESPONSE;
                break;

            case 0x64:     /* A_ABORT */
                indication = AcseIndication.ACSE_ABORT;
                break;

            default:
                iecs.logger.LogDebug("ACSE: Unknown ACSE message\n");
                indication = AcseIndication.ACSE_ERROR;
                break;
            }

            return(indication);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Parses Iso presentation user data message
        /// </summary>
        /// <param name="buffer">Data buffer</param>
        /// <param name="offset">Index of the first message byte</param>
        /// <param name="length">Length of the buffer from offset to end</param>
        /// <returns>Index to the user data (payload) in the absolute numbering (from the buffer index 0)</returns>
        public int parseUserData(byte[] buffer, int offset, int length)
        {
            int bufPos = offset;

            if (length < 9)
            {
                return(0);
            }

            if (buffer[bufPos++] != 0x61)
            {
                return(0);
            }

            int len = 0;

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, length);

            if (buffer[bufPos++] != 0x30)
            {
                return(0);
            }

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, length);

            if (buffer[bufPos++] != 0x02)
            {
                return(0);
            }

            if (buffer[bufPos++] != 0x01)
            {
                return(0);
            }

            nextContextId = buffer[bufPos++];

            if (buffer[bufPos++] != 0xa0)
            {
                return(0);
            }

            int userDataLength = 0;

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref userDataLength, bufPos, length);

            return(bufPos);
        }
Ejemplo n.º 11
0
        public int createAbortUserMessage(byte[] buffer, byte[] payload, int payloadLength)
        {
            int contentLength = 0;

            contentLength = +encodeUserData(null, 0, payload, payloadLength, false, acseContextId);

            contentLength += IsoUtil.BerEncoder_determineLengthSize((uint)contentLength) + 1;

            int bufPos = 0;

            bufPos = IsoUtil.BerEncoder_encodeTL(0xa0, (uint)contentLength, buffer, bufPos);

            /* encode user data */
            bufPos = encodeUserData(buffer, bufPos, payload, payloadLength, true, acseContextId);

            /*writeBuffer->partLength = bufPos;
             * writeBuffer->length = bufPos + payload->length;
             * writeBuffer->nextPart = payload;*/

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);
            return(bufPos + payloadLength);
        }
Ejemplo n.º 12
0
        AcseIndication parseAarqPdu(byte[] buffer, int bufPos, int maxBufPos)
        {
            iecs.logger.LogDebug("ACSE: parse AARQ PDU\n");

            int  authValuePos     = 0;
            int  authValueLen     = 0;
            int  authMechanismPos = 0;
            int  authMechLen      = 0;
            bool userInfoValid    = false;

            while (bufPos < maxBufPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                if (bufPos < 0)
                {
                    iecs.logger.LogDebug("ACSE: parseAarqPdu: user info invalid!\n");
                    return(AcseIndication.ACSE_ASSOCIATE_FAILED);
                }

                switch (tag)
                {
                case 0xa1:     /* application context name */
                    bufPos += len;
                    break;

                case 0xa2:     /* called AP title */
                    bufPos += len;
                    break;

                case 0xa3:     /* called AE qualifier */
                    bufPos += len;
                    break;

                case 0xa6:     /* calling AP title */
                    bufPos += len;
                    break;

                case 0xa7:     /* calling AE qualifier */
                    bufPos += len;
                    break;

                case 0x8a:     /* sender ACSE requirements */
                    bufPos += len;
                    break;

                case 0x8b:     /* (authentication) mechanism name */
                    authMechLen      = len;
                    authMechanismPos = bufPos;
                    bufPos          += len;
                    break;

                case 0xac:     /* authentication value */
                    bufPos++;
                    bufPos       = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);
                    authValueLen = len;
                    authValuePos = bufPos;
                    bufPos      += len;
                    break;

                case 0xbe:     /* user information */
                    if (buffer[bufPos] != 0x28)
                    {
                        iecs.logger.LogDebug("ACSE: invalid user info\n");
                        bufPos += len;
                    }
                    else
                    {
                        bufPos++;

                        bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                        bufPos = parseUserInformation(buffer, bufPos, bufPos + len, ref userInfoValid);
                    }
                    break;

                default:     /* ignore unknown tag */
                    iecs.logger.LogDebug(String.Format("ACSE: parseAarqPdu: unknown tag 0x{0:X2}", tag));

                    bufPos += len;
                    break;
                }
            }

            if (checkAuthentication(buffer, authMechanismPos, authMechLen, authValuePos, authValueLen) == false)
            {
                iecs.logger.LogDebug("ACSE: parseAarqPdu: check authentication failed!");

                return(AcseIndication.ACSE_ASSOCIATE_FAILED);
            }

            if (userInfoValid == false)
            {
                iecs.logger.LogDebug("ACSE: parseAarqPdu: user info invalid!");

                return(AcseIndication.ACSE_ASSOCIATE_FAILED);
            }

            return(AcseIndication.ACSE_ASSOCIATE);
        }
Ejemplo n.º 13
0
        AcseIndication parseAarePdu(byte[] buffer, int bufPos, int maxBufPos)
        {
            iecs.logger.LogDebug("ACSE: parse AARE PDU");

            bool userInfoValid = false;

            uint result = 99;

            while (bufPos < maxBufPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                switch (tag)
                {
                case 0xa1:     /* application context name */
                    bufPos += len;
                    break;

                case 0xa2:     /* result */
                    bufPos++;

                    bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);
                    result = IsoUtil.BerDecoder_decodeUint32(buffer, len, bufPos);

                    bufPos += len;
                    break;

                case 0xa3:     /* result source diagnostic */
                    bufPos += len;
                    break;

                case 0xbe:     /* user information */
                    if (buffer[bufPos] != 0x28)
                    {
                        iecs.logger.LogDebug("ACSE: invalid user info");
                        bufPos += len;
                    }
                    else
                    {
                        bufPos++;

                        bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                        bufPos = parseUserInformation(buffer, bufPos, bufPos + len, ref userInfoValid);
                    }
                    break;

                default:     /* ignore unknown tag */
                    iecs.logger.LogDebug(String.Format("ACSE: parseAarePdu: unknown tag 0x{0:X2}", tag));

                    bufPos += len;
                    break;
                }
            }

            if (!userInfoValid)
            {
                return(AcseIndication.ACSE_ERROR);
            }

            if (result != 0)
            {
                return(AcseIndication.ACSE_ASSOCIATE_FAILED);
            }

            return(AcseIndication.ACSE_ASSOCIATE);
        }
Ejemplo n.º 14
0
 public void setRemoteApTitle(string ApTitle, int AEQualifier)
 {
     remoteApTitleS    = ApTitle;
     remoteApTitleLen  = IsoUtil.BerEncoder_encodeOIDToBuffer(ApTitle, remoteApTitle, 10);
     remoteAEQualifier = AEQualifier;
 }
Ejemplo n.º 15
0
        public IsoConnectionParameters(StringDictionary stringDictionary)
        {
            init(null);
            int remoteTSelectorVal  = remoteTSelector.value;
            int remoteTSelectorSize = remoteTSelector.value;

            foreach (string key in stringDictionary.Keys)
            {
                switch (key.ToLower())
                {
                case "hostname":
                    hostname = stringDictionary[key];
                    break;

                case "port":
                    int.TryParse(stringDictionary[key], out port);
                    break;

                case "remoteaptitle":
                    remoteApTitleS   = stringDictionary[key];
                    remoteApTitleLen = IsoUtil.BerEncoder_encodeOIDToBuffer(remoteApTitleS, remoteApTitle, 10);
                    break;

                case "remoteaequalifier":
                    int.TryParse(stringDictionary[key], out remoteAEQualifier);
                    break;

                case "remotepselector":
                    uint.TryParse(stringDictionary[key], out remotePSelector);
                    break;

                case "remotesselector":
                    ushort.TryParse(stringDictionary[key], out remoteSSelector);
                    break;

                case "remotetselectorvalue":
                    int.TryParse(stringDictionary[key], out remoteTSelector.value);
                    break;

                case "remotetselectorsize":
                    byte.TryParse(stringDictionary[key], out remoteTSelector.size);
                    break;

                case "localaptitle":
                    localApTitleS   = stringDictionary[key];
                    localApTitleLen = IsoUtil.BerEncoder_encodeOIDToBuffer(localApTitleS, localApTitle, 10);
                    break;

                case "localaequalifier":
                    int.TryParse(stringDictionary[key], out localAEQualifier);
                    break;

                case "localpselector":
                    uint.TryParse(stringDictionary[key], out localPSelector);
                    break;

                case "localsselector":
                    ushort.TryParse(stringDictionary[key], out localSSelector);
                    break;

                case "localtselectorvalue":
                    int.TryParse(stringDictionary[key], out localTSelector.value);
                    break;

                case "localtselectorsize":
                    byte.TryParse(stringDictionary[key], out localTSelector.size);
                    break;

                case "authenticationmechanism":
                    if (acseAuthParameter == null)
                    {
                        acseAuthParameter = new IsoAcse.AcseAuthenticationParameter();
                    }
                    Enum.TryParse <IsoAcse.AcseAuthenticationMechanism>(stringDictionary[key], out acseAuthParameter.mechanism);
                    break;

                case "authenticationpassword":
                    if (acseAuthParameter == null)
                    {
                        acseAuthParameter = new IsoAcse.AcseAuthenticationParameter();
                    }
                    acseAuthParameter.password           = stringDictionary[key];
                    acseAuthParameter.paswordOctetString = Encoding.ASCII.GetBytes(acseAuthParameter.password);
                    acseAuthParameter.passwordLength     = acseAuthParameter.paswordOctetString.Length;
                    break;
                }
            }
        }
Ejemplo n.º 16
0
 public void setLocalApTitle(string ApTitle, int AEQualifier)
 {
     localApTitleS    = ApTitle;
     localApTitleLen  = IsoUtil.BerEncoder_encodeOIDToBuffer(ApTitle, localApTitle, 10);
     localAEQualifier = AEQualifier;
 }
Ejemplo n.º 17
0
        int createConnectPdu(byte[] buffer, byte[] payload, int payloadLength)
        {
            int contentLength = 0;

            /* mode-selector */
            contentLength += 5;

            int normalModeLength = 0;

            /* called- and calling-presentation-selector */
            normalModeLength += 12;

            int pclLength = 35;

            normalModeLength += pclLength;

            normalModeLength += encodeUserData(null, 0, payload, payloadLength, false, acseContextId);

            normalModeLength += 2;

            contentLength += normalModeLength; // + 2;

            contentLength += 1 + IsoUtil.BerEncoder_determineLengthSize((uint)normalModeLength);

            int bufPos = 0;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x31, (uint)contentLength, buffer, bufPos);

            /* mode-selector */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0xa0, 3, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x80, 1, buffer, bufPos);
            buffer[bufPos++] = 1; /* 1 = normal-mode */

            /* normal-mode-parameters */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa2, (uint)normalModeLength, buffer, bufPos);

            /* calling-presentation-selector */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x81, 4, buffer, bufPos);
            buffer[bufPos++] = (byte)((callingPresentationSelector >> 24) & 0xff);
            buffer[bufPos++] = (byte)((callingPresentationSelector >> 16) & 0xff);
            buffer[bufPos++] = (byte)((callingPresentationSelector >> 8) & 0xff);
            buffer[bufPos++] = (byte)(callingPresentationSelector & 0xff);

            /* called-presentation-selector */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x82, 4, buffer, bufPos);
            buffer[bufPos++] = (byte)((calledPresentationSelector >> 24) & 0xff);
            buffer[bufPos++] = (byte)((calledPresentationSelector >> 16) & 0xff);
            buffer[bufPos++] = (byte)((calledPresentationSelector >> 8) & 0xff);
            buffer[bufPos++] = (byte)(calledPresentationSelector & 0xff);

            /* presentation-context-id list */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa4, 35, buffer, bufPos);

            /* acse context list item */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x30, 15, buffer, bufPos);

            bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
            buffer[bufPos++] = 1;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 4, buffer, bufPos);
            //memcpy(buffer + bufPos, asn_id_as_acse, 4);
            asn_id_as_acse.CopyTo(buffer, bufPos);
            bufPos += 4;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x30, 4, buffer, bufPos);
            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 2, buffer, bufPos);
            //memcpy(buffer + bufPos, ber_id, 2);
            ber_id.CopyTo(buffer, bufPos);
            bufPos += 2;

            /* mms context list item */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x30, 16, buffer, bufPos);

            bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
            buffer[bufPos++] = 3;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 5, buffer, bufPos);
            //memcpy(buffer + bufPos, asn_id_mms, 5);
            asn_id_mms.CopyTo(buffer, bufPos);
            bufPos += 5;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x30, 4, buffer, bufPos);
            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 2, buffer, bufPos);
            //memcpy(buffer + bufPos, ber_id, 2);
            ber_id.CopyTo(buffer, bufPos);
            bufPos += 2;

            /* encode user data */
            bufPos = encodeUserData(buffer, bufPos, payload, payloadLength, true, acseContextId);

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);

            /*
             * writeBuffer->partLength = bufPos;
             * writeBuffer->length = bufPos + payload->length;
             * writeBuffer->nextPart = payload;*/
            return(bufPos + payloadLength);
        }
Ejemplo n.º 18
0
        int parsePCDLEntry(byte[] buffer, int totalLength, int bufPos)
        {
            int endPos = bufPos + totalLength;

            int  contextId = -1;
            bool isAcse    = false;
            bool isMms     = false;

            while (bufPos < endPos)
            {
                byte tag = buffer[bufPos++];
                int  len = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, endPos);

                switch (tag)
                {
                case 0x02:     /* presentation-context-identifier */
                    contextId = (int)IsoUtil.BerDecoder_decodeUint32(buffer, len, bufPos);
                    bufPos   += len;
                    break;

                case 0x06:     /* abstract-syntax-name */
                    iecs.logger.LogDebug(String.Format("PRES: abstract-syntax-name with len {0}", len));

                    if (len == 5)
                    {
                        isMms = true;
                        for (int i = 0; i < 5; i++)
                        {
                            if (buffer[bufPos + i] != asn_id_mms[i])
                            {
                                isMms = false;
                            }
                        }
                        //if (memcmp(buffer + bufPos, asn_id_mms, 5) == 0)
                        //    isMms = true;
                    }
                    else if (len == 4)
                    {
                        isAcse = true;
                        for (int i = 0; i < 4; i++)
                        {
                            if (buffer[bufPos + i] != asn_id_as_acse[i])
                            {
                                isAcse = false;
                            }
                        }
                        //if (memcmp(buffer + bufPos, asn_id_as_acse, 4) == 0)
                        //    isAcse = true;
                    }

                    bufPos += len;

                    break;

                case 0x30:     /* transfer-syntax-name */
                    iecs.logger.LogDebug("PRES: ignore transfer-syntax-name");

                    bufPos += len;
                    break;

                default:
                    iecs.logger.LogDebug("PRES: unknown tag in presentation-context-definition-list-entry");
                    bufPos += len;
                    break;
                }
            }

            if (contextId < 0)
            {
                iecs.logger.LogDebug("PRES: ContextId not defined!");
                return(-1);
            }

            if ((isAcse == false) && (isMms == false))
            {
                iecs.logger.LogDebug("PRES: not an ACSE nor MMS context definition");

                return(-1);
            }

            if (isMms)
            {
                mmsContextId = (byte)contextId;
                iecs.logger.LogDebug(String.Format("PRES: MMS context id is {0}", contextId));
            }
            else
            {
                acseContextId = (byte)contextId;
                iecs.logger.LogDebug(String.Format("PRES: ACSE context id is {0}", contextId));
            }

            return(bufPos);
        }
Ejemplo n.º 19
0
        public int createAssociateResponseMessage(byte acseResult, byte[] buffer, int bufIndex, byte[] payload, int payloadLength)
        {
            int appContextLength       = 9;
            int resultLength           = 5;
            int resultDiagnosticLength = 5;

            int fixedContentLength = appContextLength + resultLength + resultDiagnosticLength;

            int variableContentLength = 0;

            int assocDataLength;
            int userInfoLength;
            int nextRefLength;

            /* single ASN1 type tag */
            variableContentLength += payloadLength;
            variableContentLength += 1;
            variableContentLength += IsoUtil.BerEncoder_determineLengthSize((uint)payloadLength);

            /* indirect reference */
            nextRefLength          = IsoUtil.BerEncoder_UInt32determineEncodedSize((uint)nextReference);
            variableContentLength += nextRefLength;
            variableContentLength += 2;

            /* association data */
            assocDataLength        = variableContentLength;
            variableContentLength += IsoUtil.BerEncoder_determineLengthSize((uint)assocDataLength);
            variableContentLength += 1;

            /* user information */
            userInfoLength         = variableContentLength;
            variableContentLength += IsoUtil.BerEncoder_determineLengthSize((uint)userInfoLength);
            variableContentLength += 1;

            variableContentLength += 2;

            int contentLength = fixedContentLength + variableContentLength;

            int bufPos = 0;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x61, (uint)contentLength, buffer, bufPos);

            /* application context name */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa1, 7, buffer, bufPos);
            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 5, buffer, bufPos);
            //memcpy(buffer + bufPos, appContextNameMms, 5);
            appContextNameMms.CopyTo(buffer, bufPos);
            bufPos += 5;

            /* result */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0xa2, 3, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
            buffer[bufPos++] = acseResult;

            /* result source diagnostics */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0xa3, 5, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0xa1, 3, buffer, bufPos);
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
            buffer[bufPos++] = 0;

            /* user information */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xbe, (uint)userInfoLength, buffer, bufPos);

            /* association data */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x28, (uint)assocDataLength, buffer, bufPos);

            /* indirect-reference */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x02, (uint)nextRefLength, buffer, bufPos);
            bufPos = IsoUtil.BerEncoder_encodeUInt32((uint)nextReference, buffer, bufPos);

            /* single ASN1 type */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa0, (uint)payloadLength, buffer, bufPos);

            /*writeBuffer.partLength = bufPos;
             * writeBuffer.length = bufPos + payloadLength;
             * writeBuffer.nextPart = payload;*/

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);
            return(bufPos + payloadLength);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Parses Iso presentation connect message
        /// </summary>
        /// <param name="buffer">Data buffer</param>
        /// <param name="offset">Index of the first message byte</param>
        /// <param name="length">Length of the buffer from offset to end</param>
        /// <returns>Index to the user data (payload) in the absolute numbering (from the buffer index 0)</returns>
        public int parseConnect(byte[] buffer, int offset, int length)
        {
            int maxBufPos = offset + length;

            int bufPos = offset;

            byte cpTag = buffer[bufPos++];

            if (cpTag != 0x31)
            {
                iecs.logger.LogDebug("PRES: not a CP type");
                return(0);
            }

            int len = 0;

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

            iecs.logger.LogDebug(String.Format("PRES: CPType with len {0}", len));

            while (bufPos < maxBufPos)
            {
                byte tag = buffer[bufPos++];

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);

                if (bufPos < 0)
                {
                    iecs.logger.LogDebug("PRES: wrong parameter length\n");
                    return(0);
                }

                switch (tag)
                {
                case 0xa0:     /* mode-selection */
                {
                    if (buffer[bufPos++] != 0x80)
                    {
                        iecs.logger.LogDebug("PRES: mode-value of wrong type!");
                        return(0);
                    }
                    bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, maxBufPos);
                    uint modeSelector = IsoUtil.BerDecoder_decodeUint32(buffer, len, bufPos);
                    iecs.logger.LogDebug(String.Format("PRES: modesel {0}", modeSelector));
                    bufPos += len;
                }
                break;

                case 0xa2:     /* normal-mode-parameters */
                    bufPos = parseNormalModeParameters(buffer, len, bufPos);

                    if (bufPos < 0)
                    {
                        iecs.logger.LogDebug("PRES: error parsing normal-mode-parameters");
                        return(0);
                    }

                    break;

                default:     /* unsupported element */
                    iecs.logger.LogDebug(String.Format("PRES: tag 0x{0:X2} not recognized\n", tag));
                    bufPos += len;
                    break;
                }
            }

            return(bufPos);
        }
Ejemplo n.º 21
0
        public int createAssociateRequestMessage(IsoConnectionParameters isoParameters, byte[] buffer, int bufIndex, byte[] payload, int payloadLength)
        {
            int authValueLength;
            int authValueStringLength = 0;

            int passwordLength = 0;

            int contentLength = 0;

            /* application context name */
            contentLength += 9;

            int calledAEQualifierLength = 0;

            if (isoParameters.remoteApTitleLen > 0)
            {
                /* called AP title */
                contentLength += (4 + isoParameters.remoteApTitleLen);

                calledAEQualifierLength = IsoUtil.BerEncoder_UInt32determineEncodedSize((uint)isoParameters.remoteAEQualifier);

                /* called AP qualifier */
                contentLength += (4 + calledAEQualifierLength);

                // Добавки Несговорова
                contentLength += 5;
                contentLength += 5;
            }

            int callingAEQualifierLength = 0;

            if (isoParameters.localApTitleLen > 0)
            {
                /* calling AP title */
                contentLength += (4 + isoParameters.localApTitleLen);

                callingAEQualifierLength = IsoUtil.BerEncoder_UInt32determineEncodedSize((uint)isoParameters.localAEQualifier);

                /* calling AP qualifier */
                contentLength += (4 + callingAEQualifierLength);

                // Добавки Несговорова
                contentLength += 5;
                contentLength += 5;
            }

            if (isoParameters.acseAuthParameter != null)
            {
                /* sender ACSE requirements */
                contentLength += 4;

                /* mechanism name */
                contentLength += 5;

                /* authentication value */
                if (isoParameters.acseAuthParameter.mechanism == AcseAuthenticationMechanism.ACSE_AUTH_PASSWORD)
                {
                    contentLength += 2;

                    //if (authParameter.value.password.passwordLength == 0)

                    passwordLength = isoParameters.acseAuthParameter.passwordLength;

                    authValueStringLength = IsoUtil.BerEncoder_determineLengthSize((uint)passwordLength);

                    contentLength += passwordLength + authValueStringLength;

                    authValueLength = IsoUtil.BerEncoder_determineLengthSize((uint)(passwordLength + authValueStringLength + 1));

                    contentLength += authValueLength;
                }
                else
                {
                    contentLength += 2;
                }
            }

            /* user information */
            int userInfoLength = 0;

            /* single ASN1 type tag */
            userInfoLength += payloadLength;
            userInfoLength += 1;
            userInfoLength += IsoUtil.BerEncoder_determineLengthSize((uint)payloadLength);

            // Добавки Несговорова
            userInfoLength += 2;
            userInfoLength += 2;

            /* indirect reference */
            userInfoLength += 1;
            userInfoLength += 2;

            /* association data */
            int assocDataLength = userInfoLength;

            userInfoLength += IsoUtil.BerEncoder_determineLengthSize((uint)assocDataLength);
            userInfoLength += 1;

            /* user information */
            int userInfoLen = userInfoLength;

            userInfoLength += IsoUtil.BerEncoder_determineLengthSize((uint)userInfoLength);
            userInfoLength += 1;

            contentLength += userInfoLength;

            int bufPos = 0;

            bufPos = IsoUtil.BerEncoder_encodeTL(0x60, (uint)contentLength, buffer, bufPos);

            /* application context name */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa1, 7, buffer, bufPos);
            bufPos = IsoUtil.BerEncoder_encodeTL(0x06, 5, buffer, bufPos);
            //memcpy(buffer + bufPos, appContextNameMms, 5);
            appContextNameMms.CopyTo(buffer, bufPos);
            bufPos += 5;

            if (isoParameters.remoteApTitleLen > 0)
            {
                /* called AP title */
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa2, (uint)isoParameters.remoteApTitleLen + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x06, (uint)isoParameters.remoteApTitleLen, buffer, bufPos);

                //memcpy(buffer + bufPos, isoParameters.remoteApTitle, isoParameters.remoteApTitleLen);
                isoParameters.remoteApTitle.CopyTo(buffer, bufPos);
                bufPos += isoParameters.remoteApTitleLen;

                /* called AE qualifier */
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa3, (uint)calledAEQualifierLength + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x02, (uint)calledAEQualifierLength, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeUInt32((uint)isoParameters.remoteAEQualifier, buffer, bufPos);
            }

            if (isoParameters.localApTitleLen > 0)
            {
                /* calling AP title */
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa6, (uint)isoParameters.localApTitleLen + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x06, (uint)isoParameters.localApTitleLen, buffer, bufPos);
                //memcpy(buffer + bufPos, isoParameters.localApTitle, isoParameters.localApTitleLen);
                isoParameters.localApTitle.CopyTo(buffer, bufPos);
                bufPos += isoParameters.localApTitleLen;

                /* calling AE qualifier */
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa7, (uint)callingAEQualifierLength + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x02, (uint)callingAEQualifierLength, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeUInt32((uint)isoParameters.localAEQualifier, buffer, bufPos);


                // Добавки Несговорова
                bufPos = IsoUtil.BerEncoder_encodeTL(0xa4, (uint)calledAEQualifierLength + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x02, (uint)calledAEQualifierLength, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeUInt32(0, buffer, bufPos);

                bufPos = IsoUtil.BerEncoder_encodeTL(0xa5, (uint)calledAEQualifierLength + 2, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeTL(0x02, (uint)calledAEQualifierLength, buffer, bufPos);
                bufPos = IsoUtil.BerEncoder_encodeUInt32(0, buffer, bufPos);
            }

            if (isoParameters.acseAuthParameter != null)
            {
                /* sender requirements */
                bufPos           = IsoUtil.BerEncoder_encodeTL(0x8a, 2, buffer, bufPos);
                buffer[bufPos++] = 0x04;

                if (isoParameters.acseAuthParameter.mechanism == AcseAuthenticationMechanism.ACSE_AUTH_PASSWORD)
                {
                    buffer[bufPos++] = requirements_authentication[0];

                    bufPos = IsoUtil.BerEncoder_encodeTL(0x8b, 3, buffer, bufPos);
                    //memcpy(buffer + bufPos, auth_mech_password_oid, 3);
                    auth_mech_password_oid.CopyTo(buffer, bufPos);
                    bufPos += 3;

                    /* authentication value */
                    bufPos = IsoUtil.BerEncoder_encodeTL(0xac, (uint)(authValueStringLength + passwordLength + 1), buffer, bufPos);
                    bufPos = IsoUtil.BerEncoder_encodeTL(0x80, (uint)passwordLength, buffer, bufPos);
                    //memcpy(buffer + bufPos, authParameter.paswordOctetString, passwordLength);
                    isoParameters.acseAuthParameter.paswordOctetString.CopyTo(buffer, bufPos);
                    bufPos += passwordLength;
                }
                else
                { /* AUTH_NONE */
                    buffer[bufPos++] = 0;
                }
            }

            /* user information */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xbe, (uint)userInfoLen, buffer, bufPos);

            /* association data */
            bufPos = IsoUtil.BerEncoder_encodeTL(0x28, (uint)assocDataLength, buffer, bufPos);

            // Добавка Несговорова
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x06, 2, buffer, bufPos);
            buffer[bufPos++] = 0x51;
            buffer[bufPos++] = 0x01;

            /* indirect-reference */
            bufPos           = IsoUtil.BerEncoder_encodeTL(0x02, 1, buffer, bufPos);
            buffer[bufPos++] = 3;

            /* single ASN1 type */
            bufPos = IsoUtil.BerEncoder_encodeTL(0xa0, (uint)payloadLength, buffer, bufPos);

            /*writeBuffer.partLength = bufPos;
             * writeBuffer.length = bufPos + payload.length;
             * writeBuffer.nextPart = payload;*/

            Array.Copy(payload, 0, buffer, bufPos, payloadLength);
            return(bufPos + payloadLength);
        }
Ejemplo n.º 22
0
        int parseFullyEncodedData(byte[] buffer, int len, int bufPos)
        {
            int  presentationSelector = -1;
            bool userDataPresent      = false;

            int endPos = bufPos + len;

            if (buffer[bufPos++] != 0x30)
            {
                iecs.logger.LogDebug("PRES: user-data parse error");
                return(-1);
            }

            bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref len, bufPos, endPos);

            endPos = bufPos + len;

            if (bufPos < 0)
            {
                iecs.logger.LogDebug("PRES: wrong parameter length");
                return(-1);
            }

            while (bufPos < endPos)
            {
                byte tag    = buffer[bufPos++];
                int  length = 0;

                bufPos = IsoUtil.BerDecoder_decodeLength(buffer, ref length, bufPos, endPos);

                if (bufPos < 0)
                {
                    iecs.logger.LogDebug("PRES: wrong parameter length");
                    return(-1);
                }

                switch (tag)
                {
                case 0x02:     /* presentation-context-identifier */
                    iecs.logger.LogDebug("PRES: presentation-context-identifier");
                    {
                        presentationSelector = (int)IsoUtil.BerDecoder_decodeUint32(buffer, length, bufPos);
                        nextContextId        = (byte)presentationSelector;
                        bufPos += length;
                    }
                    break;

                case 0xa0:
                    iecs.logger.LogDebug("PRES: fully-encoded-data");

                    userDataPresent = true;

                    nextPayload_bufferIndex = bufPos;
                    nextPayload_size        = length;

                    bufPos += length;
                    break;

                default:
                    iecs.logger.LogDebug(String.Format("PRES: fed: unknown tag 0x{0:X2}", tag));

                    bufPos += length;
                    break;
                }
            }

            if (!userDataPresent)
            {
                iecs.logger.LogDebug("PRES: user-data not present\n");
                return(-1);
            }

            return(bufPos);
        }