ConnectedUser *ProtocolController::createUserFromPublicKey(QByteArray &publicKey)
{
    Botan::DataSource_Memory key_pub(publicKey.toStdString());
    auto publicRsaKey = Botan::X509::load_key(key_pub);

    return new ConnectedUser(publicRsaKey);
}
QByteArray Encryptor::encryptAsymmetricly(QByteArray &publicKey, QByteArray &data)
{
    Botan::DataSource_Memory key_pub(publicKey.toStdString());
    auto publicRsaKey = Botan::X509::load_key(key_pub);

    const uint DATA_SIZE = data.size();
    Botan::byte msgtoencrypt[DATA_SIZE];

    for (uint i = 0; i < DATA_SIZE; i++)
    {
        msgtoencrypt[i] = data[i];
    }

    Botan::PK_Encryptor_EME encryptor(*publicRsaKey, "EME1(SHA-256)");
    Botan::AutoSeeded_RNG rng;
    std::vector<Botan::byte> ciphertext = encryptor.encrypt(msgtoencrypt, DATA_SIZE, rng);

    QByteArray keyCipherData;
    keyCipherData.resize(ciphertext.size());

    for ( uint i = 0; i < ciphertext.size(); i++ ) {
        keyCipherData[i] = ciphertext.at(i);
    }

    return keyCipherData;
}
Example #3
0
void SshKeyController::generate_keys(QWidget* parent) {
  QString str_file = QFileDialog::getSaveFileName(
      parent, tr("After generating the SSH key pair, you must not change the path to the SSH folder."),
      CSettingsManager::Instance().ssh_keys_storage(),
      tr("Ssh keys (*.pub);; All files (*.*)"));
  if (str_file.isEmpty()) return;
  QFileInfo fi(str_file);

  QString str_private = CSettingsManager::Instance().ssh_keys_storage() +
                        QDir::separator() + fi.baseName();

  QString str_public = str_file + (str_private == str_file ? ".pub" : "");

  QFile key(str_private);
  QFile key_pub(str_public);

  if (fi.baseName().contains(QRegularExpression("[/|\\\\$%~\"*?:<>^]")) ||
      fi.baseName().isEmpty() || fi.baseName()[0] == '.') {
    CNotificationObserver::Error(QString("SSH key name can not begin with . "
                                         "(dot) and can not contain following "
                                         "symbols:\n/|\\$%~\"<>:*?^"),
                                 DlgNotification::N_NO_ACTION);
  } else {
    if (key.exists() && key_pub.exists()) {
      key.remove();
      key_pub.remove();
    }

    system_call_wrapper_error_t scwe = CSystemCallWrapper::generate_ssh_key(
        CHubController::Instance().current_user(), str_private);
    if (scwe != SCWE_SUCCESS) {
      CNotificationObserver::Instance()->Error(
          tr("An error has occurred while trying to generate the SSH key: %1. You can manually "
             "create an SSH key or try again by restarting the Control Center first.")
              .arg(CSystemCallWrapper::scwe_error_to_str(scwe)), DlgNotification::N_NO_ACTION);
      return;
    }
  }
}
KeyPair Encryptor::createAsymmetricKeys()
{
    // Private key generation
    Botan::AutoSeeded_RNG rng;
    Botan::RSA_PrivateKey key(rng, 4096);

    // Generate public key
    std::string pub = Botan::X509::PEM_encode(key);
    std::string priv = Botan::PKCS8::PEM_encode(key);

    Botan::DataSource_Memory key_pub(pub);
    Botan::DataSource_Memory key_priv(priv);

    // Load keys
    auto publicRsaKey = Botan::X509::load_key(key_pub);
    auto privateRsaKey = Botan::PKCS8::load_key(key_priv, rng);

    KeyPair pair;
    pair.publicKey = publicRsaKey;
    pair.privateKey = privateRsaKey;

    return pair;
}