summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/skylogin/skylogin.c
blob: 7eff38a1a8e74389e968f723db5b1d4e8cca4a4e (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
/*  
 * Skype Login
 * 
 * Based on:
 *   FakeSkype : Skype reverse engineering proof-of-concept client
 *               Ouanilo MEDEGAN (c) 2006   http://www.oklabs.net
 *   pyskype   : Skype login Python script by uunicorn
 *
 * Written by:   leecher@dose.0wnz.at (c) 2015 
 *
 * Module:       Main module which contains public functions of library
 *
 */
#ifdef _WIN32
#define EXPORT
#define strcasecmp stricmp
#endif
#include <time.h>
#include "common.h"
#include "login.h"
#include "platform.h"
#include "uic.h"
#include "objects.h"
#include "credentials.h"
#include "skylogin.h"

EXPORT SkyLogin SkyLogin_Init()
{
	Skype_Inst *pInst = calloc(1, sizeof(Skype_Inst));

#ifdef _WIN32
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD( 2, 2 );
	WSAStartup( wVersionRequested, &wsaData);
#endif

	if (pInst) InitInstance(pInst);
	return (SkyLogin*)pInst;
}

EXPORT void SkyLogin_Exit(SkyLogin pPInst)
{
	Skype_Inst *pInst = (Skype_Inst*)pPInst;
	if (pInst->LoginD.User) free(pInst->LoginD.User);
	if (pInst->LoginD.RSAKeys) RSA_free(pInst->LoginD.RSAKeys);
	if (pInst->LoginD.SignedCredentials.Memory) free(pInst->LoginD.SignedCredentials.Memory);
	free(pInst);
}

EXPORT void SkyLogin_SetLogFunction(SkyLogin pPInst, int (__cdecl *pfLog)(void *stream, const char *format, ...), void *pLogStream)
{
	Skype_Inst *pInst = (Skype_Inst*)pPInst;

	pInst->pfLog = pfLog;
	pInst->pLogStream = pLogStream;
}

EXPORT int SkyLogin_LoadCredentials(SkyLogin pPInst, char *User)
{
	Skype_Inst *pInst = (Skype_Inst*)pPInst;

	Memory_U creds = Credentials_Load(User);
	int ret = 0;

	if (creds.Memory)
	{
		// Credentials were found and loaded, now let's parse them.
		SResponse LoginDatas;

		if (Credentials_Read(pInst, creds, &LoginDatas) == 0)
		{
			// Credentials were successfully read :)
			// Now verify if they are still valid
			uint Idx;
			time_t t;

			for (Idx = 0, ret = 1; ret && Idx < LoginDatas.NbObj; Idx++)
			{
				switch (LoginDatas.Objs[Idx].Id)
				{
				case OBJ_ID_LDUSER:
					// Credentials for wrong user?
					ret = !strcasecmp((char*)LoginDatas.Objs[Idx].Value.Memory.Memory, User);
					if (pInst->LoginD.User) free(pInst->LoginD.User);
					pInst->LoginD.User = strdup((char*)LoginDatas.Objs[Idx].Value.Memory.Memory);
					break;
				case OBJ_ID_LDEXPIRY:
					// Credentials expired?
					ret = (int)LoginDatas.Objs[Idx].Value.Nbr * 60 > time(&t)-60;
					break;
				}
			}
			FreeResponse(&LoginDatas);
		}
		free(creds.Memory);
	}
	return ret; 
}

EXPORT int SkyLogin_PerformLogin(SkyLogin pPInst, char *User, char *Pass)
{
	int ret;
	Skype_Inst *pInst = (Skype_Inst*)pPInst;

	if ((ret = PerformLogin(pInst, User, Pass)) > 0)
	{
		// On successful login, save login datas
		Memory_U creds = Credentials_Write(pInst);
		if (creds.Memory)
		{
			Credentials_Save(creds, User);
			free (creds.Memory);
		}

		if (pInst->LoginD.User) free(pInst->LoginD.User);
		pInst->LoginD.User = strdup(User);
	}
	return ret;
}

EXPORT int SkyLogin_PerformLoginOAuth(SkyLogin pPInst, const char *OAuth)
{
	int ret;
	Skype_Inst *pInst = (Skype_Inst*)pPInst;

	if ((ret = PerformLogin(pInst, OAuth, NULL)) > 0)
	{
		// On successful login, save login datas
		Memory_U creds = Credentials_Write(pInst);
		if (creds.Memory)
		{
			SResponse LoginDatas;

			// We don't know user name, so read it from Credentials
			if (Credentials_Parse(pInst, pInst->LoginD.SignedCredentials, &LoginDatas) == 0)
			{
				uint Idx;

				for (Idx = 0; Idx < LoginDatas.NbObj; Idx++)
				{ 
					if (LoginDatas.Objs[Idx].Id == OBJ_ID_LDUSER)
					{
						Credentials_Save(creds, (char*)LoginDatas.Objs[Idx].Value.Memory.Memory);
						if (pInst->LoginD.User) free(pInst->LoginD.User);
						pInst->LoginD.User = (uchar*)strdup((char*)LoginDatas.Objs[Idx].Value.Memory.Memory);
						break;
					}
				}
				FreeResponse(&LoginDatas);
			}
			free (creds.Memory);
		}
	}
	return ret;
}


EXPORT int SkyLogin_CreateUICString(SkyLogin pInst, const char *pszNonce, char *pszOutUIC)
{
	return CreateUICString((Skype_Inst*)pInst, pszNonce, "WS-SecureConversationSESSION KEY TOKEN", pszOutUIC);
}

EXPORT int SkyLogin_GetCredentialsUIC(SkyLogin pInst, char *pszOutUIC)
{
	return GetCredentialsUIC((Skype_Inst*)pInst, pszOutUIC);
}

EXPORT char *SkyLogin_GetUser(SkyLogin pInst)
{
	return (char*)((Skype_Inst*)pInst)->LoginD.User;
}