-
Notifications
You must be signed in to change notification settings - Fork 0
/
cData.go
151 lines (128 loc) · 3.54 KB
/
cData.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/DataManager-Go/DataManagerCLI/commands"
libdm "github.com/DataManager-Go/libdatamanager"
)
// Generates a commands.Commanddata object based on the cli parameter
func buildCData(parsed string, appTrimName int) *commands.CommandData {
// Command data
commandData := commands.CommandData{
Command: parsed,
Config: config,
Details: uint8(*appDetails),
FileAttributes: libdm.FileAttributes{
Namespace: *appNamespace,
Groups: *appGroups,
Tags: *appTags,
},
Namespace: *appNamespace,
All: *appAll,
NoRedaction: *appNoRedaction,
OutputJSON: *appOutputJSON,
Yes: *appYes,
Force: *appForce,
NameLen: appTrimName,
Encryption: *appFileEncryption,
NoDecrypt: *appNoDecrypt,
NoEmojis: *appNoEmojis,
RandKey: *appFileEncrRandKey,
Quiet: *appQuiet,
VerifyFile: *appVerify,
UnmodifiedNamespace: unmodifiedNS,
Compression: *appDisableCompression,
Extract: *appDecompress,
}
// Init cdata
if !commandData.Init() {
return nil
}
// Initialize encryption sources
return initInputKey(commandData)
}
// ----- Init en/decryption ------
func initInputKey(cData commands.CommandData) *commands.CommandData {
// --> RandKey
randKeySize := *appFileEncrRandKey
if randKeySize > 0 && cData.RequestedEncryptionInput() {
// Check correct keylen for given encryption
switch *appFileEncryption {
case libdm.EncryptionCiphers[0]:
// AES
if !vaildAESkeylen(randKeySize) {
fmt.Printf("The keysize %d is invalid\n", randKeySize)
return nil
}
// TODO add age key generation
}
// Generate key
err := initRandomKey(&cData)
if err != nil {
log.Fatal(err)
}
}
// --> Stdin
if *appFileEncrKeyFromStdin {
cData.EncryptionKey = readStdinWithTimeout(48)
}
// TODO password
// --> Keyfile
encrKeyFile := *appFileEncrKeyFile
if len(encrKeyFile) > 0 {
initKeyfile(encrKeyFile, &cData)
}
// FlagInput --key
if len(*appFileEncrKey) > 0 {
cData.EncryptionKey = []byte(*appFileEncrKey)
switch *appFileEncryption {
case libdm.EncryptionCiphers[0]:
if !vaildAESkeylen(randKeySize) {
fmt.Printf("The keysize %d is invalid\n", len(*appFileEncrKey))
return nil
}
case libdm.EncryptionCiphers[1]:
if len(*appFileEncrKey) != 62 {
fmt.Printf("The key \"%s\" is invalid (Invalid keysize)\n", *appFileEncrKey)
if strings.HasPrefix(*appFileEncrKey, "/") || strings.HasPrefix(*appFileEncrKey, "~/") || strings.HasPrefix(*appFileEncrKey, "./") {
fmt.Println("\nDid you want to pass a file? use --keyfile")
}
return nil
}
}
}
return &cData
}
// Generate and save a random key
func initRandomKey(cData *commands.CommandData) error {
// Generate a random key
cData.EncryptionKey = randKey(cData.RandKey)
path := "./"
// use keystorepath if keystore is enabled
if keystore, _ := cData.GetKeystore(); keystore != nil {
path = keystore.Path
}
// Generate file and save key
cData.Keyfile = genFile(path, "key")
return ioutil.WriteFile(cData.Keyfile, cData.EncryptionKey, 0600)
}
// Read keyfile to cData.EncryptionKey
func initKeyfile(encrKeyFile string, cData *commands.CommandData) {
if !fileExists(encrKeyFile) {
log.Fatal("Keyfile does not exists!")
}
// Read key
var err error
cData.EncryptionKey, err = ioutil.ReadFile(filepath.Clean(encrKeyFile))
if err != nil {
log.Fatal(err)
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}