13
13
import org .jetbrains .annotations .NotNull ;
14
14
15
15
import java .io .IOException ;
16
+ import java .util .ArrayList ;
17
+ import java .util .List ;
16
18
import java .util .Objects ;
17
19
18
20
import infosecadventures .allsafe .R ;
27
29
28
30
public class CertificatePinning extends Fragment {
29
31
32
+ private static final String INVALID_HASH = "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" ;
33
+
30
34
@ Override
31
35
public View onCreateView (@ NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
32
36
View view = inflater .inflate (R .layout .fragment_certificate_pinning , container , false );
33
37
setHasOptionsMenu (true );
38
+
39
+ // make an intentional request with broken config
40
+ // to get the actual peer certificate chain public key hashes from okhttp exception
41
+ List <String > hashes = extractPeerCertificateChain ();
42
+
34
43
Button test = view .findViewById (R .id .execute );
35
44
test .setOnClickListener (v -> {
36
- CertificatePinner certificatePinner = new CertificatePinner . Builder ()
37
- . add ( "httpbin.org" ,
38
- "sha256/J0dKy1gw45muM4o/vm/tskFQ2BWudtp9XLxaW7OtowQ=" )
39
- . add ( "httpbin.org" ,
40
- "sha256/JSMzqOOrtyOT1kmau6zKhgT676hGgczD5VMdRMyJZFA=" )
41
- . build ();
45
+
46
+ CertificatePinner . Builder certificatePinner = new CertificatePinner . Builder ();
47
+ for ( String hash : hashes ) {
48
+ Log . d ( "ALLSAFE" , hash );
49
+ certificatePinner . add ( "httpbing.org" , hash );
50
+ }
42
51
43
52
OkHttpClient okHttpClient = new OkHttpClient .Builder ()
44
- .certificatePinner (certificatePinner )
53
+ .certificatePinner (certificatePinner . build () )
45
54
.build ();
46
55
47
56
Request request = new Request .Builder ()
@@ -51,6 +60,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
51
60
okHttpClient .newCall (request ).enqueue (new Callback () {
52
61
@ Override
53
62
public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
63
+ Log .d ("ALLSAFE" , e .getMessage ());
54
64
requireActivity ().runOnUiThread (() -> SnackUtil .INSTANCE .simpleMessage (requireActivity (), e .getMessage ()));
55
65
}
56
66
@@ -67,4 +77,39 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
67
77
});
68
78
return view ;
69
79
}
80
+
81
+ private List <String > extractPeerCertificateChain () {
82
+ List <String > chain = new ArrayList <>();
83
+
84
+ OkHttpClient okHttpClient = new OkHttpClient .Builder ()
85
+ .certificatePinner (new CertificatePinner .Builder ()
86
+ .add ("httpbin.org" , INVALID_HASH )
87
+ .build ())
88
+ .build ();
89
+
90
+ Request request = new Request .Builder ()
91
+ .url ("https://httpbin.org/json" )
92
+ .build ();
93
+
94
+ okHttpClient .newCall (request ).enqueue (new Callback () {
95
+ @ Override
96
+ public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
97
+ requireActivity ().runOnUiThread (() -> {
98
+ String [] lines = e .getMessage ().split (System .getProperty ("line.separator" ));
99
+ for (String line : lines ) {
100
+ if (!line .trim ().equals (INVALID_HASH ) && line .trim ().startsWith ("sha256" )) {
101
+ String pin = line .trim ().split (":" )[0 ].trim ();
102
+ chain .add (pin );
103
+ }
104
+ }
105
+ });
106
+ }
107
+
108
+ @ Override
109
+ public void onResponse (@ NotNull Call call , @ NotNull Response response ) {
110
+
111
+ }
112
+ });
113
+ return chain ;
114
+ }
70
115
}
0 commit comments