Ruby  2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)
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 
8 static VALUE eSSLSession;
9 
10 static void
11 ossl_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 
24 static 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  */
37 static 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) {
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 
76 static VALUE
77 ossl_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 
96 static int
97 ossl_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  */
118 static 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  */
137 static VALUE
138 ossl_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  */
159 static VALUE
160 ossl_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  */
179 static 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  */
199 static 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 */
216 static 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  */
235 static 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  */
261 static 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  */
288 static 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 }
GetSSL
#define GetSSL(obj, ssl)
Definition: ossl_ssl.h:13
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:797
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:679
ossl_membio2str
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:29
SSL_SESSION_get_protocol_version
#define SSL_SESSION_get_protocol_version(s)
VALUE
unsigned long VALUE
Definition: ruby.h:102
ossl_obj2bio
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
rb_intern
#define rb_intern(str)
OSSL_BIO_reset
#define OSSL_BIO_reset(bio)
Definition: ossl.h:114
rb_cTime
RUBY_EXTERN VALUE rb_cTime
Definition: ruby.h:2048
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:772
ossl.h
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
ptr
struct RIMemo * ptr
Definition: debug.c:74
Qfalse
#define Qfalse
Definition: ruby.h:467
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:86
GetSSLSession
#define GetSSLSession(obj, sess)
Definition: ossl_ssl.h:20
NULL
#define NULL
Definition: _sdbm.c:101
ossl_ssl_session_type
const rb_data_type_t ossl_ssl_session_type
Definition: ossl_ssl_session.c:16
cSSLSocket
VALUE cSSLSocket
Definition: ossl_ssl.c:30
LONG2NUM
#define LONG2NUM(x)
Definition: ruby.h:1644
mOSSL
VALUE mOSSL
Definition: ossl.c:231
rb_check_frozen
#define rb_check_frozen(obj)
Definition: intern.h:319
rb_obj_is_instance_of
VALUE rb_obj_is_instance_of(VALUE, VALUE)
Determines if obj is an instance of c.
Definition: object.c:675
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1231
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Definition: ruby.h:1179
cSSLSession
VALUE cSSLSession
Definition: ossl_ssl_session.c:7
npn_select_cb_common_args::in
const unsigned char * in
Definition: ossl_ssl.c:618
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6585
mSSL
VALUE mSSL
Definition: ossl_ssl.c:26
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.0.h:13254
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RUBY_TYPED_FREE_IMMEDIATELY
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1207
RDATA
#define RDATA(obj)
Definition: ruby.h:1274
rb_data_type_struct
Definition: ruby.h:1148
rb_str_new
#define rb_str_new(str, len)
Definition: rb_mjit_min_header-2.7.0.h:6116
Qtrue
#define Qtrue
Definition: ruby.h:468
len
uint8_t len
Definition: escape.c:17
Init_ossl_ssl_session
void Init_ossl_ssl_session(void)
Definition: ossl_ssl_session.c:308
eOSSLError
VALUE eOSSLError
Definition: ossl.c:236
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:698
Qnil
#define Qnil
Definition: ruby.h:469
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:919
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)