[ruby-core:109885] [Ruby master Feature#12354] PKey::EC Can't output public key pem when private key exists
From:
"rhenium (Kazuki Yamaguchi)" <noreply@...>
Date:
2022-09-11 07:13:10 UTC
List:
ruby-core #109885
Issue #12354 has been updated by rhenium (Kazuki Yamaguchi).
Status changed from Assigned to Closed
openssl v2.2 added OpenSSL::PKey::PKey#public_to_pem and #public_to_der for this purpose.
https://github.com/ruby/openssl/pull/297
Ruby 3.0 includes v2.2 by default.
----------------------------------------
Feature #12354: PKey::EC Can't output public key pem when private key exists
https://bugs.ruby-lang.org/issues/12354#change-99126
* Author: armour (Armour Comms)
* Status: Closed
* Priority: Normal
* Assignee: rhenium (Kazuki Yamaguchi)
----------------------------------------
Steps to reproduce:
Create EC key:
```ruby
key = OpenSSL::PKey::EC.new("prime256v1")
key.generate_key
```
Try and output in pem format
```ruby
key.to_pem #Outputs private key pem
key.public_key.to_pem #Error
```
In order to output a public key pem, a new key object must be created with no private key:
```ruby
key_pub = OpenSSL::PKey::EC.new(key.group)
key_pub.public_key = key.public_key
```
Output pem
```ruby
key_pub.to_pem #Success!
```
From viewing the source, http://rxr.whitequark.org/mri/source/ext/openssl/ossl_pkey_ec.c#466 it seems that if the key is private there is no way to output a public key for that key object
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>