-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
130 lines (114 loc) · 3.18 KB
/
parse.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
// Copyright (C) 2023 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package rsautil
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"golang.org/x/crypto/ssh"
"shanhu.io/g/errcode"
"shanhu.io/g/osutil"
"shanhu.io/g/termutil"
)
var (
errNotRSA = errcode.InvalidArgf("public key is not an RSA key")
errNoKey = errcode.InvalidArgf("no key")
)
// ParsePrivateKey parses the PEM encoded RSA private key.
func ParsePrivateKey(bs []byte) (*rsa.PrivateKey, error) {
if len(bs) == 0 {
return nil, errNoKey
}
b, _ := pem.Decode(bs)
if b == nil {
return nil, errcode.InvalidArgf("pem decode failed")
}
if x509.IsEncryptedPEMBlock(b) {
return nil, errcode.InvalidArgf("key is encrypted")
}
return x509.ParsePKCS1PrivateKey(b.Bytes)
}
// ReadPrivateKey parses the PEM encded RSA private key file.
func ReadPrivateKey(f string) (*rsa.PrivateKey, error) {
bs, err := osutil.ReadPrivateFile(f)
if err != nil {
return nil, err
}
return ParsePrivateKey(bs)
}
// ParsePublicKey parses a marshalled public key in SSH authorized key format.
func ParsePublicKey(bs []byte) (*rsa.PublicKey, error) {
if len(bs) == 0 {
return nil, errNoKey
}
k, _, _, _, err := ssh.ParseAuthorizedKey(bs)
if err != nil {
return nil, err
}
if k.Type() != "ssh-rsa" {
return nil, errNotRSA
}
ck, ok := k.(ssh.CryptoPublicKey)
if !ok {
return nil, errNotRSA
}
ret, ok := ck.CryptoPublicKey().(*rsa.PublicKey)
if !ok {
return nil, errNotRSA
}
return ret, nil
}
// ReadPublicKey parses a marshalled public key file in SSH authorized key
// file format.
func ReadPublicKey(f string) (*rsa.PublicKey, error) {
bs, err := os.ReadFile(f)
if err != nil {
return nil, err
}
return ParsePublicKey(bs)
}
// ParsePrivateKeyTTY parses a private key and asks for the passphrase
// if the key is an encrypted PEM.
func ParsePrivateKeyTTY(name string, bs []byte) (
*rsa.PrivateKey, error,
) {
b, _ := pem.Decode(bs)
if b == nil {
return nil, errcode.InvalidArgf("%q decode failed", name)
}
if !x509.IsEncryptedPEMBlock(b) {
return x509.ParsePKCS1PrivateKey(b.Bytes)
}
prompt := fmt.Sprintf("Passphrase for %s: ", name)
pwd, err := termutil.ReadPassword(prompt)
if err != nil {
return nil, err
}
der, err := x509.DecryptPEMBlock(b, pwd)
if err != nil {
return nil, err
}
return x509.ParsePKCS1PrivateKey(der)
}
// ReadPrivateKeyTTY reads a private key from a key file.
func ReadPrivateKeyTTY(pemFile string) (*rsa.PrivateKey, error) {
bs, err := osutil.ReadPrivateFile(pemFile)
if err != nil {
return nil, err
}
return ParsePrivateKeyTTY(pemFile, bs)
}