summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/skylogin/credentials.c
blob: df80f2cafb81f157f93203e9a4b7c4ae9a6af9c7 (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
/*  
 * 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:       Credentials handling, reads/writes old Skype credentials format
 *
 */
#include "common.h"
#include "objects.h"
#include "random.h"
#include "crc.h"
#include "credentials.h"
#ifdef _WIN32
#define CRC_SIZE sizeof(short)
#else
#define CRC_SIZE 0
#endif


Memory_U Credentials_Write(Skype_Inst *pInst)
{
	Memory_U creds;
	uchar *Browser;
	uint Crc;

	creds.MsZ = sizeof(pInst->LoginD.LoginHash) + MODULUS_SZ + pInst->LoginD.SignedCredentials.MsZ + CRC_SIZE;
	if (!(Browser = creds.Memory = malloc(creds.MsZ)))
	{
		creds.MsZ = 0;
		return creds;
	}
	memcpy(Browser, pInst->LoginD.LoginHash, sizeof(pInst->LoginD.LoginHash));
	Browser+=sizeof(pInst->LoginD.LoginHash);
	BN_bn2bin(pInst->LoginD.RSAKeys->d, Browser);
	Browser+=MODULUS_SZ;
	memcpy(Browser, pInst->LoginD.SignedCredentials.Memory, pInst->LoginD.SignedCredentials.MsZ);
	Browser+=pInst->LoginD.SignedCredentials.MsZ;
#ifdef _WIN32
	Crc = crc32(creds.Memory, Browser-creds.Memory, -1);
	*Browser++ = *((uchar *)(&Crc) + 0);
	*Browser++ = *((uchar *)(&Crc) + 1);
#endif
	return creds;
}

/*
	 0	-	OK
	-1	-	Invalid size of credentials blob
	-2	-	Invalid CRC
	-3	-	Out of memory
	-4	-	Unable to parse Signed credentials
	-5	-	RSA public key not found
*/
int Credentials_Read(Skype_Inst *pInst, Memory_U creds, SResponse *LoginDatas)
{
	uchar *Browser;
	uint Crc, Idx;
	RSA	  *Keys;

	if (creds.MsZ < sizeof(pInst->LoginD.LoginHash) + MODULUS_SZ + 16 + CRC_SIZE)
		return -1;

#ifdef _WIN32
	Crc = crc32(creds.Memory, creds.MsZ-2, -1);
	if (*((uchar *)(&Crc) + 0) != *((uchar*)creds.Memory+creds.MsZ-2) ||
		*((uchar *)(&Crc) + 1) != *((uchar*)creds.Memory+creds.MsZ-1))
	{
		pInst->pfLog(pInst->pLogStream, "Credentials: Bad CRC!");
		return -2;
	}
#endif

	Browser = creds.Memory;
	memcpy (pInst->LoginD.LoginHash, Browser, sizeof(pInst->LoginD.LoginHash));
	Browser+=sizeof(pInst->LoginD.LoginHash);
	Keys=RSA_new();
	BN_hex2bn(&(Keys->e), "010001");
	Keys->d = BN_bin2bn(Browser, MODULUS_SZ, NULL);
	Browser+=MODULUS_SZ;
	if (pInst->LoginD.SignedCredentials.Memory) free(pInst->LoginD.SignedCredentials.Memory);
	pInst->LoginD.SignedCredentials.MsZ = creds.MsZ - CRC_SIZE - (Browser-creds.Memory);
	if (!(pInst->LoginD.SignedCredentials.Memory = malloc(pInst->LoginD.SignedCredentials.MsZ)))
	{
		pInst->LoginD.SignedCredentials.MsZ = 0;
		RSA_free(Keys);
		return -3;
	}
	memcpy(pInst->LoginD.SignedCredentials.Memory, Browser, pInst->LoginD.SignedCredentials.MsZ);

	// Now credentials are read, but we need to finish LoginD.RSAKeys by unpacking Signed Credentials
	if (Credentials_Parse(pInst, pInst->LoginD.SignedCredentials, LoginDatas)<0)
	{
		RSA_free(Keys);
		return -4;
	}

	for (Idx = 0; Idx < LoginDatas->NbObj; Idx++)
	{
		if (LoginDatas->Objs[Idx].Id == OBJ_ID_LDMODULUS)
		{
			Keys->n = BN_bin2bn(LoginDatas->Objs[Idx].Value.Memory.Memory, 
				LoginDatas->Objs[Idx].Value.Memory.MsZ, NULL);
			if (pInst->LoginD.RSAKeys) RSA_free(pInst->LoginD.RSAKeys);
			pInst->LoginD.RSAKeys = Keys;
			return 0;
		}
	}

	RSA_free(Keys);
	return -5;
}

int Credentials_Parse(Skype_Inst *pInst, Memory_U Pcred, SResponse *LoginDatas)
{
	uchar	*PostProcessed, *Browser;
	char	*Key;
	uint	KeyIdx, PPsZ;
	RSA		*SkypeRSA;
	Memory_U creds = Pcred;

	if (!(creds.Memory = malloc(creds.MsZ)))
		return -2;
	KeyIdx = htonl(*(uint *)Pcred.Memory);
	Pcred.Memory += sizeof(uint);
	Pcred.MsZ -= sizeof(uint);
	
	SkypeRSA = RSA_new();
	Key = KeySelect(KeyIdx);
	BN_hex2bn(&(SkypeRSA->n), Key);
	BN_hex2bn(&(SkypeRSA->e), "010001");
	PPsZ = RSA_public_decrypt(Pcred.MsZ, Pcred.Memory, creds.Memory, SkypeRSA, RSA_NO_PADDING);
	RSA_free(SkypeRSA);
	if (PPsZ == -1)
	{
		char szErr[256];

		ERR_load_crypto_strings();
		ERR_error_string(ERR_get_error(), szErr);
		pInst->pfLog(pInst->pLogStream, "Credentials decryption failed: %s\n", szErr);
		free(creds.Memory);
		return -1;
	}

	PostProcessed = FinalizeLoginDatas(pInst, creds.Memory, &PPsZ, NULL, 0);

	if (PostProcessed == NULL)
	{
		pInst->pfLog(pInst->pLogStream, "Bad Datas Finalization..\n");
		free (creds.Memory);
		return -1;
	}

	LoginDatas->Objs = NULL;
	LoginDatas->NbObj = 0;
	Browser = PostProcessed;

	ManageObjects(&Browser, PPsZ, LoginDatas);
	free(PostProcessed);
	free(creds.Memory);
	return 0;
}