openssl: Loading certs via `file:` scheme only works for absolute paths and fails silently on error
#3542 added to the documentation (in OSSL_STORE_LOADER.pod):
The B<file:> scheme has built in support.
#8442 introduced the following documentation text regarding to the -CAstore option of many apps to openssl.pod:
=item B<-CAstore> I<uri>
Use I<uri> as a store of trusted CA certificates. The URI may
indicate a single certificate, as well as a collection of them.
With URIs in the C<file:> scheme, this acts as B<-CAfile> or
B<-CApath>, depending on if the URI indicates a single file or
directory.
See L<ossl_store-file(7)> for more information on the C<file:> scheme.
from which it got moved later to openssl-verification-options.pod.
Similarly, the following text was added regarding the -chainCAstore option to openssl-s_server.pod.in:
=item B<-chainCAstore> I<uri>
The URI to a store to use for building the chain provided to the client.
The URI may indicate a single certificate, as well as a collection of
them.
With URIs in the C<file:> scheme, this acts as B<-chainCAfile> or
B<-chainCApath>, depending on if the URI indicates a directory or a
single file.
See L<ossl_store-file(7)> for more information on the C<file:> scheme.
which got later copied for consistency also to openssl-s_client.pod.in.
Yet this does not really work.
For instance, when using s_client with -CAfile and the explicit file: scheme with an existing file,
such as file:test/certs/root-cert.pem, one gets:
80CC5EF401000000:error:80000002:system library:BIO_new_file:No such file or directory:crypto/bio/bss_file.c:67:calling fopen(file:test/certs/root-cert.pem, r)
And with the -CAstore option, when using the same argument or just test/certs/root-cert.pem, the option gets ignored entirely (which can be seen also by referencing a non-existing file).
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 63 (59 by maintainers)
Commits related to this issue
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to levitte/openssl by levitte 2 years ago
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to levitte/openssl by levitte 2 years ago
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to levitte/openssl by levitte 2 years ago
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to levitte/openssl by levitte 2 years ago
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to levitte/openssl by levitte 2 years ago
- Relax absolut path checking in our 'file' scheme implementation So far, we strictly obeyed [RFC 8089], which only allows absolute paths in a 'file:' URI. However, this seems to give a confusing user... — committed to kiyolee/openssl by levitte 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file Fixes #27461 — committed to siemens/openssl by DDvO 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file This fixes the remaining issues discussed in bug report #27461: When registering a store UR... — committed to siemens/openssl by DDvO 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file This fixes the remaining issues discussed in bug report #27461: When registering a store UR... — committed to siemens/openssl by DDvO 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file This fixes the remaining issues discussed in bug report #27461: When registering a store UR... — committed to siemens/openssl by DDvO 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file This fixes the remaining issues discussed in bug report #27461: When registering a store UR... — committed to siemens/openssl by DDvO 2 years ago
- X509_LOOKUP_add_store{,_ex}() etc: add missing check that file is present if URI gets interpreted as a file This fixes the remaining issues discussed in bug report #27461: When registering a store UR... — committed to siemens/openssl by DDvO 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to levitte/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to levitte/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to levitte/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to openssl/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to Ballalhossaintalukder/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to levitte/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to levitte/openssl by levitte 2 years ago
- Rework the "by store" X509_LOOKUP method to open the given URI early The cached X509_LOOKUP method data is no longer just the URI, but now includes the OSSL_STORE_CTX pointer, and required parameters... — committed to siemens/openssl by levitte 2 years ago
Huh? @petrovr ,
-CAstoreshould indeed be able to handlefile:URIs. That’s its design.Ah, I see where the problem lies.
See, the
file:scheme is a bit strict by wanting an absolute path, as you can see in https://datatracker.ietf.org/doc/html/rfc8089#section-2 (see how it sayspath-absolutefor the path part), so the ourfilestore loader applies the same strictness when thefile:scheme is an explicit part of the given URI.However, it does cheat when the
file:scheme is only implied, and allows relative paths then.So, while this command doesn’t work:
… this command does work:
Now, this is how our
filestore loader / storemgmt works. Someone else might make an alternative implementation that’s more lenient. I’m guessing that you’d want to modify some documentation accordingly, just be careful that you’re not making undue assumption that ourfilestoremgmt is the one in use.If you wanna see the “magic” in how the implicit vs explicit
filescheme is being treated, this is the place to look:https://github.com/openssl/openssl/blob/1ad186986c8c90dfc58666531c0554641e022dbb/providers/implementations/storemgmt/file_store.c#L208-L270
Now, I’ll point out that an error is actually generated when a relative path that should be absolute is encountered:
https://github.com/openssl/openssl/blob/1ad186986c8c90dfc58666531c0554641e022dbb/providers/implementations/storemgmt/file_store.c#L256-L261
But since we’re turning off all error reports further up the call chain, we never get to see them… which is quite unfortunate.
-CAfileexpects a file name, period, not a URI. That file name is eventually passed toBIO_new_file(), just like you experienced.A hack to get around that is to shave off
file:, not in theX509_L_ADD_STOREcase, but in theX509_L_FILE_LOADcase, found incrypto/x509/by_path.c. It’s a pretty horrible thing to do, though, 'cause it’s always possible that this code finds itself on a platform that has a devicefile, which is then a legitimate part of the file name.So all in all, trying a URI with
-CAfileis strictly speaking a user error, even though I understand why you’d do that.Ugh, just realized that also on Linux and MacOS a simple file or directory name may start with
file:.So we may have to keep dealing with the ambiguity, and for returning an (early) error in case a file store (CLI option or function) argument refers to a non-existing (or non-readable) file, where the argument string is a
file:URI or happens to be a plain file path (without a scheme) starting withfile:, we likely will need to try both the full string and the one resulting from chopping the leadingfile:.My suggestion would then be to first call
stat()on the full argument wrapped withERR_set_mark()…ERR_pop_to_mark(), and on failure do a non-wrappedstat()call without the leadingfile:because if this call does not succeed either, the error queue entry will refer to the more likely file (path) name withoutfile:.For quite a lot of other parameters, you will see something like this:
The idea was always that our
filescheme implementation should be able to handle both (and in some cases, alsoI<directory>).Regarding RFC 3986, you might want to study section, there’s a whole lot about relative references and how that’s merged with a base URI, so just imagine
file:$(pwd)/as a base URI, yeah?I’m not very keen on adding “special knowledge” into places that are meant to be generic. It would be better, in my opinion, to add a
stat()like feature in the OSSL_STORE library, and use that.But, this is quite off-topic for this issue, would you mind raising another for that sort of effort?
Mind you that the error on
file:whatever.pemerror happens in the internals, during certificate path building, because “by store” works like “by dir” and loads objects on demand, and the verify callback would of course struggle to know if the verification error is due to unmatched verification criteria or because there was an underlying data issue.A fix for this could be to open the URIs much earlier in “by store”… but oh boy, that’s a potential can of worms.
This highlights a more fundamental issue: an unfortunate ambiguity between URIs and local file paths on some systems.
In my view, for
-CAstoreand the like and their API counterparts, all of which are documented as taking a URI argument, in doubt the URI interpretation (with file: being interpreted as scheme) should be chosen. Thus we would be back to just one open/stat call.@DDvO, ‘file:foo.pem’ can be two things:
So our file scheme implementation attempts to open (actually calling
stat()on) both ‘file:foo.pem’ and ‘foo.pem’, and if one of them is successful, opening is successful.I this case, your logic of “if one fails, the other is likely to fail” isn’t quite right.
@petrovr, “by store” doesn’t distinguish between file and directory. For non-file URIs (like PKCS#11 URIs), that’s not even relevant.
From an OSSL_STORE (which is what “by store” uses) perspective, a URI is a container, which may contain one or more objects (certs, keys,…) of interest. So “by store” treats a URI more like a directory than a file, so loads objects on request (by subject).
Because it’s a bad place for that hack. It means that while certificates loaded specifically into the X509_STORE would enjoy explicit
file:URIs with relative paths, other uses offile:URIs wouldn’t. An example is loading a key, which is done with other functions. The end result would be inconsistent.There are a few spots where multiple attempts are made to open a file resource. It is feasible to ignore all errors surrounding that if one of the attempts is successful. However, we may want to keep all those queued errors if none of the attempts are successful. That would be a worthy fix.
I’d like to point out that you can’t refer to the generic URI syntax spec to dispute a specific scheme’s limitations. It just doesn’t work like that