1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include "base/string.hpp"
26 #include <openssl/ssl.h>
27 #include <openssl/bio.h>
28 #include <openssl/err.h>
29 #include <openssl/comp.h>
30 #include <openssl/sha.h>
31 #include <openssl/x509v3.h>
32 #include <openssl/evp.h>
33 #include <openssl/rand.h>
34 #include <boost/exception/info.hpp>
39 void InitializeOpenSSL();
40 std::shared_ptr<SSL_CTX> MakeSSLContext(const String& pubkey = String(), const String& privkey = String(), const String& cakey = String());
41 void AddCRLToSSLContext(const std::shared_ptr<SSL_CTX>& context, const String& crlPath);
42 void SetCipherListToSSLContext(const std::shared_ptr<SSL_CTX>& context, const String& cipherList);
43 void SetTlsProtocolminToSSLContext(const std::shared_ptr<SSL_CTX>& context, const String& tlsProtocolmin);
44 String GetCertificateCN(const std::shared_ptr<X509>& certificate);
45 std::shared_ptr<X509> GetX509Certificate(const String& pemfile);
46 int MakeX509CSR(const String& cn, const String& keyfile, const String& csrfile = String(), const String& certfile = String(), bool ca = false);
47 std::shared_ptr<X509> CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME *issuer, EVP_PKEY *cakey, bool ca);
48 String GetIcingaCADir();
49 String CertificateToString(const std::shared_ptr<X509>& cert);
50 std::shared_ptr<X509> StringToCertificate(const String& cert);
51 std::shared_ptr<X509> CreateCertIcingaCA(EVP_PKEY *pubkey, X509_NAME *subject);
52 std::shared_ptr<X509> CreateCertIcingaCA(const std::shared_ptr<X509>& cert);
53 String PBKDF2_SHA1(const String& password, const String& salt, int iterations);
54 String PBKDF2_SHA256(const String& password, const String& salt, int iterations);
55 String SHA1(const String& s, bool binary = false);
56 String SHA256(const String& s);
57 String RandomString(int length);
58 bool VerifyCertificate(const std::shared_ptr<X509>& caCertificate, const std::shared_ptr<X509>& certificate);
60 class openssl_error : virtual public std::exception, virtual public boost::exception { };
62 struct errinfo_openssl_error_;
63 typedef boost::error_info<struct errinfo_openssl_error_, unsigned long> errinfo_openssl_error;
65 std::string to_string(const errinfo_openssl_error& e);
69 #endif /* TLSUTILITY_H */