Get rid of AlgNameMapper

This was only a hack to support old Harmony code, so we don't need it
anymore. Remove the direct references to AlgNameMapper and use
reflection for compatibility in unbundled code.

Change-Id: I7ec14f19e5098ffe12592b79b2b163b41031b6e6
diff --git a/Android.mk b/Android.mk
index a6b1058..d189a99 100644
--- a/Android.mk
+++ b/Android.mk
@@ -340,11 +340,7 @@
 
 # Conscrypt Java library for host OpenJDK
 include $(CLEAR_VARS)
-local_exclude_src_files := \
-	%/AlgNameMapper.java \
-	%/AlgNameMapperSource.java
 LOCAL_SRC_FILES := $(call all-java-files-under,src/main/java)
-LOCAL_SRC_FILES := $(filter-out $(local_exclude_src_files), $(LOCAL_SRC_FILES))
 LOCAL_SRC_FILES += $(call all-java-files-under,src/openjdk/java)
 LOCAL_SRC_FILES += $(call all-java-files-under,src/openjdk-host/java)
 LOCAL_GENERATED_SOURCES := $(conscrypt_gen_java_files)
@@ -357,5 +353,4 @@
 # clear out local variables
 core_cflags :=
 core_cppflags :=
-local_exclude_src_files :=
 local_javac_flags :=
diff --git a/src/compat/java/org/conscrypt/Platform.java b/src/compat/java/org/conscrypt/Platform.java
index 7df17f7..e9ab604 100644
--- a/src/compat/java/org/conscrypt/Platform.java
+++ b/src/compat/java/org/conscrypt/Platform.java
@@ -413,4 +413,51 @@
     public static void blockGuardOnNetwork() {
         BlockGuard.getThreadPolicy().onNetwork();
     }
+
+    /**
+     * OID to Algorithm Name mapping.
+     */
+    public static String oidToAlgorithmName(String oid) {
+        // Old Harmony style
+        try {
+            Class<?> algNameMapperClass = Class.forName(
+                        "org.apache.harmony.security.utils.AlgNameMapper");
+            Method map2AlgNameMethod = algNameMapperClass.getDeclaredMethod("map2AlgName",
+                        String.class);
+            map2AlgNameMethod.setAccessible(true);
+            return (String) map2AlgNameMethod.invoke(null, oid);
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof RuntimeException) {
+                throw (RuntimeException) cause;
+            } else if (cause instanceof Error) {
+                throw (Error) cause;
+            }
+            throw new RuntimeException(e);
+        } catch (Exception ignored) {
+        }
+
+        // Newer OpenJDK style
+        try {
+            Class<?> algorithmIdClass = Class.forName("sun.security.x509.AlgorithmId");
+            Method getMethod = algorithmIdClass.getDeclaredMethod("get", String.class);
+            getMethod.setAccessible(true);
+            Method getNameMethod = algorithmIdClass.getDeclaredMethod("getName");
+            getNameMethod.setAccessible(true);
+
+            Object algIdObj = getMethod.invoke(null, oid);
+            return (String) getNameMethod.invoke(algIdObj);
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof RuntimeException) {
+                throw (RuntimeException) cause;
+            } else if (cause instanceof Error) {
+                throw (Error) cause;
+            }
+            throw new RuntimeException(e);
+        } catch (Exception ignored) {
+        }
+
+        return oid;
+    }
 }
diff --git a/src/main/java/org/apache/harmony/security/utils/AlgNameMapper.java b/src/main/java/org/apache/harmony/security/utils/AlgNameMapper.java
deleted file mode 100644
index 054ed87..0000000
--- a/src/main/java/org/apache/harmony/security/utils/AlgNameMapper.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.harmony.security.utils;
-
-import org.conscrypt.NativeCrypto;
-
-public class AlgNameMapper {
-    private AlgNameMapper() {
-    }
-
-    public static String map2AlgName(String oid) {
-        return NativeCrypto.OBJ_txt2nid_longName(oid);
-    }
-
-    public static void setSource(Object o) {
-    }
-}
diff --git a/src/main/java/org/apache/harmony/security/utils/AlgNameMapperSource.java b/src/main/java/org/apache/harmony/security/utils/AlgNameMapperSource.java
deleted file mode 100644
index abca36c..0000000
--- a/src/main/java/org/apache/harmony/security/utils/AlgNameMapperSource.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.harmony.security.utils;
-
-/**
- * Provides a mapping source that the {@link AlgNameMapper} can query for
- * mapping between algorithm names and OIDs.
- */
-public interface AlgNameMapperSource {
-    public String mapNameToOid(String algName);
-
-    public String mapOidToName(String oid);
-}
diff --git a/src/main/java/org/conscrypt/OpenSSLX509CRL.java b/src/main/java/org/conscrypt/OpenSSLX509CRL.java
index a063536..d5bc6bc 100644
--- a/src/main/java/org/conscrypt/OpenSSLX509CRL.java
+++ b/src/main/java/org/conscrypt/OpenSSLX509CRL.java
@@ -16,7 +16,6 @@
 
 package org.conscrypt;
 
-import org.apache.harmony.security.utils.AlgNameMapper;
 import org.conscrypt.OpenSSLX509CertificateFactory.ParsingException;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -328,7 +327,12 @@
 
     @Override
     public String getSigAlgName() {
-        return AlgNameMapper.map2AlgName(getSigAlgOID());
+        String oid = getSigAlgOID();
+        String algName = Platform.oidToAlgorithmName(oid);
+        if (algName != null) {
+            return algName;
+        }
+        return oid;
     }
 
     @Override
