rubysl-openssl 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -6
  3. data/ext/rubysl/openssl/.gitignore +3 -0
  4. data/ext/rubysl/openssl/deprecation.rb +21 -0
  5. data/ext/rubysl/openssl/extconf.rb +45 -32
  6. data/ext/rubysl/openssl/openssl_missing.c +20 -7
  7. data/ext/rubysl/openssl/openssl_missing.h +22 -15
  8. data/ext/rubysl/openssl/ossl.c +610 -61
  9. data/ext/rubysl/openssl/ossl.h +31 -17
  10. data/ext/rubysl/openssl/ossl_asn1.c +974 -183
  11. data/ext/rubysl/openssl/ossl_asn1.h +3 -3
  12. data/ext/rubysl/openssl/ossl_bio.c +4 -3
  13. data/ext/rubysl/openssl/ossl_bio.h +1 -1
  14. data/ext/rubysl/openssl/ossl_bn.c +32 -28
  15. data/ext/rubysl/openssl/ossl_bn.h +1 -1
  16. data/ext/rubysl/openssl/ossl_cipher.c +494 -93
  17. data/ext/rubysl/openssl/ossl_cipher.h +1 -1
  18. data/ext/rubysl/openssl/ossl_config.c +4 -5
  19. data/ext/rubysl/openssl/ossl_config.h +1 -1
  20. data/ext/rubysl/openssl/ossl_digest.c +206 -24
  21. data/ext/rubysl/openssl/ossl_digest.h +1 -1
  22. data/ext/rubysl/openssl/ossl_engine.c +48 -26
  23. data/ext/rubysl/openssl/ossl_engine.h +1 -1
  24. data/ext/rubysl/openssl/ossl_hmac.c +40 -38
  25. data/ext/rubysl/openssl/ossl_hmac.h +1 -1
  26. data/ext/rubysl/openssl/ossl_ns_spki.c +157 -25
  27. data/ext/rubysl/openssl/ossl_ns_spki.h +1 -1
  28. data/ext/rubysl/openssl/ossl_ocsp.c +57 -40
  29. data/ext/rubysl/openssl/ossl_ocsp.h +1 -1
  30. data/ext/rubysl/openssl/ossl_pkcs12.c +15 -13
  31. data/ext/rubysl/openssl/ossl_pkcs12.h +1 -1
  32. data/ext/rubysl/openssl/ossl_pkcs5.c +108 -18
  33. data/ext/rubysl/openssl/ossl_pkcs7.c +44 -37
  34. data/ext/rubysl/openssl/ossl_pkcs7.h +1 -1
  35. data/ext/rubysl/openssl/ossl_pkey.c +211 -15
  36. data/ext/rubysl/openssl/ossl_pkey.h +19 -9
  37. data/ext/rubysl/openssl/ossl_pkey_dh.c +180 -47
  38. data/ext/rubysl/openssl/ossl_pkey_dsa.c +184 -47
  39. data/ext/rubysl/openssl/ossl_pkey_ec.c +177 -93
  40. data/ext/rubysl/openssl/ossl_pkey_rsa.c +209 -102
  41. data/ext/rubysl/openssl/ossl_rand.c +15 -15
  42. data/ext/rubysl/openssl/ossl_rand.h +1 -1
  43. data/ext/rubysl/openssl/ossl_ssl.c +939 -192
  44. data/ext/rubysl/openssl/ossl_ssl.h +6 -6
  45. data/ext/rubysl/openssl/ossl_ssl_session.c +78 -62
  46. data/ext/rubysl/openssl/ossl_version.h +2 -2
  47. data/ext/rubysl/openssl/ossl_x509.c +1 -1
  48. data/ext/rubysl/openssl/ossl_x509.h +1 -1
  49. data/ext/rubysl/openssl/ossl_x509attr.c +20 -19
  50. data/ext/rubysl/openssl/ossl_x509cert.c +169 -67
  51. data/ext/rubysl/openssl/ossl_x509crl.c +41 -39
  52. data/ext/rubysl/openssl/ossl_x509ext.c +51 -38
  53. data/ext/rubysl/openssl/ossl_x509name.c +139 -29
  54. data/ext/rubysl/openssl/ossl_x509req.c +42 -40
  55. data/ext/rubysl/openssl/ossl_x509revoked.c +20 -20
  56. data/ext/rubysl/openssl/ossl_x509store.c +99 -47
  57. data/ext/rubysl/openssl/ruby_missing.h +3 -16
  58. data/lib/openssl/bn.rb +19 -19
  59. data/lib/openssl/buffering.rb +222 -14
  60. data/lib/openssl/cipher.rb +20 -20
  61. data/lib/openssl/config.rb +1 -4
  62. data/lib/openssl/digest.rb +47 -19
  63. data/lib/openssl/ssl.rb +197 -1
  64. data/lib/openssl/x509.rb +162 -1
  65. data/lib/rubysl/openssl.rb +4 -8
  66. data/lib/rubysl/openssl/version.rb +1 -1
  67. data/rubysl-openssl.gemspec +1 -2
  68. metadata +16 -34
  69. data/ext/rubysl/openssl/extconf.h +0 -50
  70. data/lib/openssl/net/ftptls.rb +0 -53
  71. data/lib/openssl/net/telnets.rb +0 -251
  72. data/lib/openssl/pkcs7.rb +0 -25
  73. data/lib/openssl/ssl-internal.rb +0 -187
  74. data/lib/openssl/x509-internal.rb +0 -153
