⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osptnepenroll.c

📁 mgcp协议源代码。支持多种编码:g711
💻 C
📖 第 1 页 / 共 5 页
字号:
        if ( retVal != OSPC_ERR_NO_ERROR )        {            OSPM_DBGERRORLOG(                 retVal,                 "Unable to create enrollment request's header.\n" );        }    }    /* If ( we could create the body and header without a glitch ) then     *  o assign the request to the message info.     *  o set the size of the message     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        ospvMsgInfoOut->RequestMsg = requestBfr;        ospvMsgInfoOut->RequestSz = OSPM_STRLEN( (const char *)requestBfr ) + 2;    }        /* Else ( we ran into some problem ) so     *  o free up the requestBfr if necessary     */    else    {        if ( requestBfr != OSPC_OSNULL )        {            OSPM_FREE( requestBfr );        }    }    OSPM_DBGEXIT(( "OSPPConstructEnrollmentRequest exit\n" ));    return retVal;}/* Check all of the enrollment parameters for any problems that might cause * it to be rejected by an enrollment server. We'll check that the * username and password are alnum, that the customer and device ids * are numeric, and that the base64-encoded certificate request exists * ( the contents of the base64-encoded certificate request are validated  * elsewhere. ) We'll get an error if any of these values are empty or null. * * Input: reference to the enrollment parameters * * Output: OSPC_ERR_NO_ERROR if all of the parameters are non-null, non-empty, *         and have the appropriate type. Otherwise, an error code other *         than OSPC_ERR_NO_ERROR will be returned. */int OSPPCheckEnrollmentParams (    OSPTENROLLPARAMS* ospvEnrollParams){    int retVal = OSPC_ERR_NO_ERROR;    OSPM_DBGENTER(( "ENTER: OSPPCheckEnrollmentParams\n" ));    /* Set an error code if the input parameters are null: */    if ( ospvEnrollParams == OSPC_OSNULL )    {        retVal = OSPC_ERR_ENROLL_INVALID_ARG;    }    if ( retVal == OSPC_ERR_NO_ERROR )    {        if ( ospvEnrollParams->Username == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_BAD_USERNAME;            OSPM_DBGERRORLOG(                 retVal,                 "The username was null.\n" );        }            /* Check the username: */        retVal = OSPPValidateAsciiString( ospvEnrollParams->Username );        OSPM_DBGMISC(( "username: <%s>\n", ospvEnrollParams->Username ));        if ( retVal != OSPC_ERR_NO_ERROR )        {            retVal = OSPC_ERR_ENROLL_BAD_USERNAME;            OSPM_DBGERRORLOG(                 retVal,                 "An invalid ( non-alnum ) username was entered.\n" );        }    }    /* Check the password: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        if ( ospvEnrollParams->Password == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_BAD_PASSWORD;            OSPM_DBGERRORLOG(                 retVal,                 "The password was null.\n" );        }            retVal = OSPPValidateAsciiString( ospvEnrollParams->Password );        if ( retVal != OSPC_ERR_NO_ERROR )        {            retVal = OSPC_ERR_ENROLL_BAD_PASSWORD;            OSPM_DBGERRORLOG(                 retVal,                 "An invalid ( non-alnum ) password was entered.\n" );        }    }    /* Check the device id: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        retVal = OSPPValidateDigitString( ospvEnrollParams->DeviceId );        if ( retVal != OSPC_ERR_NO_ERROR )        {            retVal = OSPC_ERR_ENROLL_BAD_DEVICE_ID;            OSPM_DBGERRORLOG(                 retVal,                 "An invalid ( non-digit ) device id was entered.\n" );        }    }    /* Check the customer id: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        retVal = OSPPValidateDigitString( ospvEnrollParams->CustomerId );        if ( retVal != OSPC_ERR_NO_ERROR )        {            retVal = OSPC_ERR_ENROLL_BAD_CUSTOMER_ID;            OSPM_DBGERRORLOG(                 retVal,                 "An invalid ( non-digit ) customer id was entered.\n" );        }    }    /* check the certificate request: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        if ( ( ospvEnrollParams->CertReq == OSPC_OSNULL ) ||             ( OSPM_STRLEN( (const char *)ospvEnrollParams->CertReq ) <= 0 ) )         {            retVal = OSPC_ERR_ENROLL_BAD_CERT_B64;        }    }    OSPM_DBGEXIT(( "EXIT: OSPPCheckEnrollmentParams\n" ));    return retVal;}/* Given a pointer to a string to write, and a pointer to its length, and * the length of a nonce to generate, generate a nonce and place it in the * output string and its referenced length. The nonce will be binary, not * ASCII. * * This function will use OSPPFillBufWithRandomBytes, which in turn * relies on OSPPUtilGetRandom, for generating the random bytes of the nonce.  * Since OSPPUtilGetRandom doesn't rely on anything special for the * entropy of these random values, it would be best to either modify  * OSPPUtilGetRandom or just pass the value of the nonce in ( so that we * don't generate it here. ) * * Input: reference to a string and its length to write to, as well as a *        length that specifies how long the nonce should be. * * Output: OSPC_ERR_NO_ERROR if there aren't any problems; in this case,  *         the *ospvNonceOut should be non-null and *ospvNonceLenOut should *         specify its length. Otherwise, an error code other than  *         OSPC_ERR_NO_ERROR will be returned if a problem comes up. In this *         case, *ospvNonceOut should be OSPC_OSNULL and *ospvNonceLenOut *         should be 0 ( but this isn't guaranteed, especially if  *         ospvNonceOut or ospvNonceLenOut were OSPC_OSNULL to begin with. ) */int OSPPCreateNonce(    unsigned char** ospvNonceOut,    unsigned*       ospvNonceLenOut,    unsigned        ospvNonceLenIn){    int            retVal   = OSPC_ERR_NO_ERROR;    OSPM_DBGENTER(( "ENTER: OSPPCreateNonce\n" ));    /* Check the input parameters for any problems: */    if ( ( ospvNonceOut == OSPC_OSNULL ) ||           ( ospvNonceLenOut == OSPC_OSNULL ) ||         ( ospvNonceLenIn <= 0 ) )    {        retVal = OSPC_ERR_ENROLL_INVALID_ARG;        OSPM_DBGERRORLOG(             retVal,             "Bad arguments for creating a nonce.\n" );    }    /* If ( we had didn't have any problems ) then     *  o allocate space for the outgoing nonce     *  o complain if we couldn't create the space.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        /* Start by freeing up space so that we don't get a memory leak: */        if ( *ospvNonceOut != OSPC_OSNULL )        {            OSPM_FREE( *ospvNonceOut );        }        OSPM_MALLOC( *ospvNonceOut, unsigned char, ospvNonceLenIn + 1 );        if ( *ospvNonceOut == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_NO_MEMORY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to alloc memory for nonce.\n" );        }    }    /* If ( we could allocate memory for the nonce ) then     *  o initialize it.     *  o fill it with random bytes     *  o complain if the return value is bad or if the output nonce or      *    length are empty.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MEMSET( *ospvNonceOut, 0, ospvNonceLenIn + 1 );        retVal =             OSPPFillBufWithRandomBytes(                 *ospvNonceOut,                 ospvNonceLenOut,                 ospvNonceLenIn );         if ( ( retVal != OSPC_ERR_NO_ERROR ) ||              ( *ospvNonceOut == OSPC_OSNULL ) ||              ( *ospvNonceLenOut <= 0 ) )        {            retVal = OSPC_ERR_ENROLL_GEN_NONCE;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to generate nonce.\n" );        }    }    return retVal;}/* Create the enrollment request body that will be sent to the enrollment * server. We'll need the enrollment parameters passed in on the command * line as well as the random bytes that constitute the nonce. The  * request buffer should be alphanumeric, so we'll need the string ( but * not a pointer to its length. ) * * Input: the buffer to write the body to; the nonce and its length ( since *        the nonce may be binary ); and the enrollment parameters. * * Output: OSPC_ERR_NO_ERROR if everything goes ok; in this case, the buffer *         should contain a request that can be transmitted as part of an *         HTTP POST to the enrollment server. Otherwise, an error code will *         be returned and the buffer will be worthless. */int OSPPCreateEnrollmentRequestBody(    unsigned char**   ospvRequestBfrOut,    OSPTENROLLPARAMS* ospvEnrollParamsIn){    int retVal         = OSPC_ERR_NO_ERROR;    /* This is the maximum size of the  request being generated; it     * will be used for allocating memory later on.     */    int requestBfrSize = 0;    OSPM_DBGENTER(( "ENTER: OSPPCreateEnrollmentRequestBody\n" ));    /* Check the parameters first: */    if ( ospvRequestBfrOut == OSPC_OSNULL )    {        retVal = OSPC_ERR_ENROLL_INVALID_ARG;        OSPM_DBGERRORLOG(             retVal,             "The buffer for the enrollment request's body was null.\n" );    }    /* Now check the enrollment parameters: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        if ( ospvEnrollParamsIn == OSPC_OSNULL )         {            retVal = OSPC_ERR_ENROLL_INVALID_ARG;            OSPM_DBGERRORLOG(                 retVal,                 "The enrollment parameters for the enrollment request were null.\n" );        }    }    /* Check the enrollment request parameters: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        retVal = OSPPCheckEnrollmentParams( ospvEnrollParamsIn );        if ( retVal != OSPC_ERR_NO_ERROR )        {            OSPM_DBGERRORLOG(                 retVal,                 "The enrollment request parameters are invlaid.\n" );        }    }    /* Now check the nonce for a non-null value; this isn't done in     * OSPPCheckEnrollParams because the nonce may not have been     * entered on the command line.      */    if ( retVal == OSPC_ERR_NO_ERROR )    {        if ( ( ospvEnrollParamsIn->Nonce == OSPC_OSNULL ) ||              ( ospvEnrollParamsIn->NonceLen <= 0 ) )        {            retVal = OSPC_ERR_ENROLL_INVALID_ARG;            OSPM_DBGERRORLOG(                 retVal,                 "The nonce for the enrollment request was null or empty.\n" );        }    }    /* Allocate enough memory to store all of these values. We'll need     * enough memory to hold all of the url-encoded characters. The maximum     * size is then 3 times the size of all of the parameters combined     * ( since each character may need to be url-encoded. )     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        requestBfrSize =            3 *             ( ospvEnrollParamsIn->NonceLen +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->Username ) +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->Password ) +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->DeviceId ) +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->CustomerId ) +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->CertReq ) +              OSPM_STRLEN( (const char *)ospvEnrollParamsIn->Function ) +              100 );        OSPM_MALLOC( *ospvRequestBfrOut, unsigned char, requestBfrSize + 1 );        if ( *ospvRequestBfrOut == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_NO_MEMORY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to allocate memory for the outbound request.\n" );        }    }    /* Now add the values; add the requested operation first: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MEMSET( *ospvRequestBfrOut, 0, requestBfrSize + 1 );        retVal = OSPPAddNameValuePair(                     *ospvRequestBfrOut,                     OSPC_ENROLL_OPERATION_REQ_PARAM,                     OSPM_STRLEN( OSPC_ENROLL_OPERATION_REQ_PARAM ),                     ospvEnrollParamsIn->Function,                     OSPM_STRLEN( (const char *)ospvEnrollParamsIn->Function ),                     OSPC_FALSE );        OSPM_PRINTF( "output buffer after operation: %s\n", *ospvRequestBfrOut );         if ( retVal != OSPC_ERR_NO_ERROR )        {                retVal = OSPC_ERR_ENROLL_APPENDING_NAME_VALUE_PAIR;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to append operation to request body.\n" );        }    }    /* Now add the nonce: */    if ( retVal == OSPC_ERR_NO_ERROR )    {        retVal = OSPPAddNameValuePair(                      *ospvRequestBfrOut,                     OSPC_ENROLL_NONCE_REQ_PARAM,                      OSPM_STRLEN( OSPC_ENROLL_NONCE_REQ_PARAM ),                     ospvEnrollParamsIn->Nonce,                     ospvEnrollParamsIn->NonceLen,                      OSPC_TRUE );        OSPM_PRINTF( "output buffer after nonce: %s\n", *ospvRequestBfrOut );         if ( retVal != OSPC_ERR_NO_ERROR )        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -