summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/WALogin.h
blob: 2949213e5386873d4a28896d5c75b3bbfac61efe (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
/*
 * WALogin.h
 *
 *  Created on: 26/06/2012
 *      Author: Antonio
 */

#ifndef WALOGIN_H_
#define WALOGIN_H_

#include "BinTreeNodeReader.h"
#include "BinTreeNodeWriter.h"
#include "WAConnection.h"
#include <string>

#include "../OpenSSL/rc4.h"
#include "../OpenSSL/hmac.h"

class WAConnection;
class BinTreeNodeReader;
class BinTreeNodeWriter;

class KeyStream {
private:
	RC4_KEY rc4;
	unsigned char key[20], keyMac[20];
	int seq;
	HMAC_CTX hmac;

	void hmacsha1(unsigned char* text, int textLength, unsigned char *out);

public:
	KeyStream(unsigned char* _key, unsigned char* _keyMac);
	~KeyStream();

	static void keyFromPasswordAndNonce(const std::string& pass, const std::vector<unsigned char>& nonce, unsigned char *out);
	void decodeMessage(unsigned char* buffer, int macOffset, int offset, const int length);
	void encodeMessage(unsigned char* buffer, int macOffset, int offset, const int length);
};


class WALogin {
private:
	static const std::string NONCE_KEY;
	KeyStream* outputKey;
	WAConnection* connection;
	BinTreeNodeReader* inn;
	BinTreeNodeWriter* out;

	std::vector<unsigned char>* getAuthBlob(const std::vector<unsigned char>& nonce);
	void sendResponse(const std::vector<unsigned char>& challengeData);
	void sendFeatures();
	void sendAuth(const std::vector<unsigned char>& nonce);
	std::vector<unsigned char>* readFeaturesUntilChallengeOrSuccess();
	void parseSuccessNode(ProtocolTreeNode* node);
	std::vector<unsigned char> readSuccess();

public:
	std::string user;
	std::string domain;
	std::string password;
	std::string resource;
	std::string push_name;
	bool supports_receipt_acks;
	time_t expire_date;
	int account_kind;

	WALogin(WAConnection* connection, BinTreeNodeReader *reader, BinTreeNodeWriter *writer, const std::string& domain, const std::string& user, const std::string& resource, const std::string& password, const std::string& push_name);
	std::vector<unsigned char>* login(const std::vector<unsigned char>& blobLength);
	BinTreeNodeReader *getTreeNodeReader();
	BinTreeNodeWriter *getTreeNodeWriter();
	virtual ~WALogin();
};

#endif /* WALOGIN_H_ */