@@ -1,5 +1,5 @@
1
1
  /*
2
- * $Id: ossl_pkey_rsa.c 28004 2010-05-24 23:58:49Z shyouhei $
2
+ * $Id$
3
3
  * 'OpenSSL for Ruby' project
4
4
  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5
5
  * All rights reserved.
@@ -13,8 +13,8 @@
13
13
  #include "ossl.h"
14
14
 
15
15
  #define GetPKeyRSA(obj, pkey) do { \
16
- GetPKey(obj, pkey); \
17
- if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) { /* PARANOIA? */ \
16
+ GetPKey((obj), (pkey)); \
17
+ if (EVP_PKEY_type((pkey)->type) != EVP_PKEY_RSA) { /* PARANOIA? */ \
18
18
  ossl_raise(rb_eRuntimeError, "THIS IS NOT A RSA!") ; \
19
19
  } \
20
20
  } while (0)
@@ -36,7 +36,7 @@ rsa_instance(VALUE klass, RSA *rsa)
36
36
  {
37
37
  EVP_PKEY *pkey;
38
38
  VALUE obj;
39
-
39
+
40
40
  if (!rsa) {
41
41
  return Qfalse;
42
42
  }
@@ -48,7 +48,7 @@ rsa_instance(VALUE klass, RSA *rsa)
48
48
  return Qfalse;
49
49
  }
50
50
  WrapPKey(klass, obj, pkey);
51
-
51
+
52
52
  return obj;
53
53
  }
54
54
 
@@ -76,22 +76,87 @@ ossl_rsa_new(EVP_PKEY *pkey)
76
76
  /*
77
77
  * Private
78
78
  */
79
+ #if defined(HAVE_RSA_GENERATE_KEY_EX) && HAVE_BN_GENCB
80
+ struct rsa_blocking_gen_arg {
81
+ RSA *rsa;
82
+ BIGNUM *e;
83
+ int size;
84
+ BN_GENCB *cb;
85
+ int result;
86
+ };
87
+
88
+ static void *
89
+ rsa_blocking_gen(void *arg)
90
+ {
91
+ struct rsa_blocking_gen_arg *gen = (struct rsa_blocking_gen_arg *)arg;
92
+ gen->result = RSA_generate_key_ex(gen->rsa, gen->size, gen->e, gen->cb);
93
+ return 0;
94
+ }
95
+ #endif
96
+
79
97
  static RSA *
