From aac2957ab6f98d087f8fb9ff68da39fe68c40e3d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sat, 19 Sep 2015 00:39:23 +0200 Subject: Werfe mehr Fehler und nutze mehr Funktionen --- crypt/cryptexception.cpp | 2 +- crypt/cryptexception.h | 4 +- crypt/hybridcrypt.cpp | 185 ++++++++++++++++++++++++++++------------------- crypt/hybridcrypt.h | 26 +++++++ 4 files changed, 140 insertions(+), 77 deletions(-) diff --git a/crypt/cryptexception.cpp b/crypt/cryptexception.cpp index d839029..42480c4 100644 --- a/crypt/cryptexception.cpp +++ b/crypt/cryptexception.cpp @@ -1,7 +1,7 @@ #include "cryptexception.h" CryptException::CryptException(std::string what, ReturnCode returnCode) - :exception() + : exception() { this->whatMsg = what; this->retId = returnCode; diff --git a/crypt/cryptexception.h b/crypt/cryptexception.h index 7f70211..768e144 100644 --- a/crypt/cryptexception.h +++ b/crypt/cryptexception.h @@ -18,7 +18,9 @@ public: DecryptionErrorRsa, DecryptionErrorAes, OpenSslError, - KeyNotRsa + KeyNotRsa, + CsprngNotSeeded, + NoUserKeyCreated }; /** diff --git a/crypt/hybridcrypt.cpp b/crypt/hybridcrypt.cpp index 9c236cb..0d56038 100644 --- a/crypt/hybridcrypt.cpp +++ b/crypt/hybridcrypt.cpp @@ -52,41 +52,44 @@ void HybridCrypt::decrypt(QString infileName, QString outfileName) void HybridCrypt::createKeypair() { - if (isCsprngSeeded()) + throwExceptionIfCsprngIsNotSeeded(); + + // Räume den alten Schlüssel vorher ab + freeEvpKey(&userKeypair); + + // Lege Schlüsselkontextvariable an + EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); + + // Erzeuge den Schlüsselkontext + if (!ctx) + { + throwOpenSslException(); + } + + // Initialisiere den Schlüsselgenerator + if (EVP_PKEY_keygen_init(ctx) <= 0) + { + freePkeyCtx(&ctx); + throwOpenSslException(); + } + + // Lege den Schlüssel mit 2048 Bit an + if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) + { + freePkeyCtx(&ctx); + throwOpenSslException(); + } + + // Erzeuge den Schlüssel + if (EVP_PKEY_keygen(ctx, &userKeypair) <= 0) { - // Räume den alten Schlüssel vorher ab - freeEvpKey(&userKeypair); - - // Lege Schlüsselkontextvariable an - EVP_PKEY_CTX *ctx = NULL; - - // Erzeuge den Schlüsselkontext - if (!(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))) - { - throwOpenSslException(); - } - - // Initialisiere den Schlüsselgenerator - if (EVP_PKEY_keygen_init(ctx) <= 0) - { - throwOpenSslException(); - } - - // Lege den Schlüssel mit 2048 Bit an - if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) - { - throwOpenSslException(); - } - - // Erzeuge den Schlüssel - if (EVP_PKEY_keygen(ctx, &userKeypair) <= 0) - { - throwOpenSslException(); - } - - // Räume den Schlüsselkontext ab - EVP_PKEY_CTX_free(ctx); + freePkeyCtx(&ctx); + throwOpenSslException(); } + + // Räume den Schlüsselkontext ab + freePkeyCtx(&ctx); + } void HybridCrypt::importUserKeypair(QString keyfileName, QString password) @@ -99,7 +102,8 @@ void HybridCrypt::importUserKeypair(QString keyfileName, QString password) // Datei existiert nicht if (keyfile == NULL) { - throw CryptException("Datei nicht gefunden: " + keyfileName.toStdString(), CryptException::FileNotFound); + throw CryptException("Datei nicht gefunden: " + keyfileName.toStdString(), + CryptException::FileNotFound); } // Ließ den Schlüssel des Nutzers ein @@ -121,56 +125,56 @@ void HybridCrypt::importUserKeypair(QString keyfileName, QString password) void HybridCrypt::exportUserKeypair(QString keyfileName, QString password) { - if (userKeypair != NULL && isCsprngSeeded()) + throwExceptionIfCsprngIsNotSeeded(); + + throwExceptionIfUserKeyIsNull(); + + FILE *keyfile = fopen(keyfileName.toStdString().c_str(), "w+"); + + // Teste, ob die Datei zum Schreiben geöffnet werden konnte + if (keyfile == NULL) { - FILE *keyfile = fopen(keyfileName.toStdString().c_str(), "w+"); - - // Teste, ob die Datei zum Schreiben geöffnet werden konnte - if (keyfile == NULL) - { - throw CryptException("Konnte Datei nicht schreiben: " + - keyfileName.toStdString(), CryptException::FileNotWritable); - } - - // Wenn password ein Leerstring ist, verschlüssele den privaten Schlüssel nicht - int opensslReturnError = PEM_write_PKCS8PrivateKey(keyfile, userKeypair, - (password.isEmpty()) ? NULL : EVP_aes_256_cbc(), NULL, 0, NULL, - (void *) password.toStdString().c_str()); - - // Räume die Ressourcen auf - fclose(keyfile); - - // Wirf einen Fehler, falls OpenSSL ein Problem hatte - if (!opensslReturnError) - { - throwOpenSslException(); - } + throw CryptException("Konnte Datei nicht schreiben: " + + keyfileName.toStdString(), CryptException::FileNotWritable); + } + + // Wenn password ein Leerstring ist, verschlüssele den privaten Schlüssel nicht + int opensslReturnError = PEM_write_PKCS8PrivateKey(keyfile, userKeypair, + (password.isEmpty()) ? NULL : EVP_aes_256_cbc(), NULL, 0, NULL, + (void *) password.toStdString().c_str()); + + // Räume die Ressourcen auf + fclose(keyfile); + + // Wirf einen Fehler, falls OpenSSL ein Problem hatte + if (!opensslReturnError) + { + throwOpenSslException(); } } void HybridCrypt::exportPublicUserKey(QString keyfileName) { - if (userKeypair != NULL) - { - FILE *keyfile = fopen(keyfileName.toStdString().c_str(), "w+"); + throwExceptionIfUserKeyIsNull(); - // Teste, ob die Datei zum Schreiben geöffnet werden konnte - if (keyfile == NULL) - { - throw CryptException("Konnte Datei nicht schreiben: " + - keyfileName.toStdString(), CryptException::FileNotWritable); - } + FILE *keyfile = fopen(keyfileName.toStdString().c_str(), "w+"); - int opensslReturnError = PEM_write_PUBKEY(keyfile, userKeypair); + // Teste, ob die Datei zum Schreiben geöffnet werden konnte + if (keyfile == NULL) + { + throw CryptException("Konnte Datei nicht schreiben: " + + keyfileName.toStdString(), CryptException::FileNotWritable); + } - // Räume die Ressourcen auf - fclose(keyfile); + int opensslReturnError = PEM_write_PUBKEY(keyfile, userKeypair); - // Wirft einen Fehler, falls OpenSSL ein Problem hatte - if (!opensslReturnError) - { - throwOpenSslException(); - } + // Räume die Ressourcen auf + fclose(keyfile); + + // Wirft einen Fehler, falls OpenSSL ein Problem hatte + if (!opensslReturnError) + { + throwOpenSslException(); } } @@ -200,7 +204,26 @@ void HybridCrypt::throwExceptionIfEvpKeyIsNotRsa(EVP_PKEY **key) if (!isKeyRsa(*key)) { freeEvpKey(key); - throw CryptException("Nur RSA Schlüssel werden unterstüzt.", CryptException::KeyNotRsa); + throw CryptException("Nur RSA Schlüssel werden unterstüzt.", + CryptException::KeyNotRsa); + } +} + +void HybridCrypt::throwExceptionIfCsprngIsNotSeeded() +{ + if (!isCsprngSeeded()) + { + throw new CryptException("Zufallszahlengenerator ist nicht initialisiert", + CryptException::CsprngNotSeeded); + } +} + +void HybridCrypt::throwExceptionIfUserKeyIsNull() +{ + if (userKeypair == NULL) + { + throw CryptException("Kein Schlüssel wurde angelegt", + CryptException::NoUserKeyCreated); } } @@ -209,3 +232,15 @@ void HybridCrypt::freeEvpKey(EVP_PKEY **key) EVP_PKEY_free(*key); *key = NULL; } + +void HybridCrypt::freeCipherCtx(EVP_CIPHER_CTX **ctx) +{ + EVP_CIPHER_CTX_free(*ctx); + *ctx = NULL; +} + +void HybridCrypt::freePkeyCtx(EVP_PKEY_CTX **ctx) +{ + EVP_PKEY_CTX_free(*ctx); + *ctx = NULL; +} diff --git a/crypt/hybridcrypt.h b/crypt/hybridcrypt.h index 602844e..ce8a553 100644 --- a/crypt/hybridcrypt.h +++ b/crypt/hybridcrypt.h @@ -117,12 +117,38 @@ private: */ void throwExceptionIfEvpKeyIsNotRsa(EVP_PKEY **key); + /** + * @brief throwExceptionIfCsprngIsNotSeeded + * Wirft eine Exception, falls der Zufallszahlengenerator nicht initialisiert wurde. + */ + void throwExceptionIfCsprngIsNotSeeded(); + + /** + * @brief throwExceptionIfUserKeyIsNull + * Wift eine Exception, falls kein Nutzerschlüssel vorhanden ist. + */ + void throwExceptionIfUserKeyIsNull(); + /** * @brief freeEvpKey * Räumt den Schlüssel hinter key ab. * @param key Der Pointer, der auf NULL gesetzt wird. */ void freeEvpKey(EVP_PKEY **key); + + /** + * @brief freeCipherCtx + * Räumt den Kontext hinter ctx ab. + * @param ctx Der Pointer, der auf NULL gesetzt wird. + */ + void freeCipherCtx(EVP_CIPHER_CTX **ctx); + + /** + * @brief freePkeyCtx + * Räumt den Kontext hinter ctx ab. + * @param ctx Der Pointer, der auf NULL gesetzt wird. + */ + void freePkeyCtx(EVP_PKEY_CTX **ctx); }; #endif // HYBRIDCRYPT_H -- cgit v1.2.3-70-g09d2