Line | Count | Source (jump to first uncovered line) |
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 | | #ifdef HAVE_CONFIG_H |
23 | | #include "config.h" |
24 | | #endif |
25 | | #include "base64.hh" |
26 | | #include <stdexcept> |
27 | | #include <boost/scoped_array.hpp> |
28 | | #include <openssl/bio.h> |
29 | | #include <openssl/evp.h> |
30 | | |
31 | | template<typename Container> int B64Decode(const std::string& src, Container& dst) |
32 | 0 | { |
33 | 0 | if (src.empty() ) { |
34 | 0 | dst.clear(); |
35 | 0 | return 0; |
36 | 0 | } |
37 | 0 | int dlen = ( src.length() * 6 + 7 ) / 8 ; |
38 | 0 | ssize_t olen = 0; |
39 | 0 | dst.resize(dlen); |
40 | 0 | BIO *bio, *b64; |
41 | 0 | bio = BIO_new(BIO_s_mem()); |
42 | 0 | BIO_write(bio, src.c_str(), src.length()); |
43 | 0 | b64 = BIO_new(BIO_f_base64()); |
44 | 0 | bio = BIO_push(b64, bio); |
45 | 0 | BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); |
46 | 0 | olen = BIO_read(b64, &dst.at(0), dlen); |
47 | 0 | if ((olen == 0 || olen == -1) && BIO_should_retry(bio)) { |
48 | 0 | BIO_free_all(bio); |
49 | 0 | throw std::runtime_error("BIO_read failed to read all data from memory buffer"); |
50 | 0 | } |
51 | 0 | BIO_free_all(bio); |
52 | 0 | if (olen > 0) { |
53 | 0 | dst.resize(olen); |
54 | 0 | return 0; |
55 | 0 | } |
56 | 0 | return -1; |
57 | 0 | } |
58 | | |
59 | | template int B64Decode<std::string>(const std::string& strInput, std::string& strOutput); |
60 | | |
61 | | std::string Base64Encode(const std::string& src) |
62 | 0 | { |
63 | 0 | if (!src.empty()) { |
64 | 0 | size_t olen = 0; |
65 | 0 | BIO *bio, *b64; |
66 | 0 | b64 = BIO_new(BIO_f_base64()); |
67 | 0 | bio = BIO_new(BIO_s_mem()); |
68 | 0 | bio = BIO_push(b64, bio); |
69 | 0 | BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); |
70 | 0 | int bioWriteRet = BIO_write(bio, src.c_str(), src.length()); |
71 | 0 | if (bioWriteRet < 0 || (size_t) bioWriteRet != src.length()) { |
72 | 0 | BIO_free_all(bio); |
73 | 0 | throw std::runtime_error("BIO_write failed to write all data to memory buffer"); |
74 | 0 | } |
75 | 0 | (void)BIO_flush(bio); |
76 | 0 | char* pp; |
77 | 0 | std::string out; |
78 | 0 | olen = BIO_get_mem_data(bio, &pp); |
79 | 0 | if (olen > 0) { |
80 | 0 | out = std::string(pp, olen); |
81 | 0 | } |
82 | 0 | BIO_free_all(bio); |
83 | 0 | return out; |
84 | 0 | } |
85 | 0 | return ""; |
86 | 0 | } |
87 | | |
88 | | #if 0 |
89 | | #include <iostream> |
90 | | int main() { |
91 | | std::string in = "PowerDNS Test String 1"; |
92 | | std::string out = Base64Encode(in); |
93 | | std::cout << out << std::endl; |
94 | | if (out != "UG93ZXJETlMgVGVzdCBTdHJpbmcgMQ==") { |
95 | | std::cerr << "output did not match expected data" << std::endl; |
96 | | } |
97 | | std::string roundtrip; |
98 | | B64Decode(out, roundtrip); |
99 | | std::cout << roundtrip << std::endl; |
100 | | if (roundtrip != in) { |
101 | | std::cerr << "roundtripped data did not match input data" << std::endl; |
102 | | } |
103 | | return 0; |
104 | | } |
105 | | #endif |