Skip to content
This repository was archived by the owner on Jan 25, 2018. It is now read-only.

Commit b4db546

Browse files
committed
first commit of chatsecureandroid gradle migration
0 parents  commit b4db546

File tree

898 files changed

+95422
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

898 files changed

+95422
-0
lines changed

androidPinning/build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion "24.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 8
9+
targetSdkVersion 17
10+
}
11+
12+
buildTypes {
13+
release {
14+
minifyEnabled false
15+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
16+
}
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.thoughtcrime.ssl.pinning"
4+
android:versionCode="1"
5+
android:versionName="1.0.0">
6+
7+
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
8+
9+
</manifest>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright (C) 2011-2013 Moxie Marlinspike
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.thoughtcrime.ssl.pinning;
19+
20+
import java.security.GeneralSecurityException;
21+
import java.security.cert.CertificateException;
22+
import java.security.cert.X509Certificate;
23+
import java.util.LinkedList;
24+
25+
/**
26+
* Does the work of cleaning up a certificate chain by sifting out any
27+
* unrelated certificates and returning something that's signed from
28+
* EE to a trust anchor.
29+
*
30+
* @author Moxie Marlinspike
31+
*/
32+
class CertificateChainCleaner {
33+
34+
private CertificateChainCleaner() {}
35+
36+
public static X509Certificate[] getCleanChain(X509Certificate[] chain,
37+
SystemKeyStore systemKeyStore)
38+
throws CertificateException
39+
{
40+
final LinkedList<X509Certificate> cleanChain = new LinkedList<X509Certificate>();
41+
boolean trustedChain = false;
42+
int i;
43+
44+
if (systemKeyStore.isTrustRoot(chain[0])) {
45+
trustedChain = true;
46+
}
47+
48+
cleanChain.add(chain[0]);
49+
50+
for (i = 1; i < chain.length; i++) {
51+
if (systemKeyStore.isTrustRoot(chain[i])) {
52+
trustedChain = true;
53+
}
54+
55+
if (isValidLink(chain[i], chain[i - 1])) {
56+
cleanChain.add(chain[i]);
57+
} else {
58+
break;
59+
}
60+
}
61+
62+
final X509Certificate trustRoot = systemKeyStore.getTrustRootFor(chain[i - 1]);
63+
64+
if (trustRoot != null) {
65+
cleanChain.add(trustRoot);
66+
trustedChain = true;
67+
}
68+
69+
if (trustedChain) {
70+
return cleanChain.toArray(new X509Certificate[cleanChain.size()]);
71+
} else {
72+
throw new CertificateException("Didn't find a trust anchor in chain cleanup!");
73+
}
74+
}
75+
76+
private static boolean isValidLink(X509Certificate parent, X509Certificate child) {
77+
if (!parent.getSubjectX500Principal().equals(child.getIssuerX500Principal())) {
78+
return false;
79+
}
80+
81+
try {
82+
child.verify(parent.getPublicKey());
83+
} catch (GeneralSecurityException gse) {
84+
return false;
85+
}
86+
87+
return true;
88+
}
89+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright (C) 2011-2013 Moxie Marlinspike
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.thoughtcrime.ssl.pinning;
19+
20+
import android.content.Context;
21+
22+
import org.apache.http.conn.ssl.SSLSocketFactory;
23+
import org.apache.http.conn.ssl.X509HostnameVerifier;
24+
import org.apache.http.params.HttpConnectionParams;
25+
import org.apache.http.params.HttpParams;
26+
27+
import javax.net.ssl.SSLContext;
28+
import javax.net.ssl.SSLSocket;
29+
import javax.net.ssl.TrustManager;
30+
import java.io.IOException;
31+
import java.net.InetAddress;
32+
import java.net.InetSocketAddress;
33+
import java.net.Socket;
34+
import java.security.KeyManagementException;
35+
import java.security.KeyStoreException;
36+
import java.security.NoSuchAlgorithmException;
37+
import java.security.UnrecoverableKeyException;
38+
39+
/**
40+
* A standard Apache SSL Socket Factory that uses an pinning trust manager.
41+
* <p>
42+
* To use:
43+
* <pre>
44+
*
45+
* String[] pins = new String[] {"40c5401d6f8cbaf08b00edefb1ee87d005b3b9cd"};
46+
* SchemeRegistry schemeRegistry = new SchemeRegistry();
47+
* schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
48+
* schemeRegistry.register(new Scheme("https", new PinningSSLSocketFactory(getContext(),pins, 0), 443));
49+
*
50+
* HttpParams httpParams = new BasicHttpParams();
51+
* ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
52+
* DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpParams);
53+
*
54+
* HttpResponse response = httpClient.execute(new HttpGet("https://www.google.com/"));
55+
*
56+
* </pre>
57+
* </p>
58+
*
59+
* @author Moxie Marlinspike
60+
*/
61+
public class PinningSSLSocketFactory extends SSLSocketFactory {
62+
63+
private final javax.net.ssl.SSLSocketFactory pinningSocketFactory;
64+
65+
/**
66+
* Constructs a PinningSSLSocketFactory with a set of valid pins.
67+
*
68+
* @param pins An array of encoded pins to match a seen certificate
69+
* chain against. A pin is a hex-encoded hash of a X.509 certificate's
70+
* SubjectPublicKeyInfo. A pin can be generated using the provided pin.py
71+
* script: python ./tools/pin.py certificate_file.pem
72+
*
73+
* @param enforceUntilTimestampMillis A timestamp (in milliseconds) when pins will stop being
74+
* enforced. Normal non-pinned certificate validation
75+
* will continue. Set this to some period after your build
76+
* date, or to 0 to enforce pins forever.
77+
*/
78+
79+
public PinningSSLSocketFactory(Context context, String[] pins, long enforceUntilTimestampMillis)
80+
throws UnrecoverableKeyException, KeyManagementException,
81+
NoSuchAlgorithmException, KeyStoreException
82+
{
83+
super(null);
84+
85+
final SystemKeyStore keyStore = SystemKeyStore.getInstance(context);
86+
final SSLContext pinningSslContext = SSLContext.getInstance(TLS);
87+
final TrustManager[] pinningTrustManagers = initializePinningTrustManagers(keyStore, pins, enforceUntilTimestampMillis);
88+
89+
pinningSslContext.init(null, pinningTrustManagers, null);
90+
this.pinningSocketFactory = pinningSslContext.getSocketFactory();
91+
}
92+
93+
@Override
94+
public Socket createSocket() throws IOException {
95+
return pinningSocketFactory.createSocket();
96+
}
97+
98+
@Override
99+
public Socket connectSocket(final Socket sock, final String host, final int port,
100+
final InetAddress localAddress, int localPort,
101+
final HttpParams params) throws IOException {
102+
final SSLSocket sslSock = (SSLSocket) ((sock != null) ? sock : createSocket());
103+
104+
if ((localAddress != null) || (localPort > 0)) {
105+
if (localPort < 0) {
106+
localPort = 0;
107+
}
108+
109+
sslSock.bind(new InetSocketAddress(localAddress, localPort));
110+
}
111+
112+
final int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
113+
final int soTimeout = HttpConnectionParams.getSoTimeout(params);
114+
115+
final InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
116+
sslSock.connect(remoteAddress, connTimeout);
117+
sslSock.setSoTimeout(soTimeout);
118+
119+
try {
120+
SSLSocketFactory.STRICT_HOSTNAME_VERIFIER.verify(host, sslSock);
121+
} catch (IOException iox) {
122+
try {
123+
sslSock.close();
124+
} catch (Exception ignored) {
125+
}
126+
throw iox;
127+
}
128+
129+
return sslSock;
130+
}
131+
132+
@Override
133+
public Socket createSocket(final Socket socket, final String host,
134+
int port, final boolean autoClose)
135+
throws IOException
136+
{
137+
if (port == -1) {
138+
port = 443;
139+
}
140+
141+
final SSLSocket sslSocket = (SSLSocket) pinningSocketFactory.createSocket(socket, host, port, autoClose);
142+
SSLSocketFactory.STRICT_HOSTNAME_VERIFIER.verify(host, sslSocket);
143+
return sslSocket;
144+
}
145+
146+
@Override
147+
public void setHostnameVerifier(X509HostnameVerifier hostnameVerifier) {
148+
throw new IllegalArgumentException("Only strict hostname verification (default) " +
149+
"is supported!");
150+
}
151+
152+
@Override
153+
public X509HostnameVerifier getHostnameVerifier() {
154+
return SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
155+
}
156+
157+
private TrustManager[] initializePinningTrustManagers(SystemKeyStore keyStore,
158+
String[] pins,
159+
long enforceUntilTimestampMillis)
160+
{
161+
final TrustManager[] trustManagers = new TrustManager[1];
162+
trustManagers[0] = new PinningTrustManager(keyStore, pins, enforceUntilTimestampMillis);
163+
164+
return trustManagers;
165+
}
166+
}

0 commit comments

Comments
 (0)