File: passwd_dialog.cpp

package info (click to toggle)
classified-ads 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 6,772 kB
  • sloc: cpp: 34,291; tcl: 1,175; xml: 64; makefile: 40
file content (118 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2016.

  This file is part of Classified Ads.

  Classified Ads is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Classified Ads 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <QtGui>
#include "passwd_dialog.h"
#include "../mcontroller.h"
#include "../log.h"
#include "../datamodel/model.h"
#include "../datamodel/contentencryptionmodel.h"

PasswdDialog::PasswdDialog(QWidget *aParent,
                           MController& aController,
                           const QString& aPrompt,
                           bool aIsPwdQueryDialog)
    : QDialog(aParent),
      iController(aController),
      iPrompt(aPrompt),
      iIsPwdQueryDialog(aIsPwdQueryDialog) {
    ui.setupUi(this) ;

    connect(ui.okButton, SIGNAL(clicked()),
            this, SLOT(okClicked()));

    ui.passwordPromptLabel->setText( aPrompt );
    ui.passwordEdit->setFocus() ;
    ui.passwordEdit->setEchoMode(QLineEdit::Password) ;
    connect(ui.echoPwdCheckBox, SIGNAL(stateChanged(int)),
            this, SLOT(showPasswordCheckBoxStateChanged(int))) ;
    LOG_STR("### PasswdDialog constructor out") ;
}
PasswdDialog::~PasswdDialog() {
    LOG_STR("PasswdDialog::~PasswdDialog") ;
}

void PasswdDialog::okClicked() {
    QString text = ui.passwordEdit->text();
    bool goodPasswordFound = false ;
    // 5 characters is damn lousy passwd.
    // this implementation does not restrict user to
    // any particular passwd max len, informed users are
    // still free to use long passphrases.
    if ( text.size() > 4 ) {
        if ( iIsPwdQueryDialog ) {
            iController.model().lock() ;
            iController.setContentKeyPasswd(text) ;
            QList<Hash> privateKeys = iController.model().contentEncryptionModel().listKeys(true,NULL)  ;
            if ( privateKeys.size() == 0 ) {
                // because we had no secret key of any kind lets generate one
                iController.setProfileInUse(iController.model().contentEncryptionModel().generateKeyPair()) ;
                close() ;
                this->deleteLater() ;
            } else {
                // try each private key we have, if we have one
                // where the password matches then use that
                for ( int i = 0 ; i  < privateKeys.size() ; i++ ) {
                    Hash keyFingerPrint = privateKeys[i] ;
                    QLOG_STR("Trying key fp " + keyFingerPrint.toString()) ;
                    QByteArray PEMBytes  ;
                    if ( iController.model().contentEncryptionModel().PrivateKey(keyFingerPrint,PEMBytes) == true ) {
                        EVP_PKEY* privateKey = iController.model().contentEncryptionModel().PrivateKeyFromPem(PEMBytes,false) ;
                        if ( privateKey != NULL ) {
                            // if we came here e.g. got the key, it is a sign
                            // that we also had the correct password to open the key
                            iController.setProfileInUse(keyFingerPrint) ;
                            EVP_PKEY_free(privateKey) ;
                            close() ;
                            QLOG_STR("Using profile " + keyFingerPrint.toString()) ;
                            this->deleteLater() ;
                            goodPasswordFound = true ;
                        }
                    }
                }
                if ( !goodPasswordFound )   {
                    QString errmsg("Bad password") ;
                    emit error(MController::BadPassword, errmsg) ;
                }
            }
            iController.model().unlock() ;
        } else {
            // this is password change dialog
            iController.model().lock() ;
            if ( iController.model().contentEncryptionModel().changeKeyPassword(iController.profileInUse(),text) == 0 ) {
                iController.setContentKeyPasswd(text) ;
            }
            iController.model().unlock() ;
            close() ;
            this->deleteLater() ;
        }
    } else {
        QString errmsg(tr("Min length 5 (use 10+)")) ;
        emit error(MController::ContentEncryptionError, errmsg) ;
    }
}

void PasswdDialog::showPasswordCheckBoxStateChanged(int aState) {
    if ( aState == Qt::Checked ) {
        ui.passwordEdit->setEchoMode(QLineEdit::Normal) ;
    } else {
        ui.passwordEdit->setEchoMode(QLineEdit::Password) ;
    }
}