diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-09-19 00:39:23 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-09-19 00:50:27 +0200 |
| commit | aac2957ab6f98d087f8fb9ff68da39fe68c40e3d (patch) | |
| tree | 8760ca11276ea47e9eb9d3ed5c063a39ffb76869 /crypt | |
| parent | d0d366731425ad6c5a221d5c12968632e7ba1478 (diff) | |
| download | src-aac2957ab6f98d087f8fb9ff68da39fe68c40e3d.tar.gz src-aac2957ab6f98d087f8fb9ff68da39fe68c40e3d.zip | |
Werfe mehr Fehler und nutze mehr Funktionen
Diffstat (limited to 'crypt')
| -rw-r--r-- | crypt/cryptexception.cpp | 2 | ||||
| -rw-r--r-- | crypt/cryptexception.h | 4 | ||||
| -rw-r--r-- | crypt/hybridcrypt.cpp | 169 | ||||
| -rw-r--r-- | crypt/hybridcrypt.h | 26 |
4 files changed, 132 insertions, 69 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()) - { - // Räume den alten Schlüssel vorher ab - freeEvpKey(&userKeypair); + throwExceptionIfCsprngIsNotSeeded(); - // Lege Schlüsselkontextvariable an - EVP_PKEY_CTX *ctx = NULL; + // Räume den alten Schlüssel vorher ab + freeEvpKey(&userKeypair); - // Erzeuge den Schlüsselkontext - if (!(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))) - { - throwOpenSslException(); - } + // Lege Schlüsselkontextvariable an + EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); - // Initialisiere den Schlüsselgenerator - if (EVP_PKEY_keygen_init(ctx) <= 0) - { - throwOpenSslException(); - } + // Erzeuge den Schlüsselkontext + if (!ctx) + { + throwOpenSslException(); + } - // Lege den Schlüssel mit 2048 Bit an - if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) - { - throwOpenSslException(); - } + // Initialisiere den Schlüsselgenerator + if (EVP_PKEY_keygen_init(ctx) <= 0) + { + freePkeyCtx(&ctx); + throwOpenSslException(); + } - // Erzeuge den Schlüssel - if (EVP_PKEY_keygen(ctx, &userKeypair) <= 0) - { - throwOpenSslException(); - } + // Lege den Schlüssel mit 2048 Bit an + if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) + { + freePkeyCtx(&ctx); + throwOpenSslException(); + } - // Räume den Schlüsselkontext ab - EVP_PKEY_CTX_free(ctx); + // Erzeuge den Schlüssel + if (EVP_PKEY_keygen(ctx, &userKeypair) <= 0) + { + 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()) - { - FILE *keyfile = fopen(keyfileName.toStdString().c_str(), "w+"); + throwExceptionIfCsprngIsNotSeeded(); + + 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+"); - // 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()); + // 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); + // 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()); - // Wirf einen Fehler, falls OpenSSL ein Problem hatte - if (!opensslReturnError) - { - throwOpenSslException(); - } + // 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); + + // Räume die Ressourcen auf + fclose(keyfile); - // Wirft einen Fehler, falls OpenSSL ein Problem hatte - if (!opensslReturnError) - { - throwOpenSslException(); - } + // 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 @@ -118,11 +118,37 @@ 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 |
