|
|
|
@ -136,7 +136,7 @@ RsCertificate::RsCertificate(const std::string& str)
|
|
|
|
|
{
|
|
|
|
|
uint32_t err_code ;
|
|
|
|
|
|
|
|
|
|
if(!initFromString(str,err_code)) // && !initFromString_oldFormat(str,err_code))
|
|
|
|
|
if(!initFromString(str,err_code))
|
|
|
|
|
throw err_code ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -435,12 +435,6 @@ unsigned short RsCertificate::loc_port_us() const
|
|
|
|
|
|
|
|
|
|
bool RsCertificate::cleanCertificate(const std::string& input,std::string& output,Format& format,int& error_code)
|
|
|
|
|
{
|
|
|
|
|
// if(cleanCertificate_oldFormat(input,output,error_code))
|
|
|
|
|
// {
|
|
|
|
|
// format = RS_CERTIFICATE_OLD_FORMAT ;
|
|
|
|
|
// return true ;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if(cleanCertificate(input,output,error_code))
|
|
|
|
|
{
|
|
|
|
|
format = RS_CERTIFICATE_RADIX ;
|
|
|
|
@ -494,576 +488,7 @@ bool RsCertificate::cleanCertificate(const std::string& instr,std::string& str,i
|
|
|
|
|
return true ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All the code below should be removed when in 0.6. Certificates will only use the new format.
|
|
|
|
|
//
|
|
|
|
|
bool RsCertificate::cleanCertificate_oldFormat(const std::string& certstr,std::string& cleanCertificate,int& error_code)
|
|
|
|
|
{
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_UNKOWN_ERROR ; // default
|
|
|
|
|
const std::string& badCertificate(certstr) ;
|
|
|
|
|
|
|
|
|
|
std::string pgpend("-----END PGP PUBLIC KEY BLOCK-----");
|
|
|
|
|
|
|
|
|
|
size_t pos = certstr.find(pgpend);
|
|
|
|
|
std::string peer_info ;
|
|
|
|
|
std::string cert ;
|
|
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
pos += pgpend.length();
|
|
|
|
|
cert = certstr.substr(0, pos);
|
|
|
|
|
if (pos + 1 < certstr.length())
|
|
|
|
|
peer_info = certstr.substr(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_NO_END_TAG ;
|
|
|
|
|
return false ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(cert.empty())
|
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Buffer for storing the cleaned certificate. In certain cases the
|
|
|
|
|
cleanCertificate can be larger than the badCertificate
|
|
|
|
|
*/
|
|
|
|
|
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=certstr.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=certstr.find(commonTag,tmpIdx))!=std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
beginCertStartIdx2=certstr.find(beginTag,beginCertStartIdx1+strlen(commonTag));
|
|
|
|
|
tmpIdx=beginCertStartIdx1+strlen(commonTag);
|
|
|
|
|
if(beginCertStartIdx2!=std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
found=true;
|
|
|
|
|
for(size_t i=beginCertStartIdx1+strlen(commonTag);i<beginCertStartIdx2;i++)
|
|
|
|
|
{
|
|
|
|
|
if(certstr[i]!=' ' && certstr[i]!='\n' )
|
|
|
|
|
{
|
|
|
|
|
found=false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
begin tag not found
|
|
|
|
|
*/
|
|
|
|
|
if(!found)
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"Certificate corrupted beyond repair: No <------BEGIN > tag"<<std::endl;
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_NO_BEGIN_TAG ;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
beginCertEndIdx=certstr.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 false;
|
|
|
|
|
}
|
|
|
|
|
tmpIdx=beginCertEndIdx+strlen(commonTag);
|
|
|
|
|
found=false;
|
|
|
|
|
/*
|
|
|
|
|
Calculating the value of the endCertStartIdx1 and endCertStartIdx2. Here we first locate the occurance of ----- and then
|
|
|
|
|
the location of END. 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 END), other than space and new line then it means that it is the certificate end tag.
|
|
|
|
|
Here we take care of the fact that we may have introduced some spaces and newlines in the end tag by mistake. This
|
|
|
|
|
takes care of the spaces and newlines between ----- and END.
|
|
|
|
|
*/
|
|
|
|
|
while(found==false && (endCertStartIdx1=certstr.find(commonTag,tmpIdx))!=std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
endCertStartIdx2=certstr.find(endTag,endCertStartIdx1+strlen(commonTag));
|
|
|
|
|
tmpIdx=endCertStartIdx1+strlen(commonTag);
|
|
|
|
|
if(endCertStartIdx2!=std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
found=true;
|
|
|
|
|
for(size_t i=endCertStartIdx1+strlen(commonTag);i<endCertStartIdx2;i++)
|
|
|
|
|
{
|
|
|
|
|
if(certstr[i]!=' '&& certstr[i]!='\n')
|
|
|
|
|
{
|
|
|
|
|
found=false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
end tag not found
|
|
|
|
|
*/
|
|
|
|
|
if(!found)
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"Certificate corrupted beyond repair: No <------END > tag"<<std::endl;
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_NO_END_TAG ;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
endCertEndIdx=certstr.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 false;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Copying the begin tag(-----BEGIN) to the clean certificate
|
|
|
|
|
*/
|
|
|
|
|
cleanCertificate += beginCertTag;
|
|
|
|
|
currBadCertIdx=beginCertStartIdx2+strlen(beginTag);
|
|
|
|
|
/*
|
|
|
|
|
Copying the name of the tag e.g XPGP CERTIFICATE. At the same time remove any white spaces and new line
|
|
|
|
|
characters.
|
|
|
|
|
*/
|
|
|
|
|
while(currBadCertIdx<beginCertEndIdx)
|
|
|
|
|
{
|
|
|
|
|
if(badCertificate[currBadCertIdx]=='\n')
|
|
|
|
|
{
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
else if(badCertificate[currBadCertIdx]==' ' && (badCertificate[currBadCertIdx-1]==' '|| badCertificate[currBadCertIdx-1]=='\n') )
|
|
|
|
|
{
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate += badCertificate[currBadCertIdx];
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
If the last character is a space we need to remove it.
|
|
|
|
|
*/
|
|
|
|
|
if(cleanCertificate.substr(cleanCertificate.length()-1, 1) == " ")
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate.erase(cleanCertificate.length()-1);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Copying the end part of the certificate start tag(-----).
|
|
|
|
|
*/
|
|
|
|
|
cleanCertificate += commonTag;
|
|
|
|
|
cleanCertificate += "\n";
|
|
|
|
|
currBadCertIdx=currBadCertIdx+strlen(commonTag);
|
|
|
|
|
/*
|
|
|
|
|
Remove the white spaces between the end of the certificate begin tag and the actual
|
|
|
|
|
start of the certificate.
|
|
|
|
|
*/
|
|
|
|
|
while(badCertificate[currBadCertIdx]=='\n'|| 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(currBadCertIdx<endCertStartIdx1 && 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
|
|
|
|
|
//new cleaned certificate
|
|
|
|
|
int cntPerLine=0;
|
|
|
|
|
while(currBadCertIdx<endCertStartIdx1)
|
|
|
|
|
{
|
|
|
|
|
if(cntPerLine==64)
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate += "\n";
|
|
|
|
|
cntPerLine=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(badCertificate[currBadCertIdx]=='=') /* checksum */
|
|
|
|
|
break;
|
|
|
|
|
else if(badCertificate[currBadCertIdx]=='\t')
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
else if(badCertificate[currBadCertIdx]==' ')
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
else if(badCertificate[currBadCertIdx]=='\n')
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
else if(is_acceptable_radix64Char(badCertificate[currBadCertIdx]))
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate += badCertificate[currBadCertIdx];
|
|
|
|
|
cntPerLine++;
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Warning: Invalid character in radix certificate encoding: " << badCertificate[currBadCertIdx] << std::endl;
|
|
|
|
|
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 false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(currBadCertIdx < endCertStartIdx1 && (badCertificate[currBadCertIdx] == '=' || badCertificate[currBadCertIdx] == ' ' || badCertificate[currBadCertIdx] == '\n' ))
|
|
|
|
|
currBadCertIdx++ ;
|
|
|
|
|
|
|
|
|
|
switch(cntPerLine % 4)
|
|
|
|
|
{
|
|
|
|
|
case 0: break ;
|
|
|
|
|
case 1: std::cerr<<"Certificate corrupted beyond repair: wrongnumber of chars on last line (n%4=1)"<<std::endl;
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_WRONG_NUMBER;
|
|
|
|
|
return false ;
|
|
|
|
|
case 2: cleanCertificate += "==" ;
|
|
|
|
|
break ;
|
|
|
|
|
case 3: cleanCertificate += "=" ;
|
|
|
|
|
break ;
|
|
|
|
|
}
|
|
|
|
|
cleanCertificate += "\n=";
|
|
|
|
|
|
|
|
|
|
// if (badCertificate[currBadCertIdx] == '=')
|
|
|
|
|
// {
|
|
|
|
|
/* checksum */
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
while(currBadCertIdx<endCertEndIdx)
|
|
|
|
|
{
|
|
|
|
|
if(badCertificate[currBadCertIdx]=='\n')
|
|
|
|
|
{
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
else if( badCertificate[currBadCertIdx]==' ' && (badCertificate[currBadCertIdx-1]==' '|| badCertificate[currBadCertIdx-1]=='\n'))
|
|
|
|
|
{
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate += badCertificate[currBadCertIdx];
|
|
|
|
|
currBadCertIdx++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
If the last character is a space we need to remove it.
|
|
|
|
|
*/
|
|
|
|
|
if(cleanCertificate.substr(cleanCertificate.length()-1,1)==" ")
|
|
|
|
|
{
|
|
|
|
|
cleanCertificate.erase(cleanCertificate.length()-1);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Copying the end part(-----) of the end tag in the certificate.
|
|
|
|
|
*/
|
|
|
|
|
cleanCertificate += commonTag;
|
|
|
|
|
cleanCertificate += "\n";
|
|
|
|
|
|
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_NO_ERROR ;
|
|
|
|
|
|
|
|
|
|
cleanCertificate += peer_info ;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string RsCertificate::toStdString_oldFormat() const
|
|
|
|
|
{
|
|
|
|
|
return std::string() ;
|
|
|
|
|
|
|
|
|
|
// not supported anymore.
|
|
|
|
|
//
|
|
|
|
|
std::string res ;
|
|
|
|
|
|
|
|
|
|
res += PGPKeyManagement::makeArmouredKey(binary_pgp_key,binary_pgp_key_size,pgp_version) ;
|
|
|
|
|
|
|
|
|
|
if(only_pgp)
|
|
|
|
|
return res ;
|
|
|
|
|
|
|
|
|
|
res += SSLID_BEGIN_SECTION ;
|
|
|
|
|
res += location_id.toStdString(false) ;
|
|
|
|
|
res += ";" ;
|
|
|
|
|
res += LOCATION_BEGIN_SECTION ;
|
|
|
|
|
res += location_name ;
|
|
|
|
|
res += ";\n" ;
|
|
|
|
|
|
|
|
|
|
if (hidden_node)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream os ;
|
|
|
|
|
os << HIDDEN_NODE_BEGIN_SECTION;
|
|
|
|
|
os << hidden_node_address << ";";
|
|
|
|
|
|
|
|
|
|
res += os.str() ;
|
|
|
|
|
res += "\n" ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream os ;
|
|
|
|
|
os << LOCAL_IP_BEGIN_SECTION ;
|
|
|
|
|
os << (int)ipv4_internal_ip_and_port[0] << "." << (int)ipv4_internal_ip_and_port[1] << "." << (int)ipv4_internal_ip_and_port[2] << "." << (int)ipv4_internal_ip_and_port[3] ;
|
|
|
|
|
os << ":" ;
|
|
|
|
|
os << ipv4_internal_ip_and_port[4]*256+ipv4_internal_ip_and_port[5] ;
|
|
|
|
|
os << ";" ;
|
|
|
|
|
|
|
|
|
|
os << EXTERNAL_IP_BEGIN_SECTION ;
|
|
|
|
|
os << (int)ipv4_external_ip_and_port[0] << "." << (int)ipv4_external_ip_and_port[1] << "." << (int)ipv4_external_ip_and_port[2] << "." << (int)ipv4_external_ip_and_port[3] ;
|
|
|
|
|
os << ":" ;
|
|
|
|
|
os << ipv4_external_ip_and_port[4]*256+ipv4_external_ip_and_port[5] ;
|
|
|
|
|
os << ";" ;
|
|
|
|
|
|
|
|
|
|
res += os.str() ;
|
|
|
|
|
res += "\n" ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RsCertificate::initFromString_oldFormat(const std::string& certstr,uint32_t& /*err_code*/)
|
|
|
|
|
{
|
|
|
|
|
return false ; // this format is not supported anymore.
|
|
|
|
|
|
|
|
|
|
//parse the text to get ip address
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const std::string CERT_SSL_ID = "--SSLID--";
|
|
|
|
|
const std::string CERT_LOCATION = "--LOCATION--";
|
|
|
|
|
const std::string CERT_LOCAL_IP = "--LOCAL--";
|
|
|
|
|
const std::string CERT_EXT_IP = "--EXT--";
|
|
|
|
|
const std::string CERT_DYNDNS = "--DYNDNS--";
|
|
|
|
|
|
|
|
|
|
std::string cert;
|
|
|
|
|
std::string peerInfo;
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
if (pos + 1 < certstr.length())
|
|
|
|
|
peerInfo = certstr.substr(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(cert.empty())
|
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
|
|
// find radix 64 part.
|
|
|
|
|
|
|
|
|
|
std::string radix_cert = PGPKeyParser::extractRadixPartFromArmouredKey(certstr,pgp_version) ;
|
|
|
|
|
|
|
|
|
|
char *key_bin ;
|
|
|
|
|
Radix64::decode(radix_cert,key_bin,binary_pgp_key_size) ;
|
|
|
|
|
|
|
|
|
|
binary_pgp_key = (unsigned char *)key_bin ;
|
|
|
|
|
only_pgp = true ;
|
|
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
|
std::cerr << "Parsing cert for sslid, location, ext and local address details. : " << certstr << std::endl;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//let's parse the 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 = peerInfo.substr(parsePosition);
|
|
|
|
|
parsePosition = subCert.find(";");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string ssl_id = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "SSL id : " << ssl_id << std::endl;
|
|
|
|
|
|
|
|
|
|
location_id = SSLIdType(ssl_id) ;
|
|
|
|
|
only_pgp = false ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//let's parse the 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 = peerInfo.substr(parsePosition);
|
|
|
|
|
parsePosition = subCert.find(";");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string location = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "location : " << location << std::endl;
|
|
|
|
|
|
|
|
|
|
location_name = location;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//let's parse ip local address
|
|
|
|
|
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 = peerInfo.substr(parsePosition);
|
|
|
|
|
parsePosition = subCert.find(":");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string local_ip = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "Local Ip : " << local_ip << std::endl;
|
|
|
|
|
|
|
|
|
|
unsigned short localPort ;
|
|
|
|
|
|
|
|
|
|
//let's parse local port
|
|
|
|
|
subCert = subCert.substr(parsePosition + 1);
|
|
|
|
|
parsePosition = subCert.find(";");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string local_port = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "Local port : " << local_port << std::endl;
|
|
|
|
|
sscanf(local_port.c_str(), "%hu", &localPort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scan_ip(local_ip,localPort,ipv4_internal_ip_and_port) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//let's parse ip ext address
|
|
|
|
|
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 = peerInfo.substr(parsePosition);
|
|
|
|
|
parsePosition = subCert.find(":");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string ext_ip = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "Ext Ip : " << ext_ip << std::endl;
|
|
|
|
|
|
|
|
|
|
unsigned short extPort ;
|
|
|
|
|
//let's parse ext port
|
|
|
|
|
subCert = subCert.substr(parsePosition + 1);
|
|
|
|
|
parsePosition = subCert.find(";");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string ext_port = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "Ext port : " << ext_port << std::endl;
|
|
|
|
|
sscanf(ext_port.c_str(), "%hu", &extPort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scan_ip(ext_ip,extPort,ipv4_external_ip_and_port) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//let's parse 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 = peerInfo.substr(parsePosition);
|
|
|
|
|
parsePosition = subCert.find(";");
|
|
|
|
|
if (parsePosition != std::string::npos) {
|
|
|
|
|
std::string DynDNS = subCert.substr(0, parsePosition);
|
|
|
|
|
std::cerr << "DynDNS : " << DynDNS << std::endl;
|
|
|
|
|
|
|
|
|
|
dns_name = DynDNS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "ConnectFriendWizard : Parse ip address error." << std::endl;
|
|
|
|
|
return false ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|