EapTlsResult EapTls_ParseMdmWebApiResponse()

in EAP-TLS_Solution/EAP-TLS Client/lib/web_api_client.c [62:152]


EapTlsResult EapTls_ParseMdmWebApiResponse(MemoryBlock *responseBlock, WebApiResponse *outResponse)
{
	EapTlsResult iRes = EapTlsResult_Error;

	if (NULL != outResponse && NULL != responseBlock && NULL != responseBlock->data)
	{
		memset(outResponse, 0, sizeof(WebApiResponse));

		size_t nullTerminatedJsonSize = responseBlock->size + 1;
		char *nullTerminatedJsonString = (char *)malloc(nullTerminatedJsonSize);
		if (nullTerminatedJsonString == NULL)
		{
			Log_Debug("ERROR: could not allocate buffer for parsing the WebAPI response.\n");
		}
		else
		{
			// Copy the provided buffer to a null terminated buffer.
			memcpy(nullTerminatedJsonString, responseBlock->data, responseBlock->size);
			nullTerminatedJsonString[nullTerminatedJsonSize - 1] = 0; // Add the null terminator at the end.

			// Parse the response
			JSON_Value *rootProperties = json_parse_string(nullTerminatedJsonString);
			if (rootProperties == NULL)
			{
				Log_Debug("WARNING: Cannot parse the response as JSON content.\n");
			}
			else
			{
				JSON_Object *rootObject = json_value_get_object(rootProperties);
#if defined(WEBAPI_SERVER)
				// TBD, depending on the WebApi's JSON response schema
#endif
#if defined(WEBAPI_KESTREL)
				const char *s = json_object_get_string(rootObject, webApiResponse_Timestamp);
				if (NULL != s)
				{
					strncpy(outResponse->timestamp, s, sizeof(outResponse->timestamp) - 1);
					s = json_object_get_string(rootObject, webApiResponse_RootCACertificate);
					if (NULL != s)
					{
						strncpy(outResponse->rootCACertficate, s, sizeof(outResponse->rootCACertficate) - 1);
						s = json_object_get_string(rootObject, webApiResponse_EapTlsNetworkSsid);
						if (NULL != s)
						{
							strncpy(outResponse->eapTlsNetworkSsid, s, sizeof(outResponse->eapTlsNetworkSsid) - 1);
							s = json_object_get_string(rootObject, webApiResponse_ClientIdentity);
							if (NULL != s)
							{
								strncpy(outResponse->clientIdentity, s, sizeof(outResponse->clientIdentity) - 1);
								s = json_object_get_string(rootObject, webApiResponse_ClientPublicCertificate);
								if (NULL != s)
								{
									strncpy(outResponse->clientPublicCertificate, s, sizeof(outResponse->clientPublicCertificate) - 1);
									s = json_object_get_string(rootObject, webApiResponse_ClientPrivateKey);
									if (NULL != s)
									{
										strncpy(outResponse->clientPrivateKey, s, sizeof(outResponse->clientPrivateKey) - 1);
										s = json_object_get_string(rootObject, webApiResponse_ClientPrivateKeyPass);
										if (NULL != s)
										{
											strncpy(outResponse->clientPrivateKeyPass, s, sizeof(outResponse->clientPrivateKeyPass) - 1);
											iRes = EapTlsResult_Success;
										}
									}
								}
							}
						}
					}
				}
#endif
			}

			if (EapTlsResult_Success != iRes)
			{
				Log_Debug("ERROR parsing response.\n");
			}

		// Release the allocated memory.
		json_value_free(rootProperties);
	}

	// Release the allocated memory.
	free(nullTerminatedJsonString);
}
	else
	{
	Log_Debug("ERROR, bad parameters.\n");
	}

	return iRes;
}