forked from gokalkan/gokalkan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkc_get_cert_from_cms.go
More file actions
61 lines (47 loc) · 1.21 KB
/
kc_get_cert_from_cms.go
File metadata and controls
61 lines (47 loc) · 1.21 KB
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
package gokalkan
// #cgo LDFLAGS: -ldl
// #include <dlfcn.h>
// #include "KalkanCrypt.h"
//
// unsigned long getCertFromCMS(char *inCMS, int inCMSLen, int inSignId, int flags, char *outCert, int *outCertLength) {
// return kc_funcs->KC_GetCertFromCMS(inCMS, inCMSLen, inSignId, flags, outCert, outCertLength);
// }
import "C"
import (
"fmt"
"unsafe"
)
// KCGetCertFromCMS обеспечивает получение сертификата из CMS.
func (cli *KCClient) KCGetCertFromCMS(cms string, flag KCFlag) (cert string, err error) {
defer func() {
if r := recover(); r != nil {
if err != nil {
err = fmt.Errorf("%w: panic: %s", err, r)
return
}
err = fmt.Errorf("%w: %s", ErrPanic, r)
}
}()
cli.mu.Lock()
defer cli.mu.Unlock()
cCMS := C.CString(cms)
defer C.free(unsafe.Pointer(cCMS))
inSignID := 1
outCertLen := 32768
outCert := C.malloc(C.ulong(C.sizeof_uchar * outCertLen))
defer C.free(outCert)
rc := int(C.getCertFromCMS(
cCMS,
C.int(len(cms)),
C.int(inSignID),
C.int(int(flag)),
(*C.char)(outCert),
(*C.int)(unsafe.Pointer(&outCertLen)),
))
err = cli.wrapError(rc)
if err != nil {
return cert, err
}
cert = C.GoString((*C.char)(outCert))
return cert, nil
}