コード例 #1
0
ファイル: license.cpp プロジェクト: SeisComP3/seiscomp3
bool isValid() {
	static bool hasValidated = false;
	static bool validates = false;

	if ( hasValidated ) return validates;

	Environment *env = Environment::Instance();
	if ( env == NULL ) {
		cerr << "FATAL ERROR: No environment available" << endl;
		return false;
	}

	hasValidated = true;

	string licenseDir = env->configDir() + "/key";
	string licenseFile = licenseDir + "/License";
	string licenseKeyfile = licenseDir + "/License.key";
	string licenseSignature = licenseDir + "/License.signed";

	boost::filesystem::path path = SC_FS_PATH(env->shareDir())
	    / SC_FS_PATH("licenses") / SC_FS_PATH("seiscomp3.crt");

	if ( !Seiscomp::Util::fileExists(path.string().c_str()) ) {
		path = SC_FS_PATH(env->configDir())
		    / SC_FS_PATH("licenses") / SC_FS_PATH("seiscomp3.crt");
		if ( !Seiscomp::Util::fileExists(path.string()) ) {
			path = SC_FS_PATH(env->configDir())
			    / SC_FS_PATH("key") / SC_FS_PATH("License.crt");
		}
	}

	X509 *x509 = readCertificate(path.string());
	if ( x509 ) {
		ASN1_TIME* notAfter = X509_get_notAfter(x509),
		         * notBefore = X509_get_notBefore(x509);
		time_t ptime = time(NULL);

		int res = X509_cmp_time(notBefore, &ptime);
		if ( res == 0 || res > 0 ) {
			X509_free(x509);
			cerr << "FATAL ERROR: License has expired: " << path.string() << endl;
			return false;
		}

		res = X509_cmp_time(notAfter, &ptime);
		if ( res == 0 || res < 0 ) {
			X509_free(x509);
			cerr << "FATAL ERROR: License has expired: " << path.string() << endl;
			return false;
		}

		OpenSSL_add_all_algorithms();
		OpenSSL_add_all_ciphers();
		OpenSSL_add_all_digests();

		EVP_PKEY* pkey=X509_get_pubkey(x509);
		if ( !pkey ) {
			X509_free(x509);
			EVP_cleanup();
			cerr << "FATAL ERROR: License verification has failed: " << path.string() << endl;
			return false;
		}

		res = X509_verify(x509, pkey);
		if ( res != 1 ) {
			X509_free(x509);
			EVP_PKEY_free(pkey);
			EVP_cleanup();
			cerr << "FATAL ERROR: License verification has failed: " << path.string() << endl;
			return false;
		}

		char *buf;
		if ( readNID(&buf, x509, NID_netscape_comment) ) {
			licenseText = buf;
			delete buf;
		}

		EVP_PKEY_free(pkey);
		X509_free(x509);

		EVP_cleanup();

		return true;
	}

	// Read license file
	MD5_CTX ctx;
	MD5_Init(&ctx);

	unsigned char digest[MD5_DIGEST_LENGTH];
	char data[64];
	size_t len;

	ifstream f;

	try {
		f.open(licenseFile.c_str(), ios_base::in);
	}
	catch ( std::exception &e ) {
		cerr << "FATAL ERROR: Failed to open license file: " << licenseFile << endl;
		validates = false;
		return false;
	}

	if ( !f.good() ) {
		cerr << "FATAL ERROR: Failed to open license file: " << licenseFile << endl;
		validates = false;
		return false;
	}

	licenseText.clear();

	try {
		while ( (len = f.rdbuf()->sgetn(data, sizeof(data))) > 0 ) {
			licenseText.append(data, len);
			MD5_Update(&ctx, data, len);
		}
	}
	catch ( ... ) {
		cerr << "FATAL ERROR: Invalid license file: " << licenseFile << endl;
		f.close();
		validates = false;
		return false;
	}

	f.close();

	MD5_Final(digest, &ctx);

	int strength = 0;
	RSA *publicKey = readKey(licenseKeyfile.c_str(), PUBLIC, 1024, 8192, strength);
	if ( publicKey == NULL ) {
		cerr << "FATAL ERROR: Invalid key file: " << licenseKeyfile << endl;
		validates = false;
		return false;
	}

	BIO *bio_file = NULL, *b64_file;
	b64_file = BIO_new(BIO_f_base64());
	bio_file = BIO_new_file(licenseSignature.c_str(), "r");
	bio_file = BIO_push(b64_file, bio_file);

	int sigLength = strength / 8;
	unsigned char *signature = new unsigned char[sigLength];

	sigLength = BIO_read(bio_file, signature, sigLength);

	BIO_free_all(bio_file);

	if ( sigLength <= 0 ) {
		delete [] signature;
		cerr << "FATAL ERROR: Empty signature" << endl;
		validates = false;
		return false;
	}

	validates = RSA_verify(NID_md5, digest, MD5_DIGEST_LENGTH, signature, sigLength, publicKey);

	delete [] signature;

	/*
	if ( validates ) {
		cerr << "-----BEGIN LICENSE-----" << endl;
		cerr << licenseText << endl;
		cerr << "-----END LICENSE-----" << endl << endl;
	}
	*/

	return validates;
}
コード例 #2
0
ファイル: gui.cpp プロジェクト: aemanov/seiscomp3
		StageSelectionDialog(QWidget *parent) : QDialog(parent) {
			Environment *env = Environment::Instance();

			QVBoxLayout *layout = new QVBoxLayout;
			QHBoxLayout *hlayout;
			QLabel *label;

			label = new QLabel;
			QFont f = label->font();
			f.setBold(true);
			f.setPointSize(f.pointSize()*150/100);
			label->setFont(f);
			label->setText(tr("Select configuration mode"));
			label->setAlignment(Qt::AlignCenter);
			layout->addWidget(label);

			layout->addSpacing(fontMetrics().ascent());

			// Create dialog here
			_systemMode = new QPushButton;
			_systemMode->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
			_systemMode->setIcon(QIcon(":/res/icons/system-settings.png"));
			_systemMode->setIconSize(QSize(72,72));

			label = new QLabel;
			label->setWordWrap(true);
			label->setAlignment(Qt::AlignCenter);
			label->setText(QString(tr("Manage system configuration in <i>%1</i>.")).arg(env->appConfigDir().c_str()));

			hlayout = new QHBoxLayout;
			hlayout->addStretch();
			hlayout->addWidget(_systemMode);
			hlayout->addStretch();

			layout->addLayout(hlayout);
			layout->addWidget(label);

			QFrame *frame = new QFrame;
			frame->setFrameShape(QFrame::HLine);

			layout->addWidget(frame);

			_userMode = new QPushButton;
			_userMode->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
			_userMode->setIcon(QIcon(":/res/icons/user-settings.png"));
			_userMode->setIconSize(QSize(72,72));

			label = new QLabel;
			label->setWordWrap(true);
			label->setAlignment(Qt::AlignCenter);
			label->setText(QString(tr("Manage user configuration in <i>%1</i>.")).arg(env->configDir().c_str()));
			hlayout = new QHBoxLayout;
			hlayout->addStretch();
			hlayout->addWidget(_userMode);
			hlayout->addStretch();

			layout->addLayout(hlayout);
			layout->addWidget(label);

			layout->addStretch();

			setLayout(layout);

			connect(_userMode, SIGNAL(clicked()), this, SLOT(accept()));
			connect(_systemMode, SIGNAL(clicked()), this, SLOT(accept()));
		}