Ferenc Erdelyi created HADOOP-20106:
---------------------------------------

             Summary: Self-test OpensslAesCtrCryptoCodec at construction so 
cipher init failures fall back to JCE
                 Key: HADOOP-20106
                 URL: https://issues.apache.org/jira/browse/HADOOP-20106
             Project: Hadoop Common
          Issue Type: Bug
          Components: hadoop-common
    Affects Versions: 3.5.0, 3.6.0
            Reporter: Ferenc Erdelyi


h2. Problem

{{CryptoCodec.getInstance}} tries the classes in 
{{hadoop.security.crypto.codec.classes.aes.ctr.nopadding}} (default: 
{{OpensslAesCtrCryptoCodec}}, {{JceAesCtrCryptoCodec}}). It skips a class only 
if its constructor throws. {{OpensslAesCtrCryptoCodec}} throws only if 
libhadoop failed to load the OpenSSL symbols 
({{OpensslCipher.getLoadingFailureReason() != null}}), and {{hadoop 
checknative}} reports {{openssl: true}} on the same check.

Whether the cipher works also depends on the host OpenSSL configuration, which 
{{OpensslCipher.c}} loads with 
{{OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG)}}: active OpenSSL 3 providers, 
FIPS properties, default ENGINEs. AES-128 and AES-256 use different EVP cipher 
objects, so one key size can fail while the other works.

The native cipher is first initialized after selection, in 
{{CryptoInputStream}} / {{CryptoOutputStream}}. If {{EVP_CipherInit_ex}} fails, 
JNI throws {{java.lang.InternalError}}. Callers handle only {{IOException}} and 
{{GeneralSecurityException}}, so the read or write fails even though 
{{JceAesCtrCryptoCodec}} is configured next. {{getInstance}} logs skipped 
codecs only at DEBUG. The only workaround is:
{code}
hadoop.security.crypto.codec.classes.aes.ctr.nopadding=org.apache.hadoop.crypto.JceAesCtrCryptoCodec
{code}

h2. Reproduction (OpenSSL 3, to be confirmed)

Set {{OPENSSL_CONF}} to a config that activates only the {{base}} provider. The 
{{default}} provider is then not loaded, so AES-CTR cannot be fetched 
(confirmed with the {{openssl enc}} CLI: 
{{inner_evp_generic_fetch:unsupported}}).

Expected: {{checknative}} still reports {{openssl: true}}, and a 
{{CryptoOutputStream}} write with the default codec list fails with 
{{java.lang.InternalError: Error in EVP_CipherInit_ex.}}

h2. Proposed fix

# {{OpensslAesCtrCryptoCodec}} runs a one-block known-answer test for 
AES-128-CTR and AES-256-CTR (NIST SP 800-38A F.5.1, F.5.5) once per JVM and 
caches the result. If the test fails, the constructor throws, so 
{{getInstance}} moves on to {{JceAesCtrCryptoCodec}}, and a single WARN names 
the library, the cause and the codec-list key. The test catches 
{{InternalError}}, {{RuntimeException}} and {{GeneralSecurityException}}; other 
{{VirtualMachineError}}s propagate. After a failure the native codec stays 
disabled for the life of the JVM.
# {{OpensslCtrCipher}} wraps an {{InternalError}} from {{init}}, {{update}} or 
{{doFinal}} in an {{IOException}}.
# {{hadoop checknative}} reports {{openssl: false}} with the self-test cause.
# When {{getInstance}} returns {{null}} (no configured codec is usable), the 
MapReduce {{CryptoUtils}} and {{DataTransferSaslUtil}} throw a descriptive 
{{IOException}} instead of a {{NullPointerException}}, as {{HdfsKMSUtil}} 
already does.

{{OpensslSm4CtrCryptoCodec}} sets its engine id in {{setConf}}, after 
construction, so SM4 is a follow-up.

h2. Compatibility

Nothing changes where the native cipher works. The fallback can move a FIPS 
host from OpenSSL's FIPS provider to the JDK's provider, which is 
FIPS-compliant only if the JDK is configured for FIPS. Question for reviewers: 
is a switch needed to forbid the fallback, or is the WARN enough?

h2. Out of scope

Crashes ({{SIGSEGV}}) inside libcrypto, for example from a broken default 
ENGINE. Java cannot catch them.

h2. Tests

* Unit tests through a package-private self-test hook: the result is cached, 
the WARN is logged once, the fallback happens, and an OpenSSL-only 
configuration gets no codec. The fallback cases need native code.
* A JCE check of the test vectors, and tests for the null-codec callers. These 
need no native code.
* With {{-Pnative}}: the self-test passes and the OpenSSL codec is still 
selected. Optional: an end-to-end run with the {{base}}-only {{OPENSSL_CONF}}.
* Existing suites keep passing: {{TestCryptoCodec}}, {{TestOpensslCipher}}, 
{{TestCryptoStreamsWithOpensslAesCtrCryptoCodec}}.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to