gocryptfs-xray: add function to dump the master key

Fixes https://github.com/rfjakob/gocryptfs/issues/83
This commit is contained in:
Jakob Unterwurzacher 2017-05-28 17:11:50 +02:00
parent c36a55e985
commit 7f5ae5f843
1 changed files with 31 additions and 3 deletions

View File

@ -7,13 +7,16 @@ import (
"io"
"os"
"github.com/rfjakob/gocryptfs/internal/configfile"
"github.com/rfjakob/gocryptfs/internal/contentenc"
"github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/readpassword"
)
const (
ivLen = contentenc.DefaultIVBits / 8
blockSize = contentenc.DefaultBS + ivLen + cryptocore.AuthTagLen
myName = "gocryptfs-xray"
)
func errExit(err error) {
@ -27,17 +30,42 @@ func prettyPrintHeader(h *contentenc.FileHeader) {
}
func main() {
dumpmasterkey := flag.Bool("dumpmasterkey", false, "Decrypt and dump the master key")
flag.Parse()
if flag.NArg() != 1 {
fmt.Printf("Usage: xray FILE\n")
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] FILE\n"+
"\n"+
"Options:\n", myName)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n"+
"Examples:\n"+
" gocryptfs-xray myfs/mCXnISiv7nEmyc0glGuhTQ\n"+
" gocryptfs-xray -dumpmasterkey myfs/gocryptfs.conf\n")
os.Exit(1)
}
f := flag.Arg(0)
fd, err := os.Open(f)
fn := flag.Arg(0)
fd, err := os.Open(fn)
if err != nil {
errExit(err)
}
defer fd.Close()
if *dumpmasterkey {
dumpMasterKey(fn)
} else {
inspectCiphertext(fd)
}
}
func dumpMasterKey(fn string) {
pw := readpassword.Once("")
masterkey, _, err := configfile.LoadConfFile(fn, pw)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(hex.EncodeToString(masterkey))
}
func inspectCiphertext(fd *os.File) {
headerBytes := make([]byte, contentenc.HeaderLen)
n, err := fd.ReadAt(headerBytes, 0)
if err == io.EOF && n == 0 {