Ruby  2.5.0dev(2017-10-22revision60238)
ossl_pkey_rsa.c
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #include "ossl.h"
11 
12 #if !defined(OPENSSL_NO_RSA)
13 
14 #define GetPKeyRSA(obj, pkey) do { \
15  GetPKey((obj), (pkey)); \
16  if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) { /* PARANOIA? */ \
17  ossl_raise(rb_eRuntimeError, "THIS IS NOT A RSA!") ; \
18  } \
19 } while (0)
20 #define GetRSA(obj, rsa) do { \
21  EVP_PKEY *_pkey; \
22  GetPKeyRSA((obj), _pkey); \
23  (rsa) = EVP_PKEY_get0_RSA(_pkey); \
24 } while (0)
25 
26 static inline int
27 RSA_HAS_PRIVATE(RSA *rsa)
28 {
29  const BIGNUM *p, *q;
30 
31  RSA_get0_factors(rsa, &p, &q);
32  return p && q; /* d? why? */
33 }
34 
35 static inline int
36 RSA_PRIVATE(VALUE obj, RSA *rsa)
37 {
38  return RSA_HAS_PRIVATE(rsa) || OSSL_PKEY_IS_PRIVATE(obj);
39 }
40 
41 /*
42  * Classes
43  */
46 
47 /*
48  * Public
49  */
50 static VALUE
51 rsa_instance(VALUE klass, RSA *rsa)
52 {
53  EVP_PKEY *pkey;
54  VALUE obj;
55 
56  if (!rsa) {
57  return Qfalse;
58  }
59  obj = NewPKey(klass);
60  if (!(pkey = EVP_PKEY_new())) {
61  return Qfalse;
62  }
63  if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
64  EVP_PKEY_free(pkey);
65  return Qfalse;
66  }
67  SetPKey(obj, pkey);
68 
69  return obj;
70 }
71 
72 VALUE
73 ossl_rsa_new(EVP_PKEY *pkey)
74 {
75  VALUE obj;
76 
77  if (!pkey) {
78  obj = rsa_instance(cRSA, RSA_new());
79  }
80  else {
81  obj = NewPKey(cRSA);
82  if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
83  ossl_raise(rb_eTypeError, "Not a RSA key!");
84  }
85  SetPKey(obj, pkey);
86  }
87  if (obj == Qfalse) {
89  }
90 
91  return obj;
92 }
93 
94 /*
95  * Private
96  */
98  RSA *rsa;
99  BIGNUM *e;
100  int size;
101  BN_GENCB *cb;
102  int result;
103 };
104 
105 static void *
106 rsa_blocking_gen(void *arg)
107 {
108  struct rsa_blocking_gen_arg *gen = (struct rsa_blocking_gen_arg *)arg;
109  gen->result = RSA_generate_key_ex(gen->rsa, gen->size, gen->e, gen->cb);
110  return 0;
111 }
112 
113 static RSA *
114 rsa_generate(int size, unsigned long exp)
115 {
116  int i;
117  struct ossl_generate_cb_arg cb_arg = { 0 };
118  struct rsa_blocking_gen_arg gen_arg;
119  RSA *rsa = RSA_new();
120  BIGNUM *e = BN_new();
121  BN_GENCB *cb = BN_GENCB_new();
122 
123  if (!rsa || !e || !cb) {
124  RSA_free(rsa);
125  BN_free(e);
126  BN_GENCB_free(cb);
127  return NULL;
128  }
129  for (i = 0; i < (int)sizeof(exp) * 8; ++i) {
130  if (exp & (1UL << i)) {
131  if (BN_set_bit(e, i) == 0) {
132  BN_free(e);
133  RSA_free(rsa);
134  BN_GENCB_free(cb);
135  return NULL;
136  }
137  }
138  }
139 
140  if (rb_block_given_p())
141  cb_arg.yield = 1;
142  BN_GENCB_set(cb, ossl_generate_cb_2, &cb_arg);
143  gen_arg.rsa = rsa;
144  gen_arg.e = e;
145  gen_arg.size = size;
146  gen_arg.cb = cb;
147  if (cb_arg.yield == 1) {
148  /* we cannot release GVL when callback proc is supplied */
149  rsa_blocking_gen(&gen_arg);
150  } else {
151  /* there's a chance to unblock */
152  rb_thread_call_without_gvl(rsa_blocking_gen, &gen_arg, ossl_generate_cb_stop, &cb_arg);
153  }
154 
155  BN_GENCB_free(cb);
156  BN_free(e);
157  if (!gen_arg.result) {
158  RSA_free(rsa);
159  if (cb_arg.state) {
160  /* must clear OpenSSL error stack */
162  rb_jump_tag(cb_arg.state);
163  }
164  return NULL;
165  }
166 
167  return rsa;
168 }
169 
170 /*
171  * call-seq:
172  * RSA.generate(size) => RSA instance
173  * RSA.generate(size, exponent) => RSA instance
174  *
175  * Generates an RSA keypair. _size_ is an integer representing the desired key
176  * size. Keys smaller than 1024 should be considered insecure. _exponent_ is
177  * an odd number normally 3, 17, or 65537.
178  */
179 static VALUE
180 ossl_rsa_s_generate(int argc, VALUE *argv, VALUE klass)
181 {
182 /* why does this method exist? why can't initialize take an optional exponent? */
183  RSA *rsa;
184  VALUE size, exp;
185  VALUE obj;
186 
187  rb_scan_args(argc, argv, "11", &size, &exp);
188 
189  rsa = rsa_generate(NUM2INT(size), NIL_P(exp) ? RSA_F4 : NUM2ULONG(exp)); /* err handled by rsa_instance */
190  obj = rsa_instance(klass, rsa);
191 
192  if (obj == Qfalse) {
193  RSA_free(rsa);
195  }
196 
197  return obj;
198 }
199 
200 /*
201  * call-seq:
202  * RSA.new(key_size) => RSA instance
203  * RSA.new(encoded_key) => RSA instance
204  * RSA.new(encoded_key, pass_phrase) => RSA instance
205  *
206  * Generates or loads an RSA keypair. If an integer _key_size_ is given it
207  * represents the desired key size. Keys less than 1024 bits should be
208  * considered insecure.
209  *
210  * A key can instead be loaded from an _encoded_key_ which must be PEM or DER
211  * encoded. A _pass_phrase_ can be used to decrypt the key. If none is given
212  * OpenSSL will prompt for the pass phrase.
213  *
214  * = Examples
215  *
216  * OpenSSL::PKey::RSA.new 2048
217  * OpenSSL::PKey::RSA.new File.read 'rsa.pem'
218  * OpenSSL::PKey::RSA.new File.read('rsa.pem'), 'my pass phrase'
219  */
220 static VALUE
221 ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
222 {
223  EVP_PKEY *pkey;
224  RSA *rsa;
225  BIO *in;
226  VALUE arg, pass;
227 
228  GetPKey(self, pkey);
229  if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) {
230  rsa = RSA_new();
231  }
232  else if (RB_INTEGER_TYPE_P(arg)) {
233  rsa = rsa_generate(NUM2INT(arg), NIL_P(pass) ? RSA_F4 : NUM2ULONG(pass));
234  if (!rsa) ossl_raise(eRSAError, NULL);
235  }
236  else {
237  pass = ossl_pem_passwd_value(pass);
238  arg = ossl_to_der_if_possible(arg);
239  in = ossl_obj2bio(&arg);
240  rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ossl_pem_passwd_cb, (void *)pass);
241  if (!rsa) {
242  OSSL_BIO_reset(in);
243  rsa = PEM_read_bio_RSA_PUBKEY(in, NULL, NULL, NULL);
244  }
245  if (!rsa) {
246  OSSL_BIO_reset(in);
247  rsa = d2i_RSAPrivateKey_bio(in, NULL);
248  }
249  if (!rsa) {
250  OSSL_BIO_reset(in);
251  rsa = d2i_RSA_PUBKEY_bio(in, NULL);
252  }
253  if (!rsa) {
254  OSSL_BIO_reset(in);
255  rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
256  }
257  if (!rsa) {
258  OSSL_BIO_reset(in);
259  rsa = d2i_RSAPublicKey_bio(in, NULL);
260  }
261  BIO_free(in);
262  if (!rsa) {
263  ossl_raise(eRSAError, "Neither PUB key nor PRIV key");
264  }
265  }
266  if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
267  RSA_free(rsa);
269  }
270 
271  return self;
272 }
273 
274 static VALUE
275 ossl_rsa_initialize_copy(VALUE self, VALUE other)
276 {
277  EVP_PKEY *pkey;
278  RSA *rsa, *rsa_new;
279 
280  GetPKey(self, pkey);
281  if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
282  ossl_raise(eRSAError, "RSA already initialized");
283  GetRSA(other, rsa);
284 
285  rsa_new = ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey, (d2i_of_void *)d2i_RSAPrivateKey, (char *)rsa);
286  if (!rsa_new)
287  ossl_raise(eRSAError, "ASN1_dup");
288 
289  EVP_PKEY_assign_RSA(pkey, rsa_new);
290 
291  return self;
292 }
293 
294 /*
295  * call-seq:
296  * rsa.public? => true
297  *
298  * The return value is always +true+ since every private key is also a public
299  * key.
300  */
301 static VALUE
302 ossl_rsa_is_public(VALUE self)
303 {
304  RSA *rsa;
305 
306  GetRSA(self, rsa);
307  /*
308  * This method should check for n and e. BUG.
309  */
310  (void)rsa;
311  return Qtrue;
312 }
313 
314 /*
315  * call-seq:
316  * rsa.private? => true | false
317  *
318  * Does this keypair contain a private key?
319  */
320 static VALUE
321 ossl_rsa_is_private(VALUE self)
322 {
323  RSA *rsa;
324 
325  GetRSA(self, rsa);
326 
327  return RSA_PRIVATE(self, rsa) ? Qtrue : Qfalse;
328 }
329 
330 /*
331  * call-seq:
332  * rsa.export([cipher, pass_phrase]) => PEM-format String
333  * rsa.to_pem([cipher, pass_phrase]) => PEM-format String
334  * rsa.to_s([cipher, pass_phrase]) => PEM-format String
335  *
336  * Outputs this keypair in PEM encoding. If _cipher_ and _pass_phrase_ are
337  * given they will be used to encrypt the key. _cipher_ must be an
338  * OpenSSL::Cipher instance.
339  */
340 static VALUE
341 ossl_rsa_export(int argc, VALUE *argv, VALUE self)
342 {
343  RSA *rsa;
344  BIO *out;
345  const EVP_CIPHER *ciph = NULL;
346  VALUE cipher, pass, str;
347 
348  GetRSA(self, rsa);
349 
350  rb_scan_args(argc, argv, "02", &cipher, &pass);
351 
352  if (!NIL_P(cipher)) {
353  ciph = ossl_evp_get_cipherbyname(cipher);
354  pass = ossl_pem_passwd_value(pass);
355  }
356  if (!(out = BIO_new(BIO_s_mem()))) {
358  }
359  if (RSA_HAS_PRIVATE(rsa)) {
360  if (!PEM_write_bio_RSAPrivateKey(out, rsa, ciph, NULL, 0,
361  ossl_pem_passwd_cb, (void *)pass)) {
362  BIO_free(out);
364  }
365  } else {
366  if (!PEM_write_bio_RSA_PUBKEY(out, rsa)) {
367  BIO_free(out);
369  }
370  }
371  str = ossl_membio2str(out);
372 
373  return str;
374 }
375 
376 /*
377  * call-seq:
378  * rsa.to_der => DER-format String
379  *
380  * Outputs this keypair in DER encoding.
381  */
382 static VALUE
383 ossl_rsa_to_der(VALUE self)
384 {
385  RSA *rsa;
386  int (*i2d_func)(const RSA *, unsigned char **);
387  unsigned char *p;
388  long len;
389  VALUE str;
390 
391  GetRSA(self, rsa);
392  if (RSA_HAS_PRIVATE(rsa))
393  i2d_func = i2d_RSAPrivateKey;
394  else
395  i2d_func = (int (*)(const RSA *, unsigned char **))i2d_RSA_PUBKEY;
396  if((len = i2d_func(rsa, NULL)) <= 0)
398  str = rb_str_new(0, len);
399  p = (unsigned char *)RSTRING_PTR(str);
400  if(i2d_func(rsa, &p) < 0)
402  ossl_str_adjust(str, p);
403 
404  return str;
405 }
406 
407 /*
408  * call-seq:
409  * rsa.public_encrypt(string) => String
410  * rsa.public_encrypt(string, padding) => String
411  *
412  * Encrypt _string_ with the public key. _padding_ defaults to PKCS1_PADDING.
413  * The encrypted string output can be decrypted using #private_decrypt.
414  */
415 static VALUE
416 ossl_rsa_public_encrypt(int argc, VALUE *argv, VALUE self)
417 {
418  RSA *rsa;
419  const BIGNUM *rsa_n;
420  int buf_len, pad;
421  VALUE str, buffer, padding;
422 
423  GetRSA(self, rsa);
424  RSA_get0_key(rsa, &rsa_n, NULL, NULL);
425  if (!rsa_n)
426  ossl_raise(eRSAError, "incomplete RSA");
427  rb_scan_args(argc, argv, "11", &buffer, &padding);
428  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
429  StringValue(buffer);
430  str = rb_str_new(0, RSA_size(rsa));
431  buf_len = RSA_public_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
432  (unsigned char *)RSTRING_PTR(str), rsa, pad);
433  if (buf_len < 0) ossl_raise(eRSAError, NULL);
434  rb_str_set_len(str, buf_len);
435 
436  return str;
437 }
438 
439 /*
440  * call-seq:
441  * rsa.public_decrypt(string) => String
442  * rsa.public_decrypt(string, padding) => String
443  *
444  * Decrypt _string_, which has been encrypted with the private key, with the
445  * public key. _padding_ defaults to PKCS1_PADDING.
446  */
447 static VALUE
448 ossl_rsa_public_decrypt(int argc, VALUE *argv, VALUE self)
449 {
450  RSA *rsa;
451  const BIGNUM *rsa_n;
452  int buf_len, pad;
453  VALUE str, buffer, padding;
454 
455  GetRSA(self, rsa);
456  RSA_get0_key(rsa, &rsa_n, NULL, NULL);
457  if (!rsa_n)
458  ossl_raise(eRSAError, "incomplete RSA");
459  rb_scan_args(argc, argv, "11", &buffer, &padding);
460  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
461  StringValue(buffer);
462  str = rb_str_new(0, RSA_size(rsa));
463  buf_len = RSA_public_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
464  (unsigned char *)RSTRING_PTR(str), rsa, pad);
465  if (buf_len < 0) ossl_raise(eRSAError, NULL);
466  rb_str_set_len(str, buf_len);
467 
468  return str;
469 }
470 
471 /*
472  * call-seq:
473  * rsa.private_encrypt(string) => String
474  * rsa.private_encrypt(string, padding) => String
475  *
476  * Encrypt _string_ with the private key. _padding_ defaults to PKCS1_PADDING.
477  * The encrypted string output can be decrypted using #public_decrypt.
478  */
479 static VALUE
480 ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self)
481 {
482  RSA *rsa;
483  const BIGNUM *rsa_n;
484  int buf_len, pad;
485  VALUE str, buffer, padding;
486 
487  GetRSA(self, rsa);
488  RSA_get0_key(rsa, &rsa_n, NULL, NULL);
489  if (!rsa_n)
490  ossl_raise(eRSAError, "incomplete RSA");
491  if (!RSA_PRIVATE(self, rsa))
492  ossl_raise(eRSAError, "private key needed.");
493  rb_scan_args(argc, argv, "11", &buffer, &padding);
494  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
495  StringValue(buffer);
496  str = rb_str_new(0, RSA_size(rsa));
497  buf_len = RSA_private_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
498  (unsigned char *)RSTRING_PTR(str), rsa, pad);
499  if (buf_len < 0) ossl_raise(eRSAError, NULL);
500  rb_str_set_len(str, buf_len);
501 
502  return str;
503 }
504 
505 /*
506  * call-seq:
507  * rsa.private_decrypt(string) => String
508  * rsa.private_decrypt(string, padding) => String
509  *
510  * Decrypt _string_, which has been encrypted with the public key, with the
511  * private key. _padding_ defaults to PKCS1_PADDING.
512  */
513 static VALUE
514 ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
515 {
516  RSA *rsa;
517  const BIGNUM *rsa_n;
518  int buf_len, pad;
519  VALUE str, buffer, padding;
520 
521  GetRSA(self, rsa);
522  RSA_get0_key(rsa, &rsa_n, NULL, NULL);
523  if (!rsa_n)
524  ossl_raise(eRSAError, "incomplete RSA");
525  if (!RSA_PRIVATE(self, rsa))
526  ossl_raise(eRSAError, "private key needed.");
527  rb_scan_args(argc, argv, "11", &buffer, &padding);
528  pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding);
529  StringValue(buffer);
530  str = rb_str_new(0, RSA_size(rsa));
531  buf_len = RSA_private_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer),
532  (unsigned char *)RSTRING_PTR(str), rsa, pad);
533  if (buf_len < 0) ossl_raise(eRSAError, NULL);
534  rb_str_set_len(str, buf_len);
535 
536  return str;
537 }
538 
539 /*
540  * call-seq:
541  * rsa.params => hash
542  *
543  * THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
544  *
545  * Stores all parameters of key to the hash. The hash has keys 'n', 'e', 'd',
546  * 'p', 'q', 'dmp1', 'dmq1', 'iqmp'.
547  *
548  * Don't use :-)) (It's up to you)
549  */
550 static VALUE
551 ossl_rsa_get_params(VALUE self)
552 {
553  RSA *rsa;
554  VALUE hash;
555  const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
556 
557  GetRSA(self, rsa);
558  RSA_get0_key(rsa, &n, &e, &d);
559  RSA_get0_factors(rsa, &p, &q);
560  RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
561 
562  hash = rb_hash_new();
563  rb_hash_aset(hash, rb_str_new2("n"), ossl_bn_new(n));
564  rb_hash_aset(hash, rb_str_new2("e"), ossl_bn_new(e));
565  rb_hash_aset(hash, rb_str_new2("d"), ossl_bn_new(d));
566  rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
567  rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
568  rb_hash_aset(hash, rb_str_new2("dmp1"), ossl_bn_new(dmp1));
569  rb_hash_aset(hash, rb_str_new2("dmq1"), ossl_bn_new(dmq1));
570  rb_hash_aset(hash, rb_str_new2("iqmp"), ossl_bn_new(iqmp));
571 
572  return hash;
573 }
574 
575 /*
576  * call-seq:
577  * rsa.to_text => String
578  *
579  * THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
580  *
581  * Dumps all parameters of a keypair to a String
582  *
583  * Don't use :-)) (It's up to you)
584  */
585 static VALUE
586 ossl_rsa_to_text(VALUE self)
587 {
588  RSA *rsa;
589  BIO *out;
590  VALUE str;
591 
592  GetRSA(self, rsa);
593  if (!(out = BIO_new(BIO_s_mem()))) {
595  }
596  if (!RSA_print(out, rsa, 0)) { /* offset = 0 */
597  BIO_free(out);
599  }
600  str = ossl_membio2str(out);
601 
602  return str;
603 }
604 
605 /*
606  * call-seq:
607  * rsa.public_key -> RSA
608  *
609  * Makes new RSA instance containing the public key from the private key.
610  */
611 static VALUE
612 ossl_rsa_to_public_key(VALUE self)
613 {
614  EVP_PKEY *pkey;
615  RSA *rsa;
616  VALUE obj;
617 
618  GetPKeyRSA(self, pkey);
619  /* err check performed by rsa_instance */
620  rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(pkey));
621  obj = rsa_instance(rb_obj_class(self), rsa);
622  if (obj == Qfalse) {
623  RSA_free(rsa);
625  }
626  return obj;
627 }
628 
629 /*
630  * TODO: Test me
631 
632 static VALUE
633 ossl_rsa_blinding_on(VALUE self)
634 {
635  RSA *rsa;
636 
637  GetRSA(self, rsa);
638 
639  if (RSA_blinding_on(rsa, ossl_bn_ctx) != 1) {
640  ossl_raise(eRSAError, NULL);
641  }
642  return self;
643 }
644 
645 static VALUE
646 ossl_rsa_blinding_off(VALUE self)
647 {
648  RSA *rsa;
649 
650  GetRSA(self, rsa);
651  RSA_blinding_off(rsa);
652 
653  return self;
654 }
655  */
656 
657 /*
658  * Document-method: OpenSSL::PKey::RSA#set_key
659  * call-seq:
660  * rsa.set_key(n, e, d) -> self
661  *
662  * Sets _n_, _e_, _d_ for the RSA instance.
663  */
664 OSSL_PKEY_BN_DEF3(rsa, RSA, key, n, e, d)
665 /*
666  * Document-method: OpenSSL::PKey::RSA#set_factors
667  * call-seq:
668  * rsa.set_factors(p, q) -> self
669  *
670  * Sets _p_, _q_ for the RSA instance.
671  */
672 OSSL_PKEY_BN_DEF2(rsa, RSA, factors, p, q)
673 /*
674  * Document-method: OpenSSL::PKey::RSA#set_crt_params
675  * call-seq:
676  * rsa.set_crt_params(dmp1, dmq1, iqmp) -> self
677  *
678  * Sets _dmp1_, _dmq1_, _iqmp_ for the RSA instance. They are calculated by
679  * <tt>d mod (p - 1)</tt>, <tt>d mod (q - 1)</tt> and <tt>q^(-1) mod p</tt>
680  * respectively.
681  */
682 OSSL_PKEY_BN_DEF3(rsa, RSA, crt_params, dmp1, dmq1, iqmp)
683 
684 /*
685  * INIT
686  */
687 #define DefRSAConst(x) rb_define_const(cRSA, #x, INT2NUM(RSA_##x))
688 
689 void
691 {
692 #if 0
696 #endif
697 
698  /* Document-class: OpenSSL::PKey::RSAError
699  *
700  * Generic exception that is raised if an operation on an RSA PKey
701  * fails unexpectedly or in case an instantiation of an instance of RSA
702  * fails due to non-conformant input data.
703  */
705 
706  /* Document-class: OpenSSL::PKey::RSA
707  *
708  * RSA is an asymmetric public key algorithm that has been formalized in
709  * RFC 3447. It is in widespread use in public key infrastructures (PKI)
710  * where certificates (cf. OpenSSL::X509::Certificate) often are issued
711  * on the basis of a public/private RSA key pair. RSA is used in a wide
712  * field of applications such as secure (symmetric) key exchange, e.g.
713  * when establishing a secure TLS/SSL connection. It is also used in
714  * various digital signature schemes.
715  */
717 
718  rb_define_singleton_method(cRSA, "generate", ossl_rsa_s_generate, -1);
719  rb_define_method(cRSA, "initialize", ossl_rsa_initialize, -1);
720  rb_define_method(cRSA, "initialize_copy", ossl_rsa_initialize_copy, 1);
721 
722  rb_define_method(cRSA, "public?", ossl_rsa_is_public, 0);
723  rb_define_method(cRSA, "private?", ossl_rsa_is_private, 0);
724  rb_define_method(cRSA, "to_text", ossl_rsa_to_text, 0);
725  rb_define_method(cRSA, "export", ossl_rsa_export, -1);
726  rb_define_alias(cRSA, "to_pem", "export");
727  rb_define_alias(cRSA, "to_s", "export");
728  rb_define_method(cRSA, "to_der", ossl_rsa_to_der, 0);
729  rb_define_method(cRSA, "public_key", ossl_rsa_to_public_key, 0);
730  rb_define_method(cRSA, "public_encrypt", ossl_rsa_public_encrypt, -1);
731  rb_define_method(cRSA, "public_decrypt", ossl_rsa_public_decrypt, -1);
732  rb_define_method(cRSA, "private_encrypt", ossl_rsa_private_encrypt, -1);
733  rb_define_method(cRSA, "private_decrypt", ossl_rsa_private_decrypt, -1);
734 
740  DEF_OSSL_PKEY_BN(cRSA, rsa, dmp1);
741  DEF_OSSL_PKEY_BN(cRSA, rsa, dmq1);
742  DEF_OSSL_PKEY_BN(cRSA, rsa, iqmp);
743  rb_define_method(cRSA, "set_key", ossl_rsa_set_key, 3);
744  rb_define_method(cRSA, "set_factors", ossl_rsa_set_factors, 2);
745  rb_define_method(cRSA, "set_crt_params", ossl_rsa_set_crt_params, 3);
746 
747  rb_define_method(cRSA, "params", ossl_rsa_get_params, 0);
748 
749  DefRSAConst(PKCS1_PADDING);
750  DefRSAConst(SSLV23_PADDING);
751  DefRSAConst(NO_PADDING);
752  DefRSAConst(PKCS1_OAEP_PADDING);
753 
754 /*
755  * TODO: Test it
756  rb_define_method(cRSA, "blinding_on!", ossl_rsa_blinding_on, 0);
757  rb_define_method(cRSA, "blinding_off!", ossl_rsa_blinding_off, 0);
758  */
759 }
760 
761 #else /* defined NO_RSA */
762 void
763 Init_ossl_rsa(void)
764 {
765 }
766 #endif /* NO_RSA */
VALUE cRSA
Definition: ossl_pkey_rsa.c:44
VALUE mOSSL
Definition: ossl.c:231
#define NewPKey(klass)
Definition: ossl_pkey.h:22
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
VALUE mPKey
Definition: ossl_pkey.c:15
VALUE ePKeyError
Definition: ossl_pkey.c:17
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:821
#define SetPKey(obj, pkey)
Definition: ossl_pkey.h:24
#define Qtrue
Definition: ruby.h:437
#define ossl_str_adjust(str, p)
Definition: ossl.h:82
#define GetRSA(obj, rsa)
Definition: ossl_pkey_rsa.c:20
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
void rb_str_set_len(VALUE, long)
Definition: string.c:2627
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define GetPKey(obj, pkey)
Definition: ossl_pkey.h:31
#define OSSL_PKEY_IS_PRIVATE(obj)
Definition: ossl_pkey.h:20
#define BN_GENCB_new()
#define DefRSAConst(x)
VALUE ossl_rsa_new(EVP_PKEY *pkey)
Definition: ossl_pkey_rsa.c:73
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:29
#define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2)
Definition: ossl_pkey.h:228
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
Definition: object.c:277
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:255
void ossl_generate_cb_stop(void *ptr)
Definition: ossl_pkey.c:50
void ossl_clear_error(void)
Definition: ossl.c:304
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
const EVP_CIPHER * ossl_evp_get_cipherbyname(VALUE obj)
Definition: ossl_cipher.c:52
#define OSSL_BIO_reset(bio)
Definition: ossl.h:110
#define NIL_P(v)
Definition: ruby.h:451
#define DEF_OSSL_PKEY_BN(class, keytype, name)
Definition: ossl_pkey.h:234
VALUE eOSSLError
Definition: ossl.c:236
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
#define rb_str_new2
Definition: intern.h:835
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
Definition: ossl.c:177
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
Definition: ossl_pkey.c:24
void Init_ossl_rsa(void)
#define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3)
Definition: ossl_pkey.h:221
VALUE eRSAError
Definition: ossl_pkey_rsa.c:45
VALUE rb_hash_new(void)
Definition: hash.c:424
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
#define GetPKeyRSA(obj, pkey)
Definition: ossl_pkey_rsa.c:14
unsigned long VALUE
Definition: ruby.h:85
VALUE ossl_pem_passwd_value(VALUE pass)
Definition: ossl.c:151
VALUE rb_eTypeError
Definition: error.c:801
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:975
#define NUM2ULONG(x)
Definition: ruby.h:658
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
#define BN_GENCB_free(cb)
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:58
#define RSTRING_LENINT(str)
Definition: ruby.h:983
VALUE cPKey
Definition: ossl_pkey.c:16
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
#define NULL
Definition: _sdbm.c:102
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
char ** argv
Definition: ruby.c:188
#define StringValue(v)
Definition: ruby.h:569
VALUE rb_str_new(const char *, long)
Definition: string.c:737