SecurityException: Failed to read sufficient bytes from /dev/urandom
In our app we are using OkHttp WebSockets from ReactNative. And sometimes okhttp.ws. WebSocketCall.create method crash with "SecurityException: Failed to read sufficient bytes from /dev/urandom. Expected: 1024, actual: 0"
The crash happens only on Android 5.x.x. After some source codes research I found out that in Android 5.x.x other Security Provider - OpenSSL than is used in Android 4.x and Android 6+
OpenSSL uses RAND_load_file function (https://github.com/openssl/openssl/blob/6974fca49d9d0b110c02c83a7bbe01907472ac5e/crypto/rand/randfile.c) to read entropy stored in file /dev/urandom in order to initialize pseudo-random number generator with a truly random seed. Apparently in some cases either file can't be opened for read or only 0 bytes are read.
Unfortunately I can't understand the root cause why only 0 bytes are read or file fails to be opened.
However, I think that in WebSocketCall you should use not a default SecureRandom constructor, but rather instantiate SecureRandom with a more reliable Security Provider like those used in Android 4 or Android 6 (need to derive from SecureRandom and override protected constructor).
The problem reproduces on a range of Android 5.x.x devices. Here's top: 8% Redmi Note 3 8% SM-N9005 6% Lenovo A6020a40 5% LG-H955 5% m2 69% 40 other machines
Here's the complete exception stacktrace:
com.android.org.conscrypt.OpenSSLRandom.seedOpenSSLPRNGFromLinuxRNG in OpenSSLRandom.java::75
com.android.org.conscrypt.OpenSSLRandom.selfSeedIfNotSeeded in OpenSSLRandom.java::61
com.android.org.conscrypt.OpenSSLRandom.engineNextBytes in OpenSSLRandom.java::37
java.security.SecureRandom.nextBytes in SecureRandom.java::271
okhttp3.ws.WebSocketCall.
That’s odd. As specified /dev/urandom is always supposed to provide entropy.