summaryrefslogtreecommitdiff
path: root/src/agent/service-adapter/sa_util.c
blob: 6538765bbefb088f6acdf559636b858bad82df96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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;
}