Changeset 401:d6a5b9530d88 in SMSSender


Ignore:
Timestamp:
May 2, 2014 1:47:47 PM (4 years ago)
Author:
Sämy Zehnder <saemy.zehnder@…>
Branch:
default
Message:
  • Encrypts all packages to the smartphone.
  • Replaces connection-secret by a per-connection peer-device which contains the secret.
Location:
gateways/Smartphone
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • gateways/Smartphone/business/connectionhandler.cpp

    r336 r401  
    4646    , m_smartphoneManager(smartphoneManager)
    4747    , m_cSocket(cSocket) // We take ownership!
    48     , m_peerDevice(peerDevice)
    4948{
     49    m_peerDevice = !peerDevice.isNull() ? peerDevice : Smartphone();
     50
    5051    // Register the data handlers
    5152    m_dataHandlers.insert(new PingHandlerV1);
     
    7677    alivenessTimer->start(10000);
    7778
    78     if (!peerDevice.isNull() && !peerDevice.secret().isNull()) {
    79         m_connectionSecret = peerDevice.secret();
    80 
     79    if (!this->peerDevice().secret().isEmpty()) {
    8180        // Send initial hello
    8281        sendHello();
     
    139138 */
    140139void ConnectionHandler::processData(QByteArray &data, bool encrypted) {
    141     Secret validSecret;
    142     QString decryptedData = (encrypted) ? decryptData(data, validSecret) : QString::fromUtf8(data);
     140    QString decryptedData = (encrypted) ? decryptData(data) : QString::fromUtf8(data);
    143141
    144142    if (decryptedData.isEmpty()) {
     
    166164    }
    167165
    168     if (!validSecret.isNull()) {
    169         m_connectionSecret = validSecret;
    170     }
    171 
    172166    bool someoneHandled = false;
    173167    foreach (IDataHandler *handler, m_dataHandlers) {
     
    214208
    215209void ConnectionHandler::sendPing() const {
    216     sendPackage(PackageWizard::createPingPackage(), false);
     210    sendPackage(PackageWizard::createPingPackage(), true);
    217211}
    218212
     
    235229 */
    236230QByteArray ConnectionHandler::encryptData(const QString &dataStr) const {
    237     Q_ASSERT(!m_connectionSecret.isNull());
     231    Q_ASSERT(!peerDevice().secret().isEmpty());
    238232
    239233    using namespace CryptoPP;
     
    246240    // Encrypt the package
    247241    CBC_Mode<AES>::Encryption enc;
    248     enc.SetKeyWithIV((byte *) m_connectionSecret.key(), Secret::KEY_LENGTH, iv);
     242    enc.SetKeyWithIV((byte *) peerDevice().secret().key(), Secret::KEY_LENGTH,
     243                     iv, IV_LENGTH);
    249244
    250245    std::string ciphertext;
     
    260255
    261256/**
    262  * Decrypts the given package. If decryption succeeds with a secret, it is returned via the
    263  * validSecret-param.
     257 * Decrypts the given package. If decryption succeeds with some secret, it is
     258 * set as the connection secret.
    264259 *
    265260 * @param data The package
    266  * @param validSecret (out) The valid secret
    267261 * @return The decrypted package
    268262 */
    269 QString ConnectionHandler::decryptData(QByteArray &data, Secret &validSecret) {
     263QString ConnectionHandler::decryptData(QByteArray &data) {
    270264    QSet<Secret> secrets;
    271     if (m_connectionSecret.isNull()) {
     265    if (peerDevice().secret().isEmpty()) {
    272266        // Try all the secrets
    273         secrets = m_smartphoneManager->unidentifiedSecrets();
     267        secrets = m_smartphoneManager->unassociatedSecrets();
    274268    } else {
    275         secrets.insert(m_connectionSecret);
     269        secrets.insert(peerDevice().secret());
    276270    }
    277271
     
    282276    // Decrypt the package
    283277    QString decryptedData;
     278    Secret validSecret;
    284279    foreach (Secret secret, secrets) {
    285280        try {
     
    295290            decryptedData = QString::fromUtf8(recovered.data(), recovered.size());
    296291        } catch (const CryptoPP::Exception &e) {
    297             // Invalid secret or modified package
     292            // Invalid secret or corrupt package
    298293            continue;
    299294        }
     
    303298    }
    304299
     300    if (peerDevice().secret().isEmpty()) {
     301        // Set this secret as the connection secret
     302        peerDevice().setSecret(validSecret);
     303    }
     304
    305305    return decryptedData;
    306306}
     
    311311 */
    312312void ConnectionHandler::onPeerIdentified(const QString &deviceIdent) {
    313     if (!m_peerDevice.isNull() && (m_peerDevice.deviceIdent() != deviceIdent)) {
    314         qCritical() << "There is an other device on this connection than expected. We ignore further requests from it!";
     313    if (!peerDevice().isNull() && (peerDevice().deviceIdent() != deviceIdent)) {
     314        qCritical() << "There is an other device on this connection than "
     315                    << "expected. We ignore further requests from it!";
    315316        return;
    316317    }
    317318
    318     Q_ASSERT(!m_connectionSecret.isNull());
    319     m_smartphoneManager->removeUnidentifiedSecret(m_connectionSecret);
    320 
    321     m_peerDevice = m_smartphoneManager->device(deviceIdent);
    322     m_peerDevice.setSecret(m_connectionSecret);
     319    setPeerDevice(m_smartphoneManager->device(deviceIdent));
     320    if (peerDevice().isNull()) {
     321        // We do not know this device
     322        return;
     323    }
     324
     325    // We have to know the connection secret to talk to the peer
     326    Q_ASSERT(!peerDevice().secret().isEmpty());
     327
     328    m_smartphoneManager->removeUnassociatedSecret(peerDevice().secret());
    323329
    324330    Q_ASSERT(isPeerIdentified());
    325331    emit peerIdentified(this);
    326332
    327     // If we did not know the secret, we did not send a hello yet
     333    // If we did not knew the secret, we did not have sent a hello yet
    328334    sendHello();
    329335}
     
    369375    return m_peerDevice;
    370376}
     377void ConnectionHandler::setPeerDevice(const Smartphone &peerDevice) {
     378    // Rescues the possibly already set connection secret.
     379    Secret oldConnectionSecret = m_peerDevice.secret();
     380
     381    m_peerDevice = peerDevice;
     382
     383    if (m_peerDevice.secret().isEmpty()) {
     384        // Restores the possibly already set connection secret.
     385        m_peerDevice.setSecret(oldConnectionSecret);
     386    }
     387}
    371388
    372389const QTcpSocket *ConnectionHandler::socket() const {
  • gateways/Smartphone/business/connectionhandler.h

    r382 r401  
    5858    bool isPeerIdentified() const;
    5959    Smartphone peerDevice() const;
     60    void setPeerDevice(const Smartphone &peerDevice);
    6061
    6162signals:
     
    7071
    7172    QByteArray encryptData(const QString &dataStr) const;
    72     QString decryptData(QByteArray &data, Secret &validSecret);
     73    QString decryptData(QByteArray &data);
    7374
    7475private slots:
     
    9394
    9495    Smartphone m_peerDevice;
    95     Secret m_connectionSecret;
    9696    QDateTime m_lastTimeDataReceived;
    9797};
  • gateways/Smartphone/business/datatypes.cpp

    r336 r401  
    134134
    135135bool Secret::isNull() const {
    136     return d->key.data() == NULL;
     136    return (d->key.data() == NULL) || d->key.empty();
     137}
     138
     139bool Secret::isEmpty() const {
     140    return isNull() || key().empty();
    137141}
    138142
  • gateways/Smartphone/business/datatypes.h

    r336 r401  
    9292
    9393public:
    94     bool isNull() const;
     94    bool isEmpty() const;
    9595    CryptoPP::SecByteBlock key() const;
    9696
    9797    const static int KEY_LENGTH;
     98
     99private:
     100    bool isNull() const;
    98101};
    99102
  • gateways/Smartphone/business/smartphonemanager.cpp

    r336 r401  
    109109Secret SmartphoneManager::generateUnidentifiedSecret() {
    110110    Secret secret = Secret::generateSecret();
    111     m_unidentifiedSecrets.insert(secret);
     111    m_unassociatedSecrets.insert(secret);
    112112
    113113    return secret;
    114114}
    115 QSet<Secret> SmartphoneManager::unidentifiedSecrets() const {
    116     return m_unidentifiedSecrets;
     115QSet<Secret> SmartphoneManager::unassociatedSecrets() const {
     116    return m_unassociatedSecrets;
    117117}
    118 void SmartphoneManager::removeUnidentifiedSecret(const Secret &secret) {
    119     m_unidentifiedSecrets.remove(secret);
     118void SmartphoneManager::removeUnassociatedSecret(const Secret &secret) {
     119    m_unassociatedSecrets.remove(secret);
    120120}
    121121
  • gateways/Smartphone/business/smartphonemanager.h

    r382 r401  
    6464
    6565    Secret generateUnidentifiedSecret();
    66     QSet<Secret> unidentifiedSecrets() const;
    67     void removeUnidentifiedSecret(const Secret &secret);
     66    QSet<Secret> unassociatedSecrets() const;
     67    void removeUnassociatedSecret(const Secret &secret);
    6868
    6969public:
     
    9292
    9393    QSet<Smartphone> m_toBePolleddevices;
    94     QSet<Secret> m_unidentifiedSecrets;
     94    QSet<Secret> m_unassociatedSecrets;
    9595};
    9696
  • gateways/Smartphone/gui/ui/settingswidget.cpp

    r382 r401  
    5555
    5656void SettingsWidget::generateQRCode() {
    57     if (m_initialDevice.secret().isNull()) {
     57    if (m_initialDevice.secret().isEmpty()) {
    5858        m_initialDevice.setSecret(m_smartphoneManager->generateUnidentifiedSecret());
    5959    }
  • gateways/Smartphone/persistence/gatewaystoragehelper.cpp

    r336 r401  
    5353        QString name = storage()->readString(getDeviceNamespace(i), "name", "");
    5454
     55        if (deviceIdent.isEmpty()) {
     56            Q_ASSERT(false); // Should really not happen...
     57            continue;
     58        }
     59
    5560        QString secretStr = storage()->readEncryptedString(getDeviceNamespace(i), "secret", "");
    5661        Secret secret = Secret::fromString(secretStr);
     
    97102        connections.remove(connections.length()-1, 1);
    98103        storage()->writeString(getDeviceNamespace(i), "paths", connections);
     104
     105        ++i;
    99106    }
    100107}
Note: See TracChangeset for help on using the changeset viewer.