80
- rsa_generate(int size, int exp)
98
+ rsa_generate(int size, unsigned long exp)
81
99
  {
82
- return RSA_generate_key(size, exp,
83
- rb_block_given_p() ? ossl_generate_cb : NULL,
84
- NULL);
100
+ #if defined(HAVE_RSA_GENERATE_KEY_EX) && HAVE_BN_GENCB
101
+ int i;
102
+ BN_GENCB cb;
103
+ struct ossl_generate_cb_arg cb_arg;
104
+ struct rsa_blocking_gen_arg gen_arg;
105
+ RSA *rsa = RSA_new();
106
+ BIGNUM *e = BN_new();
107
+
108
+ if (!rsa || !e) {
109
+ if (e) BN_free(e);
110
+ if (rsa) RSA_free(rsa);
111
+ return 0;
112
+ }
113
+ for (i = 0; i < (int)sizeof(exp) * 8; ++i) {
114
+ if (exp & (1UL << i)) {
115
+ if (BN_set_bit(e, i) == 0) {
116
+ BN_free(e);
117
+ RSA_free(rsa);
118
+ return 0;
119
+ }
120
+ }
121
+ }
122
+
123
+ memset(&cb_arg, 0, sizeof(struct ossl_generate_cb_arg));
124
+ if (rb_block_given_p())
125
+ cb_arg.yield = 1;
126
+ BN_GENCB_set(&cb, ossl_generate_cb_2, &cb_arg);
127
+ gen_arg.rsa = rsa;
128
+ gen_arg.e = e;
129
+ gen_arg.size = size;
130
+ gen_arg.cb = &cb;
131
+ if (cb_arg.yield == 1) {
132
+ /* we cannot release GVL when callback proc is supplied */
133
+ rsa_blocking_gen(&gen_arg);
134
+ } else {
135
+ /* there's a chance to unblock */
136
+ rb_thread_call_without_gvl(rsa_blocking_gen, &gen_arg, ossl_generate_cb_stop, &cb_arg);
137
+ }
138
+ if (!gen_arg.result) {
139
+ BN_free(e);
140
+ RSA_free(rsa);
141
+ if (cb_arg.state) rb_jump_tag(cb_arg.state);
142
+ return 0;
143
+ }
144
+
145
+ BN_free(e);
146
+ return rsa;
147
+ #else
148
+ return RSA_generate_key(size, exp, rb_block_given_p() ? ossl_generate_cb : NULL, NULL);
149
+ #endif
85
150
  }
86
151
 
87
152
  /*
88
- * call-seq:
89
- * RSA.generate(size [, exponent]) -> rsa
90
- *
91
- * === Parameters
92
- * * +size+ is an integer representing the desired key size. Keys smaller than 1024 should be considered insecure.
93
- * * +exponent+ is an odd number normally 3, 17, or 65537.
153
+ * call-seq:
154
+ * RSA.generate(size) => RSA instance
155
+ * RSA.generate(size, exponent) => RSA instance
94
156
  *
157
+ * Generates an RSA keypair. +size+ is an integer representing the desired key
158
+ * size. Keys smaller than 1024 should be considered insecure. +exponent+ is
159
+ * an odd number normally 3, 17, or 65537.
95
160
  */
96
161
  static VALUE
97
162
  ossl_rsa_s_generate(int argc, VALUE *argv, VALUE klass)
@@ -103,7 +168,7 @@ ossl_rsa_s_generate(int argc, VALUE *argv, VALUE klass)
103
168
 
104
169
  rb_scan_args(argc, argv, "11", &size, &exp);
105
170
 
106
- rsa = rsa_generate(NUM2INT(size), NIL_P(exp) ? RSA_F4 : NUM2INT(exp)); /* err handled by rsa_instance */
171
+ rsa = rsa_generate(NUM2INT(size), NIL_P(exp) ? RSA_F4 : NUM2ULONG(exp)); /* err handled by rsa_instance */
107
172
  obj = rsa_instance(klass, rsa);
108
173
 
109
174
  if (obj == Qfalse) {
@@ -115,18 +180,24 @@ ossl_rsa_s_generate(int argc, VALUE *argv, VALUE klass)
115
180
  }
116
181
 
