diff options
Diffstat (limited to '')
-rw-r--r-- | src/gpg/gpg_context/GpgContextKeyOpera.cpp | 381 |
1 files changed, 0 insertions, 381 deletions
diff --git a/src/gpg/gpg_context/GpgContextKeyOpera.cpp b/src/gpg/gpg_context/GpgContextKeyOpera.cpp deleted file mode 100644 index 63b266ca..00000000 --- a/src/gpg/gpg_context/GpgContextKeyOpera.cpp +++ /dev/null @@ -1,381 +0,0 @@ -/** - * This file is part of GPGFrontend. - * - * GPGFrontend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Foobar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Foobar. If not, see <https://www.gnu.org/licenses/>. - * - * The initial version of the source code is inherited from gpg4usb-team. - * Their source code version also complies with GNU General Public License. - * - * The source code version of this software was modified and released - * by Saturneric<[email protected]> starting on May 12, 2021. - * - */ - -#include "gpg/GpgContext.h" - -/** - * Import key pair - * @param inBuffer input byte array - * @return Import information - */ -GpgImportInformation GpgFrontend::GpgContext::importKey(QByteArray inBuffer) { - auto *importInformation = new GpgImportInformation(); - err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1); - check_gpg_error(err); - err = gpgme_op_import(*this, in); - gpgme_import_result_t result; - - result = gpgme_op_import_result(*this); - - if (result->unchanged) importInformation->unchanged = result->unchanged; - if (result->considered) importInformation->considered = result->considered; - if (result->no_user_id) importInformation->no_user_id = result->no_user_id; - if (result->imported) importInformation->imported = result->imported; - if (result->imported_rsa) importInformation->imported_rsa = result->imported_rsa; - if (result->unchanged) importInformation->unchanged = result->unchanged; - if (result->new_user_ids) importInformation->new_user_ids = result->new_user_ids; - if (result->new_sub_keys) importInformation->new_sub_keys = result->new_sub_keys; - if (result->new_signatures) importInformation->new_signatures = result->new_signatures; - if (result->new_revocations) importInformation->new_revocations = result->new_revocations; - if (result->secret_read) importInformation->secret_read = result->secret_read; - if (result->secret_imported) importInformation->secret_imported = result->secret_imported; - if (result->secret_unchanged) importInformation->secret_unchanged = result->secret_unchanged; - if (result->not_imported) importInformation->not_imported = result->not_imported; - - gpgme_import_status_t status = result->imports; - while (status != nullptr) { - GpgImportedKey key; - key.importStatus = static_cast<int>(status->status); - key.fpr = status->fpr; - importInformation->importedKeys.emplace_back(key); - status = status->next; - } - check_gpg_error(err); - emit signalKeyDBChanged(); - gpgme_data_release(in); - return *importInformation; -} - -/** - * Generate a new key pair - * @param params key generation args - * @return error information - */ -gpgme_error_t GpgFrontend::GpgContext::generateKey(GenKeyInfo *params) { - - auto userid_utf8 = params->getUserid().toUtf8(); - const char *userid = userid_utf8.constData(); - auto algo_utf8 = (params->getAlgo() + params->getKeySizeStr()).toUtf8(); - const char *algo = algo_utf8.constData(); - unsigned long expires = QDateTime::currentDateTime().secsTo(params->getExpired()); - unsigned int flags = 0; - - if (!params->isSubKey()) flags |= GPGME_CREATE_CERT; - if (params->isAllowEncryption()) flags |= GPGME_CREATE_ENCR; - if (params->isAllowSigning()) flags |= GPGME_CREATE_SIGN; - if (params->isAllowAuthentication()) flags |= GPGME_CREATE_AUTH; - if (params->isNonExpired()) flags |= GPGME_CREATE_NOEXPIRE; - if (params->isNoPassPhrase()) flags |= GPGME_CREATE_NOPASSWD; - - err = gpgme_op_createkey(*this, userid, algo, 0, expires, nullptr, flags); - - if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { - check_gpg_error(err); - return err; - } else { - emit signalKeyDBChanged(); - return err; - } -} - -/** - * Export Key - * @param uidList key ids - * @param outBuffer output byte array - * @return if success - */ -bool GpgFrontend::GpgContext::exportKeys(QStringList *uidList, QByteArray *outBuffer) { - gpgme_data_t dataOut = nullptr; - outBuffer->resize(0); - - if (uidList->count() == 0) { - QMessageBox::critical(nullptr, "Export Keys Error", "No Keys Selected"); - return false; - } - - // Alleviate another crash problem caused by an unknown array out-of-bounds access - gpgme_ctx_t ctx = create_ctx(); - - for (int i = 0; i < uidList->count(); i++) { - err = gpgme_data_new(&dataOut); - check_gpg_error(err); - - err = gpgme_op_export(ctx, uidList->at(i).toUtf8().constData(), 0, dataOut); - check_gpg_error(err); - - qDebug() << "exportKeys read_bytes" << gpgme_data_seek(dataOut, 0, SEEK_END); - - err = readToBuffer(dataOut, outBuffer); - check_gpg_error(err); - gpgme_data_release(dataOut); - } - - gpgme_release(ctx); - - return true; -} - -/** - * Get and store all key pairs info - */ -void GpgFrontend::GpgContext::fetch_keys() { - - gpgme_error_t gpgmeError; - - gpgme_key_t key; - - qDebug() << "Clear List and Map"; - - mKeyList.clear(); - mKeyMap.clear(); - - auto &keys = mKeyList; - auto &keys_map = mKeyMap; - - qDebug() << "Set Key Listing Mode"; - - gpgmeError = gpgme_set_keylist_mode(*this, - GPGME_KEYLIST_MODE_LOCAL - | GPGME_KEYLIST_MODE_WITH_SECRET - | GPGME_KEYLIST_MODE_SIGS - | GPGME_KEYLIST_MODE_SIG_NOTATIONS - | GPGME_KEYLIST_MODE_WITH_TOFU); - if (gpg_err_code(gpgmeError) != GPG_ERR_NO_ERROR) { - check_gpg_error(gpgmeError); - return; - } - - qDebug() << "Operate KeyList Start"; - - gpgmeError = gpgme_op_keylist_start(*this, nullptr, 0); - if (gpg_err_code(gpgmeError) != GPG_ERR_NO_ERROR) { - check_gpg_error(gpgmeError); - return; - } - - qDebug() << "Start Loop"; - - while ((gpgmeError = gpgme_op_keylist_next(*this, &key)) == GPG_ERR_NO_ERROR) { - if (!key->subkeys) - continue; - - qDebug() << "Append Key" << key->subkeys->keyid; - - keys.emplace_back(gpgme_key_t(key)); - keys_map.insert(keys.back().id(), &keys.back()); - gpgme_key_unref(key); - } - - - if (gpg_err_code(gpgmeError) != GPG_ERR_EOF) { - check_gpg_error(gpgmeError); - return; - } - - gpgmeError = gpgme_op_keylist_end(*this); - if (gpg_err_code(gpgmeError) != GPG_ERR_NO_ERROR) { - check_gpg_error(gpgmeError); - return; - } - - qDebug() << "Operate KeyList End"; - - mKeyList = keys; -} - -/** - * Delete keys - * @param uidList key ids - */ -void GpgFrontend::GpgContext::deleteKeys(QStringList *uidList) { - - gpgme_error_t error; - gpgme_key_t key; - - for (const auto &tmp : *uidList) { - - error = gpgme_op_keylist_start(*this, tmp.toUtf8().constData(), 0); - if (error != GPG_ERR_NO_ERROR) { - check_gpg_error(error); - continue; - } - - error = gpgme_op_keylist_next(*this, &key); - if (error != GPG_ERR_NO_ERROR) { - check_gpg_error(error); - continue; - } - - error = gpgme_op_keylist_end(*this); - if (error != GPG_ERR_NO_ERROR) { - check_gpg_error(error); - continue; - } - - error = gpgme_op_delete(*this, key, 1); - if (error != GPG_ERR_NO_ERROR) { - check_gpg_error(error); - continue; - } - - } - emit signalKeyDBChanged(); -} - -/** - * Export keys - * @param keys keys used - * @param outBuffer output byte array - * @return if success - */ -bool GpgFrontend::GpgContext::exportKeys(const QVector <GpgKey> &keys, QByteArray &outBuffer) { - gpgme_data_t data_out = nullptr; - outBuffer.resize(0); - - if (keys.empty()) { - QMessageBox::critical(nullptr, "Export Keys Error", "No Keys Selected"); - return false; - } - - for (const auto &key : keys) { - err = gpgme_data_new(&data_out); - check_gpg_error(err); - - err = gpgme_op_export(*this, key.id().toUtf8().constData(), 0, data_out); - check_gpg_error(err); - - gpgme_data_seek(data_out, 0, SEEK_END); - - err = readToBuffer(data_out, &outBuffer); - check_gpg_error(err); - gpgme_data_release(data_out); - } - - return true; -} - -/** - * Set the expire date and time of a key pair(actually the master key) or subkey - * @param key target key pair - * @param subkey null if master key - * @param expires date and time - * @return if successful - */ -bool GpgFrontend::GpgContext::setExpire(const GpgKey &key, std::unique_ptr <GpgSubKey> &subkey, - std::unique_ptr <QDateTime> &expires) { - unsigned long expires_time = 0; - if (expires != nullptr) { - qDebug() << "Expire Datetime" << expires->toString(); - expires_time = QDateTime::currentDateTime().secsTo(*expires); - } - - const char *sub_fprs = nullptr; - - if (subkey != nullptr) sub_fprs = subkey->fpr().toUtf8().constData(); - - auto gpgmeError = gpgme_op_setexpire(*this, gpgme_key_t(key), - expires_time, sub_fprs, 0); - if (gpgmeError == GPG_ERR_NO_ERROR) { - emit signalKeyUpdated(key.id()); - return true; - } else { - check_gpg_error(gpgmeError); - return false; - } -} - -/** - * Export the secret key of a key pair(including subkeys) - * @param key target key pair - * @param outBuffer output byte array - * @return if successful - */ -bool GpgFrontend::GpgContext::exportSecretKey(const GpgKey &key, QByteArray *outBuffer) const { - qDebug() << "Export Secret Key" << key.id(); - gpgme_key_t target_key[2] = { - gpgme_key_t(key), - nullptr - }; - - gpgme_data_t dataOut; - gpgme_data_new(&dataOut); - - // export private key to outBuffer - gpgme_error_t error = gpgme_op_export_keys(*this, target_key, GPGME_EXPORT_MODE_SECRET, dataOut); - - if (gpgme_err_code(error) != GPG_ERR_NO_ERROR) { - check_gpg_error(error); - gpgme_data_release(dataOut); - return false; - } - - readToBuffer(dataOut, outBuffer); - gpgme_data_release(dataOut); - return true; -} - -/** - * Generate revoke cert of a key pair - * @param key target key pair - * @param outputFileName out file name(path) - * @return the process doing this job - */ -void GpgFrontend::GpgContext::generateRevokeCert(const GpgKey &key, const QString &outputFileName) { - executeGpgCommand({ - "--command-fd", - "0", - "--status-fd", - "1", - //"--no-tty", - "-o", - outputFileName, - "--gen-revoke", - key.fpr() - }, - [](QProcess *proc) -> void { - qDebug() << "Function Called" << proc; - // Code From Gpg4Win - while (proc->canReadLine()) { - const QString line = QString::fromUtf8(proc->readLine()).trimmed(); - if (line == QLatin1String("[GNUPG:] GET_BOOL gen_revoke.okay")) { - proc->write("y\n"); - } else if (line == QLatin1String( - "[GNUPG:] GET_LINE ask_revocation_reason.code")) { - proc->write("0\n"); - } else if (line == QLatin1String( - "[GNUPG:] GET_LINE ask_revocation_reason.text")) { - proc->write("\n"); - } else if (line == QLatin1String( - "[GNUPG:] GET_BOOL openfile.overwrite.okay")) { - // We asked before - proc->write("y\n"); - } else if (line == QLatin1String( - "[GNUPG:] GET_BOOL ask_revocation_reason.okay")) { - proc->write("y\n"); - } - } - // Code From Gpg4Win - } - ); -} |