CertTool.java revision 41d8565e816a29192d966f271c06bee91272087c
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 17package android.webkit; 18 19import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 20import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 21import org.bouncycastle.jce.netscape.NetscapeCertRequest; 22import org.bouncycastle.util.encoders.Base64; 23 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; 32 33class CertTool { 34 private static final String LOGTAG = "CertTool"; 35 36 private static final AlgorithmIdentifier MD5_WITH_RSA = 37 new AlgorithmIdentifier(PKCSObjectIdentifiers.md5WithRSAEncryption); 38 39 static final String[] KEY_STRENGTH_LIST = {"High Grade", "Medium Grade"}; 40 41 static final String CERT = Credentials.CERTIFICATE; 42 static final String PKCS12 = Credentials.PKCS12; 43 44 static String getSignedPublicKey(Context context, int index, String challenge) { 45 try { 46 KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); 47 generator.initialize((index == 0) ? 2048 : 1024); 48 KeyPair pair = generator.genKeyPair(); 49 50 NetscapeCertRequest request = new NetscapeCertRequest(challenge, 51 MD5_WITH_RSA, pair.getPublic()); 52 request.sign(pair.getPrivate()); 53 byte[] signed = request.toASN1Object().getDEREncoded(); 54 55 Credentials.getInstance().install(context, pair); 56 return new String(Base64.encode(signed)); 57 } catch (Exception e) { 58 Log.w(LOGTAG, e); 59 } 60 return null; 61 } 62 63 static void addCertificate(Context context, String type, byte[] value) { 64 Credentials.getInstance().install(context, type, value); 65 } 66 67 private CertTool() {} 68} 69