117
182
  /*
118
- * call-seq:
119
- * RSA.new([size | encoded_key] [, pass]) -> rsa
183
+ * call-seq:
184
+ * RSA.new(key_size) => RSA instance
185
+ * RSA.new(encoded_key) => RSA instance
186
+ * RSA.new(encoded_key, pass_phrase) => RSA instance
120
187
  *
121
- * === Parameters
122
- * * +size+ is an integer representing the desired key size.
123
- * * +encoded_key+ is a string containing PEM or DER encoded key.
124
- * * +pass+ is an optional string with the password to decrypt the encoded key.
188
+ * Generates or loads an RSA keypair. If an integer +key_size+ is given it
189
+ * represents the desired key size. Keys less than 1024 bits should be
190
+ * considered insecure.
125
191
  *
126
- * === Examples
127
- * * RSA.new(2048) -> rsa
128
- * * RSA.new(File.read("rsa.pem")) -> rsa
129
- * * RSA.new(File.read("rsa.pem"), "mypassword") -> rsa
192
+ * A key can instead be loaded from an +encoded_key+ which must be PEM or DER
193
+ * encoded. A +pass_phrase+ can be used to decrypt the key. If none is given
194
+ * OpenSSL will prompt for the pass phrase.
195
+ *
196
+ * = Examples
197
+ *
198
+ * OpenSSL::PKey::RSA.new 2048
199
+ * OpenSSL::PKey::RSA.new File.read 'rsa.pem'
200
+ * OpenSSL::PKey::RSA.new File.read('rsa.pem'), 'my pass phrase'
130
201
  */
131
202
  static VALUE
132
203
  ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
@@ -136,13 +207,13 @@ ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
136
207
  BIO *in;
137
208
  char *passwd = NULL;
138
209
  VALUE arg, pass;
139
-
210
+
140
211
  GetPKey(self, pkey);
141
212
  if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) {
142
213
  rsa = RSA_new();
143
214
  }
144
215
  else if (FIXNUM_P(arg)) {
145
- rsa = rsa_generate(FIX2INT(arg), NIL_P(pass) ? RSA_F4 : NUM2INT(pass));
216
+ rsa = rsa_generate(FIX2INT(arg), NIL_P(pass) ? RSA_F4 : NUM2ULONG(pass));
146
217
  if (!rsa) ossl_raise(eRSAError, NULL);
147
218
  }
148
219
  else {
@@ -151,27 +222,29 @@ ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
151
222
  in = ossl_obj2bio(arg);
152
223
  rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ossl_pem_passwd_cb, passwd);
153
224
  if (!rsa) {
154
- BIO_reset(in);
155
- rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
156
- }
157
- if (!rsa) {
158
- BIO_reset(in);
225
+ OSSL_BIO_reset(in);
159
226
  rsa = PEM_read_bio_RSA_PUBKEY(in, NULL, NULL, NULL);
160
227
  }
161
228
  if (!rsa) {
162
- BIO_reset(in);
229
+ OSSL_BIO_reset(in);
163
230
  rsa = d2i_RSAPrivateKey_bio(in, NULL);
164
231
  }
165
232
  if (!rsa) {
166
- BIO_reset(in);
167
- rsa = d2i_RSAPublicKey_bio(in, NULL);
233
+ OSSL_BIO_reset(in);
234
+ rsa = d2i_RSA_PUBKEY_bio(in, NULL);
168
235
  }
169
236
  if (!rsa) {
170
- BIO_reset(in);
171
- rsa = d2i_RSA_PUBKEY_bio(in, NULL);
237
+ OSSL_BIO_reset(in);
238
+ rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
239
+ }
240
+ if (!rsa) {
241
+ OSSL_BIO_reset(in);
242
+ rsa = d2i_RSAPublicKey_bio(in, NULL);
172
243
  }
173
244
  BIO_free(in);
174
- if (!rsa) ossl_raise(eRSAError, "Neither PUB key nor PRIV key:");
245
+ if (!rsa) {
246
+ ossl_raise(eRSAError, "Neither PUB key nor PRIV key");
247
+ }
175
248
  }
176
249
  if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
177
250
  RSA_free(rsa);
