add RSAKeypairGenerator to generate a 2048-bit keypair

This commit is contained in:
Stephen Birarda 2014-10-14 11:36:40 -07:00
parent 14d4dd1f4b
commit 912512936e
6 changed files with 120 additions and 4 deletions

View file

@ -105,7 +105,6 @@ link_hifi_libraries(shared octree voxels fbx metavoxels networking particles ent
# find any optional and required libraries
find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED)
# perform standard include and linking for found externals
foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
@ -169,10 +168,9 @@ endif ()
# include headers for interface and InterfaceConfig.
include_directories("${PROJECT_SOURCE_DIR}/src" "${PROJECT_BINARY_DIR}/includes")
include_directories("${OPENSSL_INCLUDE_DIR}")
target_link_libraries(
${TARGET_NAME} ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES}
${TARGET_NAME} ${ZLIB_LIBRARIES}
Qt5::Gui Qt5::Network Qt5::Multimedia Qt5::OpenGL Qt5::Script Qt5::Svg Qt5::WebKitWidgets
)

View file

@ -64,6 +64,7 @@
#include <ParticlesScriptingInterface.h>
#include <PerfStat.h>
#include <ResourceCache.h>
#include <RSAKeypairGenerator.h>
#include <UserActivityLogger.h>
#include <UUID.h>

View file

@ -10,5 +10,13 @@ if (WIN32)
target_link_libraries(${TARGET_NAME} ws2_32.lib)
endif ()
# find OpenSSL
find_package(OpenSSL REQUIRED)
include_directories(SYSTEM "${OPENSSL_INCLUDE_DIR}")
# append OpenSSL to our list of libraries to link
list(APPEND ${TARGET_NAME}_LIBRARIES_TO_LINK "${OPENSSL_LIBRARIES}")
# call macro to link our dependencies and bubble them up via a property on our target
link_shared_dependencies()

View file

@ -67,7 +67,7 @@ private:
QUuid _walletID;
qint64 _balance;
bool _hasBalance;
QString _privateKey;
QByteArray _privateKey;
};
#endif // hifi_DataServerAccountInfo_h

View file

@ -0,0 +1,83 @@
//
// RSAKeypairGenerator.cpp
// libraries/networking/src
//
// Created by Stephen Birarda on 2014-10-14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <qdebug.h>
#include "RSAKeypairGenerator.h"
void RSAKeypairGenerator::generateKeypair() {
RSA* keyPair = RSA_new();
BIGNUM* exponent = BN_new();
const unsigned long RSA_KEY_EXPONENT = 65537;
BN_set_word(exponent, RSA_KEY_EXPONENT);
// seed the random number generator before we call RSA_generate_key_ex
srand(time(NULL));
const int RSA_KEY_BITS = 2048;
if (!RSA_generate_key_ex(keyPair, RSA_KEY_BITS, exponent, NULL)) {
qDebug() << "Error generating 2048-bit RSA Keypair -" << ERR_get_error();
emit errorGeneratingKeypair();
// we're going to bust out of here but first we cleanup the BIGNUM
BN_free(exponent);
return;
}
// we don't need the BIGNUM anymore so clean that up
BN_free(exponent);
// grab the public key and private key from the file
BIO *privateKeyBIO = BIO_new(BIO_s_mem());
int privateWrite = PEM_write_bio_RSAPrivateKey(privateKeyBIO, keyPair, NULL, NULL, 0, NULL, NULL);
BIO *publicKeyBIO = BIO_new(BIO_s_mem());
int publicWrite = PEM_write_bio_RSAPublicKey(publicKeyBIO, keyPair);
if (privateWrite == 0 || publicWrite == 0) {
// we had a error grabbing either the private or public key from the RSA
// bubble up our error
emit errorGeneratingKeypair();
// cleanup the RSA struct
RSA_free(keyPair);
// cleanup the BIOs
BIO_free(privateKeyBIO);
BIO_free(publicKeyBIO);
return;
}
// we have the public key and private key in memory
// we can cleanup the RSA struct before we continue on
RSA_free(keyPair);
char* publicKeyData;
int publicKeyLength = BIO_get_mem_data(publicKeyBIO, &publicKeyData);
char* privateKeyData;
int privateKeyLength = BIO_get_mem_data(privateKeyBIO, &privateKeyData);
QByteArray publicKeyArray(publicKeyData, publicKeyLength);
QByteArray privateKeyArray(privateKeyData, privateKeyLength);
emit generatedKeypair(publicKeyArray, privateKeyArray);
}

View file

@ -0,0 +1,26 @@
//
// RSAKeypairGenerator.h
// libraries/networking/src
//
// Created by Stephen Birarda on 2014-10-14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_RSAKeypairGenerator_h
#define hifi_RSAKeypairGenerator_h
#include <qobject.h>
class RSAKeypairGenerator : public QObject {
Q_OBJECT
public slots:
void generateKeypair();
signals:
void errorGeneratingKeypair();
void generatedKeypair(const QByteArray& publicKey, const QByteArray& privateKey);
};
#endif // hifi_RSAKeypairGenerator_h