-
Notifications
You must be signed in to change notification settings - Fork 48
/
common.go
87 lines (72 loc) · 1.91 KB
/
common.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
// common package of CB-Spider's Cloud Drivers
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.08.
package common
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"golang.org/x/crypto/ssh"
)
// generate a KeyPair with 4KB length
// returns: privateKeyBytes, publicKeyBytes, error
func GenKeyPair() ([]byte, []byte, error) {
// (1) Generate a private Key
keyLength := 4096
privateKey, err := rsa.GenerateKey(rand.Reader, keyLength)
if err != nil {
return nil, nil, err
}
err = privateKey.Validate()
if err != nil {
return nil, nil, err
}
// for ASN.1 DER format
DERKey := x509.MarshalPKCS1PrivateKey(privateKey)
keyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: DERKey,
}
// for PEM format
privateKeyBytes := pem.EncodeToMemory(&keyBlock)
// (2) Generate a public key
publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
publicKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
return privateKeyBytes, publicKeyBytes, nil
}
// save a key to a file
func SaveKey(keyBytes []byte, targetFile string) error {
err := ioutil.WriteFile(targetFile, keyBytes, 0600)
if err != nil {
return err
}
return nil
}
// ParseKey reads the given RSA private key and create a public one for it.
func MakePublicKeyFromPrivateKey(pem string) (string, error) {
key, err := ssh.ParseRawPrivateKey([]byte(pem))
if err != nil {
return "", err
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return "", fmt.Errorf("%q is not a RSA key", pem)
}
pub, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return "", err
}
return string(bytes.TrimRight(ssh.MarshalAuthorizedKey(pub), "\n")), nil
}