From 8f0793f0711d964c4fdc15d1aed1f711b0a65c55 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Fri, 8 Apr 2011 18:53:12 +0000 Subject: [PATCH] Removed automatic cleaning of the certificate in p3Peers::loadDetailsFromStringCert. Added new button in ConnectFriendWizard for cleaning the certificate. Fixed cleaning: - added blank line after the armor header - moved the checksum to a new line Recompile of the GUI needed. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4134 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pqi/cleanupxpgp.cc | 151 ++++++++++++------ libretroshare/src/pqi/cleanupxpgp.h | 5 +- libretroshare/src/retroshare/rspeers.h | 1 + libretroshare/src/rsserver/p3peers.cc | 72 ++++++--- libretroshare/src/rsserver/p3peers.h | 3 +- .../src/gui/connect/ConnectFriendWizard.cpp | 33 +++- .../src/gui/connect/ConnectFriendWizard.h | 11 +- 7 files changed, 192 insertions(+), 84 deletions(-) diff --git a/libretroshare/src/pqi/cleanupxpgp.cc b/libretroshare/src/pqi/cleanupxpgp.cc index 5946e8c2e..8e74ba4f0 100644 --- a/libretroshare/src/pqi/cleanupxpgp.cc +++ b/libretroshare/src/pqi/cleanupxpgp.cc @@ -26,6 +26,7 @@ #include "cleanupxpgp.h" #include #include //strlen +#include /* Method for cleaning up the certificate. This method removes any unnecessay white spaces and unnecessary @@ -40,7 +41,7 @@ end tag we take care of cases like ----- END XPGP . Here extra empty spaces h introduced and the actual tag should have been -----END XPGP */ -std::string cleanUpCertificate(std::string badCertificate) +std::string cleanUpCertificate(const std::string& badCertificate) { /* Buffer for storing the cleaned certificate. In certain cases the @@ -48,33 +49,33 @@ std::string cleanUpCertificate(std::string badCertificate) */ std::string cleanCertificate; //The entire certificate begin tag - const char * beginCertTag="-----BEGIN"; + const char * beginCertTag="-----BEGIN"; //The entire certificate end tag - const char * endCertTag="-----END"; + const char * endCertTag="-----END"; //Tag containing dots. The common part of both start and end tags - const char * commonTag="-----"; + const char * commonTag="-----"; //Only BEGIN part of the begin tag - const char * beginTag="BEGIN"; + const char * beginTag="BEGIN"; //Only END part of the end tag - const char * endTag="END"; + const char * endTag="END"; //The start index of the ----- part of the certificate begin tag - size_t beginCertStartIdx1=0; + size_t beginCertStartIdx1=0; //The start index of the BEGIN part of the certificate begin tag - size_t beginCertStartIdx2=0; + size_t beginCertStartIdx2=0; //The start index of the end part(-----) of the certificate begin tag. The begin tag ends with -----. Example -----BEGIN XPGP CERTIFICATE----- - size_t beginCertEndIdx=0; + size_t beginCertEndIdx=0; //The start index of the ----- part of the certificate end tag - size_t endCertStartIdx1=0; + size_t endCertStartIdx1=0; //The start index of the END part of the certificate end tag - size_t endCertStartIdx2=0; + size_t endCertStartIdx2=0; //The start index of the end part(-----) of the certificate end tag. The begin tag ends with -----. Example -----BEGIN XPGP CERTIFICATE----- - size_t endCertEndIdx=0; + size_t endCertEndIdx=0; //The length of the bad certificate. - size_t lengthOfCert=badCertificate.length(); + size_t lengthOfCert=badCertificate.length(); //The current index value in the bad certificate - size_t currBadCertIdx=0; + size_t currBadCertIdx=0; //Temporary index value - size_t tmpIdx=0; + size_t tmpIdx=0; //Boolean flag showing if the begin tag or the end tag has been found bool found=false; /* @@ -92,7 +93,7 @@ std::string cleanUpCertificate(std::string badCertificate) if(beginCertStartIdx2!=std::string::npos) { found=true; - for(size_t i=beginCertStartIdx1+strlen(commonTag);i header; + header.push_back("Version"); + header.push_back("Comment"); + header.push_back("MessageID"); + header.push_back("Hash"); + header.push_back("Charset"); + + for (std::list::iterator headerIt = header.begin (); headerIt != header.end(); headerIt++) + { + if (badCertificate.substr(currBadCertIdx, (*headerIt).length()) == *headerIt) + { + cleanCertificate += badCertificate.substr(currBadCertIdx, (*headerIt).length()); + currBadCertIdx += (*headerIt).length(); + while(badCertificate[currBadCertIdx]!='\n') + { + cleanCertificate += badCertificate[currBadCertIdx]; + currBadCertIdx++; + } + cleanCertificate += "\n"; + } + } + + //add empty line after armor header + cleanCertificate += "\n"; //Start of the actual certificate. Remove spaces in the certificate //and make sure there are 64 characters per line in the @@ -236,6 +252,11 @@ std::string cleanUpCertificate(std::string badCertificate) cntPerLine=0; continue; } + else if(badCertificate[currBadCertIdx]=='=') + { + /* checksum */ + break; + } else if(badCertificate[currBadCertIdx]==' ') { currBadCertIdx++; @@ -249,24 +270,50 @@ std::string cleanUpCertificate(std::string badCertificate) cleanCertificate += badCertificate[currBadCertIdx]; cntPerLine++; currBadCertIdx++; - } - if(cleanCertificate.substr(cleanCertificate.length()-1,1)!="\n") - { - cleanCertificate += "\n"; -// std::cerr<<"zeeeee"<LoadCertificateFromString(cleancert, gpg_id,error_string); + AuthGPG::getAuthGPG()->LoadCertificateFromString(cert, gpg_id,error_string); AuthGPG::getAuthGPG()->getGPGDetails(gpg_id, pd); - if (gpg_id == "") { + if (gpg_id.empty()) { return false; } + } else { + return false; } #ifdef P3PEERS_DEBUG @@ -990,11 +1005,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai #endif //let's parse the ssl id - parsePosition = certstr.find(CERT_SSL_ID); + size_t parsePosition = peerInfo.find(CERT_SSL_ID); std::cerr << "sslid position : " << parsePosition << std::endl; if (parsePosition != std::string::npos) { parsePosition += CERT_SSL_ID.length(); - std::string subCert = certstr.substr(parsePosition); + std::string subCert = peerInfo.substr(parsePosition); parsePosition = subCert.find(";"); if (parsePosition != std::string::npos) { std::string ssl_id = subCert.substr(0, parsePosition); @@ -1005,11 +1020,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai } //let's parse the location - parsePosition = certstr.find(CERT_LOCATION); + parsePosition = peerInfo.find(CERT_LOCATION); std::cerr << "location position : " << parsePosition << std::endl; if (parsePosition != std::string::npos) { parsePosition += CERT_LOCATION.length(); - std::string subCert = certstr.substr(parsePosition); + std::string subCert = peerInfo.substr(parsePosition); parsePosition = subCert.find(";"); if (parsePosition != std::string::npos) { std::string location = subCert.substr(0, parsePosition); @@ -1019,11 +1034,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai } //let's parse ip local address - parsePosition = certstr.find(CERT_LOCAL_IP); + parsePosition = peerInfo.find(CERT_LOCAL_IP); std::cerr << "local ip position : " << parsePosition << std::endl; if (parsePosition != std::string::npos) { parsePosition += CERT_LOCAL_IP.length(); - std::string subCert = certstr.substr(parsePosition); + std::string subCert = peerInfo.substr(parsePosition); parsePosition = subCert.find(":"); if (parsePosition != std::string::npos) { std::string local_ip = subCert.substr(0, parsePosition); @@ -1045,11 +1060,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai } //let's parse ip ext address - parsePosition = certstr.find(CERT_EXT_IP); + parsePosition = peerInfo.find(CERT_EXT_IP); std::cerr << "Ext ip position : " << parsePosition << std::endl; if (parsePosition != std::string::npos) { parsePosition = parsePosition + CERT_EXT_IP.length(); - std::string subCert = certstr.substr(parsePosition); + std::string subCert = peerInfo.substr(parsePosition); parsePosition = subCert.find(":"); if (parsePosition != std::string::npos) { std::string ext_ip = subCert.substr(0, parsePosition); @@ -1071,11 +1086,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai } //let's parse DynDNS - parsePosition = certstr.find(CERT_DYNDNS); + parsePosition = peerInfo.find(CERT_DYNDNS); std::cerr << "location DynDNS : " << parsePosition << std::endl; if (parsePosition != std::string::npos) { parsePosition += CERT_DYNDNS.length(); - std::string subCert = certstr.substr(parsePosition); + std::string subCert = peerInfo.substr(parsePosition); parsePosition = subCert.find(";"); if (parsePosition != std::string::npos) { std::string DynDNS = subCert.substr(0, parsePosition); @@ -1095,9 +1110,26 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai } } +bool p3Peers::cleanCertificate(const std::string &certstr, std::string &cleanCert) +{ + std::string cert; + std::string peerInfo; + if (splitCert(certstr, cert, peerInfo)) { + cleanCert = cleanUpCertificate(cert); + if (!cleanCert.empty()) { + if (!peerInfo.empty()) { + if (*cleanCert.rbegin() != '\n') { + cleanCert += "\n"; + } + cleanCert += peerInfo; + } + return true; + } + } - + return false; +} bool p3Peers::saveCertificateToFile(const std::string &id, const std::string &fname) { diff --git a/libretroshare/src/rsserver/p3peers.h b/libretroshare/src/rsserver/p3peers.h index 486a0469f..435b58d79 100644 --- a/libretroshare/src/rsserver/p3peers.h +++ b/libretroshare/src/rsserver/p3peers.h @@ -94,7 +94,8 @@ virtual std::string GetRetroshareInvite(const std::string& ssl_id); virtual std::string GetRetroshareInvite(); virtual bool loadCertificateFromFile(const std::string &fname, std::string &id, std::string &gpg_id); -virtual bool loadDetailsFromStringCert(const std::string &cert, RsPeerDetails &pd,std::string& error_string); +virtual bool loadDetailsFromStringCert(const std::string &cert, RsPeerDetails &pd, std::string& error_string); +virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert); virtual bool saveCertificateToFile(const std::string &id, const std::string &fname); virtual std::string saveCertificateToString(const std::string &id); diff --git a/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp b/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp index 18d07f4bc..d89c8893f 100755 --- a/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp +++ b/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp @@ -228,11 +228,9 @@ TextPage::TextPage(QWidget *parent) std::string invite = rsPeers->GetRetroshareInvite(); userCertEdit->setReadOnly(true); - userCertEdit->setMinimumHeight(200); - userCertEdit->setMinimumWidth(530); QFont font("Courier New",10,50,false); - font.setStyleHint(QFont::TypeWriter,QFont::PreferMatch); - font.setStyle(QFont::StyleNormal); + font.setStyleHint(QFont::TypeWriter,QFont::PreferMatch); + font.setStyle(QFont::StyleNormal); userCertEdit->setFont(font); userCertEdit->setText(QString::fromStdString(invite)); @@ -292,12 +290,27 @@ TextPage::TextPage(QWidget *parent) //font.setWeight(75); friendCertEdit->setFont(font); + friendCertCleanButton = new QPushButton; + friendCertCleanButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + friendCertCleanButton->setFixedSize(20,20); + friendCertCleanButton->setFlat(true); + friendCertCleanButton->setIcon( QIcon(":images/accepted16.png") ); + friendCertCleanButton->setToolTip(tr("Clean certificate")); + connect (friendCertCleanButton, SIGNAL(clicked()), this, SLOT(cleanFriendCert())); + + friendCertButtonsLayout = new QVBoxLayout(); + friendCertButtonsLayout->addWidget(friendCertCleanButton); + + friendCertLayout = new QHBoxLayout(); + friendCertLayout->addWidget(friendCertEdit); + friendCertLayout->addLayout(friendCertButtonsLayout); + //=== add all widgets to one layout textPageLayout = new QVBoxLayout(); textPageLayout->addWidget(userCertLabel); textPageLayout->addLayout(userCertLayout); textPageLayout->addWidget(friendCertLabel); - textPageLayout->addWidget(friendCertEdit); + textPageLayout->addLayout(friendCertLayout); // setLayout(textPageLayout); } @@ -329,6 +342,16 @@ TextPage::runEmailClient() sendMail ("", tr("RetroShare Invite").toStdString(), userCertEdit->toPlainText().toStdString()); } +void TextPage::cleanFriendCert() +{ + std::string cert = friendCertEdit->toPlainText().toStdString(); + std::string cleanCert; + + if (rsPeers->cleanCertificate(cert, cleanCert)) { + friendCertEdit->setText(QString::fromStdString(cleanCert)); + } +} + // //============================================================================ // diff --git a/retroshare-gui/src/gui/connect/ConnectFriendWizard.h b/retroshare-gui/src/gui/connect/ConnectFriendWizard.h index 33356e7e4..a45461433 100755 --- a/retroshare-gui/src/gui/connect/ConnectFriendWizard.h +++ b/retroshare-gui/src/gui/connect/ConnectFriendWizard.h @@ -89,7 +89,10 @@ private: //! launches default email client QLabel* friendCertLabel; QTextEdit* friendCertEdit; - + QPushButton* friendCertCleanButton; + QVBoxLayout* friendCertButtonsLayout; + QHBoxLayout* friendCertLayout; + QVBoxLayout* textPageLayout; void setCurrentFileName(const QString &fileName); @@ -99,13 +102,13 @@ private: private slots: void showHelpUserCert(); void copyCert(); - + void cleanFriendCert(); + bool fileSave(); bool fileSaveAs(); //! launches default email client (on windows) - //! Tested on Vista, it work normally... But a bit slowly. void runEmailClient(); }; @@ -130,7 +133,7 @@ private: QLabel *userFileLabel; QPushButton* userFileCreateButton; QHBoxLayout* userFileLayout; - + QLabel* friendFileLabel; QLineEdit *friendFileNameEdit; QPushButton* friendFileNameOpenButton;