/* * 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 #include //strlen #include #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(const std::string& badCertificate,int& error_code) { error_code = RS_PEER_CERT_CLEANING_CODE_UNKOWN_ERROR ; // default /* Buffer for storing the cleaned certificate. In certain cases the cleanCertificate can be larger than the badCertificate */ std::string cleanCertificate; //The entire certificate begin tag const char * beginCertTag="-----BEGIN"; //The entire certificate end tag const char * endCertTag="-----END"; //Tag containing dots. The common part of both start and end tags const char * commonTag="-----"; //Only BEGIN part of the begin tag const char * beginTag="BEGIN"; //Only END part of the end tag const char * endTag="END"; //The start index of the ----- part of the certificate begin tag size_t beginCertStartIdx1=0; //The start index of the BEGIN part of the certificate begin tag 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; //The start index of the ----- part of the certificate end tag size_t endCertStartIdx1=0; //The start index of the END part of the certificate end tag 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; //The length of the bad certificate. size_t lengthOfCert=badCertificate.length(); //The current index value in the bad certificate size_t currBadCertIdx=0; //Temporary index value size_t 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(size_t i=beginCertStartIdx1+strlen(commonTag);i tag"< tag"< tag"<=lengthOfCert) { std::cerr<<"Certificate corrupted beyond repair: No <------END > tag"< 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(currBadCertIdx=endCertStartIdx1) { std::cerr<<"Certificate corrupted beyond repair: No checksum, or no newline after first tag"<