Commit c819dbad authored by Jarvis Dong's avatar Jarvis Dong
Browse files

marked all code need to be fixed

parent e8aae596
......@@ -83,10 +83,21 @@ void UserIdentity::setupService()
QString legacyDir = m_settings->read("dataDirectory").toString();
if (!keyData.isEmpty()) {
// todo add v3 support: check keyDate length?
// TODO: add v3 support: by checking keyData length we know it's v3 or v2
// FIXME: need to add loadfromfile() inside loadfromdatav3() or not ?
CryptoKey key;
if (keyData.size() == 88) {
// v3
// if (!key.loadFromDataV3(keyData.toLatin1().toStdString(), CryptoKey::V3PrivateKey)) {
// qWarning() << "Cannot load v3 service key from configuration";
// return;
// }
// else {
//
// }
}
if (!key.loadFromData(QByteArray::fromBase64(keyData.toLatin1()), CryptoKey::PrivateKey, CryptoKey::DER)) {
qWarning() << "Cannot load service key from configuration";
qWarning() << "Cannot load v2 service key from configuration";
return;
}
......
......@@ -216,6 +216,7 @@ void AuthHiddenServiceChannel::sendAuthMessage()
// cookie: pre-stored 16 bits random byte array
// message: proof data
QByteArray signature;
//FIXME: d->privateKey is a CryptoKey instance with v3serviceID=""
QByteArray proofData = d->getProofData(d->privateKey.torServiceID());
if (!proofData.isEmpty()) {
// make a HMAC of the proof data
......@@ -253,6 +254,9 @@ void AuthHiddenServiceChannel::sendAuthMessage()
*/
QByteArray AuthHiddenServiceChannelPrivate::getProofData(const QString &client)
{
// TODO: add v3 compatibility
// TODO: change if condition to make v3 compatible. V2 has 16 chars ricochet: <ID>. V3 has 52.
// FIXME: Currently, clientHostname is empty string, client didn't get passed correctly from parameters.
QByteArray serverHostname = connection->serverHostname().toLatin1().mid(0, 16);
QByteArray clientHostname = client.toLatin1();
......@@ -334,6 +338,7 @@ void AuthHiddenServiceChannel::handleProof(const Data::AuthHiddenService::Proof
qWarning() << "Received invalid signature (size" << signature.size() << ") on" << type();
}
if (decoded) {
// FIXME: parameter is a empty string
QByteArray proofData = d->getProofData(publicKey.torServiceID());
if (!proofData.isEmpty()) {
// form a message for signature
......
......@@ -53,12 +53,12 @@ QByteArray AddOnionCommand::build()
QByteArray out("ADD_ONION");
if (m_service->privateKey().isLoaded()) {
out += " RSA1024:";
// out += " ED25519-V3:";
// out += " RSA1024:";
out += " ED25519-V3:";
out += m_service->privateKey().encodedPrivateKey(CryptoKey::DER).toBase64();
} else {
out += " NEW:RSA1024";
// out += " NEW:ED25519-V3";
// out += " NEW:RSA1024";
out += " NEW:ED25519-V3";
}
foreach (const HiddenService::Target &target, m_service->targets()) {
......@@ -97,6 +97,7 @@ void AddOnionCommand::onReply(int statusCode, const QByteArray &data)
m_service->setPrivateKey(key);
}
// returned data is v3 private key
//FIXME: v3 key should be a global instance
else if (data.startsWith(keyPrefixV3)) {
CryptoKey key(CryptoKey::V3);
std::string keyData = data.toStdString()
......@@ -110,10 +111,6 @@ void AddOnionCommand::onReply(int statusCode, const QByteArray &data)
// returned data is v3 serviced ID
else if ((data.size() == CryptoKey::V3ServiceIDLength + serviceIDPrefix.size())
&& data.startsWith((serviceIDPrefix))) {
// store the v3 service ID
// to get public key from serviceID:
// 1. remove prefix and last 4 char, the rest is public key in base32.
// 2. decode to get the 32byte key
CryptoKey key(CryptoKey::V3);
std::string keyData = data.toStdString()
.substr(serviceIDPrefix.size());
......
......@@ -120,11 +120,12 @@ void HiddenService::setV3serviceID(const CryptoKey &serviceID)
return;
}
if (!serviceID.isPrivate()) {
BUG() << "Cannot create a hidden service with a public key";
if (!serviceID.isV3serviceID()) {
BUG() << "Cannot set up hidden service with non-v3 service ID";
return;
}
m_v3serviceId = serviceID;
m_privateKey = serviceID;
m_hostname = m_privateKey.torServiceID() + QStringLiteral(".onion");
emit privateKeyChanged();
......
......@@ -147,6 +147,7 @@ bool CryptoKey::loadFromDataV3(const std::string &data, CryptoKey::KeyType type)
v3publicKey = getDecodedV3PublicKey().constData();
return true;
} else if (type == V3PrivateKey) {
this->v3privateKey = data;
v3privateKey = getDecodedV3PrivateKey().constData();
return true;
} else {
......@@ -156,18 +157,24 @@ bool CryptoKey::loadFromDataV3(const std::string &data, CryptoKey::KeyType type)
bool CryptoKey::loadFromFile(const QString &path, KeyType type, KeyFormat format)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "Failed to open" << (type == PrivateKey ? "private" : "public") << "key from"
<< path << "-" << file.errorString();
return false;
}
if (version == V2) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "Failed to open" << (type == PrivateKey ? "private" : "public") << "key from"
<< path << "-" << file.errorString();
return false;
}
QByteArray data = file.readAll();
file.close();
QByteArray data = file.readAll();
file.close();
return loadFromData(data, type, format);
return loadFromData(data, type, format);
}
else if (version == V3) {
//FIXME: this is incorrect, fix it later
return true;
}
}
/**
......@@ -178,7 +185,16 @@ bool CryptoKey::isLoaded() const{
if (version == V2) {
return d.data() && d->key != 0;
} else if (version == V3) {
return true;
//TODO: for v3
//FIXME: logic for v3 need to be corrected
if (v3privateKey.empty()) {
// v3 private key is empty. check service id
return v3serviceID.length() == CryptoKey::V3ServiceIDLength;
}
else {
// v3 private key is not empty. check v3 private key
return v3privateKey.length() == CryptoKey::V3PrivateKeyByteLength;
}
}
return false;
}
......@@ -198,7 +214,9 @@ bool CryptoKey::isPrivate() const
RSA_get0_factors(d->key, &p, &q);
return (p != 0);
} else if (version == V3) {
return (v3privateKey.length() == CryptoKey::V3PrivateKeyLength || (v3serviceID.empty()));
//TODO: for v3
//FIXME: logic for v3 need to be corrected
return v3privateKey.length() == CryptoKey::V3PrivateKeyByteLength;
} else {
return false;
}
......@@ -267,9 +285,9 @@ QByteArray CryptoKey::getDecodedV3PublicKey() const{
* @return private key in bytes
*/
QByteArray CryptoKey::getDecodedV3PrivateKey() const{
if (!isLoaded() || version != V3 || !isPrivate()) {
return QByteArray();
} else {
// if (!isLoaded() || version != V3 || !isPrivate()) {
// return QByteArray();
// } else {
QByteArray stringBytes = QByteArray::fromStdString(this->v3privateKey);
QByteArray bytes(QByteArray::fromBase64(stringBytes));
// bytes.toHex().constData() will show the char* of the hex representation of decoded key
......@@ -278,7 +296,7 @@ QByteArray CryptoKey::getDecodedV3PrivateKey() const{
} else {
return QByteArray();
}
}
// }
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment