~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/openssl/ossl_ssl.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: ossl_ssl.c 32382 2011-07-03 12:24:02Z yugui $
 
2
 * $Id: ossl_ssl.c 32975 2011-08-15 01:20:27Z emboss $
3
3
 * 'OpenSSL for Ruby' project
4
4
 * Copyright (C) 2000-2002  GOTOU Yuuzou <gotoyuzo@notwork.org>
5
5
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
16
16
#  include <unistd.h> /* for read(), and write() */
17
17
#endif
18
18
 
19
 
#define numberof(ary) (int)(sizeof(ary)/sizeof(ary[0]))
 
19
#define numberof(ary) (int)(sizeof(ary)/sizeof((ary)[0]))
20
20
 
21
21
#ifdef _WIN32
22
22
#  define TO_SOCKET(s) _get_osfhandle(s)
23
23
#else
24
 
#  define TO_SOCKET(s) s
 
24
#  define TO_SOCKET(s) (s)
25
25
#endif
26
26
 
27
27
VALUE mSSL;
108
108
    OSSL_SSL_METHOD_ENTRY(TLSv1_server),
109
109
    OSSL_SSL_METHOD_ENTRY(TLSv1_client),
110
110
#if defined(HAVE_SSLV2_METHOD) && defined(HAVE_SSLV2_SERVER_METHOD) && \
111
 
        defined(HAVE_SSLV2_CLIENT_METHOD)       
 
111
        defined(HAVE_SSLV2_CLIENT_METHOD)
112
112
    OSSL_SSL_METHOD_ENTRY(SSLv2),
113
113
    OSSL_SSL_METHOD_ENTRY(SSLv2_server),
114
114
    OSSL_SSL_METHOD_ENTRY(SSLv2_client),
140
140
ossl_sslctx_s_alloc(VALUE klass)
141
141
{
142
142
    SSL_CTX *ctx;
 
143
    long mode = SSL_MODE_ENABLE_PARTIAL_WRITE;
 
144
 
 
145
#ifdef SSL_MODE_RELEASE_BUFFERS
 
146
    mode |= SSL_MODE_RELEASE_BUFFERS;
 
147
#endif
143
148
 
144
149
    ctx = SSL_CTX_new(SSLv23_method());
145
150
    if (!ctx) {
146
151
        ossl_raise(eSSLError, "SSL_CTX_new:");
147
152
    }
148
 
    SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
 
153
    SSL_CTX_set_mode(ctx, mode);
149
154
    SSL_CTX_set_options(ctx, SSL_OP_ALL);
150
155
    return Data_Wrap_Struct(klass, 0, ossl_sslctx_free, ctx);
151
156
}
152
157
 
 
158
/*
 
159
 * call-seq:
 
160
 *    ctx.ssl_version = :TLSv1
 
161
 *    ctx.ssl_version = "SSLv23_client"
 
162
 *
 
163
 * You can get a list of valid versions with OpenSSL::SSL::SSLContext::METHODS
 
164
 */
153
165
static VALUE
154
166
ossl_sslctx_set_ssl_version(VALUE self, VALUE ssl_method)
155
167
{
228
240
static int
229
241
ossl_client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
230
242
{
231
 
    VALUE obj;
232
 
    int status, success;
 
243
    VALUE obj, success;
233
244
 
234
245
    obj = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx);
235
246
    success = rb_protect((VALUE(*)_((VALUE)))ossl_call_client_cert_cb,
236
 
                         obj, &status);
237
 
    if (status || !success) return 0;
 
247
                         obj, NULL);
 
248
    if (!RTEST(success)) return 0;
238
249
    *x509 = DupX509CertPtr(ossl_ssl_get_x509(obj));
239
250
    *pkey = DupPKeyPtr(ossl_ssl_get_key(obj));
240
251
 
263
274
static DH*
264
275
ossl_tmp_dh_callback(SSL *ssl, int is_export, int keylength)
265
276
{
266
 
    VALUE args[3];
267
 
    int status, success;
 
277
    VALUE args[3], success;
268
278
 
269
279
    args[0] = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx);
270
280
    args[1] = INT2FIX(is_export);
271
281
    args[2] = INT2FIX(keylength);
272
282
    success = rb_protect((VALUE(*)_((VALUE)))ossl_call_tmp_dh_callback,
273
 
                         (VALUE)args, &status);
274
 
    if (status || !success) return NULL;
 
283
                         (VALUE)args, NULL);
 
284
    if (!RTEST(success)) return NULL;
275
285
 
276
286
    return GetPKeyPtr(ossl_ssl_get_tmp_dh(args[0]))->pkey.dh;
277
287
}
390
400
    ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_new_cb, ary, &state);
391
401
    if (state) {
392
402
        rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(state));
393
 
        return 0; /* what should be returned here??? */
394
403
    }
395
404
 
396
 
    return RTEST(ret_obj) ? 1 : 0;
 
405
    /*
 
406
     * return 0 which means to OpenSSL that the the session is still
 
407
     * valid (since we created Ruby Session object) and was not freed by us
 
408
     * with SSL_SESSION_free(). Call SSLContext#remove_session(sess) in
 
409
     * session_get_cb block if you don't want OpenSSL to cache the session
 
410
     * internally.
 
411
     */
 
412
    return 0;
397
413
}
398
414
 
399
 
#if 0                           /* unused */
400
415
static VALUE
401
416
ossl_call_session_remove_cb(VALUE ary)
402
417
{
410
425
 
411
426
    return rb_funcall(cb, rb_intern("call"), 1, ary);
412
427
}
413
 
#endif
414
428
 
415
429
static void
416
430
ossl_sslctx_session_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
432
446
    rb_ary_push(ary, sslctx_obj);
433
447
    rb_ary_push(ary, sess_obj);
434
448
 
435
 
    ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_new_cb, ary, &state);
 
449
    ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_remove_cb, ary, &state);
436
450
    if (state) {
437
451
/*
438
452
  the SSL_CTX is frozen, nowhere to save state.
483
497
        Data_Get_Struct(ret_obj, SSL_CTX, ctx2);
484
498
        SSL_set_SSL_CTX(ssl, ctx2);
485
499
    } else if (!NIL_P(ret_obj)) {
486
 
            rb_raise(rb_eArgError, "servername_cb must return an OpenSSL::SSL::SSLContext object or nil");
 
500
            ossl_raise(rb_eArgError, "servername_cb must return an OpenSSL::SSL::SSLContext object or nil");
487
501
    }
488
502
 
489
503
    return ret_obj;
523
537
 *    ctx.setup => nil # thereafter
524
538
 *
525
539
 * This method is called automatically when a new SSLSocket is created.
526
 
 * Normally you do not need to call this method (unless you are writing an extension in C).
 
540
 * Normally you do not need to call this method (unless you are writing an
 
541
 * extension in C).
527
542
 */
528
543
static VALUE
529
544
ossl_sslctx_setup(VALUE self)
625
640
    if(!NIL_P(val)) SSL_CTX_set_timeout(ctx, NUM2LONG(val));
626
641
 
627
642
    val = ossl_sslctx_get_verify_dep(self);
628
 
    if(!NIL_P(val)) SSL_CTX_set_verify_depth(ctx, NUM2LONG(val));
 
643
    if(!NIL_P(val)) SSL_CTX_set_verify_depth(ctx, NUM2INT(val));
629
644
 
630
645
    val = ossl_sslctx_get_options(self);
631
646
    if(!NIL_P(val)) SSL_CTX_set_options(ctx, NUM2LONG(val));
635
650
    if (!NIL_P(val)){
636
651
        StringValue(val);
637
652
        if (!SSL_CTX_set_session_id_context(ctx, (unsigned char *)RSTRING_PTR(val),
638
 
                                            RSTRING_LEN(val))){
 
653
                                            RSTRING_LENINT(val))){
639
654
            ossl_raise(eSSLError, "SSL_CTX_set_session_id_context:");
640
655
        }
641
656
    }
683
698
/*
684
699
 * call-seq:
685
700
 *    ctx.ciphers => [[name, version, bits, alg_bits], ...]
 
701
 *
 
702
 * The list of ciphers configured for this context.
686
703
 */
687
704
static VALUE
688
705
ossl_sslctx_get_ciphers(VALUE self)
703
720
    if (!ciphers)
704
721
        return rb_ary_new();
705
722
 
706
 
    num = sk_num((STACK*)ciphers);
 
723
    num = sk_SSL_CIPHER_num(ciphers);
707
724
    ary = rb_ary_new2(num);
708
725
    for(i = 0; i < num; i++){
709
 
        cipher = (SSL_CIPHER*)sk_value((STACK*)ciphers, i);
 
726
        cipher = sk_SSL_CIPHER_value(ciphers, i);
710
727
        rb_ary_push(ary, ossl_ssl_cipher_to_ary(cipher));
711
728
    }
712
729
    return ary;
717
734
 *    ctx.ciphers = "cipher1:cipher2:..."
718
735
 *    ctx.ciphers = [name, ...]
719
736
 *    ctx.ciphers = [[name, version, bits, alg_bits], ...]
 
737
 *
 
738
 * Sets the list of available ciphers for this context.  Note in a server
 
739
 * context some ciphers require the appropriate certificates.  For example, an
 
740
 * RSA cipher can only be chosen when an RSA certificate is available.
 
741
 *
 
742
 * See also OpenSSL::Cipher and OpenSSL::Cipher::ciphers
720
743
 */
721
744
static VALUE
722
745
ossl_sslctx_set_ciphers(VALUE self, VALUE v)
759
782
 *  call-seq:
760
783
 *     ctx.session_add(session) -> true | false
761
784
 *
 
785
 * Adds +session+ to the session cache
762
786
 */
763
787
static VALUE
764
788
ossl_sslctx_session_add(VALUE self, VALUE arg)
776
800
 *  call-seq:
777
801
 *     ctx.session_remove(session) -> true | false
778
802
 *
 
803
 * Removes +session+ from the session cache
