Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 96bfead5 authored by Michael Groover's avatar Michael Groover Committed by Automerger Merge Worker
Browse files

Merge "Add support for -providerArg in signapk" am: 9d1a0a47 am: 9401efe6

parents 67e5e7f1 9401efe6
Loading
Loading
Loading
Loading
+42 −20
Original line number Diff line number Diff line
@@ -901,7 +901,7 @@ class SignApk {
     * Tries to load a JSE Provider by class name. This is for custom PrivateKey
     * types that might be stored in PKCS#11-like storage.
     */
    private static void loadProviderIfNecessary(String providerClassName) {
    private static void loadProviderIfNecessary(String providerClassName, String providerArg) {
        if (providerClassName == null) {
            return;
        }
@@ -920,27 +920,41 @@ class SignApk {
            return;
        }

        Constructor<?> constructor = null;
        for (Constructor<?> c : klass.getConstructors()) {
            if (c.getParameterTypes().length == 0) {
                constructor = c;
                break;
            }
        }
        if (constructor == null) {
            System.err.println("No zero-arg constructor found for " + providerClassName);
        Constructor<?> constructor;
        Object o = null;
        if (providerArg == null) {
            try {
                constructor = klass.getConstructor();
                o = constructor.newInstance();
            } catch (ReflectiveOperationException e) {
                e.printStackTrace();
                System.err.println("Unable to instantiate " + providerClassName
                        + " with a zero-arg constructor");
                System.exit(1);
            return;
            }

        final Object o;
        } else {
            try {
                constructor = klass.getConstructor(String.class);
                o = constructor.newInstance(providerArg);
            } catch (ReflectiveOperationException e) {
                // This is expected from JDK 9+; the single-arg constructor accepting the
                // configuration has been replaced with a configure(String) method to be invoked
                // after instantiating the Provider with the zero-arg constructor.
                try {
                    constructor = klass.getConstructor();
                    o = constructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
                    // The configure method will return either the modified Provider or a new
                    // Provider if this one cannot be configured in-place.
                    o = klass.getMethod("configure", String.class).invoke(o, providerArg);
                } catch (ReflectiveOperationException roe) {
                    roe.printStackTrace();
                    System.err.println("Unable to instantiate " + providerClassName
                            + " with the provided argument " + providerArg);
                    System.exit(1);
            return;
                }
            }
        }

        if (!(o instanceof Provider)) {
            System.err.println("Not a Provider class: " + providerClassName);
            System.exit(1);
@@ -1049,6 +1063,7 @@ class SignApk {
                           "[-a <alignment>] " +
                           "[--align-file-size] " +
                           "[-providerClass <className>] " +
                           "[-providerArg <configureArg>] " +
                           "[-loadPrivateKeysFromKeyStore <keyStoreName>]" +
                           "[-keyStorePin <pin>]" +
                           "[--min-sdk-version <n>] " +
@@ -1073,6 +1088,7 @@ class SignApk {

        boolean signWholeFile = false;
        String providerClass = null;
        String providerArg = null;
        String keyStoreName = null;
        String keyStorePin = null;
        int alignment = 4;
@@ -1094,6 +1110,12 @@ class SignApk {
                }
                providerClass = args[++argstart];
                ++argstart;
            } else if("-providerArg".equals(args[argstart])) {
                if (argstart + 1 >= args.length) {
                    usage();
                }
                providerArg = args[++argstart];
                ++argstart;
            } else if ("-loadPrivateKeysFromKeyStore".equals(args[argstart])) {
                if (argstart + 1 >= args.length) {
                    usage();
@@ -1163,7 +1185,7 @@ class SignApk {
            System.exit(2);
        }

        loadProviderIfNecessary(providerClass);
        loadProviderIfNecessary(providerClass, providerArg);

        String inputFilename = args[numArgsExcludeV4FilePath - 2];
        String outputFilename = args[numArgsExcludeV4FilePath - 1];