Ruby: ext/openssl/ossl_ssl_session.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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
7 VALUE cSSLSession;
8static VALUE eSSLSession;
9
10static void
11ossl_ssl_session_free(void *ptr)
12{
13 SSL_SESSION_free(ptr);
14}
15
16 const rb_data_type_t ossl_ssl_session_type = {
17 "OpenSSL/SSL/Session",
18 {
19 0, ossl_ssl_session_free,
20 },
21 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
22};
23
24static VALUE ossl_ssl_session_alloc(VALUE klass)
25{
26 return TypedData_Wrap_Struct(klass, &ossl_ssl_session_type, NULL);
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
44 if (rb_obj_is_instance_of(arg1, cSSLSocket)) {
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) {
57 OSSL_BIO_reset(in);
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
104 if (SSL_SESSION_get_protocol_version(a) != SSL_SESSION_get_protocol_version(b))
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);
251 ossl_str_adjust(str, 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
308 void Init_ossl_ssl_session(void)
309{
310#if 0
311 mOSSL = rb_define_module("OpenSSL");
312 mSSL = rb_define_module_under(mOSSL, "SSL");
313 eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
314#endif
315 cSSLSession = rb_define_class_under(mSSL, "Session", rb_cObject);
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}
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
ptr
struct RIMemo * ptr
Definition: debug.c:88
out
string_t out
Definition: enough.c:230
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
rb_cTime
VALUE rb_cTime
Definition: time.c:645
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:797
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:871
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:895
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1054
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
rb_obj_is_instance_of
VALUE rb_obj_is_instance_of(VALUE, VALUE)
Determines if obj is an instance of c.
Definition: object.c:707
in
unsigned in(void *in_desc, z_const unsigned char **buf)
Definition: gun.c:89
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1077
rb_check_frozen
#define rb_check_frozen
Definition: error.h:72
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:785
LONG2NUM
#define LONG2NUM
Definition: long.h:50
NUM2LONG
#define NUM2LONG
Definition: long.h:51
SSL_SESSION_get_protocol_version
#define SSL_SESSION_get_protocol_version(s)
mOSSL
VALUE mOSSL
Definition: ossl.c:231
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
eOSSLError
VALUE eOSSLError
Definition: ossl.c:236
ossl.h
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:89
OSSL_BIO_reset
#define OSSL_BIO_reset(bio)
Definition: ossl.h:116
ossl_obj2bio
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
ossl_membio2str
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:29
mSSL
VALUE mSSL
Definition: ossl_ssl.c:32
cSSLSocket
VALUE cSSLSocket
Definition: ossl_ssl.c:36
GetSSL
#define GetSSL(obj, ssl)
Definition: ossl_ssl.h:13
GetSSLSession
#define GetSSLSession(obj, sess)
Definition: ossl_ssl.h:20
cSSLSession
VALUE cSSLSession
Definition: ossl_ssl_session.c:7
Init_ossl_ssl_session
void Init_ossl_ssl_session(void)
Definition: ossl_ssl_session.c:308
ossl_ssl_session_type
const rb_data_type_t ossl_ssl_session_type
Definition: ossl_ssl_session.c:16
RDATA
#define RDATA(obj)
Definition: rdata.h:55
NULL
#define NULL
Definition: regenc.h:69
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Definition: rtypeddata.h:47
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: rtypeddata.h:101
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62
Qtrue
#define Qtrue
Definition: special_consts.h:52
Qnil
#define Qnil
Definition: special_consts.h:51
Qfalse
#define Qfalse
Definition: special_consts.h:50
rb_data_type_struct
Definition: rtypeddata.h:70
t
#define t
Definition: symbol.c:253
VALUE
unsigned long VALUE
Definition: value.h:38