upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / src / ServiceAdapter / SA_Util.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26
27 /*
28  * For any sort of issue you concern as to this software,
29  * you may use following point of contact.
30  * All resources contributed on this software
31  * are orinigally written by S-Core Inc., a member of Samsung Group.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @SA_Util.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of utility function
40  */
41
42 #include "agent-framework/securityAssistant/EXT_SA_Encryption_Decryption.h"
43 #include "agent-framework/Utility/fw_log.h"
44 #include "ServiceAdapter/SA_Util.h"
45
46 #define LOG_TAG "OMA_DS_SA"
47
48 void put_into_list(GList **commands, GList **commands_last, void *pCommand)
49 {
50         GList *temp = NULL;
51         if (*commands_last == NULL) {
52                 *commands_last = *commands = g_list_append(*commands, pCommand);
53         } else {
54                 temp = g_list_append(*commands_last, pCommand);
55                 *commands_last = g_list_next(*commands_last);
56         }
57 }
58
59 SA_ErrorType create_cred_string(AuthType type,   const char *username,  const char *password,
60                                                                                                         const char *nonce, unsigned int nonce_size, char **pCred)
61 {
62         FW_LOGV("start");
63         SA_ErrorType errortype = SA_INTERNAL_OK;
64
65         switch (type) {
66                 case AUTH_TYPE_BASIC:
67                 {
68                         char *plain = g_strjoin(":", username, password, NULL);
69                         *pCred = g_base64_encode((unsigned char *) plain, strlen(plain));
70                         if (*pCred == NULL) {
71                                 free(plain);
72                                 errortype = SA_INTERNAL_NO_MEMORY;
73                                 goto error;
74                         }
75                         free(plain);
76
77                         break;
78                 }
79                 case AUTH_TYPE_MD5:
80                 {
81                         /* How does syncml:auth-md5 works?
82                          *
83                          * base64(
84                          *        md5(
85                          *            base64(
86                          *                   md5(
87                          *                       username + ":" + password
88                          *                     )
89                          *                 ) +
90                          *            ":" + nonce
91                          *          )
92                          *      )
93                          */
94
95                         /* Let's determine the string for the comparison. */
96                         char *auth = g_strjoin(":", username, password, NULL);
97                         FW_LOGV("username:password = %s", auth);
98                         unsigned char *digest = NULL;
99                         digest = SA_Get_Encryption_Value(SA_ENCRYPTION_MD5, auth, strlen(auth));
100                         free(auth);
101                         *pCred = g_base64_encode(digest, 16);
102                         free(digest);
103                         if (*pCred == NULL) {
104                                 errortype = SA_INTERNAL_NO_MEMORY;
105                                 goto error;
106                         }
107
108                         if (nonce != NULL) {
109                                 FW_LOGV("nonce = %s", nonce);
110                                 FW_LOGV("nonce_size = %d", nonce_size);
111
112                                 int auth_size = strlen(*pCred) + nonce_size + 1;
113                                 auth = (char *)calloc(auth_size + 1, sizeof(char));
114                                 memcpy(auth, *pCred, strlen(*pCred));
115                                 auth[strlen(*pCred)] = ':';
116                                 memcpy(auth + strlen(*pCred) + 1, nonce, nonce_size);
117                                 FW_LOGV("base64[md5[username:password]] = %s", *pCred);
118                                 FW_LOGV("before last base64 encoding = %s", auth);
119                                 free(*pCred);
120
121                                 /*MD5GetDigest (auth, strlen(auth), digest);*/
122
123                                 /*
124                                 GChecksum* pMd5 = g_checksum_new(G_CHECKSUM_MD5);
125                                 g_checksum_update(pMd5, auth, auth_size);
126                                 gsize temp = 16;
127                                 digest = (unsigned char*)calloc(16, sizeof(unsigned char));
128                                 g_checksum_get_digest(pMd5, digest, &temp);
129                                 */
130
131                                 digest = SA_Get_Encryption_Value(SA_ENCRYPTION_MD5, auth, auth_size);
132                                 FW_LOGV("md5[base64[md5[username:password]]] = %s", digest);
133
134                                 free(auth);
135                                 *pCred = g_base64_encode(digest, 16);
136                                 free(digest);
137                                 FW_LOGV("base64[md5[base64[md5[username:password]]]] = %s", *pCred);
138                                 if (*pCred == 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         FW_LOGV("end");
151         return errortype;
152
153 error:
154         FW_LOGE("error : %d", errortype);
155         return errortype;
156 }
157
158 void  set_xml_to_file(char *xml, const char *path)
159 {
160         FILE *pFile = NULL;
161
162         if (xml != NULL)
163                 pFile = fopen(path, "a");
164
165         if (pFile == NULL)
166                 return;
167
168         fputs("==================================================================================", pFile);
169         fputs("\n", pFile);
170
171         if (xml != NULL)
172                 fputs(xml, pFile);
173
174         if (xml != NULL)
175                 fclose(pFile);
176 }