Skip to content

Commit 411aefb

Browse files
committed
[compat] adjust error raised from PKey::RSA
1 parent adc5665 commit 411aefb

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/java/org/jruby/ext/openssl/PKeyRSA.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ private static ASN1ObjectIdentifier osslNameToCipherOid(final String osslName) {
595595

596596
private String getPadding(final int padding) {
597597
if ( padding < 1 || padding > 4 ) {
598-
throw newRSAError(getRuntime(), "");
598+
throw newPKeyError(getRuntime(), "");
599599
}
600600
// BC accepts "/NONE/*" but SunJCE doesn't. use "/ECB/*"
601601
String p = "/ECB/PKCS1Padding";
@@ -615,7 +615,7 @@ public IRubyObject private_encrypt(final ThreadContext context, final IRubyObjec
615615
if ( Arity.checkArgumentCount(context.runtime, args, 1, 2) == 2 && ! args[1].isNil() ) {
616616
padding = RubyNumeric.fix2int(args[1]);
617617
}
618-
if ( privateKey == null ) throw newRSAError(context.runtime, "incomplete RSA");
618+
if ( privateKey == null ) throw newPKeyError(context.runtime, "incomplete RSA");
619619
return doCipherRSA(context.runtime, args[0], padding, ENCRYPT_MODE, privateKey);
620620
}
621621

@@ -625,7 +625,7 @@ public IRubyObject private_decrypt(final ThreadContext context, final IRubyObjec
625625
if ( Arity.checkArgumentCount(context.runtime, args, 1, 2) == 2 && ! args[1].isNil()) {
626626
padding = RubyNumeric.fix2int(args[1]);
627627
}
628-
if ( privateKey == null ) throw newRSAError(context.runtime, "incomplete RSA");
628+
if ( privateKey == null ) throw newPKeyError(context.runtime, "incomplete RSA");
629629
return doCipherRSA(context.runtime, args[0], padding, DECRYPT_MODE, privateKey);
630630
}
631631

@@ -635,7 +635,7 @@ public IRubyObject public_encrypt(final ThreadContext context, final IRubyObject
635635
if ( Arity.checkArgumentCount(context.runtime, args, 1, 2) == 2 && ! args[1].isNil()) {
636636
padding = RubyNumeric.fix2int(args[1]);
637637
}
638-
if ( publicKey == null ) throw newRSAError(context.runtime, "incomplete RSA");
638+
if ( publicKey == null ) throw newPKeyError(context.runtime, "incomplete RSA");
639639
return doCipherRSA(context.runtime, args[0], padding, ENCRYPT_MODE, publicKey);
640640
}
641641

@@ -645,7 +645,7 @@ public IRubyObject public_decrypt(final ThreadContext context, final IRubyObject
645645
if ( Arity.checkArgumentCount(context.runtime, args, 1, 2) == 2 && ! args[1].isNil() ) {
646646
padding = RubyNumeric.fix2int(args[1]);
647647
}
648-
if ( publicKey == null ) throw newRSAError(context.runtime, "incomplete RSA");
648+
if ( publicKey == null ) throw newPKeyError(context.runtime, "incomplete RSA");
649649
return doCipherRSA(context.runtime, args[0], padding, DECRYPT_MODE, publicKey);
650650
}
651651

src/test/ruby/rsa/test_rsa.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ def setup
99
require 'base64'
1010
end
1111

12+
def test_no_private_exp
13+
key = OpenSSL::PKey::RSA.new
14+
rsa = Fixtures.pkey("rsa-1.pem")
15+
key.set_key(rsa.n, rsa.e, nil)
16+
key.set_factors(rsa.p, rsa.q)
17+
assert_raise(OpenSSL::PKey::PKeyError){ key.private_encrypt("foo") }
18+
assert_raise(OpenSSL::PKey::PKeyError){ key.private_decrypt("foo") }
19+
end
20+
1221
def test_private
1322
# Generated by key size and public exponent
1423
key = OpenSSL::PKey::RSA.new(512, 3)
@@ -128,6 +137,33 @@ def test_rsa_public_encrypt
128137
# }
129138
end
130139

140+
def test_sign_verify_raw_legacy
141+
key = Fixtures.pkey("rsa-1.pem")
142+
bits = key.n.num_bits
143+
144+
# Need right size for raw mode
145+
plain0 = "x" * (bits/8)
146+
cipher = key.private_encrypt(plain0, OpenSSL::PKey::RSA::NO_PADDING)
147+
plain1 = key.public_decrypt(cipher, OpenSSL::PKey::RSA::NO_PADDING)
148+
assert_equal(plain0, plain1)
149+
150+
# Need smaller size for pkcs1 mode
151+
plain0 = "x" * (bits/8 - 11)
152+
cipher1 = key.private_encrypt(plain0, OpenSSL::PKey::RSA::PKCS1_PADDING)
153+
plain1 = key.public_decrypt(cipher1, OpenSSL::PKey::RSA::PKCS1_PADDING)
154+
assert_equal(plain0, plain1)
155+
156+
cipherdef = key.private_encrypt(plain0) # PKCS1_PADDING is default
157+
plain1 = key.public_decrypt(cipherdef)
158+
assert_equal(plain0, plain1)
159+
assert_equal(cipher1, cipherdef)
160+
161+
# Failure cases
162+
assert_raise(ArgumentError){ key.private_encrypt() }
163+
assert_raise(ArgumentError){ key.private_encrypt("hi", 1, nil) }
164+
assert_raise(OpenSSL::PKey::PKeyError){ key.private_encrypt(plain0, 666) }
165+
end
166+
131167
def test_rsa_param_accessors
132168
key_file = File.join(File.dirname(__FILE__), 'private_key.pem')
133169
key = OpenSSL::PKey::RSA.new(File.read(key_file))

0 commit comments

Comments
 (0)