-
Notifications
You must be signed in to change notification settings - Fork 94
/
cert.go
158 lines (130 loc) · 3.6 KB
/
cert.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package goczmq
/*
#include "czmq.h"
void Set_meta(zcert_t *self, const char *key, const char *value) {zcert_set_meta(self, key, "%s", value);}
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
// Cert wraps the CZMQ zcert class. It provides tools for
// creating and working with ZMQ CURVE security certs.
// The certs can be used as a temporary object in memory
// or persisted to disk. Certs are made up of a public
// and secret keypair + metadata.
type Cert struct {
zcertT *C.struct__zcert_t
}
// NewCert creates a new empty Cert instance
func NewCert() *Cert {
return &Cert{
zcertT: C.zcert_new(),
}
}
// NewCertFromKeys creates a new Cert from a public and private key
func NewCertFromKeys(public []byte, secret []byte) (*Cert, error) {
if len(public) != 32 {
return nil, fmt.Errorf("invalid public key")
}
if len(secret) != 32 {
return nil, fmt.Errorf("invalid private key")
}
return &Cert{
zcertT: C.zcert_new_from(
(*C.byte)(unsafe.Pointer(&public[0])),
(*C.byte)(unsafe.Pointer(&secret[0]))),
}, nil
}
// NewCertFromFile Load loads a Cert from files
func NewCertFromFile(filename string) (*Cert, error) {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return nil, ErrCertNotFound
}
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
cert := C.zcert_load(cFilename)
return &Cert{
zcertT: cert,
}, nil
}
// SetMeta sets meta data for a Cert
func (c *Cert) SetMeta(key string, value string) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
C.Set_meta(c.zcertT, cKey, cValue)
}
// Meta returns a meta data item from a Cert given a key
func (c *Cert) Meta(key string) string {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
val := C.zcert_meta(c.zcertT, cKey)
return C.GoString(val)
}
// PublicText returns the public key as a string
func (c *Cert) PublicText() string {
val := C.zcert_public_txt(c.zcertT)
return C.GoString(val)
}
// Apply sets the public and private keys for a socket
func (c *Cert) Apply(s *Sock) {
handle := C.zsock_resolve(unsafe.Pointer(s.zsockT))
C.zsock_set_curve_secretkey_bin(handle, C.zcert_secret_key(c.zcertT))
C.zsock_set_curve_publickey_bin(handle, C.zcert_public_key(c.zcertT))
}
// Dup duplicates a Cert
func (c *Cert) Dup() *Cert {
return &Cert{
zcertT: C.zcert_dup(c.zcertT),
}
}
// Equal checks two Certs for equality
func (c *Cert) Equal(compare *Cert) bool {
check := C.zcert_eq(c.zcertT, compare.zcertT)
if check == C.bool(true) {
return true
}
return false
}
// Print prints a Cert to stdout
func (c *Cert) Print() {
C.zcert_print(c.zcertT)
}
// SavePublic saves the public key to a file
func (c *Cert) SavePublic(filename string) error {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
rc := C.zcert_save_public(c.zcertT, cFilename)
if rc == C.int(-1) {
return fmt.Errorf("SavePublic error")
}
return nil
}
// SaveSecret saves the secret key to a file
func (c *Cert) SaveSecret(filename string) error {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
rc := C.zcert_save_secret(c.zcertT, cFilename)
if rc == C.int(-1) {
return fmt.Errorf("SaveSecret error")
}
return nil
}
// Save saves the public and secret key to filename and filename_secret
func (c *Cert) Save(filename string) error {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
rc := C.zcert_save(c.zcertT, cFilename)
if rc == C.int(-1) {
return fmt.Errorf("SavePublic: error")
}
return nil
}
// Destroy destroys Cert instance
func (c *Cert) Destroy() {
C.zcert_destroy(&c.zcertT)
}