779
804
 */
780
805
static VALUE
781
806
ossl_sslctx_session_remove(VALUE self, VALUE arg)
791
816
 
792
817
/*
793
818
 *  call-seq:
794
 
 *     ctx.session_cache_mode -> integer
 
819
 *     ctx.session_cache_mode -> Integer
795
820
 *
 
821
 * The current session cache mode.
796
822
 */
797
823
static VALUE
798
824
ossl_sslctx_get_session_cache_mode(VALUE self)
806
832
 
807
833
/*
808
834
 *  call-seq:
809
 
 *     ctx.session_cache_mode=(integer) -> integer
 
835
 *     ctx.session_cache_mode=(integer) -> Integer
810
836
 *
 
837
 * Sets the SSL session cache mode.  Bitwise-or together the desired
 
838
 * SESSION_CACHE_* constants to set.  See SSL_CTX_set_session_cache_mode(3) for
 
839
 * details.
811
840
 */
812
841
static VALUE
813
842
ossl_sslctx_set_session_cache_mode(VALUE self, VALUE arg)
823
852
 
824
853
/*
825
854
 *  call-seq:
826
 
 *     ctx.session_cache_size -> integer
 
855
 *     ctx.session_cache_size -> Integer
827
856
 *
 
857
 * Returns the current session cache size.  Zero is used to represent an
 
858
 * unlimited cache size.
828
859
 */
829
860
static VALUE
830
861
ossl_sslctx_get_session_cache_size(VALUE self)
838
869
 
839
870
/*
840
871
 *  call-seq:
841
 
 *     ctx.session_cache_size=(integer) -> integer
 
872
 *     ctx.session_cache_size=(integer) -> Integer
842
873
 *
 
874
 * Sets the session cache size.  Returns the previously valid session cache
 
875
 * size.  Zero is used to represent an unlimited session cache size.
843
876
 */
844
877
static VALUE
845
878
ossl_sslctx_set_session_cache_size(VALUE self, VALUE arg)
857
890
 *  call-seq:
858
891
 *     ctx.session_cache_stats -> Hash
859
892
 *
 
893
 * Returns a Hash containing the following keys:
 
894
 *
 
895
 * :accept:: Number of started SSL/TLS handshakes in server mode
 
896
 * :accept_good:: Number of established SSL/TLS sessions in server mode
 
897
 * :accept_renegotiate:: Number of start renegotiations in server mode
 
898
 * :cache_full:: Number of sessions that were removed due to cache overflow
 
899
 * :cache_hits:: Number of successfully reused connections
 
900
 * :cache_misses:: Number of sessions proposed by clients that were not found
 
901
 *                 in the cache
 
902
 * :cache_num:: Number of sessions in the internal session cache
 
903
 * :cb_hits:: Number of sessions retrieved from the external cache in server
 
904
 *            mode
 
905
 * :connect:: Number of started SSL/TLS handshakes in client mode
 
906
 * :connect_good:: Number of established SSL/TLS sessions in client mode
 
907
 * :connect_renegotiate:: Number of start renegotiations in client mode
 
908
 * :timeouts:: Number of sessions proposed by clients that were found in the
 
909
 *             cache but had expired due to timeouts
860
910
 */
861
911
static VALUE
862
912
ossl_sslctx_get_session_cache_stats(VALUE self)
888
938
 *  call-seq:
889
939
 *     ctx.flush_sessions(time | nil) -> self
890
940
 *
 
941
 * Removes sessions in the internal cache that have expired at +time+.
891
942
 */
892
943
static VALUE
893
944
ossl_sslctx_flush_sessions(int argc, VALUE *argv, VALUE self)
905
956
    } else if (rb_obj_is_instance_of(arg1, rb_cTime)) {
906
957
        tm = NUM2LONG(rb_funcall(arg1, rb_intern("to_i"), 0));
907
958
    } else {
908
 
        rb_raise(rb_eArgError, "arg must be Time or nil");
 
959
        ossl_raise(rb_eArgError, "arg must be Time or nil");
909
960
    }
910
961
 
911
962
    SSL_CTX_flush_sessions(ctx, (long)tm);
919
970
static void
920
971
ossl_ssl_shutdown(SSL *ssl)
921
972
{
 
973
    int i, rc;
 
974
 
922
975
    if (ssl) {
923
 
        SSL_shutdown(ssl);
924
 
        SSL_clear(ssl);
 
976
        /* 4 is from SSL_smart_shutdown() of mod_ssl.c (v2.2.19) */
 
977
        /* It says max 2x pending + 2x data = 4 */
 
978
        for (i = 0; i < 4; ++i) {
 
979
            /*
 
980
             * Ignore the case SSL_shutdown returns -1. Empty handshake_func
 
981
             * must not happen.
 
982
             */
 
983
            if (rc = SSL_shutdown(ssl))
 
984
                break;
 
985
        }
 
986
        ERR_clear_error();
 
987
        SSL_clear(ssl);
925
988
    }
926
989
}
927
990
 
