From: k@... Date: 2016-09-05T12:46:56+00:00 Subject: [ruby-dev:49777] [Ruby trunk Bug#12726][Closed] OpenSSL::PKCS12.new がプライベートキーを含まないデータでエラーになる Issue #12726 has been updated by Kazuki Yamaguchi. Status changed from Open to Closed Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: UNKNOWN, 2.2: REQUIRED, 2.3: REQUIRED ありがとうございます。 同様に x509 も NULL になる場合があり、それもあわせて ruby/openssl に取り込みました。 https://github.com/ruby/openssl/commit/68ca4b61bf43a22581ebb5649252a65633a1b680 ---------------------------------------- Bug #12726: OpenSSL::PKCS12.new がプライベートキーを含まないデータでエラーになる https://bugs.ruby-lang.org/issues/12726#change-60374 * Author: Masahiro Tomita * Status: Closed * Priority: Normal * Assignee: * ruby -v: ruby 2.4.0preview1 (2016-06-20 trunk 55466) [x86_64-linux] * Backport: 2.1: UNKNOWN, 2.2: REQUIRED, 2.3: REQUIRED ---------------------------------------- 次のように作成したプライベートキーを含まない 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/