blob: 4c534f93cd26a6c2bc4839cd530f27248e79d6fe [file] [log] [blame]
Chia-chi Yeh41d85652009-09-18 12:11:20 +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.webkit;
18
Brian Carlstrom4140fae2011-01-24 16:17:43 -080019import com.android.org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
20import com.android.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
21import com.android.org.bouncycastle.jce.netscape.NetscapeCertRequest;
22import com.android.org.bouncycastle.util.encoders.Base64;
Chia-chi Yeh41d85652009-09-18 12:11:20 +080023
24import android.content.ActivityNotFoundException;
25import android.content.Context;
26import android.content.Intent;
27import android.security.Credentials;
28import android.util.Log;
29
30import java.security.KeyPair;
31import java.security.KeyPairGenerator;
Huahui Wuc7939b12011-01-26 22:11:02 -080032import java.util.HashMap;
Chia-chi Yeh41d85652009-09-18 12:11:20 +080033
34class CertTool {
35 private static final String LOGTAG = "CertTool";
36
37 private static final AlgorithmIdentifier MD5_WITH_RSA =
38 new AlgorithmIdentifier(PKCSObjectIdentifiers.md5WithRSAEncryption);
39
Chia-chi Yeh41d85652009-09-18 12:11:20 +080040 static final String CERT = Credentials.CERTIFICATE;
41 static final String PKCS12 = Credentials.PKCS12;
42
Huahui Wuc7939b12011-01-26 22:11:02 -080043 private static HashMap<String, String> sCertificateTypeMap;
44 static {
45 sCertificateTypeMap = new HashMap<String, String>();
46 sCertificateTypeMap.put("application/x-x509-ca-cert", CertTool.CERT);
47 sCertificateTypeMap.put("application/x-x509-user-cert", CertTool.CERT);
48 sCertificateTypeMap.put("application/x-pkcs12", CertTool.PKCS12);
49 }
50
Chia-chi Yehc6332532009-09-18 13:55:43 +080051 static String[] getKeyStrengthList() {
52 return new String[] {"High Grade", "Medium Grade"};
53 }
54
Chia-chi Yeh41d85652009-09-18 12:11:20 +080055 static String getSignedPublicKey(Context context, int index, String challenge) {
56 try {
57 KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
58 generator.initialize((index == 0) ? 2048 : 1024);
59 KeyPair pair = generator.genKeyPair();
60
61 NetscapeCertRequest request = new NetscapeCertRequest(challenge,
62 MD5_WITH_RSA, pair.getPublic());
63 request.sign(pair.getPrivate());
64 byte[] signed = request.toASN1Object().getDEREncoded();
65
66 Credentials.getInstance().install(context, pair);
67 return new String(Base64.encode(signed));
68 } catch (Exception e) {
69 Log.w(LOGTAG, e);
70 }
71 return null;
72 }
73
74 static void addCertificate(Context context, String type, byte[] value) {
75 Credentials.getInstance().install(context, type, value);
76 }
77
Huahui Wuc7939b12011-01-26 22:11:02 -080078 static String getCertType(String mimeType) {
79 return sCertificateTypeMap.get(mimeType);
80 }
81
Chia-chi Yeh41d85652009-09-18 12:11:20 +080082 private CertTool() {}
83}