Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.security;
     18 
     19 import android.content.ActivityNotFoundException;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.util.Log;
     23 import com.android.org.bouncycastle.openssl.PEMReader;
     24 import com.android.org.bouncycastle.openssl.PEMWriter;
     25 import java.io.ByteArrayInputStream;
     26 import java.io.ByteArrayOutputStream;
     27 import java.io.IOException;
     28 import java.io.InputStreamReader;
     29 import java.io.OutputStreamWriter;
     30 import java.io.Reader;
     31 import java.io.Writer;
     32 import java.nio.charset.Charsets;
     33 import java.security.KeyPair;
     34 import java.util.ArrayList;
     35 import java.util.List;
     36 
     37 /**
     38  * {@hide}
     39  */
     40 public class Credentials {
     41     private static final String LOGTAG = "Credentials";
     42 
     43     public static final String INSTALL_ACTION = "android.credentials.INSTALL";
     44 
     45     public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK";
     46 
     47     /** Key prefix for CA certificates. */
     48     public static final String CA_CERTIFICATE = "CACERT_";
     49 
     50     /** Key prefix for user certificates. */
     51     public static final String USER_CERTIFICATE = "USRCERT_";
     52 
     53     /** Key prefix for user private keys. */
     54     public static final String USER_PRIVATE_KEY = "USRPKEY_";
     55 
     56     /** Key prefix for VPN. */
     57     public static final String VPN = "VPN_";
     58 
     59     /** Key prefix for WIFI. */
     60     public static final String WIFI = "WIFI_";
     61 
     62     /** Data type for public keys. */
     63     public static final String EXTRA_PUBLIC_KEY = "KEY";
     64 
     65     /** Data type for private keys. */
     66     public static final String EXTRA_PRIVATE_KEY = "PKEY";
     67 
     68     // historically used by Android
     69     public static final String EXTENSION_CRT = ".crt";
     70     public static final String EXTENSION_P12 = ".p12";
     71     // commonly used on Windows
     72     public static final String EXTENSION_CER = ".cer";
     73     public static final String EXTENSION_PFX = ".pfx";
     74 
     75     /**
     76      * Convert objects to a PEM format, which is used for
     77      * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
     78      * entries.
     79      */
     80     public static byte[] convertToPem(Object... objects) throws IOException {
     81         ByteArrayOutputStream bao = new ByteArrayOutputStream();
     82         Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII);
     83         PEMWriter pw = new PEMWriter(writer);
     84         for (Object o : objects) {
     85             pw.writeObject(o);
     86         }
     87         pw.close();
     88         return bao.toByteArray();
     89     }
     90     /**
     91      * Convert objects from PEM format, which is used for
     92      * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
     93      * entries.
     94      */
     95     public static List<Object> convertFromPem(byte[] bytes) throws IOException {
     96         ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
     97         Reader reader = new InputStreamReader(bai, Charsets.US_ASCII);
     98         PEMReader pr = new PEMReader(reader);
     99 
    100         List<Object> result = new ArrayList<Object>();
    101         Object o;
    102         while ((o = pr.readObject()) != null) {
    103             result.add(o);
    104         }
    105         pr.close();
    106         return result;
    107     }
    108 
    109     private static Credentials singleton;
    110 
    111     public static Credentials getInstance() {
    112         if (singleton == null) {
    113             singleton = new Credentials();
    114         }
    115         return singleton;
    116     }
    117 
    118     public void unlock(Context context) {
    119         try {
    120             Intent intent = new Intent(UNLOCK_ACTION);
    121             context.startActivity(intent);
    122         } catch (ActivityNotFoundException e) {
    123             Log.w(LOGTAG, e.toString());
    124         }
    125     }
    126 
    127     public void install(Context context) {
    128         try {
    129             Intent intent = KeyChain.createInstallIntent();
    130             context.startActivity(intent);
    131         } catch (ActivityNotFoundException e) {
    132             Log.w(LOGTAG, e.toString());
    133         }
    134     }
    135 
    136     public void install(Context context, KeyPair pair) {
    137         try {
    138             Intent intent = KeyChain.createInstallIntent();
    139             intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded());
    140             intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded());
    141             context.startActivity(intent);
    142         } catch (ActivityNotFoundException e) {
    143             Log.w(LOGTAG, e.toString());
    144         }
    145     }
    146 
    147     public void install(Context context, String type, byte[] value) {
    148         try {
    149             Intent intent = KeyChain.createInstallIntent();
    150             intent.putExtra(type, value);
    151             context.startActivity(intent);
    152         } catch (ActivityNotFoundException e) {
    153             Log.w(LOGTAG, e.toString());
    154         }
    155     }
    156 }
    157