Skip to content

Commit be3d2c8

Browse files
committed
[compat] Store#add_cert and add_crl handle wrong argument
MRI raises TypeError with a descriptive message (e.g. "wrong argument type String (expected OpenSSL/X509)")
1 parent 5f8a1bd commit be3d2c8

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,25 @@ public IRubyObject set_default_paths(final ThreadContext context) {
203203
}
204204

205205
@JRubyMethod
206-
public X509Store add_cert(final IRubyObject cert) {
207-
X509AuxCertificate auxCert = cert instanceof X509Cert ? ((X509Cert) cert).getAuxCert() : null;
206+
public X509Store add_cert(final ThreadContext context, final IRubyObject cert) {
207+
if (!(cert instanceof X509Cert)) {
208+
throw context.runtime.newTypeError(cert, _X509(context.runtime).getClass("Certificate"));
209+
}
210+
X509AuxCertificate auxCert = ((X509Cert) cert).getAuxCert();
208211
if ( store.addCertificate(auxCert) != 1 ) {
209-
throw newStoreError(getRuntime(), X509Error.getLastErrorMessage());
212+
throw newStoreError(context.runtime, X509Error.getLastErrorMessage());
210213
}
211214
return this;
212215
}
213216

214217
@JRubyMethod
215-
public X509Store add_crl(final IRubyObject crl) {
216-
java.security.cert.X509CRL jCRL = (crl instanceof X509CRL) ? ((X509CRL) crl).getCRL() : null;
218+
public X509Store add_crl(final ThreadContext context, final IRubyObject crl) {
219+
if (!(crl instanceof X509CRL)) {
220+
throw context.runtime.newTypeError(crl, _X509(context.runtime).getClass("CRL"));
221+
}
222+
java.security.cert.X509CRL jCRL = ((X509CRL) crl).getCRL();
217223
if ( store.addCRL(jCRL) != 1 ) {
218-
throw newStoreError(getRuntime(), X509Error.getLastErrorMessage());
224+
throw newStoreError(context.runtime, X509Error.getLastErrorMessage());
219225
}
220226
return this;
221227
}

src/test/ruby/x509/test_x509store.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ def test_store_time_accepts_integer
149149
assert store.verify(@cert)
150150
end
151151

152+
# CRuby raises TypeError (not StoreError) for wrong argument types
153+
def test_add_cert_type_check
154+
store = OpenSSL::X509::Store.new
155+
assert_raise(TypeError) { store.add_cert("not a cert") }
156+
assert_raise(TypeError) { store.add_cert(nil) }
157+
end
158+
159+
def test_add_crl_type_check
160+
store = OpenSSL::X509::Store.new
161+
assert_raise(TypeError) { store.add_crl("not a crl") }
162+
assert_raise(TypeError) { store.add_crl(nil) }
163+
end
164+
152165
# CRuby undefs initialize_copy, blocking both dup and clone
153166
def test_store_dup_clone_not_allowed
154167
store = OpenSSL::X509::Store.new

0 commit comments

Comments
 (0)