Logo Search packages:      
Sourcecode: valknut version File versions  Download package

void DCOptions::slotGenerateKeyCert (  )  [private, slot]

Generates a new key and certificate for SSL transfers

Definition at line 2441 of file dcoptions.cpp.

Referenced by InitDocument().

{
      QProcess * proc1 = 0;
      QProcess * proc2 = 0;
      QString keyname = LineEdit_TRANSFERKEY->text();
      QString certname = LineEdit_TRANSFERCERT->text();
      QFile key;
      QFile cert;
      QDir keypath;
      QDir certpath;
      int i = 0;
      
      if (keyname == "")
      {
            keyname = QString(g_pConfig->GetConfigPath().Data()) + QString("/ssl/valknut.key");
      }
      
      if (certname == "")
      {
            certname = QString(g_pConfig->GetConfigPath().Data()) + QString("/ssl/valknut.cert");
      }
      
      keyname = keyname.replace("//", "/");
      certname = certname.replace("//", "/");
      
      key.setName(keyname);
      cert.setName(certname);
      
      if ( key.exists() || cert.exists() )
      {
            int ret = QMessageBox::question(
                  this,
                  tr("Overwrite?"),
                  tr("Replace existing key/cert?"),
                  QMessageBox::Yes,
                  QMessageBox::No
            );
            
            if ( ret == QMessageBox::No )
            {
                  return;
            }
      }
      
      keypath.setPath(QFileInfo(keyname).dirPath());
      certpath.setPath(QFileInfo(certname).dirPath());
      
      if (keypath.exists() == FALSE)
      {
            if ( keypath.mkdir(keypath.path()) == FALSE )
            {
                  QMessageBox::critical(
                        this,
                        tr("Could not create folder for key"),
                        tr("Could not create ") + keypath.path(),
                        QMessageBox::Ok,
                        QMessageBox::NoButton,
                        QMessageBox::NoButton
                  );
                  return;
            }
      }
      
      if (certpath.exists() == FALSE)
      {
            if ( certpath.mkdir(certpath.path()) == FALSE )
            {
                  QMessageBox::critical(
                        this,
                        tr("Could not create folder for cert"),
                        tr("Could not create ") + certpath.path(),
                        QMessageBox::Ok,
                        QMessageBox::NoButton,
                        QMessageBox::NoButton
                  );
                  return;
            }
      }
      
      proc1 = new QProcess(this);
      proc1->setWorkingDirectory(keypath);
      proc1->addArgument("openssl");
      proc1->addArgument("genrsa");
      proc1->addArgument("-out");
      proc1->addArgument(keyname);
      proc1->addArgument("2048");
      
      if ( !proc1->start() )
      {
            QMessageBox::critical(
                  this,
                  tr("Could not start openssl 1"),
                  tr("Make sure the openssl program is in your PATH."),
                  QMessageBox::Ok,
                  QMessageBox::NoButton,
                  QMessageBox::NoButton
            );
            delete proc1;
            return;
      }
      
      proc1->closeStdin();
      i = 0;
      while ( (proc1->isRunning() == TRUE) && (i < 5) )
      {
            sleep(1);
            i++;
      }
      
      if ( proc1->isRunning() == TRUE )
      {
            proc1->tryTerminate();
            sleep(1);
            if ( proc1->isRunning() == TRUE )
            {
                  proc1->kill();
            }
      }
      
      if ( (proc1->normalExit() == FALSE) || (proc1->exitStatus() != 0) )
      {
            QMessageBox::critical(
                  this,
                  tr("Error occurred while running openssl 1"),
                  QString(proc1->readStderr()),
                  QMessageBox::Ok,
                  QMessageBox::NoButton,
                  QMessageBox::NoButton
            );
            delete proc1;
            return;
      }
      
      delete proc1;
      
      proc2 = new QProcess(this);
      proc2->setWorkingDirectory(certpath);
      proc2->addArgument("openssl");
      proc2->addArgument("req");
      proc2->addArgument("-x509");
      proc2->addArgument("-new");
      proc2->addArgument("-batch");
      proc2->addArgument("-days");
      proc2->addArgument("3650");
      proc2->addArgument("-key");
      proc2->addArgument(keyname);
      proc2->addArgument("-out");
      proc2->addArgument(certname);
      proc2->addArgument("-subj");
      proc2->addArgument("/CN=Valknut-" + LineEdit_NICK->text());
      
      if ( !proc2->start() )
      {
            QMessageBox::critical(
                  this,
                  tr("Could not start openssl 2"),
                  tr("Make sure the openssl program is in your PATH."),
                  QMessageBox::Ok,
                  QMessageBox::NoButton,
                  QMessageBox::NoButton
            );
            delete proc2;
            return;
      }
      
      proc2->closeStdin();
      i = 0;
      while ( (proc2->isRunning() == TRUE) && (i < 5) )
      {
            sleep(1);
            i++;
      }
      
      if ( proc2->isRunning() == TRUE )
      {
            proc2->tryTerminate();
            sleep(1);
            if ( proc2->isRunning() == TRUE )
            {
                  proc2->kill();
            }
      }
      
      if ( (proc2->normalExit() == FALSE) || (proc2->exitStatus() != 0) )
      {
            QMessageBox::critical(
                  this,
                  tr("Error occurred while running openssl 2"),
                  QString(proc2->readStderr()),
                  QMessageBox::Ok,
                  QMessageBox::NoButton,
                  QMessageBox::NoButton
            );
            delete proc2;
            return;
      }
      
      delete proc2;
      
      LineEdit_TRANSFERKEY->setText(keyname);
      LineEdit_TRANSFERCERT->setText(certname);
      
      // set them even if dialog is cancelled since files have been created
      g_pConfig->SetTransferKey(keyname.ascii());
      g_pConfig->SetTransferCert(certname.ascii());
}


Generated by  Doxygen 1.6.0   Back to index