mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
corrected a few bugs in the cert cleaning method. Added feedback to the GUI and error codes for the cert cleaning function of rspeers
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4580 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
3ff69a1be5
commit
8941aa5991
6 changed files with 81 additions and 32 deletions
|
@ -27,7 +27,7 @@
|
|||
#include <iostream>
|
||||
#include <string.h> //strlen
|
||||
#include <list>
|
||||
|
||||
#include <retroshare/rspeers.h>
|
||||
/*
|
||||
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
|
||||
|
@ -41,8 +41,10 @@ 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(const std::string& badCertificate)
|
||||
|
||||
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
|
||||
|
@ -79,11 +81,14 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
//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.
|
||||
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)
|
||||
|
@ -114,12 +119,14 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
if(!found)
|
||||
{
|
||||
std::cerr<<"Certificate corrupted beyond repair: No <------BEGIN > tag"<<std::endl;
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_BEGIN_TAG ;
|
||||
return badCertificate;
|
||||
}
|
||||
beginCertEndIdx=badCertificate.find(commonTag,beginCertStartIdx2);
|
||||
if(beginCertEndIdx==std::string::npos)
|
||||
{
|
||||
std::cerr<<"Certificate corrupted beyond repair: No <------BEGIN > tag"<<std::endl;
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_BEGIN_TAG ;
|
||||
return badCertificate;
|
||||
}
|
||||
tmpIdx=beginCertEndIdx+strlen(commonTag);
|
||||
|
@ -159,12 +166,14 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
if(!found)
|
||||
{
|
||||
std::cerr<<"Certificate corrupted beyond repair: No <------END > tag"<<std::endl;
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_END_TAG ;
|
||||
return badCertificate;
|
||||
}
|
||||
endCertEndIdx=badCertificate.find(commonTag,endCertStartIdx2);
|
||||
if(endCertEndIdx==std::string::npos || endCertEndIdx>=lengthOfCert)
|
||||
{
|
||||
std::cerr<<"Certificate corrupted beyond repair: No <------END > tag"<<std::endl;
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_END_TAG ;
|
||||
return badCertificate;
|
||||
}
|
||||
/*
|
||||
|
@ -228,7 +237,7 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
{
|
||||
cleanCertificate += badCertificate.substr(currBadCertIdx, (*headerIt).length());
|
||||
currBadCertIdx += (*headerIt).length();
|
||||
while(badCertificate[currBadCertIdx]!='\n')
|
||||
while(currBadCertIdx<endCertStartIdx1 && badCertificate[currBadCertIdx]!='\n')
|
||||
{
|
||||
cleanCertificate += badCertificate[currBadCertIdx];
|
||||
currBadCertIdx++;
|
||||
|
@ -250,35 +259,39 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
{
|
||||
cleanCertificate += "\n";
|
||||
cntPerLine=0;
|
||||
continue;
|
||||
}
|
||||
else if(badCertificate[currBadCertIdx]=='=')
|
||||
|
||||
if(badCertificate[currBadCertIdx]=='=') /* checksum */
|
||||
{
|
||||
/* checksum */
|
||||
cntPerLine=0 ;
|
||||
break;
|
||||
}
|
||||
else if(badCertificate[currBadCertIdx]==' ')
|
||||
{
|
||||
currBadCertIdx++;
|
||||
continue;
|
||||
}
|
||||
else if(badCertificate[currBadCertIdx]=='\n')
|
||||
{
|
||||
currBadCertIdx++;
|
||||
continue;
|
||||
else
|
||||
{
|
||||
cleanCertificate += badCertificate[currBadCertIdx];
|
||||
cntPerLine++;
|
||||
currBadCertIdx++;
|
||||
}
|
||||
cleanCertificate += badCertificate[currBadCertIdx];
|
||||
cntPerLine++;
|
||||
currBadCertIdx++;
|
||||
}
|
||||
if(currBadCertIdx>=endCertStartIdx1)
|
||||
{
|
||||
std::cerr<<"Certificate corrupted beyond repair: No checksum, or no newline after first tag"<<std::endl;
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_CHECKSUM ;
|
||||
return badCertificate;
|
||||
}
|
||||
|
||||
if (badCertificate[currBadCertIdx] == '=')
|
||||
{
|
||||
while(currBadCertIdx < endCertStartIdx1 && (badCertificate[currBadCertIdx] == '=' || badCertificate[currBadCertIdx] == ' ' || badCertificate[currBadCertIdx] == '\n' ))
|
||||
currBadCertIdx++ ;
|
||||
|
||||
cleanCertificate += "==\n=";
|
||||
|
||||
// if (badCertificate[currBadCertIdx] == '=')
|
||||
// {
|
||||
/* checksum */
|
||||
if (*cleanCertificate.rbegin() != '\n')
|
||||
{
|
||||
cleanCertificate += "\n";
|
||||
}
|
||||
|
||||
while(currBadCertIdx<endCertStartIdx1)
|
||||
{
|
||||
|
@ -296,7 +309,7 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
cntPerLine++;
|
||||
currBadCertIdx++;
|
||||
}
|
||||
}
|
||||
// }
|
||||
|
||||
if(cleanCertificate.substr(cleanCertificate.length()-1,1)!="\n")
|
||||
{
|
||||
|
@ -347,6 +360,7 @@ std::string cleanUpCertificate(const std::string& badCertificate)
|
|||
cleanCertificate += commonTag;
|
||||
cleanCertificate += "\n";
|
||||
|
||||
error_code = RS_PEER_CERT_CLEANING_CODE_NO_ERROR ;
|
||||
return cleanCertificate;
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,10 @@
|
|||
//!
|
||||
//! 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(const std::string& badCertificate);
|
||||
|
||||
// Error codes (need appropriate message andtranslation in GUI) are listed in rspeers.h
|
||||
//
|
||||
std::string cleanUpCertificate(const std::string& badCertificate,int& error_code);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -70,6 +70,13 @@ const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_UDP = 5;
|
|||
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_TUNNEL = 6;
|
||||
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN = 7;
|
||||
|
||||
/* Error codes for certificate cleaning */
|
||||
const int RS_PEER_CERT_CLEANING_CODE_NO_ERROR = 0x00 ;
|
||||
const int RS_PEER_CERT_CLEANING_CODE_UNKOWN_ERROR = 0x01 ;
|
||||
const int RS_PEER_CERT_CLEANING_CODE_NO_BEGIN_TAG = 0x02 ;
|
||||
const int RS_PEER_CERT_CLEANING_CODE_NO_END_TAG = 0x03 ;
|
||||
const int RS_PEER_CERT_CLEANING_CODE_NO_CHECKSUM = 0x04 ;
|
||||
|
||||
/* Groups */
|
||||
#define RS_GROUP_ID_FRIENDS "Friends"
|
||||
#define RS_GROUP_ID_FAMILY "Family"
|
||||
|
@ -215,7 +222,7 @@ virtual bool hasExportMinimal() = 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 cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code) = 0;
|
||||
virtual bool saveCertificateToFile(const std::string &id, const std::string &fname) = 0;
|
||||
virtual std::string saveCertificateToString(const std::string &id) = 0;
|
||||
|
||||
|
|
|
@ -1285,13 +1285,13 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
|
|||
}
|
||||
}
|
||||
|
||||
bool p3Peers::cleanCertificate(const std::string &certstr, std::string &cleanCert)
|
||||
bool p3Peers::cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code)
|
||||
{
|
||||
std::string cert;
|
||||
std::string peerInfo;
|
||||
|
||||
if (splitCert(certstr, cert, peerInfo)) {
|
||||
cleanCert = cleanUpCertificate(cert);
|
||||
cleanCert = cleanUpCertificate(cert,error_code);
|
||||
if (!cleanCert.empty()) {
|
||||
if (!peerInfo.empty()) {
|
||||
if (*cleanCert.rbegin() != '\n') {
|
||||
|
|
|
@ -99,7 +99,7 @@ virtual bool hasExportMinimal() ;
|
|||
|
||||
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 cleanCertificate(const std::string &certstr, std::string &cleanCert);
|
||||
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code);
|
||||
virtual bool saveCertificateToFile(const std::string &id, const std::string &fname);
|
||||
virtual std::string saveCertificateToString(const std::string &id);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue