blob: fd6c22c2b94588f0256ae6f2faf3c64cfef1f642 [file] [log] [blame]
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +08001/*
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
17package android.security;
18
19import android.content.ActivityNotFoundException;
20import android.content.Context;
21import android.content.Intent;
22import android.util.Log;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070023import com.android.org.bouncycastle.openssl.PEMReader;
24import com.android.org.bouncycastle.openssl.PEMWriter;
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.InputStreamReader;
29import java.io.OutputStreamWriter;
30import java.io.Reader;
31import java.io.Writer;
32import java.nio.charset.Charsets;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080033import java.security.KeyPair;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070034import java.util.ArrayList;
35import java.util.List;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080036
37/**
38 * {@hide}
39 */
40public class Credentials {
41 private static final String LOGTAG = "Credentials";
Chia-chi Yeh44039172009-09-21 11:53:59 +080042
Chia-chi Yeh44039172009-09-21 11:53:59 +080043 public static final String INSTALL_ACTION = "android.credentials.INSTALL";
44
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -070045 public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK";
46
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080047 /** 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 PUBLIC_KEY = "KEY";
64
65 /** Data type for private keys. */
66 public static final String PRIVATE_KEY = "PKEY";
67
68 /** Data type for certificates. */
69 public static final String CERTIFICATE = "CERT";
70
71 /** Data type for PKCS12. */
72 public static final String PKCS12 = "PKCS12";
73
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070074 /**
75 * Convert objects to a PEM format, which is used for
76 * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
77 * entries.
78 */
79 public static byte[] convertToPem(Object... objects) throws IOException {
80 ByteArrayOutputStream bao = new ByteArrayOutputStream();
81 Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII);
82 PEMWriter pw = new PEMWriter(writer);
83 for (Object o : objects) {
84 pw.writeObject(o);
85 }
86 pw.close();
87 return bao.toByteArray();
88 }
89 /**
90 * Convert objects from PEM format, which is used for
91 * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
92 * entries.
93 */
94 public static List<Object> convertFromPem(byte[] bytes) throws IOException {
95 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
96 Reader reader = new InputStreamReader(bai, Charsets.US_ASCII);
97 PEMReader pr = new PEMReader(reader);
98
99 List<Object> result = new ArrayList<Object>();
100 Object o;
101 while ((o = pr.readObject()) != null) {
102 result.add(o);
103 }
104 pr.close();
105 return result;
106 }
107
Chia-chi Yeh44039172009-09-21 11:53:59 +0800108 private static Credentials singleton;
109
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800110 public static Credentials getInstance() {
111 if (singleton == null) {
112 singleton = new Credentials();
113 }
114 return singleton;
115 }
116
117 public void unlock(Context context) {
118 try {
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -0700119 Intent intent = new Intent(UNLOCK_ACTION);
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800120 context.startActivity(intent);
121 } catch (ActivityNotFoundException e) {
122 Log.w(LOGTAG, e.toString());
123 }
124 }
125
Hung-ying Tyanc5e630a2010-10-08 08:20:16 +0800126 private Intent createInstallIntent() {
127 Intent intent = new Intent(INSTALL_ACTION);
128 intent.setClassName("com.android.certinstaller",
129 "com.android.certinstaller.CertInstallerMain");
130 return intent;
131 }
132
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800133 public void install(Context context, KeyPair pair) {
134 try {
Hung-ying Tyanc5e630a2010-10-08 08:20:16 +0800135 Intent intent = createInstallIntent();
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800136 intent.putExtra(PRIVATE_KEY, pair.getPrivate().getEncoded());
137 intent.putExtra(PUBLIC_KEY, pair.getPublic().getEncoded());
138 context.startActivity(intent);
139 } catch (ActivityNotFoundException e) {
140 Log.w(LOGTAG, e.toString());
141 }
142 }
143
144 public void install(Context context, String type, byte[] value) {
145 try {
Hung-ying Tyanc5e630a2010-10-08 08:20:16 +0800146 Intent intent = createInstallIntent();
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800147 intent.putExtra(type, value);
148 context.startActivity(intent);
149 } catch (ActivityNotFoundException e) {
150 Log.w(LOGTAG, e.toString());
151 }
152 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800153}