Ruby  2.4.2p198(2017-09-14revision59899)
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 
9 
10 static void
12 {
13  SSL_SESSION_free(ptr);
14 }
15 
17  "OpenSSL/SSL/Session",
18  {
20  },
22 };
23 
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  */
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 
76 static VALUE
78 {
79  SSL_SESSION *sess, *sess_other, *sess_new;
80 
81  rb_check_frozen(self);
82  sess = RTYPEDDATA_DATA(self); /* XXX */
83  SafeGetSSLSession(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 #if !defined(HAVE_SSL_SESSION_CMP)
97 int 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 #define SSL_SESSION_cmp(a, b) ossl_SSL_SESSION_cmp(a, b)
112 #endif
113 
114 /*
115  * call-seq:
116  * session1 == session2 -> boolean
117  *
118  * Returns true if the two Session is the same, false if not.
119  */
121 {
122  SSL_SESSION *ctx1, *ctx2;
123 
124  GetSSLSession(val1, ctx1);
125  SafeGetSSLSession(val2, ctx2);
126 
127  switch (SSL_SESSION_cmp(ctx1, ctx2)) {
128  case 0: return Qtrue;
129  default: return Qfalse;
130  }
131 }
132 
133 /*
134  * call-seq:
135  * session.time -> Time
136  *
137  * Returns the time at which the session was established.
138  */
139 static VALUE
141 {
142  SSL_SESSION *ctx;
143  long t;
144 
145  GetSSLSession(self, ctx);
146  t = SSL_SESSION_get_time(ctx);
147  if (t == 0)
148  return Qnil;
149 
150  return rb_funcall(rb_cTime, rb_intern("at"), 1, LONG2NUM(t));
151 }
152 
153 /*
154  * call-seq:
155  * session.timeout -> Integer
156  *
157  * Returns the timeout value set for the session, in seconds from the
158  * established time.
159  *
160  */
161 static VALUE
163 {
164  SSL_SESSION *ctx;
165  long t;
166 
167  GetSSLSession(self, ctx);
168  t = SSL_SESSION_get_timeout(ctx);
169 
170  return LONG2NUM(t);
171 }
172 
173 /*
174  * call-seq:
175  * session.time = time
176  * session.time = integer
177  *
178  * Sets start time of the session. Time resolution is in seconds.
179  *
180  */
182 {
183  SSL_SESSION *ctx;
184  long t;
185 
186  GetSSLSession(self, ctx);
187  if (rb_obj_is_instance_of(time_v, rb_cTime)) {
188  time_v = rb_funcall(time_v, rb_intern("to_i"), 0);
189  }
190  t = NUM2LONG(time_v);
191  SSL_SESSION_set_time(ctx, t);
192  return ossl_ssl_session_get_time(self);
193 }
194 
195 /*
196  * call-seq:
197  * session.timeout = integer
198  *
199  * Sets how long until the session expires in seconds.
200  */
202 {
203  SSL_SESSION *ctx;
204  long t;
205 
206  GetSSLSession(self, ctx);
207  t = NUM2LONG(time_v);
208  SSL_SESSION_set_timeout(ctx, t);
209  return ossl_ssl_session_get_timeout(self);
210 }
211 
212 /*
213  * call-seq:
214  * session.id -> String
215  *
216  * Returns the Session ID.
217 */
219 {
220  SSL_SESSION *ctx;
221  const unsigned char *p = NULL;
222  unsigned int i = 0;
223 
224  GetSSLSession(self, ctx);
225 
226  p = SSL_SESSION_get_id(ctx, &i);
227 
228  return rb_str_new((const char *) p, i);
229 }
230 
231 /*
232  * call-seq:
233  * session.to_der -> String
234  *
235  * Returns an ASN1 encoded String that contains the Session object.
236  */
238 {
239  SSL_SESSION *ctx;
240  unsigned char *p;
241  int len;
242  VALUE str;
243 
244  GetSSLSession(self, ctx);
245  len = i2d_SSL_SESSION(ctx, NULL);
246  if (len <= 0) {
247  ossl_raise(eSSLSession, "i2d_SSL_SESSION");
248  }
249 
250  str = rb_str_new(0, len);
251  p = (unsigned char *)RSTRING_PTR(str);
252  i2d_SSL_SESSION(ctx, &p);
253  ossl_str_adjust(str, p);
254  return str;
255 }
256 
257 /*
258  * call-seq:
259  * session.to_pem -> String
260  *
261  * Returns a PEM encoded String that contains the Session object.
262  */
264 {
265  SSL_SESSION *ctx;
266  BIO *out;
267 
268  GetSSLSession(self, ctx);
269 
270  if (!(out = BIO_new(BIO_s_mem()))) {
271  ossl_raise(eSSLSession, "BIO_s_mem()");
272  }
273 
274  if (!PEM_write_bio_SSL_SESSION(out, ctx)) {
275  BIO_free(out);
276  ossl_raise(eSSLSession, "SSL_SESSION_print()");
277  }
278 
279 
280  return ossl_membio2str(out);
281 }
282 
283 
284 /*
285  * call-seq:
286  * session.to_text -> String
287  *
288  * Shows everything in the Session object. This is for diagnostic purposes.
289  */
291 {
292  SSL_SESSION *ctx;
293  BIO *out;
294 
295  GetSSLSession(self, ctx);
296 
297  if (!(out = BIO_new(BIO_s_mem()))) {
298  ossl_raise(eSSLSession, "BIO_s_mem()");
299  }
300 
301  if (!SSL_SESSION_print(out, ctx)) {
302  BIO_free(out);
303  ossl_raise(eSSLSession, "SSL_SESSION_print()");
304  }
305 
306  return ossl_membio2str(out);
307 }
308 
309 
311 {
312 #if 0
313  mOSSL = rb_define_module("OpenSSL");
316 #endif
319 
323 
325 
334 }
VALUE rb_eStandardError
Definition: error.c:760
VALUE mOSSL
Definition: ossl.c:213
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
static VALUE ossl_ssl_session_initialize(VALUE self, VALUE arg1)
static VALUE ossl_ssl_session_to_text(VALUE self)
static VALUE ossl_ssl_session_get_timeout(VALUE self)
#define Qtrue
Definition: ruby.h:437
#define ossl_str_adjust(str, p)
Definition: ossl.h:82
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1169
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
int ossl_SSL_SESSION_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
static VALUE ossl_ssl_session_set_time(VALUE self, VALUE time_v)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE ossl_ssl_session_alloc(VALUE klass)
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:47
#define SSL_SESSION_get_protocol_version(s)
#define RDATA(obj)
Definition: ruby.h:1211
static void ossl_ssl_session_free(void *ptr)
#define rb_define_copy_func(klass, func)
Definition: ruby_missing.h:13
VALUE mSSL
Definition: ossl_ssl.c:26
void Init_ossl_ssl_session(void)
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
#define SSL_SESSION_cmp(a, b)
VALUE cSSLSession
#define OSSL_BIO_reset(bio)
Definition: ossl.h:110
#define GetSSL(obj, ssl)
Definition: ossl_ssl.h:13
VALUE eOSSLError
Definition: ossl.c:218
#define Qfalse
Definition: ruby.h:436
#define GetSSLSession(obj, sess)
Definition: ossl_ssl.h:20
static VALUE ossl_ssl_session_get_time(VALUE self)
VALUE cSSLSocket
Definition: ossl_ssl.c:30
#define Qnil
Definition: ruby.h:438
unsigned long VALUE
Definition: ruby.h:85
static VALUE ossl_ssl_session_initialize_copy(VALUE self, VALUE other)
const rb_data_type_t ossl_ssl_session_type
static VALUE ossl_ssl_session_set_timeout(VALUE self, VALUE time_v)
#define LONG2NUM(x)
Definition: ruby.h:1573
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define RSTRING_PTR(str)
Definition: ruby.h:982
VALUE rb_obj_is_instance_of(VALUE, VALUE)
Definition: object.c:653
static VALUE ossl_ssl_session_eq(VALUE val1, VALUE val2)
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:278
static VALUE eSSLSession
#define RTYPEDDATA_DATA(v)
Definition: ruby.h:1117
#define SafeGetSSLSession(obj, sess)
Definition: ossl_ssl.h:27
#define rb_check_frozen(obj)
Definition: intern.h:276
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
#define NULL
Definition: _sdbm.c:102
RUBY_EXTERN VALUE rb_cTime
Definition: ruby.h:1910
static VALUE ossl_ssl_session_get_id(VALUE self)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
static VALUE ossl_ssl_session_to_pem(VALUE self)
static VALUE ossl_ssl_session_to_der(VALUE self)
VALUE rb_eArgError
Definition: error.c:763
#define NUM2LONG(x)
Definition: ruby.h:648
VALUE rb_str_new(const char *, long)
Definition: string.c:736