mirror of
https://github.com/libp2p/go-openssl.git
synced 2024-12-28 23:50:14 +08:00
improve GC handling (fixes #10)
This commit is contained in:
parent
3945574fd1
commit
1a2646cde3
2
cert.go
2
cert.go
@ -53,6 +53,7 @@ type Certificate struct {
|
|||||||
x *C.X509
|
x *C.X509
|
||||||
Issuer *Certificate
|
Issuer *Certificate
|
||||||
ref interface{}
|
ref interface{}
|
||||||
|
pubKey PublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
type CertificateInfo struct {
|
type CertificateInfo struct {
|
||||||
@ -221,6 +222,7 @@ func (c *Certificate) SetExpireDate(when time.Duration) error {
|
|||||||
|
|
||||||
// SetPubKey assigns a new public key to a certificate.
|
// SetPubKey assigns a new public key to a certificate.
|
||||||
func (c *Certificate) SetPubKey(pubKey PublicKey) error {
|
func (c *Certificate) SetPubKey(pubKey PublicKey) error {
|
||||||
|
c.pubKey = pubKey
|
||||||
if C.X509_set_pubkey(c.x, pubKey.evpPKey()) != 1 {
|
if C.X509_set_pubkey(c.x, pubKey.evpPKey()) != 1 {
|
||||||
return errors.New("failed to set public key")
|
return errors.New("failed to set public key")
|
||||||
}
|
}
|
||||||
|
11
ctx.go
11
ctx.go
@ -101,6 +101,9 @@ var (
|
|||||||
|
|
||||||
type Ctx struct {
|
type Ctx struct {
|
||||||
ctx *C.SSL_CTX
|
ctx *C.SSL_CTX
|
||||||
|
cert *Certificate
|
||||||
|
chain []*Certificate
|
||||||
|
key PrivateKey
|
||||||
verify_cb VerifyCallback
|
verify_cb VerifyCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,6 +247,7 @@ func (c *Ctx) SetEllipticCurve(curve EllipticCurve) error {
|
|||||||
func (c *Ctx) UseCertificate(cert *Certificate) error {
|
func (c *Ctx) UseCertificate(cert *Certificate) error {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
c.cert = cert
|
||||||
if int(C.SSL_CTX_use_certificate(c.ctx, cert.x)) != 1 {
|
if int(C.SSL_CTX_use_certificate(c.ctx, cert.x)) != 1 {
|
||||||
return errorFromErrorQueue()
|
return errorFromErrorQueue()
|
||||||
}
|
}
|
||||||
@ -255,6 +259,7 @@ func (c *Ctx) UseCertificate(cert *Certificate) error {
|
|||||||
func (c *Ctx) AddChainCertificate(cert *Certificate) error {
|
func (c *Ctx) AddChainCertificate(cert *Certificate) error {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
c.chain = append(c.chain, cert)
|
||||||
if int(C.SSL_CTX_add_extra_chain_cert_not_a_macro(c.ctx, cert.x)) != 1 {
|
if int(C.SSL_CTX_add_extra_chain_cert_not_a_macro(c.ctx, cert.x)) != 1 {
|
||||||
return errorFromErrorQueue()
|
return errorFromErrorQueue()
|
||||||
}
|
}
|
||||||
@ -266,6 +271,7 @@ func (c *Ctx) AddChainCertificate(cert *Certificate) error {
|
|||||||
func (c *Ctx) UsePrivateKey(key PrivateKey) error {
|
func (c *Ctx) UsePrivateKey(key PrivateKey) error {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
c.key = key
|
||||||
if int(C.SSL_CTX_use_PrivateKey(c.ctx, key.evpPKey())) != 1 {
|
if int(C.SSL_CTX_use_PrivateKey(c.ctx, key.evpPKey())) != 1 {
|
||||||
return errorFromErrorQueue()
|
return errorFromErrorQueue()
|
||||||
}
|
}
|
||||||
@ -274,7 +280,9 @@ func (c *Ctx) UsePrivateKey(key PrivateKey) error {
|
|||||||
|
|
||||||
type CertificateStore struct {
|
type CertificateStore struct {
|
||||||
store *C.X509_STORE
|
store *C.X509_STORE
|
||||||
ctx *Ctx // for gc
|
// for GC
|
||||||
|
ctx *Ctx
|
||||||
|
certs []*Certificate
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCertificateStore returns the context's certificate store that will be
|
// GetCertificateStore returns the context's certificate store that will be
|
||||||
@ -292,6 +300,7 @@ func (c *Ctx) GetCertificateStore() *CertificateStore {
|
|||||||
func (s *CertificateStore) AddCertificate(cert *Certificate) error {
|
func (s *CertificateStore) AddCertificate(cert *Certificate) error {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
s.certs = append(s.certs, cert)
|
||||||
if int(C.X509_STORE_add_cert(s.store, cert.x)) != 1 {
|
if int(C.X509_STORE_add_cert(s.store, cert.x)) != 1 {
|
||||||
return errorFromErrorQueue()
|
return errorFromErrorQueue()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user