1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.net;
7 import android.util.Log;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace;
12 import java.security.KeyFactory;
13 import java.security.NoSuchAlgorithmException;
14 import java.security.PrivateKey;
15 import java.security.spec.InvalidKeySpecException;
16 import java.security.spec.KeySpec;
17 import java.security.spec.PKCS8EncodedKeySpec;
19 @JNINamespace("net::android")
20 public class AndroidKeyStoreTestUtil {
22 private static final String TAG = "AndroidKeyStoreTestUtil";
24 private static final DefaultAndroidKeyStore sKeyStore = new DefaultAndroidKeyStore();
26 * Called from native code to create a PrivateKey object from its
27 * encoded PKCS#8 representation.
28 * @param type The key type, accoding to PrivateKeyType.
29 * @return new PrivateKey handle, or null in case of error.
32 public static AndroidPrivateKey createPrivateKeyFromPKCS8(int type, byte[] encodedKey) {
33 String algorithm = null;
35 case PrivateKeyType.RSA:
38 case PrivateKeyType.DSA:
41 case PrivateKeyType.ECDSA:
49 KeyFactory factory = KeyFactory.getInstance(algorithm);
50 KeySpec ks = new PKCS8EncodedKeySpec(encodedKey);
51 PrivateKey key = factory.generatePrivate(ks);
52 return sKeyStore.createKey(key);
54 } catch (NoSuchAlgorithmException e) {
55 Log.e(TAG, "Could not create " + algorithm + " factory instance!");
57 } catch (InvalidKeySpecException e) {
58 Log.e(TAG, "Could not load " + algorithm + " private key from bytes!");