Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
ossl_ssl_session.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2007 Technorama Ltd. <oss-ruby@technorama.net>
3 */
4
5#include "ossl.h"
6
8static VALUE eSSLSession;
9
10static void
11ossl_ssl_session_free(void *ptr)
12{
13 SSL_SESSION_free(ptr);
14}
15
17 "OpenSSL/SSL/Session",
18 {
19 0, ossl_ssl_session_free,
20 },
22};
23
24static VALUE ossl_ssl_session_alloc(VALUE klass)
25{
27}
28
29/*
30 * call-seq:
31 * Session.new(ssl_socket) -> Session
32 * Session.new(string) -> Session
33 *
34 * Creates a new Session object from an instance of SSLSocket or DER/PEM encoded
35 * String.
36 */
37static VALUE ossl_ssl_session_initialize(VALUE self, VALUE arg1)
38{
39 SSL_SESSION *ctx = NULL;
40
41 if (RDATA(self)->data)
42 ossl_raise(eSSLSession, "SSL Session already initialized");
43
45 SSL *ssl;
46
47 GetSSL(arg1, ssl);
48
49 if ((ctx = SSL_get1_session(ssl)) == NULL)
50 ossl_raise(eSSLSession, "no session available");
51 } else {
52 BIO *in = ossl_obj2bio(&arg1);
53
54 ctx = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
55
56 if (!ctx) {
58 ctx = d2i_SSL_SESSION_bio(in, NULL);
59 }
60
61 BIO_free(in);
62
63 if (!ctx)
64 ossl_raise(rb_eArgError, "unknown type");
65 }
66
67 /* should not happen */
68 if (ctx == NULL)
69 ossl_raise(eSSLSession, "ctx not set - internal error");
70
71 RDATA(self)->data = ctx;
72
73 return self;
74}
75
76static VALUE
77ossl_ssl_session_initialize_copy(VALUE self, VALUE other)
78{
79 SSL_SESSION *sess, *sess_other, *sess_new;
80
81 rb_check_frozen(self);
82 sess = RTYPEDDATA_DATA(self); /* XXX */
83 GetSSLSession(other, sess_other);
84
85 sess_new = ASN1_dup((i2d_of_void *)i2d_SSL_SESSION, (d2i_of_void *)d2i_SSL_SESSION,
86 (char *)sess_other);
87 if (!sess_new)
88 ossl_raise(eSSLSession, "ASN1_dup");
89
90 RTYPEDDATA_DATA(self) = sess_new;
91 SSL_SESSION_free(sess);
92
93 return self;
94}
95
96static int
97ossl_SSL_SESSION_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
98{
99 unsigned int a_len;
100 const unsigned char *a_sid = SSL_SESSION_get_id(a, &a_len);
101 unsigned int b_len;
102 const unsigned char *b_sid = SSL_SESSION_get_id(b, &b_len);
103
105 return 1;
106 if (a_len != b_len)
107 return 1;
108
109 return CRYPTO_memcmp(a_sid, b_sid, a_len);
110}
111
112/*
113 * call-seq:
114 * session1 == session2 -> boolean
115 *
116 * Returns +true+ if the two Session is the same, +false+ if not.
117 */
118static VALUE ossl_ssl_session_eq(VALUE val1, VALUE val2)
119{
120 SSL_SESSION *ctx1, *ctx2;
121
122 GetSSLSession(val1, ctx1);
123 GetSSLSession(val2, ctx2);
124
125 switch (ossl_SSL_SESSION_cmp(ctx1, ctx2)) {
126 case 0: return Qtrue;
127 default: return Qfalse;
128 }
129}
130
131/*
132 * call-seq:
133 * session.time -> Time
134 *
135 * Returns the time at which the session was established.
136 */
137static VALUE
138ossl_ssl_session_get_time(VALUE self)
139{
140 SSL_SESSION *ctx;
141 long t;
142
143 GetSSLSession(self, ctx);
144 t = SSL_SESSION_get_time(ctx);
145 if (t == 0)
146 return Qnil;
147
148 return rb_funcall(rb_cTime, rb_intern("at"), 1, LONG2NUM(t));
149}
150
151/*
152 * call-seq:
153 * session.timeout -> Integer
154 *
155 * Returns the timeout value set for the session, in seconds from the
156 * established time.
157 *
158 */
159static VALUE
160ossl_ssl_session_get_timeout(VALUE self)
161{
162 SSL_SESSION *ctx;
163 long t;
164
165 GetSSLSession(self, ctx);
166 t = SSL_SESSION_get_timeout(ctx);
167
168 return LONG2NUM(t);
169}
170
171/*
172 * call-seq:
173 * session.time = time
174 * session.time = integer
175 *
176 * Sets start time of the session. Time resolution is in seconds.
177 *
178 */
179static VALUE ossl_ssl_session_set_time(VALUE self, VALUE time_v)
180{
181 SSL_SESSION *ctx;
182 long t;
183
184 GetSSLSession(self, ctx);
185 if (rb_obj_is_instance_of(time_v, rb_cTime)) {
186 time_v = rb_funcall(time_v, rb_intern("to_i"), 0);
187 }
188 t = NUM2LONG(time_v);
189 SSL_SESSION_set_time(ctx, t);
190 return ossl_ssl_session_get_time(self);
191}
192
193/*
194 * call-seq:
195 * session.timeout = integer
196 *
197 * Sets how long until the session expires in seconds.
198 */
199static VALUE ossl_ssl_session_set_timeout(VALUE self, VALUE time_v)
200{
201 SSL_SESSION *ctx;
202 long t;
203
204 GetSSLSession(self, ctx);
205 t = NUM2LONG(time_v);
206 SSL_SESSION_set_timeout(ctx, t);
207 return ossl_ssl_session_get_timeout(self);
208}
209
210/*
211 * call-seq:
212 * session.id -> String
213 *
214 * Returns the Session ID.
215*/
216static VALUE ossl_ssl_session_get_id(VALUE self)
217{
218 SSL_SESSION *ctx;
219 const unsigned char *p = NULL;
220 unsigned int i = 0;
221
222 GetSSLSession(self, ctx);
223
224 p = SSL_SESSION_get_id(ctx, &i);
225
226 return rb_str_new((const char *) p, i);
227}
228
229/*
230 * call-seq:
231 * session.to_der -> String
232 *
233 * Returns an ASN1 encoded String that contains the Session object.
234 */
235static VALUE ossl_ssl_session_to_der(VALUE self)
236{
237 SSL_SESSION *ctx;
238 unsigned char *p;
239 int len;
240 VALUE str;
241
242 GetSSLSession(self, ctx);
243 len = i2d_SSL_SESSION(ctx, NULL);
244 if (len <= 0) {
245 ossl_raise(eSSLSession, "i2d_SSL_SESSION");
246 }
247
248 str = rb_str_new(0, len);
249 p = (unsigned char *)RSTRING_PTR(str);
250 i2d_SSL_SESSION(ctx, &p);
252 return str;
253}
254
255/*
256 * call-seq:
257 * session.to_pem -> String
258 *
259 * Returns a PEM encoded String that contains the Session object.
260 */
261static VALUE ossl_ssl_session_to_pem(VALUE self)
262{
263 SSL_SESSION *ctx;
264 BIO *out;
265
266 GetSSLSession(self, ctx);
267
268 if (!(out = BIO_new(BIO_s_mem()))) {
269 ossl_raise(eSSLSession, "BIO_s_mem()");
270 }
271
272 if (!PEM_write_bio_SSL_SESSION(out, ctx)) {
273 BIO_free(out);
274 ossl_raise(eSSLSession, "SSL_SESSION_print()");
275 }
276
277
278 return ossl_membio2str(out);
279}
280
281
282/*
283 * call-seq:
284 * session.to_text -> String
285 *
286 * Shows everything in the Session object. This is for diagnostic purposes.
287 */
288static VALUE ossl_ssl_session_to_text(VALUE self)
289{
290 SSL_SESSION *ctx;
291 BIO *out;
292
293 GetSSLSession(self, ctx);
294
295 if (!(out = BIO_new(BIO_s_mem()))) {
296 ossl_raise(eSSLSession, "BIO_s_mem()");
297 }
298
299 if (!SSL_SESSION_print(out, ctx)) {
300 BIO_free(out);
301 ossl_raise(eSSLSession, "SSL_SESSION_print()");
302 }
303
304 return ossl_membio2str(out);
305}
306
307
309{
310#if 0
311 mOSSL = rb_define_module("OpenSSL");
314#endif
316 eSSLSession = rb_define_class_under(cSSLSession, "SessionError", eOSSLError);
317
318 rb_define_alloc_func(cSSLSession, ossl_ssl_session_alloc);
319 rb_define_method(cSSLSession, "initialize", ossl_ssl_session_initialize, 1);
320 rb_define_method(cSSLSession, "initialize_copy", ossl_ssl_session_initialize_copy, 1);
321
322 rb_define_method(cSSLSession, "==", ossl_ssl_session_eq, 1);
323
324 rb_define_method(cSSLSession, "time", ossl_ssl_session_get_time, 0);
325 rb_define_method(cSSLSession, "time=", ossl_ssl_session_set_time, 1);
326 rb_define_method(cSSLSession, "timeout", ossl_ssl_session_get_timeout, 0);
327 rb_define_method(cSSLSession, "timeout=", ossl_ssl_session_set_timeout, 1);
328 rb_define_method(cSSLSession, "id", ossl_ssl_session_get_id, 0);
329 rb_define_method(cSSLSession, "to_der", ossl_ssl_session_to_der, 0);
330 rb_define_method(cSSLSession, "to_pem", ossl_ssl_session_to_pem, 0);
331 rb_define_method(cSSLSession, "to_text", ossl_ssl_session_to_text, 0);
332}
struct RIMemo * ptr
Definition: debug.c:65
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
Definition: class.c:711
VALUE rb_define_module(const char *)
Definition: class.c:785
VALUE rb_define_module_under(VALUE, const char *)
Definition: class.c:810
VALUE rb_cObject
Object class.
Definition: ruby.h:2012
VALUE rb_cTime
Definition: ruby.h:2050
VALUE rb_eStandardError
Definition: error.c:921
VALUE rb_eArgError
Definition: error.c:925
VALUE rb_obj_is_instance_of(VALUE, VALUE)
Determines if obj is an instance of c.
Definition: object.c:675
#define SSL_SESSION_get_protocol_version(s)
VALUE mOSSL
Definition: ossl.c:231
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
VALUE eOSSLError
Definition: ossl.c:236
#define ossl_str_adjust(str, p)
Definition: ossl.h:87
#define OSSL_BIO_reset(bio)
Definition: ossl.h:115
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:29
VALUE mSSL
Definition: ossl_ssl.c:32
VALUE cSSLSocket
Definition: ossl_ssl.c:36
#define GetSSL(obj, ssl)
Definition: ossl_ssl.h:13
#define GetSSLSession(obj, sess)
Definition: ossl_ssl.h:20
VALUE cSSLSession
void Init_ossl_ssl_session(void)
const rb_data_type_t ossl_ssl_session_type
#define NULL
#define rb_check_frozen(obj)
#define RSTRING_PTR(str)
#define RTYPEDDATA_DATA(v)
#define rb_str_new(str, len)
unsigned long VALUE
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
uint32_t i
__inline__ const void *__restrict__ size_t len
#define LONG2NUM(x)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define rb_funcall(recv, mid, argc,...)
#define rb_intern(str)
#define TypedData_Wrap_Struct(klass, data_type, sval)
#define RDATA(obj)
#define Qtrue
#define Qnil
#define Qfalse
#define NUM2LONG(x)
void rb_define_method(VALUE, const char *, VALUE(*)(), int)