forked from l-vitaly/cryptopro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert.go
More file actions
90 lines (73 loc) · 2.03 KB
/
cert.go
File metadata and controls
90 lines (73 loc) · 2.03 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
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
package cryptopro
//#include "common.h"
import "C"
import (
"encoding/hex"
"unsafe"
"github.com/pkg/errors"
)
var (
ErrCreatingCertificateCtx = errors.New("error a new certificate could not be created")
)
type Cert struct {
pCert C.PCCERT_CONTEXT
}
func ParseCert(buf []byte) (Cert, error) {
bufBytes := C.CBytes(buf)
defer C.free(bufBytes)
var res Cert
res.pCert = C.CertCreateCertificateContext(C.MY_ENC_TYPE, (*C.BYTE)(bufBytes), C.DWORD(len(buf)))
if res.pCert == nil {
return Cert{}, ErrCreatingCertificateCtx
}
return res, nil
}
func (c Cert) Close() error {
if C.CertFreeCertificateContext(c.pCert) == 0 {
return errors.New("error releasing certificate context")
}
return nil
}
type CertPropertyId C.DWORD
const (
CertHashProp CertPropertyId = C.CERT_HASH_PROP_ID
CertKeyIdentifierProp CertPropertyId = C.CERT_KEY_IDENTIFIER_PROP_ID
CertProvInfoProp CertPropertyId = C.CERT_KEY_PROV_INFO_PROP_ID
)
func (c Cert) GetProperty(propId CertPropertyId) ([]byte, error) {
var slen C.DWORD
var res []byte
if C.CertGetCertificateContextProperty(c.pCert, C.DWORD(propId), nil, &slen) == 0 {
return res, errors.New("error getting cert context property size")
}
res = make([]byte, slen)
if C.CertGetCertificateContextProperty(c.pCert, C.DWORD(propId), unsafe.Pointer(&res[0]), &slen) == 0 {
return res, errors.New("error getting cert context property body")
}
return res, nil
}
func (c Cert) ThumbPrint() (string, error) {
thumb, err := c.GetProperty(CertHashProp)
return hex.EncodeToString(thumb), err
}
func (c Cert) MustThumbPrint() string {
if thumb, err := c.ThumbPrint(); err != nil {
panic(err)
} else {
return thumb
}
}
func (c Cert) SubjectId() (string, error) {
thumb, err := c.GetProperty(CertKeyIdentifierProp)
return hex.EncodeToString(thumb), err
}
func (c Cert) MustSubjectId() string {
if subj, err := c.SubjectId(); err != nil {
panic(err)
} else {
return subj
}
}
func (c Cert) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(c.pCert.pbCertEncoded), C.int(c.pCert.cbCertEncoded))
}