2007-11-14 22:18:48 -05:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2006 , crypton
*
* This program 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 2
* of the License , or ( at your option ) any later version .
*
* This program 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 this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <rshare.h>
2011-01-29 09:27:16 -05:00
# include <util/rsrandom.h>
2010-08-06 05:40:23 -04:00
# include <retroshare/rsinit.h>
2007-11-14 22:18:48 -05:00
# include "GenCertDialog.h"
2010-05-01 11:17:23 -04:00
# include <QAbstractEventDispatcher>
2007-11-14 22:18:48 -05:00
# include <QFileDialog>
# include <QMessageBox>
2010-01-20 17:02:43 -05:00
# include <time.h>
2012-10-31 21:07:36 -04:00
# include <iostream>
2007-11-14 22:18:48 -05:00
/** Default constructor */
2012-11-06 18:26:47 -05:00
GenCertDialog : : GenCertDialog ( bool onlyGenerateIdentity , QWidget * parent )
: QDialog ( parent , Qt : : WindowSystemMenuHint | Qt : : WindowTitleHint | Qt : : WindowCloseButtonHint ) , mOnlyGenerateIdentity ( onlyGenerateIdentity )
2007-11-14 22:18:48 -05:00
{
2012-10-31 21:07:36 -04:00
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
connect ( ui . new_gpg_key_checkbox , SIGNAL ( clicked ( ) ) , this , SLOT ( newGPGKeyGenUiSetup ( ) ) ) ;
connect ( ui . genButton , SIGNAL ( clicked ( ) ) , this , SLOT ( genPerson ( ) ) ) ;
connect ( ui . importIdentity_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( importIdentity ( ) ) ) ;
connect ( ui . exportIdentity_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( exportIdentity ( ) ) ) ;
//ui.genName->setFocus(Qt::OtherFocusReason);
2012-03-18 08:26:15 -04:00
# if QT_VERSION >= 0x040700
2012-10-31 21:07:36 -04:00
ui . email_input - > setPlaceholderText ( tr ( " [Optional] Visible to your friends, and friends of friends. " ) ) ;
ui . location_input - > setPlaceholderText ( tr ( " [Required] Examples: Home, Laptop,... " ) ) ;
ui . name_input - > setPlaceholderText ( tr ( " [Required] Visible to your friends, and friends of friends. " ) ) ;
ui . password_input - > setPlaceholderText ( tr ( " [Required] This password protects your PGP key. " ) ) ;
2012-03-18 08:26:15 -04:00
# endif
2012-10-31 21:07:36 -04:00
/* get all available pgp private certificates....
* mark last one as default .
*/
2012-08-07 15:16:17 -04:00
2012-10-31 21:07:36 -04:00
init ( ) ;
2012-08-07 15:16:17 -04:00
}
void GenCertDialog : : init ( )
{
2012-10-31 21:07:36 -04:00
std : : cerr < < " Finding PGPUsers " < < std : : endl ;
ui . genPGPuser - > clear ( ) ;
std : : list < std : : string > pgpIds ;
std : : list < std : : string > : : iterator it ;
bool foundGPGKeys = false ;
if ( ! mOnlyGenerateIdentity ) {
if ( RsInit : : GetPGPLogins ( pgpIds ) ) {
for ( it = pgpIds . begin ( ) ; it ! = pgpIds . end ( ) ; it + + )
{
QVariant userData ( QString : : fromStdString ( * it ) ) ;
std : : string name , email ;
RsInit : : GetPGPLoginDetails ( * it , name , email ) ;
std : : cerr < < " Adding PGPUser: " < < name < < " id: " < < * it < < std : : endl ;
QString gid = QString : : fromStdString ( * it ) . right ( 8 ) ;
ui . genPGPuser - > addItem ( QString : : fromUtf8 ( name . c_str ( ) ) + " < " + QString : : fromUtf8 ( email . c_str ( ) ) + " > ( " + gid + " ) " , userData ) ;
foundGPGKeys = true ;
}
}
}
if ( foundGPGKeys ) {
ui . no_gpg_key_label - > hide ( ) ;
ui . new_gpg_key_checkbox - > setChecked ( false ) ;
setWindowTitle ( tr ( " Create new Location " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Location " ) ) ;
ui . headerLabel - > setText ( tr ( " Create a new Location " ) ) ;
genNewGPGKey = false ;
} else {
ui . no_gpg_key_label - > setVisible ( ! mOnlyGenerateIdentity ) ;
ui . new_gpg_key_checkbox - > setChecked ( true ) ;
2013-03-19 17:18:49 -04:00
ui . new_gpg_key_checkbox - > setEnabled ( true ) ;
2012-10-31 21:07:36 -04:00
setWindowTitle ( tr ( " Create new Identity " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Identity " ) ) ;
ui . headerLabel - > setText ( tr ( " Create a new Identity " ) ) ;
genNewGPGKey = true ;
}
QString text = ui . headerLabel2 - > text ( ) + " \n " ;
if ( mOnlyGenerateIdentity ) {
ui . new_gpg_key_checkbox - > setChecked ( true ) ;
ui . new_gpg_key_checkbox - > hide ( ) ;
ui . label - > hide ( ) ;
text + = tr ( " You can create a new identity with this form. " ) ;
} else {
2012-12-01 19:25:11 -05:00
text + = tr ( " You can use an existing identity (i.e. a PGP key pair), from the list below, or create a new one with this form. " ) ;
2012-10-31 21:07:36 -04:00
}
ui . headerLabel2 - > setText ( text ) ;
newGPGKeyGenUiSetup ( ) ;
2007-11-14 22:18:48 -05:00
}
2010-01-19 16:44:13 -05:00
void GenCertDialog : : newGPGKeyGenUiSetup ( ) {
2010-06-04 20:44:43 -04:00
2012-10-31 21:07:36 -04:00
if ( ui . new_gpg_key_checkbox - > isChecked ( ) ) {
genNewGPGKey = true ;
ui . name_label - > show ( ) ;
ui . name_input - > show ( ) ;
ui . email_label - > show ( ) ;
ui . email_input - > show ( ) ;
ui . password_label - > show ( ) ;
ui . password_input - > show ( ) ;
ui . genPGPuserlabel - > hide ( ) ;
ui . genPGPuser - > hide ( ) ;
ui . importIdentity_PB - > hide ( ) ;
ui . exportIdentity_PB - > hide ( ) ;
setWindowTitle ( tr ( " Create new Identity " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Identity " ) ) ;
ui . headerLabel - > setText ( tr ( " Create a new Identity " ) ) ;
} else {
genNewGPGKey = false ;
ui . name_label - > hide ( ) ;
ui . name_input - > hide ( ) ;
ui . email_label - > hide ( ) ;
ui . email_input - > hide ( ) ;
ui . password_label - > hide ( ) ;
ui . password_input - > hide ( ) ;
ui . genPGPuserlabel - > show ( ) ;
ui . genPGPuser - > show ( ) ;
ui . importIdentity_PB - > setVisible ( ! mOnlyGenerateIdentity ) ;
ui . exportIdentity_PB - > setVisible ( ! mOnlyGenerateIdentity ) ;
ui . exportIdentity_PB - > setEnabled ( ui . genPGPuser - > count ( ) ! = 0 ) ;
setWindowTitle ( tr ( " Create new Location " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Location " ) ) ;
ui . headerLabel - > setText ( tr ( " Create a new Location " ) ) ;
}
2010-01-19 16:44:13 -05:00
}
2011-08-12 16:02:00 -04:00
2012-07-10 17:40:53 -04:00
void GenCertDialog : : exportIdentity ( )
{
2012-07-15 17:37:35 -04:00
QString fname = QFileDialog : : getSaveFileName ( this , tr ( " Export Identity " ) , " " , tr ( " RetroShare Identity files (*.asc) " ) ) ;
2012-07-10 17:40:53 -04:00
if ( fname . isNull ( ) )
return ;
QVariant data = ui . genPGPuser - > itemData ( ui . genPGPuser - > currentIndex ( ) ) ;
std : : string gpg_id = data . toString ( ) . toStdString ( ) ;
if ( RsInit : : exportIdentity ( fname . toStdString ( ) , gpg_id ) )
QMessageBox : : information ( this , tr ( " Identity saved " ) , tr ( " Your identity was successfully saved \n It is encrypted \n \n You can now copy it to another computer \n and use the import button to load it " ) ) ;
else
2012-10-11 06:28:24 -04:00
QMessageBox : : information ( this , tr ( " Identity not saved " ) , tr ( " Your identity was not saved. An error occurred. " ) ) ;
2012-07-10 17:40:53 -04:00
}
2012-10-31 21:07:36 -04:00
2012-07-10 17:40:53 -04:00
void GenCertDialog : : importIdentity ( )
{
2012-07-15 17:37:35 -04:00
QString fname = QFileDialog : : getOpenFileName ( this , tr ( " Export Identity " ) , " " , tr ( " RetroShare Identity files (*.asc) " ) ) ;
2012-07-10 17:40:53 -04:00
if ( fname . isNull ( ) )
return ;
std : : string gpg_id ;
2012-07-12 15:20:31 -04:00
std : : string err_string ;
2012-07-10 17:40:53 -04:00
2012-07-12 15:20:31 -04:00
if ( ! RsInit : : importIdentity ( fname . toStdString ( ) , gpg_id , err_string ) )
2012-07-10 17:40:53 -04:00
{
2012-07-12 15:20:31 -04:00
QMessageBox : : information ( this , tr ( " Identity not loaded " ) , tr ( " Your identity was not loaded properly: " ) + " \n " + QString : : fromStdString ( err_string ) ) ;
2012-07-10 17:40:53 -04:00
return ;
}
2012-08-07 15:16:17 -04:00
else
{
std : : string name , email ;
RsInit : : GetPGPLoginDetails ( gpg_id , name , email ) ;
std : : cerr < < " Adding PGPUser: " < < name < < " id: " < < gpg_id < < std : : endl ;
2012-07-10 17:40:53 -04:00
2012-10-11 06:28:24 -04:00
QMessageBox : : information ( this , tr ( " New identity imported " ) , tr ( " Your identity was imported successfully: " ) + " \n " + " \n Name : " + QString : : fromStdString ( name ) + " \n email: " + QString : : fromStdString ( email ) + " \n Key ID: " + QString : : fromStdString ( gpg_id ) + " \n \n " + tr ( " You can use it now to create a new location. " ) ) ;
2012-08-07 15:16:17 -04:00
}
2012-07-10 17:40:53 -04:00
2012-08-07 15:16:17 -04:00
init ( ) ;
2012-07-10 17:40:53 -04:00
}
2007-11-14 22:18:48 -05:00
void GenCertDialog : : genPerson ( )
{
/* Check the data from the GUI. */
2012-10-31 21:07:36 -04:00
std : : string genLoc = ui . location_input - > text ( ) . toUtf8 ( ) . constData ( ) ;
std : : string PGPId ;
if ( ! genNewGPGKey ) {
if ( genLoc . length ( ) < 3 ) {
/* Message Dialog */
QMessageBox : : warning ( this ,
2012-12-01 19:25:11 -05:00
tr ( " Generate PGP key Failure " ) ,
2012-10-31 21:07:36 -04:00
tr ( " Location field is required with a minimum of 3 characters " ) ,
QMessageBox : : Ok ) ;
return ;
}
int pgpidx = ui . genPGPuser - > currentIndex ( ) ;
if ( pgpidx < 0 )
{
/* Message Dialog */
QMessageBox : : warning ( this ,
" Generate ID Failure " ,
" Missing PGP Certificate " ,
QMessageBox : : Ok ) ;
return ;
}
QVariant data = ui . genPGPuser - > itemData ( pgpidx ) ;
PGPId = ( data . toString ( ) ) . toStdString ( ) ;
} else {
2012-12-04 06:42:34 -05:00
if ( ui . password_input - > text ( ) . length ( ) < 3 | | ui . name_input - > text ( ) . length ( ) < 3 | | genLoc . length ( ) < 3 ) {
2012-10-31 21:07:36 -04:00
/* Message Dialog */
QMessageBox : : warning ( this ,
2012-12-01 19:25:11 -05:00
tr ( " Generate PGP key Failure " ) ,
2012-10-31 21:07:36 -04:00
tr ( " All fields are required with a minimum of 3 characters " ) ,
QMessageBox : : Ok ) ;
return ;
}
//generate a new gpg key
std : : string err_string ;
2012-12-01 19:25:11 -05:00
ui . no_gpg_key_label - > setText ( tr ( " Generating new PGP key, please be patient: this process needs generating large prime numbers, and can take some minutes on slow computers. \n \n Fill in your PGP password when asked, to sign your new key. " ) ) ;
2012-10-31 21:07:36 -04:00
ui . no_gpg_key_label - > show ( ) ;
ui . new_gpg_key_checkbox - > hide ( ) ;
ui . name_label - > hide ( ) ;
ui . name_input - > hide ( ) ;
ui . email_label - > hide ( ) ;
ui . email_input - > hide ( ) ;
ui . password_label - > hide ( ) ;
ui . password_input - > hide ( ) ;
ui . genPGPuserlabel - > hide ( ) ;
ui . genPGPuser - > hide ( ) ;
ui . location_label - > hide ( ) ;
ui . location_input - > hide ( ) ;
ui . genButton - > hide ( ) ;
ui . label_location2 - > hide ( ) ;
ui . importIdentity_PB - > hide ( ) ;
setCursor ( Qt : : WaitCursor ) ;
QCoreApplication : : processEvents ( ) ;
while ( QAbstractEventDispatcher : : instance ( ) - > processEvents ( QEventLoop : : AllEvents ) ) ;
RsInit : : GeneratePGPCertificate ( ui . name_input - > text ( ) . toUtf8 ( ) . constData ( ) , ui . email_input - > text ( ) . toUtf8 ( ) . constData ( ) , ui . password_input - > text ( ) . toUtf8 ( ) . constData ( ) , PGPId , err_string ) ;
setCursor ( Qt : : ArrowCursor ) ;
}
2009-08-04 19:37:01 -04:00
2009-08-18 08:43:48 -04:00
//generate a random ssl password
2011-01-29 09:27:16 -05:00
std : : string sslPasswd = RSRandom : : random_alphaNumericString ( RsInit : : getSslPwdLen ( ) ) ;
2011-01-29 09:31:44 -05:00
2009-08-04 19:37:01 -04:00
/* Initialise the PGP user first */
RsInit : : SelectGPGAccount ( PGPId ) ;
std : : string sslId ;
2011-08-12 16:02:00 -04:00
std : : cerr < < " GenCertDialog::genPerson() Generating SSL cert with gpg id : " < < PGPId < < std : : endl ;
std : : string err ;
bool okGen = RsInit : : GenerateSSLCertificate ( PGPId , " " , genLoc , " " , sslPasswd , sslId , err ) ;
2007-11-14 22:18:48 -05:00
if ( okGen )
{
/* complete the process */
2009-08-18 08:43:48 -04:00
RsInit : : LoadPassword ( sslId , sslPasswd ) ;
2012-10-31 21:07:36 -04:00
if ( Rshare : : loadCertificate ( sslId , false , PGPId ) ) {
accept ( ) ;
}
2007-11-14 22:18:48 -05:00
}
else
{
/* Message Dialog */
2012-02-21 14:17:38 -05:00
QMessageBox : : warning ( this ,
2012-10-31 21:07:36 -04:00
tr ( " Generate ID Failure " ) ,
tr ( " Failed to Generate your new Certificate, maybe PGP password is wrong! " ) ,
2011-08-12 16:02:00 -04:00
QMessageBox : : Ok ) ;
2007-11-14 22:18:48 -05:00
}
}