3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * @brief This file is the source file of implementation of utility function
24 #include <sync_agent.h>
26 #include "service-adapter/sa_util.h"
28 #ifndef OMADS_AGENT_LOG
30 #define LOG_TAG "OMA_DS_SA"
33 void put_into_list(GList ** commands, GList ** commands_last, void *command)
38 if (*commands_last == NULL) {
39 *commands_last = *commands = g_list_append(*commands, command);
41 temp = g_list_append(*commands_last, command);
42 *commands_last = g_list_next(*commands_last);
48 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)
52 sa_error_type_e errortype = SA_INTERNAL_OK;
57 char *plain = g_strjoin(":", user_name, password, NULL);
58 *cred = g_base64_encode((unsigned char *)plain, strlen(plain));
61 errortype = SA_INTERNAL_NO_MEMORY;
70 /* How does syncml:auth-md5 works?
76 * username + ":" + password
84 /* Let's determine the string for the comparison. */
85 char *auth = g_strjoin(":", user_name, password, NULL);
86 _DEBUG_INFO("username:password = %s", auth);
87 unsigned char *digest = NULL;
88 digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, strlen(auth));
90 *cred = g_base64_encode(digest, 16);
93 errortype = SA_INTERNAL_NO_MEMORY;
98 _DEBUG_INFO("nonce = %s", nonce);
99 _DEBUG_INFO("nonce_size = %d", nonce_size);
101 int auth_size = strlen(*cred) + nonce_size + 1;
102 auth = (char *)calloc(auth_size + 1, sizeof(char));
104 _DEBUG_ERROR("auth is NULL");
105 errortype = SA_INTERNAL_NO_MEMORY;
109 memcpy(auth, *cred, strlen(*cred));
110 auth[strlen(*cred)] = ':';
111 memcpy(auth + strlen(*cred) + 1, nonce, nonce_size);
112 _DEBUG_INFO("base64[md5[username:password]] = %s", *cred);
113 _DEBUG_INFO("before last base64 encoding = %s", auth);
116 /*MD5GetDigest (auth, strlen(auth), digest); */
119 GChecksum* pMd5 = g_checksum_new(G_CHECKSUM_MD5);
120 g_checksum_update(pMd5, auth, auth_size);
122 digest = (unsigned char*)calloc(16, sizeof(unsigned char));
123 if (digest == NULL) {
124 _DEBUG_ERROR("digest is NULL");
125 errortype = SA_INTERNAL_NO_MEMORY;
128 g_checksum_get_digest(pMd5, digest, &temp);
131 digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, auth_size);
132 _DEBUG_INFO("md5[base64[md5[username:password]]] = %s", digest);
135 *cred = g_base64_encode(digest, 16);
137 _DEBUG_INFO("base64[md5[base64[md5[username:password]]]] = %s", *cred);
139 errortype = SA_INTERNAL_NO_MEMORY;
144 case AUTH_TYPE_UNKNOWN:
156 void set_xml_to_file(char *xml, const char *path)
163 pFile = fopen(path, "a");
165 retm_if(pFile == NULL, "pFile is NULL");
167 fputs("==================================================================================", pFile);