diff options
author | Zachary Scott <e@zzak.io> | 2015-03-25 21:33:52 -0700 |
---|---|---|
committer | Zachary Scott <e@zzak.io> | 2015-03-25 21:33:52 -0700 |
commit | 30b43508092659adc0a9f7f038c5e0f9f4435de3 (patch) | |
tree | e28946b3d63f89b5b7de3442d220f1fe50d35eba /ext | |
parent | 86eb72136fdbc11c125d13c675759b380132be03 (diff) | |
download | ruby-openssl-30b43508092659adc0a9f7f038c5e0f9f4435de3.tar.gz |
Upstream the following commits from trunk ruby:
- r49681: 97f9589c4b0641141af32244021dd9eba001b3c7
- r49682: c5d781dded856a86609ebd9fd4904c3e9f3474fd
- r49948: aaf2d070a8351dc3118422bae478978f3d3e3966
- r49954: ddf2558a167652cfec6a901b2116b832221e6e83b
- r49955: 9941f348e056a5e717cb943cee37ba8ba2396e6f
Fixes #6 and #7
Diffstat (limited to 'ext')
-rw-r--r-- | ext/openssl/extconf.rb | 1 | ||||
-rw-r--r-- | ext/openssl/ossl_asn1.c | 2 | ||||
-rw-r--r-- | ext/openssl/ossl_bn.c | 225 | ||||
-rw-r--r-- | ext/openssl/ossl_pkey.c | 6 | ||||
-rw-r--r-- | ext/openssl/ossl_rand.c | 4 | ||||
-rw-r--r-- | ext/openssl/ossl_ssl.c | 62 |
6 files changed, 214 insertions, 86 deletions
diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb index e272cba0..3a1fa716 100644 --- a/ext/openssl/extconf.rb +++ b/ext/openssl/extconf.rb @@ -87,6 +87,7 @@ have_func("HMAC_CTX_init") have_func("PEM_def_callback") have_func("PKCS5_PBKDF2_HMAC") have_func("PKCS5_PBKDF2_HMAC_SHA1") +have_func("RAND_egd") have_func("X509V3_set_nconf") have_func("X509V3_EXT_nconf_nid") have_func("X509_CRL_add0_revoked") diff --git a/ext/openssl/ossl_asn1.c b/ext/openssl/ossl_asn1.c index 7efb0475..6c91d145 100644 --- a/ext/openssl/ossl_asn1.c +++ b/ext/openssl/ossl_asn1.c @@ -1424,7 +1424,7 @@ ossl_asn1obj_get_ln(VALUE self) return ret; } -/* Document-method: OpenSSL::ASN1::ObjectId.oid +/* Document-method: OpenSSL::ASN1::ObjectId#oid * * The object identifier as a +String+, e.g. "1.2.3.4.5" */ diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c index b5de7ecf..b794343f 100644 --- a/ext/openssl/ossl_bn.c +++ b/ext/openssl/ossl_bn.c @@ -329,11 +329,6 @@ ossl_bn_coerce(VALUE self, VALUE other) } #define BIGNUM_BOOL1(func) \ - /* \ - * call-seq: \ - * bn.##func => true | false \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self) \ { \ @@ -344,16 +339,26 @@ ossl_bn_coerce(VALUE self, VALUE other) } \ return Qfalse; \ } + +/* + * Document-method: OpenSSL::BN#zero? + * bn.zero? => true | false + */ BIGNUM_BOOL1(is_zero) + +/* + * Document-method: OpenSSL::BN#one? + * bn.one? => true | false + */ BIGNUM_BOOL1(is_one) + +/* + * Document-method: OpenSSL::BN#odd? + * bn.odd? => true | false + */ BIGNUM_BOOL1(is_odd) #define BIGNUM_1c(func) \ - /* \ - * call-seq: \ - * bn.##func => aBN \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self) \ { \ @@ -370,14 +375,14 @@ BIGNUM_BOOL1(is_odd) WrapBN(CLASS_OF(self), obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN#sqr + * bn.sqr => aBN + */ BIGNUM_1c(sqr) #define BIGNUM_2(func) \ - /* \ - * call-seq: \ - * bn.##func(bn2) => aBN \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE other) \ { \ @@ -394,15 +399,20 @@ BIGNUM_1c(sqr) WrapBN(CLASS_OF(self), obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN#+ + * bn + bn2 => aBN + */ BIGNUM_2(add) + +/* + * Document-method: OpenSSL::BN#- + * bn - bn2 => aBN + */ BIGNUM_2(sub) #define BIGNUM_2c(func) \ - /* \ - * call-seq: \ - * bn.##func(bn2) => aBN \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE other) \ { \ @@ -419,15 +429,45 @@ BIGNUM_2(sub) WrapBN(CLASS_OF(self), obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN#* + * bn * bn2 => aBN + */ BIGNUM_2c(mul) + +/* + * Document-method: OpenSSL::BN#% + * bn % bn2 => aBN + */ BIGNUM_2c(mod) + +/* + * Document-method: OpenSSL::BN#** + * bn ** bn2 => aBN + */ BIGNUM_2c(exp) + +/* + * Document-method: OpenSSL::BN#gcd + * bn.gcd(bn2) => aBN + */ BIGNUM_2c(gcd) + +/* + * Document-method: OpenSSL::BN#mod_sqr + * bn.mod_sqr(bn2) => aBN + */ BIGNUM_2c(mod_sqr) + +/* + * Document-method: OpenSSL::BN#mod_inverse + * bn.mod_inverse(bn2) => aBN + */ BIGNUM_2c(mod_inverse) /* - * call-seq: + * Document-method: OpenSSL::BN#/ * bn1 / bn2 => [result, remainder] * * Division of OpenSSL::BN instances @@ -459,11 +499,6 @@ ossl_bn_div(VALUE self, VALUE other) } #define BIGNUM_3c(func) \ - /* \ - * call-seq: \ - * bn.##func(bn1, bn2) -> aBN \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \ { \ @@ -481,17 +516,32 @@ ossl_bn_div(VALUE self, VALUE other) WrapBN(CLASS_OF(self), obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN#mod_add + * bn.mod_add(bn1, bn2) -> aBN + */ BIGNUM_3c(mod_add) + +/* + * Document-method: OpenSSL::BN#mod_sub + * bn.mod_sub(bn1, bn2) -> aBN + */ BIGNUM_3c(mod_sub) + +/* + * Document-method: OpenSSL::BN#mod_mul + * bn.mod_mul(bn1, bn2) -> aBN + */ BIGNUM_3c(mod_mul) + +/* + * Document-method: OpenSSL::BN#mod_exp + * bn.mod_exp(bn1, bn2) -> aBN + */ BIGNUM_3c(mod_exp) #define BIGNUM_BIT(func) \ - /* \ - * call-seq: \ - * bn.##func(bit) -> self \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE bit) \ { \ @@ -502,8 +552,23 @@ BIGNUM_3c(mod_exp) } \ return self; \ } + +/* + * Document-method: OpenSSL::BN#set_bit! + * bn.set_bit!(bit) -> self + */ BIGNUM_BIT(set_bit) + +/* + * Document-method: OpenSSL::BN#clear_bit! + * bn.clear_bit!(bit) -> self + */ BIGNUM_BIT(clear_bit) + +/* + * Document-method: OpenSSL::BN#mask_bit! + * bn.mask_bit!(bit) -> self + */ BIGNUM_BIT(mask_bits) /* Document-method: OpenSSL::BN#bit_set? @@ -529,11 +594,6 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit) } #define BIGNUM_SHIFT(func) \ - /* \ - * call-seq: \ - * bn.##func(bits) -> aBN \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE bits) \ { \ @@ -552,15 +612,22 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit) WrapBN(CLASS_OF(self), obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN#<< + * call-seq: + * bn << bits -> aBN + */ BIGNUM_SHIFT(lshift) + +/* + * Document-method: OpenSSL::BN#>> + * call-seq: + * bn >> bits -> aBN + */ BIGNUM_SHIFT(rshift) #define BIGNUM_SELF_SHIFT(func) \ - /* \ - * call-seq: \ - * bn.##func!(bits) -> self \ - * \ - */ \ static VALUE \ ossl_bn_self_##func(VALUE self, VALUE bits) \ { \ @@ -572,15 +639,20 @@ BIGNUM_SHIFT(rshift) ossl_raise(eBNError, NULL); \ return self; \ } + +/* + * Document-method: OpenSSL::BN#lshift! + * bn.lshift!(bits) -> self + */ BIGNUM_SELF_SHIFT(lshift) + +/* + * Document-method: OpenSSL::BN#rshift! + * bn.rshift!(bits) -> self + */ BIGNUM_SELF_SHIFT(rshift) #define BIGNUM_RAND(func) \ - /* \ - * call-seq: \ - * BN.##func(bits [, fill [, odd]]) -> aBN \ - * \ - */ \ static VALUE \ ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \ { \ @@ -606,15 +678,20 @@ BIGNUM_SELF_SHIFT(rshift) WrapBN(klass, obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN.rand + * BN.rand(bits [, fill [, odd]]) -> aBN + */ BIGNUM_RAND(rand) + +/* + * Document-method: OpenSSL::BN.pseudo_rand + * BN.pseudo_rand(bits [, fill [, odd]]) -> aBN + */ BIGNUM_RAND(pseudo_rand) #define BIGNUM_RAND_RANGE(func) \ - /* \ - * call-seq: \ - * BN.##func(range) -> aBN \ - * \ - */ \ static VALUE \ ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ { \ @@ -630,7 +707,19 @@ BIGNUM_RAND(pseudo_rand) WrapBN(klass, obj, result); \ return obj; \ } + +/* + * Document-method: OpenSSL::BN.rand_range + * BN.rand_range(range) -> aBN + * + */ BIGNUM_RAND_RANGE(rand) + +/* + * Document-method: OpenSSL::BN.pseudo_rand_range + * BN.pseudo_rand_range(range) -> aBN + * + */ BIGNUM_RAND_RANGE(pseudo_rand) /* @@ -674,11 +763,6 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass) } #define BIGNUM_NUM(func) \ - /* \ - * call-seq: \ - * bn.##func => integer \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self) \ { \ @@ -686,7 +770,17 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass) GetBN(self, bn); \ return INT2FIX(BN_##func(bn)); \ } + +/* + * Document-method: OpenSSL::BN#num_bytes + * bn.num_bytes => integer + */ BIGNUM_NUM(num_bytes) + +/* + * Document-method: OpenSSL::BN#num_bits + * bn.num_bits => integer + */ BIGNUM_NUM(num_bits) static VALUE @@ -708,11 +802,6 @@ ossl_bn_copy(VALUE self, VALUE other) } #define BIGNUM_CMP(func) \ - /* \ - * call-seq: \ - * bn.##func(bn2) => integer \ - * \ - */ \ static VALUE \ ossl_bn_##func(VALUE self, VALUE other) \ { \ @@ -720,7 +809,21 @@ ossl_bn_copy(VALUE self, VALUE other) GetBN(self, bn1); \ return INT2FIX(BN_##func(bn1, bn2)); \ } + +/* + * Document-method: OpenSSL::BN#cmp + * bn.cmp(bn2) => integer + */ +/* + * Document-method: OpenSSL::BN#<=> + * bn <=> bn2 => integer + */ BIGNUM_CMP(cmp) + +/* + * Document-method: OpenSSL::BN#ucmp + * bn.ucmp(bn2) => integer + */ BIGNUM_CMP(ucmp) /* diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index aa9b046d..f781677c 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -199,7 +199,7 @@ GetPrivPKeyPtr(VALUE obj) { EVP_PKEY *pkey; - if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) { + if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) { ossl_raise(rb_eArgError, "Private key is needed."); } SafeGetPKey(obj, pkey); @@ -223,7 +223,7 @@ DupPrivPKeyPtr(VALUE obj) { EVP_PKEY *pkey; - if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) { + if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) { ossl_raise(rb_eArgError, "Private key is needed."); } SafeGetPKey(obj, pkey); @@ -290,7 +290,7 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data) unsigned int buf_len; VALUE str; - if (rb_funcall(self, id_private_q, 0, NULL) != Qtrue) { + if (rb_funcallv(self, id_private_q, 0, NULL) != Qtrue) { ossl_raise(rb_eArgError, "Private key is needed."); } GetPKey(self, pkey); diff --git a/ext/openssl/ossl_rand.c b/ext/openssl/ossl_rand.c index 29cbf8c3..27466fe2 100644 --- a/ext/openssl/ossl_rand.c +++ b/ext/openssl/ossl_rand.c @@ -148,6 +148,7 @@ ossl_rand_pseudo_bytes(VALUE self, VALUE len) return str; } +#ifdef HAVE_RAND_EGD /* * call-seq: * egd(filename) -> true @@ -186,6 +187,7 @@ ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len) } return Qtrue; } +#endif /* HAVE_RAND_EGD */ /* * call-seq: @@ -219,8 +221,10 @@ Init_ossl_rand(void) rb_define_module_function(mRandom, "write_random_file", ossl_rand_write_file, 1); rb_define_module_function(mRandom, "random_bytes", ossl_rand_bytes, 1); rb_define_module_function(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1); +#ifdef HAVE_RAND_EGD rb_define_module_function(mRandom, "egd", ossl_rand_egd, 1); rb_define_module_function(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2); +#endif /* HAVE_RAND_EGD */ rb_define_module_function(mRandom, "status?", ossl_rand_status, 0); } diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index af93252e..1a67d35d 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -107,7 +107,7 @@ static const char *ossl_ssl_attrs[] = { ID ID_callback_state; -static VALUE sym_exception; +static VALUE sym_exception, sym_wait_readable, sym_wait_writable; /* * SSLContext class @@ -1271,7 +1271,8 @@ read_would_block(int nonblock) } static VALUE -ossl_start_ssl(VALUE self, int (*func)(), const char *funcname, int nonblock) +ossl_start_ssl(VALUE self, int (*func)(), const char *funcname, + int nonblock, int no_exception) { SSL *ssl; rb_io_t *fptr; @@ -1295,10 +1296,12 @@ ossl_start_ssl(VALUE self, int (*func)(), const char *funcname, int nonblock) switch((ret2 = ssl_get_error(ssl, ret))){ case SSL_ERROR_WANT_WRITE: + if (no_exception) { return sym_wait_writable; } write_would_block(nonblock); rb_io_wait_writable(FPTR_TO_FD(fptr)); continue; case SSL_ERROR_WANT_READ: + if (no_exception) { return sym_wait_readable; } read_would_block(nonblock); rb_io_wait_readable(FPTR_TO_FD(fptr)); continue; @@ -1324,7 +1327,7 @@ static VALUE ossl_ssl_connect(VALUE self) { ossl_ssl_setup(self); - return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0); + return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0, 0); } /* @@ -1349,7 +1352,7 @@ static VALUE ossl_ssl_connect_nonblock(VALUE self) { ossl_ssl_setup(self); - return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1); + return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1, 0); } /* @@ -1363,12 +1366,20 @@ static VALUE ossl_ssl_accept(VALUE self) { ossl_ssl_setup(self); - return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0); + return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0, 0); +} + +static int +get_no_exception(VALUE opts) +{ + if (!NIL_P(opts) && Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef)) + return 1; + return 0; } /* * call-seq: - * ssl.accept_nonblock => self + * ssl.accept_nonblock([options]) => self * * Initiates the SSL/TLS handshake as a server in non-blocking manner. * @@ -1383,12 +1394,22 @@ ossl_ssl_accept(VALUE self) * retry * end * + * By specifying `exception: false`, the options hash allows you to indicate + * that accept_nonblock should not raise an IO::WaitReadable or + * IO::WaitWritable exception, but return the symbol :wait_readable or + * :wait_writable instead. */ static VALUE -ossl_ssl_accept_nonblock(VALUE self) +ossl_ssl_accept_nonblock(int argc, VALUE *argv, VALUE self) { + int no_exception; + VALUE opts = Qnil; + + rb_scan_args(argc, argv, "0:", &opts); + no_exception = get_no_exception(opts); + ossl_ssl_setup(self); - return ossl_start_ssl(self, SSL_accept, "SSL_accept", 1); + return ossl_start_ssl(self, SSL_accept, "SSL_accept", 1, no_exception); } static VALUE @@ -1396,15 +1417,13 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock) { SSL *ssl; int ilen, nread = 0; - int no_exception = 0; + int no_exception; VALUE len, str; rb_io_t *fptr; VALUE opts = Qnil; rb_scan_args(argc, argv, "11:", &len, &str, &opts); - - if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception)) - no_exception = 1; + no_exception = get_no_exception(opts); ilen = NUM2INT(len); if(NIL_P(str)) str = rb_str_new(0, ilen); @@ -1429,12 +1448,12 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock) if (no_exception) { return Qnil; } rb_eof_error(); case SSL_ERROR_WANT_WRITE: - if (no_exception) { return ID2SYM(rb_intern("wait_writable")); } + if (no_exception) { return sym_wait_writable; } write_would_block(nonblock); rb_io_wait_writable(FPTR_TO_FD(fptr)); continue; case SSL_ERROR_WANT_READ: - if (no_exception) { return ID2SYM(rb_intern("wait_readable")); } + if (no_exception) { return sym_wait_readable; } read_would_block(nonblock); rb_io_wait_readable(FPTR_TO_FD(fptr)); continue; @@ -1517,12 +1536,12 @@ ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception) case SSL_ERROR_NONE: goto end; case SSL_ERROR_WANT_WRITE: - if (no_exception) { return ID2SYM(rb_intern("wait_writable")); } + if (no_exception) { return sym_wait_writable; } write_would_block(nonblock); rb_io_wait_writable(FPTR_TO_FD(fptr)); continue; case SSL_ERROR_WANT_READ: - if (no_exception) { return ID2SYM(rb_intern("wait_readable")); } + if (no_exception) { return sym_wait_readable; } read_would_block(nonblock); rb_io_wait_readable(FPTR_TO_FD(fptr)); continue; @@ -1567,12 +1586,10 @@ ossl_ssl_write_nonblock(int argc, VALUE *argv, VALUE self) { VALUE str; VALUE opts = Qnil; - int no_exception = 0; + int no_exception; rb_scan_args(argc, argv, "1:", &str, &opts); - - if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception)) - no_exception = 1; + no_exception = get_no_exception(opts); return ossl_ssl_write_internal(self, str, 1, no_exception); } @@ -2220,7 +2237,7 @@ Init_ossl_ssl(void) rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0); rb_define_method(cSSLSocket, "connect_nonblock", ossl_ssl_connect_nonblock, 0); rb_define_method(cSSLSocket, "accept", ossl_ssl_accept, 0); - rb_define_method(cSSLSocket, "accept_nonblock", ossl_ssl_accept_nonblock, 0); + rb_define_method(cSSLSocket, "accept_nonblock", ossl_ssl_accept_nonblock, -1); rb_define_method(cSSLSocket, "sysread", ossl_ssl_read, -1); rb_define_private_method(cSSLSocket, "sysread_nonblock", ossl_ssl_read_nonblock, -1); rb_define_method(cSSLSocket, "syswrite", ossl_ssl_write, 1); @@ -2298,5 +2315,8 @@ Init_ossl_ssl(void) ossl_ssl_def_const(OP_NETSCAPE_CA_DN_BUG); ossl_ssl_def_const(OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); +#undef rb_intern sym_exception = ID2SYM(rb_intern("exception")); + sym_wait_readable = ID2SYM(rb_intern("wait_readable")); + sym_wait_writable = ID2SYM(rb_intern("wait_writable")); } |