Exemplo n.º 1
0
void Base64TestCase::EncodeDecodeRandom()
{
    size_t size = rand() * 3000 / RAND_MAX + 11;
    unsigned char *buff = new unsigned char[size];
    generateRandomData(buff, size);
    wxString str = wxBase64Encode(buff, size);
    wxMemoryBuffer mbuff = wxBase64Decode(str);
    CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);

    generateGibberish(buff, size);
    char *buff2 = new char[size];
    size_t realsize = size;
    CPPUNIT_ASSERT(wxBase64Decode(buff2, realsize, (char *)buff, size));
    CPPUNIT_ASSERT(wxBase64Encode(buff2, size, buff2, realsize));
}
Exemplo n.º 2
0
bool CUserItem::getAsString( wxString& strUser )
{
    wxString wxstr;
    strUser.Empty();
        
    strUser += wxString::Format( _("%ld;"), getUserID() );
    strUser += getUser();
    strUser += _(";");
    // Protect password
    wxstr = getPassword();
    for ( int i=0; i<wxstr.Length(); i++ ) {
        strUser += _("*");
    }
    //strUser += getPassword();
    strUser += _(";");
    strUser += getFullname();
    strUser += _(";");
    vscp_writeFilterToString( getFilter(), wxstr );
    strUser += wxstr;
    strUser += _(";");
    vscp_writeMaskToString( getFilter(), wxstr );
    strUser += wxstr;
    strUser += _(";");
    strUser += getUserRightsAsString();
    strUser += _(";");
    strUser += getAllowedRemotesAsString();
    strUser += _(";");
    strUser += getAllowedEventsAsString();
    strUser += _(";");
    strUser += wxBase64Encode( getNote().mbc_str(), strlen( getNote().mbc_str() ) ); 	
    //strUser += getNote();
    
    return true;
}
Exemplo n.º 3
0
void Base64TestCase::EncodeDecodePatternC()
{
    unsigned char buff[11];
    generatePatternedData(buff, 11, 1, 0, 2);
    wxString str = wxBase64Encode(buff, 11);
    wxMemoryBuffer mbuff = wxBase64Decode(str);
    CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
}
Exemplo n.º 4
0
static void
wxgtk_webview_webkit_resource_req(WebKitWebView *,
                                  WebKitWebFrame *,
                                  WebKitWebResource *,
                                  WebKitNetworkRequest *request,
                                  WebKitNetworkResponse *,
                                  wxWebViewWebKit *webKitCtrl)
{
    wxString uri = webkit_network_request_get_uri(request);

    wxSharedPtr<wxWebViewHandler> handler;
    wxVector<wxSharedPtr<wxWebViewHandler> > handlers = webKitCtrl->GetHandlers();

    //We are not vetoed so see if we match one of the additional handlers
#if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */
#   pragma ivdep
#   pragma swp
#   pragma unroll
#   pragma prefetch
#   if 0
#       pragma simd noassert
#   endif
#endif /* VDM auto patch */
    for(wxVector<wxSharedPtr<wxWebViewHandler> >::iterator it = handlers.begin();
        it != handlers.end(); ++it)
    {
        if(uri.substr(0, (*it)->GetName().length()) == (*it)->GetName())
        {
            handler = (*it);
        }
    }
    //If we found a handler we can then use it to load the file directly
    //ourselves
    if(handler)
    {
        //If it is requsting the page itself then return as we have already
        //loaded it from the archive
        if(webKitCtrl->m_vfsurl == uri)
            return;

        wxFSFile* file = handler->GetFile(uri);
        if(file)
        {
            //We load the data into a data url to save it being written out again
            size_t size = file->GetStream()->GetLength();
            char *buffer = new char[size];
            file->GetStream()->Read(buffer, size);
            wxString data = wxBase64Encode(buffer, size);
            delete[] buffer;
            wxString mime = file->GetMimeType();
            wxString path = "data:" + mime + ";base64," + data;
            //Then we can redirect the call
            webkit_network_request_set_uri(request, path.utf8_str());
        }

    }
}
Exemplo n.º 5
0
void Base64TestCase::EncodeDecodeEmpty()
{
    char shouldBeEmpty[10];
    shouldBeEmpty[0] = '\0';
    size_t len = 10;

    CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty, len, "", 0) != wxCONV_FAILED);
    CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);

    CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty, len, "") != wxCONV_FAILED);
    CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);

    wxMemoryBuffer bufmt;
    wxString resultEmpty = wxBase64Encode(bufmt);
    CPPUNIT_ASSERT(resultEmpty.empty());

    bufmt = wxBase64Decode(resultEmpty);
    CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
}
Exemplo n.º 6
0
void Base64TestCase::EncodeDecode0to255()
{
    unsigned char buff[256];
    generatePatternedData(buff, 256, 0, 1);
    wxString str = wxBase64Encode(buff, 256);
    wxMemoryBuffer mbuff = wxBase64Decode(str);
    CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);

    mbuff = wxBase64Decode(encoded0to255);
    CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
}
Exemplo n.º 7
0
void Base64TestCase::EncodeDecodeA()
{
    const wxString str = wxBase64Encode("A", 1);
    CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);

    wxMemoryBuffer buf = wxBase64Decode(str);
    CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
    CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());

    char cbuf[10];
    memset(cbuf, (char)-1, sizeof(cbuf));
    CPPUNIT_ASSERT_EQUAL( 1, wxBase64Decode(cbuf, 1, str) );
    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[1] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
}
Exemplo n.º 8
0
void Base64TestCase::EncodeDecodeAB()
{
    const wxString str = wxBase64Encode("AB", 2);
    CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);

    wxMemoryBuffer buf = wxBase64Decode(str);
    CPPUNIT_ASSERT_EQUAL(2, buf.GetDataLen());
    CPPUNIT_ASSERT_EQUAL('A', buf[0]);
    CPPUNIT_ASSERT_EQUAL('B', buf[1]);

    char cbuf[10];
    memset(cbuf, (char)-1, sizeof(cbuf));
    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 1, str) );
    CPPUNIT_ASSERT_EQUAL( 2, wxBase64Decode(cbuf, 2, str) );
    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
    CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[3] );
}
Exemplo n.º 9
0
bool
wxSVGBitmapEmbedHandler::ProcessBitmap(const wxBitmap& bmp,
                                       wxCoord x, wxCoord y,
                                       wxOutputStream& stream) const
{
    static int sub_images = 0;

    if ( wxImage::FindHandler(wxBITMAP_TYPE_PNG) == NULL )
        wxImage::AddHandler(new wxPNGHandler);

    // write the bitmap as a PNG to a memory stream and Base64 encode
    wxMemoryOutputStream mem;
    bmp.ConvertToImage().SaveFile(mem, wxBITMAP_TYPE_PNG);
    wxString data = wxBase64Encode(mem.GetOutputStreamBuffer()->GetBufferStart(),
                                   mem.GetSize());

    // write image meta information
    wxString s;
    s += wxString::Format(" <image x=\"%d\" y=\"%d\" "
                          "width=\"%dpx\" height=\"%dpx\" "
                          "title=\"Image from wxSVG\"\n",
                          x, y, bmp.GetWidth(), bmp.GetHeight());
    s += wxString::Format(" id=\"image%d\" "
                          "xlink:href=\"data:image/png;base64,\n",
                          sub_images++);

    // Wrap Base64 encoded data on 76 columns boundary (same as Inkscape).
    const unsigned WRAP = 76;
    for ( size_t i = 0; i < data.size(); i += WRAP )
    {
        if (i < data.size() - WRAP)
            s += data.Mid(i, WRAP) + "\n";
        else
            s += data.Mid(i, s.size() - i) + "\"\n/>"; // last line
    }

    // write to the SVG file
    const wxCharBuffer buf = s.utf8_str();
    stream.Write(buf, strlen((const char *)buf));

    return stream.IsOk();
}
Exemplo n.º 10
0
// -------------------------------------------------------------------------------- //
wxString GetAmazonSign( const wxString &text )
{
#define AMAZON_SEARCH_SECRETL    wxT( "ICsfRx7YNpBBamJyJcol" )
#define AMAZON_SEARCH_SECRETR    wxT( "N0qGKH6bBG7NlA9kLqhq" )
    wxString Str = wxT( "GET\necs.amazonaws.com\n/onca/xml\n" ) + text;

    //guLogMessage( wxT( "String : '%s'" ), Str.c_str() );
    wxString Key = AMAZON_SEARCH_SECRETL AMAZON_SEARCH_SECRETR;
    char * Output = ( char * ) malloc( 1024 );

    HMACSha256( Key.char_str(), Key.Length(), Str.char_str(), Str.Length(), Output, SHA256_DIGEST_SIZE );

    wxString Sign = wxBase64Encode( Output, SHA256_DIGEST_SIZE );
    //guLogMessage( wxT( "Signature: '%s'" ), Sign.c_str() );
    Sign.Replace( wxT( "+" ), wxT( "%2B" ) );
    Sign.Replace( wxT( "=" ), wxT( "%3D" ) );

    //guLogMessage( wxT( "Encoded: '%s'" ), Sign.c_str() );
    free( Output );
    return Sign;
}
Exemplo n.º 11
0
void Base64TestCase::EncodeDecodeABCD()
{
    const wxString str = wxBase64Encode("ABCD", 4);
    CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);

    wxMemoryBuffer buf = wxBase64Decode(str);
    CPPUNIT_ASSERT_EQUAL(4, buf.GetDataLen());
    CPPUNIT_ASSERT_EQUAL('A', buf[0]);
    CPPUNIT_ASSERT_EQUAL('B', buf[1]);
    CPPUNIT_ASSERT_EQUAL('C', buf[2]);
    CPPUNIT_ASSERT_EQUAL('D', buf[3]);

    char cbuf[10];
    memset(cbuf, (char)-1, sizeof(cbuf));
    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 3, str) );
    CPPUNIT_ASSERT_EQUAL( 4, wxBase64Decode(cbuf, 4, str) );
    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
    CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
    CPPUNIT_ASSERT_EQUAL( 'C', cbuf[2] );
    CPPUNIT_ASSERT_EQUAL( 'D', cbuf[3] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[4] );
    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[5] );
}
Exemplo n.º 12
0
znHmacSignature znUtils::GenerateHmacSignature(wxString service_bus_name, wxString event_hub_name,
    wxString publisher_name, wxString shared_access_policy_name, wxString shared_access_key, int ttl)
{
    // This routine will generate 2 types of HMAC SHA256 signatures in 2 different formats.

    // Input data sanitization.

    service_bus_name.Trim().Trim(false);
    event_hub_name.Trim().Trim(false);
    publisher_name.Trim().Trim(false);
    shared_access_policy_name.Trim().Trim(false);
    shared_access_key.Trim().Trim(false);

    // Input data validity checks.

    if (service_bus_name == wxEmptyString || event_hub_name == wxEmptyString ||
        shared_access_policy_name == wxEmptyString || shared_access_key == wxEmptyString)
    {
        // Error.
        wxMessageBox("One of the inputs is blank. Aborting !", ZN_APP_TITLE, wxOK | wxICON_INFORMATION, NULL);
        return{ -1, -1 };
    }

    ////////////////////////////////////////////////////////
    // Get the current UTF time in seconds.
    ////////////////////////////////////////////////////////

    // Add a time offset so that the HMAC SHA256 signature will expire in near future
    // 1 minute later in this case

    wxDateTime time_0 = wxDateTime::UNow() + wxTimeSpan::Seconds(ttl);

    time_t params_utc_time = time_0.GetTicks();

    // For debugging. With time set at 1443266928, output should be
    // "r6UIOLS7t31sEDwFuJcIH9FO0mbPcNpdtKZaH2lC7uU%3d"
    //
    // params_utc_time = 1443266928;

    ////////////////////////////////////////////////////////
    // Construct the HMAC SHA256 signature - Format 1.
    ////////////////////////////////////////////////////////

    wxString params_url_1 = service_bus_name + ".servicebus.windows.net";

    wxString final_signature_1;

    {
        // The input needed for HMAC signature calculations are
        //
        //  - ID_ZN_TXT_SHARED_ACCESS_POLICY_KEY
        //  - UTC time

        std::string params_key_std = shared_access_key.ToUTF8();
        std::string params_data_std = znUtils::UrlEncode(params_url_1) + wxString::Format("\n%d", (int)params_utc_time);

        // Initialize HMAC object.
        HMAC_CTX ctx;
        HMAC_CTX_init(&ctx);

        // Set HMAC key.

        HMAC_Init_ex(&ctx, params_key_std.c_str(), params_key_std.length(), EVP_sha256(), NULL);

        HMAC_Update(&ctx, (unsigned char*)params_data_std.c_str(), params_data_std.length());

        // Finish HMAC computation and fetch result.
        unsigned char hmac_result[1024 * 10];
        unsigned int hmac_result_len;

        int rvalue = HMAC_Final(&ctx, hmac_result, &hmac_result_len);

        // Done with HMAC object.
        HMAC_CTX_cleanup(&ctx);

        // Encode HMAC result with Base64.

        final_signature_1 = wxBase64Encode(hmac_result, hmac_result_len);
    }

    // Encode the result with URL formatting.

    final_signature_1.Replace(wxT("="), wxT("%3d"));
    final_signature_1.Replace(wxT("/"), wxT("%2f"));
    final_signature_1.Replace(wxT("+"), wxT("%2b"));

    ////////////////////////////////////////////////////////
    // Construct Authorization String
    ////////////////////////////////////////////////////////

    // Construct an Authorization string with the calculated HMAC SHA256 signature.
    // A valid Authorization string looks like this.
    //
    // "Authorization: SharedAccessSignature sr=zailorbus.servicebus.windows.net&
    //  sig=52HQdj3Z6HI3n0T93HkbSlZXLKOCMs4URQb5cxQE2Bo%3d&se=1443339673&skn=super"

    wxString final_authorization_1;

    final_authorization_1 = "Authorization: SharedAccessSignature ";
    final_authorization_1 += "sr=" + znUtils::UrlEncode(params_url_1);
    final_authorization_1 += "&sig=" + final_signature_1;
    final_authorization_1 += "&se=" + wxString::Format("%d", (int)params_utc_time);
    final_authorization_1 += "&skn=" + shared_access_policy_name;

    ////////////////////////////////////////////////////////
    // Construct the HMAC SHA256 signature - Format 2.
    ////////////////////////////////////////////////////////

    wxString params_url_2 = "https://" + service_bus_name + ".servicebus.windows.net/" +
        event_hub_name + "/publishers/" + publisher_name + "/messages";

    wxString final_signature_2;

    {
        // The input needed for HMAC signature calculations are
        //
        //  - ID_ZN_TXT_SHARED_ACCESS_POLICY_KEY
        //  - UTC time

        std::string params_key_std = shared_access_key.ToUTF8();
        std::string params_data_std = znUtils::UrlEncode(params_url_2) + wxString::Format("\n%d", (int)params_utc_time);

        // Initialize HMAC object.
        HMAC_CTX ctx;
        HMAC_CTX_init(&ctx);

        // Set HMAC key.

        HMAC_Init_ex(&ctx, params_key_std.c_str(), params_key_std.length(), EVP_sha256(), NULL);

        HMAC_Update(&ctx, (unsigned char*)params_data_std.c_str(), params_data_std.length());

        // Finish HMAC computation and fetch result.
        unsigned char hmac_result[1024 * 10];
        unsigned int hmac_result_len;

        int rvalue = HMAC_Final(&ctx, hmac_result, &hmac_result_len);

        // Done with HMAC object.
        HMAC_CTX_cleanup(&ctx);

        // Encode HMAC result with Base64.

        final_signature_2 = wxBase64Encode(hmac_result, hmac_result_len);
    }

    // Encode the result with URL formatting.

    final_signature_2.Replace(wxT("="), wxT("%3d"));
    final_signature_2.Replace(wxT("/"), wxT("%2f"));
    final_signature_2.Replace(wxT("+"), wxT("%2b"));

    ////////////////////////////////////////////////////////
    // Construct Authorization String
    ////////////////////////////////////////////////////////

    wxString final_authorization_2;

    final_authorization_2 = "Authorization: SharedAccessSignature ";
    final_authorization_2 += "sr=" + znUtils::UrlEncode(params_url_2);
    final_authorization_2 += "&sig=" + final_signature_2;
    final_authorization_2 += "&se=" + wxString::Format("%d", (int)params_utc_time);
    final_authorization_2 += "&skn=" + shared_access_policy_name;

    return{ 0, params_utc_time,
        params_url_1, final_signature_1, final_authorization_1,
        params_url_2, final_signature_2, final_authorization_2 };
}
Exemplo n.º 13
0
bool wxDatabaseConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
{
	return Write(key, wxBase64Encode(buf));
}
Exemplo n.º 14
0
void SetServer(pugi::xml_node node, const CServer& server)
{
	if (!node)
		return;

	bool kiosk_mode = COptions::Get()->GetOptionVal(OPTION_DEFAULT_KIOSKMODE) != 0;

	for (auto child = node.first_child(); child; child = node.first_child()) {
		node.remove_child(child);
	}

	AddTextElement(node, "Host", server.GetHost());
	AddTextElement(node, "Port", server.GetPort());
	AddTextElement(node, "Protocol", server.GetProtocol());
	AddTextElement(node, "Type", server.GetType());

	enum LogonType logonType = server.GetLogonType();

	if (server.GetLogonType() != ANONYMOUS) {
		AddTextElement(node, "User", server.GetUser());

		if (server.GetLogonType() == NORMAL || server.GetLogonType() == ACCOUNT) {
			if (kiosk_mode)
				logonType = ASK;
			else {
				wxString pass = server.GetPass();
				auto const& buf = pass.utf8_str(); // wxWidgets has such an ugly string API....
				std::string utf8(buf.data(), buf.length());

				wxString base64 = wxBase64Encode(utf8.c_str(), utf8.size());
				pugi::xml_node passElement = AddTextElement(node, "Pass", base64);
				if (passElement) {
					SetTextAttribute(passElement, "encoding", _T("base64"));
				}

				if (server.GetLogonType() == ACCOUNT)
					AddTextElement(node, "Account", server.GetAccount());
			}
		}
		else if (server.GetLogonType() == KEY)
		{
			AddTextElement(node, "Keyfile", server.GetKeyFile());
		}
	}
	AddTextElement(node, "Logontype", logonType);

	AddTextElement(node, "TimezoneOffset", server.GetTimezoneOffset());
	switch (server.GetPasvMode())
	{
	case MODE_PASSIVE:
		AddTextElementRaw(node, "PasvMode", "MODE_PASSIVE");
		break;
	case MODE_ACTIVE:
		AddTextElementRaw(node, "PasvMode", "MODE_ACTIVE");
		break;
	default:
		AddTextElementRaw(node, "PasvMode", "MODE_DEFAULT");
		break;
	}
	AddTextElement(node, "MaximumMultipleConnections", server.MaximumMultipleConnections());

	switch (server.GetEncodingType())
	{
	case ENCODING_AUTO:
		AddTextElementRaw(node, "EncodingType", "Auto");
		break;
	case ENCODING_UTF8:
		AddTextElementRaw(node, "EncodingType", "UTF-8");
		break;
	case ENCODING_CUSTOM:
		AddTextElementRaw(node, "EncodingType", "Custom");
		AddTextElement(node, "CustomEncoding", server.GetCustomEncoding());
		break;
	}

	if (CServer::SupportsPostLoginCommands(server.GetProtocol())) {
		std::vector<wxString> const& postLoginCommands = server.GetPostLoginCommands();
		if (!postLoginCommands.empty()) {
			auto element = node.append_child("PostLoginCommands");
			for (std::vector<wxString>::const_iterator iter = postLoginCommands.begin(); iter != postLoginCommands.end(); ++iter) {
				AddTextElement(element, "Command", *iter);
			}				
		}
	}

	AddTextElementRaw(node, "BypassProxy", server.GetBypassProxy() ? "1" : "0");
	const wxString& name = server.GetName();
	if (!name.empty())
		AddTextElement(node, "Name", name);
}