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 <iostream>
#include <string.h> //strlen
#include <list>
/*
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<beginCertStartIdx2;i++)
for(size_t i=beginCertStartIdx1+strlen(commonTag);i<beginCertStartIdx2;i++)
{
if(badCertificate[i]!=' ' && badCertificate[i]!='\n' )
{
@ -137,7 +138,7 @@ std::string cleanUpCertificate(std::string badCertificate)
if(endCertStartIdx2!=std::string::npos)
{
found=true;
for(size_t i=endCertStartIdx1+strlen(commonTag);i<endCertStartIdx2;i++)
for(size_t i=endCertStartIdx1+strlen(commonTag);i<endCertStartIdx2;i++)
{
if(badCertificate[i]!=' '&& badCertificate[i]!='\n')
{
@ -211,18 +212,33 @@ std::string cleanUpCertificate(std::string badCertificate)
while(badCertificate[currBadCertIdx]=='\n'|| badCertificate[currBadCertIdx]==' ')
{
currBadCertIdx++;
}
}
//keep the first line : gnupg version
cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++;
while(badCertificate[currBadCertIdx]!='\n')
{
cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++;
}
cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++;
//keep the armor header
std::list<std::string> header;
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')
{
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"<<std::endl;
}
else
{
// std::cerr<<"zooooo"<<std::endl;
}
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")
{
cleanCertificate += "\n";
// std::cerr<<"zeeeee"<<std::endl;
}
else
{
// std::cerr<<"zooooo"<<std::endl;
}
/*
Copying the begining part of the certificate end tag. Copying
-----END part of the tag.
*/
cleanCertificate += endCertTag;
currBadCertIdx=endCertStartIdx2+strlen(endTag);
/*
/*
Copying the name of the certificate e.g XPGP CERTIFICATE. The end tag also has the
the name of the tag.
*/
@ -284,9 +331,9 @@ std::string cleanUpCertificate(std::string badCertificate)
{
cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++;
}
}
/*
If the last character is a space we need to remove it.
*/
@ -300,25 +347,26 @@ std::string cleanUpCertificate(std::string badCertificate)
cleanCertificate += commonTag;
cleanCertificate += "\n";
return cleanCertificate;
return cleanCertificate;
}
#ifdef UNUSED_CODE
int findEndIdxOfCertStartTag(std::string badCertificate)
{
size_t idxTag1=0;
size_t tmpIdx=0;
size_t idxTag2=0;
const char * tag1="---";
const char * tag2="---";
size_t idxTag1=0;
size_t tmpIdx=0;
size_t idxTag2=0;
const char * tag1="---";
const char * tag2="---";
bool found=false;
while(found==false && (idxTag1=badCertificate.find(tag1,tmpIdx))!=std::string::npos)
{
idxTag2=badCertificate.find(tag2,idxTag1+strlen(tag1));
if(idxTag2!=std::string::npos)
{
found=true;
for(size_t i=idxTag1+strlen(tag1);i<idxTag2;i++)
for(size_t i=idxTag1+strlen(tag1);i<idxTag2;i++)
{
if(badCertificate[i]!=' ')
{
@ -331,10 +379,7 @@ int findEndIdxOfCertStartTag(std::string badCertificate)
{
break;
}
}
return 1;
return 1;
}
#endif

View File

@ -38,13 +38,16 @@
//! ... text stuff .....
//!
//!-----BEGIN XPGP CERTIFICATE-----
//!Version: ...
//!
//!MIICxQIBADCCAUkCAQAwHhcNMDkwMjI4MTgzODIyWhcNMTQwMjI3MTgzODIyWjCC
//! ...more ines here...
//!mEuhG8UmDIzC1jeTu8rTMnO+DO3FH/cek1vlfFl4t9g/xktG9U4SPLg=
//!=checksum
//!-----END XPGP CERTIFICATE-----
//!
//! 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
std::string cleanUpCertificate(std::string badCertificate);
std::string cleanUpCertificate(const std::string& badCertificate);
#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 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 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;
//}
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)
{
@ -966,23 +983,21 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
std::cerr << std::endl;
#endif
/* search for -----END CERTIFICATE----- */
std::string pgpend("-----END PGP PUBLIC KEY BLOCK-----");
//parse the text to get ip address
try {
size_t parsePosition = certstr.find(pgpend);
std::string cert;
std::string peerInfo;
if (parsePosition != std::string::npos) {
parsePosition += pgpend.length();
std::string pgpCert = certstr.substr(0, parsePosition);
if (splitCert(certstr, cert, peerInfo)) {
std::string gpg_id;
std::string cleancert = cleanUpCertificate(pgpCert);
AuthGPG::getAuthGPG()->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)
{

View File

@ -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);

View File

@ -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));
}
}
//
//============================================================================
//

View File

@ -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;