Skip to content

Commit dcd8141

Browse files
authored
exception for wrong password (#425)
1 parent 022c190 commit dcd8141

23 files changed

Lines changed: 71 additions & 49 deletions

cli/src/meta.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <odr/internal/util/odr_meta_util.hpp>
44

5+
#include <odr/exceptions.hpp>
6+
57
#include <iostream>
68
#include <string>
79

@@ -19,7 +21,9 @@ int main(int argc, char **argv) {
1921
DocumentFile document_file{input};
2022

2123
if (document_file.password_encrypted() && has_password) {
22-
if (!document_file.decrypt(password)) {
24+
try {
25+
document_file = document_file.decrypt(password).document_file();
26+
} catch (const WrongPasswordError &) {
2327
std::cerr << "wrong password" << std::endl;
2428
return 1;
2529
}

cli/src/server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <odr/exceptions.hpp>
12
#include <odr/file.hpp>
23
#include <odr/html.hpp>
34
#include <odr/http_server.hpp>
@@ -26,12 +27,12 @@ int main(int argc, char **argv) {
2627
return 2;
2728
}
2829
if (decoded_file.password_encrypted()) {
29-
auto decrypt_result = decoded_file.decrypt(*password);
30-
if (!decrypt_result.has_value()) {
30+
try {
31+
decoded_file = decoded_file.decrypt(*password);
32+
} catch (const WrongPasswordError &) {
3133
std::cerr << "wrong password" << std::endl;
3234
return 1;
3335
}
34-
decoded_file = std::move(*decrypt_result);
3536
}
3637

3738
HttpServer::Config server_config;

cli/src/translate.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <odr/exceptions.hpp>
12
#include <odr/file.hpp>
23
#include <odr/html.hpp>
34
#include <odr/html_service.hpp>
@@ -24,12 +25,12 @@ int main(int argc, char **argv) {
2425
return 2;
2526
}
2627
if (decoded_file.password_encrypted()) {
27-
auto decrypt_result = decoded_file.decrypt(*password);
28-
if (!decrypt_result.has_value()) {
28+
try {
29+
decoded_file = decoded_file.decrypt(*password);
30+
} catch (const WrongPasswordError &) {
2931
std::cerr << "wrong password" << std::endl;
3032
return 1;
3133
}
32-
decoded_file = std::move(*decrypt_result);
3334
}
3435

3536
HtmlConfig config;

src/odr/exceptions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,13 @@ UnsupportedOption::UnsupportedOption(const std::string &message)
108108
NullPointerError::NullPointerError(const std::string &variable)
109109
: std::runtime_error("null pointer error: " + variable) {}
110110

111+
WrongPasswordError::WrongPasswordError()
112+
: std::runtime_error("wrong password error") {}
113+
114+
DecryptionFailed::DecryptionFailed()
115+
: std::runtime_error("decryption failed") {}
116+
117+
NotEncryptedError::NotEncryptedError()
118+
: std::runtime_error("not encrypted error") {}
119+
111120
} // namespace odr

src/odr/exceptions.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,19 @@ struct NullPointerError : public std::runtime_error {
192192
explicit NullPointerError(const std::string &variable);
193193
};
194194

195+
/// @brief Wrong password error
196+
struct WrongPasswordError : public std::runtime_error {
197+
explicit WrongPasswordError();
198+
};
199+
200+
/// @brief Decryption failed
201+
struct DecryptionFailed : public std::runtime_error {
202+
explicit DecryptionFailed();
203+
};
204+
205+
/// @brief Not encrypted error
206+
struct NotEncryptedError : public std::runtime_error {
207+
explicit NotEncryptedError();
208+
};
209+
195210
} // namespace odr

src/odr/file.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,8 @@ EncryptionState DecodedFile::encryption_state() const {
127127
return m_impl->encryption_state();
128128
}
129129

130-
std::optional<DecodedFile> DecodedFile::decrypt(const std::string &password) {
131-
auto decrypted = m_impl->decrypt(password);
132-
if (decrypted == nullptr) {
133-
return std::nullopt;
134-
}
135-
return DecodedFile(decrypted);
130+
DecodedFile DecodedFile::decrypt(const std::string &password) const {
131+
return DecodedFile(m_impl->decrypt(password));
136132
}
137133

138134
bool DecodedFile::is_text_file() const {

src/odr/file.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class DecodedFile {
196196

197197
[[nodiscard]] bool password_encrypted() const;
198198
[[nodiscard]] EncryptionState encryption_state() const;
199-
[[nodiscard]] std::optional<DecodedFile> decrypt(const std::string &password);
199+
[[nodiscard]] DecodedFile decrypt(const std::string &password) const;
200200

201201
[[nodiscard]] bool is_text_file() const;
202202
[[nodiscard]] bool is_image_file() const;

src/odr/internal/abstract/file.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DecodedFile {
4646
return EncryptionState::not_encrypted;
4747
}
4848
[[nodiscard]] virtual std::shared_ptr<DecodedFile>
49-
decrypt(const std::string &password) const noexcept {
49+
decrypt(const std::string &password) const {
5050
(void)password;
5151
return nullptr;
5252
}

src/odr/internal/odf/odf_crypto.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ std::shared_ptr<abstract::ReadableFilesystem>
167167
odf::decrypt(const std::shared_ptr<abstract::ReadableFilesystem> &filesystem,
168168
const Manifest &manifest, const std::string &password) {
169169
if (!manifest.encrypted) {
170-
return nullptr;
170+
throw NotEncryptedError();
171171
}
172172

173173
if (auto it = manifest.entries.find(common::Path("encrypted-package"));
@@ -183,7 +183,7 @@ odf::decrypt(const std::shared_ptr<abstract::ReadableFilesystem> &filesystem,
183183
std::make_shared<common::MemoryFile>(std::move(decrypt));
184184
return zip::ZipFile(memory_file).archive()->filesystem();
185185
} catch (...) {
186-
return nullptr;
186+
throw WrongPasswordError();
187187
}
188188
}
189189

@@ -200,12 +200,12 @@ odf::decrypt(const std::shared_ptr<abstract::ReadableFilesystem> &filesystem,
200200
const std::string decrypt =
201201
derive_key_and_decrypt(smallest_file_entry, start_key, input);
202202
if (!validate_password(smallest_file_entry, decrypt)) {
203-
return nullptr;
203+
throw WrongPasswordError();
204204
}
205205
return std::make_shared<DecryptedFilesystem>(filesystem, manifest,
206206
start_key);
207207
} catch (...) {
208-
return nullptr;
208+
throw DecryptionFailed();
209209
}
210210
}
211211

src/odr/internal/odf/odf_file.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ EncryptionState OpenDocumentFile::encryption_state() const noexcept {
6363
}
6464

6565
std::shared_ptr<abstract::DecodedFile>
66-
OpenDocumentFile::decrypt(const std::string &password) const noexcept {
66+
OpenDocumentFile::decrypt(const std::string &password) const {
6767
if (m_encryption_state != EncryptionState::encrypted) {
68-
return nullptr;
68+
throw NotEncryptedError();
6969
}
7070

7171
auto decrypted_filesystem = odf::decrypt(m_filesystem, m_manifest, password);
72-
if (decrypted_filesystem == nullptr) {
73-
return nullptr;
74-
}
75-
7672
auto decrypted_file = std::make_shared<OpenDocumentFile>(*this);
7773
decrypted_file->m_filesystem = std::move(decrypted_filesystem);
7874
decrypted_file->m_file_meta =

0 commit comments

Comments
 (0)