-
Notifications
You must be signed in to change notification settings - Fork 5
/
sec1_serializer.go
218 lines (170 loc) · 5.77 KB
/
sec1_serializer.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package foundation
// #include <virgil/crypto/foundation/vscf_foundation_public.h>
import "C"
import unsafe "unsafe"
import "runtime"
/*
* Implements SEC 1 key serialization to DER format.
* See also RFC 5480 and RFC 5915.
*/
type Sec1Serializer struct {
cCtx *C.vscf_sec1_serializer_t /*ct10*/
}
func (obj *Sec1Serializer) SetAsn1Writer(asn1Writer Asn1Writer) {
C.vscf_sec1_serializer_release_asn1_writer(obj.cCtx)
C.vscf_sec1_serializer_use_asn1_writer(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(asn1Writer.Ctx())))
runtime.KeepAlive(asn1Writer)
runtime.KeepAlive(obj)
}
/*
* Setup predefined values to the uninitialized class dependencies.
*/
func (obj *Sec1Serializer) SetupDefaults() {
C.vscf_sec1_serializer_setup_defaults(obj.cCtx)
runtime.KeepAlive(obj)
return
}
/*
* Serialize Public Key by using internal ASN.1 writer.
* Note, that caller code is responsible to reset ASN.1 writer with
* an output buffer.
*/
func (obj *Sec1Serializer) SerializePublicKeyInplace(publicKey *RawPublicKey) (uint, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialize_public_key_inplace(obj.cCtx, (*C.vscf_raw_public_key_t)(unsafe.Pointer(publicKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return 0, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return uint(proxyResult) /* r9 */, nil
}
/*
* Serialize Private Key by using internal ASN.1 writer.
* Note, that caller code is responsible to reset ASN.1 writer with
* an output buffer.
*/
func (obj *Sec1Serializer) SerializePrivateKeyInplace(privateKey *RawPrivateKey) (uint, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialize_private_key_inplace(obj.cCtx, (*C.vscf_raw_private_key_t)(unsafe.Pointer(privateKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return 0, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return uint(proxyResult) /* r9 */, nil
}
/* Handle underlying C context. */
func (obj *Sec1Serializer) Ctx() uintptr {
return uintptr(unsafe.Pointer(obj.cCtx))
}
func NewSec1Serializer() *Sec1Serializer {
ctx := C.vscf_sec1_serializer_new()
obj := &Sec1Serializer {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Sec1Serializer).Delete)
return obj
}
/* Acquire C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newSec1SerializerWithCtx(ctx *C.vscf_sec1_serializer_t /*ct10*/) *Sec1Serializer {
obj := &Sec1Serializer {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Sec1Serializer).Delete)
return obj
}
/* Acquire retained C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newSec1SerializerCopy(ctx *C.vscf_sec1_serializer_t /*ct10*/) *Sec1Serializer {
obj := &Sec1Serializer {
cCtx: C.vscf_sec1_serializer_shallow_copy(ctx),
}
runtime.SetFinalizer(obj, (*Sec1Serializer).Delete)
return obj
}
/*
* Release underlying C context.
*/
func (obj *Sec1Serializer) Delete() {
if obj == nil {
return
}
runtime.SetFinalizer(obj, nil)
obj.delete()
}
/*
* Release underlying C context.
*/
func (obj *Sec1Serializer) delete() {
C.vscf_sec1_serializer_delete(obj.cCtx)
}
/*
* Calculate buffer size enough to hold serialized public key.
*
* Precondition: public key must be exportable.
*/
func (obj *Sec1Serializer) SerializedPublicKeyLen(publicKey *RawPublicKey) uint {
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialized_public_key_len(obj.cCtx, (*C.vscf_raw_public_key_t)(unsafe.Pointer(publicKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return uint(proxyResult) /* r9 */
}
/*
* Serialize given public key to an interchangeable format.
*
* Precondition: public key must be exportable.
*/
func (obj *Sec1Serializer) SerializePublicKey(publicKey *RawPublicKey) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.SerializedPublicKeyLen(publicKey) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialize_public_key(obj.cCtx, (*C.vscf_raw_public_key_t)(unsafe.Pointer(publicKey.Ctx())), outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return outBuf.getData() /* r7 */, nil
}
/*
* Calculate buffer size enough to hold serialized private key.
*
* Precondition: private key must be exportable.
*/
func (obj *Sec1Serializer) SerializedPrivateKeyLen(privateKey *RawPrivateKey) uint {
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialized_private_key_len(obj.cCtx, (*C.vscf_raw_private_key_t)(unsafe.Pointer(privateKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return uint(proxyResult) /* r9 */
}
/*
* Serialize given private key to an interchangeable format.
*
* Precondition: private key must be exportable.
*/
func (obj *Sec1Serializer) SerializePrivateKey(privateKey *RawPrivateKey) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.SerializedPrivateKeyLen(privateKey) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
proxyResult := /*pr4*/C.vscf_sec1_serializer_serialize_private_key(obj.cCtx, (*C.vscf_raw_private_key_t)(unsafe.Pointer(privateKey.Ctx())), outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return outBuf.getData() /* r7 */, nil
}