QString ItemEncryptedScriptable::generateTestKeys() { const KeyPairPaths keys; for ( const auto &keyFileName : {keys.sec, keys.pub} ) { if ( QFile::exists(keyFileName) && !QFile::remove(keyFileName) ) return QString("Failed to remove \"%1\"").arg(keys.sec); } QProcess process; startGenerateKeysProcess(&process, true); if ( !verifyProcess(&process) ) { return QString("ItemEncrypt ERROR: %1; stderr: %2") .arg( process.errorString(), QString::fromUtf8(process.readAllStandardError()) ); } const auto error = exportImportGpgKeys(); if ( !error.isEmpty() ) return error; for ( const auto &keyFileName : {keys.sec, keys.pub} ) { if ( !QFile::exists(keyFileName) ) return QString("Failed to create \"%1\"").arg(keys.sec); } return QString(); }
ItemSaverPtr ItemEncryptedLoader::loadItems(const QString &, QAbstractItemModel *model, QIODevice *file, int maxItems) { // This is needed to skip header. if ( !canLoadItems(file) ) return nullptr; if (status() == GpgNotInstalled) { emit error( ItemEncryptedLoader::tr("GnuPG must be installed to view encrypted tabs.") ); return nullptr; } importGpgKey(); QProcess p; startGpgProcess( &p, QStringList("--decrypt"), QIODevice::ReadWrite ); char encryptedBytes[4096]; QDataStream stream(file); while ( !stream.atEnd() ) { const int bytesRead = stream.readRawData(encryptedBytes, 4096); if (bytesRead == -1) { emitDecryptFailed(); COPYQ_LOG("ItemEncrypted ERROR: Failed to read encrypted data"); return nullptr; } p.write(encryptedBytes, bytesRead); } p.closeWriteChannel(); // Wait for password entry dialog. p.waitForFinished(-1); if ( !verifyProcess(&p) ) { emitDecryptFailed(); return nullptr; } const QByteArray bytes = p.readAllStandardOutput(); if ( bytes.isEmpty() ) { emitDecryptFailed(); COPYQ_LOG("ItemEncrypt ERROR: Failed to read encrypted data."); verifyProcess(&p); return nullptr; } QDataStream stream2(bytes); quint64 length; stream2 >> length; if ( length <= 0 || stream2.status() != QDataStream::Ok ) { emitDecryptFailed(); COPYQ_LOG("ItemEncrypt ERROR: Failed to parse item count!"); return nullptr; } length = qMin(length, static_cast<quint64>(maxItems)) - static_cast<quint64>(model->rowCount()); const auto count = length < maxItemCount ? static_cast<int>(length) : maxItemCount; for ( int i = 0; i < count && stream2.status() == QDataStream::Ok; ++i ) { if ( !model->insertRow(i) ) { emitDecryptFailed(); COPYQ_LOG("ItemEncrypt ERROR: Failed to insert item!"); return nullptr; } QVariantMap dataMap; stream2 >> dataMap; model->setData( model->index(i, 0), dataMap, contentType::data ); } if ( stream2.status() != QDataStream::Ok ) { emitDecryptFailed(); COPYQ_LOG("ItemEncrypt ERROR: Failed to decrypt item!"); return nullptr; } return createSaver(); }
bool TasNativeUtils::processExitStatus(quint64 pid, int &status) { if (verifyProcess(pid)) return false; status = 0; return true; }