diff --git a/cert.go b/cert.go index b4f8d1fa..0d4a1bef 100644 --- a/cert.go +++ b/cert.go @@ -53,6 +53,7 @@ type Certificate struct { x *C.X509 Issuer *Certificate ref interface{} + pubKey PublicKey } type CertificateInfo struct { @@ -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") } diff --git a/ctx.go b/ctx.go index 921e9bea..538679f5 100644 --- a/ctx.go +++ b/ctx.go @@ -101,6 +101,9 @@ var ( type Ctx struct { ctx *C.SSL_CTX + cert *Certificate + chain []*Certificate + key PrivateKey verify_cb VerifyCallback } @@ -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() } @@ -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() } @@ -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() } @@ -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 @@ -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() }