From: tommy@... Date: 2016-09-05T06:45:05+00:00 Subject: [ruby-dev:49776] [Ruby trunk Bug#12726] OpenSSL::PKCS12.new がプライベートキーを含まないデータでエラーになる Issue #12726 has been reported by Masahiro Tomita. ---------------------------------------- Bug #12726: OpenSSL::PKCS12.new がプライベートキーを含まないデータでエラーになる https://bugs.ruby-lang.org/issues/12726 * Author: Masahiro Tomita * Status: Open * Priority: Normal * Assignee: * ruby -v: ruby 2.4.0preview1 (2016-06-20 trunk 55466) [x86_64-linux] * Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- 次のように作成したプライベートキーを含まない pfx ファイルを、 ``` % openssl pkcs12 -export -out hoge.pfx -in hoge.pem -nokeys Enter Export Password:hoge Verifying - Enter Export Password:hoge ``` OpenSSL::PKCS12.new で読み込むとエラーになります。 ``` % ruby -ropenssl -e 'OpenSSL::PKCS12.new(File.read("hoge.pfx"), "hoge")' -e:1:in `initialize': Cannot make new key from NULL. (OpenSSL::PKey::PKeyError) from -e:1:in `new' from -e:1:in `
' ``` Ruby 2.1, 2.2, 2.3 でも同様でした。 次のパッチで直ります。 ```diff --- a/ext/openssl/ossl_pkcs12.c +++ b/ext/openssl/ossl_pkcs12.c @@ -190,9 +190,11 @@ ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self) if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s)) ossl_raise(ePKCS12Error, "PKCS12_parse"); ERR_pop_to_mark(); - pkey = rb_protect((VALUE (*)(VALUE))ossl_pkey_new, (VALUE)key, - &st); /* NO DUP */ - if(st) goto err; + if (key) { + pkey = rb_protect((VALUE (*)(VALUE))ossl_pkey_new, (VALUE)key, + &st); /* NO DUP */ + if(st) goto err; + } cert = rb_protect((VALUE (*)(VALUE))ossl_x509_new, (VALUE)x509, &st); if(st) goto err; if(x509s){ ``` -- https://bugs.ruby-lang.org/