CCCertificate.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // CCCertificate.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/08/16.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCUtility.h"
  24. #import "CCCertificate.h"
  25. #import <openssl/x509.h>
  26. #import <openssl/bio.h>
  27. #import <openssl/err.h>
  28. #import <openssl/pem.h>
  29. #ifndef EXTENSION
  30. #ifdef CUSTOM_BUILD
  31. #import "CustomSwift.h"
  32. #else
  33. #import "Nextcloud-Swift.h"
  34. #endif
  35. #else
  36. #ifdef EXTENSION_SHARE
  37. #ifdef CUSTOM_BUILD
  38. #import "CustomSwiftShare.h"
  39. #else
  40. #import "Share-Swift.h"
  41. #endif
  42. #endif
  43. #ifdef EXTENSION_PICKER
  44. #ifdef CUSTOM_BUILD
  45. #import "CustomSwiftPick.h"
  46. #else
  47. #import "Picker-Swift.h"
  48. #endif
  49. #endif
  50. #endif
  51. @implementation CCCertificate
  52. //Singleton
  53. + (id)sharedManager {
  54. static CCCertificate *CCCertificate = nil;
  55. static dispatch_once_t onceToken;
  56. dispatch_once(&onceToken, ^{
  57. CCCertificate = [[self alloc] init];
  58. });
  59. return CCCertificate;
  60. }
  61. static SecCertificateRef SecTrustGetLeafCertificate(SecTrustRef trust)
  62. // Returns the leaf certificate from a SecTrust object (that is always the
  63. // certificate at index 0).
  64. {
  65. SecCertificateRef result;
  66. assert(trust != NULL);
  67. if (SecTrustGetCertificateCount(trust) > 0) {
  68. result = SecTrustGetCertificateAtIndex(trust, 0);
  69. assert(result != NULL);
  70. } else {
  71. result = NULL;
  72. }
  73. return result;
  74. }
  75. - (BOOL)checkTrustedChallenge:(NSURLAuthenticationChallenge *)challenge
  76. {
  77. BOOL trusted = NO;
  78. SecTrustRef trust;
  79. NSURLProtectionSpace *protectionSpace;
  80. protectionSpace = [challenge protectionSpace];
  81. trust = [protectionSpace serverTrust];
  82. [CCUtility getDirectoryCerificates];
  83. if(trust != nil) {
  84. [self saveCertificate:trust withName:@"tmp.der"];
  85. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  86. NSArray *listCertificateLocation = [[NCManageDatabase sharedInstance] getAllCertificatesLocation:[CCUtility getDirectoryCerificates]];
  87. for (int i = 0 ; i < [listCertificateLocation count] ; i++) {
  88. NSString *currentLocalCertLocation = [listCertificateLocation objectAtIndex:i];
  89. NSFileManager *fileManager = [ NSFileManager defaultManager];
  90. if([fileManager contentsEqualAtPath:[NSString stringWithFormat:@"%@%@",localCertificatesFolder,@"tmp.der"] andPath:[NSString stringWithFormat:@"%@",currentLocalCertLocation]]) {
  91. NSLog(@"[LOG] Is the same certificate!!!");
  92. trusted = YES;
  93. }
  94. }
  95. } else
  96. trusted = NO;
  97. return trusted;
  98. }
  99. - (void)saveCertificate:(SecTrustRef)trust withName:(NSString *)certName
  100. {
  101. SecCertificateRef currentServerCert = SecTrustGetLeafCertificate(trust);
  102. CFDataRef data = SecCertificateCopyData(currentServerCert);
  103. X509 *x509cert = NULL;
  104. if (data) {
  105. BIO *mem = BIO_new_mem_buf((void *)CFDataGetBytePtr(data), (int)CFDataGetLength(data));
  106. x509cert = d2i_X509_bio(mem, NULL);
  107. BIO_free(mem);
  108. CFRelease(data);
  109. if (!x509cert) {
  110. NSLog(@"[LOG] OpenSSL couldn't parse X509 Certificate");
  111. } else {
  112. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  113. certName = [NSString stringWithFormat:@"%@%@",localCertificatesFolder,certName];
  114. if ([[NSFileManager defaultManager] fileExistsAtPath:certName]) {
  115. NSError *error;
  116. [[NSFileManager defaultManager] removeItemAtPath:certName error:&error];
  117. }
  118. FILE *file;
  119. file = fopen([certName UTF8String], "w");
  120. if (file) {
  121. PEM_write_X509(file, x509cert);
  122. }
  123. fclose(file);
  124. }
  125. } else {
  126. NSLog(@"[LOG] Failed to retrieve DER data from Certificate Ref");
  127. }
  128. //Free
  129. X509_free(x509cert);
  130. }
  131. - (void)presentViewControllerCertificateWithTitle:(NSString *)title viewController:(UIViewController *)viewController delegate:(id)delegate
  132. {
  133. if (![viewController isKindOfClass:[UIViewController class]])
  134. return;
  135. //viewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
  136. _delegate = delegate;
  137. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  138. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  139. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  140. [[CCCertificate sharedManager] acceptCertificate];
  141. if([self.delegate respondsToSelector:@selector(trustedCerticateAccepted)])
  142. [self.delegate trustedCerticateAccepted];
  143. }]];
  144. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  145. if([self.delegate respondsToSelector:@selector(trustedCerticateDenied)])
  146. [self.delegate trustedCerticateDenied];
  147. }]];
  148. [viewController presentViewController:alertController animated:YES completion:nil];
  149. });
  150. }
  151. - (void)viewCertificate:(SecTrustRef)trust
  152. {
  153. SecCertificateRef currentServerCert = SecTrustGetLeafCertificate(trust);
  154. CFDataRef data = SecCertificateCopyData(currentServerCert);
  155. X509 *x509cert = NULL;
  156. if (data) {
  157. BIO *mem = BIO_new_mem_buf((void *)CFDataGetBytePtr(data), (int)CFDataGetLength(data));
  158. x509cert = d2i_X509_bio(mem, NULL);
  159. BIO_free(mem);
  160. CFRelease(data);
  161. if (!x509cert) {
  162. NSLog(@"[LOG] OpenSSL couldn't parse X509 Certificate");
  163. } else {
  164. NSString *issuer = CertificateGetIssuerName(x509cert);
  165. NSDate *expiryDate = CertificateGetExpiryDate(x509cert);
  166. NSLog(@"[LOG] %@ %@", issuer, expiryDate);
  167. }
  168. } else {
  169. NSLog(@"[LOG] Failed to retrieve DER data from Certificate Ref");
  170. }
  171. //Free
  172. X509_free(x509cert);
  173. }
  174. - (BOOL)acceptCertificate
  175. {
  176. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  177. NSError *error;
  178. NSFileManager *fm = [[NSFileManager alloc] init];
  179. NSTimeInterval dateCertificate = [[NSDate date] timeIntervalSince1970];
  180. NSString *currentCertLocation = [NSString stringWithFormat:@"%@%f.der",localCertificatesFolder, dateCertificate];
  181. NSLog(@"[LOG] currentCertLocation: %@", currentCertLocation);
  182. if(![fm moveItemAtPath:[NSString stringWithFormat:@"%@%@",localCertificatesFolder, @"tmp.der"] toPath:currentCertLocation error:&error]) {
  183. NSLog(@"[LOG] Error: %@", [error localizedDescription]);
  184. return NO;
  185. } else {
  186. [[NCManageDatabase sharedInstance] addCertificates:[NSString stringWithFormat:@"%f.der", dateCertificate]];
  187. }
  188. return YES;
  189. }
  190. static NSString * CertificateGetIssuerName(X509 *certificateX509)
  191. {
  192. NSString *issuer = nil;
  193. if (certificateX509 != NULL) {
  194. X509_NAME *issuerX509Name = X509_get_issuer_name(certificateX509);
  195. if (issuerX509Name != NULL) {
  196. int nid = OBJ_txt2nid("O"); // organization
  197. int index = X509_NAME_get_index_by_NID(issuerX509Name, nid, -1);
  198. X509_NAME_ENTRY *issuerNameEntry = X509_NAME_get_entry(issuerX509Name, index);
  199. if (issuerNameEntry) {
  200. ASN1_STRING *issuerNameASN1 = X509_NAME_ENTRY_get_data(issuerNameEntry);
  201. if (issuerNameASN1 != NULL) {
  202. unsigned char *issuerName = ASN1_STRING_data(issuerNameASN1);
  203. issuer = [NSString stringWithUTF8String:(char *)issuerName];
  204. }
  205. }
  206. }
  207. }
  208. return issuer;
  209. }
  210. static NSDate *CertificateGetExpiryDate(X509 *certificateX509)
  211. {
  212. NSDate *expiryDate = nil;
  213. if (certificateX509 != NULL) {
  214. ASN1_TIME *certificateExpiryASN1 = X509_get_notAfter(certificateX509);
  215. if (certificateExpiryASN1 != NULL) {
  216. ASN1_GENERALIZEDTIME *certificateExpiryASN1Generalized = ASN1_TIME_to_generalizedtime(certificateExpiryASN1, NULL);
  217. if (certificateExpiryASN1Generalized != NULL) {
  218. unsigned char *certificateExpiryData = ASN1_STRING_data(certificateExpiryASN1Generalized);
  219. // ASN1 generalized times look like this: "20131114230046Z"
  220. // format: YYYYMMDDHHMMSS
  221. // indices: 01234567890123
  222. // 1111
  223. // There are other formats (e.g. specifying partial seconds or
  224. // time zones) but this is good enough for our purposes since
  225. // we only use the date and not the time.
  226. //
  227. // (Source: http://www.obj-sys.com/asn1tutorial/node14.html)
  228. NSString *expiryTimeStr = [NSString stringWithUTF8String:(char *)certificateExpiryData];
  229. NSDateComponents *expiryDateComponents = [[NSDateComponents alloc] init];
  230. expiryDateComponents.year = [[expiryTimeStr substringWithRange:NSMakeRange(0, 4)] intValue];
  231. expiryDateComponents.month = [[expiryTimeStr substringWithRange:NSMakeRange(4, 2)] intValue];
  232. expiryDateComponents.day = [[expiryTimeStr substringWithRange:NSMakeRange(6, 2)] intValue];
  233. expiryDateComponents.hour = [[expiryTimeStr substringWithRange:NSMakeRange(8, 2)] intValue];
  234. expiryDateComponents.minute = [[expiryTimeStr substringWithRange:NSMakeRange(10, 2)] intValue];
  235. expiryDateComponents.second = [[expiryTimeStr substringWithRange:NSMakeRange(12, 2)] intValue];
  236. NSCalendar *calendar = [NSCalendar currentCalendar];
  237. expiryDate = [calendar dateFromComponents:expiryDateComponents];
  238. }
  239. }
  240. }
  241. return expiryDate;
  242. }
  243. @end