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