-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
255 lines (217 loc) · 4.81 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"github.com/dropbox/godropbox/errors"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/mgo.v2/bson"
"hash/fnv"
"io/ioutil"
"strings"
"syscall"
"time"
)
type ReadError struct {
errors.DropboxError
}
type WriteError struct {
errors.DropboxError
}
type ParseError struct {
errors.DropboxError
}
type importData struct {
Keys []string `json:"keys"`
}
func ParsePemKey(pass string, data string) (key crypto.PrivateKey, err error) {
encBlock, _ := pem.Decode([]byte(data))
blockBytes, err := x509.DecryptPEMBlock(encBlock, []byte(pass))
if err != nil {
err = &ParseError{
errors.Wrap(err, "main: Failed to decrypt key"),
}
return
}
switch encBlock.Type {
case "RSA PRIVATE KEY":
key, err = x509.ParsePKCS1PrivateKey(blockBytes)
if err != nil {
err = &ParseError{
errors.Wrap(err, "main: Failed to parse rsa key"),
}
return
}
break
case "EC PRIVATE KEY":
key, err = x509.ParseECPrivateKey(blockBytes)
if err != nil {
err = &ParseError{
errors.Wrap(err, "main: Failed to parse ec key"),
}
return
}
break
default:
err = &ParseError{
errors.Newf("main: Unknown key type '%s'", encBlock.Type),
}
return
}
return
}
func MarshalCertificate(cert *ssh.Certificate, comment string) []byte {
b := &bytes.Buffer{}
b.WriteString(cert.Type())
b.WriteByte(' ')
e := base64.NewEncoder(base64.StdEncoding, b)
e.Write(cert.Marshal())
e.Close()
b.WriteByte(' ')
b.Write([]byte(comment))
return b.Bytes()
}
func CreateCertificate(pass string, privKey string, sshPubKey string,
roles []string) (certMarshaled string, err error) {
privateKey, err := ParsePemKey(pass, privKey)
if err != nil {
return
}
pubKey, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(sshPubKey))
if err != nil {
err = &ParseError{
errors.Wrap(err, "authority: Failed to parse ssh public key"),
}
return
}
serialHash := fnv.New64a()
serialHash.Write([]byte(bson.NewObjectId().Hex()))
serial := serialHash.Sum64()
validAfter := time.Now().Add(-5 * time.Minute).Unix()
validBefore := time.Now().Add(30 * time.Minute).Unix()
cert := &ssh.Certificate{
Key: pubKey,
Serial: serial,
CertType: ssh.UserCert,
KeyId: "emergency",
ValidPrincipals: roles,
ValidAfter: uint64(validAfter),
ValidBefore: uint64(validBefore),
Permissions: ssh.Permissions{
Extensions: map[string]string{
"permit-X11-forwarding": "",
"permit-agent-forwarding": "",
"permit-port-forwarding": "",
"permit-pty": "",
"permit-user-rc": "",
},
},
}
signer, err := ssh.NewSignerFromKey(privateKey)
if err != nil {
return
}
err = cert.SignCert(rand.Reader, signer)
if err != nil {
return
}
certMarshaled = string(MarshalCertificate(cert, comment))
return
}
func Certificate() (err error) {
importPath := flag.Arg(0)
sshPubPath := flag.Arg(1)
if importPath == "" {
err = &ReadError{
errors.Wrap(err, "main: Missing import path"),
}
return
}
if sshPubPath == "" {
err = &ReadError{
errors.Wrap(err, "main: Missing SSH public key path"),
}
return
}
roles := []string{}
args := flag.Args()
for _, arg := range args[2:] {
roles = append(roles, arg)
}
if len(roles) == 0 {
roles = []string{"emergency"}
}
fmt.Print("Enter encryption passphrase: ")
passByt, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
err = &ReadError{
errors.Wrap(err, "main: Failed to read passphrase"),
}
return
}
pass := string(passByt)
fmt.Println("")
marhData, err := ioutil.ReadFile(importPath)
if err != nil {
err = &ReadError{
errors.Wrap(err, "main: Failed to read import file"),
}
return
}
sshPubKeyByt, err := ioutil.ReadFile(sshPubPath)
if err != nil {
err = &ReadError{
errors.Wrap(err, "main: Failed to read import file"),
}
return
}
sshPubKey := string(sshPubKeyByt)
data := &importData{}
err = json.Unmarshal(marhData, data)
if err != nil {
err = &ParseError{
errors.Wrap(err, "main: Failed to parse import file"),
}
return
}
certs := []string{}
for _, key := range data.Keys {
cert, e := CreateCertificate(
pass,
key,
sshPubKey,
roles,
)
if e != nil {
err = e
return
}
certs = append(certs, cert)
}
certsStr := strings.Join(certs, "\n")
sshCertPath := strings.Replace(sshPubPath, ".pub", "-cert.pub", 1)
err = ioutil.WriteFile(sshCertPath, []byte(certsStr), 0644)
if err != nil {
err = &WriteError{
errors.Wrapf(err,
"main: Failed to write ssh certificate at %s", sshCertPath),
}
return
}
fmt.Printf("Wrote SSH certificate to %s\n", sshCertPath)
return
}
func main() {
flag.Parse()
err := Certificate()
if err != nil {
panic(err)
}
}