expose generated public key and private key from generator

This commit is contained in:
Stephen Birarda 2016-02-19 16:48:58 -08:00
parent 5de8601b43
commit 6e4ecffb09
4 changed files with 43 additions and 31 deletions

View file

@ -612,10 +612,6 @@ void AccountManager::generateNewKeypair(bool isUserKeypair, const QUuid& domainI
connect(keypairGenerator, &RSAKeypairGenerator::errorGeneratingKeypair,
this, &AccountManager::handleKeypairGenerationError);
// cleanup the keypair generator and the thread once the generation succeeds or fails
connect(keypairGenerator, &RSAKeypairGenerator::generatedKeypair, keypairGenerator, &RSAKeypairGenerator::deleteLater);
connect(keypairGenerator, &RSAKeypairGenerator::errorGeneratingKeypair, keypairGenerator, &RSAKeypairGenerator::deleteLater);
connect(keypairGenerator, &QObject::destroyed, generateThread, &QThread::quit);
connect(generateThread, &QThread::finished, generateThread, &QThread::deleteLater);
@ -625,32 +621,43 @@ void AccountManager::generateNewKeypair(bool isUserKeypair, const QUuid& domainI
generateThread->start();
}
void AccountManager::processGeneratedKeypair(QByteArray publicKey, QByteArray privateKey) {
void AccountManager::processGeneratedKeypair() {
qCDebug(networking) << "Generated 2048-bit RSA key-pair. Storing private key and uploading public key.";
// set the private key on our metaverse API account info
_accountInfo.setPrivateKey(privateKey);
persistAccountToSettings();
// upload the public key so data-web has an up-to-date key
const QString PUBLIC_KEY_UPDATE_PATH = "api/v1/user/public_key";
// setup a multipart upload to send up the public key
QHttpMultiPart* requestMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart keyPart;
keyPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
keyPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"public_key\"; filename=\"public_key\""));
keyPart.setBody(publicKey);
requestMultiPart->append(keyPart);
sendRequest(PUBLIC_KEY_UPDATE_PATH, AccountManagerAuth::Required, QNetworkAccessManager::PutOperation,
JSONCallbackParameters(), QByteArray(), requestMultiPart);
RSAKeypairGenerator* keypairGenerator = qobject_cast<RSAKeypairGenerator*>(sender());
if (keypairGenerator) {
// set the private key on our metaverse API account info
_accountInfo.setPrivateKey(keypairGenerator->getPrivateKey());
persistAccountToSettings();
// upload the public key so data-web has an up-to-date key
const QString PUBLIC_KEY_UPDATE_PATH = "api/v1/user/public_key";
// setup a multipart upload to send up the public key
QHttpMultiPart* requestMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart keyPart;
keyPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
keyPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"public_key\"; filename=\"public_key\""));
keyPart.setBody(keypairGenerator->getPublicKey());
requestMultiPart->append(keyPart);
sendRequest(PUBLIC_KEY_UPDATE_PATH, AccountManagerAuth::Required, QNetworkAccessManager::PutOperation,
JSONCallbackParameters(), QByteArray(), requestMultiPart);
keypairGenerator->deleteLater();
} else {
qCWarning(networking) << "Expected processGeneratedKeypair to be called by a live RSAKeypairGenerator"
<< "but the casted sender is NULL. Will not process generated keypair.";
}
}
void AccountManager::handleKeypairGenerationError() {
qCritical() << "Error generating keypair - this is likely to cause authentication issues.";
sender()->deleteLater();
}

View file

@ -103,7 +103,7 @@ signals:
private slots:
void processReply();
void handleKeypairGenerationError();
void processGeneratedKeypair(QByteArray publicKey, QByteArray privateKey);
void processGeneratedKeypair();
private:
AccountManager();

View file

@ -85,12 +85,12 @@ void RSAKeypairGenerator::generateKeypair() {
// we can cleanup the RSA struct before we continue on
RSA_free(keyPair);
QByteArray publicKeyArray(reinterpret_cast<char*>(publicKeyDER), publicKeyLength);
QByteArray privateKeyArray(reinterpret_cast<char*>(privateKeyDER), privateKeyLength);
_publicKey = QByteArray { reinterpret_cast<char*>(publicKeyDER), publicKeyLength };
_privateKey = QByteArray { reinterpret_cast<char*>(privateKeyDER), privateKeyLength };
// cleanup the publicKeyDER and publicKeyDER data
OPENSSL_free(publicKeyDER);
OPENSSL_free(privateKeyDER);
emit generatedKeypair(publicKeyArray, privateKeyArray);
emit generatedKeypair();
}

View file

@ -22,16 +22,21 @@ public:
void setDomainID(const QUuid& domainID) { _domainID = domainID; }
const QUuid& getDomainID() const { return _domainID; }
const QByteArray& getPublicKey() const { return _publicKey; }
const QByteArray& getPrivateKey() const { return _privateKey; }
public slots:
void generateKeypair();
signals:
void errorGeneratingKeypair();
void generatedKeypair(QByteArray publicKey, QByteArray privateKey);
void generatedKeypair();
private:
QUuid _domainID;
QByteArray _publicKey;
QByteArray _privateKey;
};
#endif // hifi_RSAKeypairGenerator_h