Skip to content

Commit 3930d3b

Browse files
author
technorama
committed
* ext/openssl/{ossl.[ch],ossl_pkey.c} Add documentation.
* ext/openssl/ossl_hmac.c Add reset method. * ext/openssl/ossl_cipher.c (Cipher#update) Take additional buffer argument. * ext/openssl/{ossl_bio.c,ossl_ssl.c,ruby_missing.h} compatibility with 1.8. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 7909990 commit 3930d3b

File tree

10 files changed

+138
-21
lines changed

10 files changed

+138
-21
lines changed

ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
Mon Apr 2 14:55:58 2007 Technorama <oss-ruby@technorama.net>
2+
* ext/openssl/{ossl.[ch],ossl_pkey.c} Add documentation.
3+
4+
* ext/openssl/ossl_hmac.c Add reset method.
5+
6+
* ext/openssl/ossl_cipher.c (Cipher#update) Take additional
7+
buffer argument.
8+
9+
* ext/openssl/{ossl_bio.c,ossl_ssl.c,ruby_missing.h}
10+
compatibility with 1.8.
11+
112
Mon Apr 2 21:55:12 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
213

314
* insns.def (throw), thread.c, yarvcore.h (throwed_errinfo): fixed

ext/openssl/ossl.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ ossl_raise(VALUE exc, const char *fmt, ...)
310310
rb_exc_raise(rb_exc_new(exc, buf, len));
311311
}
312312

313+
/*
314+
* call-seq:
315+
* OpenSSL.errors -> [String...]
316+
*
317+
* See any remaining errors held in queue.
318+
*
319+
* Any errors you see here are probably due to a bug in ruby's OpenSSL implementation.
320+
*/
313321
VALUE
314322
ossl_get_errors()
315323
{
@@ -345,12 +353,23 @@ ossl_debug(const char *fmt, ...)
345353
}
346354
#endif
347355

356+
/*
357+
* call-seq:
358+
* OpenSSL.debug -> true | false
359+
*/
348360
static VALUE
349361
ossl_debug_get(VALUE self)
350362
{
351363
return dOSSL;
352364
}
353365

366+
/*
367+
* call-seq:
368+
* OpenSSL.debug = boolean -> boolean
369+
*
370+
* Turns on or off CRYPTO_MEM_CHECK.
371+
* Also shows some debugging message on stderr.
372+
*/
354373
static VALUE
355374
ossl_debug_set(VALUE self, VALUE val)
356375
{
@@ -427,8 +446,8 @@ Init_openssl()
427446
/*
428447
* Verify callback Proc index for ext-data
429448
*/
430-
ossl_verify_cb_idx =
431-
X509_STORE_CTX_get_ex_new_index(0, "ossl_verify_cb_idx", 0, 0, 0);
449+
if ((ossl_verify_cb_idx = X509_STORE_CTX_get_ex_new_index(0, "ossl_verify_cb_idx", 0, 0, 0)) < 0)
450+
ossl_raise(eOSSLError, "X509_STORE_CTX_get_ex_new_index");
432451

433452
/*
434453
* Init debug core

ext/openssl/ossl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
extern "C" {
1818
#endif
1919

20+
#if 0
21+
mOSSL = rb_define_module("OpenSSL");
22+
mX509 = rb_define_module_under(mOSSL, "X509");
23+
#endif
24+
2025
/*
2126
* OpenSSL has defined RFILE and Ruby has defined RFILE - so undef it!
2227
*/

ext/openssl/ossl_bio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ossl_obj2bio(VALUE obj)
2525

2626
GetOpenFile(obj, fptr);
2727
rb_io_check_readable(fptr);
28-
if ((fd = dup(fptr->fd)) < 0){
28+
if ((fd = dup(FPTR_TO_FD(fptr))) < 0){
2929
rb_sys_fail(0);
3030
}
3131
if (!(fp = fdopen(fd, "r"))){

ext/openssl/ossl_cipher.c

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,25 +295,59 @@ ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
295295
return Qnil;
296296
}
297297

298+
299+
/*
300+
* call-seq:
301+
* cipher << data -> string
302+
*
303+
* === Parameters
304+
* +data+ is a nonempty string.
305+
*
306+
* This method is deprecated and not available in 1.9.x or later.
307+
*/
308+
static VALUE
309+
ossl_cipher_update_deprecated(VALUE self, VALUE data)
310+
{
311+
char *cname;
312+
313+
cname = rb_class2name(rb_obj_class(self));
314+
rb_warning("%s#<< is deprecated; use %s#update instead", cname, cname);
315+
return rb_funcall(self, rb_intern("update"), 1, data);
316+
}
317+
318+
298319
/*
299320
* call-seq:
300-
* cipher.update(string) -> aString
321+
* cipher.update(data [, buffer]) -> string or buffer
301322
*
323+
* === Parameters
324+
* +data+ is a nonempty string.
325+
* +buffer+ is an optional string to store the result.
302326
*/
303327
static VALUE
304-
ossl_cipher_update(VALUE self, VALUE data)
328+
ossl_cipher_update(int argc, VALUE *argv, VALUE self)
305329
{
306330
EVP_CIPHER_CTX *ctx;
307331
char *in;
308332
int in_len, out_len;
309-
VALUE str;
333+
VALUE data, str;
334+
335+
rb_scan_args(argc, argv, "11", &data, &str);
310336

311337
StringValue(data);
312338
in = RSTRING_PTR(data);
313339
if ((in_len = RSTRING_LEN(data)) == 0)
314340
rb_raise(rb_eArgError, "data must not be empty");
315341
GetCipher(self, ctx);
316-
str = rb_str_new(0, in_len+EVP_CIPHER_CTX_block_size(ctx));
342+
out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
343+
344+
if (NIL_P(str)) {
345+
str = rb_str_new(0, out_len);
346+
} else {
347+
StringValue(str);
348+
rb_str_resize(str, out_len);
349+
}
350+
317351
if (!EVP_CipherUpdate(ctx, RSTRING_PTR(str), &out_len, in, in_len))
318352
ossl_raise(eCipherError, NULL);
319353
assert(out_len < RSTRING_LEN(str));
@@ -518,7 +552,10 @@ Init_ossl_cipher(void)
518552
rb_define_method(cCipher, "encrypt", ossl_cipher_encrypt, -1);
519553
rb_define_method(cCipher, "decrypt", ossl_cipher_decrypt, -1);
520554
rb_define_method(cCipher, "pkcs5_keyivgen", ossl_cipher_pkcs5_keyivgen, -1);
521-
rb_define_method(cCipher, "update", ossl_cipher_update, 1);
555+
rb_define_method(cCipher, "update", ossl_cipher_update, -1);
556+
#if RUBY_VERSION_CODE < 190
557+
rb_define_method(cCipher, "<<", ossl_cipher_update_deprecated, 1);
558+
#endif
522559
rb_define_method(cCipher, "final", ossl_cipher_final, 0);
523560
rb_define_method(cCipher, "name", ossl_cipher_name, 0);
524561
rb_define_method(cCipher, "key=", ossl_cipher_set_key, 1);
@@ -528,6 +565,5 @@ Init_ossl_cipher(void)
528565
rb_define_method(cCipher, "iv_len", ossl_cipher_iv_length, 0);
529566
rb_define_method(cCipher, "block_size", ossl_cipher_block_size, 0);
530567
rb_define_method(cCipher, "padding=", ossl_cipher_set_padding, 1);
531-
532-
rb_define_const(mCipher, "PKCS5_SALT_LEN", PKCS5_SALT_LEN);
533568
}
569+

ext/openssl/ossl_hmac.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ ossl_hmac_hexdigest(VALUE self)
172172
return hexdigest;
173173
}
174174

175+
/*
176+
* call-seq:
177+
* hmac.reset -> self
178+
*
179+
*/
180+
static VALUE
181+
ossl_hmac_reset(VALUE self)
182+
{
183+
HMAC_CTX *ctx;
184+
185+
GetHMAC(self, ctx);
186+
HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
187+
188+
return self;
189+
}
190+
175191
/*
176192
* call-seq:
177193
* HMAC.digest(digest, key, data) -> aString
@@ -237,6 +253,7 @@ Init_ossl_hmac()
237253
rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
238254
rb_define_copy_func(cHMAC, ossl_hmac_copy);
239255

256+
rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
240257
rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
241258
rb_define_alias(cHMAC, "<<", "update");
242259
rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0);

ext/openssl/ossl_pkey.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ VALUE ossl_dh_new(EVP_PKEY *);
7878
void Init_ossl_dh(void);
7979

8080
#define OSSL_PKEY_BN(keytype, name) \
81+
/* \
82+
* call-seq: \
83+
* key.##name -> aBN \
84+
*/ \
8185
static VALUE ossl_##keytype##_get_##name(VALUE self) \
8286
{ \
8387
EVP_PKEY *pkey; \
@@ -89,6 +93,10 @@ static VALUE ossl_##keytype##_get_##name(VALUE self) \
8993
return Qnil; \
9094
return ossl_bn_new(bn); \
9195
} \
96+
/* \
97+
* call-seq: \
98+
* key.##name = bn -> bn \
99+
*/ \
92100
static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
93101
{ \
94102
EVP_PKEY *pkey; \

ext/openssl/ossl_pkey_dh.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
483483
return dh;
484484
}
485485

486-
/*
487-
* TEST
488-
*/
486+
/*
487+
* INIT
488+
*/
489489
void
490490
Init_ossl_dh()
491491
{

ext/openssl/ossl_ssl.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ ossl_ssl_setup(VALUE self)
550550
GetOpenFile(io, fptr);
551551
rb_io_check_readable(fptr);
552552
rb_io_check_writable(fptr);
553-
SSL_set_fd(ssl, TO_SOCKET(fptr->fd));
553+
SSL_set_fd(ssl, TO_SOCKET(FPTR_TO_FD(fptr)));
554554
SSL_set_ex_data(ssl, ossl_ssl_ex_ptr_idx, (void*)self);
555555
cb = ossl_sslctx_get_verify_cb(v_ctx);
556556
SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void*)cb);
@@ -582,10 +582,10 @@ ossl_start_ssl(VALUE self, int (*func)())
582582
if((ret = func(ssl)) > 0) break;
583583
switch(ssl_get_error(ssl, ret)){
584584
case SSL_ERROR_WANT_WRITE:
585-
rb_io_wait_writable(fptr->fd);
585+
rb_io_wait_writable(FPTR_TO_FD(fptr));
586586
continue;
587587
case SSL_ERROR_WANT_READ:
588-
rb_io_wait_readable(fptr->fd);
588+
rb_io_wait_readable(FPTR_TO_FD(fptr));
589589
continue;
590590
case SSL_ERROR_SYSCALL:
591591
if (errno) rb_sys_fail(0);
@@ -633,7 +633,7 @@ ossl_ssl_read(int argc, VALUE *argv, VALUE self)
633633
GetOpenFile(ossl_ssl_get_io(self), fptr);
634634
if (ssl) {
635635
if(SSL_pending(ssl) <= 0)
636-
rb_thread_wait_fd(fptr->fd);
636+
rb_thread_wait_fd(FPTR_TO_FD(fptr));
637637
for (;;){
638638
nread = SSL_read(ssl, RSTRING_PTR(str), RSTRING_LEN(str));
639639
switch(ssl_get_error(ssl, nread)){
@@ -642,10 +642,10 @@ ossl_ssl_read(int argc, VALUE *argv, VALUE self)
642642
case SSL_ERROR_ZERO_RETURN:
643643
rb_eof_error();
644644
case SSL_ERROR_WANT_WRITE:
645-
rb_io_wait_writable(fptr->fd);
645+
rb_io_wait_writable(FPTR_TO_FD(fptr));
646646
continue;
647647
case SSL_ERROR_WANT_READ:
648-
rb_io_wait_readable(fptr->fd);
648+
rb_io_wait_readable(FPTR_TO_FD(fptr));
649649
continue;
650650
case SSL_ERROR_SYSCALL:
651651
if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
@@ -686,10 +686,10 @@ ossl_ssl_write(VALUE self, VALUE str)
686686
case SSL_ERROR_NONE:
687687
goto end;
688688
case SSL_ERROR_WANT_WRITE:
689-
rb_io_wait_writable(fptr->fd);
689+
rb_io_wait_writable(FPTR_TO_FD(fptr));
690690
continue;
691691
case SSL_ERROR_WANT_READ:
692-
rb_io_wait_readable(fptr->fd);
692+
rb_io_wait_readable(FPTR_TO_FD(fptr));
693693
continue;
694694
case SSL_ERROR_SYSCALL:
695695
if (errno) rb_sys_fail(0);

ext/openssl/ruby_missing.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,26 @@
1414
#define rb_define_copy_func(klass, func) \
1515
rb_define_method(klass, "initialize_copy", func, 1)
1616

17+
18+
#if RUBY_VERSION_CODE > 190
19+
#define FPTR_TO_FD(fptr) (fptr->fd)
20+
#else
21+
22+
#define rb_io_t OpenFile
23+
#define FPTR_TO_FD(fptr) (fileno(fptr->f))
24+
25+
26+
/* these methods should probably be backported to 1.8 */
27+
#define rb_str_set_len(str, length) do { \
28+
RSTRING(str)->ptr[length] = 0; \
29+
RSTRING(str)->len = length; \
30+
} while(0)
31+
32+
/* the openssl module doesn't use arg[3-4] and arg2 is always rb_each */
33+
#define rb_block_call(arg1, arg2, arg3, arg4, arg5, arg6) rb_iterate(rb_each, arg1, arg5, arg6)
34+
35+
#endif /* RUBY_VERSION_CODE > 190 */
36+
37+
1738
#endif /* _OSS_RUBY_MISSING_H_ */
1839

0 commit comments

Comments
 (0)