示例#1
0
 void urlsafe_base64decode(const std::string& input,std::string& output){
     assert(input.size()<1024);
     try {
         char buffer[1024];
         const char *p = input.c_str();
         char *dst = buffer;
         while (*p != '\0') {
             if (*p == '-') {
                 *dst = '+';
             } else if (*p == '_') {
                 *dst = '/';
             } else {
                 *dst = *p;
             }
             p++;
             dst++;
         }
         CryptoPP::Base64Decoder decoder;
         decoder.Attach(new CryptoPP::StringSink(output));
         decoder.Put((uchar_t *) buffer, input.size());
         decoder.MessageEnd();
     }catch(exception& e){
         DebugMessageWithTime("urlsafe_base64decode failed,e:",e.what());
     }
 }
示例#2
0
文件: Reseed.cpp 项目: hagen-i2p/i2pd
	void Reseeder::LoadCertificate (const std::string& filename)
	{
		std::ifstream s(filename, std::ifstream::binary);
		if (s.is_open ())	
		{
			s.seekg (0, std::ios::end);
			size_t len = s.tellg ();
			s.seekg (0, std::ios::beg);
			char buf[2048];
			s.read (buf, len);
			std::string cert (buf, len);
			// assume file in pem format
			auto pos1 = cert.find (CERTIFICATE_HEADER);	
			auto pos2 = cert.find (CERTIFICATE_FOOTER);	
			if (pos1 == std::string::npos || pos2 == std::string::npos)
			{
				LogPrint (eLogError, "Malformed certificate file");
				return;
			}	
			pos1 += strlen (CERTIFICATE_HEADER);
			pos2 -= pos1;
			std::string base64 = cert.substr (pos1, pos2);

			CryptoPP::ByteQueue queue;
			CryptoPP::Base64Decoder decoder; // regular base64 rather than I2P 
			decoder.Attach (new CryptoPP::Redirector (queue));
			decoder.Put ((const uint8_t *)base64.data(), base64.length());
			decoder.MessageEnd ();

			// extract X.509
			CryptoPP::BERSequenceDecoder x509Cert (queue);
			CryptoPP::BERSequenceDecoder tbsCert (x509Cert);
			// version
			uint32_t ver;
			CryptoPP::BERGeneralDecoder context (tbsCert, 0xa0);
			CryptoPP::BERDecodeUnsigned<uint32_t>(context, ver, CryptoPP::INTEGER);
			LogPrint (eLogInfo, ver);	
			// serial
			CryptoPP::Integer serial;
       		serial.BERDecode(tbsCert);	
			// signature
			CryptoPP::BERSequenceDecoder signature (tbsCert);
       		signature.SkipAll();
			// issuer
			CryptoPP::BERSequenceDecoder issuer (tbsCert);
       		issuer.SkipAll();
			// validity
			CryptoPP::BERSequenceDecoder validity (tbsCert);
       		validity.SkipAll();
			// subject
			CryptoPP::BERSequenceDecoder subject (tbsCert);
       		subject.SkipAll();
			// public key
			CryptoPP::BERSequenceDecoder publicKey (tbsCert);
       		publicKey.SkipAll();
			
			tbsCert.SkipAll();
			x509Cert.SkipAll();
		}
		else
			LogPrint (eLogError, "Can't open certificate file ", filename);
	}