@@ -182,11 +255,11 @@ ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
182
255
  }
183
256
 
184
257
  /*
185
- * call-seq:
186
- * rsa.public? -> true
187
- *
188
- * The return value is always true since every private key is also a public key.
258
+ * call-seq:
259
+ * rsa.public? => true
189
260
  *
261
+ * The return value is always true since every private key is also a public
262
+ * key.
190
263
  */
191
264
  static VALUE
192
265
  ossl_rsa_is_public(VALUE self)
@@ -201,31 +274,29 @@ ossl_rsa_is_public(VALUE self)
201
274
  }
202
275
 
203
276
  /*
204
- * call-seq:
205
- * rsa.private? -> true | false
277
+ * call-seq:
278
+ * rsa.private? => true | false
206
279
  *
280
+ * Does this keypair contain a private key?
207
281
  */
208
282
  static VALUE
209
283
  ossl_rsa_is_private(VALUE self)
210
284
  {
211
285
  EVP_PKEY *pkey;
212
-
286
+
213
287
  GetPKeyRSA(self, pkey);
214
-
288
+
215
289
  return (RSA_PRIVATE(self, pkey->pkey.rsa)) ? Qtrue : Qfalse;
216
290
  }
217
291
 
218
292
  /*
219
- * call-seq:
220
- * rsa.to_pem([cipher, pass]) -> aString
293
+ * call-seq:
294
+ * rsa.to_pem => PEM-format String
295
+ * rsa.to_pem(cipher, pass_phrase) => PEM-format String
221
296
  *
222
- * === Parameters
223
- * * +cipher+ is a Cipher object.
224
- * * +pass+ is a string.
225
- *
226
- * === Examples
227
- * * rsa.to_pem -> aString
228
- * * rsa.to_pem(cipher, pass) -> aString
297
+ * Outputs this keypair in PEM encoding. If +cipher+ and +pass_phrase+ are
298
+ * given they will be used to encrypt the key. +cipher+ must be an
299
+ * OpenSSL::Cipher::Cipher instance.
229
300
  */
230
301
  static VALUE
231
302
  ossl_rsa_export(int argc, VALUE *argv, VALUE self)
@@ -243,7 +314,10 @@ ossl_rsa_export(int argc, VALUE *argv, VALUE self)
243
314
  if (!NIL_P(cipher)) {
244
315
  ciph = GetCipherPtr(cipher);
245
316
  if (!NIL_P(pass)) {
246
- passwd = StringValuePtr(pass);
317
+ StringValue(pass);
318
+ if (RSTRING_LENINT(pass) < OSSL_MIN_PWD_LEN)
319
+ ossl_raise(eOSSLError, "OpenSSL requires passwords to be at least four characters long");
320
+ passwd = RSTRING_PTR(pass);
247
321
  }
248
322
  }
249
323
  if (!(out = BIO_new(BIO_s_mem()))) {
@@ -256,20 +330,21 @@ ossl_rsa_export(int argc, VALUE *argv, VALUE self)
256
330
  ossl_raise(eRSAError, NULL);
257
331
  }
258
332
  } else {
259
- if (!PEM_write_bio_RSAPublicKey(out, pkey->pkey.rsa)) {
333
+ if (!PEM_write_bio_RSA_PUBKEY(out, pkey->pkey.rsa)) {
260
334
  BIO_free(out);
261
335
  ossl_raise(eRSAError, NULL);
262
336
  }
263
337
  }
264
338
  str = ossl_membio2str(out);
265
-
339
+
266
340
  return str;
267
341
  }
268
342
 
269
343
  /*
270
- * call-seq:
271
- * rsa.to_der -> aString
344
+ * call-seq:
345
+ * rsa.to_der => DER-format String
272
346
  *
347
+ * Outputs this keypair in DER encoding.
273
348
  */
274
349
  static VALUE
275
350
  ossl_rsa_to_der(VALUE self)
@@ -284,11 +359,11 @@ ossl_rsa_to_der(VALUE self)
284
359
  if(RSA_HAS_PRIVATE(pkey->pkey.rsa))
