-
Notifications
You must be signed in to change notification settings - Fork 581
/
ssh.go
145 lines (122 loc) · 3.69 KB
/
ssh.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
package util
/*
Copyright 2019 - 2020 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"bytes"
"crypto/ed25519"
"encoding/pem"
"math/rand"
"golang.org/x/crypto/ssh"
)
// SSHKey stores byte slices that represent private and public ssh keys
type SSHKey struct {
Private []byte
Public []byte
}
// NewPrivatePublicKeyPair generates a an ed25519 ssh private and public key
func NewPrivatePublicKeyPair() (SSHKey, error) {
var keys SSHKey
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return SSHKey{}, err
}
keys.Public, err = newPublicKey(pub)
if err != nil {
return SSHKey{}, err
}
keys.Private, err = newPrivateKey(priv)
if err != nil {
return SSHKey{}, err
}
return keys, nil
}
// newPublicKey generates a byte slice containing an public key that can be used
// to ssh. This key is based off of the ed25519.PublicKey type The function is
// only used by NewPrivatePublicKeyPair
func newPublicKey(key ed25519.PublicKey) ([]byte, error) {
pubKey, err := ssh.NewPublicKey(key)
if err != nil {
return nil, err
}
return ssh.MarshalAuthorizedKey(pubKey), nil
}
// newPrivateKey generates a byte slice containing an OpenSSH private ssh key.
// This key is based off of the ed25519.PrivateKey type. The function is only
// used by NewPrivatePublicKeyPair
func newPrivateKey(key ed25519.PrivateKey) ([]byte, error) {
// The following link describes the private key format for OpenSSH. It
// oulines the structs that are used to generate the OpenSSH private key
// from the ed25519 private key
// https://anongit.mindrot.org/openssh.git/tree/PROTOCOL.key?h=V_8_1_P1
const authMagic = "openssh-key-v1"
const noneCipherBlockSize = 8
private := struct {
Check1 uint32
Check2 uint32
KeyType string
Public []byte
Private []byte
Comment string
Pad []byte `ssh:"rest"`
}{
KeyType: ssh.KeyAlgoED25519,
Public: key.Public().(ed25519.PublicKey),
Private: key,
}
// check fields should match to easily verify
// that a decryption was successful
private.Check1 = rand.Uint32()
private.Check2 = private.Check1
{
bsize := noneCipherBlockSize
plen := len(ssh.Marshal(private))
private.Pad = make([]byte, bsize-(plen%bsize))
}
// The list of privatekey/comment pairs is padded with the
// bytes 1, 2, 3, ... until the total length is a multiple
// of the cipher block size.
for i := range private.Pad {
private.Pad[i] = byte(i) + 1
}
public := struct {
Keytype string
Public []byte
}{
Keytype: ssh.KeyAlgoED25519,
Public: private.Public,
}
// The overall key consists of a header, a list of public keys, and
// an encrypted list of matching private keys.
overall := struct {
CipherName string
KDFName string
KDFOpts string
NumKeys uint32
PubKey []byte
PrivKeyBlock []byte
}{
CipherName: "none", KDFName: "none", // unencrypted
NumKeys: 1,
PubKey: ssh.Marshal(public),
PrivKeyBlock: ssh.Marshal(private),
}
pemBlock := &pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: append(append([]byte(authMagic), 0), ssh.Marshal(overall)...),
}
var privateKeyPEM bytes.Buffer
if err := pem.Encode(&privateKeyPEM, pemBlock); err != nil {
return nil, err
}
return privateKeyPEM.Bytes(), nil
}