]>
Commit | Line | Data |
---|---|---|
bfcdbc13 RG |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | #pragma once | |
23 | ||
f2e4e85a FM |
24 | #include "config.h" |
25 | #include <memory> | |
bfcdbc13 RG |
26 | #include <stdexcept> |
27 | #include <string> | |
28 | ||
29 | #include <openssl/evp.h> | |
30 | ||
f2e4e85a FM |
31 | namespace pdns |
32 | { | |
33 | inline std::string hash(const EVP_MD* messageDigest, const std::string& input) | |
bfcdbc13 | 34 | { |
629440d4 | 35 | #if defined(HAVE_EVP_MD_CTX_NEW) && defined(HAVE_EVP_MD_CTX_FREE) |
f2e4e85a | 36 | auto mdctx = std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX*)>(EVP_MD_CTX_new(), EVP_MD_CTX_free); |
629440d4 | 37 | #else |
f2e4e85a | 38 | auto mdctx = std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX*)>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy); |
bfcdbc13 RG |
39 | #endif |
40 | if (!mdctx) { | |
f2e4e85a | 41 | throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " context initialization failed"); |
bfcdbc13 RG |
42 | } |
43 | ||
f2e4e85a FM |
44 | if (EVP_DigestInit_ex(mdctx.get(), messageDigest, nullptr) != 1) { |
45 | throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP initialization failed"); | |
bfcdbc13 RG |
46 | } |
47 | ||
48 | if (EVP_DigestUpdate(mdctx.get(), input.data(), input.size()) != 1) { | |
f2e4e85a | 49 | throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP update failed"); |
bfcdbc13 RG |
50 | } |
51 | ||
f2e4e85a | 52 | unsigned int written = 0; |
bfcdbc13 | 53 | std::string result; |
f2e4e85a | 54 | result.resize(EVP_MD_size(messageDigest)); |
bfcdbc13 | 55 | |
f2e4e85a FM |
56 | // NOLINTNEXTLINE(*-cast): Using OpenSSL C APIs. |
57 | if (EVP_DigestFinal_ex(mdctx.get(), const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(result.c_str())), &written) != 1) { | |
58 | throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP final failed"); | |
bfcdbc13 RG |
59 | } |
60 | ||
61 | if (written != result.size()) { | |
f2e4e85a | 62 | throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP final wrote " + std::to_string(written) + ", expected " + std::to_string(result.size())); |
bfcdbc13 RG |
63 | } |
64 | ||
65 | return result; | |
66 | } | |
67 | ||
f2e4e85a | 68 | inline std::string md5(const std::string& input) |
bfcdbc13 | 69 | { |
f2e4e85a FM |
70 | const auto* const messageDigest = EVP_md5(); |
71 | if (messageDigest == nullptr) { | |
bfcdbc13 RG |
72 | throw std::runtime_error("The MD5 digest is not available via the OpenSSL EVP interface"); |
73 | } | |
74 | ||
f2e4e85a | 75 | return pdns::hash(messageDigest, input); |
bfcdbc13 RG |
76 | } |
77 | ||
f2e4e85a | 78 | inline std::string sha1(const std::string& input) |
bfcdbc13 | 79 | { |
f2e4e85a FM |
80 | const auto* const messageDigest = EVP_sha1(); |
81 | if (messageDigest == nullptr) { | |
bfcdbc13 RG |
82 | throw std::runtime_error("The SHA1 digest is not available via the OpenSSL EVP interface"); |
83 | } | |
84 | ||
f2e4e85a FM |
85 | return pdns::hash(messageDigest, input); |
86 | } | |
bfcdbc13 | 87 | } |