-
Notifications
You must be signed in to change notification settings - Fork 4
/
splitshare.go
180 lines (150 loc) · 4.07 KB
/
splitshare.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/SSSaaS/sssa-golang"
"github.com/TheZ3ro/go-pgp/pgp"
"golang.org/x/crypto/openpgp"
"github.com/go-mail/mail"
"github.com/spf13/viper"
)
type User struct {
keypath string
mail string
share string
}
type MailConf struct {
server string
port int
username string
password string
subject string
sender string
}
func splitSecret(secret []byte) []string {
var minShares int
var totalShares int
fmt.Print("Insert Number of Slices: ")
fmt.Scan(&totalShares)
fmt.Print("Insert Minumim number of Slices to decrypt the secret: ")
fmt.Scan(&minShares)
if (minShares > totalShares) || (minShares <= 0) {
fmt.Println("The minimum nuber of Slices must be lower than the total number of Slices")
os.Exit(1)
}
fmt.Println("[+] Generating Slices")
shares, err := sssa.Create(minShares, totalShares, string(secret))
if err != nil {
fmt.Println("Unable to create Shamir's Shares")
os.Exit(1)
}
return shares
}
func registerUsers(users []User, share string, useKeyring bool) []User {
newUser := User{}
newUser.share = share
fmt.Print("Enter email: ")
fmt.Scanln(&newUser.mail)
if useKeyring {
newUser.keypath = ""
} else {
fmt.Print("Enter keypath: ")
fmt.Scanln(&newUser.keypath)
}
users = append(users, newUser)
return users
}
func encryptPassword(person User, conf MailConf, publicKeyring openpgp.EntityList) {
fmt.Println("[+] Encrypting message for " + person.mail)
var pubEntity *openpgp.Entity = nil
if publicKeyring != nil {
pubEntity = pgp.GetKeyByEmail(publicKeyring, string(person.mail))
} else {
key, err := ioutil.ReadFile(person.keypath)
if err != nil {
fmt.Println("Unable to read keyfile " + person.keypath)
os.Exit(1)
}
pubEntity, err = pgp.GetEntity(key, []byte{})
if err != nil {
fmt.Println("Unable to generate public Entity")
os.Exit(1)
}
}
if pubEntity == nil {
fmt.Println("Unable to load public key for entity:", person.mail)
os.Exit(1)
}
fingerprint := pgp.GetFingerprint(pubEntity)
fmt.Println("[+] Selected key with fingerprint:", fingerprint)
encrypted, err := pgp.Encrypt(pubEntity, []byte(person.share))
if err != nil {
fmt.Println("Unable to encrypt secret")
os.Exit(1)
}
fmt.Println("[+] Sending mail to: " + person.mail)
sendViaMail(person, string(encrypted), conf)
}
func sendViaMail(person User, message string, conf MailConf) {
m := mail.NewMessage()
m.SetHeader("From", conf.sender)
m.SetHeader("To", person.mail)
m.SetHeader("Subject", conf.subject)
m.SetBody("text/html", message)
d := mail.NewDialer(conf.server, conf.port, conf.username, conf.password)
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
func initConfig(conf MailConf) MailConf {
viper.SetConfigType("toml")
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
conf.server = viper.GetString("SMTP.server")
conf.port = viper.GetInt("SMTP.port")
conf.username = viper.GetString("SMTP.username")
conf.password = viper.GetString("SMTP.password")
conf.subject = viper.GetString("MAIL.subject")
conf.sender = viper.GetString("MAIL.sender")
return conf
}
func main() {
users := []User{}
conf := MailConf{}
var publicKeyring string
flag.StringVar(&publicKeyring, "pub-keyring", "", "the keyring path")
flag.Parse()
fmt.Println(publicKeyring)
if len(flag.Args()) != 1 {
fmt.Println("You must specify the secret file")
os.Exit(1)
}
secretFile := flag.Arg(0)
secret, err := ioutil.ReadFile(secretFile)
if err != nil {
panic("Unable to read secret file")
}
shares := splitSecret(secret)
fmt.Println("[+] Adding Users")
for _, share := range shares {
users = registerUsers(users, share, publicKeyring != "")
}
conf = initConfig(conf)
var keyring openpgp.EntityList = nil
if publicKeyring != "" {
keyring, err = pgp.OpenKeyring(publicKeyring)
if err != nil {
fmt.Println("Error loading keyring: %s", err)
os.Exit(1)
}
}
for _, person := range users {
encryptPassword(person, conf, keyring)
}
}