This source file includes following definitions.
- getJavaKey
- getKeyStore
- createKey
- getRSAKeyModulus
- getDSAKeyParamQ
- getECKeyOrder
- getPrivateKeyEncodedBytes
- rawSignDigestWithPrivateKey
- getPrivateKeyType
- getOpenSSLHandleForPrivateKey
- releaseKey
package org.chromium.net;
import android.util.Log;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.interfaces.DSAKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.ECKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.ECParameterSpec;
public class DefaultAndroidKeyStore implements AndroidKeyStore {
private static final String TAG = "AndroidKeyStoreInProcessImpl";
private static class DefaultAndroidPrivateKey implements AndroidPrivateKey {
final PrivateKey mKey;
final DefaultAndroidKeyStore mStore;
DefaultAndroidPrivateKey(PrivateKey key, DefaultAndroidKeyStore store) {
mKey = key;
mStore = store;
}
PrivateKey getJavaKey() {
return mKey;
}
@Override
public AndroidKeyStore getKeyStore() {
return mStore;
}
}
public AndroidPrivateKey createKey(PrivateKey javaKey) {
return new DefaultAndroidPrivateKey(javaKey, this);
}
@Override
public byte[] getRSAKeyModulus(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
if (javaKey instanceof RSAKey) {
return ((RSAKey) javaKey).getModulus().toByteArray();
}
Log.w(TAG, "Not a RSAKey instance!");
return null;
}
@Override
public byte[] getDSAKeyParamQ(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
if (javaKey instanceof DSAKey) {
DSAParams params = ((DSAKey) javaKey).getParams();
return params.getQ().toByteArray();
}
Log.w(TAG, "Not a DSAKey instance!");
return null;
}
@Override
public byte[] getECKeyOrder(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
if (javaKey instanceof ECKey) {
ECParameterSpec params = ((ECKey) javaKey).getParams();
return params.getOrder().toByteArray();
}
Log.w(TAG, "Not an ECKey instance!");
return null;
}
@Override
public byte[] getPrivateKeyEncodedBytes(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
return javaKey.getEncoded();
}
@Override
public byte[] rawSignDigestWithPrivateKey(AndroidPrivateKey key,
byte[] message) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
Signature signature = null;
try {
if (javaKey instanceof RSAPrivateKey) {
signature = Signature.getInstance("NONEwithRSA");
} else if (javaKey instanceof DSAPrivateKey) {
signature = Signature.getInstance("NONEwithDSA");
} else if (javaKey instanceof ECPrivateKey) {
signature = Signature.getInstance("NONEwithECDSA");
}
} catch (NoSuchAlgorithmException e) {
;
}
if (signature == null) {
Log.e(TAG, "Unsupported private key algorithm: " + javaKey.getAlgorithm());
return null;
}
try {
signature.initSign(javaKey);
signature.update(message);
return signature.sign();
} catch (Exception e) {
Log.e(TAG, "Exception while signing message with " + javaKey.getAlgorithm() +
" private key: " + e);
return null;
}
}
@Override
public int getPrivateKeyType(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
if (javaKey instanceof RSAPrivateKey)
return PrivateKeyType.RSA;
if (javaKey instanceof DSAPrivateKey)
return PrivateKeyType.DSA;
if (javaKey instanceof ECPrivateKey)
return PrivateKeyType.ECDSA;
else
return PrivateKeyType.INVALID;
}
@Override
public long getOpenSSLHandleForPrivateKey(AndroidPrivateKey key) {
PrivateKey javaKey = ((DefaultAndroidPrivateKey) key).getJavaKey();
if (javaKey == null) {
Log.e(TAG, "key == null");
return 0;
}
if (!(javaKey instanceof RSAPrivateKey)) {
Log.e(TAG, "does not implement RSAPrivateKey");
return 0;
}
Class<?> superClass;
try {
superClass = Class.forName(
"org.apache.harmony.xnet.provider.jsse.OpenSSLRSAPrivateKey");
} catch (Exception e) {
Log.e(TAG, "Cannot find system OpenSSLRSAPrivateKey class: " + e);
return 0;
}
if (!superClass.isInstance(key)) {
Log.e(TAG, "Private key is not an OpenSSLRSAPrivateKey instance, its class name is:" +
javaKey.getClass().getCanonicalName());
return 0;
}
try {
Method getKey = superClass.getDeclaredMethod("getOpenSSLKey");
getKey.setAccessible(true);
Object opensslKey = null;
try {
opensslKey = getKey.invoke(javaKey);
} finally {
getKey.setAccessible(false);
}
if (opensslKey == null) {
Log.e(TAG, "getOpenSSLKey() returned null");
return 0;
}
Method getPkeyContext;
try {
getPkeyContext = opensslKey.getClass().getDeclaredMethod("getPkeyContext");
} catch (Exception e) {
Log.e(TAG, "No getPkeyContext() method on OpenSSLKey member:" + e);
return 0;
}
getPkeyContext.setAccessible(true);
long evp_pkey = 0;
try {
evp_pkey = (Long) getPkeyContext.invoke(opensslKey);
} finally {
getPkeyContext.setAccessible(false);
}
if (evp_pkey == 0) {
Log.e(TAG, "getPkeyContext() returned null");
}
return evp_pkey;
} catch (Exception e) {
Log.e(TAG, "Exception while trying to retrieve system EVP_PKEY handle: " + e);
return 0;
}
}
@Override
public void releaseKey(AndroidPrivateKey key) {
}
}