285
360
  i2d_func = i2d_RSAPrivateKey;
286
361
  else
287
- i2d_func = i2d_RSAPublicKey;
362
+ i2d_func = (int (*)(const RSA*, unsigned char**))i2d_RSA_PUBKEY;
288
363
  if((len = i2d_func(pkey->pkey.rsa, NULL)) <= 0)
289
364
  ossl_raise(eRSAError, NULL);
290
365
  str = rb_str_new(0, len);
291
- p = RSTRING_PTR(str);
366
+ p = (unsigned char *)RSTRING_PTR(str);
292
367
  if(i2d_func(pkey->pkey.rsa, &p) < 0)
293
368
  ossl_raise(eRSAError, NULL);
294
369
  ossl_str_adjust(str, p);
@@ -299,9 +374,12 @@ ossl_rsa_to_der(VALUE self)
299
374
  #define ossl_rsa_buf_size(pkey) (RSA_size((pkey)->pkey.rsa)+16)
300
375
 
301
376
  /*
302
- * call-seq:
303
- * rsa.public_encrypt(string [, padding]) -> aString
377
+ * call-seq:
378
+ * rsa.public_encrypt(string) => String
379
+ * rsa.public_encrypt(string, padding) => String
304
380
  *
381
+ * Encrypt +string+ with the public key. +padding+ defaults to PKCS1_PADDING.
382
+ * The encrypted string output can be decrypted using #private_decrypt.
305
383
  */
306
384
  static VALUE
307
385
  ossl_rsa_public_encrypt(int argc, VALUE *argv, VALUE self)
@@ -315,8 +393,8 @@ ossl_rsa_public_encrypt(int argc, VALUE *argv, VALUE self)
315
393
  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
316
394
  StringValue(buffer);
317
395
  str = rb_str_new(0, ossl_rsa_buf_size(pkey));
318
- buf_len = RSA_public_encrypt(RSTRING_LEN(buffer), RSTRING_PTR(buffer),
319
- RSTRING_PTR(str), pkey->pkey.rsa,
396
+ buf_len = RSA_public_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
397
+ (unsigned char *)RSTRING_PTR(str), pkey->pkey.rsa,
320
398
  pad);
321
399
  if (buf_len < 0) ossl_raise(eRSAError, NULL);
322
400
  rb_str_set_len(str, buf_len);
@@ -325,9 +403,12 @@ ossl_rsa_public_encrypt(int argc, VALUE *argv, VALUE self)
325
403
  }
326
404
 
327
405
  /*
328
- * call-seq:
329
- * rsa.public_decrypt(string [, padding]) -> aString
406
+ * call-seq:
407
+ * rsa.public_decrypt(string) => String
408
+ * rsa.public_decrypt(string, padding) => String
330
409
  *
410
+ * Decrypt +string+, which has been encrypted with the private key, with the
411
+ * public key. +padding+ defaults to PKCS1_PADDING.
331
412
  */
332
413
  static VALUE
333
414
  ossl_rsa_public_decrypt(int argc, VALUE *argv, VALUE self)
@@ -341,19 +422,22 @@ ossl_rsa_public_decrypt(int argc, VALUE *argv, VALUE self)
341
422
  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
342
423
  StringValue(buffer);
343
424
  str = rb_str_new(0, ossl_rsa_buf_size(pkey));
344
- buf_len = RSA_public_decrypt(RSTRING_LEN(buffer), RSTRING_PTR(buffer),
345
- RSTRING_PTR(str), pkey->pkey.rsa,
425
+ buf_len = RSA_public_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
426
+ (unsigned char *)RSTRING_PTR(str), pkey->pkey.rsa,
346
427
  pad);
347
428
  if (buf_len < 0) ossl_raise(eRSAError, NULL);
348
429
  rb_str_set_len(str, buf_len);
349
-
430
+
350
431
  return str;
351
432
  }
352
433
 
