diff --git a/libretroshare/src/pqi/Makefile b/libretroshare/src/pqi/Makefile index 8dcbf653a..f66730ade 100644 --- a/libretroshare/src/pqi/Makefile +++ b/libretroshare/src/pqi/Makefile @@ -16,7 +16,7 @@ MGR_OBJ = pqimonitor.o p3dhtmgr.o p3connmgr.o p3cfgmgr.o p3authmgr.o GRP_OBJ = pqiperson.o pqihandler.o pqiservice.o pqipersongrp.o ifdef PQI_USE_XPGP - SSL_OBJ = authxpgp.o + SSL_OBJ = authxpgp.o cleanupxpgp.o else SSL_OBJ = sslcert.o endif diff --git a/libretroshare/src/pqi/authxpgp.cc b/libretroshare/src/pqi/authxpgp.cc index 63c39e342..009f16de6 100644 --- a/libretroshare/src/pqi/authxpgp.cc +++ b/libretroshare/src/pqi/authxpgp.cc @@ -24,6 +24,7 @@ */ #include "authxpgp.h" +#include "cleanupxpgp.h" #include "pqinetwork.h" @@ -465,7 +466,14 @@ bool AuthXPGP::LoadCertificateFromString(std::string pem, std::string &id) std::cerr << std::endl; #endif - XPGP *xpgp = loadXPGPFromPEM(pem); +#ifdef AUTHXPGP_DEBUG + std::cerr << "AuthXPGP::LoadCertificateFromString() Cleaning up Certificate First!"; + std::cerr << std::endl; +#endif + + std::string cleancert = cleanUpCertificate(pem); + + XPGP *xpgp = loadXPGPFromPEM(cleancert); if (!xpgp) return false; diff --git a/libretroshare/src/pqi/cleanupxpgp.cc b/libretroshare/src/pqi/cleanupxpgp.cc new file mode 100644 index 000000000..e18f3ad9d --- /dev/null +++ b/libretroshare/src/pqi/cleanupxpgp.cc @@ -0,0 +1,364 @@ +/* + * libretroshare/src/pqi: cleanupxpgp.cc + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2008 by Sourashis Roy + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include "cleanupxpgp.h" +#include + + +/* +Method for cleaning up the certificate. This method removes any unnecessay white spaces and unnecessary +new line characters in the certificate. Also it makes sure that there are 64 characters per line in +the certificate. This function removes white spaces and new line characters in the entire segment +-----BEGIN XPGP CERTIFICATE----- + +-----END XPGP CERTIFICATE----- +We also take care of correcting cases like ----- BEGIN. Here extra empty spaces +have been introduced between ----- and BEGIN. Similarly for the +end tag we take care of cases like ----- END XPGP . Here extra empty spaces have been +introduced and the actual tag should have been -----END XPGP +*/ + +std::string cleanUpCertificate(std::string badCertificate) +{ + /* + Buffer for storing the cleaned certificate. In certain cases the + cleanCertificate can be larger than the badCertificate + */ + char * cleanCertificate=new char[badCertificate.length()+100]; + //The entire certificate begin tag + char * beginCertTag="-----BEGIN"; + //The entire certificate end tag + char * endCertTag="-----END"; + //Tag containing dots. The common part of both start and end tags + char * commonTag="-----"; + //Only BEGIN part of the begin tag + char * beginTag="BEGIN"; + //Only END part of the end tag + char * endTag="END"; + //The start index of the ----- part of the certificate begin tag + int beginCertStartIdx1=0; + //The start index of the BEGIN part of the certificate begin tag + int beginCertStartIdx2=0; + //The start index of the end part(-----) of the certificate begin tag. The begin tag ends with -----. Example -----BEGIN XPGP CERTIFICATE----- + int beginCertEndIdx=0; + //The start index of the ----- part of the certificate end tag + int endCertStartIdx1=0; + //The start index of the END part of the certificate end tag + int endCertStartIdx2=0; + //The start index of the end part(-----) of the certificate end tag. The begin tag ends with -----. Example -----BEGIN XPGP CERTIFICATE----- + int endCertEndIdx=0; + //The length of the bad certificate. + int lengthOfCert=badCertificate.length(); + //The current index value in the cleaned certificate. + int currCleanCertIdx=0; + //The current index value in the bad certificate + int currBadCertIdx=0; + //Temporary index value + int tmpIdx=0; + //Boolean flag showing if the begin tag or the end tag has been found + bool found=false; + /* + Calculating the value of the beginCertStartIdx1 and beginCertStartIdx2. Here we first locate the occurance of ----- and then + the location of BEGIN. Next we check if there are any non space or non new-line characters between their occureance. If there are any other + characters between the two(----- and BEGIN), other than space and new line then it means that it is the certificate begin tag. + Here we take care of the fact that we may have introduced some spaces and newlines in the begin tag by mistake. This + takes care of the spaces and newlines between ----- and BEGIN. + */ + + while(found==false && (beginCertStartIdx1=badCertificate.find(commonTag,tmpIdx))!=std::string::npos) + { + beginCertStartIdx2=badCertificate.find(beginTag,beginCertStartIdx1+strlen(commonTag)); + tmpIdx=beginCertStartIdx1+strlen(commonTag); + if(beginCertStartIdx2!=std::string::npos) + { + found=true; + for(int i=beginCertStartIdx1+strlen(commonTag);i tag"< tag"< tag"<=lengthOfCert) + { + std::cerr<<"Certificate corrupted beyond repair: No <------END > tag"< + +std::string cleanUpCertificate(std::string badCertificate); + +#endif diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index f76adb207..5c0a0ec39 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -190,6 +190,7 @@ int pqipersongrp::restart_listener() int pqipersongrp::setConfig(p3GeneralConfig *cfg) { config = cfg; + return 1; } static const std::string pqih_ftr("PQIH_FTR");