Skip to content

Commit fb2fcbb

Browse files
committed
x509store: let X509::Store#add_file raise TypeError if nil is given
Undo special treatment of nil and simply pass the value to StringValueCStr(). nil was never a valid argument for the method; OpenSSL::X509::StoreError with an unhelpful error message "system lib" was raised in that case.
1 parent f36af95 commit fb2fcbb

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

ext/openssl/ossl_x509store.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,15 @@ ossl_x509store_add_file(VALUE self, VALUE file)
301301
{
302302
X509_STORE *store;
303303
X509_LOOKUP *lookup;
304-
char *path = NULL;
304+
const char *path;
305305

306-
if(file != Qnil){
307-
path = StringValueCStr(file);
308-
}
309306
GetX509Store(self, store);
307+
path = StringValueCStr(file);
310308
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
311-
if(lookup == NULL) ossl_raise(eX509StoreError, NULL);
312-
if(X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM) != 1){
313-
ossl_raise(eX509StoreError, NULL);
314-
}
309+
if (!lookup)
310+
ossl_raise(eX509StoreError, "X509_STORE_add_lookup");
311+
if (X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM) != 1)
312+
ossl_raise(eX509StoreError, "X509_LOOKUP_load_file");
315313
#if OPENSSL_VERSION_NUMBER < 0x10101000 || defined(LIBRESSL_VERSION_NUMBER)
316314
/*
317315
* X509_load_cert_crl_file() which is called from X509_LOOKUP_load_file()
@@ -336,17 +334,15 @@ ossl_x509store_add_path(VALUE self, VALUE dir)
336334
{
337335
X509_STORE *store;
338336
X509_LOOKUP *lookup;
339-
char *path = NULL;
337+
const char *path;
340338

341-
if(dir != Qnil){
342-
path = StringValueCStr(dir);
343-
}
344339
GetX509Store(self, store);
340+
path = StringValueCStr(dir);
345341
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
346-
if(lookup == NULL) ossl_raise(eX509StoreError, NULL);
347-
if(X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1){
348-
ossl_raise(eX509StoreError, NULL);
349-
}
342+
if (!lookup)
343+
ossl_raise(eX509StoreError, "X509_STORE_add_lookup");
344+
if (X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1)
345+
ossl_raise(eX509StoreError, "X509_LOOKUP_add_dir");
350346

351347
return self;
352348
}

test/openssl/test_x509store.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,44 @@ def test_nosegv_on_cleanup
2626
ctx.verify
2727
end
2828

29-
def test_add_file
29+
def test_add_file_path
3030
ca_exts = [
3131
["basicConstraints", "CA:TRUE", true],
3232
["keyUsage", "cRLSign,keyCertSign", true],
3333
]
34-
cert1 = issue_cert(@ca1, @rsa1024, 1, ca_exts, nil, nil)
35-
cert2 = issue_cert(@ca2, @rsa2048, 1, ca_exts, nil, nil)
36-
tmpfile = Tempfile.open { |f| f << cert1.to_pem << cert2.to_pem; f }
34+
cert1_subj = OpenSSL::X509::Name.parse_rfc2253("CN=Cert 1")
35+
cert1_key = Fixtures.pkey("rsa-1")
36+
cert1 = issue_cert(cert1_subj, cert1_key, 1, ca_exts, nil, nil)
37+
cert2_subj = OpenSSL::X509::Name.parse_rfc2253("CN=Cert 2")
38+
cert2_key = Fixtures.pkey("rsa-2")
39+
cert2 = issue_cert(cert2_subj, cert2_key, 1, ca_exts, nil, nil)
3740

41+
# X509::Store#add_file reads concatenated PEM file
42+
tmpfile = Tempfile.open { |f| f << cert1.to_pem << cert2.to_pem; f }
3843
store = OpenSSL::X509::Store.new
3944
assert_equal false, store.verify(cert1)
4045
assert_equal false, store.verify(cert2)
4146
store.add_file(tmpfile.path)
4247
assert_equal true, store.verify(cert1)
4348
assert_equal true, store.verify(cert2)
4449

50+
# X509::Store#add_path
51+
Dir.mktmpdir do |dir|
52+
hash1 = "%08x.%d" % [cert1_subj.hash, 0]
53+
File.write(File.join(dir, hash1), cert1.to_pem)
54+
store = OpenSSL::X509::Store.new
55+
store.add_path(dir)
56+
57+
assert_equal true, store.verify(cert1)
58+
assert_equal false, store.verify(cert2)
59+
end
60+
4561
# OpenSSL < 1.1.1 leaks an error on a duplicate certificate
4662
assert_nothing_raised { store.add_file(tmpfile.path) }
4763
assert_equal [], OpenSSL.errors
64+
65+
# Non-String is given
66+
assert_raise(TypeError) { store.add_file(nil) }
4867
ensure
4968
tmpfile and tmpfile.close!
5069
end

0 commit comments

Comments
 (0)