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
This commit is contained in:
thunder2 2011-04-08 18:53:12 +00:00
parent 1bef23042f
commit 8f0793f071
7 changed files with 192 additions and 84 deletions

View file

@ -26,6 +26,7 @@
#include "cleanupxpgp.h" #include "cleanupxpgp.h"
#include <iostream> #include <iostream>
#include <string.h> //strlen #include <string.h> //strlen
#include <list>
/* /*
Method for cleaning up the certificate. This method removes any unnecessay white spaces and unnecessary 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 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 Buffer for storing the cleaned certificate. In certain cases the
@ -213,16 +214,31 @@ std::string cleanUpCertificate(std::string badCertificate)
currBadCertIdx++; currBadCertIdx++;
} }
//keep the first line : gnupg version //keep the armor header
cleanCertificate += badCertificate[currBadCertIdx]; std::list<std::string> header;
currBadCertIdx++; header.push_back("Version");
header.push_back("Comment");
header.push_back("MessageID");
header.push_back("Hash");
header.push_back("Charset");
for (std::list<std::string>::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') while(badCertificate[currBadCertIdx]!='\n')
{ {
cleanCertificate += badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++; currBadCertIdx++;
} }
cleanCertificate += badCertificate[currBadCertIdx]; cleanCertificate += "\n";
currBadCertIdx++; }
}
//add empty line after armor header
cleanCertificate += "\n";
//Start of the actual certificate. Remove spaces in the certificate //Start of the actual certificate. Remove spaces in the certificate
//and make sure there are 64 characters per line in the //and make sure there are 64 characters per line in the
@ -236,6 +252,11 @@ std::string cleanUpCertificate(std::string badCertificate)
cntPerLine=0; cntPerLine=0;
continue; continue;
} }
else if(badCertificate[currBadCertIdx]=='=')
{
/* checksum */
break;
}
else if(badCertificate[currBadCertIdx]==' ') else if(badCertificate[currBadCertIdx]==' ')
{ {
currBadCertIdx++; currBadCertIdx++;
@ -249,8 +270,34 @@ std::string cleanUpCertificate(std::string badCertificate)
cleanCertificate += badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
cntPerLine++; cntPerLine++;
currBadCertIdx++; currBadCertIdx++;
} }
if (badCertificate[currBadCertIdx] == '=')
{
/* checksum */
if (*cleanCertificate.rbegin() != '\n')
{
cleanCertificate += "\n";
}
while(currBadCertIdx<endCertStartIdx1)
{
if (badCertificate[currBadCertIdx]==' ')
{
currBadCertIdx++;
continue;
}
else if(badCertificate[currBadCertIdx]=='\n')
{
currBadCertIdx++;
continue;
}
cleanCertificate += badCertificate[currBadCertIdx];
cntPerLine++;
currBadCertIdx++;
}
}
if(cleanCertificate.substr(cleanCertificate.length()-1,1)!="\n") if(cleanCertificate.substr(cleanCertificate.length()-1,1)!="\n")
{ {
cleanCertificate += "\n"; cleanCertificate += "\n";
@ -284,9 +331,9 @@ std::string cleanUpCertificate(std::string badCertificate)
{ {
cleanCertificate += badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++; currBadCertIdx++;
}
}
}
}
/* /*
If the last character is a space we need to remove it. If the last character is a space we need to remove it.
*/ */
@ -303,6 +350,7 @@ std::string cleanUpCertificate(std::string badCertificate)
return cleanCertificate; return cleanCertificate;
} }
#ifdef UNUSED_CODE
int findEndIdxOfCertStartTag(std::string badCertificate) int findEndIdxOfCertStartTag(std::string badCertificate)
{ {
size_t idxTag1=0; size_t idxTag1=0;
@ -331,10 +379,7 @@ int findEndIdxOfCertStartTag(std::string badCertificate)
{ {
break; break;
} }
} }
return 1; return 1;
} }
#endif

View file

@ -38,13 +38,16 @@
//! ... text stuff ..... //! ... text stuff .....
//! //!
//!-----BEGIN XPGP CERTIFICATE----- //!-----BEGIN XPGP CERTIFICATE-----
//!Version: ...
//!
//!MIICxQIBADCCAUkCAQAwHhcNMDkwMjI4MTgzODIyWhcNMTQwMjI3MTgzODIyWjCC //!MIICxQIBADCCAUkCAQAwHhcNMDkwMjI4MTgzODIyWhcNMTQwMjI3MTgzODIyWjCC
//! ...more ines here... //! ...more ines here...
//!mEuhG8UmDIzC1jeTu8rTMnO+DO3FH/cek1vlfFl4t9g/xktG9U4SPLg= //!mEuhG8UmDIzC1jeTu8rTMnO+DO3FH/cek1vlfFl4t9g/xktG9U4SPLg=
//!=checksum
//!-----END XPGP CERTIFICATE----- //!-----END XPGP CERTIFICATE-----
//! //!
//! In the newer gui version, users send each other almost clean certificates, //! In the newer gui version, users send each other almost clean certificates,
//! so this functon is used only to avoid possible bugs with line endings //! so this functon is used only to avoid possible bugs with line endings
std::string cleanUpCertificate(std::string badCertificate); std::string cleanUpCertificate(const std::string& badCertificate);
#endif #endif