943
1006
 *    SSLSocket.new(io) => aSSLSocket
944
1007
 *    SSLSocket.new(io, ctx) => aSSLSocket
945
1008
 *
946
 
 * === Parameters
947
 
 * * +io+ is a real ruby IO object.  Not an IO like object that responds to read/write.
948
 
 * * +ctx+ is an OpenSSLSSL::SSLContext.
 
1009
 * Creates a new SSL socket from +io+ which must be a real ruby object (not an
 
1010
 * IO-like object that responds to read/write.
 
1011
 *
 
1012
 * If +ctx+ is provided the SSL Sockets initial params will be taken from
 
1013
 * the context.
949
1014
 *
950
1015
 * The OpenSSL::Buffering module provides additional IO methods.
951
1016
 *
1021
1086
}
1022
1087
 
1023
1088
#ifdef _WIN32
1024
 
#define ssl_get_error(ssl, ret) (errno = rb_w32_map_errno(WSAGetLastError()), SSL_get_error(ssl, ret))
 
1089
#define ssl_get_error(ssl, ret) (errno = rb_w32_map_errno(WSAGetLastError()), SSL_get_error((ssl), (ret)))
1025
1090
#else
1026
 
#define ssl_get_error(ssl, ret) SSL_get_error(ssl, ret)
 
1091
#define ssl_get_error(ssl, ret) SSL_get_error((ssl), (ret))
1027
1092
#endif
1028
1093
 
1029
1094
static void
1091
1156
/*
1092
1157
 * call-seq:
1093
1158
 *    ssl.connect => self
 
1159
 *
 
1160
 * Initiates an SSL/TLS handshake with a server.  The handshake may be started
 
1161
 * after unencrypted data has been sent over the socket.
1094
1162
 */
1095
1163
static VALUE
1096
1164
ossl_ssl_connect(VALUE self)
1103
1171
 * call-seq:
1104
1172
 *    ssl.connect_nonblock => self
1105
1173
 *
1106
 
 * initiate the TLS/SSL handshake as a client in non-blocking manner.
 
1174
 * Initiates the SSL/TLS handshake as a client in non-blocking manner.
1107
1175
 *
1108
1176
 *   # emulates blocking connect
1109
1177
 *   begin
1127
1195
/*
1128
1196
 * call-seq:
1129
1197
 *    ssl.accept => self
 
1198
 *
 
1199
 * Waits for a SSL/TLS client to initiate a handshake.  The handshake may be
 
1200
 * started after unencrypted data has been sent over the socket.
1130
1201
 */
1131
1202
static VALUE
1132
1203
ossl_ssl_accept(VALUE self)
1139
1210
 * call-seq:
1140
1211
 *    ssl.accept_nonblock => self
1141
1212
 *
1142
 
 * initiate the TLS/SSL handshake as a server in non-blocking manner.
 
1213
 * Initiates the SSL/TLS handshake as a server in non-blocking manner.
1143
1214
 *
1144
1215
 *   # emulates blocking accept
1145
1216
 *   begin
1184
1255
        if(!nonblock && SSL_pending(ssl) <= 0)
1185
1256
            rb_thread_wait_fd(FPTR_TO_FD(fptr));
