2012-08-11 20:43:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-08-13 19:37:50 +00:00
|
|
|
#include <retroshare/rspeers.h>
|
2012-08-11 20:43:10 +00:00
|
|
|
#include <util/radix64.h>
|
|
|
|
#include <pgp/pgpkeyutil.h>
|
|
|
|
#include "rscertificate.h"
|
2014-01-18 05:27:54 +00:00
|
|
|
#include "util/rsstring.h"
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-18 16:53:19 +00:00
|
|
|
#define DEBUG_RSCERTIFICATE
|
2012-08-14 07:34:29 +00:00
|
|
|
|
2012-08-11 20:43:10 +00:00
|
|
|
static const std::string PGP_CERTIFICATE_START ( "-----BEGIN PGP PUBLIC KEY BLOCK-----" );
|
|
|
|
static const std::string PGP_CERTIFICATE_END ( "-----END PGP PUBLIC KEY BLOCK-----" );
|
|
|
|
static const std::string EXTERNAL_IP_BEGIN_SECTION ( "--EXT--" );
|
|
|
|
static const std::string LOCAL_IP_BEGIN_SECTION ( "--LOCAL--" );
|
|
|
|
static const std::string SSLID_BEGIN_SECTION ( "--SSLID--" );
|
|
|
|
static const std::string LOCATION_BEGIN_SECTION ( "--LOCATION--" );
|
2013-09-09 02:10:49 +00:00
|
|
|
static const std::string HIDDEN_NODE_BEGIN_SECTION ( "--HIDDEN--" );
|
2012-08-11 20:43:10 +00:00
|
|
|
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_PGP_SECTION = 0x01 ;
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_EXTIPANDPORT_SECTION = 0x02 ;
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_LOCIPANDPORT_SECTION = 0x03 ;
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_DNS_SECTION = 0x04 ;
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_SSLID_SECTION = 0x05 ;
|
|
|
|
static const uint8_t CERTIFICATE_PTAG_NAME_SECTION = 0x06 ;
|
2012-12-22 21:22:03 +00:00
|
|
|
static const uint8_t CERTIFICATE_PTAG_CHECKSUM_SECTION = 0x07 ;
|
2013-09-09 02:10:49 +00:00
|
|
|
static const uint8_t CERTIFICATE_PTAG_HIDDENNODE_SECTION = 0x08 ;
|
2014-01-18 16:53:19 +00:00
|
|
|
static const uint8_t CERTIFICATE_PTAG_VERSION_SECTION = 0x09 ;
|
|
|
|
|
|
|
|
static const uint8_t CERTIFICATE_VERSION_06 = 0x06 ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2012-08-13 19:37:50 +00:00
|
|
|
static bool is_acceptable_radix64Char(char c)
|
2012-08-11 20:43:10 +00:00
|
|
|
{
|
2012-12-22 21:22:03 +00:00
|
|
|
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '=' ;
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RsCertificate::~RsCertificate()
|
|
|
|
{
|
|
|
|
delete[] binary_pgp_key ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsCertificate::addPacket(uint8_t ptag, const unsigned char *mem, size_t size, unsigned char *& buf, size_t& offset, size_t& buf_size)
|
|
|
|
{
|
|
|
|
// Check that the buffer has sufficient size. If not, increase it.
|
|
|
|
|
|
|
|
while(offset + size + 6 >= buf_size)
|
|
|
|
{
|
|
|
|
unsigned char *newbuf = new unsigned char[2*buf_size] ;
|
|
|
|
|
|
|
|
memcpy(newbuf, buf, buf_size) ;
|
|
|
|
buf_size *= 2 ;
|
|
|
|
|
|
|
|
delete[] buf ;
|
|
|
|
|
|
|
|
buf = newbuf ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write ptag and size
|
|
|
|
|
|
|
|
buf[offset] = ptag ;
|
|
|
|
offset += 1 ;
|
|
|
|
|
|
|
|
offset += PGPKeyParser::write_125Size(&buf[offset],size) ;
|
|
|
|
|
|
|
|
// Copy the data
|
|
|
|
|
|
|
|
memcpy(&buf[offset], mem, size) ;
|
|
|
|
offset += size ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RsCertificate::toStdString() const
|
|
|
|
{
|
|
|
|
std::string res ;
|
|
|
|
size_t BS = 1000 ;
|
|
|
|
size_t p = 0 ;
|
|
|
|
unsigned char *buf = new unsigned char[BS] ;
|
|
|
|
|
2014-01-18 16:53:19 +00:00
|
|
|
addPacket( CERTIFICATE_PTAG_VERSION_SECTION, &CERTIFICATE_VERSION_06 , 1 , buf, p, BS ) ;
|
|
|
|
addPacket( CERTIFICATE_PTAG_PGP_SECTION , binary_pgp_key , binary_pgp_key_size , buf, p, BS ) ;
|
2012-08-15 20:08:18 +00:00
|
|
|
|
|
|
|
if(!only_pgp)
|
|
|
|
{
|
2013-09-09 02:10:49 +00:00
|
|
|
if (hidden_node)
|
|
|
|
{
|
|
|
|
addPacket( CERTIFICATE_PTAG_HIDDENNODE_SECTION, (unsigned char *)hidden_node_address.c_str(), hidden_node_address.length() , buf, p, BS ) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addPacket( CERTIFICATE_PTAG_EXTIPANDPORT_SECTION, ipv4_external_ip_and_port , 6 , buf, p, BS ) ;
|
|
|
|
addPacket( CERTIFICATE_PTAG_LOCIPANDPORT_SECTION, ipv4_internal_ip_and_port , 6 , buf, p, BS ) ;
|
|
|
|
addPacket( CERTIFICATE_PTAG_DNS_SECTION , (unsigned char *)dns_name.c_str() , dns_name.length() , buf, p, BS ) ;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:08:18 +00:00
|
|
|
addPacket( CERTIFICATE_PTAG_NAME_SECTION , (unsigned char *)location_name.c_str() ,location_name.length() , buf, p, BS ) ;
|
|
|
|
addPacket( CERTIFICATE_PTAG_SSLID_SECTION , location_id.toByteArray() ,location_id.SIZE_IN_BYTES, buf, p, BS ) ;
|
|
|
|
}
|
2013-10-02 20:28:42 +00:00
|
|
|
|
2012-12-22 21:22:03 +00:00
|
|
|
uint32_t computed_crc = PGPKeyManagement::compute24bitsCRC(buf,p) ;
|
|
|
|
|
|
|
|
// handle endian issues.
|
|
|
|
unsigned char mem[3] ;
|
|
|
|
mem[0] = computed_crc & 0xff ;
|
|
|
|
mem[1] = (computed_crc >> 8 ) & 0xff ;
|
|
|
|
mem[2] = (computed_crc >> 16) & 0xff ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2012-12-22 21:22:03 +00:00
|
|
|
addPacket( CERTIFICATE_PTAG_CHECKSUM_SECTION,mem,3,buf,p,BS) ;
|
2013-10-02 20:28:42 +00:00
|
|
|
|
2012-08-11 20:43:10 +00:00
|
|
|
std::string out_string ;
|
|
|
|
|
|
|
|
Radix64::encode((char *)buf, p, out_string) ;
|
|
|
|
|
2012-08-13 19:37:50 +00:00
|
|
|
// Now slice up to 64 chars.
|
|
|
|
//
|
|
|
|
std::string out2 ;
|
|
|
|
static const int LINE_LENGTH = 64 ;
|
|
|
|
|
|
|
|
for(int i=0;i<(int)out_string.length();++i)
|
|
|
|
{
|
|
|
|
out2 += out_string[i] ;
|
|
|
|
|
|
|
|
if(i % LINE_LENGTH == LINE_LENGTH-1)
|
|
|
|
out2 += '\n' ;
|
|
|
|
}
|
|
|
|
|
2012-08-11 20:43:10 +00:00
|
|
|
delete[] buf ;
|
2012-08-13 19:37:50 +00:00
|
|
|
return out2 ;
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RsCertificate::RsCertificate(const std::string& str)
|
|
|
|
:
|
|
|
|
location_name(""),
|
2012-08-11 21:16:27 +00:00
|
|
|
pgp_version("Version: OpenPGP:SDK v0.9"),
|
2012-08-15 20:13:19 +00:00
|
|
|
dns_name(""),only_pgp(true)
|
2012-08-11 20:43:10 +00:00
|
|
|
{
|
2012-12-22 21:22:03 +00:00
|
|
|
uint32_t err_code ;
|
2014-01-09 20:20:46 +00:00
|
|
|
binary_pgp_key = NULL ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-19 14:42:16 +00:00
|
|
|
if(!initFromString(str,err_code))
|
2012-12-22 21:22:03 +00:00
|
|
|
throw err_code ;
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 19:37:50 +00:00
|
|
|
RsCertificate::RsCertificate(const RsPeerDetails& Detail, const unsigned char *binary_pgp_block,size_t binary_pgp_block_size)
|
|
|
|
:pgp_version("Version: OpenPGP:SDK v0.9")
|
|
|
|
{
|
|
|
|
if(binary_pgp_block_size == 0 || binary_pgp_block == NULL)
|
|
|
|
throw std::runtime_error("Cannot init a certificate with a void key block.") ;
|
|
|
|
|
|
|
|
binary_pgp_key = new unsigned char[binary_pgp_block_size] ;
|
|
|
|
memcpy(binary_pgp_key,binary_pgp_block,binary_pgp_block_size) ;
|
|
|
|
binary_pgp_key_size = binary_pgp_block_size ;
|
|
|
|
|
2012-08-15 20:08:18 +00:00
|
|
|
if(!Detail.isOnlyGPGdetail)
|
|
|
|
{
|
|
|
|
only_pgp = false ;
|
|
|
|
location_id = SSLIdType( Detail.id ) ;
|
|
|
|
location_name = Detail.location ;
|
2012-08-13 19:37:50 +00:00
|
|
|
|
2013-09-09 02:10:49 +00:00
|
|
|
if (Detail.isHiddenNode)
|
|
|
|
{
|
|
|
|
hidden_node = true;
|
2014-01-18 05:27:54 +00:00
|
|
|
hidden_node_address = Detail.hiddenNodeAddress;
|
|
|
|
rs_sprintf_append(hidden_node_address, ":%u", Detail.hiddenNodePort);
|
2012-08-13 19:37:50 +00:00
|
|
|
|
2013-09-09 02:10:49 +00:00
|
|
|
memset(ipv4_internal_ip_and_port,0,6) ;
|
|
|
|
memset(ipv4_external_ip_and_port,0,6) ;
|
|
|
|
dns_name = "" ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hidden_node = false;
|
|
|
|
hidden_node_address = "";
|
|
|
|
|
2014-01-18 05:42:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
scan_ip(Detail.localAddr,Detail.localPort,ipv4_internal_ip_and_port) ;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "RsCertificate::Invalid LocalAddress";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
memset(ipv4_internal_ip_and_port,0,6) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
scan_ip(Detail.extAddr,Detail.extPort,ipv4_external_ip_and_port) ;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "RsCertificate::Invalid ExternalAddress";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
memset(ipv4_external_ip_and_port,0,6) ;
|
|
|
|
}
|
2012-08-13 19:37:50 +00:00
|
|
|
|
2013-09-09 02:10:49 +00:00
|
|
|
dns_name = Detail.dyndns ;
|
|
|
|
}
|
2012-08-15 20:08:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
only_pgp = true ;
|
2013-09-09 02:10:49 +00:00
|
|
|
hidden_node = false;
|
|
|
|
hidden_node_address = "";
|
2012-08-15 20:08:18 +00:00
|
|
|
location_id = SSLIdType() ;
|
|
|
|
location_name = "" ;
|
|
|
|
memset(ipv4_internal_ip_and_port,0,6) ;
|
|
|
|
memset(ipv4_external_ip_and_port,0,6) ;
|
|
|
|
dns_name = "" ;
|
|
|
|
}
|
2012-08-13 19:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RsCertificate::scan_ip(const std::string& ip_string, unsigned short port,unsigned char *ip_and_port)
|
|
|
|
{
|
|
|
|
int d0,d1,d2,d3 ;
|
|
|
|
|
|
|
|
if(4 != sscanf(ip_string.c_str(),"%d.%d.%d.%d",&d0,&d1,&d2,&d3))
|
|
|
|
throw std::runtime_error( "Cannot parse ip from given string." );
|
|
|
|
|
|
|
|
ip_and_port[0] = d0 ;
|
|
|
|
ip_and_port[1] = d1 ;
|
|
|
|
ip_and_port[2] = d2 ;
|
|
|
|
ip_and_port[3] = d3 ;
|
|
|
|
|
|
|
|
ip_and_port[4] = (port >> 8 ) & 0xff ;
|
|
|
|
ip_and_port[5] = port & 0xff ;
|
|
|
|
}
|
|
|
|
|
2012-12-22 21:22:03 +00:00
|
|
|
bool RsCertificate::initFromString(const std::string& instr,uint32_t& err_code)
|
2012-08-11 20:43:10 +00:00
|
|
|
{
|
2014-01-09 20:20:46 +00:00
|
|
|
try
|
2012-08-11 20:43:10 +00:00
|
|
|
{
|
2014-01-09 20:20:46 +00:00
|
|
|
std::string str ;
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_NO_ERROR ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
// 0 - clean the string and check that it is pure radix64
|
|
|
|
//
|
|
|
|
for(uint32_t i=0;i<instr.length();++i)
|
|
|
|
{
|
|
|
|
if(instr[i] == ' ' || instr[i] == '\t' || instr[i] == '\n')
|
|
|
|
continue ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
if(! is_acceptable_radix64Char(instr[i]))
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
str += instr[i] ;
|
|
|
|
}
|
2012-08-14 07:34:29 +00:00
|
|
|
#ifdef DEBUG_RSCERTIFICATE
|
2014-01-09 20:20:46 +00:00
|
|
|
std::cerr << "Decoding from:" << str << std::endl;
|
2012-08-14 07:34:29 +00:00
|
|
|
#endif
|
2014-01-09 20:20:46 +00:00
|
|
|
// 1 - decode the string.
|
|
|
|
//
|
|
|
|
char *bf = NULL ;
|
|
|
|
size_t size ;
|
|
|
|
Radix64::decode(str,bf, size) ;
|
|
|
|
|
|
|
|
bool checksum_check_passed = false ;
|
|
|
|
unsigned char *buf = (unsigned char *)bf ;
|
|
|
|
size_t total_s = 0 ;
|
|
|
|
only_pgp = true ;
|
2014-02-01 14:16:15 +00:00
|
|
|
uint8_t certificate_version = 0x00 ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
while(total_s < size)
|
|
|
|
{
|
|
|
|
uint8_t ptag = buf[0];
|
|
|
|
buf = &buf[1] ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
unsigned char *buf2 = buf ;
|
|
|
|
uint32_t s = PGPKeyParser::read_125Size(buf) ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
total_s += 1 + ((unsigned long)buf-(unsigned long)buf2) ;
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
if(total_s > size)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_SIZE_ERROR ;
|
|
|
|
return false ;
|
|
|
|
}
|
2012-08-13 19:37:50 +00:00
|
|
|
|
2012-08-14 07:34:29 +00:00
|
|
|
#ifdef DEBUG_RSCERTIFICATE
|
2014-01-09 20:20:46 +00:00
|
|
|
std::cerr << "Packet parse: read ptag " << (int)ptag << ", size " << s << ", total_s = " << total_s << ", expected total = " << size << std::endl;
|
2012-08-14 07:34:29 +00:00
|
|
|
#endif
|
2014-01-09 20:20:46 +00:00
|
|
|
switch(ptag)
|
|
|
|
{
|
2014-02-01 14:16:15 +00:00
|
|
|
case CERTIFICATE_PTAG_VERSION_SECTION: certificate_version = buf[0] ;
|
2014-01-18 16:53:19 +00:00
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
|
2014-02-01 14:16:15 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
case CERTIFICATE_PTAG_PGP_SECTION: binary_pgp_key = new unsigned char[s] ;
|
|
|
|
memcpy(binary_pgp_key,buf,s) ;
|
|
|
|
binary_pgp_key_size = s ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CERTIFICATE_PTAG_NAME_SECTION: location_name = std::string((char *)buf,s) ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CERTIFICATE_PTAG_SSLID_SECTION:
|
|
|
|
if(s != location_id.SIZE_IN_BYTES)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCATION_ID ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
location_id = SSLIdType(buf) ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
only_pgp = false ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case CERTIFICATE_PTAG_DNS_SECTION: dns_name = std::string((char *)buf,s) ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
|
2014-02-01 14:16:15 +00:00
|
|
|
case CERTIFICATE_PTAG_HIDDENNODE_SECTION:
|
|
|
|
hidden_node_address = std::string((char *)buf,s);
|
|
|
|
hidden_node = true;
|
|
|
|
buf = &buf[s];
|
2013-09-09 02:10:49 +00:00
|
|
|
|
2014-02-01 14:16:15 +00:00
|
|
|
break ;
|
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
case CERTIFICATE_PTAG_LOCIPANDPORT_SECTION:
|
|
|
|
if(s != 6)
|
2013-09-09 02:10:49 +00:00
|
|
|
|
2012-12-22 21:22:03 +00:00
|
|
|
{
|
2014-01-09 20:20:46 +00:00
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCAL_IP;
|
2012-12-22 21:22:03 +00:00
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
memcpy(ipv4_internal_ip_and_port,buf,s) ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
case CERTIFICATE_PTAG_EXTIPANDPORT_SECTION:
|
|
|
|
if(s != 6)
|
2012-12-22 21:22:03 +00:00
|
|
|
{
|
2014-01-09 20:20:46 +00:00
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_INVALID_EXTERNAL_IP;
|
2012-12-22 21:22:03 +00:00
|
|
|
return false ;
|
|
|
|
}
|
2014-01-09 20:20:46 +00:00
|
|
|
|
|
|
|
memcpy(ipv4_external_ip_and_port,buf,s) ;
|
|
|
|
buf = &buf[s] ;
|
|
|
|
break ;
|
|
|
|
case CERTIFICATE_PTAG_CHECKSUM_SECTION:
|
|
|
|
{
|
|
|
|
if(s != 3 || total_s+3 != size)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_INVALID_CHECKSUM_SECTION ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
uint32_t computed_crc = PGPKeyManagement::compute24bitsCRC((unsigned char *)bf,size-5) ;
|
|
|
|
uint32_t certificate_crc = buf[0] + (buf[1] << 8) + (buf[2] << 16) ;
|
|
|
|
|
|
|
|
if(computed_crc != certificate_crc)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_CHECKSUM_ERROR ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
checksum_check_passed = true ;
|
|
|
|
}
|
|
|
|
break ;
|
|
|
|
default:
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_UNKNOWN_SECTION_PTAG ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_s += s ;
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
2012-12-22 21:22:03 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
if(!checksum_check_passed)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_MISSING_CHECKSUM ;
|
|
|
|
return false ;
|
|
|
|
}
|
2014-02-01 14:16:15 +00:00
|
|
|
|
|
|
|
if(certificate_version != CERTIFICATE_VERSION_06)
|
|
|
|
{
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_WRONG_VERSION ;
|
|
|
|
return false ;
|
|
|
|
}
|
2014-01-18 16:53:19 +00:00
|
|
|
#ifdef DEBUG_RSCERTIFICATE
|
2014-02-01 14:16:15 +00:00
|
|
|
std::cerr << "Certificate is version " << (int)certificate_version << std::endl;
|
2014-01-09 20:20:46 +00:00
|
|
|
#endif
|
2012-08-11 20:43:10 +00:00
|
|
|
|
2014-01-09 20:20:46 +00:00
|
|
|
if(total_s != size)
|
|
|
|
std::cerr << "(EE) Certificate contains trailing characters. Weird." << std::endl;
|
|
|
|
|
|
|
|
delete[] bf ;
|
|
|
|
return true ;
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
2014-01-09 20:20:46 +00:00
|
|
|
catch(std::exception& e)
|
2012-12-22 21:22:03 +00:00
|
|
|
{
|
2014-01-09 20:20:46 +00:00
|
|
|
if(binary_pgp_key != NULL)
|
|
|
|
delete[] binary_pgp_key ;
|
|
|
|
|
|
|
|
err_code = CERTIFICATE_PARSING_ERROR_SIZE_ERROR ;
|
2012-12-22 21:22:03 +00:00
|
|
|
return false ;
|
|
|
|
}
|
2012-08-11 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 02:10:49 +00:00
|
|
|
std::string RsCertificate::hidden_node_string() const
|
|
|
|
{
|
|
|
|
if ((!only_pgp) && (hidden_node))
|
|
|
|
{
|
|
|
|
return hidden_node_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string empty;
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2013-08-26 06:56:20 +00:00
|
|
|
std::string RsCertificate::sslid_string() const
|
|
|
|
{
|
|
|
|
if (only_pgp)
|
|
|
|
{
|
|
|
|
std::string empty;
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return location_id.toStdString(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 19:37:50 +00:00
|
|
|
std::string RsCertificate::ext_ip_string() const
|
|
|
|
{
|
|
|
|
std::ostringstream os ;
|
|
|
|
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] ;
|
|
|
|
return os.str() ;
|
|
|
|
}
|
|
|
|
std::string RsCertificate::loc_ip_string() const
|
|
|
|
{
|
|
|
|
std::ostringstream os ;
|
|
|
|
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] ;
|
|
|
|
return os.str() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short RsCertificate::ext_port_us() const
|
|
|
|
{
|
|
|
|
return (int)ipv4_external_ip_and_port[4]*256 + (int)ipv4_external_ip_and_port[5] ;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short RsCertificate::loc_port_us() const
|
|
|
|
{
|
|
|
|
return (int)ipv4_internal_ip_and_port[4]*256 + (int)ipv4_internal_ip_and_port[5] ;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsCertificate::cleanCertificate(const std::string& input,std::string& output,Format& format,int& error_code)
|
|
|
|
{
|
|
|
|
if(cleanCertificate(input,output,error_code))
|
|
|
|
{
|
|
|
|
format = RS_CERTIFICATE_RADIX ;
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RsCertificate::armouredPGPKey() const
|
|
|
|
{
|
|
|
|
return PGPKeyManagement::makeArmouredKey(binary_pgp_key,binary_pgp_key_size,pgp_version) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yeah, this is simple, and that is what's good about the radix format. Can't be broken ;-)
|
|
|
|
//
|
|
|
|
bool RsCertificate::cleanCertificate(const std::string& instr,std::string& str,int& error_code)
|
|
|
|
{
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_NO_ERROR ;
|
|
|
|
|
|
|
|
// 0 - clean the string and check that it is pure radix64
|
|
|
|
//
|
|
|
|
for(uint32_t i=0;i<instr.length();++i)
|
|
|
|
{
|
|
|
|
if(instr[i] == ' ' || instr[i] == '\t' || instr[i] == '\n')
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
if(! is_acceptable_radix64Char(instr[i]))
|
|
|
|
{
|
|
|
|
error_code = RS_PEER_CERT_CLEANING_CODE_WRONG_RADIX_CHAR ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += instr[i] ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now slice up to 64 chars.
|
|
|
|
//
|
|
|
|
std::string str2 ;
|
|
|
|
static const int LINE_LENGTH = 64 ;
|
|
|
|
|
|
|
|
for(int i=0;i<(int)str.length();++i)
|
|
|
|
{
|
|
|
|
str2 += str[i] ;
|
|
|
|
|
|
|
|
if(i % LINE_LENGTH == LINE_LENGTH-1)
|
|
|
|
str2 += '\n' ;
|
|
|
|
}
|
|
|
|
str = str2 ;
|
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 20:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|