From: "brandur (Brandur Leach)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109863] [Ruby master Feature#12354] PKey::EC Can't output public key pem when private key exists
Date: Fri, 09 Sep 2022 20:44:14 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-99103.20220909204413.10905@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-12354.20160506161059.10905@ruby-lang.org
Issue #12354 has been updated by brandur (Brandur Leach).
If you're linked against OpenSSL >= 3, the workarounds suggested here don't seem to work any longer because a pkey is not allowed to be modified:
```
Failure/Error: pub.public_key = SIGNING_KEY.public_key
OpenSSL::PKey::PKeyError:
pkeys are immutable on OpenSSL 3.0
# ./app/pellet.rb:41:in `public_key='
```
And seen from Ruby's OpenSSL source:
``` c
static VALUE ossl_ec_key_set_private_key(VALUE self, VALUE private_key)
{
#if OSSL_OPENSSL_PREREQ(3, 0, 0)
rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
#else
...
```
I looked around for some other workaround, but I wasn't able to find one (although I don't know this code well, so I'm hoping someone else can suggest one). For what it's worth, I'd intuitively expect something like `key.public_key.to_pem` to work.
Given the OpenSSL 3 incompatibility, I wonder if this should be upgraded back to a bug?
----------------------------------------
Feature #12354: PKey::EC Can't output public key pem when private key exists
https://bugs.ruby-lang.org/issues/12354#change-99103
* Author: armour (Armour Comms)
* Status: Assigned
* 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/
next parent reply other threads:[~2022-09-09 20:44 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-12354.20160506161059.10905@ruby-lang.org>
2022-09-09 20:44 ` brandur (Brandur Leach) [this message]
2022-09-11 7:13 ` [ruby-core:109885] [Ruby master Feature#12354] PKey::EC Can't output public key pem when private key exists rhenium (Kazuki Yamaguchi)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-99103.20220909204413.10905@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).