]>
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 | ||
24 | #include <stdexcept> | |
25 | #include <string> | |
26 | ||
27 | #include <openssl/evp.h> | |
28 | ||
29 | inline std::string pdns_hash(const EVP_MD * md, const std::string& input) | |
30 | { | |
31 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL | |
32 | auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy); | |
33 | #else | |
34 | auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_new(), EVP_MD_CTX_free); | |
35 | #endif | |
36 | if (!mdctx) { | |
37 | throw std::runtime_error(std::string(EVP_MD_name(md)) + " context initialization failed"); | |
38 | } | |
39 | ||
40 | if (EVP_DigestInit_ex(mdctx.get(), md, nullptr) != 1) { | |
41 | throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP initialization failed"); | |
42 | } | |
43 | ||
44 | if (EVP_DigestUpdate(mdctx.get(), input.data(), input.size()) != 1) { | |
45 | throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP update failed"); | |
46 | } | |
47 | ||
48 | unsigned int written; | |
49 | std::string result; | |
50 | result.resize(EVP_MD_size(md)); | |
51 | ||
52 | if (EVP_DigestFinal_ex(mdctx.get(), const_cast<unsigned char *>(reinterpret_cast<const unsigned char*>(result.c_str())), &written) != 1) { | |
53 | throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final failed"); | |
54 | } | |
55 | ||
56 | if (written != result.size()) { | |
57 | throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final wrote " + std::to_string(written) + ", expected " + std::to_string(result.size())); | |
58 | } | |
59 | ||
60 | return result; | |
61 | } | |
62 | ||
63 | inline std::string pdns_md5sum(const std::string& input) | |
64 | { | |
65 | const auto md = EVP_md5(); | |
66 | if (md == nullptr) { | |
67 | throw std::runtime_error("The MD5 digest is not available via the OpenSSL EVP interface"); | |
68 | } | |
69 | ||
70 | return pdns_hash(md, input); | |
71 | } | |
72 | ||
73 | inline std::string pdns_sha1sum(const std::string& input) | |
74 | { | |
75 | const auto md = EVP_sha1(); | |
76 | if (md == nullptr) { | |
77 | throw std::runtime_error("The SHA1 digest is not available via the OpenSSL EVP interface"); | |
78 | } | |
79 | ||
80 | return pdns_hash(md, input); | |
81 | } |