From 630923d0de4a5cab558531e943370f00a64b316d Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 30 Sep 2012 16:48:52 +0200 Subject: autogenerate keys and autostartup daemon --- distfoldd/localkey.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 distfoldd/localkey.cc (limited to 'distfoldd/localkey.cc') diff --git a/distfoldd/localkey.cc b/distfoldd/localkey.cc new file mode 100644 index 0000000..9855ff4 --- /dev/null +++ b/distfoldd/localkey.cc @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "localkey.h" + +LocalKey::LocalKey() +{ +} + +QString LocalKey::localKeyDir() +{ + return QDir::home().absoluteFilePath(".config/distfold"); +} + +bool LocalKey::setupLocalKey() +{ + QDir local_key_dir(localKeyDir()); + if (local_key_dir.exists("server.crt") && local_key_dir.exists("server.key")) { + return true; + } + + QCA::Initializer qca; + + QCA::KeyGenerator keygen; + keygen.setBlockingEnabled(true); + + qDebug() << "Generating private key..."; + QCA::PrivateKey private_key = keygen.createRSA(2048); + if (!private_key.toPEMFile(local_key_dir.absoluteFilePath("server.key"))) { + qWarning() << "Failed to save private key"; + return false; + } + + qDebug() << "Generating server certificate..."; + QCA::CertificateInfo cert_info; + cert_info.insert(QCA::CommonName, "Distfold Private Generic Cert"); + QCA::CertificateOptions cert_options; + cert_options.setAsCA(1); + cert_options.setInfo(cert_info); + cert_options.setValidityPeriod(QDateTime::currentDateTime(), + QDateTime::currentDateTime().addYears(2)); + QCA::Certificate cert(cert_options, private_key); + if (!cert.toPEMFile(local_key_dir.absoluteFilePath("server.crt"))) { + qWarning() << "Failed to save server certificate"; + return false; + } + + return true; +} -- cgit v1.2.3