1186
1257
        for (;;){
1187
 
            nread = SSL_read(ssl, RSTRING_PTR(str), RSTRING_LEN(str));
 
1258
            nread = SSL_read(ssl, RSTRING_PTR(str), RSTRING_LENINT(str));
1188
1259
            switch(ssl_get_error(ssl, nread)){
1189
1260
            case SSL_ERROR_NONE:
1190
1261
                goto end;
1225
1296
 *    ssl.sysread(length) => string
1226
1297
 *    ssl.sysread(length, buffer) => buffer
1227
1298
 *
1228
 
 * === Parameters
1229
 
 * * +length+ is a positive integer.
1230
 
 * * +buffer+ is a string used to store the result.
 
1299
 * Reads +length+ bytes from the SSL connection.  If a pre-allocated +buffer+
 
1300
 * is provided the data will be written into it.
1231
1301
 */
1232
1302
static VALUE
1233
1303
ossl_ssl_read(int argc, VALUE *argv, VALUE self)
1240
1310
 *    ssl.sysread_nonblock(length) => string
1241
1311
 *    ssl.sysread_nonblock(length, buffer) => buffer
1242
1312
 *
1243
 
 * === Parameters
1244
 
 * * +length+ is a positive integer.
1245
 
 * * +buffer+ is a string used to store the result.
 
1313
 * A non-blocking version of #sysread.  Raises an SSLError if reading would
 
1314
 * block.
 
1315
 *
 
1316
 * Reads +length+ bytes from the SSL connection.  If a pre-allocated +buffer+
 
1317
 * is provided the data will be written into it.
1246
1318
 */
1247
1319
static VALUE
1248
1320
ossl_ssl_read_nonblock(int argc, VALUE *argv, VALUE self)
1263
1335
 
1264
1336
    if (ssl) {
1265
1337
        for (;;){
1266
 
            nwrite = SSL_write(ssl, RSTRING_PTR(str), RSTRING_LEN(str));
 
1338
            nwrite = SSL_write(ssl, RSTRING_PTR(str), RSTRING_LENINT(str));
1267
1339
            switch(ssl_get_error(ssl, nwrite)){
1268
1340
            case SSL_ERROR_NONE:
1269
1341
                goto end;
1294
1366
 
1295
1367
/*
1296
1368
 * call-seq:
1297
 
 *    ssl.syswrite(string) => integer
 
1369
 *    ssl.syswrite(string) => Integer
 
1370
 *
 
1371
 * Writes +string+ to the SSL connection.
1298
1372
 */
1299
1373
static VALUE
1300
1374
ossl_ssl_write(VALUE self, VALUE str)
1304
1378
 
1305
1379
/*
1306
1380
 * call-seq:
1307
 
 *    ssl.syswrite_nonblock(string) => integer
 
1381
 *    ssl.syswrite_nonblock(string) => Integer
 
1382
 *
 
1383
 * Writes +string+ to the SSL connection in a non-blocking manner.  Raises an
 
1384
 * SSLError if writing would block.
1308
1385
 */
1309
1386
static VALUE
1310
1387
ossl_ssl_write_nonblock(VALUE self, VALUE str)
1315
1392
/*
1316
1393
 * call-seq:
1317
1394
 *    ssl.sysclose => nil
 
1395
 *
 
1396
 * Shuts down the SSL connection and prepares it for another connection.
1318
1397
 */
1319
1398
static VALUE
1320
1399
ossl_ssl_close(VALUE self)
1332
1411
/*
1333
1412
 * call-seq:
1334
1413
 *    ssl.cert => cert or nil
 
1414
 *
 
1415
 * The X509 certificate for this socket endpoint.
1335
1416
 */
1336
1417
static VALUE
1337
1418
ossl_ssl_get_cert(VALUE self)
1360
1441
/*
1361
1442
 * call-seq:
1362
1443
 *    ssl.peer_cert => cert or nil
 
1444
 *
 
1445
 * The X509 certificate for this socket's peer.
1363
1446
 */
1364
1447
static VALUE
1365
1448
ossl_ssl_get_peer_cert(VALUE self)
1389
1472
/*
1390
1473
 * call-seq:
1391
1474
 *    ssl.peer_cert_chain => [cert, ...] or nil
 
1475
 *
 
1476
 * The X509 certificate chain for this socket's peer.
1392
1477
 */
1393
1478
static VALUE
1394
1479
ossl_ssl_get_peer_cert_chain(VALUE self)
1419
1504
/*
1420
1505
 * call-seq:
1421
1506
 *    ssl.cipher => [name, version, bits, alg_bits]
 
1507
 *
 
1508
 * The cipher being used for the current connection
1422
1509
 */
1423
1510
static VALUE
1424
1511
ossl_ssl_get_cipher(VALUE self)
1439
1526
/*
1440
1527
 * call-seq:
1441
1528
 *    ssl.state => string
 
1529
 *
 
1530
 * A description of the current connection state.
1442
1531
 */
1443
1532
static VALUE
1444
1533
ossl_ssl_get_state(VALUE self)
1461
1550
 
1462
1551
/*
1463
1552
 * call-seq:
1464
 
 *    ssl.pending => integer
 
1553
 *    ssl.pending => Integer
 
1554
 *
 
1555
 * The number of bytes that are immediately available for reading
1465
1556
 */
1466
1557
static VALUE
1467
1558
ossl_ssl_pending(VALUE self)
1478
1569
}
1479
1570
 
1480
1571
/*
1481
 
 *  call-seq:
1482
 
 *     ssl.session_reused? -> true | false
 
1572
 * call-seq:
 
1573
 *    ssl.session_reused? -> true | false
1483
1574
 *
 
1575
 * Returns true if a reused session was negotiated during the handshake.
1484
1576
 */
1485
1577
static VALUE
1486
1578
ossl_ssl_session_reused(VALUE self)
1501
1593
}
1502
1594
 
1503
1595
/*
1504
 
 *  call-seq:
1505
 
 *     ssl.session = session -> session
 
1596
 * call-seq:
 
1597
 *    ssl.session = session -> session
1506
1598
 *
 
1599
 * Sets the Session to be used when the connection is established.
1507
1600
 */
1508
1601
static VALUE
1509
1602
ossl_ssl_set_session(VALUE self, VALUE arg1)
1528
1621
    return arg1;
1529
1622
}
1530
1623
 
 
1624
/*
 
1625
 * call-seq:
 
1626
 *    ssl.verify_result => Integer
 
1627
 *
 
1628
 * Returns the result of the peer certificates verification.  See verify(1)
 
1629
 * for error values and descriptions.
 
1630
 *
 
1631
 * If no peer certificate was presented X509_V_OK is returned.
 
1632
 */
1531
1633
static VALUE
1532
1634
ossl_ssl_get_verify_result(VALUE self)
1533
1635
{
1542
1644
    return INT2FIX(SSL_get_verify_result(ssl));
1543
1645
}
1544
1646
 
 
1647
/*
 
1648
 * call-seq:
 
1649
 *    ssl.client_ca => [x509name, ...]
 
1650
 *
 
1651
 * Returns the list of client CAs. Please note that in contrast to
 
1652
 * SSLContext#client_ca= no array of X509::Certificate is returned but
 
1653
 * X509::Name instances of the CA's subject distinguished name.
 
1654
 *
 
1655
 * In server mode, returns the list set by SSLContext#client_ca=.
 
1656
 * In client mode, returns the list of client CAs sent from the server.
 
1657
 */
 
1658
static VALUE
 
1659
ossl_ssl_get_client_ca_list(VALUE self)
 
1660
{
 
1661
    SSL *ssl;
 
1662
    STACK_OF(X509_NAME) *ca;
 
1663
 
 
1664
    Data_Get_Struct(self, SSL, ssl);
 
1665
    if (!ssl) {
 
1666
        rb_warning("SSL session is not started yet.");
 
1667
        return Qnil;
 
1668
    }
 
1669
 
 
1670
    ca = SSL_get_client_CA_list(ssl);
 
1671
    return ossl_x509name_sk2ary(ca);
 
1672
}
 
1673
 
1545
1674
void
1546
1675
Init_ossl_ssl()
1547
1676
{
1548
1677
    int i;
1549
1678
    VALUE ary;
1550
1679
 
1551
 
#if 0 /* let rdoc know about mOSSL */
1552
 
    mOSSL = rb_define_module("OpenSSL");
 
1680
#if 0
 
1681
    mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
1553
1682
#endif
1554
1683
 
1555
1684
    ID_callback_state = rb_intern("@callback_state");
1567
1696
 
1568
1697
    Init_ossl_ssl_session();
1569
1698
 
1570
 
    /* class SSLContext
1571
 
     *
1572
 
     * The following attributes are available but don't show up in rdoc.
1573
 
     * All attributes must be set before calling SSLSocket.new(io, ctx).
 
1699
    /* Document-class: OpenSSL::SSL::SSLContext
 
1700
     *
 
1701
     * An SSLContext is used to set various options regarding certificates,
 
1702
     * algorithms, verification, session caching, etc.  The SSLContext is
 
1703
     * used to create an SSLSocket.
 
1704
     *
 
1705
     * All attributes must be set before creating an SSLSocket as the
 
1706
     * SSLContext will be frozen afterward.
 
1707
     *
 
1708
     * The following attributes are available but don't show up in rdoc:
1574
1709
     * * ssl_version, cert, key, client_ca, ca_file, ca_path, timeout,
1575
1710
     * * verify_mode, verify_depth client_cert_cb, tmp_dh_callback,
1576
1711
     * * session_id_context, session_add_cb, session_new_cb, session_remove_cb
1577
1712
     */
1578
1713
    cSSLContext = rb_define_class_under(mSSL, "SSLContext", rb_cObject);
1579
1714
    rb_define_alloc_func(cSSLContext, ossl_sslctx_s_alloc);
1580
 
    for(i = 0; i < numberof(ossl_sslctx_attrs); i++)
1581
 
        rb_attr(cSSLContext, rb_intern(ossl_sslctx_attrs[i]), 1, 1, Qfalse);
 
1715
 
 
1716
    /*
 
1717
     * Context certificate
 
1718
     */
 
1719
    rb_attr(cSSLContext, rb_intern("cert"), 1, 1, Qfalse);
 
1720
 
 
1721
    /*
 
1722
     * Context private key
 
1723
     */
 
1724
    rb_attr(cSSLContext, rb_intern("key"), 1, 1, Qfalse);
 
1725
 
 
1726
    /*
 
1727
     * A certificate or Array of certificates that will be sent to the client.
 
1728
     */
 
1729
    rb_attr(cSSLContext, rb_intern("client_ca"), 1, 1, Qfalse);
 
1730
 
 
1731
    /*
 
1732
     * The path to a file containing a PEM-format CA certificate
 
1733
     */
 
1734
    rb_attr(cSSLContext, rb_intern("ca_file"), 1, 1, Qfalse);
 
1735
 
 
1736
    /*
 
1737
     * The path to a directory containing CA certificates in PEM format.
 
1738
     *
 
1739
     * Files are looked up by subject's X509 name's hash value.
 
1740
     */
 
1741
    rb_attr(cSSLContext, rb_intern("ca_path"), 1, 1, Qfalse);
 
1742
 
 
1743
    /*
 
1744
     * Maximum session lifetime.
 
1745
     */
 
1746
    rb_attr(cSSLContext, rb_intern("timeout"), 1, 1, Qfalse);
 
1747
 
 
1748
    /*
 
1749
     * Session verification mode.
 
1750
     *
 
1751
     * Valid modes are VERIFY_NONE, VERIFY_PEER, VERIFY_CLIENT_ONCE,
 
1752
     * VERIFY_FAIL_IF_NO_PEER_CERT and defined on OpenSSL::SSL
 
1753
     */
 
1754
    rb_attr(cSSLContext, rb_intern("verify_mode"), 1, 1, Qfalse);
 
1755
 
 
1756
    /*
 
1757
     * Number of CA certificates to walk when verifying a certificate chain.
 
1758
     */
 
1759
    rb_attr(cSSLContext, rb_intern("verify_depth"), 1, 1, Qfalse);
 
1760
 
 
1761
    /*
 
1762
     * A callback for additional certificate verification.  The callback is
 
1763
     * invoked for each certificate in the chain.
 
1764
     *
 
1765
     * The callback is invoked with two values.  +preverify_ok+ indicates
 
1766
     * indicates if the verification was passed (true) or not (false).
 
1767
     * +store_context+ is an OpenSSL::X509::StoreContext containing the
 
1768
     * context used for certificate verification.
 
1769
     *
 
1770
     * If the callback returns false verification is stopped.
 
1771
     */
 
1772
    rb_attr(cSSLContext, rb_intern("verify_callback"), 1, 1, Qfalse);
 
1773
 
 
1774
    /*
 
1775
     * Sets various OpenSSL options.
 
1776
     */
 
1777
    rb_attr(cSSLContext, rb_intern("options"), 1, 1, Qfalse);
 
1778
 
 
1779
    /*
 
1780
     * An OpenSSL::X509::Store used for certificate verification
 
1781
     */
 
1782
    rb_attr(cSSLContext, rb_intern("cert_store"), 1, 1, Qfalse);
 
1783
 
 
1784
    /*
 
1785
     * An Array of extra X509 certificates to be added to the certificate
 
1786
     * chain.
 
1787
     */
 
1788
    rb_attr(cSSLContext, rb_intern("extra_chain_cert"), 1, 1, Qfalse);
 
1789
 
 
1790
    /*
 
1791
     * A callback invoked when a client certificate is requested by a server
 
1792
     * and no certificate has been set.
 
1793
     *
 
1794
     * The callback is invoked with a Session and must return an Array
 
1795
     * containing an OpenSSL::X509::Certificate and an OpenSSL::PKey.  If any
 
1796
     * other value is returned the handshake is suspended.
 
1797
     */
 
1798
    rb_attr(cSSLContext, rb_intern("client_cert_cb"), 1, 1, Qfalse);
 
1799
 
 
1800
    /*
 
1801
     * A callback invoked when DH parameters are required.
 
1802
     *
 
1803
     * The callback is invoked with the Session for the key exchange, an
 
1804
     * flag indicating the use of an export cipher and the keylength
 
1805
     * required.
 
1806
     *
 
1807
     * The callback must return an OpenSSL::PKey::DH instance of the correct
 
1808
     * key length.
 
1809
     */
 
1810
    rb_attr(cSSLContext, rb_intern("tmp_dh_callback"), 1, 1, Qfalse);
 
1811
 
 
1812
    /*
 
1813
     * Sets the context in which a session can be reused.  This allows
 
1814
     * sessions for multiple applications to be distinguished, for exapmle, by
 
1815
     * name.
 
1816
     */
 
1817
    rb_attr(cSSLContext, rb_intern("session_id_context"), 1, 1, Qfalse);
 
1818
 
 
1819
    /*
 
1820
     * A callback invoked on a server when a session is proposed by the client
 
1821
     * but the session could not be found in the server's internal cache.
 
1822
     *
 
1823
     * The callback is invoked with the SSLSocket and session id.  The
 
1824
     * callback may return a Session from an external cache.
 
1825
     */
 
1826
    rb_attr(cSSLContext, rb_intern("session_get_cb"), 1, 1, Qfalse);
 
1827
 
 
1828
    /*
 
1829
     * A callback invoked when a new session was negotiatied.
 
1830
     *
 
1831
     * The callback is invoked with an SSLSocket.  If false is returned the
 
1832
     * session will be removed from the internal cache.
 
1833
     */
 
1834
    rb_attr(cSSLContext, rb_intern("session_new_cb"), 1, 1, Qfalse);
 
1835
 
 
1836
    /*
 
1837
     * A callback invoked when a session is removed from the internal cache.
 
1838
     *
 
1839
     * The callback is invoked with an SSLContext and a Session.
 
1840
     */
 
1841
    rb_attr(cSSLContext, rb_intern("session_remove_cb"), 1, 1, Qfalse);
 
1842
 
 
1843
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
 
1844
    /*
 
1845
     * A callback invoked at connect time to distinguish between multiple
 
1846
     * server names.
 
1847
     *
 
1848
     * The callback is invoked with an SSLSocket and a server name.  The
 
1849
     * callback must return an SSLContext for the server name or nil.
 
1850
     */
 
1851
    rb_attr(cSSLContext, rb_intern("servername_cb"), 1, 1, Qfalse);
 
1852
#endif
 
1853
 
1582
1854
    rb_define_alias(cSSLContext, "ssl_timeout", "timeout");
1583
1855
    rb_define_alias(cSSLContext, "ssl_timeout=", "timeout=");
1584
1856
    rb_define_method(cSSLContext, "initialize",  ossl_sslctx_initialize, -1);
1589
1861
    rb_define_method(cSSLContext, "setup", ossl_sslctx_setup, 0);
1590
1862
 
1591
1863
 
 
1864
    /*
 
1865
     * No session caching for client or server
 
1866
     */
1592
1867
    rb_define_const(cSSLContext, "SESSION_CACHE_OFF", LONG2FIX(SSL_SESS_CACHE_OFF));
 
1868
 
 
1869
    /*
 
1870
     * Client sessions are added to the session cache
 
1871
     */
1593
1872
    rb_define_const(cSSLContext, "SESSION_CACHE_CLIENT", LONG2FIX(SSL_SESS_CACHE_CLIENT)); /* doesn't actually do anything in 0.9.8e */
 
1873
 
 
1874
    /*
 
1875
     * Server sessions are added to the session cache
 
1876
     */
1594
1877
    rb_define_const(cSSLContext, "SESSION_CACHE_SERVER", LONG2FIX(SSL_SESS_CACHE_SERVER));
 
1878
 
 
1879
    /*
 
1880
     * Both client and server sessions are added to the session cache
 
1881
     */
1595
1882
    rb_define_const(cSSLContext, "SESSION_CACHE_BOTH", LONG2FIX(SSL_SESS_CACHE_BOTH)); /* no different than CACHE_SERVER in 0.9.8e */
 
1883
 
 
1884
    /*
 
1885
     * Normally the sesison cache is checked for expired sessions every 255
 
1886
     * connections.  Since this may lead to a delay that cannot be controlled,
 
1887
     * the automatic flushing may be disabled and #flush_sessions can be
 
1888
     * called explicitly.
 
1889
     */
1596
1890
    rb_define_const(cSSLContext, "SESSION_CACHE_NO_AUTO_CLEAR", LONG2FIX(SSL_SESS_CACHE_NO_AUTO_CLEAR));
 
1891
 
 
1892
    /*
 
1893
     * Always perform external lookups of sessions even if they are in the
 
1894
     * internal cache.
 
1895
     *
 
1896
     * This flag has no effect on clients
 
1897
     */
1597
1898
    rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL_LOOKUP", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL_LOOKUP));
 
1899
 
 
1900
    /*
 
1901
     * Never automatically store sessions in the internal store.
 
1902
     */
1598
1903
    rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL_STORE", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL_STORE));
 
1904
 
 
1905
    /*
 
1906
     * Enables both SESSION_CACHE_NO_INTERNAL_LOOKUP and
 
1907
     * SESSION_CACHE_NO_INTERNAL_STORE.
 
1908
     */
1599
1909
    rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL));
 
1910
 
1600
1911
    rb_define_method(cSSLContext, "session_add",     ossl_sslctx_session_add, 1);
1601
1912
    rb_define_method(cSSLContext, "session_remove",     ossl_sslctx_session_remove, 1);
1602
1913
    rb_define_method(cSSLContext, "session_cache_mode",     ossl_sslctx_get_session_cache_mode, 0);
1611
1922
        rb_ary_push(ary, ID2SYM(rb_intern(ossl_ssl_method_tab[i].name)));
1612
1923
    }
