Line | Count | Source |
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 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include "base64.hh" |
26 | | #include <limits> |
27 | | #include <memory> |
28 | | #include <stdexcept> |
29 | | |
30 | | #include <boost/scoped_array.hpp> |
31 | | #include <openssl/bio.h> |
32 | | #include <openssl/evp.h> |
33 | | |
34 | | template <typename Container> |
35 | | int B64Decode(const std::string& src, Container& dst) |
36 | 2.68k | { |
37 | 2.68k | if (src.empty()) { |
38 | 94 | dst.clear(); |
39 | 94 | return 0; |
40 | 94 | } |
41 | | // check if the dlen computation might overflow or it does not fit into an int (for IO_write) |
42 | 2.58k | if (src.length() > std::numeric_limits<size_t>::max() / 7U || src.length() > std::numeric_limits<int>::max()) { |
43 | 0 | throw std::runtime_error("B64Decode too large"); |
44 | 0 | } |
45 | 2.58k | const size_t dlen = (src.length() * 6U + 7U) / 8U; |
46 | 2.58k | dst.resize(dlen); |
47 | 2.58k | auto bio = std::unique_ptr<BIO, void (*)(BIO*)>(BIO_new(BIO_s_mem()), BIO_free_all); |
48 | 2.58k | if (!bio) { |
49 | 0 | throw std::runtime_error("BIO_new failed"); |
50 | 0 | } |
51 | 2.58k | if (BIO_write(bio.get(), src.c_str(), src.length()) != static_cast<int>(src.length())) { |
52 | 0 | throw std::runtime_error("BIO_write failed"); |
53 | 0 | } |
54 | 2.58k | auto* b64 = BIO_new(BIO_f_base64()); |
55 | 2.58k | if (b64 == nullptr) { |
56 | 0 | throw std::runtime_error("BIO_new failed"); |
57 | 0 | } |
58 | 2.58k | bio = std::unique_ptr<BIO, void (*)(BIO*)>(BIO_push(b64, bio.release()), BIO_free_all); |
59 | 2.58k | BIO_set_flags(bio.get(), BIO_FLAGS_BASE64_NO_NL); |
60 | 2.58k | auto olen = BIO_read(b64, &dst.at(0), dlen); |
61 | 2.58k | if ((olen == 0 || olen == -1) && BIO_should_retry(bio.get())) { |
62 | 0 | throw std::runtime_error("BIO_read failed to read all data from memory buffer"); |
63 | 0 | } |
64 | 2.58k | bio.reset(); |
65 | 2.58k | if (olen > 0) { |
66 | 1.44k | dst.resize(olen); |
67 | 1.44k | return 0; |
68 | 1.44k | } |
69 | 1.13k | return -1; |
70 | 2.58k | } |
71 | | |
72 | | template int B64Decode<std::string>(const std::string& strInput, std::string& strOutput); |
73 | | |
74 | | std::string Base64Encode(const std::string& src) |
75 | 1.29k | { |
76 | 1.29k | if (!src.empty()) { |
77 | 1.25k | auto bio = std::unique_ptr<BIO, void (*)(BIO*)>(BIO_new(BIO_s_mem()), BIO_free_all); |
78 | 1.25k | if (!bio) { |
79 | 0 | throw std::runtime_error("BIO_new failed"); |
80 | 0 | } |
81 | 1.25k | auto* b64 = BIO_new(BIO_f_base64()); |
82 | 1.25k | if (b64 == nullptr) { |
83 | 0 | throw std::runtime_error("BIO_new failed"); |
84 | 0 | } |
85 | 1.25k | bio = std::unique_ptr<BIO, void (*)(BIO*)>(BIO_push(b64, bio.release()), BIO_free_all); |
86 | 1.25k | BIO_set_flags(bio.get(), BIO_FLAGS_BASE64_NO_NL); |
87 | 1.25k | int bioWriteRet = BIO_write(bio.get(), src.c_str(), src.length()); |
88 | 1.25k | if (bioWriteRet < 0 || static_cast<size_t>(bioWriteRet) != src.length()) { |
89 | 0 | throw std::runtime_error("BIO_write failed to write all data to memory buffer"); |
90 | 0 | } |
91 | 1.25k | (void)BIO_flush(bio.get()); |
92 | 1.25k | char* pp; |
93 | 1.25k | std::string out; |
94 | 1.25k | auto olen = BIO_get_mem_data(bio.get(), &pp); |
95 | 1.25k | if (olen > 0) { |
96 | 1.25k | out = std::string(pp, olen); |
97 | 1.25k | } |
98 | 1.25k | bio.reset(); |
99 | 1.25k | return out; |
100 | 1.25k | } |
101 | 41 | return ""; |
102 | 1.29k | } |
103 | | |
104 | | #if 0 |
105 | | #include <iostream> |
106 | | int main() { |
107 | | std::string in = "PowerDNS Test String 1"; |
108 | | std::string out = Base64Encode(in); |
109 | | std::cout << out << std::endl; |
110 | | if (out != "UG93ZXJETlMgVGVzdCBTdHJpbmcgMQ==") { |
111 | | std::cerr << "output did not match expected data" << std::endl; |
112 | | } |
113 | | std::string roundtrip; |
114 | | B64Decode(out, roundtrip); |
115 | | std::cout << roundtrip << std::endl; |
116 | | if (roundtrip != in) { |
117 | | std::cerr << "roundtripped data did not match input data" << std::endl; |
118 | | } |
119 | | return 0; |
120 | | } |
121 | | #endif |