Skip to content

OpenSSL::PKey::PKey subclass for EVP_PKEY_RSA_PSS #715

Open

Description

@Anstuhrm

I had the same problem, see #562, to open a key and got the error OpenSSL::PKey::RSAError: incorrect pkey type: RSASSA-PSS.
So I used the mentioned method and called OpenSSL::PKey.read.
But now I have a problem to sign a JWT with the algorithm RSASSA-PSS:

JWT.encode({}, key, "ps256")
# This returns:
# NoMethodError: undefined method `sign_pss' for #<OpenSSL::PKey::PKey oid=RSASSA-PSS>

Activity

  1. rhenium commented on Jan 17, 2024

    @rhenium
    Member

    You can currently use OpenSSL::PKey::PKey#sign to do RSA-PSS without needing the RSA#sign_pss. The test code may be useful for you today:

    def test_sign_verify_options
    key = Fixtures.pkey("rsa1024")
    data = "Sign me!"
    pssopts = {
    "rsa_padding_mode" => "pss",
    "rsa_pss_saltlen" => 20,
    "rsa_mgf1_md" => "SHA1"
    }
    sig_pss = key.sign("SHA256", data, pssopts)
    assert_equal 128, sig_pss.bytesize
    assert_equal true, key.verify("SHA256", sig_pss, data, pssopts)
    assert_equal true, key.verify_pss("SHA256", sig_pss, data,
    salt_length: 20, mgf1_hash: "SHA1")
    # Defaults to PKCS #1 v1.5 padding => verification failure
    assert_equal false, key.verify("SHA256", sig_pss, data)
    # option type check
    assert_raise_with_message(TypeError, /expected Hash/) {
    key.sign("SHA256", data, ["x"])
    }
    end

  2. rhenium commented on Jan 17, 2024

    @rhenium
    Member

    I wonder if we could add a PKey class for EVP_PKEY_RSA_PSS. I think #sign_pss and #verify_pss should just work on the pkey object decoded from PKCS#8.

    EVP_PKEY_RSA_PSS seems to be just a variant of EVP_PKEY_RSA that contains default parameters for RSA-PSS and is locked to RSA-PSS operations.

  3. changed the title [-]Problem to sign a JWT with RSASSA-PSS key[/-] [+]OpenSSL::PKey::PKey subclass for EVP_PKEY_RSA_PSS[/+] on Jan 17, 2024
  4. trkoch commented on Jan 19, 2024

    @trkoch

    I'm having the same issue. I'm trying to sign a JWT with a RSASSA-PSS1 private key (created with openssl req -newkey rsa-pss).

    Not sure I follow the suggestion. Using #sign instead of #sign_pss does not work.

    # jwt-2.7.1/lib/jwt/algos/ps.rb:19
    key.sign(translated_algorithm, msg, salt_length: :digest, mgf1_hash: translated_algorithm)

    Throws:

    OpenSSL::PKey::PKeyError: EVP_PKEY_CTX_ctrl_str(ctx, "salt_length", "digest"): command not supported ([action:2, state:4] name=salt_length, value=digest)
    

    @Anstuhrm Did you mange to work around this?

  5. trkoch commented on Jan 19, 2024

    @trkoch

    It does seem to work with:

    key.sign(translated_algorithm, msg)

    I have no idea what I'm doing here, frankly. Can someone elaborate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions