Skip to content

Commit

Permalink
improve GC handling (fixes spacemonkeygo#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunixbochs committed Nov 15, 2014
1 parent a556b04 commit e1857f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Certificate struct {
x *C.X509
Issuer *Certificate
ref interface{}
pubKey PublicKey
}

type CertificateInfo struct {
Expand Down Expand Up @@ -221,6 +222,7 @@ func (c *Certificate) SetExpireDate(when time.Duration) error {

// SetPubKey assigns a new public key to a certificate.
func (c *Certificate) SetPubKey(pubKey PublicKey) error {
c.pubKey = pubKey
if C.X509_set_pubkey(c.x, pubKey.evpPKey()) == 0 {
return errors.New("failed to set public key")
}
Expand Down
11 changes: 10 additions & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ var (

type Ctx struct {
ctx *C.SSL_CTX
cert *Certificate
chain []*Certificate
key PrivateKey
verify_cb VerifyCallback
}

Expand Down Expand Up @@ -244,6 +247,7 @@ func (c *Ctx) SetEllipticCurve(curve EllipticCurve) error {
func (c *Ctx) UseCertificate(cert *Certificate) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.cert = cert
if int(C.SSL_CTX_use_certificate(c.ctx, cert.x)) != 1 {
return errorFromErrorQueue()
}
Expand All @@ -255,6 +259,7 @@ func (c *Ctx) UseCertificate(cert *Certificate) error {
func (c *Ctx) AddChainCertificate(cert *Certificate) error {
runtime.LockOSThread()
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 {
return errorFromErrorQueue()
}
Expand All @@ -266,6 +271,7 @@ func (c *Ctx) AddChainCertificate(cert *Certificate) error {
func (c *Ctx) UsePrivateKey(key PrivateKey) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.key = key
if int(C.SSL_CTX_use_PrivateKey(c.ctx, key.evpPKey())) != 1 {
return errorFromErrorQueue()
}
Expand All @@ -274,7 +280,9 @@ func (c *Ctx) UsePrivateKey(key PrivateKey) error {

type CertificateStore struct {
store *C.X509_STORE
ctx *Ctx // for gc
// for GC
ctx *Ctx
certs []*Certificate
}

// GetCertificateStore returns the context's certificate store that will be
Expand All @@ -292,6 +300,7 @@ func (c *Ctx) GetCertificateStore() *CertificateStore {
func (s *CertificateStore) AddCertificate(cert *Certificate) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
s.certs = append(s.certs, cert)
if int(C.X509_STORE_add_cert(s.store, cert.x)) != 1 {
return errorFromErrorQueue()
}
Expand Down

0 comments on commit e1857f7

Please sign in to comment.