1 /******************************************************************************
3 * Copyright (C) 2012-2017 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 ******************************************************************************/
20 #include "base/tlsutility.hpp"
21 #include "base/convert.hpp"
22 #include "base/logger.hpp"
23 #include "base/context.hpp"
24 #include "base/utility.hpp"
25 #include "base/application.hpp"
26 #include "base/exception.hpp"
32 static bool l_SSLInitialized = false;
33 static boost::mutex *l_Mutexes;
36 static void OpenSSLLockingCallback(int mode, int type, const char *, int)
38 if (mode & CRYPTO_LOCK)
39 l_Mutexes[type].lock();
41 l_Mutexes[type].unlock();
44 static unsigned long OpenSSLIDCallback(void)
47 return (unsigned long)GetCurrentThreadId();
49 return (unsigned long)pthread_self();
52 #endif /* CRYPTO_LOCK */
55 * Initializes the OpenSSL library.
57 void InitializeOpenSSL(void)
63 SSL_load_error_strings();
65 SSL_COMP_get_compression_methods();
68 l_Mutexes = new boost::mutex[CRYPTO_num_locks()];
69 CRYPTO_set_locking_callback(&OpenSSLLockingCallback);
70 CRYPTO_set_id_callback(&OpenSSLIDCallback);
71 #endif /* CRYPTO_LOCK */
73 l_SSLInitialized = true;
77 * Initializes an SSL context using the specified certificates.
79 * @param pubkey The public key.
80 * @param privkey The matching private key.
81 * @param cakey CA certificate chain file.
82 * @returns An SSL context.
84 boost::shared_ptr<SSL_CTX> MakeSSLContext(const String& pubkey, const String& privkey, const String& cakey)
90 boost::shared_ptr<SSL_CTX> sslContext = boost::shared_ptr<SSL_CTX>(SSL_CTX_new(SSLv23_method()), SSL_CTX_free);
92 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_CIPHER_SERVER_PREFERENCE;
94 #ifdef SSL_OP_NO_COMPRESSION
95 flags |= SSL_OP_NO_COMPRESSION;
96 #endif /* SSL_OP_NO_COMPRESSION */
98 SSL_CTX_set_options(sslContext.get(), flags);
100 SSL_CTX_set_mode(sslContext.get(), SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
101 SSL_CTX_set_session_id_context(sslContext.get(), (const unsigned char *)"Icinga 2", 8);
103 if (!pubkey.IsEmpty()) {
104 if (!SSL_CTX_use_certificate_chain_file(sslContext.get(), pubkey.CStr())) {
105 Log(LogCritical, "SSL")
106 << "Error with public key file '" << pubkey << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
107 BOOST_THROW_EXCEPTION(openssl_error()
108 << boost::errinfo_api_function("SSL_CTX_use_certificate_chain_file")
109 << errinfo_openssl_error(ERR_peek_error())
110 << boost::errinfo_file_name(pubkey));
114 if (!privkey.IsEmpty()) {
115 if (!SSL_CTX_use_PrivateKey_file(sslContext.get(), privkey.CStr(), SSL_FILETYPE_PEM)) {
116 Log(LogCritical, "SSL")
117 << "Error with private key file '" << privkey << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
118 BOOST_THROW_EXCEPTION(openssl_error()
119 << boost::errinfo_api_function("SSL_CTX_use_PrivateKey_file")
120 << errinfo_openssl_error(ERR_peek_error())
121 << boost::errinfo_file_name(privkey));
124 if (!SSL_CTX_check_private_key(sslContext.get())) {
125 Log(LogCritical, "SSL")
126 << "Error checking private key '" << privkey << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
127 BOOST_THROW_EXCEPTION(openssl_error()
128 << boost::errinfo_api_function("SSL_CTX_check_private_key")
129 << errinfo_openssl_error(ERR_peek_error()));
133 if (!cakey.IsEmpty()) {
134 if (!SSL_CTX_load_verify_locations(sslContext.get(), cakey.CStr(), NULL)) {
135 Log(LogCritical, "SSL")
136 << "Error loading and verifying locations in ca key file '" << cakey << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
137 BOOST_THROW_EXCEPTION(openssl_error()
138 << boost::errinfo_api_function("SSL_CTX_load_verify_locations")
139 << errinfo_openssl_error(ERR_peek_error())
140 << boost::errinfo_file_name(cakey));
143 STACK_OF(X509_NAME) *cert_names;
145 cert_names = SSL_load_client_CA_file(cakey.CStr());
146 if (cert_names == NULL) {
147 Log(LogCritical, "SSL")
148 << "Error loading client ca key file '" << cakey << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
149 BOOST_THROW_EXCEPTION(openssl_error()
150 << boost::errinfo_api_function("SSL_load_client_CA_file")
151 << errinfo_openssl_error(ERR_peek_error())
152 << boost::errinfo_file_name(cakey));
155 SSL_CTX_set_client_CA_list(sslContext.get(), cert_names);
162 * Set the cipher list to the specified SSL context.
163 * @param context The ssl context.
164 * @param cipherList The ciper list.
166 void SetCipherListToSSLContext(const boost::shared_ptr<SSL_CTX>& context, const String& cipherList)
170 if (SSL_CTX_set_cipher_list(context.get(), cipherList.CStr()) == 0) {
171 Log(LogCritical, "SSL")
174 << "' does not specify any usable ciphers: "
175 << ERR_peek_error() << ", \""
176 << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
178 BOOST_THROW_EXCEPTION(openssl_error()
179 << boost::errinfo_api_function("SSL_CTX_set_cipher_list")
180 << errinfo_openssl_error(ERR_peek_error()));
185 * Set the minimum TLS protocol version to the specified SSL context.
187 * @param context The ssl context.
188 * @param tlsProtocolmin The minimum TLS protocol version.
190 void SetTlsProtocolminToSSLContext(const boost::shared_ptr<SSL_CTX>& context, const String& tlsProtocolmin)
192 long flags = SSL_CTX_get_options(context.get());
194 flags |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
196 #ifdef SSL_TXT_TLSV1_1
197 if (tlsProtocolmin == SSL_TXT_TLSV1_1)
198 flags |= SSL_OP_NO_TLSv1;
200 #endif /* SSL_TXT_TLSV1_1 */
201 #ifdef SSL_TXT_TLSV1_2
202 if (tlsProtocolmin == SSL_TXT_TLSV1_2)
203 flags |= SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
205 #endif /* SSL_TXT_TLSV1_2 */
206 if (tlsProtocolmin != SSL_TXT_TLSV1)
207 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid TLS protocol version specified."));
209 SSL_CTX_set_options(context.get(), flags);
213 * Loads a CRL and appends its certificates to the specified SSL context.
215 * @param context The SSL context.
216 * @param crlPath The path to the CRL file.
218 void AddCRLToSSLContext(const boost::shared_ptr<SSL_CTX>& context, const String& crlPath)
221 X509_STORE *x509_store = SSL_CTX_get_cert_store(context.get());
224 lookup = X509_STORE_add_lookup(x509_store, X509_LOOKUP_file());
227 Log(LogCritical, "SSL")
228 << "Error adding X509 store lookup: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
229 BOOST_THROW_EXCEPTION(openssl_error()
230 << boost::errinfo_api_function("X509_STORE_add_lookup")
231 << errinfo_openssl_error(ERR_peek_error()));
234 if (X509_LOOKUP_load_file(lookup, crlPath.CStr(), X509_FILETYPE_PEM) != 1) {
235 Log(LogCritical, "SSL")
236 << "Error loading crl file '" << crlPath << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
237 BOOST_THROW_EXCEPTION(openssl_error()
238 << boost::errinfo_api_function("X509_LOOKUP_load_file")
239 << errinfo_openssl_error(ERR_peek_error())
240 << boost::errinfo_file_name(crlPath));
243 X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
244 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
245 X509_STORE_set1_param(x509_store, param);
246 X509_VERIFY_PARAM_free(param);
249 static String GetX509NameCN(X509_NAME *name)
254 int rc = X509_NAME_get_text_by_NID(name, NID_commonName, buffer, sizeof(buffer));
257 Log(LogCritical, "SSL")
258 << "Error with x509 NAME getting text by NID: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
259 BOOST_THROW_EXCEPTION(openssl_error()
260 << boost::errinfo_api_function("X509_NAME_get_text_by_NID")
261 << errinfo_openssl_error(ERR_peek_error()));
268 * Retrieves the common name for an X509 certificate.
270 * @param certificate The X509 certificate.
271 * @returns The common name.
273 String GetCertificateCN(const boost::shared_ptr<X509>& certificate)
275 return GetX509NameCN(X509_get_subject_name(certificate.get()));
279 * Retrieves an X509 certificate from the specified file.
281 * @param pemfile The filename.
282 * @returns An X509 certificate.
284 boost::shared_ptr<X509> GetX509Certificate(const String& pemfile)
288 BIO *fpcert = BIO_new(BIO_s_file());
290 if (fpcert == NULL) {
291 Log(LogCritical, "SSL")
292 << "Error creating new BIO: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
293 BOOST_THROW_EXCEPTION(openssl_error()
294 << boost::errinfo_api_function("BIO_new")
295 << errinfo_openssl_error(ERR_peek_error()));
298 if (BIO_read_filename(fpcert, pemfile.CStr()) < 0) {
299 Log(LogCritical, "SSL")
300 << "Error reading pem file '" << pemfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
301 BOOST_THROW_EXCEPTION(openssl_error()
302 << boost::errinfo_api_function("BIO_read_filename")
303 << errinfo_openssl_error(ERR_peek_error())
304 << boost::errinfo_file_name(pemfile));
307 cert = PEM_read_bio_X509_AUX(fpcert, NULL, NULL, NULL);
309 Log(LogCritical, "SSL")
310 << "Error on bio X509 AUX reading pem file '" << pemfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
311 BOOST_THROW_EXCEPTION(openssl_error()
312 << boost::errinfo_api_function("PEM_read_bio_X509_AUX")
313 << errinfo_openssl_error(ERR_peek_error())
314 << boost::errinfo_file_name(pemfile));
319 return boost::shared_ptr<X509>(cert, X509_free);
322 int MakeX509CSR(const String& cn, const String& keyfile, const String& csrfile, const String& certfile, bool ca)
328 RSA *rsa = RSA_generate_key(4096, RSA_F4, NULL, NULL);
330 Log(LogInformation, "base")
331 << "Writing private key to '" << keyfile << "'.";
333 BIO *bio = BIO_new_file(const_cast<char *>(keyfile.CStr()), "w");
336 Log(LogCritical, "SSL")
337 << "Error while opening private RSA key file '" << keyfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
338 BOOST_THROW_EXCEPTION(openssl_error()
339 << boost::errinfo_api_function("BIO_new_file")
340 << errinfo_openssl_error(ERR_peek_error())
341 << boost::errinfo_file_name(keyfile));
344 if (!PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL)) {
345 Log(LogCritical, "SSL")
346 << "Error while writing private RSA key to file '" << keyfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
347 BOOST_THROW_EXCEPTION(openssl_error()
348 << boost::errinfo_api_function("PEM_write_bio_RSAPrivateKey")
349 << errinfo_openssl_error(ERR_peek_error())
350 << boost::errinfo_file_name(keyfile));
356 chmod(keyfile.CStr(), 0600);
359 EVP_PKEY *key = EVP_PKEY_new();
360 EVP_PKEY_assign_RSA(key, rsa);
362 if (!certfile.IsEmpty()) {
363 X509_NAME *subject = X509_NAME_new();
364 X509_NAME_add_entry_by_txt(subject, "CN", MBSTRING_ASC, (unsigned char *)cn.CStr(), -1, -1, 0);
366 boost::shared_ptr<X509> cert = CreateCert(key, subject, subject, key, ca);
368 X509_NAME_free(subject);
370 Log(LogInformation, "base")
371 << "Writing X509 certificate to '" << certfile << "'.";
373 bio = BIO_new_file(const_cast<char *>(certfile.CStr()), "w");
376 Log(LogCritical, "SSL")
377 << "Error while opening certificate file '" << certfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
378 BOOST_THROW_EXCEPTION(openssl_error()
379 << boost::errinfo_api_function("BIO_new_file")
380 << errinfo_openssl_error(ERR_peek_error())
381 << boost::errinfo_file_name(certfile));
384 if (!PEM_write_bio_X509(bio, cert.get())) {
385 Log(LogCritical, "SSL")
386 << "Error while writing certificate to file '" << certfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
387 BOOST_THROW_EXCEPTION(openssl_error()
388 << boost::errinfo_api_function("PEM_write_bio_X509")
389 << errinfo_openssl_error(ERR_peek_error())
390 << boost::errinfo_file_name(certfile));
396 if (!csrfile.IsEmpty()) {
397 X509_REQ *req = X509_REQ_new();
402 X509_REQ_set_version(req, 0);
403 X509_REQ_set_pubkey(req, key);
405 X509_NAME *name = X509_REQ_get_subject_name(req);
406 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)cn.CStr(), -1, -1, 0);
408 X509_REQ_sign(req, key, EVP_sha256());
410 Log(LogInformation, "base")
411 << "Writing certificate signing request to '" << csrfile << "'.";
413 bio = BIO_new_file(const_cast<char *>(csrfile.CStr()), "w");
416 Log(LogCritical, "SSL")
417 << "Error while opening CSR file '" << csrfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
418 BOOST_THROW_EXCEPTION(openssl_error()
419 << boost::errinfo_api_function("BIO_new_file")
420 << errinfo_openssl_error(ERR_peek_error())
421 << boost::errinfo_file_name(csrfile));
424 if (!PEM_write_bio_X509_REQ(bio, req)) {
425 Log(LogCritical, "SSL")
426 << "Error while writing CSR to file '" << csrfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
427 BOOST_THROW_EXCEPTION(openssl_error()
428 << boost::errinfo_api_function("PEM_write_bio_X509")
429 << errinfo_openssl_error(ERR_peek_error())
430 << boost::errinfo_file_name(csrfile));
443 boost::shared_ptr<X509> CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME *issuer, EVP_PKEY *cakey, bool ca)
445 X509 *cert = X509_new();
446 X509_set_version(cert, 2);
447 X509_gmtime_adj(X509_get_notBefore(cert), 0);
448 X509_gmtime_adj(X509_get_notAfter(cert), 365 * 24 * 60 * 60 * 15);
449 X509_set_pubkey(cert, pubkey);
451 X509_set_subject_name(cert, subject);
452 X509_set_issuer_name(cert, issuer);
454 String id = Utility::NewUniqueID();
458 unsigned char digest[SHA_DIGEST_LENGTH];
460 if (!SHA1_Init(&context)) {
461 Log(LogCritical, "SSL")
462 << "Error on SHA1 Init: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
463 BOOST_THROW_EXCEPTION(openssl_error()
464 << boost::errinfo_api_function("SHA1_Init")
465 << errinfo_openssl_error(ERR_peek_error()));
468 if (!SHA1_Update(&context, (unsigned char*)id.CStr(), id.GetLength())) {
469 Log(LogCritical, "SSL")
470 << "Error on SHA1 Update: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
471 BOOST_THROW_EXCEPTION(openssl_error()
472 << boost::errinfo_api_function("SHA1_Update")
473 << errinfo_openssl_error(ERR_peek_error()));
476 if (!SHA1_Final(digest, &context)) {
477 Log(LogCritical, "SSL")
478 << "Error on SHA1 Final: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
479 BOOST_THROW_EXCEPTION(openssl_error()
480 << boost::errinfo_api_function("SHA1_Final")
481 << errinfo_openssl_error(ERR_peek_error()));
484 BIGNUM *bn = BN_new();
485 BN_bin2bn(digest, sizeof(digest), bn);
486 BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(cert));
490 X509V3_set_ctx_nodb(&ctx);
491 X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
496 attr = "critical,CA:TRUE";
498 attr = "critical,CA:FALSE";
500 X509_EXTENSION *basicConstraintsExt = X509V3_EXT_conf_nid(NULL, &ctx, NID_basic_constraints, const_cast<char *>(attr));
502 if (basicConstraintsExt) {
503 X509_add_ext(cert, basicConstraintsExt, -1);
504 X509_EXTENSION_free(basicConstraintsExt);
507 String cn = GetX509NameCN(subject);
509 if (!cn.Contains(" ") && cn.Contains(".")) {
510 String san = "DNS:" + cn;
511 X509_EXTENSION *subjectAltNameExt = X509V3_EXT_conf_nid(NULL, &ctx, NID_subject_alt_name, const_cast<char *>(san.CStr()));
512 if (subjectAltNameExt) {
513 X509_add_ext(cert, subjectAltNameExt, -1);
514 X509_EXTENSION_free(subjectAltNameExt);
518 X509_sign(cert, cakey, EVP_sha256());
520 return boost::shared_ptr<X509>(cert, X509_free);
523 String GetIcingaCADir(void)
525 return Application::GetLocalStateDir() + "/lib/icinga2/ca";
528 boost::shared_ptr<X509> CreateCertIcingaCA(EVP_PKEY *pubkey, X509_NAME *subject)
532 String cadir = GetIcingaCADir();
534 String cakeyfile = cadir + "/ca.key";
538 BIO *cakeybio = BIO_new_file(const_cast<char *>(cakeyfile.CStr()), "r");
541 Log(LogCritical, "SSL")
542 << "Could not open CA key file '" << cakeyfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
543 return boost::shared_ptr<X509>();
546 rsa = PEM_read_bio_RSAPrivateKey(cakeybio, NULL, NULL, NULL);
549 Log(LogCritical, "SSL")
550 << "Could not read RSA key from CA key file '" << cakeyfile << "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
551 return boost::shared_ptr<X509>();
556 String cacertfile = cadir + "/ca.crt";
558 boost::shared_ptr<X509> cacert = GetX509Certificate(cacertfile);
560 EVP_PKEY *privkey = EVP_PKEY_new();
561 EVP_PKEY_assign_RSA(privkey, rsa);
563 return CreateCert(pubkey, subject, X509_get_subject_name(cacert.get()), privkey, false);
566 String CertificateToString(const boost::shared_ptr<X509>& cert)
568 BIO *mem = BIO_new(BIO_s_mem());
569 PEM_write_bio_X509(mem, cert.get());
572 long len = BIO_get_mem_data(mem, &data);
574 String result = String(data, data + len);
581 String PBKDF2_SHA1(const String& password, const String& salt, int iterations)
583 unsigned char digest[SHA_DIGEST_LENGTH];
584 PKCS5_PBKDF2_HMAC_SHA1(password.CStr(), password.GetLength(), reinterpret_cast<const unsigned char *>(salt.CStr()), salt.GetLength(),
585 iterations, sizeof(digest), digest);
587 char output[SHA_DIGEST_LENGTH*2+1];
588 for (int i = 0; i < SHA_DIGEST_LENGTH; i++)
589 sprintf(output + 2 * i, "%02x", digest[i]);
594 String SHA1(const String& s, bool binary)
598 unsigned char digest[SHA_DIGEST_LENGTH];
600 if (!SHA1_Init(&context)) {
601 Log(LogCritical, "SSL")
602 << "Error on SHA Init: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
603 BOOST_THROW_EXCEPTION(openssl_error()
604 << boost::errinfo_api_function("SHA1_Init")
605 << errinfo_openssl_error(ERR_peek_error()));
608 if (!SHA1_Update(&context, (unsigned char*)s.CStr(), s.GetLength())) {
609 Log(LogCritical, "SSL")
610 << "Error on SHA Update: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
611 BOOST_THROW_EXCEPTION(openssl_error()
612 << boost::errinfo_api_function("SHA1_Update")
613 << errinfo_openssl_error(ERR_peek_error()));
616 if (!SHA1_Final(digest, &context)) {
617 Log(LogCritical, "SSL")
618 << "Error on SHA Final: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
619 BOOST_THROW_EXCEPTION(openssl_error()
620 << boost::errinfo_api_function("SHA1_Final")
621 << errinfo_openssl_error(ERR_peek_error()));
625 return String(reinterpret_cast<const char*>(digest), reinterpret_cast<const char *>(digest + SHA_DIGEST_LENGTH));
627 char output[SHA_DIGEST_LENGTH*2+1];
628 for (int i = 0; i < 20; i++)
629 sprintf(output + 2 * i, "%02x", digest[i]);
634 String SHA256(const String& s)
638 unsigned char digest[SHA256_DIGEST_LENGTH];
640 if (!SHA256_Init(&context)) {
641 Log(LogCritical, "SSL")
642 << "Error on SHA256 Init: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
643 BOOST_THROW_EXCEPTION(openssl_error()
644 << boost::errinfo_api_function("SHA256_Init")
645 << errinfo_openssl_error(ERR_peek_error()));
648 if (!SHA256_Update(&context, (unsigned char*)s.CStr(), s.GetLength())) {
649 Log(LogCritical, "SSL")
650 << "Error on SHA256 Update: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
651 BOOST_THROW_EXCEPTION(openssl_error()
652 << boost::errinfo_api_function("SHA256_Update")
653 << errinfo_openssl_error(ERR_peek_error()));
656 if (!SHA256_Final(digest, &context)) {
657 Log(LogCritical, "SSL")
658 << "Error on SHA256 Final: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
659 BOOST_THROW_EXCEPTION(openssl_error()
660 << boost::errinfo_api_function("SHA256_Final")
661 << errinfo_openssl_error(ERR_peek_error()));
664 char output[SHA256_DIGEST_LENGTH*2+1];
665 for (int i = 0; i < 32; i++)
666 sprintf(output + 2 * i, "%02x", digest[i]);
671 String RandomString(int length)
673 unsigned char *bytes = new unsigned char[length];
675 if (!RAND_bytes(bytes, length)) {
680 Log(LogCritical, "SSL")
681 << "Error for RAND_bytes: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
682 BOOST_THROW_EXCEPTION(openssl_error()
683 << boost::errinfo_api_function("RAND_bytes")
684 << errinfo_openssl_error(ERR_peek_error()));
687 char *output = new char[length * 2 + 1];
688 for (int i = 0; i < length; i++)
689 sprintf(output + 2 * i, "%02x", bytes[i]);
691 String result = output;