Skip to content
LibSip_Steps.ttcn 207 KiB
Newer Older
/**
 * @author      STF 346, STF366, STF368, STF369, STF450, STF471
 * @version     $Id: LibSip_Steps.ttcn 645 2016-12-01 13:50:32Z pintar $
 * @desc        This module provides the functions, altsteps and external functions used 
 *              for SIP-IMS tests.
 *              This module is part of LibSipV3.
*/
module LibSip_Steps {
    // LibCommon
    import from LibCommon_Sync all;
    import from LibCommon_VerdictControl all;
    // LibSip
    import from LibSip_SIPTypesAndValues all;
    import from LibSip_SDPTypes all;
    import from LibSip_Templates all;
    import from LibSip_Interface all;
    import from LibSip_PIXITS all;
    import from LibSip_XMLTypes all;
    import from LibSip_Common all;

    group externalfunctions {

        /**
         * @desc External function to return random charstring
         */
        external function fx_rndStr(
        ) return charstring;
        
        /**
         * @desc External function to return the equivalent string in lower case
         */
        external function fx_putInLowercase(
            charstring p_string
        ) return charstring;

        /**
         * @desc External function to get IP address.
         */
        external function fx_getIpAddr(
            charstring p_host_name
        ) return charstring;

        /**
         * @desc External function to generate a digest response.
         * @reference RFC 2617 HTTP Authentication: Basic and Digest Access Authentication, and RFC 1321 The MD5 Message-Digest Algorithm
         * @see RFC 2617, chapter 5 Sample implementation, for example usage, as the signature of calculateDigestResponse is according to the example given in the RFC.
         */
        external function fx_calculateDigestResponse(
            charstring p_nonce,
            charstring p_cnonce,
            charstring p_user,
            charstring p_realm,
            charstring p_passwd,
            charstring p_alg,
            charstring p_nonceCount,
            charstring p_method,
            charstring p_qop,
            charstring p_URI,
            charstring p_HEntity
        ) return charstring;

        /**
         * @desc External function to encode Base64
         */
        external function fx_encodeBase64(
                                          in charstring p_value
                                          ) return charstring;
        
        /**
         * @desc External function to decode Base64
         */
        external function fx_decodeBase64(
                                          in charstring p_value
                                          ) return charstring;
        
    }

    group ParameterOperations {

        /**
         * @desc function to generate a 32 bits random number as a charstring for tag field (used as e.g.: tag in from-header field, or branch parameter in via header)
         * @return random value with at least 32 bits of randomness
         */
        function f_getRndTag(
        ) return charstring  {
            var charstring v_tag_value;
            // tag_value is initialized with a random value with at least 32 bits of randomness
            // 4294967296 is a 32 bits integer
            if (PX_SEED == false) {
              v_tag_value := fx_rndStr() & fx_rndStr();
            } else {
              v_tag_value := "12345";
            }
            return (v_tag_value);
        }

        /**
         * @desc Function to prepare credentials for request that has an empty entity body such as a REGISTER message.
         * @param p_userprofile to get important parameters
         * @param p_algorithm Algorthm to be used. Default: omit
         * @return Credentials field
         */
        function f_calculatecCredentials_empty(
            in SipUserProfile p_userprofile,
            in boolean p_algorithm := false
        ) return Credentials  {
            var Credentials v_result;
            

            // RFC 2617 3.2.2 username:
            // The name of user in the specified realm.
            var charstring v_username := p_userprofile.privUsername;
            var charstring v_realm := p_userprofile.registrarDomain;
            var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;
            var CommaParam_List v_digestResponse := {};
            // Construct credentials for an Authorization field of a request.
            v_digestResponse := f_addCommaParameter(v_digestResponse, {
                id := "username",
                paramValue := { quotedString := v_username }
            });
            v_digestResponse := f_addCommaParameter(v_digestResponse, {
                id := "realm",
                paramValue := { quotedString := v_realm }
            });
            v_digestResponse := f_addCommaParameter(v_digestResponse, {
                id := "uri",
                paramValue := { quotedString := v_uri }
            });
            v_digestResponse := f_addCommaParameter(v_digestResponse, {
                id := "nonce",
                paramValue := { quotedString := "" }
            }); // already enclosed to " characters
            v_digestResponse := f_addCommaParameter(v_digestResponse, {
                id := "response",
                paramValue := { quotedString := "" }
            }); // already enclosed to " characters

            if (p_algorithm) {
                v_digestResponse := f_addCommaParameter(v_digestResponse, {
                    id := "algorithm",
                    paramValue := { tokenOrHost := PX_AUTH_ALGORITHM }
                }); // already enclosed to " characters
            }
            v_result := {digestResponse := v_digestResponse};

            return v_result;
        }


        /**
         * @desc Function to calculate credentials for request that has an empty entity body such as a REGISTER message.
         * @param p_userprofile to get important parameters
         * @param p_method (can be "REGISTER", "INVITE",....)
         * @param p_challenge parameter from 4xx response
         * @return Credentials field
         * @verdict
         */
        function f_calculatecCredentials(
            in SipUserProfile p_userprofile,
            in charstring p_method,
            in CommaParam_List p_challenge
        ) return Credentials  {
            var Credentials v_result;
            var charstring v_nonce := "";
            var charstring v_cnonce := int2str(float2int(int2float(13172657659 - 1317266) * rnd()) + 1317265);

            // RFC 2617 3.2.2 username:
            // The name of user in the specified realm.
            var charstring v_username := p_userprofile.privUsername;
            var charstring v_realm;

            // RFC 2617 3.2.2.2 passwd:
            // A known shared secret, the password of user of the specified
            // username.
            var charstring v_passwd := p_userprofile.passwd;
            var charstring v_algorithm;

            // a new pseudo-random cnonce value is used every time
            // that assumes it is only used once
            const charstring cl_nonceCount := "00000001";
            var charstring v_qop := p_userprofile.qop;
            var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;

            // MD5 hash of empty entity body.
            const charstring cl_hEntity := "d41d8cd98f00b204e9800998ecf8427e";
            var charstring v_response;
            var charstring v_opaque;
            var CommaParam_List v_digestResponse := {};


            // extract nonce, realm, algorithm, and opaque from challenge
            v_nonce := f_extractParamValueFromChallenge(p_challenge, "nonce");
            v_realm := f_extractParamValueFromChallenge(p_challenge, "realm");
            v_algorithm := f_extractParamValueFromChallenge(p_challenge, "algorithm");
            v_opaque := f_extractParamValueFromChallenge(p_challenge, "opaque");

            // calculate a digest response for the Authorize header
            v_response := fx_calculateDigestResponse(v_nonce, v_cnonce, v_username, v_realm, v_passwd, v_algorithm, cl_nonceCount, p_method, v_qop, v_uri, cl_hEntity);

            // Construct credentials for an Authorization field of a request.
Loading
Loading full blame…