diff options
Diffstat (limited to 'src/agent/service-adapter/sa_util.c')
-rwxr-xr-x | src/agent/service-adapter/sa_util.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/src/agent/service-adapter/sa_util.c b/src/agent/service-adapter/sa_util.c new file mode 100755 index 0000000..6538765 --- /dev/null +++ b/src/agent/service-adapter/sa_util.c @@ -0,0 +1,177 @@ +/* + * oma-ds-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. + */ + +/** + * @SA_Util.c + * @version 0.1 + * @brief This file is the source file of implementation of utility function + */ + +#include <sync_agent.h> + +#include "service-adapter/sa_util.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +void put_into_list(GList ** commands, GList ** commands_last, void *command) +{ + _EXTERN_FUNC_ENTER; + + GList *temp = NULL; + if (*commands_last == NULL) { + *commands_last = *commands = g_list_append(*commands, command); + } else { + temp = g_list_append(*commands_last, command); + *commands_last = g_list_next(*commands_last); + } + + _EXTERN_FUNC_EXIT; +} + +sa_error_type_e create_cred_string(auth_type_e type, const char *user_name, const char *password, const char *nonce, unsigned int nonce_size, char **cred) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errortype = SA_INTERNAL_OK; + + switch (type) { + case AUTH_TYPE_BASIC: + { + char *plain = g_strjoin(":", user_name, password, NULL); + *cred = g_base64_encode((unsigned char *)plain, strlen(plain)); + if (*cred == NULL) { + free(plain); + errortype = SA_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 = g_strjoin(":", user_name, password, NULL); + _DEBUG_INFO("username:password = %s", auth); + unsigned char *digest = NULL; + digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, strlen(auth)); + free(auth); + *cred = g_base64_encode(digest, 16); + free(digest); + if (*cred == NULL) { + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (nonce != NULL) { + _DEBUG_INFO("nonce = %s", nonce); + _DEBUG_INFO("nonce_size = %d", nonce_size); + + int auth_size = strlen(*cred) + nonce_size + 1; + auth = (char *)calloc(auth_size + 1, sizeof(char)); + if (auth == NULL) { + _DEBUG_ERROR("auth is NULL"); + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + + memcpy(auth, *cred, strlen(*cred)); + auth[strlen(*cred)] = ':'; + memcpy(auth + strlen(*cred) + 1, nonce, nonce_size); + _DEBUG_INFO("base64[md5[username:password]] = %s", *cred); + _DEBUG_INFO("before last base64 encoding = %s", auth); + free(*cred); + + /*MD5GetDigest (auth, strlen(auth), digest); */ + + /* + GChecksum* pMd5 = g_checksum_new(G_CHECKSUM_MD5); + g_checksum_update(pMd5, auth, auth_size); + gsize temp = 16; + digest = (unsigned char*)calloc(16, sizeof(unsigned char)); + if (digest == NULL) { + _DEBUG_ERROR("digest is NULL"); + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + g_checksum_get_digest(pMd5, digest, &temp); + */ + + digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, auth_size); + _DEBUG_INFO("md5[base64[md5[username:password]]] = %s", digest); + + free(auth); + *cred = g_base64_encode(digest, 16); + free(digest); + _DEBUG_INFO("base64[md5[base64[md5[username:password]]]] = %s", *cred); + if (*cred == NULL) { + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + } + break; + case AUTH_TYPE_UNKNOWN: + break; + + } + } + + error: + + _EXTERN_FUNC_EXIT; + return errortype; +} + +void set_xml_to_file(char *xml, const char *path) +{ + _EXTERN_FUNC_ENTER; + + FILE *pFile = NULL; + + if (xml != NULL) + pFile = fopen(path, "a"); + + retm_if(pFile == NULL, "pFile is NULL"); + + fputs("==================================================================================", pFile); + fputs("\n", pFile); + + if (xml != NULL) + fputs(xml, pFile); + + if (xml != NULL) + fclose(pFile); + + _EXTERN_FUNC_EXIT; +} |