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
@ -213,16 +214,31 @@ std::string cleanUpCertificate(std::string badCertificate)
currBadCertIdx++;
}
//keep the first line : gnupg version
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 += 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,8 +270,34 @@ std::string cleanUpCertificate(std::string badCertificate)
cleanCertificate += badCertificate[currBadCertIdx];
cntPerLine++;
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")
{
cleanCertificate += "\n";
@ -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.
*/
@ -303,6 +350,7 @@ std::string cleanUpCertificate(std::string badCertificate)
return cleanCertificate;
}
#ifdef UNUSED_CODE
int findEndIdxOfCertStartTag(std::string badCertificate)
{
size_t idxTag1=0;
@ -331,10 +379,7 @@ int findEndIdxOfCertStartTag(std::string badCertificate)
{
break;
}
}
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,8 +228,6 @@ 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);
@ -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,6 +89,9 @@ private:
//! launches default email client
QLabel* friendCertLabel;
QTextEdit* friendCertEdit;
QPushButton* friendCertCleanButton;
QVBoxLayout* friendCertButtonsLayout;
QHBoxLayout* friendCertLayout;
QVBoxLayout* textPageLayout;
@ -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();
};