forked from gokyle/sshkeygen
/
keygen.go
201 lines (174 loc) · 4.2 KB
/
keygen.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"flag"
"fmt"
"github.com/gokyle/sshkey"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
func ReadPrompt(prompt string) (in string, err error) {
fmt.Printf("%s", prompt)
rd := bufio.NewReader(os.Stdin)
line, err := rd.ReadString('\n')
if err != nil {
return
}
in = strings.TrimSpace(line)
return
}
func checkKeyType(keytype string) {
switch keytype {
case "rsa":
return
case "ecdsa":
return
default:
fmt.Println("bad key type")
os.Exit(1)
}
}
func checkErr(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func main() {
flComment := flag.String("c", "", "key comment")
flFile := flag.String("f", "", "input keyfile")
flFpr := flag.Bool("l", false, "show fingerprint of keyfile")
flType := flag.String("t", "ecdsa", "type of key to create")
flSize := flag.Int("b", 256, "bitsize of key to create")
flag.Parse()
checkKeyType(*flType)
if *flComment == "" {
*flComment = getDefaultComment()
}
if *flType == "rsa" && *flSize == 256 {
*flSize = 2048
}
if *flFpr {
fingerprint(*flFile, *flType)
} else {
keygen(*flFile, *flType, *flComment, *flSize)
}
}
func defaultKey(keytype string) string {
home := os.Getenv("HOME")
defkey := filepath.Join(home, ".ssh", "id_"+keytype)
return defkey
}
func fingerprint(keyfile string, keytype string) {
var err error
if keyfile == "" {
defkey := defaultKey(keytype)
defkey += ".pub"
prompt := fmt.Sprintf("Enter file in which the key is (%s): ", defkey)
keyfile, err = ReadPrompt(prompt)
checkErr(err)
if keyfile == "" {
keyfile = defkey
}
}
pub, err := sshkey.LoadPublicKeyFile(keyfile, true)
checkErr(err)
fpr, err := sshkey.FingerprintPretty(pub, 0)
checkErr(err)
comment := pub.Comment
if comment == "" {
comment = keyfile
}
fmt.Printf("%d %s %s (%s)\n", pub.Size(), fpr, comment, strings.ToUpper(keytype))
os.Exit(0)
}
func keygen(keyfile, keytype, comment string, size int) {
var (
err error
key interface{}
)
switch keytype {
case "rsa":
fmt.Println("Generating public/private rsa key pair.")
key, err = sshkey.GenerateKey(sshkey.KEY_RSA, size)
checkErr(err)
case "ecdsa":
fmt.Println("Generating public/private ecdsa key pair.")
key, err = sshkey.GenerateKey(sshkey.KEY_ECDSA, size)
checkErr(err)
}
if keyfile == "" {
defkey := defaultKey(keytype)
prompt := fmt.Sprintf("Enter file in which to save the key (%s): ", defkey)
keyfile, err = ReadPrompt(prompt)
checkErr(err)
if keyfile == "" {
keyfile = defkey
}
}
if _, err = os.Stat(keyfile); !os.IsNotExist(err) {
fmt.Printf("%s already exists.\n", keyfile)
yn, err := ReadPrompt("Overwrite (y/n)? ")
checkErr(err)
if strings.ToUpper(string(yn[0])) != "Y" {
os.Exit(1)
}
}
var password string
for {
password, err = sshkey.PasswordPrompt("Enter password (empty for no passphrase): ")
checkErr(err)
if password != "" {
var password2 string
password2, err = sshkey.PasswordPrompt("Enter password again: ")
checkErr(err)
if password != password2 {
fmt.Println("Passphrases do not match. Try again.")
continue
}
break
} else {
break
}
}
if password != "" && len(password) < 5 {
fmt.Printf("passphrase too short: have %d bytes, need > 4\n",
len(password))
fmt.Println("Saving the key failed:", keyfile)
os.Exit(1)
}
privout, err := sshkey.MarshalPrivate(key, password)
checkErr(err)
pub := sshkey.NewPublic(key, comment)
if key == nil {
fmt.Println("Failed to create a public key.")
os.Exit(1)
}
pubout := sshkey.MarshalPublic(pub)
if pubout == nil {
fmt.Println("Failed to create a public key.")
os.Exit(1)
}
err = ioutil.WriteFile(keyfile, privout, 0600)
checkErr(err)
err = ioutil.WriteFile(keyfile+".pub", pubout, 0644)
checkErr(err)
}
func getDefaultComment() string {
cmd := exec.Command("whoami")
out, err := cmd.Output()
if err != nil {
return ""
}
whoami := strings.TrimSpace(string(out))
cmd = exec.Command("hostname")
out, err = cmd.Output()
if err != nil {
return ""
}
hostname := strings.TrimSpace(string(out))
return fmt.Sprintf("%s@%s", whoami, hostname)
}