diff --git a/src/main/java/org/conscrypt/OpenSSLX509Certificate.java b/src/main/java/org/conscrypt/OpenSSLX509Certificate.java
index e06ca7e..66ddce8 100644
--- a/src/main/java/org/conscrypt/OpenSSLX509Certificate.java
+++ b/src/main/java/org/conscrypt/OpenSSLX509Certificate.java
@@ -48,7 +48,6 @@
 import java.util.TimeZone;
 import javax.crypto.BadPaddingException;
 import javax.security.auth.x500.X500Principal;
-import org.apache.harmony.security.utils.AlgNameMapper;
 import org.conscrypt.OpenSSLX509CertificateFactory.ParsingException;
 
 public class OpenSSLX509Certificate extends X509Certificate {
@@ -285,7 +284,7 @@
     @Override
     public String getSigAlgName() {
         String oid = getSigAlgOID();
-        String algName = AlgNameMapper.map2AlgName(oid);
+        String algName = Platform.oidToAlgorithmName(oid);
         if (algName != null) {
             return algName;
         }
diff --git a/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapper.java b/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapper.java
deleted file mode 100644
index 6ab1e72..0000000
--- a/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapper.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.harmony.security.utils;
-
-import java.security.NoSuchAlgorithmException;
-import org.conscrypt.NativeCrypto;
-import sun.security.x509.AlgorithmId;
-
-public class AlgNameMapper {
-    private AlgNameMapper() {
-    }
-
-    public static String map2AlgName(String oid) {
-        try {
-            // This gives us the common name in the Java language.
-            AlgorithmId algId = AlgorithmId.get(oid);
-            if (algId != null) {
-                return algId.getName();
-            }
-        } catch (NoSuchAlgorithmException ignored) {
-        }
-
-        // Otherwise fall back to OpenSSL or BoringSSL's name for it.
-        return NativeCrypto.OBJ_txt2nid_longName(oid);
-    }
-
-    public static void setSource(Object o) {
-    }
-}
diff --git a/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapperSource.java b/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapperSource.java
deleted file mode 100644
index abca36c..0000000
--- a/src/openjdk/java/org/apache/harmony/security/utils/AlgNameMapperSource.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.harmony.security.utils;
-
-/**
- * Provides a mapping source that the {@link AlgNameMapper} can query for
- * mapping between algorithm names and OIDs.
- */
-public interface AlgNameMapperSource {
-    public String mapNameToOid(String algName);
-
-    public String mapOidToName(String oid);
-}
diff --git a/src/openjdk/java/org/conscrypt/Platform.java b/src/openjdk/java/org/conscrypt/Platform.java
index 140ca46..9dc65fa 100644
--- a/src/openjdk/java/org/conscrypt/Platform.java
+++ b/src/openjdk/java/org/conscrypt/Platform.java
@@ -16,7 +16,6 @@
 
 package org.conscrypt;
 
-import org.conscrypt.GCMParameters;
 import java.io.FileDescriptor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -24,17 +23,18 @@
 import java.net.Socket;
 import java.net.SocketException;
 import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
 import java.security.PrivateKey;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.security.spec.AlgorithmParameterSpec;
 import java.security.spec.ECParameterSpec;
 import javax.crypto.spec.GCMParameterSpec;
-
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.X509TrustManager;
+import sun.security.x509.AlgorithmId;
 
 /**
  *
@@ -199,4 +199,15 @@
 
     public static void blockGuardOnNetwork() {
     }
+
+    /**
+     * OID to Algorithm Name mapping.
+     */
+    public static String oidToAlgorithmName(String oid) {
+        try {
+            return AlgorithmId.get(oid).getName();
+        } catch (NoSuchAlgorithmException e) {
+            return oid;
+        }
+    }
 }
diff --git a/src/platform/java/org/conscrypt/Platform.java b/src/platform/java/org/conscrypt/Platform.java
index fde9acb..805c559 100644
--- a/src/platform/java/org/conscrypt/Platform.java
+++ b/src/platform/java/org/conscrypt/Platform.java
@@ -32,6 +32,7 @@
 import java.net.Socket;
 import java.net.SocketException;
 import java.net.SocketImpl;
+import java.security.NoSuchAlgorithmException;
 import java.security.PrivateKey;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
@@ -42,9 +43,8 @@
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.X509TrustManager;
-import org.apache.harmony.security.utils.AlgNameMapper;
-import org.apache.harmony.security.utils.AlgNameMapperSource;
 import org.conscrypt.GCMParameters;
+import sun.security.x509.AlgorithmId;
 
 class Platform {
     private static class NoPreloadHolder {
@@ -65,19 +65,6 @@
     }
 
     private Platform() {
-        AlgNameMapper.setSource(new OpenSSLMapper());
-    }
-
-    private static class OpenSSLMapper implements AlgNameMapperSource {
-        @Override
-        public String mapNameToOid(String algName) {
-            return NativeCrypto.OBJ_txt2nid_oid(algName);
-        }
-
-        @Override
-        public String mapOidToName(String oid) {
-            return NativeCrypto.OBJ_txt2nid_longName(oid);
-        }
     }
 
     public static FileDescriptor getFileDescriptor(Socket s) {
@@ -230,4 +217,15 @@
     public static void blockGuardOnNetwork() {
         BlockGuard.getThreadPolicy().onNetwork();
     }
+
+    /**
+     * OID to Algorithm Name mapping.
+     */
+    public static String oidToAlgorithmName(String oid) {
+        try {
+            return AlgorithmId.get(oid).getName();
+        } catch (NoSuchAlgorithmException e) {
+            return oid;
+        }
+    }
 }