ec_pem_pwd2.diff

Martin Bosslet, 03/01/2011 07:56 AM

Download (4.44 KB)

View differences:

ruby/ext/openssl/ossl_pkey_ec.c (working copy)
152 152
 *     OpenSSL::PKey::EC.new(ec_group)
153 153
 *     OpenSSL::PKey::EC.new("secp112r1")
154 154
 *     OpenSSL::PKey::EC.new(pem_string)
155
 *     OpenSSL::PKey::EC.new(pem_string [, pwd])
155 156
 *     OpenSSL::PKey::EC.new(der_string)
156 157
 *
157 158
 *  See the OpenSSL documentation for:
......
163 164
    EC_KEY *ec = NULL;
164 165
    VALUE arg, pass;
165 166
    VALUE group = Qnil;
167
    char *passwd = NULL;
166 168

  
167 169
    GetPKey(self, pkey);
168 170
    if (pkey->pkey.ec)
......
183 185
        	group = arg;
184 186
        } else {
185 187
            BIO *in = ossl_obj2bio(arg);
186

  
187
            ec = PEM_read_bio_ECPrivateKey(in, NULL, NULL, NULL);
188
            if (!NIL_P(pass)) {
189
                passwd = StringValuePtr(pass);
190
            }
191
            ec = PEM_read_bio_ECPrivateKey(in, NULL, ossl_pem_passwd_cb, passwd);
188 192
            if (!ec) {
189 193
                (void)BIO_reset(in);
190
                ec = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
194
                ec = PEM_read_bio_EC_PUBKEY(in, NULL, ossl_pem_passwd_cb, passwd);
191 195
            }
192 196
            if (!ec) {
193 197
                (void)BIO_reset(in);
......
457 461
    return (EC_KEY_get0_private_key(ec) ? Qtrue : Qfalse);
458 462
}
459 463

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

  
472 473
    Require_EC_KEY(self, ec);
......
486 487
    switch(format) {
487 488
    case EXPORT_PEM:
488 489
    	if (private) {
489
#if 0  /* unused now */
490
    	    if (cipher || password)
491
/* BUG: finish cipher/password key export */
492
    	        rb_notimplement();
490
	    const EVP_CIPHER *cipher;
491
	    if (!NIL_P(ciph)) {
492
		cipher = GetCipherPtr(ciph);
493
		if (!NIL_P(pass)) {
494
		    password = StringValuePtr(pass);
495
		}
496
	    }
497
	    else {
498
		cipher = NULL;
499
	    }
493 500
            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 501
    	} 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 502
            i = PEM_write_bio_EC_PUBKEY(out, ec);
503 503
        }
504 504

  
505 505
    	break;
506 506
    case EXPORT_DER:
507 507
        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 508
            i = i2d_ECPrivateKey_bio(out, ec);
514 509
        } 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 510
            i = i2d_EC_PUBKEY_bio(out, ec);
521 511
        }
522 512

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

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

  
561 559
/*
......
1526 1524
    rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1527 1525
/* do_sign/do_verify */
1528 1526

  
1529
    rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, 0);
1527
    rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, -1);
1530 1528
    rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);
1531 1529
    rb_define_method(cEC, "to_text", ossl_ec_key_to_text, 0);
1532 1530