2016-05-28 19:13:44 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/secretstore.h
|
|
|
|
// Purpose: Storing and retrieving secrets using OS-provided facilities.
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2016-05-27
|
|
|
|
// Copyright: (c) 2016 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_SECRETSTORE_H_
|
|
|
|
#define _WX_SECRETSTORE_H_
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
|
|
|
|
#if wxUSE_SECRETSTORE
|
|
|
|
|
2016-06-04 13:19:15 -04:00
|
|
|
#include "wx/string.h"
|
|
|
|
|
2016-05-28 19:13:44 -04:00
|
|
|
class wxSecretStoreImpl;
|
|
|
|
class wxSecretValueImpl;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Represents a secret value, e.g. a password string.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// This is an immutable value-like class which tries to ensure that the secret
|
|
|
|
// value will be wiped out from memory once it's not needed any more.
|
|
|
|
class WXDLLIMPEXP_BASE wxSecretValue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Creates an empty secret value (not the same as an empty password).
|
|
|
|
wxSecretValue() : m_impl(NULL) { }
|
|
|
|
|
|
|
|
// Creates a secret value from the given data.
|
2016-06-04 12:46:23 -04:00
|
|
|
wxSecretValue(size_t size, const void *data)
|
|
|
|
: m_impl(NewImpl(size, data))
|
|
|
|
{
|
|
|
|
}
|
2016-05-28 19:13:44 -04:00
|
|
|
|
2016-06-04 13:19:15 -04:00
|
|
|
// Creates a secret value from string.
|
|
|
|
explicit wxSecretValue(const wxString& secret)
|
|
|
|
{
|
|
|
|
const wxScopedCharBuffer buf(secret.utf8_str());
|
|
|
|
m_impl = NewImpl(buf.length(), buf.data());
|
|
|
|
}
|
|
|
|
|
2016-05-28 19:13:44 -04:00
|
|
|
wxSecretValue(const wxSecretValue& other);
|
|
|
|
wxSecretValue& operator=(const wxSecretValue& other);
|
|
|
|
|
|
|
|
~wxSecretValue();
|
|
|
|
|
|
|
|
// Check if a secret is not empty.
|
|
|
|
bool IsOk() const { return m_impl != NULL; }
|
|
|
|
|
|
|
|
// Compare with another secret.
|
|
|
|
bool operator==(const wxSecretValue& other) const;
|
|
|
|
bool operator!=(const wxSecretValue& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the size, in bytes, of the secret data.
|
|
|
|
size_t GetSize() const;
|
|
|
|
|
|
|
|
// Get read-only access to the secret data.
|
|
|
|
//
|
|
|
|
// Don't assume it is NUL-terminated, use GetSize() instead.
|
|
|
|
const void *GetData() const;
|
|
|
|
|
2016-06-04 13:19:15 -04:00
|
|
|
// Get the secret data as a string.
|
|
|
|
//
|
|
|
|
// Notice that you may want to overwrite the string contents after using it
|
|
|
|
// by calling WipeString().
|
|
|
|
wxString GetAsString(const wxMBConv& conv = wxConvWhateverWorks) const;
|
|
|
|
|
2016-05-28 19:13:44 -04:00
|
|
|
// Erase the given area of memory overwriting its presumably sensitive
|
|
|
|
// content.
|
|
|
|
static void Wipe(size_t size, void *data);
|
|
|
|
|
2016-06-04 13:19:15 -04:00
|
|
|
// Overwrite the contents of the given wxString.
|
|
|
|
static void WipeString(wxString& str);
|
|
|
|
|
2016-05-28 19:13:44 -04:00
|
|
|
private:
|
2016-06-04 12:46:23 -04:00
|
|
|
// This method is implemented in platform-specific code and must return a
|
|
|
|
// new heap-allocated object initialized with the given data.
|
|
|
|
static wxSecretValueImpl* NewImpl(size_t size, const void *data);
|
|
|
|
|
2016-05-28 19:13:44 -04:00
|
|
|
// This ctor is only used by wxSecretStore and takes ownership of the
|
|
|
|
// provided existing impl pointer.
|
|
|
|
explicit wxSecretValue(wxSecretValueImpl* impl) : m_impl(impl) { }
|
|
|
|
|
|
|
|
wxSecretValueImpl* m_impl;
|
|
|
|
|
|
|
|
friend class wxSecretStore;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A collection of secrets, sometimes called a key chain.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_BASE wxSecretStore
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Returns the default secrets collection to use.
|
|
|
|
//
|
|
|
|
// Currently this is the only way to create a secret store object. In the
|
|
|
|
// future we could add more factory functions to e.g. create non-persistent
|
|
|
|
// stores or allow creating stores corresponding to the native facilities
|
|
|
|
// being used (e.g. specify schema name under Linux or a SecKeychainRef
|
|
|
|
// under OS X).
|
|
|
|
static wxSecretStore GetDefault();
|
|
|
|
|
|
|
|
// This class has no default ctor, use GetDefault() instead.
|
|
|
|
|
|
|
|
// But it can be copied, a copy refers to the same store as the original.
|
|
|
|
wxSecretStore(const wxSecretStore& store);
|
|
|
|
|
|
|
|
// Dtor is not virtual, this class is not supposed to be derived from.
|
|
|
|
~wxSecretStore();
|
|
|
|
|
|
|
|
|
|
|
|
// Check if this object is valid.
|
|
|
|
bool IsOk() const { return m_impl != NULL; }
|
|
|
|
|
|
|
|
|
|
|
|
// Store a secret.
|
|
|
|
//
|
|
|
|
// The service name should be user readable and unique.
|
|
|
|
//
|
|
|
|
// If a secret with the same service name and user already exists, it will
|
|
|
|
// be overwritten with the new value.
|
|
|
|
//
|
|
|
|
// Returns false after logging an error message if an error occurs,
|
|
|
|
// otherwise returns true indicating that the secret has been stored.
|
|
|
|
bool Save(const wxString& service,
|
|
|
|
const wxString& user,
|
|
|
|
const wxSecretValue& secret);
|
|
|
|
|
|
|
|
// Look up a secret.
|
|
|
|
//
|
|
|
|
// If no such secret is found, an empty value is returned, but no error is
|
|
|
|
// logged (however an error may still be logged if some other error occurs).
|
|
|
|
// If more than one secret matching the parameters exist, only one
|
|
|
|
// arbitrarily chosen of them is returned (notice that it's impossible to
|
|
|
|
// get into such situation using this API only).
|
|
|
|
wxSecretValue Load(const wxString& service, const wxString& user) const;
|
|
|
|
|
|
|
|
// Delete a previously stored secret.
|
|
|
|
//
|
|
|
|
// If there is more than one matching secret, all of them are deleted.
|
|
|
|
//
|
|
|
|
// If any secrets were deleted, returns true. Otherwise returns false and
|
|
|
|
// logs an error if any error other than not finding any matching secrets
|
|
|
|
// occurred.
|
|
|
|
bool Delete(const wxString& service, const wxString& user);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Ctor takes ownership of the passed pointer.
|
|
|
|
explicit wxSecretStore(wxSecretStoreImpl* impl) : m_impl(impl) { }
|
|
|
|
|
|
|
|
wxSecretStoreImpl* const m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // wxUSE_SECRETSTORE
|
|
|
|
|
|
|
|
#endif // _WX_SECRETSTORE_H_
|