Skip to content

Commit 275fce8

Browse files
author
Selim Gurun
committed
Use private key context when necessary
Bug: 6249185 Due to recent changes to keystore, we cannot rely on encoded key format anymore. Rather we receive the key context (a pointer to private key really) and pass it to native openssl. We also keep the original logic however. Change-Id: Iefe9f0336dd5f47eec4222fcb6fec58807e7cac0
1 parent dd8412d commit 275fce8

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

core/java/android/webkit/BrowserFrame.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import java.util.Set;
5757

5858
import org.apache.harmony.security.provider.cert.X509CertImpl;
59+
import org.apache.harmony.xnet.provider.jsse.OpenSSLDSAPrivateKey;
60+
import org.apache.harmony.xnet.provider.jsse.OpenSSLRSAPrivateKey;
5961

6062
class BrowserFrame extends Handler {
6163

@@ -1104,12 +1106,23 @@ private void requestClientCert(int handle, String hostAndPort) {
11041106
SslClientCertLookupTable table = SslClientCertLookupTable.getInstance();
11051107
if (table.IsAllowed(hostAndPort)) {
11061108
// previously allowed
1107-
nativeSslClientCert(handle,
1108-
table.PrivateKey(hostAndPort),
1109-
table.CertificateChain(hostAndPort));
1109+
PrivateKey pkey = table.PrivateKey(hostAndPort);
1110+
if (pkey instanceof OpenSSLRSAPrivateKey) {
1111+
nativeSslClientCert(handle,
1112+
((OpenSSLRSAPrivateKey)pkey).getPkeyContext(),
1113+
table.CertificateChain(hostAndPort));
1114+
} else if (pkey instanceof OpenSSLDSAPrivateKey) {
1115+
nativeSslClientCert(handle,
1116+
((OpenSSLDSAPrivateKey)pkey).getPkeyContext(),
1117+
table.CertificateChain(hostAndPort));
1118+
} else {
1119+
nativeSslClientCert(handle,
1120+
pkey.getEncoded(),
1121+
table.CertificateChain(hostAndPort));
1122+
}
11101123
} else if (table.IsDenied(hostAndPort)) {
11111124
// previously denied
1112-
nativeSslClientCert(handle, null, null);
1125+
nativeSslClientCert(handle, 0, null);
11131126
} else {
11141127
// previously ignored or new
11151128
mCallbackProxy.onReceivedClientCertRequest(
@@ -1296,7 +1309,11 @@ public void stopLoading() {
12961309
private native void nativeSslCertErrorCancel(int handle, int certError);
12971310

12981311
native void nativeSslClientCert(int handle,
1299-
byte[] pkcs8EncodedPrivateKey,
1312+
int ctx,
1313+
byte[][] asn1DerEncodedCertificateChain);
1314+
1315+
native void nativeSslClientCert(int handle,
1316+
byte[] pkey,
13001317
byte[][] asn1DerEncodedCertificateChain);
13011318

13021319
/**

core/java/android/webkit/ClientCertRequestHandler.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.security.cert.CertificateEncodingException;
2222
import java.security.cert.X509Certificate;
2323
import org.apache.harmony.xnet.provider.jsse.NativeCrypto;
24+
import org.apache.harmony.xnet.provider.jsse.OpenSSLDSAPrivateKey;
25+
import org.apache.harmony.xnet.provider.jsse.OpenSSLRSAPrivateKey;
2426

2527
/**
2628
* ClientCertRequestHandler: class responsible for handling client
@@ -50,33 +52,58 @@ public final class ClientCertRequestHandler extends Handler {
5052
* Proceed with the specified private key and client certificate chain.
5153
*/
5254
public void proceed(PrivateKey privateKey, X509Certificate[] chain) {
53-
final byte[] privateKeyBytes = privateKey.getEncoded();
54-
final byte[][] chainBytes;
5555
try {
56-
chainBytes = NativeCrypto.encodeCertificates(chain);
57-
mTable.Allow(mHostAndPort, privateKeyBytes, chainBytes);
58-
post(new Runnable() {
59-
public void run() {
60-
mBrowserFrame.nativeSslClientCert(mHandle, privateKeyBytes, chainBytes);
61-
}
62-
});
56+
byte[][] chainBytes = NativeCrypto.encodeCertificates(chain);
57+
mTable.Allow(mHostAndPort, privateKey, chainBytes);
58+
59+
if (privateKey instanceof OpenSSLRSAPrivateKey) {
60+
setSslClientCertFromCtx(((OpenSSLRSAPrivateKey)privateKey).getPkeyContext(),
61+
chainBytes);
62+
} else if (privateKey instanceof OpenSSLDSAPrivateKey) {
63+
setSslClientCertFromCtx(((OpenSSLDSAPrivateKey)privateKey).getPkeyContext(),
64+
chainBytes);
65+
} else {
66+
setSslClientCertFromPKCS8(privateKey.getEncoded(),chainBytes);
67+
}
6368
} catch (CertificateEncodingException e) {
6469
post(new Runnable() {
6570
public void run() {
66-
mBrowserFrame.nativeSslClientCert(mHandle, null, null);
71+
mBrowserFrame.nativeSslClientCert(mHandle, 0, null);
6772
return;
6873
}
6974
});
7075
}
7176
}
7277

78+
/**
79+
* Proceed with the specified private key bytes and client certificate chain.
80+
*/
81+
private void setSslClientCertFromCtx(final int ctx, final byte[][] chainBytes) {
82+
post(new Runnable() {
83+
public void run() {
84+
mBrowserFrame.nativeSslClientCert(mHandle, ctx, chainBytes);
85+
}
86+
});
87+
}
88+
89+
/**
90+
* Proceed with the specified private key context and client certificate chain.
91+
*/
92+
private void setSslClientCertFromPKCS8(final byte[] key, final byte[][] chainBytes) {
93+
post(new Runnable() {
94+
public void run() {
95+
mBrowserFrame.nativeSslClientCert(mHandle, key, chainBytes);
96+
}
97+
});
98+
}
99+
73100
/**
74101
* Igore the request for now, the user may be prompted again.
75102
*/
76103
public void ignore() {
77104
post(new Runnable() {
78105
public void run() {
79-
mBrowserFrame.nativeSslClientCert(mHandle, null, null);
106+
mBrowserFrame.nativeSslClientCert(mHandle, 0, null);
80107
}
81108
});
82109
}
@@ -88,7 +115,7 @@ public void cancel() {
88115
mTable.Deny(mHostAndPort);
89116
post(new Runnable() {
90117
public void run() {
91-
mBrowserFrame.nativeSslClientCert(mHandle, null, null);
118+
mBrowserFrame.nativeSslClientCert(mHandle, 0, null);
92119
}
93120
});
94121
}

core/java/android/webkit/SslClientCertLookupTable.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.webkit;
1818

19+
import java.security.PrivateKey;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.Map;
@@ -26,7 +27,7 @@
2627
*/
2728
final class SslClientCertLookupTable {
2829
private static SslClientCertLookupTable sTable;
29-
private final Map<String, byte[]> privateKeys;
30+
private final Map<String, PrivateKey> privateKeys;
3031
private final Map<String, byte[][]> certificateChains;
3132
private final Set<String> denied;
3233

@@ -38,12 +39,12 @@ public static synchronized SslClientCertLookupTable getInstance() {
3839
}
3940

4041
private SslClientCertLookupTable() {
41-
privateKeys = new HashMap<String, byte[]>();
42+
privateKeys = new HashMap<String, PrivateKey>();
4243
certificateChains = new HashMap<String, byte[][]>();
4344
denied = new HashSet<String>();
4445
}
4546

46-
public void Allow(String host_and_port, byte[] privateKey, byte[][] chain) {
47+
public void Allow(String host_and_port, PrivateKey privateKey, byte[][] chain) {
4748
privateKeys.put(host_and_port, privateKey);
4849
certificateChains.put(host_and_port, chain);
4950
denied.remove(host_and_port);
@@ -63,7 +64,7 @@ public boolean IsDenied(String host_and_port) {
6364
return denied.contains(host_and_port);
6465
}
6566

66-
public byte[] PrivateKey(String host_and_port) {
67+
public PrivateKey PrivateKey(String host_and_port) {
6768
return privateKeys.get(host_and_port);
6869
}
6970

0 commit comments

Comments
 (0)