1613
1924
    rb_obj_freeze(ary);
1614
 
    /* holds a list of available SSL/TLS methods */
 
1925
    /* The list of available SSL/TLS methods */
1615
1926
    rb_define_const(cSSLContext, "METHODS", ary);
1616
1927
 
1617
 
    /* class SSLSocket
 
1928
    /*
 
1929
     * Document-class: OpenSSL::SSL::SSLSocket
1618
1930
     *
1619
1931
     * The following attributes are available but don't show up in rdoc.
1620
1932
     * * io, context, sync_close
1646
1958
    rb_define_method(cSSLSocket, "session_reused?",    ossl_ssl_session_reused, 0);
1647
1959
    rb_define_method(cSSLSocket, "session=",    ossl_ssl_set_session, 1);
1648
1960
    rb_define_method(cSSLSocket, "verify_result", ossl_ssl_get_verify_result, 0);
 
1961
    rb_define_method(cSSLSocket, "client_ca", ossl_ssl_get_client_ca_list, 0);
1649
1962
 
1650
1963
#define ossl_ssl_def_const(x) rb_define_const(mSSL, #x, INT2NUM(SSL_##x))
1651
1964
 
1684
1997
#if defined(SSL_OP_NO_TICKET)
1685
1998
    ossl_ssl_def_const(OP_NO_TICKET);
1686
1999
#endif
 
2000
#if defined(SSL_OP_NO_COMPRESSION)
 
2001
    ossl_ssl_def_const(OP_NO_COMPRESSION);
 
2002
#endif
1687
2003
    ossl_ssl_def_const(OP_PKCS1_CHECK_1);
1688
2004
    ossl_ssl_def_const(OP_PKCS1_CHECK_2);
1689
2005
    ossl_ssl_def_const(OP_NETSCAPE_CA_DN_BUG);