Skip to content

Commit

Permalink
Merge pull request #1384 from meganz/release/v3.4.9
Browse files Browse the repository at this point in the history
Version 3.4.9
  • Loading branch information
sergiohs84 authored Mar 18, 2019
2 parents 2c7713d + f7d85f6 commit 19222dd
Show file tree
Hide file tree
Showing 33 changed files with 255 additions and 152 deletions.
27 changes: 25 additions & 2 deletions bindings/java/nz/mega/sdk/MegaApiJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ public static String userHandleToBase64(long handle) {
* @param size
* Size of the byte array (in bytes).
*/
public static void addEntropy(String data, long size) {
MegaApi.addEntropy(data, size);
public void addEntropy(String data, long size) {
megaApi.addEntropy(data, size);
}

/**
Expand Down Expand Up @@ -6930,6 +6930,29 @@ public MegaNode authorizeNode(MegaNode node){
return megaApi.authorizeNode(node);
}

/**
*
* Returns a MegaNode that can be downloaded/copied with a chat-authorization
*
* During preview of chat-links, you need to call this method to authorize the MegaNode
* from a node-attachment message, so the API allows to access to it. The parameter to
* authorize the access can be retrieved from MegaChatRoom::getAuthorizationToken when
* the chatroom in in preview mode.
*
* You can use MegaApi::startDownload and/or MegaApi::copyNode with the resulting
* node with any instance of MegaApi, even if it's logged into another account,
* a public folder, or not logged in.
*
* You take the ownership of the returned value.
*
* @param node MegaNode to authorize
* @param cauth Authorization token (public handle of the chatroom in B64url encoding)
* @return Authorized node, or NULL if the node can't be authorized
*/
public MegaNode authorizeChatNode(MegaNode node, String cauth){
return megaApi.authorizeChatNode(node, cauth);
}

/**
* Get the SDK version.
*
Expand Down
4 changes: 2 additions & 2 deletions examples/megacli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ class TreeProcCopy_mcli : public TreeProc
else
{
byte buf[FOLDERNODEKEYLENGTH];
PrnGen::genblock(buf, sizeof buf);
client->rng.genblock(buf, sizeof buf);
t->nodekey.assign((char*) buf, FOLDERNODEKEYLENGTH);
}

Expand Down Expand Up @@ -3871,7 +3871,7 @@ static void process_line(char* l)
newnode->parenthandle = UNDEF;

// generate fresh random key for this folder node
PrnGen::genblock(buf, FOLDERNODEKEYLENGTH);
client->rng.genblock(buf, FOLDERNODEKEYLENGTH);
newnode->nodekey.assign((char*) buf, FOLDERNODEKEYLENGTH);
key.setkey(buf);

Expand Down
5 changes: 3 additions & 2 deletions include/mega/backofftimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MEGA_API BackoffTimer
dstime next;
dstime delta;
dstime base;
PrnGen &rng;

public:
// reset timer
Expand Down Expand Up @@ -63,15 +64,15 @@ class MEGA_API BackoffTimer
// update time to wait
void update(dstime*);

BackoffTimer();
BackoffTimer(PrnGen &rng);
};


class MEGA_API TimerWithBackoff: public BackoffTimer {

public:
int tag;
TimerWithBackoff(int tag);
TimerWithBackoff(PrnGen &rng, int tag);
};

} // namespace
Expand Down
14 changes: 7 additions & 7 deletions include/mega/crypto/cryptopp.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ using std::string;
/**
* @brief A generic pseudo-random number generator.
*/
class MEGA_API PrnGen
class MEGA_API PrnGen : public CryptoPP::AutoSeededRandomPool
{
public:
static CryptoPP::AutoSeededRandomPool rng;

/**
* @brief Generates a block of random bytes of length `len` into a buffer
* `buf`.
Expand All @@ -58,15 +56,15 @@ class MEGA_API PrnGen
* @param len The number of random bytes to generate.
* @return Void.
*/
static void genblock(byte* buf, int len);
void genblock(byte* buf, int len);

/**
* @brief Generates a random integer between 0 ... max - 1.
*
* @param max The maximum of which the number is to generate under.
* @return The random number generated.
*/
static uint32_t genuint32(uint64_t max);
uint32_t genuint32(uint64_t max);
};

// symmetric cryptography: AES-128
Expand Down Expand Up @@ -307,13 +305,14 @@ class MEGA_API AsymmCipher
/**
* @brief Encrypts a randomly padded plain text into a buffer.
*
* @param rng Reference to the random block generator
* @param plain The plain text to encrypt.
* @param plainlen Length of the plain text.
* @param buf Buffer to take the cipher text..
* @param buflen Length of the cipher text.
* @return Number of bytes encrypted, 0 on failure.
*/
int encrypt(const byte* plain, int plainlen, byte* buf, int buflen);
int encrypt(PrnGen &rng, const byte* plain, int plainlen, byte* buf, int buflen);

/**
* @brief Decrypts a cipher text into a buffer and strips random padding.
Expand Down Expand Up @@ -374,12 +373,13 @@ class MEGA_API AsymmCipher
/**
* @brief Generates an RSA key pair of a given key size.
*
* @param rng Reference to the random block generator
* @param privk Private key.
* @param pubk Public key.
* @param size Size of key to generate in bits (key strength).
* @return Always returns 1.
*/
void genkeypair(CryptoPP::Integer* privk, CryptoPP::Integer* pubk, int size);
void genkeypair(PrnGen &rng, CryptoPP::Integer* privk, CryptoPP::Integer* pubk, int size);
};

class MEGA_API Hash
Expand Down
4 changes: 3 additions & 1 deletion include/mega/crypto/sodium.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace mega {

class PrnGen;

/**
* @brief Asymmetric cryptographic signature using EdDSA with Edwards 25519.
*/
Expand All @@ -41,7 +43,7 @@ class MEGA_API EdDSA
static const string TLV_KEY;
bool initializationOK;

EdDSA(unsigned char* keySeed = NULL);
EdDSA(PrnGen &rng, unsigned char* keySeed = NULL);
~EdDSA();

unsigned char keySeed[SEED_KEY_LENGTH];
Expand Down
5 changes: 3 additions & 2 deletions include/mega/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace mega {
class MEGA_API DbTable
{
static const int IDSPACING = 16;
PrnGen &rng;

public:
// for a full sequential get: rewind to first record
Expand Down Expand Up @@ -67,7 +68,7 @@ class MEGA_API DbTable
// autoincrement
uint32_t nextid;

DbTable();
DbTable(PrnGen &rng);
virtual ~DbTable() { }
};

Expand All @@ -77,7 +78,7 @@ struct MEGA_API DbAccess
static const int DB_VERSION = LEGACY_DB_VERSION + 1;

DbAccess();
virtual DbTable* open(FileSystemAccess*, string*, bool = false) = 0;
virtual DbTable* open(PrnGen &rng, FileSystemAccess*, string*, bool = false) = 0;

virtual ~DbAccess() { }

Expand Down
4 changes: 2 additions & 2 deletions include/mega/db/sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MEGA_API SqliteDbAccess : public DbAccess
string dbpath;

public:
DbTable* open(FileSystemAccess*, string*, bool = false);
DbTable* open(PrnGen &rng, FileSystemAccess*, string*, bool = false);

SqliteDbAccess(string* = NULL);
~SqliteDbAccess();
Expand All @@ -56,7 +56,7 @@ class MEGA_API SqliteDbTable : public DbTable
void abort();
void remove();

SqliteDbTable(sqlite3*, FileSystemAccess *fs, string *filepath);
SqliteDbTable(PrnGen &rng, sqlite3*, FileSystemAccess *fs, string *filepath);
~SqliteDbTable();
};
} // namespace
Expand Down
10 changes: 6 additions & 4 deletions include/mega/fileattributefetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace mega {
// file attribute fetching for a specific source cluster
struct MEGA_API FileAttributeFetchChannel
{
MegaClient *client;

handle fahref;

BackoffTimer bt;
Expand All @@ -45,15 +47,15 @@ struct MEGA_API FileAttributeFetchChannel
error e;

// dispatch new and retrying attributes by POSTing to existing URL
void dispatch(MegaClient*);
void dispatch();

// parse fetch result and remove completed attributes from pending
void parse(MegaClient*, int, bool);
void parse(int, bool);

// notify app of nodes that failed to receive their requested attribute
void failed(MegaClient*);
void failed();

FileAttributeFetchChannel();
FileAttributeFetchChannel(MegaClient*);
};

// pending individual attribute fetch
Expand Down
2 changes: 1 addition & 1 deletion include/mega/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ struct MEGA_API HttpReq

struct MEGA_API GenericHttpReq : public HttpReq
{
GenericHttpReq(bool = false);
GenericHttpReq(PrnGen &rng, bool = false);

// tag related to the request
int tag;
Expand Down
5 changes: 4 additions & 1 deletion include/mega/megaclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ class MEGA_API MegaClient
// Account has VOIP push enabled (only for Apple)
bool aplvp_enabled;

// pseudo-random number generator
PrnGen rng;

#ifdef ENABLE_CHAT
// all chats
textchat_map chats;
Expand Down Expand Up @@ -793,7 +796,7 @@ class MEGA_API MegaClient
void sc_ph();
void sc_se();
#ifdef ENABLE_CHAT
void sc_chatupdate();
void sc_chatupdate(bool readingPublicChat);
void sc_chatnode();
void sc_chatflags();
#endif
Expand Down
11 changes: 8 additions & 3 deletions include/mega/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct MEGA_API PaddedCBC
* for encryption will be generated and available through the reference.
* @return Void.
*/
static void encrypt(string* data, SymmCipher* key, string* iv = NULL);
static void encrypt(PrnGen &rng, string* data, SymmCipher* key, string* iv = NULL);

/**
* @brief Decrypts a string and strips the padding.
Expand Down Expand Up @@ -141,12 +141,17 @@ class MEGA_API PayCrypter
*/
byte iv[IV_BYTES];

/**
* @brief Random blocks generator
*/
PrnGen &rng;

public:

/**
* @brief Constructor. Initializes keys with random values.
*/
PayCrypter();
PayCrypter(PrnGen &rng);

/**
* @brief Updates the crypto keys (mainly for testing)
Expand Down Expand Up @@ -256,7 +261,7 @@ struct MEGA_API TLVstore
* @param encSetting Block encryption mode to be used by AES
* @return A new string holding the encrypted byte array. You take the ownership of the string.
*/
string *tlvRecordsToContainer(SymmCipher *key, encryptionsetting_t encSetting = AES_GCM_12_16);
string *tlvRecordsToContainer(PrnGen &rng, SymmCipher *key, encryptionsetting_t encSetting = AES_GCM_12_16);

/**
* @brief Converts the TLV records into a byte array
Expand Down
2 changes: 1 addition & 1 deletion include/mega/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#define MEGA_MINOR_VERSION 4
#endif
#ifndef MEGA_MICRO_VERSION
#define MEGA_MICRO_VERSION 8
#define MEGA_MICRO_VERSION 9
#endif
5 changes: 3 additions & 2 deletions include/megaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6204,7 +6204,7 @@ class MegaApi
* @param data Byte array with random data
* @param size Size of the byte array (in bytes)
*/
static void addEntropy(char* data, unsigned int size);
void addEntropy(char* data, unsigned int size);

#ifdef WINDOWS_PHONE
/**
Expand Down Expand Up @@ -11626,10 +11626,11 @@ class MegaApi
* @param parentHandle Handle of the parent node
* @param privateAuth Private authentication token to access the node
* @param publicAuth Public authentication token to access the node
* @param chatAuth Chat authentication token to access the node
* @return MegaNode object
*/
MegaNode *createForeignFileNode(MegaHandle handle, const char *key, const char *name,
int64_t size, int64_t mtime, MegaHandle parentHandle, const char *privateAuth, const char *publicAuth);
int64_t size, int64_t mtime, MegaHandle parentHandle, const char *privateAuth, const char *publicAuth, const char *chatAuth);

/**
* @brief Create a MegaNode that represents a folder of a different account
Expand Down
5 changes: 3 additions & 2 deletions include/megaapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ class MegaApiImpl : public MegaApp
static const char* ebcEncryptKey(const char* encryptionKey, const char* plainKey);
void retryPendingConnections(bool disconnect = false, bool includexfers = false, MegaRequestListener* listener = NULL);
void setDnsServers(const char *dnsServers, MegaRequestListener* listener = NULL);
static void addEntropy(char* data, unsigned int size);
void addEntropy(char* data, unsigned int size);
static string userAttributeToString(int);
static string userAttributeToLongName(int);
static int userAttributeFromString(const char *name);
Expand Down Expand Up @@ -2102,7 +2102,7 @@ class MegaApiImpl : public MegaApp
MegaNodeList* search(const char* searchString, int order = MegaApi::ORDER_NONE);

MegaNode *createForeignFileNode(MegaHandle handle, const char *key, const char *name, m_off_t size, m_off_t mtime,
MegaHandle parentHandle, const char *privateauth, const char *publicauth);
MegaHandle parentHandle, const char *privateauth, const char *publicauth, const char *chatauth);
MegaNode *createForeignFolderNode(MegaHandle handle, const char *name, MegaHandle parentHandle,
const char *privateauth, const char *publicauth);

Expand Down Expand Up @@ -2925,6 +2925,7 @@ class MegaHTTPContext : public MegaTCPContext
m_off_t nodesize;
std::string nodepubauth;
std::string nodeprivauth;
std::string nodechatauth;
int resultCode;


Expand Down
8 changes: 5 additions & 3 deletions src/backofftimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

namespace mega {
// timer with capped exponential backoff
BackoffTimer::BackoffTimer()
BackoffTimer::BackoffTimer(PrnGen &rng)
: rng(rng)
{
reset();
}
Expand All @@ -48,7 +49,7 @@ void BackoffTimer::backoff()
base = 6000;
}

delta = base + (dstime)((base / 2.0) * (PrnGen::genuint32(RAND_MAX)/(float)RAND_MAX));
delta = base + (dstime)((base / 2.0) * (rng.genuint32(RAND_MAX)/(float)RAND_MAX));
}

void BackoffTimer::backoff(dstime newdelta)
Expand Down Expand Up @@ -129,7 +130,8 @@ void BackoffTimer::update(dstime* waituntil)
}
}

TimerWithBackoff::TimerWithBackoff(int tag)
TimerWithBackoff::TimerWithBackoff(PrnGen &rng, int tag)
: BackoffTimer(rng)
{
this->tag = tag;
}
Expand Down
Loading

0 comments on commit 19222dd

Please sign in to comment.