EISErr RequestSignatureFromEIS()

in src/utils/eis_utils/src/eis_coms.c [457:564]


EISErr RequestSignatureFromEIS(
    const char* keyHandle, const char* uri, const char* expiry, unsigned int timeoutMS, char** responseBuffer)
{
    EISErr result = EISErr_Failed;

    if (responseBuffer == NULL || keyHandle == NULL || uri == NULL || expiry == NULL)
    {
        return EISErr_InvalidArg;
    }

    *responseBuffer = NULL;

    char* response = NULL;

    char* serializedPayload = NULL;

    char* uriToSign = NULL;
    STRING_HANDLE encodedUriToSign = NULL;

    JSON_Value* payloadValue = json_value_init_object();

    if (payloadValue == NULL)
    {
        goto done;
    }

    /**
     * Example KeyService Sign Request
     * {
     *  "keyHandle":"foo",
     *  "algorithm":"HMAC-256",
     *  "parameters" : {
     *      "message":"something-to-be-signed"
     *  }
     * }
     */
    JSON_Object* payloadObj = json_value_get_object(payloadValue);

    if (payloadObj == NULL)
    {
        goto done;
    }

    uriToSign = ADUC_StringFormat("%s\n%s", uri, expiry);

    if (uriToSign == NULL)
    {
        goto done;
    }

    encodedUriToSign = Azure_Base64_Encode_Bytes((unsigned char*)uriToSign, strlen(uriToSign));

    if (encodedUriToSign == NULL)
    {
        goto done;
    }

    if (json_object_set_string(payloadObj, EIS_SIGN_REQ_KEYHANDLE_FIELD, keyHandle) != JSONSuccess)
    {
        goto done;
    }

    if (json_object_set_string(payloadObj, EIS_SIGN_REQ_ALG_FIELD, EIS_SIGN_ALGORITHM) != JSONSuccess)
    {
        goto done;
    }

    if (json_object_dotset_string(payloadObj, EIS_SIGN_REQ_DOTSET_PARAMS_MSG_FIELD, STRING_c_str(encodedUriToSign))
        != JSONSuccess)
    {
        goto done;
    }

    serializedPayload = json_serialize_to_string(payloadValue);

    if (serializedPayload == NULL)
    {
        goto done;
    }

    result = SendEISRequest(EIS_UDS_SIGN_SOCKET_PATH, EIS_SIGN_REQUEST_URI, serializedPayload, timeoutMS, &response);

    if (result != EISErr_Ok)
    {
        goto done;
    }

    result = EISErr_Ok;
done:

    json_value_free(payloadValue);

    free(serializedPayload);

    STRING_delete(encodedUriToSign);

    free(uriToSign);

    if (result != EISErr_Ok)
    {
        free(response);
        response = NULL;
    }

    *responseBuffer = response;

    return result;
}