summaryrefslogtreecommitdiff
path: root/src/agent/serviceadapter/sa_util.c
diff options
context:
space:
mode:
authorHyungKyu Song <hk76.song@samsung.com>2013-02-16 00:55:29 +0900
committerHyungKyu Song <hk76.song@samsung.com>2013-02-16 00:55:29 +0900
commit4043c48868b1eafa7701ac19b4f93bc90ce79c99 (patch)
tree6f3bda67c8725cb999c8fe93c4c3846c83074be8 /src/agent/serviceadapter/sa_util.c
parent2505051c83d63da1cac1e0ca958574cf98e164d9 (diff)
downloadoma-dm-agent-4043c48868b1eafa7701ac19b4f93bc90ce79c99.tar.gz
oma-dm-agent-4043c48868b1eafa7701ac19b4f93bc90ce79c99.tar.bz2
oma-dm-agent-4043c48868b1eafa7701ac19b4f93bc90ce79c99.zip
Diffstat (limited to 'src/agent/serviceadapter/sa_util.c')
-rwxr-xr-xsrc/agent/serviceadapter/sa_util.c394
1 files changed, 394 insertions, 0 deletions
diff --git a/src/agent/serviceadapter/sa_util.c b/src/agent/serviceadapter/sa_util.c
new file mode 100755
index 0000000..287e960
--- /dev/null
+++ b/src/agent/serviceadapter/sa_util.c
@@ -0,0 +1,394 @@
+/*
+ * oma-dm-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*lib*/
+#include <glib.h>
+#include <glib/gprintf.h>
+
+/*sync-agent*/
+#include <sync_agent.h>
+
+/*dm-agent*/
+#include "common/dm_common.h"
+#include "common/util/util.h"
+#include "serviceadapter/sa_util.h"
+
+#ifndef OMADM_AGENT_LOG
+#undef LOG_TAG
+#define LOG_TAG "OMA_DM_SA"
+#endif
+
+#define BUF_SIZE 300
+#define MAX_BUFFER 50
+
+void putCmdIntoList(GList ** commands, GList ** commands_last, void *pCommand)
+{
+ _EXTERN_FUNC_ENTER;
+
+ GList *temp = NULL;
+ if (*commands_last == NULL) {
+ *commands_last = *commands = g_list_append(*commands, pCommand);
+ } else {
+ temp = g_list_append(*commands_last, pCommand);
+ if(temp == NULL) {
+ _DEBUG_ERROR("g_list_append return value NULL!!");
+ }
+ *commands_last = g_list_next(*commands_last);
+ }
+
+ _EXTERN_FUNC_EXIT;
+}
+
+DM_ERROR create_auth_credString(AuthType type, const char *username, const char *password, const unsigned char *nonce, const unsigned int nonce_size, char **pCred)
+{
+ _EXTERN_FUNC_ENTER;
+
+ retvm_if((username) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "username is NULL!!");
+ retvm_if((password) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "password is NULL!!");
+
+ _DEBUG_INFO(" type : [%d] username :[%s], password :[%s] , nonce : [%s],nonce_size : [%d]", type, username, password, nonce, nonce_size);
+
+ DM_ERROR ret = DM_OK;
+
+ switch (type) {
+ case AUTH_TYPE_BASIC:
+ {
+ char *plain = g_strjoin(":", username, password, NULL);
+ *pCred = g_base64_encode((unsigned char *)plain, strlen(plain));
+ if (*pCred == NULL) {
+ free(plain);
+ ret = COMMON_ERR_INTERNAL_NO_MEMORY;
+ goto error;
+ }
+ free(plain);
+
+ break;
+ }
+ case AUTH_TYPE_MD5:
+ {
+ /* How does syncml:auth-md5 works?
+ *
+ * base64(
+ * md5(
+ * base64(
+ * md5(
+ * username + ":" + password
+ * )
+ * ) +
+ * ":" + nonce
+ * )
+ * )
+ */
+
+ /* Let's determine the string for the comparison. */
+ char *auth = NULL;
+ auth = g_strjoin(":", username, password, NULL);
+ _DEBUG_INFO("[username:password] = %s\n", auth);
+
+ unsigned char *digest = NULL;
+ digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, strlen(auth));
+ free(auth);
+ *pCred = g_base64_encode(digest, 16);
+ if (*pCred == NULL) {
+ ret = COMMON_ERR_INTERNAL_NO_MEMORY;
+ goto error;
+ }
+
+ _DEBUG_INFO("nonce = %s", nonce);
+ _DEBUG_INFO("nonce_size = %d", nonce_size);
+ int auth_size = strlen(*pCred) + nonce_size + 1;
+ auth = (char *)calloc(strlen(*pCred) + nonce_size + 1 + 1, sizeof(char));
+ if (auth == NULL) {
+ _DEBUG_INFO("alloc fail");
+ _EXTERN_FUNC_EXIT;
+ return COMMON_ERR_ALLOC;
+ }
+ memcpy(auth, *pCred, strlen(*pCred));
+ auth[strlen(*pCred)] = ':';
+ memcpy(auth + strlen(*pCred) + 1, nonce, nonce_size);
+ _DEBUG_INFO("base64[md5[username:password]] = %s\n", *pCred);
+ _DEBUG_INFO("before last base64 encoding = %s\n", auth);
+ free(*pCred);
+
+ digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, auth_size);
+ _DEBUG_INFO("md5[base64[md5[username:password]]:nonce] = %s\n", digest);
+
+ free(auth);
+ *pCred = g_base64_encode(digest, 16);
+ free(digest);
+ _DEBUG_INFO("base64[md5[base64[md5[username:password]]:nonce]] = %s", *pCred);
+ if (*pCred == NULL) {
+ ret = COMMON_ERR_INTERNAL_NO_MEMORY;
+ goto error;
+ }
+ break;
+ }
+ case AUTH_TYPE_HMAC:
+ {
+ /*do not use cred element */
+ }
+ break;
+ case AUTH_TYPE_UNKNOWN:
+ case AUTH_TYPE_HTTP_BASIC:
+ case AUTH_TYPE_HTTP_DIGEST:
+ case AUTH_TYPE_X509:
+ case AUTH_TYPE_SECURID:
+ case AUTH_TYPE_SAFEWORD:
+ case AUTH_TYPE_DIGIPASS:
+ case AUTH_TYPE_TRANSPORT:
+ _DEBUG_INFO("not support auth type");
+ ret = DM_ERR_FORBIDDEN;
+ goto error;
+ break;
+ default:
+ break;
+ }
+
+ _EXTERN_FUNC_EXIT;
+ return ret;
+
+ error:
+ _DEBUG_INFO("error : %d\n", ret);
+ _EXTERN_FUNC_EXIT;
+ return ret;
+}
+
+DM_ERROR gemerate_hmac(char *username, char *password, unsigned char *nonce, unsigned int nonce_size, char *msg, unsigned int msg_size, char **mac)
+{
+ _EXTERN_FUNC_ENTER;
+
+ DM_ERROR ret = DM_OK;
+
+ retvm_if((msg) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
+ retvm_if((username) == NULL, COMMON_ERR_IS_NULL, "username is NULL!!");
+ retvm_if((password) == NULL, COMMON_ERR_IS_NULL, "password is NULL!!");
+ retvm_if((nonce) == NULL, COMMON_ERR_IS_NULL, "nonce is NULL!!");
+
+ char *resultmd = NULL;
+ unsigned char *resultmd5 = NULL;
+ unsigned char *resultmd6 = NULL;
+ //char md5Digest[16];
+ char buffer[BUF_SIZE];
+ char *temp_result1 = NULL;
+ char *temp_result2 = NULL;
+ char *temp_result3 = NULL;
+ unsigned int size;
+ int len = 0;
+ int str_len = 0;
+/* if (nonce == NULL|| username == NULL|| password == NULL|| msg == NULL)
+ return 0;*/
+
+ /* Logging */
+ _DEBUG_INFO("generate HMAC");
+ //resultmd5 = (unsigned char *)calloc(1, sizeof(char)*17);
+
+ /* H(messageBody) */
+ resultmd = (char *)sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, (const char *)msg, (int)msg_size);
+ resultmd5 = (unsigned char *)strdup(resultmd);
+
+ size = 16;
+
+ int i = 0;
+ char *digest_msg = (char *)calloc(MAX_BUFFER, sizeof(char));
+ if (digest_msg == NULL) {
+ _DEBUG_INFO("alloc fail");
+ _EXTERN_FUNC_EXIT;
+ return COMMON_ERR_ALLOC;
+ }
+ for (i = 0; i < 16; i++) {
+ snprintf(digest_msg + (i * 2), MAX_BUFFER, "%02x", resultmd5[i]);
+ }
+ _DEBUG_INFO("MD 5 = %s", digest_msg);
+ /* B64(H(messageBody) */
+ temp_result1 = g_base64_encode(resultmd5, size);
+ msg_size = size;
+
+ /* Logging */
+ _DEBUG_INFO("B64(H(messageBody)) : %s", temp_result1);
+
+ /* H(username:password) */
+ str_len = g_strlcpy(buffer, username, (int)sizeof(buffer));
+ if (str_len >= sizeof(buffer)) {
+ _DEBUG_INFO("buffer over flow");
+ ret = COMMON_ERR_BUFFER_OVERFLOW;
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+ len = g_strlcat(buffer, ":", BUF_SIZE);
+ len = g_strlcat(buffer, password, BUF_SIZE);
+ size = strlen(buffer);
+
+ _DEBUG_INFO("username:password: %s", buffer);
+ resultmd5 = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, (const char *)buffer, (int)size);
+ size = 16;
+
+ /* B64(H(username:password)) */
+ temp_result2 = g_base64_encode(resultmd5, size);
+ _DEBUG_INFO("B64(H(username:password)) %s", temp_result2);
+
+ /* B64(H(username:password)):nonce:B64(H(message body)) */
+ str_len = g_strlcpy(buffer, (const char *)temp_result2, (int)sizeof(buffer));
+ if (str_len >= sizeof(buffer)) {
+ _DEBUG_INFO("buffer over flow");
+ ret = COMMON_ERR_BUFFER_OVERFLOW;
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+
+ len = g_strlcat(buffer, ":", BUF_SIZE);
+
+ _DEBUG_INFO("B64(H(username:password)): = %s", buffer);
+ size = strlen((const char *)temp_result2) + 1;
+ _DEBUG_INFO("buffer : %s, real size : %d , size : %d\n", buffer, strlen(buffer), size);
+
+ len = g_strlcat(buffer, (const char *)nonce, BUF_SIZE);
+
+ _DEBUG_INFO("B64(H(username:password)):nonce = %s", buffer);
+ size += strlen((const char *)nonce);
+ _DEBUG_INFO("buffer : %s, real size : %d , size : %d\n", buffer, strlen(buffer), size);
+
+ len = g_strlcat(buffer, ":", BUF_SIZE);
+
+ size++;
+ _DEBUG_INFO("buffer : %s, real size : %d , size : %d\n", buffer, strlen(buffer), size);
+
+ len = g_strlcat(buffer, temp_result1, BUF_SIZE);
+
+ size += strlen((const char *)temp_result1);
+ _DEBUG_INFO("B64(H(username:password)):nonce:B64(H(message body)): %s", buffer);
+
+ /* Interim Clean up */
+ str_free((char **)(&temp_result1));
+ str_free((char **)(&temp_result2));
+
+ if (str_len >= BUF_SIZE) {
+ _DEBUG_INFO("buffer over flow");
+ ret = COMMON_ERR_BUFFER_OVERFLOW;
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+
+ /*char *buffer1 = "wUN5aEY1cn4K1AhZnuSg6Q==:12345:stthIupGdabKGLdI7ezeBw==";
+ int size1 = strlen(buffer1);
+ printf("buffer1 : %s, size1 : %d\n", buffer1, size1);
+ */
+ /* H(B64(H(username:password)):nonce:B64(H(message body))) */
+ resultmd6 = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, buffer, size);
+ size = 16;
+
+ i = 0;
+ printf("gener : ");
+ for (i = 0; i < size; ++i) {
+ printf(" %02x", resultmd6[i]);
+ }
+ printf("\n");
+
+ /* Return the base 64 of digest */
+ temp_result3 = g_base64_encode(resultmd6, size);
+
+ /* Logging */
+ _DEBUG_INFO("B64(H(B64(H(username:password)):nonce:B64(H(message body)))): %s", temp_result3);
+
+ (*mac) = strdup((const char *)temp_result3);
+ str_free((char **)(&temp_result3));
+
+ _EXTERN_FUNC_EXIT;
+ return ret;
+
+}
+
+void set_xml_to_file(char *xml, const char *path)
+{
+ _EXTERN_FUNC_ENTER;
+
+ FILE *pFile = NULL;
+
+ if (xml != NULL) {
+ pFile = fopen(path, "a");
+ }
+
+ if (pFile == NULL) {
+ _EXTERN_FUNC_EXIT;
+ return;
+ }
+
+ fputs("==================================================================================", pFile);
+ fputs("\n", pFile);
+ fputs(xml, pFile);
+
+ fclose(pFile);
+
+ _EXTERN_FUNC_EXIT;
+}
+
+DM_ERROR findStgringValue(char *original, char *findParam, char *findSep, char **findValue)
+{
+ _EXTERN_FUNC_ENTER;
+
+ DM_ERROR ret = DM_OK;
+
+ retvm_if((original) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "original is NULL!!");
+ retvm_if((findParam) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "findParam is NULL!!");
+
+ char *findOption = NULL;
+ char *findEnd = NULL;
+ char *findSepEnd = NULL;
+ int valueSize = 0;
+ /*int optionSize = 0;
+ int sepaSize = 0; */
+
+ /*mindt=10&dr=1&maclen=10 */
+ findOption = strstr(original, findParam);
+ if (findOption != NULL) {
+ _DEBUG_INFO(" find Option : %s\n", findOption);
+
+ /*=10&dr=1&maclen=10*/
+ findEnd = strstr(findOption, findSep);
+ if (findEnd != NULL) {
+ /*&dr=1&maclen=10 */
+ findSepEnd = strstr(findOption, UI_OP_SEPARATOR);
+ if (findSepEnd != NULL) {
+ valueSize = strlen(findEnd) - strlen(findSepEnd) - 1;
+ } else {
+ valueSize = strlen(findEnd);
+ }
+ _DEBUG_INFO(" findEnd : %s\n", findEnd);
+ _DEBUG_INFO(" findSepEnd : %s\n", findSepEnd);
+ _DEBUG_INFO(" value size : %d\n", valueSize);
+ } else {
+ _DEBUG_INFO("not found value");
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+ (*findValue) = calloc(1, valueSize + 1);
+ if ((*findValue) == NULL) {
+ _DEBUG_INFO("alloc fail");
+ return COMMON_ERR_ALLOC;
+ }
+ memcpy((*findValue), findEnd + 1, valueSize);
+ (*findValue)[valueSize] = '\0';
+
+ _DEBUG_INFO(" find Value : %s : %d \n", (*findValue), valueSize);
+
+ } else {
+ _DEBUG_INFO(" not found \n");
+ }
+
+ _EXTERN_FUNC_EXIT;
+ return ret;
+
+}