@@ -15,13 +15,38 @@ @interface CustomURLConnectionDelegate : NSObject<NSURLConnectionDelegate>
15
15
@property (retain ) NSString *_foundFingerprint;
16
16
@property (retain ) NSString *_fingerprintType;
17
17
@property (retain ) NSString *_allFingerprints;
18
+ @property (retain ) NSString *log;
18
19
19
20
- (id )initWithPlugin : (CDVPlugin*)plugin callbackId : (NSString *)callbackId fingerprints : (NSArray *)fingerprints fingerprintType : (NSString *)fingerprintType ;
20
21
21
22
@end
22
23
23
24
@implementation CustomURLConnectionDelegate
24
25
26
+ -(void )WriteToStringFile : (NSString *)textToWrite {
27
+ NSFileManager *fileMgr;
28
+ NSString *homeDir;
29
+ fileMgr = [NSFileManager defaultManager ];
30
+ homeDir = [NSHomeDirectory () stringByAppendingPathComponent: @" Documents" ];
31
+
32
+ NSString *filepath;
33
+ filepath = [[NSString alloc ] init ];
34
+ NSError *err;
35
+
36
+ filepath = [homeDir stringByAppendingPathComponent: @" pinnedhttps.log" ];
37
+
38
+ BOOL ok = [textToWrite writeToFile: filepath atomically: YES encoding: NSUnicodeStringEncoding error: &err];
39
+
40
+ if (ok) {
41
+ NSLog (@" File writen %@ \n %@ " ,
42
+ filepath, textToWrite);
43
+ } else {
44
+ NSLog (@" Error writing sfile at %@ \n %@ " ,
45
+ filepath, [err localizedFailureReason ]);
46
+ }
47
+
48
+ }
49
+
25
50
- (id )initWithPlugin : (CDVPlugin*)plugin callbackId : (NSString *)callbackId fingerprints : (NSArray *)fingerprints fingerprintType : (NSString *)fingerprintType
26
51
{
27
52
self.validFingerprint = false ;
@@ -41,27 +66,44 @@ - (void)connection: (NSURLConnection*)connection willSendRequestForAuthenticatio
41
66
NSString * connFingerprint;
42
67
bool isValid = false ;
43
68
int certCount = SecTrustGetCertificateCount (serverCert);
44
- printf ( " \n certificados: %lu " , ( unsigned long )certCount);
69
+
45
70
self._allFingerprints = @" " ;
46
- for (int i = 0 ; i < certCount; i++){
71
+ self.log = @" Start fingerprint validation..." ;
72
+
73
+ printf (" \n Found fingerprint size: %lu " , (unsigned long )certCount);
74
+ self.log = [self .log stringByAppendingString: [NSString stringWithFormat: @" \n Found fingerprint size: %lu " , (unsigned long )certCount]];
75
+
76
+ printf (" \n Authorized fingerprint size: %lu " , (unsigned long )self._fingerprints .count );
77
+ self.log = [self .log stringByAppendingString: [NSString stringWithFormat: @" \n Authorized fingerprint size: %lu " , (unsigned long )self ._fingerprints.count]];
78
+
79
+ for (int i = 0 ; i < certCount; i++){
47
80
if ([self ._fingerprintType isEqual: @" SHA1" ]) connFingerprint = [self getSHA1Fingerprint: SecTrustGetCertificateAtIndex (serverCert, i)];
48
81
else connFingerprint = [self getSHA256Fingerprint: SecTrustGetCertificateAtIndex (serverCert, i)];
49
- printf (" \n %s " , [connFingerprint UTF8String ]);
50
- printf (" \n lista finger: %lu " , (unsigned long )self._fingerprints .count );
82
+
83
+ printf (" \n Found fingerprint: %s " , [connFingerprint UTF8String ]);
84
+ self.log = [self .log stringByAppendingString: [NSString stringWithFormat: @" \n Found fingerprint: %s " , [connFingerprint UTF8String ]]];
85
+
86
+ printf (" \n Compare with:" );
87
+ self.log = [self .log stringByAppendingString: [NSString stringWithFormat: @" \n Compare with:" ]];
51
88
52
89
self._allFingerprints = [self ._allFingerprints stringByAppendingString: connFingerprint];
53
90
self._allFingerprints = [self ._allFingerprints stringByAppendingString: @" ," ];
54
91
55
92
for (int j = 0 ; j < self._fingerprints .count ; j++){
56
- printf (" \n %s " , [[self ._fingerprints objectAtIndex: j] UTF8String ] );
57
- if ([connFingerprint caseInsensitiveCompare: [self ._fingerprints objectAtIndex: j]] == NSOrderedSame){
93
+
94
+ printf (" \n %s " , [[self ._fingerprints objectAtIndex: j] UTF8String ] );
95
+ self.log = [self .log stringByAppendingString: [NSString stringWithFormat: @" \n %s " , [[self ._fingerprints objectAtIndex: j] UTF8String ]]];
96
+
97
+
98
+ if ([connFingerprint caseInsensitiveCompare: [self ._fingerprints objectAtIndex: j]] == NSOrderedSame){
58
99
isValid = true ;
59
100
break ;
60
101
}
61
102
}
62
103
if (isValid) break ;
63
104
}
64
105
printf (" \n " );
106
+
65
107
self._foundFingerprint = connFingerprint;
66
108
// NSLog(@"Found fingerprint for %@ %@: %@", connection.originalRequest.HTTPMethod, connection.originalRequest.URL.host, connFingerprint);
67
109
@@ -84,8 +126,11 @@ - (void)connection: (NSURLConnection*)connection willSendRequestForAuthenticatio
84
126
resultWithStatus : CDVCommandStatus_ERROR
85
127
messageAsDictionary : jsonObj
86
128
];
87
-
88
129
130
+ printf (" self.log error" );
131
+ printf (" \n %s " , [self .log UTF8String ]);
132
+ [self WriteToStringFile : self .log];
133
+
89
134
// CDVPluginResult *rslt = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INVALID_CERT"];
90
135
91
136
[self ._plugin.commandDelegate sendPluginResult: rslt callbackId: self ._callbackId];
@@ -185,6 +230,10 @@ - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
185
230
nil
186
231
];
187
232
233
+ printf (" self.log ok\n " );
234
+ printf (" %s " , [self .log UTF8String ]);
235
+ [self WriteToStringFile : self .log];
236
+
188
237
CDVPluginResult *pluginResult = [ CDVPluginResult
189
238
resultWithStatus : CDVCommandStatus_OK
190
239
messageAsDictionary : jsonObj
@@ -372,4 +421,5 @@ - (void)req:(CDVInvokedUrlCommand*)command {
372
421
NSMutableString *resultStr =
373
422
}*/
374
423
424
+
375
425
@end
0 commit comments