/**
 * Copyright (c) 2021-2026 Governikus GmbH & Co. KG, Germany
 */

#include <QCoreApplication>
#include <QSslCipher>
#include <QSslSocket>

#include <openssl/crypto.h>

#include <algorithm>

#ifdef OPENSSL_NO_CMS
	#error Cryptographic Message Syntax (CMS) is required. Do you use old LibreSSL?
#endif

#ifdef OPENSSL_NO_EC
	#error Elliptic Curve is required.
#endif

#ifdef OPENSSL_NO_TLS1_2
	#error TLS 1.2 is required.
#endif

#ifdef OPENSSL_NO_PSK
	#error RSA-PSK is required.
#endif

#ifdef OPENSSL_NO_EC_EXPLICIT_CURVES
	#error Enable "ec_explicit_curves" in OpenSSL or use -DUSE_LEGACY_OPENSSL_API=ON if possible.
#endif

int main(int argc, char** argv)
{
	QCoreApplication app(argc, argv);
	Q_UNUSED(app)

	// It is required that Qt and AusweisApp uses the library and the same version.
	// Also this binary will crash if your libraries aren't binary compatible. For example
	// this occurs if you link Qt against OpenSSL and the AusweisApp against LibreSSL.
	if (QSslSocket::sslLibraryVersionString() != QLatin1String(OpenSSL_version(OPENSSL_VERSION)))
	{
		return 1;
	}

	// The AusweisApp requires at least one of an RSA-PSK cipher. LibreSSL and OpenSSL <= 1.0.2 does not support that!
	const QStringList ciphers({"RSA-PSK-AES256-GCM-SHA384", "RSA-PSK-AES128-GCM-SHA256", "RSA-PSK-AES256-CBC-SHA"});
	return std::any_of(ciphers.constBegin(), ciphers.constEnd(), [](const QString& pCipherName)
			{
				return !QSslCipher(pCipherName).isNull();
			}) ? 0 : 2;
}
