QString OtrInternal::encryptMessage(const QString& from, const QString& to, const QString& message, TreeModelItem &item) { QString protocol = item.m_protocol_name; char* encMessage = NULL; gcry_error_t err; err = otrl_message_sending(m_userstate, &m_uiOps, this, from.toStdString().c_str(), protocol.toStdString().c_str(), to.toStdString().c_str(), message.toUtf8().data(), NULL, &encMessage, NULL, NULL); if (err != 0) { QMessageBox mb(QMessageBox::Critical, tr("qutim-otr"), tr("Encrypting message from %1 to %2 failed.").arg(QString(from)).arg(QString(to)) + "\n" + tr("The message was not sent."), QMessageBox::Ok, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint); mb.exec(); return QString(); } if (encMessage != NULL) { QString retMessage(QString::fromUtf8(encMessage)); otrl_message_free(encMessage); return retMessage; } return message; }
QString OtrInternal::encryptMessage(const QString& account, const QString& contact, const QString& message) { char* encMessage = NULL; gcry_error_t err; err = otrl_message_sending(m_userstate, &m_uiOps, this, account.toUtf8().constData(), OTR_PROTOCOL_STRING, contact.toUtf8().constData(), #if (OTRL_VERSION_MAJOR >= 4) OTRL_INSTAG_BEST, #endif message.toUtf8().constData(), NULL, &encMessage, #if (OTRL_VERSION_MAJOR >= 4) OTRL_FRAGMENT_SEND_SKIP, NULL, #endif NULL, NULL); if (err) { QString err_message = QObject::tr("Encrypting message to %1 " "failed.\nThe message was not sent.") .arg(contact); if (!m_callback->displayOtrMessage(account, contact, err_message)) { m_callback->notifyUser(account, contact, err_message, psiotr::OTR_NOTIFY_ERROR); } return QString(); } if (encMessage) { QString retMessage(QString::fromUtf8(encMessage)); otrl_message_free(encMessage); return retMessage; } return message; }
QString OtrInternal::decryptMessage(const QString& from, const QString& to, const QString& cryptedMessage, TreeModelItem &item) { QString protocol = item.m_protocol_name; int ignoreMessage = 0; char *newMessage = NULL; OtrlTLV *tlvs = NULL; OtrlTLV *tlv = NULL; ConnContext *context = 0; NextExpectedSMP nextMsg; ignoreMessage = otrl_message_receiving(m_userstate, &m_uiOps, this, to.toStdString().c_str(), protocol.toStdString().c_str(), from.toStdString().c_str(), cryptedMessage.toUtf8().data(), &newMessage, &tlvs, NULL, NULL); context = otrl_context_find( m_userstate, from.toStdString().c_str(), to.toStdString().c_str(), protocol.toStdString().c_str(), 0, NULL, NULL, NULL); // qDebug() << "[OTR] context fragment: " << QString(context->lastmessage); tlv = otrl_tlv_find(tlvs, OTRL_TLV_DISCONNECTED); if( tlv ){ sendCustomNessage(item,tr("%1 has ended the OTR session. You should do the same.").arg(item.m_item_name)); gone_insecure(context); } while (context) { OtrlSMState *state = context->smstate; if(!state) break; nextMsg = state->nextExpected; tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1Q); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP1Q"; //a-la pidgin if (nextMsg != OTRL_SMP_EXPECT1) abortSMP(context,item); else { char *question = (char *)tlv->data; char *eoq = (char*)memchr(question, '\0', tlv->len); if (eoq) { QString ans = QInputDialog::getText(NULL,tr("Auth"),tr("Please, answer the question to be authorised by %1.<br>Question: <b>%2</b>").arg(from).arg(QString(question))); if(!ans.isEmpty()) respondSMP(context,item,ans,false); else abortSMP(context,item); } } } tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP1"; if (nextMsg != OTRL_SMP_EXPECT1 ){ abortSMP( context, item ); } else { QString s = QInputDialog::getText ( NULL, tr("Authorysing"), tr("Please, enter passphrase to authorise %1").arg(context->username), QLineEdit::Normal); if(!s.isEmpty()) respondSMP(context,item,s,false); else abortSMP(context,item); } } tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP2); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP2"; if (nextMsg != OTRL_SMP_EXPECT2){ abortSMP( context, item ); } else { context->smstate->nextExpected = OTRL_SMP_EXPECT4; } } tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP3); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP3"; if (nextMsg != OTRL_SMP_EXPECT3){ abortSMP( context, item ); } else { if (context->active_fingerprint->trust && context->active_fingerprint->trust[0]) { sendCustomNessage(item, tr("Your buddy has successfully authenticated you. The conversation is now secure!")); gone_secure(context); } else { // sendCustomNessage(item, tr("Authentication failed. The conversation is now insecure!")); sendCustomNessage(item,tr("Your buddy has successfully authenticated you. You may want to authenticate your buddy as well by asking your own question.")); gone_secure(context); } context->smstate->nextExpected = OTRL_SMP_EXPECT1; } } tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP4); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP4"; if (nextMsg != OTRL_SMP_EXPECT4) { abortSMP( context, item ); } else { if (context->active_fingerprint->trust && context->active_fingerprint->trust[0]) { sendCustomNessage(item, tr("Authentication successful. The conversation is now secure!")); gone_secure(context); } else { sendCustomNessage(item, tr("Authentication failed. The conversation is now insecure!")); gone_secure(context); } context->smstate->nextExpected = OTRL_SMP_EXPECT1; } } tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP_ABORT); if (tlv) { // qDebug() << "[OTR] SMP detected. Found SMP_ABORT"; sendCustomNessage(item,tr("Authentication error!").toLocal8Bit() ); context->smstate->nextExpected = OTRL_SMP_EXPECT1; } otrl_tlv_free(tlvs); break; } if (ignoreMessage == 1) // internal protocol message { OtrlMessageType type = otrl_proto_message_type( cryptedMessage.toStdString().c_str()); QString retMessage("<Internal OTR message>\n"+tr("received %1 \nOTR state now is [%2]").arg(otrlMessageTypeToString(type)).arg(getMessageStateString(to, from, item))) ; if (getMessageState(to, from, item) == qutimotr::OTR_MESSAGESTATE_ENCRYPTED) { retMessage.append(tr("\nsessionId: ") + getSessionId(to, from, item)); } // TODO: если бы эти сообщения можна было заблокировать... // но recivelevel1/2 не дает такой возможности... почему то // хотя в вики написано обратное // sendCustomNessage(item,retMessage); return retMessage; } else if (ignoreMessage == 0) { if (newMessage != NULL) // message has been decrypted. replace it { QString retMessage = QString::fromUtf8(newMessage); otrl_message_free(newMessage); return retMessage; } else // received message was not an otr message { return cryptedMessage; } } assert(false); return QString(); }