From 2b20ab7fca5e6c0d687a90322bc9ec48f6e30922 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 15 Aug 2012 20:08:18 +0000 Subject: [PATCH] fixed crashes with poping ConfCertDialog with incomplete certificate git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5429 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pgp/rscertificate.cc | 55 +++++++++++++---- libretroshare/src/pgp/rscertificate.h | 2 + libretroshare/src/util/rsid.h | 2 +- retroshare-gui/src/gui/NetworkDialog.cpp | 78 ++++++++++++------------ 4 files changed, 84 insertions(+), 53 deletions(-) diff --git a/libretroshare/src/pgp/rscertificate.cc b/libretroshare/src/pgp/rscertificate.cc index 8ef42d2a9..625ff21ee 100644 --- a/libretroshare/src/pgp/rscertificate.cc +++ b/libretroshare/src/pgp/rscertificate.cc @@ -71,11 +71,15 @@ std::string RsCertificate::toStdString() const unsigned char *buf = new unsigned char[BS] ; addPacket( CERTIFICATE_PTAG_PGP_SECTION , binary_pgp_key , binary_pgp_key_size , buf, p, BS ) ; - addPacket( CERTIFICATE_PTAG_EXTIPANDPORT_SECTION, ipv4_external_ip_and_port , 6 , buf, p, BS ) ; - addPacket( CERTIFICATE_PTAG_LOCIPANDPORT_SECTION, ipv4_internal_ip_and_port , 6 , buf, p, BS ) ; - addPacket( CERTIFICATE_PTAG_DNS_SECTION , (unsigned char *)dns_name.c_str() , dns_name.length() , buf, p, BS ) ; - addPacket( CERTIFICATE_PTAG_NAME_SECTION , (unsigned char *)location_name.c_str() ,location_name.length() , buf, p, BS ) ; - addPacket( CERTIFICATE_PTAG_SSLID_SECTION , location_id.toByteArray() ,location_id.SIZE_IN_BYTES, buf, p, BS ) ; + + if(!only_pgp) + { + addPacket( CERTIFICATE_PTAG_EXTIPANDPORT_SECTION, ipv4_external_ip_and_port , 6 , buf, p, BS ) ; + addPacket( CERTIFICATE_PTAG_LOCIPANDPORT_SECTION, ipv4_internal_ip_and_port , 6 , buf, p, BS ) ; + addPacket( CERTIFICATE_PTAG_DNS_SECTION , (unsigned char *)dns_name.c_str() , dns_name.length() , buf, p, BS ) ; + addPacket( CERTIFICATE_PTAG_NAME_SECTION , (unsigned char *)location_name.c_str() ,location_name.length() , buf, p, BS ) ; + addPacket( CERTIFICATE_PTAG_SSLID_SECTION , location_id.toByteArray() ,location_id.SIZE_IN_BYTES, buf, p, BS ) ; + } std::string out_string ; @@ -116,20 +120,30 @@ RsCertificate::RsCertificate(const RsPeerDetails& Detail, const unsigned char *b if(binary_pgp_block_size == 0 || binary_pgp_block == NULL) throw std::runtime_error("Cannot init a certificate with a void key block.") ; - if(Detail.isOnlyGPGdetail) - throw std::runtime_error("Cannot init a certificate with a RsPeerDetails with only GPG details.") ; - binary_pgp_key = new unsigned char[binary_pgp_block_size] ; memcpy(binary_pgp_key,binary_pgp_block,binary_pgp_block_size) ; binary_pgp_key_size = binary_pgp_block_size ; - location_id = SSLIdType( Detail.id ) ; - location_name = Detail.location ; + if(!Detail.isOnlyGPGdetail) + { + only_pgp = false ; + location_id = SSLIdType( Detail.id ) ; + location_name = Detail.location ; - scan_ip(Detail.localAddr,Detail.localPort,ipv4_internal_ip_and_port) ; - scan_ip(Detail.extAddr,Detail.extPort,ipv4_external_ip_and_port) ; + scan_ip(Detail.localAddr,Detail.localPort,ipv4_internal_ip_and_port) ; + scan_ip(Detail.extAddr,Detail.extPort,ipv4_external_ip_and_port) ; - dns_name = Detail.dyndns ; + dns_name = Detail.dyndns ; + } + else + { + only_pgp = true ; + location_id = SSLIdType() ; + location_name = "" ; + memset(ipv4_internal_ip_and_port,0,6) ; + memset(ipv4_external_ip_and_port,0,6) ; + dns_name = "" ; + } } void RsCertificate::scan_ip(const std::string& ip_string, unsigned short port,unsigned char *ip_and_port) @@ -195,6 +209,7 @@ bool RsCertificate::initFromString(const std::string& instr,std::string& err_str #ifdef DEBUG_RSCERTIFICATE std::cerr << "Packet parse: read ptag " << (int)ptag << ", size " << s << ", total_s = " << total_s << ", expected total = " << size << std::endl; #endif + only_pgp = true ; switch(ptag) { @@ -206,6 +221,7 @@ bool RsCertificate::initFromString(const std::string& instr,std::string& err_str case CERTIFICATE_PTAG_NAME_SECTION: location_name = std::string((char *)buf,s) ; buf = &buf[s] ; + only_pgp = false ; break ; case CERTIFICATE_PTAG_SSLID_SECTION: @@ -217,10 +233,12 @@ bool RsCertificate::initFromString(const std::string& instr,std::string& err_str location_id = SSLIdType(buf) ; buf = &buf[s] ; + only_pgp = false ; break ; case CERTIFICATE_PTAG_DNS_SECTION: dns_name = std::string((char *)buf,s) ; buf = &buf[s] ; + only_pgp = false ; break ; case CERTIFICATE_PTAG_LOCIPANDPORT_SECTION: @@ -230,6 +248,7 @@ bool RsCertificate::initFromString(const std::string& instr,std::string& err_str return false ; } + only_pgp = false ; memcpy(ipv4_internal_ip_and_port,buf,s) ; buf = &buf[s] ; break ; @@ -240,6 +259,7 @@ bool RsCertificate::initFromString(const std::string& instr,std::string& err_str return false ; } + only_pgp = false ; memcpy(ipv4_external_ip_and_port,buf,s) ; buf = &buf[s] ; break ; @@ -711,6 +731,9 @@ std::string RsCertificate::toStdString_oldFormat() const res += PGPKeyManagement::makeArmouredKey(binary_pgp_key,binary_pgp_key_size,pgp_version) ; + if(only_pgp) + return res ; + res += SSLID_BEGIN_SECTION ; res += location_id.toStdString() ; res += ";" ; @@ -775,6 +798,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str Radix64::decode(radix_cert,key_bin,binary_pgp_key_size) ; binary_pgp_key = (unsigned char *)key_bin ; + only_pgp = true ; #ifdef P3PEERS_DEBUG std::cerr << "Parsing cert for sslid, location, ext and local address details. : " << certstr << std::endl; @@ -792,6 +816,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str std::cerr << "SSL id : " << ssl_id << std::endl; location_id = SSLIdType(ssl_id) ; + only_pgp = false ; } } @@ -807,6 +832,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str std::cerr << "location : " << location << std::endl; location_name = location; + only_pgp = false ; } } @@ -832,6 +858,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str sscanf(local_port.c_str(), "%hu", &localPort); } + only_pgp = false ; scan_ip(local_ip,localPort,ipv4_internal_ip_and_port) ; } } @@ -858,6 +885,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str } scan_ip(ext_ip,extPort,ipv4_external_ip_and_port) ; + only_pgp = false ; } } @@ -873,6 +901,7 @@ bool RsCertificate::initFromString_oldFormat(const std::string& certstr,std::str std::cerr << "DynDNS : " << DynDNS << std::endl; dns_name = DynDNS; + only_pgp = false ; } } diff --git a/libretroshare/src/pgp/rscertificate.h b/libretroshare/src/pgp/rscertificate.h index 52820e46f..187e9795e 100644 --- a/libretroshare/src/pgp/rscertificate.h +++ b/libretroshare/src/pgp/rscertificate.h @@ -69,5 +69,7 @@ class RsCertificate SSLIdType location_id ; std::string pgp_version ; std::string dns_name ; + + bool only_pgp ; // does the cert contain only pgp info? }; diff --git a/libretroshare/src/util/rsid.h b/libretroshare/src/util/rsid.h index df1cf420d..2c29a1f7e 100644 --- a/libretroshare/src/util/rsid.h +++ b/libretroshare/src/util/rsid.h @@ -34,7 +34,7 @@ template class t_RsGenericIdType { public: - t_RsGenericIdType() {} + t_RsGenericIdType() { memset(bytes,0,ID_SIZE_IN_BYTES) ;} virtual ~t_RsGenericIdType() {} // Explicit constructor from a hexadecimal string diff --git a/retroshare-gui/src/gui/NetworkDialog.cpp b/retroshare-gui/src/gui/NetworkDialog.cpp index 12e9e7759..0ea086813 100644 --- a/retroshare-gui/src/gui/NetworkDialog.cpp +++ b/retroshare-gui/src/gui/NetworkDialog.cpp @@ -195,60 +195,60 @@ void NetworkDialog::updateNewDiscoveryInfo() void NetworkDialog::connecttreeWidgetCostumPopupMenu( QPoint /*point*/ ) { - //std::cerr << "NetworkDialog::connecttreeWidgetCostumPopupMenu( QPoint point ) called" << std::endl; - QTreeWidgetItem *wi = getCurrentNeighbour(); - if (!wi) - return; + //std::cerr << "NetworkDialog::connecttreeWidgetCostumPopupMenu( QPoint point ) called" << std::endl; + QTreeWidgetItem *wi = getCurrentNeighbour(); + if (!wi) + return; - QMenu contextMnu( this ); + QMenu *contextMnu = new QMenu; std::string peer_id = wi->text(COLUMN_PEERID).toStdString() ; - // That's what context menus are made for - RsPeerDetails detail; - if(!rsPeers->getGPGDetails(peer_id, detail)) // that is not suppose to fail. - return ; + // That's what context menus are made for + RsPeerDetails detail; + if(!rsPeers->getGPGDetails(peer_id, detail)) // that is not suppose to fail. + return ; - if(peer_id != rsPeers->getGPGOwnId()) + if(peer_id != rsPeers->getGPGOwnId()) + { + if(detail.accept_connection) { - if(detail.accept_connection) - { - QAction* denyFriendAct = new QAction(QIcon(IMAGE_DENIED), tr( "Deny friend" ), &contextMnu ); + QAction* denyFriendAct = new QAction(QIcon(IMAGE_DENIED), tr( "Deny friend" ), contextMnu ); - connect( denyFriendAct , SIGNAL( triggered() ), this, SLOT( denyFriend() ) ); - contextMnu.addAction( denyFriendAct); - } - else // not a friend - { - QAction* makefriendAct = new QAction(QIcon(IMAGE_MAKEFRIEND), tr( "Make friend" ), &contextMnu ); + connect( denyFriendAct , SIGNAL( triggered() ), this, SLOT( denyFriend() ) ); + contextMnu->addAction( denyFriendAct); + } + else // not a friend + { + QAction* makefriendAct = new QAction(QIcon(IMAGE_MAKEFRIEND), tr( "Make friend" ), contextMnu ); - connect( makefriendAct , SIGNAL( triggered() ), this, SLOT( makeFriend() ) ); - contextMnu.addAction( makefriendAct); + connect( makefriendAct , SIGNAL( triggered() ), this, SLOT( makeFriend() ) ); + contextMnu->addAction( makefriendAct); #ifdef TODO - if(detail.validLvl > RS_TRUST_LVL_MARGINAL) // it's a denied old friend. - { - QAction* deleteCertAct = new QAction(QIcon(IMAGE_PEERDETAILS), tr( "Delete certificate" ), &contextMnu ); - connect( deleteCertAct, SIGNAL( triggered() ), this, SLOT( deleteCert() ) ); - contextMnu.addAction( deleteCertAct ); - } + if(detail.validLvl > RS_TRUST_LVL_MARGINAL) // it's a denied old friend. + { + QAction* deleteCertAct = new QAction(QIcon(IMAGE_PEERDETAILS), tr( "Delete certificate" ), contextMnu ); + connect( deleteCertAct, SIGNAL( triggered() ), this, SLOT( deleteCert() ) ); + contextMnu->addAction( deleteCertAct ); + } #endif - } - } - if(peer_id == rsPeers->getGPGOwnId()) - { - QAction* exportcertAct = new QAction(QIcon(IMAGE_EXPORT), tr( "Export my Cert" ), &contextMnu ); - connect( exportcertAct , SIGNAL( triggered() ), this, SLOT( on_actionExportKey_activated() ) ); - contextMnu.addAction( exportcertAct); } + } + if(peer_id == rsPeers->getGPGOwnId()) + { + QAction* exportcertAct = new QAction(QIcon(IMAGE_EXPORT), tr( "Export my Cert" ), contextMnu ); + connect( exportcertAct , SIGNAL( triggered() ), this, SLOT( on_actionExportKey_activated() ) ); + contextMnu->addAction( exportcertAct); + } - QAction* peerdetailsAct = new QAction(QIcon(IMAGE_PEERDETAILS), tr( "Peer details..." ), &contextMnu ); - connect( peerdetailsAct , SIGNAL( triggered() ), this, SLOT( peerdetails() ) ); - contextMnu.addAction( peerdetailsAct); + QAction* peerdetailsAct = new QAction(QIcon(IMAGE_PEERDETAILS), tr( "Peer details..." ), contextMnu ); + connect( peerdetailsAct , SIGNAL( triggered() ), this, SLOT( peerdetails() ) ); + contextMnu->addAction( peerdetailsAct); - contextMnu.addAction(QIcon(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyLink())); + contextMnu->addAction(QIcon(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyLink())); - contextMnu.exec(QCursor::pos()); + contextMnu->exec(QCursor::pos()); } void NetworkDialog::denyFriend()