Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / service-adapter / sa_util.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *   @SA_Util.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of utility function
22  */
23
24 #include <sync_agent.h>
25
26 #include "service-adapter/sa_util.h"
27
28 #ifndef OMADS_AGENT_LOG
29 #undef LOG_TAG
30 #define LOG_TAG "OMA_DS_SA"
31 #endif
32
33 void put_into_list(GList ** commands, GList ** commands_last, void *command)
34 {
35         _EXTERN_FUNC_ENTER;
36
37         GList *temp = NULL;
38         if (*commands_last == NULL) {
39                 *commands_last = *commands = g_list_append(*commands, command);
40         } else {
41                 temp = g_list_append(*commands_last, command);
42                 *commands_last = g_list_next(*commands_last);
43         }
44
45         _EXTERN_FUNC_EXIT;
46 }
47
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)
49 {
50         _EXTERN_FUNC_ENTER;
51
52         sa_error_type_e errortype = SA_INTERNAL_OK;
53
54         switch (type) {
55         case AUTH_TYPE_BASIC:
56                 {
57                         char *plain = g_strjoin(":", user_name, password, NULL);
58                         *cred = g_base64_encode((unsigned char *)plain, strlen(plain));
59                         if (*cred == NULL) {
60                                 free(plain);
61                                 errortype = SA_INTERNAL_NO_MEMORY;
62                                 goto error;
63                         }
64                         free(plain);
65
66                         break;
67                 }
68         case AUTH_TYPE_MD5:
69                 {
70                         /* How does syncml:auth-md5 works?
71                          *
72                          * base64(
73                          *        md5(
74                          *            base64(
75                          *                   md5(
76                          *                       username + ":" + password
77                          *                     )
78                          *                 ) +
79                          *            ":" + nonce
80                          *          )
81                          *      )
82                          */
83
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));
89                         free(auth);
90                         *cred = g_base64_encode(digest, 16);
91                         free(digest);
92                         if (*cred == NULL) {
93                                 errortype = SA_INTERNAL_NO_MEMORY;
94                                 goto error;
95                         }
96
97                         if (nonce != NULL) {
98                                 _DEBUG_INFO("nonce = %s", nonce);
99                                 _DEBUG_INFO("nonce_size = %d", nonce_size);
100
101                                 int auth_size = strlen(*cred) + nonce_size + 1;
102                                 auth = (char *)calloc(auth_size + 1, sizeof(char));
103                                 if (auth == NULL) {
104                                         _DEBUG_ERROR("auth is NULL");
105                                         errortype = SA_INTERNAL_NO_MEMORY;
106                                         goto error;
107                                 }
108
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);
114                                 free(*cred);
115
116                                 /*MD5GetDigest (auth, strlen(auth), digest); */
117
118                                 /*
119                                    GChecksum* pMd5 = g_checksum_new(G_CHECKSUM_MD5);
120                                    g_checksum_update(pMd5, auth, auth_size);
121                                    gsize temp = 16;
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;
126                                    goto error;
127                                    }
128                                    g_checksum_get_digest(pMd5, digest, &temp);
129                                  */
130
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);
133
134                                 free(auth);
135                                 *cred = g_base64_encode(digest, 16);
136                                 free(digest);
137                                 _DEBUG_INFO("base64[md5[base64[md5[username:password]]]] = %s", *cred);
138                                 if (*cred == NULL) {
139                                         errortype = SA_INTERNAL_NO_MEMORY;
140                                         goto error;
141                                 }
142                         }
143                         break;
144         case AUTH_TYPE_UNKNOWN:
145                         break;
146
147                 }
148         }
149
150  error:
151
152         _EXTERN_FUNC_EXIT;
153         return errortype;
154 }
155
156 void set_xml_to_file(char *xml, const char *path)
157 {
158         _EXTERN_FUNC_ENTER;
159
160         FILE *pFile = NULL;
161
162         if (xml != NULL)
163                 pFile = fopen(path, "a");
164
165         retm_if(pFile == NULL, "pFile is NULL");
166
167         fputs("==================================================================================", pFile);
168         fputs("\n", pFile);
169
170         if (xml != NULL)
171                 fputs(xml, pFile);
172
173         if (xml != NULL)
174                 fclose(pFile);
175
176         _EXTERN_FUNC_EXIT;
177 }