Skip to content

Commit 39eb5b2

Browse files
UseCertAndKey added.
New func: * UseCertAndKey() int * mapped to SSL_CTX_use_cert_and_key https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_use_cert_and_key.html ChangeLog:none Signed-off-by: Peter Grzybowski <[email protected]>
1 parent c2dcc5c commit 39eb5b2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ctx.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,28 @@ func (c *Ctx) UsePrivateKey(key PrivateKey) error {
239239
return nil
240240
}
241241

242+
// UserCertAndKey configures the context to use the given certificate
243+
// and private key for the SSL handshakes.
244+
// It allows you to use private keys that are never accessible directly
245+
// e.g.: to which openssl has access only via Engine module.
246+
// https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_use_cert_and_key.html
247+
func (c *Ctx) UseCertAndKey(cert *Certificate, key *PrivateKey) error {
248+
runtime.LockOSThread()
249+
defer runtime.UnlockOSThread()
250+
if key == nil {
251+
//this is the case where the private key cannot be accessed here, e.g.:
252+
//comes from the Engine (for instance a hw security module)
253+
if int(C.SSL_CTX_use_cert_and_key(c.ctx, cert.x, nil, nil, 0)) != 1 {
254+
return errorFromErrorQueue()
255+
}
256+
}
257+
c.key = *key
258+
if int(C.SSL_CTX_use_cert_and_key(c.ctx, cert.x, (*key).evpPKey(), nil, 0)) != 1 {
259+
return errorFromErrorQueue()
260+
}
261+
return nil
262+
}
263+
242264
type CertificateStore struct {
243265
store *C.X509_STORE
244266
// for GC

0 commit comments

Comments
 (0)