|
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 | * |
|
16 | 16 | #ifndef SRC_UTILS_SHA1_H_ |
17 | 17 | #define SRC_UTILS_SHA1_H_ |
18 | 18 |
|
| 19 | +#include <array> |
| 20 | +#include <exception> |
19 | 21 | #include <string> |
20 | | -#include <cassert> |
| 22 | +#include <string_view> |
21 | 23 |
|
22 | 24 | #include "src/utils/string.h" |
23 | | -#include "mbedtls/sha1.h" |
| 25 | +#include "mbedtls/md.h" |
24 | 26 |
|
25 | 27 | namespace modsecurity::Utils { |
26 | 28 |
|
| 29 | +class DigestCalculationException : public std::exception { |
| 30 | + public: |
| 31 | + explicit DigestCalculationException(const char *message) noexcept |
| 32 | + : m_message(message) { } |
27 | 33 |
|
28 | | -using DigestOp = int (*)(const unsigned char *, size_t, unsigned char []); |
| 34 | + const char *what() const noexcept override { |
| 35 | + return m_message; |
| 36 | + } |
29 | 37 |
|
| 38 | + private: |
| 39 | + const char *m_message; |
| 40 | +}; |
30 | 41 |
|
31 | | -template<DigestOp digestOp, int DigestSize> |
| 42 | + |
| 43 | +template<mbedtls_md_type_t DigestType, int DigestSize> |
32 | 44 | class DigestImpl { |
33 | 45 | public: |
34 | | - |
35 | 46 | static std::string digest(const std::string& input) { |
36 | | - return digestHelper(input, [](const auto digest) { |
37 | | - return std::string(digest); |
38 | | - }); |
| 47 | + const auto digestBytes = calculateDigest(input); |
| 48 | + return std::string(digestBytes.begin(), digestBytes.end()); |
39 | 49 | } |
40 | 50 |
|
41 | 51 | static void digestReplace(std::string& value) { |
42 | | - digestHelper(value, [&value](const auto digest) mutable { |
43 | | - value = digest; |
44 | | - }); |
| 52 | + const auto digestBytes = calculateDigest(value); |
| 53 | + value.assign(digestBytes.begin(), digestBytes.end()); |
45 | 54 | } |
46 | 55 |
|
47 | | - static std::string hexdigest(const std::string &input) { |
48 | | - return digestHelper(input, [](const auto digest) { |
49 | | - return utils::string::string_to_hex(digest); |
50 | | - }); |
| 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); |
51 | 60 | } |
52 | 61 |
|
53 | | -private: |
54 | | - |
55 | | - template<typename ConvertOp> |
56 | | - static auto digestHelper(const std::string &input, |
57 | | - ConvertOp convertOp) -> auto { |
58 | | - char digest[DigestSize]; |
59 | | - |
60 | | - const auto ret = (*digestOp)(reinterpret_cast<const unsigned char *>(input.c_str()), |
61 | | - input.size(), reinterpret_cast<unsigned char *>(digest)); |
62 | | - assert(ret == 0); |
63 | | - |
64 | | - return convertOp(std::string_view(digest, DigestSize)); |
| 62 | + private: |
| 63 | + static std::array<unsigned char, DigestSize> calculateDigest( |
| 64 | + std::string_view input) { |
| 65 | + std::array<unsigned char, DigestSize> digestBytes = {}; |
| 66 | + |
| 67 | + const mbedtls_md_info_t *mdInfo = mbedtls_md_info_from_type(DigestType); |
| 68 | + if (mdInfo == nullptr) { |
| 69 | + throw DigestCalculationException( |
| 70 | + "mbedtls_md_info_from_type() returned nullptr"); |
| 71 | + } |
| 72 | + |
| 73 | + const auto *inputBytes = |
| 74 | + static_cast<const unsigned char *>(static_cast<const void *>(input.data())); |
| 75 | + |
| 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"); |
| 82 | + } |
| 83 | + |
| 84 | + return digestBytes; |
65 | 85 | } |
66 | 86 | }; |
67 | 87 |
|
68 | 88 |
|
69 | | -class Sha1 : public DigestImpl<&mbedtls_sha1, 20> { |
| 89 | +class Sha1 : public DigestImpl<MBEDTLS_MD_SHA1, 20> { |
70 | 90 | }; |
71 | 91 |
|
72 | | - |
73 | 92 | } // namespace modsecurity::Utils |
74 | 93 |
|
75 | 94 | #endif // SRC_UTILS_SHA1_H_ |
0 commit comments