353
434
  /*
354
- * call-seq:
355
- * rsa.private_encrypt(string [, padding]) -> aString
435
+ * call-seq:
436
+ * rsa.private_encrypt(string) => String
437
+ * rsa.private_encrypt(string, padding) => String
356
438
  *
439
+ * Encrypt +string+ with the private key. +padding+ defaults to PKCS1_PADDING.
440
+ * The encrypted string output can be decrypted using #public_decrypt.
357
441
  */
358
442
  static VALUE
359
443
  ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self)
@@ -365,25 +449,27 @@ ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self)
365
449
  GetPKeyRSA(self, pkey);
366
450
  if (!RSA_PRIVATE(self, pkey->pkey.rsa)) {
367
451
  ossl_raise(eRSAError, "private key needed.");
368
- }
452
+ }
369
453
  rb_scan_args(argc, argv, "11", &buffer, &padding);
370
454
  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
371
455
  StringValue(buffer);
372
456
  str = rb_str_new(0, ossl_rsa_buf_size(pkey));
373
- buf_len = RSA_private_encrypt(RSTRING_LEN(buffer), RSTRING_PTR(buffer),
374
- RSTRING_PTR(str), pkey->pkey.rsa,
457
+ buf_len = RSA_private_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
458
+ (unsigned char *)RSTRING_PTR(str), pkey->pkey.rsa,
375
459
  pad);
376
460
  if (buf_len < 0) ossl_raise(eRSAError, NULL);
377
461
  rb_str_set_len(str, buf_len);
378
-
462
+
379
463
  return str;
380
464
  }
381
465
 
382
-
383
466
  /*
384
- * call-seq:
385
- * rsa.private_decrypt(string [, padding]) -> aString
467
+ * call-seq:
468
+ * rsa.private_decrypt(string) => String
469
+ * rsa.private_decrypt(string, padding) => String
386
470
  *
471
+ * Decrypt +string+, which has been encrypted with the public key, with the
472
+ * private key. +padding+ defaults to PKCS1_PADDING.
387
473
  */
388
474
  static VALUE
389
475
  ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
@@ -400,8 +486,8 @@ ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
400
486
  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
401
487
  StringValue(buffer);
402
488
  str = rb_str_new(0, ossl_rsa_buf_size(pkey));
403
- buf_len = RSA_private_decrypt(RSTRING_LEN(buffer), RSTRING_PTR(buffer),
404
- RSTRING_PTR(str), pkey->pkey.rsa,
489
+ buf_len = RSA_private_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
490
+ (unsigned char *)RSTRING_PTR(str), pkey->pkey.rsa,
405
491
  pad);
406
492
  if (buf_len < 0) ossl_raise(eRSAError, NULL);
407
493
  rb_str_set_len(str, buf_len);
@@ -410,12 +496,15 @@ ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
410
496
  }
411
497
 
412
498
  /*
413
- * call-seq:
414
- * rsa.params -> hash
499
+ * call-seq:
500
+ * rsa.params => hash
501
+ *
502
+ * THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
415
503
  *
416
- * Stores all parameters of key to the hash
417
- * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
418
- * Don't use :-)) (I's up to you)
504
+ * Stores all parameters of key to the hash. The hash has keys 'n', 'e', 'd',
505
+ * 'p', 'q', 'dmp1', 'dmq1', 'iqmp'.
506
+ *
507
+ * Don't use :-)) (It's up to you)
419
508
  */
420
509
  static VALUE
421
510
  ossl_rsa_get_params(VALUE self)
@@ -435,16 +524,18 @@ ossl_rsa_get_params(VALUE self)
435
524
  rb_hash_aset(hash, rb_str_new2("dmp1"), ossl_bn_new(pkey->pkey.rsa->dmp1));
436
525
  rb_hash_aset(hash, rb_str_new2("dmq1"), ossl_bn_new(pkey->pkey.rsa->dmq1));
437
526
  rb_hash_aset(hash, rb_str_new2("iqmp"), ossl_bn_new(pkey->pkey.rsa->iqmp));
438
-
527
+
439
528
  return hash;
440
529
  }
441
530
 
