icinga2/lib/base/tlsutility.hpp

77 lines
3.6 KiB
C++
Raw Normal View History

2013-03-18 19:02:42 +01:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
2013-03-18 19:02:42 +01:00
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef TLSUTILITY_H
#define TLSUTILITY_H
2014-05-25 16:23:35 +02:00
#include "base/i2-base.hpp"
#include "base/object.hpp"
#include "base/string.hpp"
2013-03-18 19:02:42 +01:00
#include <openssl/ssl.h>
2013-09-03 14:05:03 +02:00
#include <openssl/bio.h>
2013-03-18 19:02:42 +01:00
#include <openssl/err.h>
2013-09-03 14:05:03 +02:00
#include <openssl/comp.h>
2013-09-04 15:47:15 +02:00
#include <openssl/sha.h>
#include <openssl/x509v3.h>
2014-10-16 13:36:25 +02:00
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <boost/smart_ptr/shared_ptr.hpp>
2014-12-15 10:16:06 +01:00
#include <boost/exception/info.hpp>
2013-03-18 19:02:42 +01:00
namespace icinga
{
void I2_BASE_API InitializeOpenSSL(void);
boost::shared_ptr<SSL_CTX> I2_BASE_API MakeSSLContext(const String& pubkey = String(), const String& privkey = String(), const String& cakey = String());
void I2_BASE_API AddCRLToSSLContext(const boost::shared_ptr<SSL_CTX>& context, const String& crlPath);
String I2_BASE_API GetCertificateCN(const boost::shared_ptr<X509>& certificate);
boost::shared_ptr<X509> I2_BASE_API GetX509Certificate(const String& pemfile);
int I2_BASE_API MakeX509CSR(const String& cn, const String& keyfile, const String& csrfile = String(), const String& certfile = String(), const String& serialFile = String(), bool ca = false);
boost::shared_ptr<X509> I2_BASE_API CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME *issuer, EVP_PKEY *cakey, bool ca, const String& serialfile = String());
String I2_BASE_API GetIcingaCADir(void);
String I2_BASE_API CertificateToString(const boost::shared_ptr<X509>& cert);
boost::shared_ptr<X509> I2_BASE_API CreateCertIcingaCA(EVP_PKEY *pubkey, X509_NAME *subject);
2014-10-16 13:36:25 +02:00
String I2_BASE_API PBKDF2_SHA1(const String& password, const String& salt, int iterations);
2013-10-10 23:06:28 +02:00
String I2_BASE_API SHA256(const String& s);
String I2_BASE_API RandomString(int length);
2013-03-18 19:02:42 +01:00
class I2_BASE_API openssl_error : virtual public std::exception, virtual public boost::exception { };
struct errinfo_openssl_error_;
typedef boost::error_info<struct errinfo_openssl_error_, unsigned long> errinfo_openssl_error;
2013-03-18 19:02:42 +01:00
inline std::string to_string(const errinfo_openssl_error& e)
{
2014-08-17 17:57:20 +02:00
std::ostringstream tmp;
int code = e.value();
char errbuf[120];
2013-03-18 19:02:42 +01:00
2014-08-17 17:57:20 +02:00
const char *message = ERR_error_string(code, errbuf);
2013-03-18 19:02:42 +01:00
2014-08-17 17:57:20 +02:00
if (message == NULL)
message = "Unknown error.";
2013-03-18 19:02:42 +01:00
2014-08-17 17:57:20 +02:00
tmp << code << ", \"" << message << "\"";
return "[errinfo_openssl_error]" + tmp.str() + "\n";
2013-03-18 19:02:42 +01:00
}
}
#endif /* TLSUTILITY_H */