View file

@ -216,6 +216,7 @@ virtual std::string GetRetroshareInvite() = 0;
virtual bool loadCertificateFromFile(const std::string &fname, std::string &ssl_id, std::string &gpg_id) = 0; virtual bool loadCertificateFromFile(const std::string &fname, std::string &ssl_id, std::string &gpg_id) = 0;
virtual bool loadDetailsFromStringCert(const std::string &certGPG, RsPeerDetails &pd,std::string& error_string) = 0; virtual bool loadDetailsFromStringCert(const std::string &certGPG, RsPeerDetails &pd,std::string& error_string) = 0;
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert) = 0;
virtual bool saveCertificateToFile(const std::string &id, const std::string &fname) = 0; virtual bool saveCertificateToFile(const std::string &id, const std::string &fname) = 0;
virtual std::string saveCertificateToString(const std::string &id) = 0; virtual std::string saveCertificateToString(const std::string &id) = 0;

View file

@ -957,7 +957,24 @@ bool p3Peers::loadCertificateFromFile(const std::string &fname, std::string &id
// return false; // return false;
//} //}
static bool splitCert(const std::string &certstr, std::string &cert, std::string &peerInfo)
{
cert.erase();
peerInfo.erase();
/* search for -----END CERTIFICATE----- */
std::string pgpend("-----END PGP PUBLIC KEY BLOCK-----");
size_t pos = certstr.find(pgpend);
if (pos != std::string::npos) {
pos += pgpend.length();
cert = certstr.substr(0, pos);
peerInfo = certstr.substr(pos + 1);
}
return !cert.empty();
}
bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetails &pd,std::string& error_string) bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetails &pd,std::string& error_string)
{ {
@ -966,23 +983,21 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
/* search for -----END CERTIFICATE----- */
std::string pgpend("-----END PGP PUBLIC KEY BLOCK-----");
//parse the text to get ip address //parse the text to get ip address
try { try {
size_t parsePosition = certstr.find(pgpend); std::string cert;
std::string peerInfo;
if (parsePosition != std::string::npos) { if (splitCert(certstr, cert, peerInfo)) {
parsePosition += pgpend.length();
std::string pgpCert = certstr.substr(0, parsePosition);
std::string gpg_id; std::string gpg_id;
std::string cleancert = cleanUpCertificate(pgpCert); AuthGPG::getAuthGPG()->LoadCertificateFromString(cert, gpg_id,error_string);
AuthGPG::getAuthGPG()->LoadCertificateFromString(cleancert, gpg_id,error_string);
AuthGPG::getAuthGPG()->getGPGDetails(gpg_id, pd); AuthGPG::getAuthGPG()->getGPGDetails(gpg_id, pd);
if (gpg_id == "") { if (gpg_id.empty()) {
return false; return false;
} }
} else {
return false;
} }
#ifdef P3PEERS_DEBUG #ifdef P3PEERS_DEBUG
@ -990,11 +1005,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
#endif #endif
//let's parse the ssl id //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; std::cerr << "sslid position : " << parsePosition << std::endl;
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
parsePosition += CERT_SSL_ID.length(); parsePosition += CERT_SSL_ID.length();
std::string subCert = certstr.substr(parsePosition); std::string subCert = peerInfo.substr(parsePosition);
parsePosition = subCert.find(";"); parsePosition = subCert.find(";");
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
std::string ssl_id = subCert.substr(0, parsePosition); 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 //let's parse the location
parsePosition = certstr.find(CERT_LOCATION); parsePosition = peerInfo.find(CERT_LOCATION);
std::cerr << "location position : " << parsePosition << std::endl; std::cerr << "location position : " << parsePosition << std::endl;
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
parsePosition += CERT_LOCATION.length(); parsePosition += CERT_LOCATION.length();
std::string subCert = certstr.substr(parsePosition); std::string subCert = peerInfo.substr(parsePosition);
parsePosition = subCert.find(";"); parsePosition = subCert.find(";");
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
std::string location = subCert.substr(0, parsePosition); 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 //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; std::cerr << "local ip position : " << parsePosition << std::endl;
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
parsePosition += CERT_LOCAL_IP.length(); parsePosition += CERT_LOCAL_IP.length();
std::string subCert = certstr.substr(parsePosition); std::string subCert = peerInfo.substr(parsePosition);
parsePosition = subCert.find(":"); parsePosition = subCert.find(":");
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
std::string local_ip = subCert.substr(0, parsePosition); 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 //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; std::cerr << "Ext ip position : " << parsePosition << std::endl;
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
parsePosition = parsePosition + CERT_EXT_IP.length(); parsePosition = parsePosition + CERT_EXT_IP.length();
std::string subCert = certstr.substr(parsePosition); std::string subCert = peerInfo.substr(parsePosition);
parsePosition = subCert.find(":"); parsePosition = subCert.find(":");
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
std::string ext_ip = subCert.substr(0, parsePosition); std::string ext_ip = subCert.substr(0, parsePosition);
@ -1071,11 +1086,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
} }
//let's parse DynDNS //let's parse DynDNS
parsePosition = certstr.find(CERT_DYNDNS); parsePosition = peerInfo.find(CERT_DYNDNS);
std::cerr << "location DynDNS : " << parsePosition << std::endl; std::cerr << "location DynDNS : " << parsePosition << std::endl;
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
parsePosition += CERT_DYNDNS.length(); parsePosition += CERT_DYNDNS.length();
std::string subCert = certstr.substr(parsePosition); std::string subCert = peerInfo.substr(parsePosition);
parsePosition = subCert.find(";"); parsePosition = subCert.find(";");
if (parsePosition != std::string::npos) { if (parsePosition != std::string::npos) {
std::string DynDNS = subCert.substr(0, parsePosition); 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) bool p3Peers::saveCertificateToFile(const std::string &id, const std::string &fname)
{ {

View file

@ -95,6 +95,7 @@ virtual std::string GetRetroshareInvite();
virtual bool loadCertificateFromFile(const std::string &fname, std::string &id, std::string &gpg_id); 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 bool saveCertificateToFile(const std::string &id, const std::string &fname);
virtual std::string saveCertificateToString(const std::string &id); virtual std::string saveCertificateToString(const std::string &id);

View file

@ -228,8 +228,6 @@ TextPage::TextPage(QWidget *parent)
std::string invite = rsPeers->GetRetroshareInvite(); std::string invite = rsPeers->GetRetroshareInvite();
userCertEdit->setReadOnly(true); userCertEdit->setReadOnly(true);
userCertEdit->setMinimumHeight(200);
userCertEdit->setMinimumWidth(530);
QFont font("Courier New",10,50,false); QFont font("Courier New",10,50,false);
font.setStyleHint(QFont::TypeWriter,QFont::PreferMatch); font.setStyleHint(QFont::TypeWriter,QFont::PreferMatch);
font.setStyle(QFont::StyleNormal); font.setStyle(QFont::StyleNormal);
@ -292,12 +290,27 @@ TextPage::TextPage(QWidget *parent)
//font.setWeight(75); //font.setWeight(75);
friendCertEdit->setFont(font); 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 //=== add all widgets to one layout
textPageLayout = new QVBoxLayout(); textPageLayout = new QVBoxLayout();
textPageLayout->addWidget(userCertLabel); textPageLayout->addWidget(userCertLabel);
textPageLayout->addLayout(userCertLayout); textPageLayout->addLayout(userCertLayout);
textPageLayout->addWidget(friendCertLabel); textPageLayout->addWidget(friendCertLabel);
textPageLayout->addWidget(friendCertEdit); textPageLayout->addLayout(friendCertLayout);
// //
setLayout(textPageLayout); setLayout(textPageLayout);
} }
@ -329,6 +342,16 @@ TextPage::runEmailClient()
sendMail ("", tr("RetroShare Invite").toStdString(), userCertEdit->toPlainText().toStdString()); 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));
}
}
// //
//============================================================================ //============================================================================
// //

View file

@ -89,6 +89,9 @@ private:
//! launches default email client //! launches default email client
QLabel* friendCertLabel; QLabel* friendCertLabel;
QTextEdit* friendCertEdit; QTextEdit* friendCertEdit;
QPushButton* friendCertCleanButton;
QVBoxLayout* friendCertButtonsLayout;
QHBoxLayout* friendCertLayout;
QVBoxLayout* textPageLayout; QVBoxLayout* textPageLayout;
@ -99,13 +102,13 @@ private:
private slots: private slots:
void showHelpUserCert(); void showHelpUserCert();
void copyCert(); void copyCert();
void cleanFriendCert();
bool fileSave(); bool fileSave();
bool fileSaveAs(); bool fileSaveAs();
//! launches default email client (on windows) //! launches default email client (on windows)
//! Tested on Vista, it work normally... But a bit slowly. //! Tested on Vista, it work normally... But a bit slowly.
void runEmailClient(); void runEmailClient();
}; };