-
Notifications
You must be signed in to change notification settings - Fork 34
/
loadPrivateKey.go
66 lines (52 loc) · 1.45 KB
/
loadPrivateKey.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
package goStrongswanVici
import (
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
)
type keyPayload struct {
Typ string `json:"type"`
Data string `json:"data"`
}
// LoadECDSAPrivateKey encodes a *ecdsa.PrivateKey as a PEM block before sending
// it to the Vici interface
func (c *ClientConn) LoadECDSAPrivateKey(key *ecdsa.PrivateKey) error {
mk, err := x509.MarshalECPrivateKey(key)
if err != nil {
return err
}
var pemData = pem.EncodeToMemory(&pem.Block{
Type: "ECDSA PRIVATE KEY",
Bytes: mk,
})
return c.loadPrivateKey("ECDSA", string(pemData))
}
// LoadRSAPrivateKey encodes a *rsa.PrivateKey as a PEM block before sending
// it to the Vici interface
func (c *ClientConn) LoadRSAPrivateKey(key *rsa.PrivateKey) error {
var mk = x509.MarshalPKCS1PrivateKey(key)
var pemData = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: mk,
})
return c.loadPrivateKey("RSA", string(pemData))
}
// loadPrivateKey expects typ to be (RSA|ECDSA) and a PEM encoded data as a
// string
func (c *ClientConn) loadPrivateKey(typ, data string) (err error) {
requestMap := &map[string]interface{}{}
var k = keyPayload{
Typ: typ,
Data: data,
}
if err = ConvertToGeneral(k, requestMap); err != nil {
return fmt.Errorf("error creating request: %v", err)
}
msg, err := c.Request("load-key", *requestMap)
if msg["success"] != "yes" {
return fmt.Errorf("unsuccessful loadPrivateKey: %v", msg["errmsg"])
}
return nil
}