-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoding.go
More file actions
107 lines (77 loc) · 2.65 KB
/
encoding.go
File metadata and controls
107 lines (77 loc) · 2.65 KB
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
package ssh
import (
"fmt"
"bytes"
"strings"
"encoding/binary"
"encoding/base64"
"crypto/rsa"
"math/big"
)
// ssh one-line format (for lack of a better term) consists of three text fields: { key_type, data, comment }
// data is base64 encoded binary which consists of tuples of length (4 bytes) and data of the length described previously.
// For RSA keys, there should be three tuples which should be: { key_type, public_exponent, modulus }
func EncodePublicKey(key interface{}, comment string) (string, error) {
if rsaKey, ok := key.(rsa.PublicKey); ok {
key_type := "ssh-rsa"
modulus_bytes := rsaKey.N.Bytes()
buf := new(bytes.Buffer)
var data = []interface{} {
uint32(len(key_type)),
[]byte(key_type),
uint32(binary.Size(uint32(rsaKey.E))),
uint32(rsaKey.E),
uint32(binary.Size(modulus_bytes)),
modulus_bytes,
}
for _, v := range data {
err := binary.Write(buf, binary.BigEndian, v)
if err != nil { return "", err }
}
return fmt.Sprintf("%s %s %s", key_type, base64.StdEncoding.EncodeToString(buf.Bytes()), comment), nil
}
return "", fmt.Errorf("Unknown key type: %T\n", key)
}
func readLength(data []byte) ([]byte, uint32, error) {
l_buf := data[0:4]
buf := bytes.NewBuffer(l_buf)
var length uint32
err := binary.Read(buf, binary.BigEndian, &length)
if err != nil { return nil, 0, err }
return data[4:], length, nil
}
func readBigInt(data []byte, length uint32) ([]byte, *big.Int, error) {
var bigint = new(big.Int)
bigint.SetBytes(data[0:length])
return data[length:], bigint, nil
}
func getRsaValues(data []byte) (format string, e *big.Int, n *big.Int, err error) {
data, length, err := readLength(data)
if err != nil { return }
format = string(data[0:length]); data = data[length:]
data, length, err = readLength(data)
if err != nil { return }
data, e, err = readBigInt(data, length)
if err != nil { return }
data, length, err = readLength(data)
if err != nil { return }
data, n, err = readBigInt(data, length)
if err != nil { return }
return
}
func DecodePublicKey(str string) (interface{}, error) {
// comes in as a three part string
// split into component parts
tokens := strings.Split(str, " ")
if len(tokens) < 2 { return nil, fmt.Errorf("Invalid key format; must contain at least two fields (keytype data [comment])") }
key_type := tokens[0]
data, err := base64.StdEncoding.DecodeString(tokens[1])
if err != nil { return nil, err }
format, e, n, err := getRsaValues(data)
if format != key_type { return nil, fmt.Errorf("Key type said %s, but encoded format said %s. These should match!", key_type, format) }
pubKey := &rsa.PublicKey{
N: n,
E: int(e.Int64()),
}
return pubKey, nil
}