| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ssl_options_impl.hpp" |
| 23 |
|
| 24 |
// https://stackoverflow.com/questions/31657499/how-to-detect-stdlib-libc-in-the-preprocessor |
| 25 |
#include <ciso646> |
| 26 |
|
| 27 |
#include "proton/ssl.hpp" |
| 28 |
#include "proton/error.hpp" |
| 29 |
#include "msg.hpp" |
| 30 |
|
| 31 |
#include <proton/ssl.h> |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
void inline set_cred(pn_ssl_domain_t *dom, const std::string &main, const std::string &extra, const std::string &pass, bool pwset) { |
| 36 |
const char *cred2 = extra.empty() ? NULL : extra.c_str(); |
| 37 |
const char *pw = pwset ? pass.c_str() : NULL; |
| 38 |
if (pn_ssl_domain_set_credentials(dom, main.c_str(), cred2, pw)) |
| 39 |
throw proton::error(MSG("SSL certificate initialization failure for " << main << ":" << |
| 40 |
(cred2 ? cred2 : "NULL") << ":" << (pw ? pw : "NULL"))); |
| 41 |
} |
| 42 |
|
| 43 |
void inline set_trusted_ca_db(pn_ssl_domain_t *dom, const std::string &trust_db) { |
| 44 |
if (pn_ssl_domain_set_trusted_ca_db(dom, trust_db.c_str())) |
| 45 |
throw proton::error(MSG("SSL trust store initialization failure for " << trust_db)); |
| 46 |
} |
| 47 |
|
| 48 |
void inline set_client_verify_mode(pn_ssl_domain_t *dom, enum proton::ssl::verify_mode mode) { |
| 49 |
if (pn_ssl_domain_set_peer_authentication(dom, pn_ssl_verify_mode_t(mode), NULL)) |
| 50 |
throw proton::error(MSG("SSL client verify mode failure")); |
| 51 |
} |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
namespace proton { |
| 56 |
|
| 57 |
ssl_options_impl::ssl_options_impl(bool is_server) : |
| 58 |
pn_domain_(pn_ssl_domain(is_server ? PN_SSL_MODE_SERVER : PN_SSL_MODE_CLIENT)), |
| 59 |
refcount_(1) { |
| 60 |
if (!pn_domain_) throw proton::error(MSG("SSL/TLS unavailable")); |
| 61 |
} |
| 62 |
|
| 63 |
ssl_options_impl::~ssl_options_impl() { |
| 64 |
pn_ssl_domain_free(pn_domain_); |
| 65 |
} |
| 66 |
|
| 67 |
ssl_server_options& ssl_server_options::operator=(const ssl_server_options& x) { |
| 68 |
if (&x!=this) { |
| 69 |
if (impl_) impl_->decref(); |
| 70 |
impl_ = x.impl_; |
| 71 |
if (impl_) impl_->incref(); |
| 72 |
} |
| 73 |
return *this; |
| 74 |
} |
| 75 |
|
| 76 |
ssl_server_options::ssl_server_options(const ssl_server_options& x): impl_(x.impl_) { |
| 77 |
if (impl_) impl_->incref(); |
| 78 |
} |
| 79 |
|
| 80 |
ssl_server_options::~ssl_server_options() { |
| 81 |
if (impl_) impl_->decref(); |
| 82 |
} |
| 83 |
|
| 84 |
ssl_server_options::ssl_server_options(const ssl_certificate &cert) : impl_(new impl) { |
| 85 |
set_cred(impl_->pn_domain(), cert.certdb_main_, cert.certdb_extra_, cert.passwd_, cert.pw_set_); |
| 86 |
} |
| 87 |
|
| 88 |
ssl_server_options::ssl_server_options( |
| 89 |
const ssl_certificate &cert, |
| 90 |
const std::string &trust_db, |
| 91 |
const std::string &advertise_db, |
| 92 |
enum ssl::verify_mode mode) : impl_(new impl) |
| 93 |
{ |
| 94 |
pn_ssl_domain_t* dom = impl_->pn_domain(); |
| 95 |
set_cred(dom, cert.certdb_main_, cert.certdb_extra_, cert.passwd_, cert.pw_set_); |
| 96 |
set_trusted_ca_db(dom, trust_db.c_str()); |
| 97 |
const std::string &db = advertise_db.empty() ? trust_db : advertise_db; |
| 98 |
if (pn_ssl_domain_set_peer_authentication(dom, pn_ssl_verify_mode_t(mode), db.c_str())) |
| 99 |
throw error(MSG("SSL server configuration failure requiring client certificates using " << db)); |
| 100 |
} |
| 101 |
|
| 102 |
ssl_server_options::ssl_server_options() : impl_(0) {} |
| 103 |
|
| 104 |
ssl_client_options::ssl_client_options(const ssl_client_options& x): impl_(x.impl_) { |
| 105 |
if (impl_) impl_->incref(); |
| 106 |
} |
| 107 |
|
| 108 |
ssl_client_options& ssl_client_options::operator=(const ssl_client_options& x) { |
| 109 |
if (&x!=this) { |
| 110 |
if (impl_) impl_->decref(); |
| 111 |
impl_ = x.impl_; |
| 112 |
if (impl_) impl_->incref(); |
| 113 |
} |
| 114 |
return *this; |
| 115 |
} |
| 116 |
|
| 117 |
ssl_client_options::~ssl_client_options() { |
| 118 |
if (impl_) impl_->decref(); |
| 119 |
} |
| 120 |
|
| 121 |
ssl_client_options::ssl_client_options() : impl_(0) {} |
| 122 |
|
| 123 |
ssl_client_options::ssl_client_options(enum ssl::verify_mode mode) : impl_(new impl) { |
| 124 |
pn_ssl_domain_t* dom = impl_->pn_domain(); |
| 125 |
set_client_verify_mode(dom, mode); |
| 126 |
} |
| 127 |
|
| 128 |
ssl_client_options::ssl_client_options(const std::string &trust_db, enum ssl::verify_mode mode) : impl_(new impl) { |
| 129 |
pn_ssl_domain_t* dom = impl_->pn_domain(); |
| 130 |
set_trusted_ca_db(dom, trust_db); |
| 131 |
set_client_verify_mode(dom, mode); |
| 132 |
} |
| 133 |
|
| 134 |
ssl_client_options::ssl_client_options(const ssl_certificate &cert, const std::string &trust_db, enum ssl::verify_mode mode) : impl_(new impl) { |
| 135 |
pn_ssl_domain_t* dom = impl_->pn_domain(); |
| 136 |
set_cred(dom, cert.certdb_main_, cert.certdb_extra_, cert.passwd_, cert.pw_set_); |
| 137 |
set_trusted_ca_db(dom, trust_db); |
| 138 |
set_client_verify_mode(dom, mode); |
| 139 |
} |
| 140 |
|
| 141 |
ssl_certificate::ssl_certificate(const std::string &main) |
| 142 |
: certdb_main_(main), pw_set_(false) {} |
| 143 |
|
| 144 |
ssl_certificate::ssl_certificate(const std::string &main, const std::string &extra) |
| 145 |
: certdb_main_(main), certdb_extra_(extra), pw_set_(false) {} |
| 146 |
|
| 147 |
ssl_certificate::ssl_certificate(const std::string &main, const std::string &extra, const std::string &pw) |
| 148 |
: certdb_main_(main), certdb_extra_(extra), passwd_(pw), pw_set_(true) {} |
| 149 |
|
| 150 |
} // namespace |
| 151 |
|
| 152 |
// Hack alert - don't do this at home - just design better initially! |
| 153 |
// For backwards ABI compatibility we need to define some symbols: |
| 154 |
// |
| 155 |
|
| 156 |
// Don't do this with libc++ or with Visual Studio |
| 157 |
#if !defined(_LIBCPP_VERSION) && !defined(_MSC_VER) |
| 158 |
|
| 159 |
// |
| 160 |
// These are a bit easier as the entire class has been removed so we can just define the class here |
| 161 |
// They do rely on the impl_ member being effectively the same type and structure location from |
| 162 |
// The previous parent type ssl_domain to ssl_server_options/ssl_client_options. |
| 163 |
// |
| 164 |
// PN_CPP_EXTERN proton::internal::ssl_domain::ssl_domain(const ssl_domain&); |
| 165 |
// PN_CPP_EXTERN proton::internal::ssl_domain::ssl_domain& operator=(const ssl_domain&); |
| 166 |
// PN_CPP_EXTERN proton::internal::ssl_domain::~ssl_domain(); |
| 167 |
// |
| 168 |
namespace proton { |
| 169 |
namespace internal { |
| 170 |
class ssl_domain { |
| 171 |
PN_CPP_EXTERN ssl_domain(const ssl_domain&); |
| 172 |
PN_CPP_EXTERN ssl_domain& operator=(const ssl_domain&); |
| 173 |
PN_CPP_EXTERN ~ssl_domain(); |
| 174 |
|
| 175 |
ssl_options_impl* impl_; |
| 176 |
}; |
| 177 |
|
| 178 |
ssl_domain::ssl_domain(const ssl_domain& x): impl_(x.impl_) { if (impl_) impl_->incref(); } |
| 179 |
ssl_domain& ssl_domain::operator=(const ssl_domain& x) { |
| 180 |
if (&x != this) { |
| 181 |
if (impl_) impl_->decref(); |
| 182 |
impl_ = x.impl_; |
| 183 |
if (impl_) impl_->incref(); |
| 184 |
} |
| 185 |
return *this; |
| 186 |
} |
| 187 |
ssl_domain::~ssl_domain() { if (impl_) impl_->decref(); } |
| 188 |
}} |
| 189 |
|
| 190 |
// These are a bit harder because they can't be publicly in the classes anymore so just use alias definitions |
| 191 |
// but that needs the mangled symbol names unfortunately. |
| 192 |
// |
| 193 |
// PN_CPP_EXTERN proton::ssl_server_options::ssl_server_options(ssl_certificate &cert); |
| 194 |
// PN_CPP_EXTERN proton::ssl_server_options::ssl_server_options(ssl_certificate &cert, const std::string &trust_db, |
| 195 |
// const std::string &advertise_db = std::string(), |
| 196 |
// enum ssl::verify_mode mode = ssl::VERIFY_PEER); |
| 197 |
// PN_CPP_EXTERN proton::ssl_client_options::ssl_client_options(ssl_certificate&, const std::string &trust_db, |
| 198 |
// enum ssl::verify_mode = ssl::VERIFY_PEER_NAME); |
| 199 |
|
| 200 |
extern "C" { |
| 201 |
#pragma GCC diagnostic ignored "-Wpragmas" |
| 202 |
#pragma GCC diagnostic ignored "-Wunknown-warning-option" |
| 203 |
#pragma GCC diagnostic ignored "-Wattribute-alias" |
| 204 |
// 2 variants of each constructor (base & complete) |
| 205 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC1ERNS_15ssl_certificateE() |
| 206 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC1ERKNS_15ssl_certificateE"))); |
| 207 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC2ERNS_15ssl_certificateE() |
| 208 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC2ERKNS_15ssl_certificateE"))); |
| 209 |
#if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI>0 |
| 210 |
// Post gcc 5.1 "C++11" ABI |
| 211 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC1ERNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_NS_3ssl11verify_modeE() |
| 212 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC1ERKNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_NS_3ssl11verify_modeE"))); |
| 213 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC2ERNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_NS_3ssl11verify_modeE() |
| 214 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC2ERKNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_NS_3ssl11verify_modeE"))); |
| 215 |
PN_CPP_EXTERN void _ZN6proton18ssl_client_optionsC1ERNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_3ssl11verify_modeE() |
| 216 |
__attribute__((alias("_ZN6proton18ssl_client_optionsC1ERKNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_3ssl11verify_modeE"))); |
| 217 |
PN_CPP_EXTERN void _ZN6proton18ssl_client_optionsC2ERNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_3ssl11verify_modeE() |
| 218 |
__attribute__((alias("_ZN6proton18ssl_client_optionsC2ERKNS_15ssl_certificateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_3ssl11verify_modeE"))); |
| 219 |
#else |
| 220 |
// Pre gcc 5.1 ABI |
| 221 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC1ERNS_15ssl_certificateERKSsS4_NS_3ssl11verify_modeE() |
| 222 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC1ERKNS_15ssl_certificateERKSsS5_NS_3ssl11verify_modeE"))); |
| 223 |
PN_CPP_EXTERN void _ZN6proton18ssl_server_optionsC2ERNS_15ssl_certificateERKSsS4_NS_3ssl11verify_modeE() |
| 224 |
__attribute__((alias("_ZN6proton18ssl_server_optionsC2ERKNS_15ssl_certificateERKSsS5_NS_3ssl11verify_modeE"))); |
| 225 |
PN_CPP_EXTERN void _ZN6proton18ssl_client_optionsC1ERNS_15ssl_certificateERKSsNS_3ssl11verify_modeE() |
| 226 |
__attribute__((alias("_ZN6proton18ssl_client_optionsC1ERKNS_15ssl_certificateERKSsNS_3ssl11verify_modeE"))); |
| 227 |
PN_CPP_EXTERN void _ZN6proton18ssl_client_optionsC2ERNS_15ssl_certificateERKSsNS_3ssl11verify_modeE() |
| 228 |
__attribute__((alias("_ZN6proton18ssl_client_optionsC2ERKNS_15ssl_certificateERKSsNS_3ssl11verify_modeE"))); |
| 229 |
#endif |
| 230 |
} |
| 231 |
|
| 232 |
#endif |