-
Notifications
You must be signed in to change notification settings - Fork 3
/
sshrsa.go
103 lines (88 loc) · 2.15 KB
/
sshrsa.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
package main
import (
"bufio"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
"reflect"
"github.com/totallylegitbiz/sshcrypt/gopass"
"golang.org/x/crypto/ssh"
)
// ParsePrivateKey returns a private key from a PEM encoded private key. It
// supports RSA (PKCS#1)
func ParsePrivateRSAKey(pemBytes []byte) (key *rsa.PrivateKey, err error) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("ssh: no key found")
}
switch block.Type {
case "RSA PRIVATE KEY":
if procType, ok := block.Headers["Proc-Type"]; ok && procType == "4,ENCRYPTED" {
keyPass, err := gopass.GetPass("Password: ")
if err != nil {
panic(err)
}
decryptedPemBtyes, err := x509.DecryptPEMBlock(block, []byte(keyPass))
if err != nil {
panic(err)
}
return x509.ParsePKCS1PrivateKey(decryptedPemBtyes)
}
return x509.ParsePKCS1PrivateKey(block.Bytes)
default:
return nil, fmt.Errorf("ssh: unsupported key type %q", block.Type)
}
}
func ParseAuthorizedRSAKey(in []byte) (out *rsa.PublicKey, err error) {
key, _, _, _, err := ssh.ParseAuthorizedKey(in)
if err != nil {
return nil, err
}
if key.Type() != "ssh-rsa" {
return nil, fmt.Errorf("%q is not an rsa key", key.Type())
}
retTyp := reflect.TypeOf(out)
retVal := reflect.ValueOf(key).Convert(retTyp)
out = retVal.Interface().(*rsa.PublicKey)
return out, nil
}
func openPubKey(path string) (out []*rsa.PublicKey, err error) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
key, err := ParseAuthorizedRSAKey(scanner.Bytes())
if err != nil {
fmt.Printf("Could not read key")
continue
}
out = append(out, key)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
return out, err
}
func openPrivKey(path string) (key *rsa.PrivateKey, err error) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
buf := make([]byte, fileInfo.Size())
_, err = file.Read(buf)
if err != nil {
panic(err)
}
return ParsePrivateRSAKey(buf)
}