From 8d2efad227fb9d1fb813b0b6090fa54dc2fa1e0b Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Tue, 12 May 2015 22:18:27 +0300 Subject: [PATCH 1/3] add a few session cache options getters and setters --- ctx.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ctx.go b/ctx.go index 57a7b4a..8c8e2dc 100644 --- a/ctx.go +++ b/ctx.go @@ -42,6 +42,22 @@ static long SSL_CTX_set_session_cache_mode_not_a_macro(SSL_CTX* ctx, long modes) return SSL_CTX_set_session_cache_mode(ctx, modes); } +static long SSL_CTX_sess_set_cache_size_not_a_macro(SSL_CTX* ctx, long t) { + return SSL_CTX_sess_set_cache_size(ctx, t); +} + +static long SSL_CTX_sess_get_cache_size_not_a_macro(SSL_CTX* ctx) { + return SSL_CTX_sess_get_cache_size(ctx); +} + +static long SSL_CTX_set_timeout_not_a_macro(SSL_CTX* ctx, long t) { + return SSL_CTX_set_timeout(ctx, t); +} + +static long SSL_CTX_get_timeout_not_a_macro(SSL_CTX* ctx) { + return SSL_CTX_get_timeout(ctx); +} + static int CRYPTO_add_not_a_macro(int *pointer,int amount,int type) { return CRYPTO_add(pointer, amount, type); } @@ -563,3 +579,27 @@ func (c *Ctx) SetSessionCacheMode(modes SessionCacheModes) SessionCacheModes { return SessionCacheModes( C.SSL_CTX_set_session_cache_mode_not_a_macro(c.ctx, C.long(modes))) } + +// Set session cache timeout in seconds. Returns previously set value. +// See https://www.openssl.org/docs/ssl/SSL_CTX_set_timeout.html +func (c *Ctx) SetTimeout(t int) int { + return int(C.SSL_CTX_set_timeout_not_a_macro(c.ctx, C.long(t))) +} + +// Get session cache timeout in seconds. +// See https://www.openssl.org/docs/ssl/SSL_CTX_set_timeout.html +func (c *Ctx) GetTimeout() int { + return int(C.SSL_CTX_get_timeout_not_a_macro(c.ctx)) +} + +// Set session cache size. Returns previously set value. +// https://www.openssl.org/docs/ssl/SSL_CTX_sess_set_cache_size.html +func (c *Ctx) SessSetCacheSize(t int) int { + return int(C.SSL_CTX_sess_set_cache_size_not_a_macro(c.ctx, C.long(t))) +} + +// Get session cache size. +// https://www.openssl.org/docs/ssl/SSL_CTX_sess_set_cache_size.html +func (c *Ctx) SessGetCacheSize() int { + return int(C.SSL_CTX_sess_get_cache_size_not_a_macro(c.ctx)) +} From c96ed22afd1b2a4c497a63506a268cd0a5067642 Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Wed, 13 May 2015 12:04:43 +0300 Subject: [PATCH 2/3] add basic test for Ctx session cache timeout/size options --- ctx_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ctx_test.go diff --git a/ctx_test.go b/ctx_test.go new file mode 100644 index 0000000..7125d73 --- /dev/null +++ b/ctx_test.go @@ -0,0 +1,47 @@ +// Copyright (C) 2014 Ryan Hileman +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package openssl + +import ( + "testing" +) + +func TestCtxTimeoutOption(t *testing.T) { + ctx, _ := NewCtx() + oldTimeout1 := ctx.GetTimeout() + newTimeout1 := oldTimeout1 + 8362 + oldTimeout2 := ctx.SetTimeout(newTimeout1) + newTimeout2 := ctx.GetTimeout() + if oldTimeout1 != oldTimeout2 { + t.Error("SetTimeout() returns something undocumented") + } + if newTimeout1 != newTimeout2 { + t.Error("SetTimeout() does not save anything to ctx") + } +} + +func TestCtxSessCacheSizeOption(t *testing.T) { + ctx, _ := NewCtx() + oldSize1 := ctx.SessGetCacheSize() + newSize1 := oldSize1 + 42 + oldSize2 := ctx.SessSetCacheSize(newSize1) + newSize2 := ctx.SessGetCacheSize() + if oldSize1 != oldSize2 { + t.Error("SessSetCacheSize() returns something undocumented") + } + if newSize1 != newSize2 { + t.Error("SessSetCacheSize() does not save anything to ctx") + } +} From 1d354f480d32a62c3989793f484292816ffd9c92 Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Thu, 14 May 2015 09:14:36 +0300 Subject: [PATCH 3/3] use time.Duration instead of int to specify session timeout --- ctx.go | 14 ++++++++------ ctx_test.go | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ctx.go b/ctx.go index 8c8e2dc..7db505e 100644 --- a/ctx.go +++ b/ctx.go @@ -104,6 +104,7 @@ import ( "io/ioutil" "os" "runtime" + "time" "unsafe" "github.com/spacemonkeygo/spacelog" @@ -580,16 +581,17 @@ func (c *Ctx) SetSessionCacheMode(modes SessionCacheModes) SessionCacheModes { C.SSL_CTX_set_session_cache_mode_not_a_macro(c.ctx, C.long(modes))) } -// Set session cache timeout in seconds. Returns previously set value. +// Set session cache timeout. Returns previously set value. // See https://www.openssl.org/docs/ssl/SSL_CTX_set_timeout.html -func (c *Ctx) SetTimeout(t int) int { - return int(C.SSL_CTX_set_timeout_not_a_macro(c.ctx, C.long(t))) +func (c *Ctx) SetTimeout(t time.Duration) time.Duration { + prev := C.SSL_CTX_set_timeout_not_a_macro(c.ctx, C.long(t/time.Second)) + return time.Duration(prev) * time.Second } -// Get session cache timeout in seconds. +// Get session cache timeout. // See https://www.openssl.org/docs/ssl/SSL_CTX_set_timeout.html -func (c *Ctx) GetTimeout() int { - return int(C.SSL_CTX_get_timeout_not_a_macro(c.ctx)) +func (c *Ctx) GetTimeout() time.Duration { + return time.Duration(C.SSL_CTX_get_timeout_not_a_macro(c.ctx)) * time.Second } // Set session cache size. Returns previously set value. diff --git a/ctx_test.go b/ctx_test.go index 7125d73..9644e51 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -16,12 +16,13 @@ package openssl import ( "testing" + "time" ) func TestCtxTimeoutOption(t *testing.T) { ctx, _ := NewCtx() oldTimeout1 := ctx.GetTimeout() - newTimeout1 := oldTimeout1 + 8362 + newTimeout1 := oldTimeout1 + (time.Duration(99) * time.Second) oldTimeout2 := ctx.SetTimeout(newTimeout1) newTimeout2 := ctx.GetTimeout() if oldTimeout1 != oldTimeout2 {