|
3 | 3 | * Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/) |
4 | 4 | * |
5 | 5 | * You may not use this file except in compliance with |
6 | | - * the License. You may obtain a copy of the License at |
| 6 | + * the License. You may obtain a copy of the License at |
7 | 7 | * |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
|
17 | 17 | #define SRC_UTILS_SHA1_H_ |
18 | 18 |
|
19 | 19 | #include <array> |
| 20 | +#include <exception> |
20 | 21 | #include <string> |
21 | 22 | #include <string_view> |
22 | | -#include <stdexcept> |
23 | 23 |
|
24 | 24 | #include "src/utils/string.h" |
25 | 25 | #include "mbedtls/md.h" |
26 | 26 |
|
27 | 27 | namespace modsecurity::Utils { |
28 | 28 |
|
| 29 | +class DigestCalculationException : public std::exception { |
| 30 | + public: |
| 31 | + explicit DigestCalculationException(const char *message) noexcept |
| 32 | + : m_message(message) { } |
| 33 | + |
| 34 | + const char *what() const noexcept override { |
| 35 | + return m_message; |
| 36 | + } |
| 37 | + |
| 38 | + private: |
| 39 | + const char *m_message; |
| 40 | +}; |
| 41 | + |
| 42 | + |
29 | 43 | template<mbedtls_md_type_t DigestType, int DigestSize> |
30 | 44 | class DigestImpl { |
31 | 45 | public: |
32 | | - |
33 | 46 | static std::string digest(const std::string& input) { |
34 | | - return digestHelper(input, [](const auto digest) { |
35 | | - return std::string(digest); |
36 | | - }); |
| 47 | + const auto digestBytes = calculateDigest(input); |
| 48 | + return std::string(digestBytes.begin(), digestBytes.end()); |
37 | 49 | } |
38 | 50 |
|
39 | 51 | static void digestReplace(std::string& value) { |
40 | | - digestHelper(value, [&value](const auto digest) mutable { |
41 | | - value = digest; |
42 | | - }); |
| 52 | + const auto digestBytes = calculateDigest(value); |
| 53 | + value.assign(digestBytes.begin(), digestBytes.end()); |
43 | 54 | } |
44 | 55 |
|
45 | | - static std::string hexdigest(const std::string &input) { |
46 | | - return digestHelper(input, [](const auto digest) { |
47 | | - return utils::string::string_to_hex(digest); |
48 | | - }); |
| 56 | + static std::string hexdigest(const std::string& input) { |
| 57 | + const auto digestBytes = calculateDigest(input); |
| 58 | + const std::string digestString(digestBytes.begin(), digestBytes.end()); |
| 59 | + return utils::string::string_to_hex(digestString); |
49 | 60 | } |
50 | 61 |
|
51 | | -private: |
52 | | - |
53 | | - template<typename ConvertOp> |
54 | | - static auto digestHelper(std::string_view input, |
55 | | - ConvertOp convertOp) { |
| 62 | + private: |
| 63 | + static std::array<unsigned char, DigestSize> calculateDigest( |
| 64 | + std::string_view input) { |
| 65 | + std::array<unsigned char, DigestSize> digestBytes = {}; |
56 | 66 |
|
57 | | - std::array<unsigned char, DigestSize> digest = {}; |
58 | | - |
59 | | - const auto *mdInfo = mbedtls_md_info_from_type(DigestType); |
| 67 | + const mbedtls_md_info_t *mdInfo = mbedtls_md_info_from_type(DigestType); |
60 | 68 | if (mdInfo == nullptr) { |
61 | | - throw std::runtime_error( |
| 69 | + throw DigestCalculationException( |
62 | 70 | "mbedtls_md_info_from_type() returned nullptr"); |
63 | 71 | } |
64 | 72 |
|
65 | | - const int ret = mbedtls_md( |
66 | | - mdInfo, |
67 | | - reinterpret_cast<const unsigned char *>(input.data()), |
68 | | - input.size(), |
69 | | - digest.data()); |
| 73 | + const auto *inputBytes = |
| 74 | + static_cast<const unsigned char *>(static_cast<const void *>(input.data())); |
70 | 75 |
|
71 | | - if (ret != 0) { |
72 | | - throw std::runtime_error( |
73 | | - "mbedtls_md() failed with error code: " + std::to_string(ret)); |
| 76 | + if (const int ret = mbedtls_md( |
| 77 | + mdInfo, |
| 78 | + inputBytes, |
| 79 | + input.size(), |
| 80 | + digestBytes.data()); ret != 0) { |
| 81 | + throw DigestCalculationException("mbedtls_md() failed"); |
74 | 82 | } |
75 | 83 |
|
76 | | - // mbedtls uses unsigned char buffers, while string_view expects char. |
77 | | - const auto *digestChars = |
78 | | - reinterpret_cast<const char *>(digest.data()); |
79 | | - |
80 | | - return convertOp(std::string_view(digestChars, DigestSize)); |
| 84 | + return digestBytes; |
81 | 85 | } |
82 | 86 | }; |
83 | 87 |
|
84 | 88 |
|
85 | 89 | class Sha1 : public DigestImpl<MBEDTLS_MD_SHA1, 20> { |
86 | 90 | }; |
87 | 91 |
|
88 | | - |
89 | 92 | } // namespace modsecurity::Utils |
90 | 93 |
|
91 | 94 | #endif // SRC_UTILS_SHA1_H_ |
0 commit comments