Menu

Key Provider - Public custom data

furryboi69
2017-03-15
2017-03-15
  • furryboi69

    furryboi69 - 2017-03-15

    Is there any way to read/write a database's public custom data from a key provider?

    I'm trying to modify KeeChallenge so that it doesn't require a separate xml file.

    In the following code, I pass the IPluginHost to the key provider.
    Setting the title text of the main KeePass window using m_host.MainWindow.Text works.
    The data seems to be added but is not actually saved with the database. GetString returns the expected string only when SetString is not commented out.

    namespace KeeChallenge
    {
        public sealed class KeeChallengeExt : Plugin
        {
            private IPluginHost m_host = null;
            private KeeChallengeProv m_prov = null;
    
            public override bool Initialize(IPluginHost host)
            {
                m_host = host;
    
                m_prov = new KeeChallengeProv(host);
                m_host.KeyProviderPool.Add(m_prov);
    
                return true;
            }
        }
    
        public sealed class KeeChallengeProv : KeyProvider
        {
            private IPluginHost m_host = null;
    
            public KeeChallengeProv(IPluginHost host)
            {
                m_host = host;
            }
    
            public override byte[] GetKey(KeyProviderQueryContext ctx)
            {
                //m_host.Database.PublicCustomData.SetString("gk", "GetKey");
                m_host.MainWindow.Text = m_host.MainWindow.Text.Replace("KeePass", m_host.Database.PublicCustomData.GetString("gk"));
                if (ctx.CreatingNewKey) return Create(ctx);
                return Get(ctx);
            }
        }
    }
    
     
  • Dominik Reichl

    Dominik Reichl - 2017-03-16

    Not really.

    The process of opening a database file is the following:

    1. KeePass obtains the path (and server credentials) of the database file to open (e.g. via the 'Open File' dialog, the command line, ...).
    2. The master key dialog is displayed (showing the database file path).
    3. KeePass tries to read and decrypt the database file.

    The key provider's GetKey method is invoked in step 2, before actually reading the file. So, the public custom data that you're accessing in your example code is either the public custom data of a different or of an empty database, but definitely not the one of the file that will be read in step 3.

    I'm not planning to change the file opening process (primarily due to keeping a file open for a longer time or opening it multiple times can be problematic with some servers). If you really want to use public custom data for this, you'd need to read and parse the KDBX header in your plugin yourself.

    Best regards,
    Dominik

     
  • furryboi69

    furryboi69 - 2017-03-16

    Thanks for your response. I guess I'll be parsing the header myself.

     
  • Dominik Reichl

    Dominik Reichl - 2017-03-17

    For OtpKeyProv, I'll continue using an auxiliary file. The auxiliary file changes each time the user opens the database (because it needs to be updated for the next one-time passwords), and when the database file is huge, writing a new small auxiliary file is far more efficient than writing a new database file each and every time.

    Best regards,
    Dominik

     

Log in to post a comment.

MongoDB Logo MongoDB