Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 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.webkit; |
| 18 | |
Brian Carlstrom | 4140fae | 2011-01-24 16:17:43 -0800 | [diff] [blame] | 19 | import com.android.org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 20 | import com.android.org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 21 | import com.android.org.bouncycastle.jce.netscape.NetscapeCertRequest; |
| 22 | import com.android.org.bouncycastle.util.encoders.Base64; |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 23 | |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 24 | import android.content.Context; |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 25 | import android.security.Credentials; |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 26 | import android.security.KeyChain; |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
| 29 | import java.security.KeyPair; |
| 30 | import java.security.KeyPairGenerator; |
Huahui Wu | c7939b1 | 2011-01-26 22:11:02 -0800 | [diff] [blame] | 31 | import java.util.HashMap; |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 32 | |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 33 | final class CertTool { |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 34 | private static final String LOGTAG = "CertTool"; |
| 35 | |
| 36 | private static final AlgorithmIdentifier MD5_WITH_RSA = |
| 37 | new AlgorithmIdentifier(PKCSObjectIdentifiers.md5WithRSAEncryption); |
| 38 | |
Huahui Wu | c7939b1 | 2011-01-26 22:11:02 -0800 | [diff] [blame] | 39 | private static HashMap<String, String> sCertificateTypeMap; |
| 40 | static { |
| 41 | sCertificateTypeMap = new HashMap<String, String>(); |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 42 | sCertificateTypeMap.put("application/x-x509-ca-cert", KeyChain.EXTRA_CERTIFICATE); |
| 43 | sCertificateTypeMap.put("application/x-x509-user-cert", KeyChain.EXTRA_CERTIFICATE); |
| 44 | sCertificateTypeMap.put("application/x-pkcs12", KeyChain.EXTRA_PKCS12); |
Huahui Wu | c7939b1 | 2011-01-26 22:11:02 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Chia-chi Yeh | c633253 | 2009-09-18 13:55:43 +0800 | [diff] [blame] | 47 | static String[] getKeyStrengthList() { |
| 48 | return new String[] {"High Grade", "Medium Grade"}; |
| 49 | } |
| 50 | |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 51 | static String getSignedPublicKey(Context context, int index, String challenge) { |
| 52 | try { |
| 53 | KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); |
| 54 | generator.initialize((index == 0) ? 2048 : 1024); |
| 55 | KeyPair pair = generator.genKeyPair(); |
| 56 | |
| 57 | NetscapeCertRequest request = new NetscapeCertRequest(challenge, |
| 58 | MD5_WITH_RSA, pair.getPublic()); |
| 59 | request.sign(pair.getPrivate()); |
| 60 | byte[] signed = request.toASN1Object().getDEREncoded(); |
| 61 | |
| 62 | Credentials.getInstance().install(context, pair); |
| 63 | return new String(Base64.encode(signed)); |
| 64 | } catch (Exception e) { |
| 65 | Log.w(LOGTAG, e); |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | static void addCertificate(Context context, String type, byte[] value) { |
| 71 | Credentials.getInstance().install(context, type, value); |
| 72 | } |
| 73 | |
Huahui Wu | c7939b1 | 2011-01-26 22:11:02 -0800 | [diff] [blame] | 74 | static String getCertType(String mimeType) { |
| 75 | return sCertificateTypeMap.get(mimeType); |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 76 | } |
Huahui Wu | c7939b1 | 2011-01-26 22:11:02 -0800 | [diff] [blame] | 77 | |
Chia-chi Yeh | 41d8565 | 2009-09-18 12:11:20 +0800 | [diff] [blame] | 78 | private CertTool() {} |
| 79 | } |