442
531
  /*
443
- * call-seq:
444
- * rsa.to_text -> aString
532
+ * call-seq:
533
+ * rsa.to_text => String
534
+ *
535
+ * THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
536
+ *
537
+ * Dumps all parameters of a keypair to a String
445
538
  *
446
- * Prints all parameters of key to buffer
447
- * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
448
539
  * Don't use :-)) (It's up to you)
449
540
  */
450
541
  static VALUE
@@ -468,10 +559,10 @@ ossl_rsa_to_text(VALUE self)
468
559
  }
469
560
 
470
561
  /*
471
- * call-seq:
472
- * rsa.public_key -> aRSA
562
+ * call-seq:
563
+ * rsa.public_key -> RSA
473
564
  *
474
- * Makes new instance RSA PUBLIC_KEY from PRIVATE_KEY
565
+ * Makes new RSA instance containing the public key from the private key.
475
566
  */
476
567
  static VALUE
477
568
  ossl_rsa_to_public_key(VALUE self)
@@ -479,7 +570,7 @@ ossl_rsa_to_public_key(VALUE self)
479
570
  EVP_PKEY *pkey;
480
571
  RSA *rsa;
481
572
  VALUE obj;
482
-
573
+
483
574
  GetPKeyRSA(self, pkey);
484
575
  /* err check performed by rsa_instance */
485
576
  rsa = RSAPublicKey_dup(pkey->pkey.rsa);
@@ -498,7 +589,7 @@ static VALUE
498
589
  ossl_rsa_blinding_on(VALUE self)
499
590
  {
500
591
  EVP_PKEY *pkey;
501
-
592
+
502
593
  GetPKeyRSA(self, pkey);
503
594
 
504
595
  if (RSA_blinding_on(pkey->pkey.rsa, ossl_bn_ctx) != 1) {
@@ -511,7 +602,7 @@ static VALUE
511
602
  ossl_rsa_blinding_off(VALUE self)
512
603
  {
513
604
  EVP_PKEY *pkey;
514
-
605
+
515
606
  GetPKeyRSA(self, pkey);
516
607
  RSA_blinding_off(pkey->pkey.rsa);
517
608
 
@@ -536,18 +627,34 @@ OSSL_PKEY_BN(rsa, iqmp)
536
627
  void
537
628
  Init_ossl_rsa()
538
629
  {
539
- #if 0 /* let rdoc know about mOSSL and mPKey */
540
- mOSSL = rb_define_module("OpenSSL");
630
+ #if 0
631
+ mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL and mPKey */
541
632
  mPKey = rb_define_module_under(mOSSL, "PKey");
542
633
  #endif
543
634
 
635
+ /* Document-class: OpenSSL::PKey::RSAError
636
+ *
637
+ * Generic exception that is raised if an operation on an RSA PKey
638
+ * fails unexpectedly or in case an instantiation of an instance of RSA
639
+ * fails due to non-conformant input data.
640
+ */
544
641
  eRSAError = rb_define_class_under(mPKey, "RSAError", ePKeyError);
545
642
 
643
+ /* Document-class: OpenSSL::PKey::RSA
644
+ *
645
+ * RSA is an asymmetric public key algorithm that has been formalized in
646
+ * RFC 3447. It is in widespread use in public key infrastuctures (PKI)
647
+ * where certificates (cf. OpenSSL::X509::Certificate) often are issued
648
+ * on the basis of a public/private RSA key pair. RSA is used in a wide
649
+ * field of applications such as secure (symmetric) key exchange, e.g.
650
+ * when establishing a secure TLS/SSL connection. It is also used in
651
+ * various digital signature schemes.
652
+ */
546
653
  cRSA = rb_define_class_under(mPKey, "RSA", cPKey);
547
654
 
548
655
  rb_define_singleton_method(cRSA, "generate", ossl_rsa_s_generate, -1);
549
656
  rb_define_method(cRSA, "initialize", ossl_rsa_initialize, -1);
550
-
657
+
551
658
  rb_define_method(cRSA, "public?", ossl_rsa_is_public, 0);
552
659
  rb_define_method(cRSA, "private?", ossl_rsa_is_private, 0);
553
660
  rb_define_method(cRSA, "to_text", ossl_rsa_to_text, 0);