-
Notifications
You must be signed in to change notification settings - Fork 25
/
key.go
290 lines (261 loc) · 7.54 KB
/
key.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package libssh
/*
#cgo pkg-config: libssh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <libssh/libssh.h>
struct pubkey_hash {
unsigned char *buffer;
size_t length;
int error;
};
int get_publickey_hash(const ssh_key key, enum ssh_publickey_hash_type type, struct pubkey_hash *hash) {
return ssh_get_publickey_hash(key, type, &hash->buffer, &hash->length);
}
char *export_base64_from_pubkey(const ssh_key key) {
char *pbuf;
if (ssh_pki_export_pubkey_base64(key, &pbuf) == SSH_OK) {
return pbuf;
}
return NULL;
}
*/
import "C"
import "errors"
type Key struct {
key C.ssh_key
}
func NewKey() (Key, error) {
k := Key{}
k.key = C.ssh_key_new()
if k.key == nil {
return k, errors.New("Unable allocate a key")
}
return k, nil
}
// Import a base64 formated certificate from a memory c-string.
//
// certType:
// SSH_KEYTYPE_UNKNOWN
// SSH_KEYTYPE_DSS
// SSH_KEYTYPE_RSA
// SSH_KEYTYPE_RSA1
// SSH_KEYTYPE_ECDSA
// SSH_KEYTYPE_ED25519
// SSH_KEYTYPE_DSS_CERT01
// SSH_KEYTYPE_RSA_CERT01
func ImportFromBase64(base64Cert string, certType int) (Key, error) {
base64Cert_cstr := CString(base64Cert)
defer base64Cert_cstr.Free()
key := Key{}
if C.ssh_pki_import_cert_base64(base64Cert_cstr.Ptr, C.enum_ssh_keytypes_e(certType), &key.key) == SSH_OK {
return key, nil
}
return key, errors.New("ssh_pki_import_cert_base64() != SSH_OK")
}
// Import a certificate from the given filename.
func ImportFromFile(certFile string) (Key, error) {
certFile_cstr := CString(certFile)
defer certFile_cstr.Free()
var key *C.ssh_key
if C.ssh_pki_import_cert_file(certFile_cstr.Ptr, key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("ssh_pki_import_cert_file != SSH_OK")
}
// import a base64 formated key from a memory c-string
//
// base64:
// The c-string holding the base64 encoded key
// passphrase:
// The passphrase to decrypt the key, or ""
func ImportPrivateKeyFromBase64(base64, passphrase string) (Key, error) {
base64_cstr := CString(base64)
defer base64_cstr.Free()
var passphrase_cstr *C.char = nil
if passphrase != "" {
cstr := CString(passphrase)
defer cstr.Free()
passphrase_cstr = cstr.Ptr
}
var key *C.ssh_key
if C.ssh_pki_import_privkey_base64(base64_cstr.Ptr, passphrase_cstr, nil, nil, key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("ssh_pki_import_privkey_base64() != SSH_OK")
}
func ImportPrivateKeyFromFile(filename, passphrase string) (Key, error) {
filename_cstr := CString(filename)
defer filename_cstr.Free()
var passphrase_cstr *C.char = nil
if passphrase != "" {
cstr := CString(passphrase)
defer cstr.Free()
passphrase_cstr = cstr.Ptr
}
var key *C.ssh_key
if C.ssh_pki_import_privkey_file(filename_cstr.Ptr, passphrase_cstr, nil, nil, key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("ssh_pki_import_privkey_file() != SSH_OK")
}
func ImportPublicKeyFromBase64(base64 string, keyType int) (Key, error) {
base64_cstr := CString(base64)
defer base64_cstr.Free()
var key *C.ssh_key
if C.ssh_pki_import_pubkey_base64(base64_cstr.Ptr, C.enum_ssh_keytypes_e(keyType), key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("ssh_pki_import_pubkey_base64() != SSH_OK")
}
func ImportPublicKeyFromFile(filename string) (Key, error) {
filename_cstr := CString(filename)
defer filename_cstr.Free()
var key *C.ssh_key
if C.ssh_pki_import_pubkey_file(filename_cstr.Ptr, key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("ssh_pki_import_pubkey_file() != SSH_OK")
}
// Allocates a buffer with the hash of the public key.
//
// This function allows you to get a hash of the public key. You can then print
// this hash in a human-readable form to the user so that he is able to verify
// it. Use ssh_get_hexa() or ssh_print_hexa() to display it.
//
// typ:
// The type of the hash you want:
// SSH_PUBLICKEY_HASH_SHA1
// SSH_PUBLICKEY_HASH_MD5
//
func (k Key) Hash(typ int) ([]byte, error) {
hash := &C.struct_pubkey_hash{}
if C.get_publickey_hash(k.key, C.enum_ssh_publickey_hash_type(typ), hash) < 0 {
return nil, errors.New("Key hash error")
}
defer C.ssh_clean_pubkey_hash(&hash.buffer)
return copyData(hash.buffer, hash.length), nil
}
// clean up the key and deallocate all existing keys
/*func (k Key) Clean() {
C.ssh_key_clean(k.key)
}*/
// deallocate a SSH key
func (k Key) Free() {
C.ssh_key_free(k.key)
}
// Compare keys if they are equal.
//
// what:
// What part or type of the key do you want to compare
// SSH_KEY_CMP_PUBLIC
// SSH_KEY_CMP_PRIVATE
func (k Key) Compare(k2 Key, what int) bool {
return C.ssh_key_cmp(k.key, k2.key, C.enum_ssh_keycmp_e(what)) == 0
}
// Check if the key is a private key.
func (k Key) IsPrivate() bool {
return C.ssh_key_is_private(k.key) == 1
}
// Check if the key has/is a public key.
func (k Key) IsPublic() bool {
return C.ssh_key_is_public(k.key) == 1
}
// Get the type of a ssh key:
// SSH_KEYTYPE_RSA
// SSH_KEYTYPE_DSS
// SSH_KEYTYPE_RSA1
// SSH_KEYTYPE_UNKNOWN
func (k Key) Type() int {
return int(C.ssh_key_type(k.key))
}
func KeyTypeFromName(name string) int {
name_cstr := CString(name)
defer name_cstr.Free()
return int(C.ssh_key_type_from_name(name_cstr.Ptr))
}
func KeyTypeString(typ int) string {
s := C.ssh_key_type_to_char(C.enum_ssh_keytypes_e(typ))
if s != nil {
return C.GoString(s)
}
return "unknown"
}
func (k Key) TypeString() string {
return KeyTypeString(k.Type())
}
// Copy the certificate part of a public key into a private key.
func (k Key) CopyCertToPrivateKey(privateKey Key) error {
if C.ssh_pki_copy_cert_to_privkey(k.key, privateKey.key) != SSH_OK {
return errors.New("ssh_pki_copy_cert_to_privkey error")
}
return nil
}
// Export a private key to a pem file on disk, or OpenSSH format for keytype
// ssh-ed25519.
//
// passphrase:
// The passphrase to use to encrypt the key with or NULL. An empty
// string means no passphrase.
//
// permFile:
// The path where to store the pem file
func (k Key) ExportPrivateKeyToFile(passphrase, permFile string) error {
var passphrase_cstr *C.char = nil
if passphrase != "" {
cstr := CString(passphrase)
defer cstr.Free()
passphrase_cstr = cstr.Ptr
}
filename := CString(permFile)
defer filename.Free()
if C.ssh_pki_export_privkey_file(k.key, passphrase_cstr, nil, nil, filename.Ptr) != SSH_OK {
return errors.New("ssh_pki_export_privkey_file failed")
}
return nil
}
// Create a public key from a private key.
func (k Key) ExportAsPublicKey() (Key, error) {
var key *C.ssh_key
if C.ssh_pki_export_privkey_to_pubkey(k.key, key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("Unable to export as a public key")
}
// Convert a public key to a base64 encoded key.
func (k Key) ExportPubkeyBase64() string {
if s := C.export_base64_from_pubkey(k.key); s != nil {
defer C.ssh_string_free_char(s)
return C.GoString(s)
}
return ""
}
// Generates a keypair.
//
// keyType:
// SSH_KEYTYPE_UNKNOWN
// SSH_KEYTYPE_DSS
// SSH_KEYTYPE_RSA
// SSH_KEYTYPE_RSA1
// SSH_KEYTYPE_ECDSA
// SSH_KEYTYPE_ED25519
// SSH_KEYTYPE_DSS_CERT01
// SSH_KEYTYPE_RSA_CERT01
//
// parameter:
// Parameter to the creation of key: rsa : length of the key in bits (e.g.
// 1024, 2048, 4096) dsa : length of the key in bits (e.g. 1024, 2048, 3072)
// ecdsa : bits of the key (e.g. 256, 384, 512)
//
// Warning:
// Generating a key pair may take some time.
func GenerateKeyPair(keyType, parameter int) (Key, error) {
var key *C.ssh_key
if C.ssh_pki_generate(C.enum_ssh_keytypes_e(keyType), C.int(parameter), key) == SSH_OK {
return Key{*key}, nil
}
return Key{}, errors.New("Unable to gernerate key pair")
}