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

Commit c8307c88 authored by Brian Carlstrom's avatar Brian Carlstrom Committed by Android (Google) Code Review
Browse files

Merge "Change KeyChain to assume PEM encoded keystore entries"

parents 34687664 9d7faa91
Loading
Loading
Loading
Loading
+46 −1
Original line number Original line Diff line number Diff line
@@ -20,8 +20,19 @@ import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.util.Log;
import android.util.Log;

import com.android.org.bouncycastle.openssl.PEMReader;
import com.android.org.bouncycastle.openssl.PEMWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charsets;
import java.security.KeyPair;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;


/**
/**
 * {@hide}
 * {@hide}
@@ -60,6 +71,40 @@ public class Credentials {
    /** Data type for PKCS12. */
    /** Data type for PKCS12. */
    public static final String PKCS12 = "PKCS12";
    public static final String PKCS12 = "PKCS12";


    /**
     * Convert objects to a PEM format, which is used for
     * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
     * entries.
     */
    public static byte[] convertToPem(Object... objects) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII);
        PEMWriter pw = new PEMWriter(writer);
        for (Object o : objects) {
            pw.writeObject(o);
        }
        pw.close();
        return bao.toByteArray();
    }
    /**
     * Convert objects from PEM format, which is used for
     * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
     * entries.
     */
    public static List<Object> convertFromPem(byte[] bytes) throws IOException {
        ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
        Reader reader = new InputStreamReader(bai, Charsets.US_ASCII);
        PEMReader pr = new PEMReader(reader);

        List<Object> result = new ArrayList<Object>();
        Object o;
        while ((o = pr.readObject()) != null) {
            result.add(o);
        }
        pr.close();
        return result;
    }

    private static Credentials singleton;
    private static Credentials singleton;


    public static Credentials getInstance() {
    public static Credentials getInstance() {
+4 −5
Original line number Original line Diff line number Diff line
@@ -34,6 +34,7 @@ import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.Closeable;
import java.io.IOException;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.Certificate;
@@ -185,11 +186,9 @@ public final class KeyChain {
            throw new IllegalArgumentException("bytes == null");
            throw new IllegalArgumentException("bytes == null");
        }
        }
        try {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            KeyPair keyPair = (KeyPair) Credentials.convertFromPem(bytes).get(0);
            return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
            return keyPair.getPrivate();
        } catch (NoSuchAlgorithmException e) {
        } catch (IOException e) {
            throw new AssertionError(e);
        } catch (InvalidKeySpecException e) {
            throw new AssertionError(e);
            throw new AssertionError(e);
        }
        }
    }
    }