/
crypto.go
131 lines (118 loc) · 3.45 KB
/
crypto.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
// Copyright 2019 Yunion
//
// 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.
package seclib2
import (
"crypto"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"fmt"
"golang.org/x/crypto/ssh"
"yunion.io/x/log"
)
func exportSshPublicKey(pubkey interface{}) ([]byte, error) {
pub, err := ssh.NewPublicKey(pubkey)
if err != nil {
return nil, err
}
return ssh.MarshalAuthorizedKey(pub), nil
}
func ssh2CryptoPublicKey(key ssh.PublicKey) crypto.PublicKey {
cryptoPub := key.(ssh.CryptoPublicKey)
return cryptoPub.CryptoPublicKey()
}
func ssh2rsaPublicKey(key ssh.PublicKey) *rsa.PublicKey {
cryptoKey := ssh2CryptoPublicKey(key)
return cryptoKey.(*rsa.PublicKey)
}
func ssh2dsaPublicKey(key ssh.PublicKey) *dsa.PublicKey {
cryptoKey := ssh2CryptoPublicKey(key)
return cryptoKey.(*dsa.PublicKey)
}
func ssh2ecdsaPublicKey(key ssh.PublicKey) *ecdsa.PublicKey {
cryptoKey := ssh2CryptoPublicKey(key)
return cryptoKey.(*ecdsa.PublicKey)
}
func Encrypt(publicKey, origData []byte) ([]byte, error) {
pub, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
if err != nil {
log.Errorf("parse authorized key error %s", err)
return nil, err
}
if pub.Type() == ssh.KeyAlgoRSA {
return rsa.EncryptOAEP(sha1.New(), rand.Reader, ssh2rsaPublicKey(pub), origData, nil)
} else {
var pubInf interface{}
switch pub.Type() {
case ssh.KeyAlgoDSA:
pubInf = ssh2dsaPublicKey(pub)
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
pubInf = ssh2ecdsaPublicKey(pub)
default:
return nil, fmt.Errorf("unsupported key type %s", pub.Type())
}
pubStr, err := exportSshPublicKey(pubInf)
if err != nil {
return nil, err
}
return encryptAES(pubStr, origData)
}
}
func Decrypt(privateKey, secret []byte) ([]byte, error) {
priv, err := ssh.ParseRawPrivateKey(privateKey)
if err != nil {
return nil, err
}
switch priv.(type) {
case *rsa.PrivateKey:
rsaPriv := priv.(*rsa.PrivateKey)
return rsa.DecryptOAEP(sha1.New(), rand.Reader, rsaPriv, secret, nil)
case *dsa.PrivateKey:
dsaPriv := priv.(*dsa.PrivateKey)
dsaPub, err := exportSshPublicKey(&dsaPriv.PublicKey)
if err != nil {
return nil, err
}
return decryptAES(dsaPub, secret)
case *ecdsa.PrivateKey:
ecdsaPriv := priv.(*ecdsa.PrivateKey)
ecdsaPub, err := exportSshPublicKey(&ecdsaPriv.PublicKey)
if err != nil {
return nil, err
}
return decryptAES(ecdsaPub, secret)
}
return nil, fmt.Errorf("unsupported")
}
func EncryptBase64(publicKey string, message string) (string, error) {
secretBytes, err := Encrypt([]byte(publicKey), []byte(message))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(secretBytes), nil
}
func DecryptBase64(privateKey string, secret string) (string, error) {
secretBytes, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return "", err
}
msgBytes, err := Decrypt([]byte(privateKey), secretBytes)
if err != nil {
return "", err
}
return string(msgBytes), nil
}