/// $PackageInfo: require_system("centos") gnutls-devel pkgconfig
/// $PackageInfo: require_system("darwin") gnutls pkg-config
+/// $PackageInfo: require_system("debian" "1.0" "7.99") libgcrypt11-dev
+/// $PackageInfo: require_system("debian") gnutls-bin libgnutls28-dev pkg-config
/// $PackageInfo: require_system("ubuntu" "1.0" "13.10") libgcrypt11-dev
-/// $PackageInfo: require_system("ubuntu" "14.04") gnutls-bin libgnutls-dev pkg-config
+/// $PackageInfo: require_system("ubuntu") gnutls-bin libgnutls-dev pkg-config
#include "inspircd.h"
#include "modules/ssl.h"
#define GNUTLS_NEW_PRIO_API
#endif
-#if (!INSPIRCD_GNUTLS_HAS_VERSION(2, 0, 0))
-typedef gnutls_certificate_credentials_t gnutls_certificate_credentials;
-typedef gnutls_dh_params_t gnutls_dh_params;
-#endif
-
enum issl_status { ISSL_NONE, ISSL_HANDSHAKING, ISSL_HANDSHAKEN };
#if INSPIRCD_GNUTLS_HAS_VERSION(2, 12, 0)
static Module* thismod;
-class RandGen : public HandlerBase2<void, char*, size_t>
+class RandGen
{
public:
- void Call(char* buffer, size_t len)
+ static void Call(char* buffer, size_t len)
{
#ifdef GNUTLS_HAS_RND
gnutls_rnd(GNUTLS_RND_RANDOM, buffer, len);
int ret() const { return retval; }
};
- class Profile : public refcountbase
+ class Profile
{
/** Name of this profile
*/
*/
const bool requestclientcert;
- Profile(const std::string& profilename, const std::string& certstr, const std::string& keystr,
- std::auto_ptr<DHParams>& DH, unsigned int mindh, const std::string& hashstr,
- const std::string& priostr, std::auto_ptr<X509CertList>& CA, std::auto_ptr<X509CRL>& CRL,
- unsigned int recsize, bool Requestclientcert)
- : name(profilename)
- , x509cred(certstr, keystr)
- , min_dh_bits(mindh)
- , hash(hashstr)
- , priority(priostr)
- , outrecsize(recsize)
- , requestclientcert(Requestclientcert)
- {
- x509cred.SetDH(DH);
- x509cred.SetCA(CA, CRL);
- }
-
static std::string ReadFile(const std::string& filename)
{
FileReader reader(filename);
}
public:
- static reference<Profile> Create(const std::string& profilename, ConfigTag* tag)
+ struct Config
{
- std::string certstr = ReadFile(tag->getString("certfile", "cert.pem"));
- std::string keystr = ReadFile(tag->getString("keyfile", "key.pem"));
-
- std::auto_ptr<DHParams> dh = DHParams::Import(ReadFile(tag->getString("dhfile", "dhparams.pem")));
+ std::string name;
- std::string priostr = GetPrioStr(profilename, tag);
- unsigned int mindh = tag->getInt("mindhbits", 1024);
- std::string hashstr = tag->getString("hash", "md5");
-
- // Load trusted CA and revocation list, if set
std::auto_ptr<X509CertList> ca;
std::auto_ptr<X509CRL> crl;
- std::string filename = tag->getString("cafile");
- if (!filename.empty())
- {
- ca.reset(new X509CertList(ReadFile(filename)));
- filename = tag->getString("crlfile");
+ std::string certstr;
+ std::string keystr;
+ std::auto_ptr<DHParams> dh;
+
+ std::string priostr;
+ unsigned int mindh;
+ std::string hashstr;
+
+ unsigned int outrecsize;
+ bool requestclientcert;
+
+ Config(const std::string& profilename, ConfigTag* tag)
+ : name(profilename)
+ , certstr(ReadFile(tag->getString("certfile", "cert.pem")))
+ , keystr(ReadFile(tag->getString("keyfile", "key.pem")))
+ , dh(DHParams::Import(ReadFile(tag->getString("dhfile", "dhparams.pem"))))
+ , priostr(GetPrioStr(profilename, tag))
+ , mindh(tag->getUInt("mindhbits", 1024))
+ , hashstr(tag->getString("hash", "md5"))
+ , requestclientcert(tag->getBool("requestclientcert", true))
+ {
+ // Load trusted CA and revocation list, if set
+ std::string filename = tag->getString("cafile");
if (!filename.empty())
- crl.reset(new X509CRL(ReadFile(filename)));
- }
+ {
+ ca.reset(new X509CertList(ReadFile(filename)));
+
+ filename = tag->getString("crlfile");
+ if (!filename.empty())
+ crl.reset(new X509CRL(ReadFile(filename)));
+ }
#ifdef INSPIRCD_GNUTLS_HAS_CORK
- // If cork support is available outrecsize represents the (rough) max amount of data we give GnuTLS while corked
- unsigned int outrecsize = tag->getInt("outrecsize", 2048, 512);
+ // If cork support is available outrecsize represents the (rough) max amount of data we give GnuTLS while corked
+ outrecsize = tag->getUInt("outrecsize", 2048, 512);
#else
- unsigned int outrecsize = tag->getInt("outrecsize", 2048, 512, 16384);
+ outrecsize = tag->getUInt("outrecsize", 2048, 512, 16384);
#endif
+ }
+ };
- const bool requestclientcert = tag->getBool("requestclientcert", true);
-
- return new Profile(profilename, certstr, keystr, dh, mindh, hashstr, priostr, ca, crl, outrecsize, requestclientcert);
+ Profile(Config& config)
+ : name(config.name)
+ , x509cred(config.certstr, config.keystr)
+ , min_dh_bits(config.mindh)
+ , hash(config.hashstr)
+ , priority(config.priostr)
+ , outrecsize(config.outrecsize)
+ , requestclientcert(config.requestclientcert)
+ {
+ x509cred.SetDH(config.dh);
+ x509cred.SetCA(config.ca, config.crl);
}
-
/** Set up the given session with the settings in this profile
*/
void SetupSession(gnutls_session_t sess)
private:
gnutls_session_t sess;
issl_status status;
- reference<GnuTLS::Profile> profile;
#ifdef INSPIRCD_GNUTLS_HAS_CORK
size_t gbuffersize;
#endif
issuer.clear();
}
- if ((ret = gnutls_x509_crt_get_fingerprint(cert, profile->GetHash(), digest, &digest_size)) < 0)
+ if ((ret = gnutls_x509_crt_get_fingerprint(cert, GetProfile().GetHash(), digest, &digest_size)) < 0)
{
certinfo->error = gnutls_strerror(ret);
}
#endif // INSPIRCD_GNUTLS_HAS_VECTOR_PUSH
public:
- GnuTLSIOHook(IOHookProvider* hookprov, StreamSocket* sock, inspircd_gnutls_session_init_flags_t flags, const reference<GnuTLS::Profile>& sslprofile)
+ GnuTLSIOHook(IOHookProvider* hookprov, StreamSocket* sock, inspircd_gnutls_session_init_flags_t flags)
: SSLIOHook(hookprov)
, sess(NULL)
, status(ISSL_NONE)
- , profile(sslprofile)
#ifdef INSPIRCD_GNUTLS_HAS_CORK
, gbuffersize(0)
#endif
gnutls_transport_set_push_function(sess, gnutls_push_wrapper);
#endif
gnutls_transport_set_pull_function(sess, gnutls_pull_wrapper);
- profile->SetupSession(sess);
+ GetProfile().SetupSession(sess);
sock->AddIOHook(this);
Handshake(sock);
// GnuTLS buffer is empty but sendq is not, begin sending data from the sendq
gnutls_record_cork(this->sess);
- while ((!sendq.empty()) && (gbuffersize < profile->GetOutgoingRecordSize()))
+ while ((!sendq.empty()) && (gbuffersize < GetProfile().GetOutgoingRecordSize()))
{
const StreamSocket::SendQueue::Element& elem = sendq.front();
gbuffersize += elem.length();
while (!sendq.empty())
{
- FlattenSendQueue(sendq, profile->GetOutgoingRecordSize());
+ FlattenSendQueue(sendq, GetProfile().GetOutgoingRecordSize());
const StreamSocket::SendQueue::Element& buffer = sendq.front();
ret = HandleWriteRet(user, gnutls_record_send(this->sess, buffer.data(), buffer.length()));
out.append(UnknownIfNULL(gnutls_mac_get_name(gnutls_mac_get(sess))));
}
- GnuTLS::Profile* GetProfile() { return profile; }
+ bool GetServerName(std::string& out) const CXX11_OVERRIDE
+ {
+ std::vector<char> nameBuffer;
+ size_t nameLength = 0;
+ unsigned int nameType = GNUTLS_NAME_DNS;
+
+ // First, determine the size of the hostname.
+ if (gnutls_server_name_get(sess, &nameBuffer[0], &nameLength, &nameType, 0) != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return false;
+
+ // Then retrieve the hostname.
+ nameBuffer.resize(nameLength);
+ if (gnutls_server_name_get(sess, &nameBuffer[0], &nameLength, &nameType, 0) != GNUTLS_E_SUCCESS)
+ return false;
+
+ out.append(&nameBuffer[0]);
+ return true;
+ }
+
+ GnuTLS::Profile& GetProfile();
bool IsHandshakeDone() const { return (status == ISSL_HANDSHAKEN); }
};
st->key_type = GNUTLS_PRIVKEY_X509;
#endif
StreamSocket* sock = reinterpret_cast<StreamSocket*>(gnutls_transport_get_ptr(sess));
- GnuTLS::X509Credentials& cred = static_cast<GnuTLSIOHook*>(sock->GetModHook(thismod))->GetProfile()->GetX509Credentials();
+ GnuTLS::X509Credentials& cred = static_cast<GnuTLSIOHook*>(sock->GetModHook(thismod))->GetProfile().GetX509Credentials();
st->ncerts = cred.certs.size();
st->cert.x509 = cred.certs.raw();
return 0;
}
-class GnuTLSIOHookProvider : public refcountbase, public IOHookProvider
+class GnuTLSIOHookProvider : public IOHookProvider
{
- reference<GnuTLS::Profile> profile;
+ GnuTLS::Profile profile;
public:
- GnuTLSIOHookProvider(Module* mod, reference<GnuTLS::Profile>& prof)
- : IOHookProvider(mod, "ssl/" + prof->GetName(), IOHookProvider::IOH_SSL)
- , profile(prof)
+ GnuTLSIOHookProvider(Module* mod, GnuTLS::Profile::Config& config)
+ : IOHookProvider(mod, "ssl/" + config.name, IOHookProvider::IOH_SSL)
+ , profile(config)
{
ServerInstance->Modules->AddService(*this);
}
void OnAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
{
- new GnuTLSIOHook(this, sock, GNUTLS_SERVER, profile);
+ new GnuTLSIOHook(this, sock, GNUTLS_SERVER);
}
void OnConnect(StreamSocket* sock) CXX11_OVERRIDE
{
- new GnuTLSIOHook(this, sock, GNUTLS_CLIENT, profile);
+ new GnuTLSIOHook(this, sock, GNUTLS_CLIENT);
}
+
+ GnuTLS::Profile& GetProfile() { return profile; }
};
+GnuTLS::Profile& GnuTLSIOHook::GetProfile()
+{
+ IOHookProvider* hookprov = prov;
+ return static_cast<GnuTLSIOHookProvider*>(hookprov)->GetProfile();
+}
+
class ModuleSSLGnuTLS : public Module
{
typedef std::vector<reference<GnuTLSIOHookProvider> > ProfileList;
// First member of the class, gets constructed first and destructed last
GnuTLS::Init libinit;
- RandGen randhandler;
ProfileList profiles;
void ReadProfiles()
try
{
- reference<GnuTLS::Profile> profile(GnuTLS::Profile::Create(defname, tag));
- newprofiles.push_back(new GnuTLSIOHookProvider(this, profile));
+ GnuTLS::Profile::Config profileconfig(defname, tag);
+ newprofiles.push_back(new GnuTLSIOHookProvider(this, profileconfig));
}
catch (CoreException& ex)
{
continue;
}
- reference<GnuTLS::Profile> profile;
+ reference<GnuTLSIOHookProvider> prov;
try
{
- profile = GnuTLS::Profile::Create(name, tag);
+ GnuTLS::Profile::Config profileconfig(name, tag);
+ prov = new GnuTLSIOHookProvider(this, profileconfig);
}
catch (CoreException& ex)
{
throw ModuleException("Error while initializing SSL profile \"" + name + "\" at " + tag->getTagLocation() + " - " + ex.GetReason());
}
- newprofiles.push_back(new GnuTLSIOHookProvider(this, profile));
+ newprofiles.push_back(prov);
}
// New profiles are ok, begin using them
// Old profiles are deleted when their refcount drops to zero
+ for (ProfileList::iterator i = profiles.begin(); i != profiles.end(); ++i)
+ {
+ GnuTLSIOHookProvider& prov = **i;
+ ServerInstance->Modules.DelService(prov);
+ }
+
profiles.swap(newprofiles);
}
{
ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "GnuTLS lib version %s module was compiled for " GNUTLS_VERSION, gnutls_check_version(NULL));
ReadProfiles();
- ServerInstance->GenRandom = &randhandler;
+ ServerInstance->GenRandom = RandGen::Call;
}
void OnModuleRehash(User* user, const std::string ¶m) CXX11_OVERRIDE
~ModuleSSLGnuTLS()
{
- ServerInstance->GenRandom = &ServerInstance->HandleGenRandom;
+ ServerInstance->GenRandom = &InspIRCd::DefaultGenRandom;
}
- void OnCleanup(int target_type, void* item) CXX11_OVERRIDE
+ void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item) CXX11_OVERRIDE
{
- if(target_type == TYPE_USER)
+ if (type == ExtensionItem::EXT_USER)
{
LocalUser* user = IS_LOCAL(static_cast<User*>(item));