NativeCrypto: adjust thrown exceptions
Should be throwing CertificateException when InputStream is null.
Sometimes OpenSSL doesn't push an error onto the list when PEM encoding
fails. This can be seen with the call to PEM_read_bio_X509 with
hyts_badpem.cer Throw a generic RuntimeException instead.
(cherry picked from commit d14eedd3c70f67a0d7af71b56dcf7b8e4f030bdd)
Bug: 8488314
Change-Id: I716c089c00ab477b4803bdd774681e52384eb95d
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertPath.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertPath.java
index 9c9d6fb..a96c361 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertPath.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertPath.java
@@ -230,7 +230,7 @@
public static CertPath fromEncoding(InputStream inStream, String encoding)
throws CertificateException {
if (inStream == null) {
- throw new NullPointerException("inStream == null");
+ throw new CertificateException("inStream == null");
}
Encoding enc = Encoding.findByApiName(encoding);
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertificateFactory.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertificateFactory.java
index ae33ec7..ca79785 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertificateFactory.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLX509CertificateFactory.java
@@ -117,7 +117,7 @@
public Collection<? extends T> generateItems(InputStream inStream)
throws ParsingException {
if (inStream == null) {
- throw new NullPointerException("inStream == null");
+ throw new ParsingException("inStream == null");
}
try {
if (inStream.available() == 0) {
diff --git a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp
index d9b7675..3075197 100644
--- a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp
+++ b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp
@@ -4702,6 +4702,10 @@
T* x = PEM_read_func(bio, NULL, NULL, NULL);
if (x == NULL) {
throwExceptionIfNecessary(env, "PEM_ASN1Object_to_jlong");
+ // Sometimes the PEM functions fail without pushing an error
+ if (!env->ExceptionCheck()) {
+ jniThrowRuntimeException(env, "Failure parsing PEM");
+ }
JNI_TRACE("PEM_ASN1Object_to_jlong(%p) => threw exception", bio);
return 0;
}