ec_pem_pwd.diff

Martin Bosslet, 02/22/2011 08:36 AM

Download (3.37 KB)

View differences:

ruby/ext/openssl/ossl_pkey_ec.c (working copy)
457 457
    return (EC_KEY_get0_private_key(ec) ? Qtrue : Qfalse);
458 458
}
459 459

  
460
static VALUE ossl_ec_key_to_string(VALUE self, int format)
460
static VALUE ossl_ec_key_to_string(VALUE self, VALUE ciph, VALUE pass, int format)
461 461
{
462 462
    EC_KEY *ec;
463 463
    BIO *out;
464 464
    int i = -1;
465 465
    int private = 0;
466
#if 0  /* unused now */
467
    EVP_CIPHER *cipher = NULL;
468 466
    char *password = NULL;
469
#endif
470 467
    VALUE str;
471 468

  
472 469
    Require_EC_KEY(self, ec);
......
486 483
    switch(format) {
487 484
    case EXPORT_PEM:
488 485
    	if (private) {
489
#if 0  /* unused now */
490
    	    if (cipher || password)
491
/* BUG: finish cipher/password key export */
492
    	        rb_notimplement();
486
	    const EVP_CIPHER *cipher;
487
	    if (!NIL_P(ciph)) {
488
		cipher = GetCipherPtr(ciph);
489
		if (!NIL_P(pass)) {
490
		    password = StringValuePtr(pass);
491
		}
492
	    }
493
	    else {
494
		cipher = NULL;
495
	    }
493 496
            i = PEM_write_bio_ECPrivateKey(out, ec, cipher, NULL, 0, NULL, password);
494
#endif
495
            i = PEM_write_bio_ECPrivateKey(out, ec, NULL, NULL, 0, NULL, NULL);
496 497
    	} else {
497
#if 0  /* unused now */
498
    	    if (cipher || password)
499
                rb_raise(rb_eArgError, "encryption is not supported when exporting this key type");
500
#endif
501

  
502 498
            i = PEM_write_bio_EC_PUBKEY(out, ec);
503 499
        }
504 500

  
505 501
    	break;
506 502
    case EXPORT_DER:
507 503
        if (private) {
508
#if 0  /* unused now */
509
    	    if (cipher || password)
510
                rb_raise(rb_eArgError, "encryption is not supported when exporting this key type");
511
#endif
512

  
513 504
            i = i2d_ECPrivateKey_bio(out, ec);
514 505
        } else {
515
#if 0  /* unused now */
516
    	    if (cipher || password)
517
                rb_raise(rb_eArgError, "encryption is not supported when exporting this key type");
518
#endif
519

  
520 506
            i = i2d_EC_PUBKEY_bio(out, ec);
521 507
        }
522 508

  
......
539 525
/*
540 526
 *  call-seq:
541 527
 *     key.to_pem   => String
528
 *     key.to_pem(cipher, pass_phrase) => String
542 529
 *
543
 *  See the OpenSSL documentation for PEM_write_bio_ECPrivateKey()
530
 * Outputs the EC key in PEM encoding.  If +cipher+ and +pass_phrase+ are
531
 * given they will be used to encrypt the key.  +cipher+ must be an
532
 * OpenSSL::Cipher::Cipher instance. Note that encryption will only be
533
 * effective for a private key, public keys will always be encoded in plain
534
 * text.
535
 *
544 536
 */
545
static VALUE ossl_ec_key_to_pem(VALUE self)
537
static VALUE ossl_ec_key_to_pem(int argc, VALUE *argv, VALUE self)
546 538
{
547
    return ossl_ec_key_to_string(self, EXPORT_PEM);
539
    VALUE cipher, passwd;
540
    rb_scan_args(argc, argv, "02", &cipher, &passwd);
541
    return ossl_ec_key_to_string(self, cipher, passwd, EXPORT_PEM);
548 542
}
549 543

  
550 544
/*
......
555 549
 */
556 550
static VALUE ossl_ec_key_to_der(VALUE self)
557 551
{
558
    return ossl_ec_key_to_string(self, EXPORT_DER);
552
    return ossl_ec_key_to_string(self, Qnil, Qnil, EXPORT_DER);
559 553
}
560 554

  
561 555
/*
......
1526 1520
    rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1527 1521
/* do_sign/do_verify */
1528 1522

  
1529
    rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, 0);
1523
    rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, -1);
1530 1524
    rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);
1531 1525
    rb_define_method(cEC, "to_text", ossl_ec_key_to_text, 0);
1532 1526