Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SP certificates to be OpenSSL::X509::Certificate #726

Conversation

tobiasamft
Copy link

@tobiasamft tobiasamft commented Oct 3, 2024

This allows settings to accept instances of OpenSSL::X509::Certificate as service provider (SP) certificates.

Solves #723

Version 1.16.0 was, at least partially, able to handle OpenSSL::X509::Certificate as input for settings.certificate (e.g. when using OneLogin::RubySaml::Response).

Since settings.get_sp_certs is the only interface that is used to access certificates, it should be enough to test that interface with instances of OpenSSL::X509::Certificate. There are 3 ways to insert certs, all of them have been tested:

  • settings.certificate
  • settings.certificate_new
  • settings.sp_cert_multi

Note that both deprecated interfaces settings.get_sp_cert and settings.get_sp_cert_new use settings.get_sp_certs internally. Thus, they are covered as well.

Same approach could be used for SP private key to accept OpenSSL::PKey.
Maybe it's a good idea to make all certificates from settings attr_writer for public and attr_accessor for private access to ensure that certs are accessed via settings.get_sp_certs only (but that would break current interface).

@johnnyshields
Copy link
Collaborator

@tobiasamft can you check if this is solved on the v2.x branch? I think it might be already. If it is, we can close this PR b/c we are releasing v2.x soon.

@tobiasamft
Copy link
Author

@johnnyshields unfortunately v2.x does not solve this. Using OpenSSL::X509::Certificate as SP certificate still crashes with the following:
git/ruby-saml/lib/ruby_saml/settings.rb:377:in 'validate_sp_certs_params!': undefined method 'empty?' for an instance of OpenSSL::X509::Certificate (NoMethodError)

@johnnyshields
Copy link
Collaborator

ok. Can you raise the PR to the v2.x branch then please? I will review it.

@tobiasamft tobiasamft changed the base branch from master to v2.x October 4, 2024 06:42
@tobiasamft tobiasamft changed the base branch from v2.x to master October 4, 2024 06:45
This allows settings to accept instances of OpenSSL::X509::Certificate
as service provider (SP) certificates.
@tobiasamft tobiasamft changed the base branch from master to v2.x October 4, 2024 08:17
@tobiasamft tobiasamft force-pushed the allow-openssl-certificate-instances branch from c0b1dd7 to 603f97e Compare October 4, 2024 08:17
@tobiasamft
Copy link
Author

@johnnyshields I've rebased the branch onto v2.x

@johnnyshields
Copy link
Collaborator

@tobiasamft see comment

return true if cert.is_a?(OpenSSL::X509::Certificate)

cert && !cert.empty?
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all changes in this file can be rolled back

Copy link
Author

@tobiasamft tobiasamft Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it we get the initial NoMethodError again (because validate_sp_certs_params is called before Utils.build_cert_object):

NoMethodError: undefined method `empty?' for an instance of OpenSSL::X509::Certificate
    lib/ruby_saml/settings.rb:377:in `validate_sp_certs_params!'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will check this later, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what status is this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pitbulk this change is fine. @tobiasamft how about also adding a pk? method which does the same thing instead of has_pk?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @johnnyshields, I've added pk? now. How about has_multi should it be treated the same way?
And how about lib/ruby_saml/utils.rb, rubocop complains about module size (I've just added 2 lines)?

Copy link
Collaborator

@johnnyshields johnnyshields Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above about allowing OpenSSL::PKey::PKey. You can also increase the max module size from rubocop so it passes.

Return the original certificate from Utils.build_cert_object when an
instance of OpenSSL::X509::Certificate is given. And return the original
key from Utils.build_private_key_object when an instance of
OpenSSL::PKey::PKey is given.
@tobiasamft tobiasamft force-pushed the allow-openssl-certificate-instances branch from 2b2c9cf to 8a6b1dd Compare January 9, 2025 17:23
# Check if private key exists and is not empty
def pk?
private_key && !private_key.empty?
end
Copy link
Collaborator

@johnnyshields johnnyshields Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant we should add:

      return true if private_key.is_a?(OpenSSL::PKey::PKey)

@johnnyshields
Copy link
Collaborator

@tobiasamft thanks for all your help on this, I've reworked and committed this here: #732

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants