File: password.c

package info (click to toggle)
libsynce 0.9.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,744 kB
  • ctags: 322
  • sloc: sh: 8,953; ansic: 2,504; makefile: 99
file content (134 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download
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
/* $Id: password.c,v 1.4 2003/12/22 12:50:43 twogood Exp $ */
#include "synce.h"
#include "synce_log.h"
#include "synce_socket.h"
#include <string.h>

/**
 * Free an encoded password returned from synce_password_encode()
 */
static void synce_password_free(unsigned char *encodedPassword)
{
	wstr_free_string(encodedPassword);
}

/**
 * Encode a password with a key
 */
static bool synce_password_encode(
		const char *asciiPassword,
		unsigned char key,
		unsigned char **encodedPassword,
		size_t *encodedPasswordSize)
{	
	int length;
	unsigned i;
	
	*encodedPassword = NULL;
	
	if (!asciiPassword)
	{
		synce_error("password parameter is NULL");
		goto error;
	}

	length = strlen(asciiPassword);

	*encodedPassword      = (unsigned char*)wstr_from_ascii(asciiPassword);
	*encodedPasswordSize  = 2 * (length + 1);

	for (i = 0; i < *encodedPasswordSize; i++)
	{
		(*encodedPassword)[i] ^= key;
	}

	return true;

error:
	synce_password_free(*encodedPassword);
	*encodedPassword = NULL;
	return false;
}

/**
 * Encode and send password on a socket
 */
bool synce_password_send(
		SynceSocket* socket,
		const char* asciiPassword,
		unsigned char key)
{
	bool success = false;
	unsigned char* encoded_password = NULL;
	size_t size = 0;
	uint16_t size_le = 0;
	
	if (!synce_password_encode(asciiPassword, key, &encoded_password, &size))
	{
		synce_error("failed to encode password");
	}

	size_le = htole16((uint16_t)size);

	if ( !synce_socket_write(socket, &size_le, sizeof(uint16_t)) )
	{
		synce_error("failed to write buffer size to socket");
		goto exit;
	}

	if ( !synce_socket_write(socket, encoded_password, size) )
	{
		synce_error("failed to write encoded password to socket");
		goto exit;
	}

	success = true;

exit:
	synce_password_free(encoded_password);
	return success;
}

bool synce_password_recv_reply(
		SynceSocket* socket,
		size_t size,
		bool* passwordCorrect)
{
	bool success = false;
	union
	{
		uint8_t   byte;
		uint16_t  word;
	} reply;

	if (size < 1 || size > 2)
	{
		synce_error("invalid size");
		goto exit;	
	}

	if (!synce_socket_read(socket, &reply, size))
	{
		synce_error("failed to read password reply");
		goto exit;	
	}

	if (size == 1)
	{
		synce_trace("password reply = 0x%02x (%i)", reply.byte, reply.byte);
		*passwordCorrect = reply.byte;
	}
	else /* size == 2 */
	{
		reply.word = letoh16(reply.word);
		synce_trace("password reply = 0x%04x (%i)", reply.word, reply.word);
		*passwordCorrect = reply.word;
	}

	synce_trace("Password was %s", *passwordCorrect ? "correct!" : "incorrect :-(");
	success = true;

exit:
	return success;
}