///////////////////////////////////////////////////////////////////////////// // Name: secretstore.cpp // Purpose: wxWidgets sample showing the use of wxSecretStore class // Author: Vadim Zeitlin // Created: 2016-05-27 // Copyright: (c) 2016 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // ============================================================================ // declarations // ============================================================================ // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" #include "wx/secretstore.h" #include "wx/init.h" #include "wx/crt.h" #include "wx/log.h" #include bool g_verbose_ {false}; bool g_binary_ {false}; bool Save(wxSecretStore& store, const wxString& service, const wxString& user) { char password[4096]; wxPrintf("Enter the password for %s/%s (echoing NOT disabled): ", service, user); if ( !wxFgets(password, WXSIZEOF(password), stdin) ) { wxFprintf(stderr, "Password not stored.\n"); return false; } size_t size = wxStrlen(password); if ( size ) { // Strip trailing new line. --size; password[size] = 0; } wxSecretValue secret(size, password); // The password data was copied into wxSecretValue, don't leave it lying // around in the stack unnecessarily. wxSecretValue::Wipe(size, password); if ( !store.Save(service, user, secret) ) { wxFprintf(stderr, "Failed to save the password for %s/%s.\n", service, user); return false; } wxPrintf("Password for %s/%s saved.\n", service, user); return true; } bool Load(wxSecretStore& store, const wxString& service) { wxString user; wxSecretValue secret; if ( !store.Load(service, user, secret) ) { wxFprintf(stderr, "Failed to load the password for %s.\n", service); return false; } // Create a temporary variable just to make it possible to wipe it after // using it. wxString str(secret.GetAsString()); const size_t size = secret.GetSize(); wxPrintf("Password for %s/%s is %zu bytes long: \"%s\"\n", service, user, size, str); wxSecretValue::WipeString(str); return true; } bool Delete(wxSecretStore& store, const wxString& service) { if ( !store.Delete(service) ) { wxFprintf(stderr, "Password for %s not deleted.\n", service); return false; } wxPrintf("Stored password for %s deleted.\n", service); return true; } static bool PrintResult(bool ok) { wxPuts(ok ? "ok" : "ERROR"); return ok; } static void PrintSecrets(const wxSecretValue& expected, const wxSecretValue& loaded) { wxPrintf("Expected: size=%ld data=", expected.GetSize()); if (g_binary_) { wxPrintf("["); size_t n = expected.GetSize(); const char* p = static_cast(expected.GetData()); for (size_t i=0; i(loaded.GetData()); for (size_t i=0; i \n" " or %s {load|delete} \n" " or %s [options] selftest \n" "\n" "Sample showing wxSecretStore class functionality.\n" "Specify one of the commands to perform the corresponding\n" "function call. The \"service\" argument is mandatory for\n" "all commands, \"save\" also requires \"user\" and will\n" "prompt for password.\n\n" "options:\n" "\t-v\trun verbose (possibly shows secrets on errors)\n" "\t-b\trun selftest using binary secrets (otherwise uses text strings)\n\n", argv[0], argv[0], argv[0]); } int main(int argc, char **argv) { // To complement the standard EXIT_{SUCCESS,FAILURE}. const int EXIT_SYNTAX = 2; wxInitializer initializer; if ( !initializer ) { fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); return EXIT_FAILURE; } wxString operation; wxString service; wxString user; for (int arg=1; arg