openldap/contrib/ldapc++/src/TlsOptions.cpp

123 lines
3.4 KiB
C++
Raw Normal View History

// $OpenLDAP$
/*
* Copyright 2010, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "TlsOptions.h"
#include "LDAPException.h"
enum opttype {
INT=0,
STRING,
OTHER
};
typedef struct tls_optmap {
int optval;
opttype type;
} tls_optmap_t;
static tls_optmap_t optmap[] = {
{ LDAP_OPT_X_TLS_CACERTFILE, STRING },
{ LDAP_OPT_X_TLS_CACERTDIR, STRING },
{ LDAP_OPT_X_TLS_CERTFILE, STRING },
{ LDAP_OPT_X_TLS_KEYFILE, STRING },
{ LDAP_OPT_X_TLS_REQUIRE_CERT, INT },
{ LDAP_OPT_X_TLS_PROTOCOL_MIN, INT },
{ LDAP_OPT_X_TLS_CIPHER_SUITE, STRING },
{ LDAP_OPT_X_TLS_RANDOM_FILE, STRING },
{ LDAP_OPT_X_TLS_CRLCHECK, INT },
{ LDAP_OPT_X_TLS_DHFILE, STRING },
{ LDAP_OPT_X_TLS_NEWCTX, INT }
};
#if 0 /* not implemented currently */
static const int TLS_CRLFILE /* GNUtls only */
static const int TLS_SSL_CTX /* OpenSSL SSL* */
static const int TLS_CONNECT_CB
static const int TLS_CONNECT_ARG
#endif
2010-02-16 12:44:22 -05:00
static void checkOpt( TlsOptions::tls_option opt, opttype type ) {
2010-02-16 06:55:33 -05:00
if ( opt < TlsOptions::CACERTFILE || opt >= TlsOptions::LASTOPT ){
throw( LDAPException( LDAP_PARAM_ERROR, "unknown Option" ) );
}
if ( optmap[opt].type != type ){
throw( LDAPException( LDAP_PARAM_ERROR, "not a string option" ) );
}
}
2010-02-18 11:22:49 -05:00
TlsOptions::TlsOptions() : m_ld(NULL) {}
TlsOptions::TlsOptions( LDAP* ld ): m_ld(ld) { }
2010-02-16 12:44:22 -05:00
void TlsOptions::setOption( tls_option opt, const std::string& value ) const {
checkOpt(opt, STRING);
2010-02-16 12:44:22 -05:00
this->setOption( opt, value.empty() ? NULL : (void*) value.c_str() );
}
2010-02-16 12:44:22 -05:00
void TlsOptions::setOption( tls_option opt, int value ) const {
checkOpt(opt, INT);
this->setOption( opt, (void*) &value);
}
2010-02-16 12:44:22 -05:00
void TlsOptions::setOption( tls_option opt, void *value ) const {
int ret = ldap_set_option( m_ld, optmap[opt].optval, value);
if ( ret != LDAP_OPT_SUCCESS )
{
if ( ret != LDAP_OPT_ERROR ){
throw( LDAPException( ret ));
} else {
throw( LDAPException( LDAP_PARAM_ERROR, "error while setting TLS option" ) );
}
}
2010-02-16 12:48:14 -05:00
if ( m_ld ){
this->newCtx();
}
}
2010-02-16 12:44:22 -05:00
void TlsOptions::getOption( tls_option opt, void* value ) const {
int ret = ldap_get_option( m_ld, optmap[opt].optval, value);
if ( ret != LDAP_OPT_SUCCESS )
{
if ( ret != LDAP_OPT_ERROR ){
throw( LDAPException( ret ));
} else {
throw( LDAPException( LDAP_PARAM_ERROR, "error while reading TLS option" ) );
}
}
}
int TlsOptions::getIntOption( tls_option opt ) const {
int value;
checkOpt(opt, INT);
ldap_get_option( m_ld, optmap[opt].optval, (void*) &value);
return value;
}
std::string TlsOptions::getStringOption( tls_option opt ) const {
char *value;
checkOpt(opt, STRING);
ldap_get_option( m_ld, optmap[opt].optval, (void*) &value);
std::string strval;
if (value)
{
strval=std::string(value);
ldap_memfree(value);
}
return strval;
}
2010-02-16 12:48:14 -05:00
void TlsOptions::newCtx() const {
int ret = ldap_set_option( m_ld, LDAP_OPT_X_TLS_NEWCTX, LDAP_OPT_ON);
if ( ret != LDAP_OPT_SUCCESS )
{
if ( ret != LDAP_OPT_ERROR ){
throw( LDAPException( ret ));
} else {
throw( LDAPException( LDAP_LOCAL_ERROR, "error while renewing TLS context" ) );
}
}
}