aboutsummaryrefslogtreecommitdiff
path: root/crypto.go
blob: 5e77d6defb4ab7f229631b7ec7b1146361a2c4d6 (plain) (blame)
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
package salticidae

// #include <stdlib.h>
// #include "salticidae/crypto.h"
import "C"
import "runtime"

// The C pointer type for a X509 handle.
type CX509 = *C.x509_t
type x509 struct{ inner CX509 }

// The handle for a X509 certificate.
type X509 = *x509

func X509FromC(ptr CX509) X509 {
	return &x509{inner: ptr}
}

func NewX509FromPemFile(fname string, passwd *string, err *Error) X509 {
	fname_c_str := C.CString(fname)
	passwd_c_str := (*C.char)(nil)
	if passwd != nil {
		passwd_c_str = C.CString(*passwd)
	}
	res := X509FromC(C.x509_new_from_pem_file(fname_c_str, passwd_c_str, err))
	if res != nil {
		runtime.SetFinalizer(res, func(self X509) { self.free() })
	}
	C.free(rawptr_t(fname_c_str))
	if passwd_c_str != nil {
		C.free(rawptr_t(passwd_c_str))
	}
	return res
}

func NewX509FromDer(der ByteArray, err *Error) X509 {
	res := X509FromC(C.x509_new_from_der(der.inner, err))
	if res != nil {
		runtime.SetFinalizer(res, func(self X509) { self.free() })
	}
	return res
}

func (self X509) free() { C.x509_free(self.inner) }
func (self X509) GetPubKey() PKey {
	res := &pKey{inner: C.x509_get_pubkey(self.inner)}
	runtime.SetFinalizer(res, func(self PKey) { self.free() })
	return res
}

func (self X509) GetDer(autoFree bool) ByteArray {
	res := ByteArrayFromC(C.x509_get_der(self.inner))
	byteArraySetFinalizer(res, autoFree)
	return res
}

// The C pointer type for a PKey handle.
type CPKey = *C.pkey_t
type pKey struct{ inner CPKey }

// The handle for an OpenSSL EVP_PKEY.
type PKey = *pKey

func NewPrivKeyFromPemFile(fname string, passwd *string, err *Error) PKey {
	fname_c_str := C.CString(fname)
	passwd_c_str := (*C.char)(nil)
	if passwd != nil {
		passwd_c_str = C.CString(*passwd)
	}
	res := &pKey{inner: C.pkey_new_privkey_from_pem_file(fname_c_str, passwd_c_str, err)}
	if res != nil {
		runtime.SetFinalizer(res, func(self PKey) { self.free() })
	}
	C.free(rawptr_t(fname_c_str))
	if passwd_c_str != nil {
		C.free(rawptr_t(passwd_c_str))
	}
	return res
}

func NewPrivKeyFromDer(der ByteArray, err *Error) PKey {
	res := &pKey{inner: C.pkey_new_privkey_from_der(der.inner, err)}
	if res != nil {
		runtime.SetFinalizer(res, func(self PKey) { self.free() })
	}
	return res
}

func (self PKey) free() { C.pkey_free(self.inner) }
func (self PKey) GetPubKeyDer(autoFree bool) ByteArray {
	res := ByteArrayFromC(C.pkey_get_pubkey_der(self.inner))
	byteArraySetFinalizer(res, autoFree)
	return res
}

func (self PKey) GetPrivKeyDer(autoFree bool) ByteArray {
	res := ByteArrayFromC(C.pkey_get_privkey_der(self.inner))
	byteArraySetFinalizer(res, autoFree)
	return res
}