tobischo / gokeepasslib

A library to read and write keepass 2 files written in go
MIT License
244 stars 30 forks source link

Minor Version issue #114

Closed philxws692 closed 1 month ago

philxws692 commented 1 month ago

Hey all,

I just stumbled upon this great project as I was searching for a way to build myself a little CLI tool for my keepass database. However when im trying to read the database I get the following error: gokeepasslib: invalid signature. Minor Version is 0. Should be 1

The error occurs when decoding the database. My code for decoding the database is taken from the example code and looks like this

file, _ := os.Open(path)
db := gokeepasslib.NewDatabase()
db.Credentials = gokeepasslib.NewPasswordCredentials("test1234")
err := gokeepasslib.NewDecoder(file).Decode(db)

The path I enter is corrected and refers to my home folder. I already tried changing the wanted Minor Version in order to see if this will work, but I ended up with another signature error: gokeepasslib: invalid signature. Base Signature is 7b226461. Should be 03d9a29a

So I decided to check the wanted signature and if it exists in the database, which it does:

Bildschirmfoto 2024-07-19 um 15 01 45

I really don't know where the issue is. The database version is KDBX 4.0 and was created with KeepassXC Version 2.7.9

Any help would be really appreciated, thanks :)

Best regards Philipp

tobischo commented 1 month ago

Hi @philxws692

would you be able to provide an example KDBX file with the same DB settings with which you can also recreate the issue? It will make the investigation a lot easier.

Best, Tobias

philxws692 commented 1 month ago

Woah that was fast,

I can provide you the exact database, as this is only a database used for testing šŸ˜„ database.kdbx.zip

Password is: test1234 for simplicity

Best regards Philipp

tobischo commented 1 month ago

Thank you

I will probably not be able to debug it immediately, but it helps to ask for needed input quickly šŸ˜„

I'll let you know what I find

philxws692 commented 1 month ago

Yeah no worries, it is not that important, as I also have not a lot time for this project šŸ˜„

But thank you already for your future debugging šŸ„³

tobischo commented 1 month ago

I believe I found the issue šŸ˜„

Your example would probably look something like this:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/tobischo/gokeepasslib"
)

func main() {
    path := "./database.kdbx"

    file, _ := os.Open(path)
    db := gokeepasslib.NewDatabase()
    db.Credentials = gokeepasslib.NewPasswordCredentials("test1234")
    err := gokeepasslib.NewDecoder(file).Decode(db)
    if err != nil {
        log.Fatal(err)
    }

    db.UnlockProtectedEntries()

    // Note: This is a simplified example and the groups and entries will depend on the specific file.
    // bound checking for the slices is recommended to avoid panics.
    entry := db.Content.Root.Groups[0].Entries[0]
    fmt.Println(entry.GetTitle())
    fmt.Println(entry.GetPassword())
}

and it should look like this:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/tobischo/gokeepasslib/v3"
)

func main() {
    path := "./database.kdbx"

    file, _ := os.Open(path)
    db := gokeepasslib.NewDatabase()
    db.Credentials = gokeepasslib.NewPasswordCredentials("test1234")
    err := gokeepasslib.NewDecoder(file).Decode(db)
    if err != nil {
        log.Fatal(err)
    }

    db.UnlockProtectedEntries()

    // Note: This is a simplified example and the groups and entries will depend on the specific file.
    // bound checking for the slices is recommended to avoid panics.
    entry := db.Content.Root.Groups[0].Entries[0]
    fmt.Println(entry.GetTitle())
    fmt.Println(entry.GetPassword())
}

The subtle difference is whether you are using github.com/tobischo/gokeepasslib or github.com/tobischo/gokeepasslib/v3

philxws692 commented 1 month ago

Ahh perfect, thanks for your fast response, I changed it to github.com/tobischo/gokeepasslib/v3 and now it's working fine. Did not thought that this would make such a big difference šŸ˜…

Thank you very much

tobischo commented 1 month ago

Generally it shouldn't have made a difference, so I think that was a valid assumption.

However, when I put the very first version of this library onto github, it only supported format version 3.1 as that was what I was using at the time. Before adding support for kdbx 4.0 some other breaking changes were introduced, resulting in major version changes šŸ˜…

Glad to read that it is working now