CCCertificate.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // CCCertificate.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/08/16.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "NCBridgeSwift.h"
  26. #import "AppDelegate.h"
  27. #import <OpenSSL/OpenSSL.h>
  28. @implementation CCCertificate
  29. //Singleton
  30. + (CCCertificate *)sharedManager {
  31. static CCCertificate *CCCertificate = nil;
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. CCCertificate = [[self alloc] init];
  35. });
  36. return CCCertificate;
  37. }
  38. static SecCertificateRef SecTrustGetLeafCertificate(SecTrustRef trust)
  39. // Returns the leaf certificate from a SecTrust object (that is always the
  40. // certificate at index 0).
  41. {
  42. SecCertificateRef result;
  43. assert(trust != NULL);
  44. if (SecTrustGetCertificateCount(trust) > 0) {
  45. result = SecTrustGetCertificateAtIndex(trust, 0);
  46. assert(result != NULL);
  47. } else {
  48. result = NULL;
  49. }
  50. return result;
  51. }
  52. - (BOOL)checkTrustedChallenge:(NSURLAuthenticationChallenge *)challenge
  53. {
  54. BOOL trusted = NO;
  55. SecTrustRef trust;
  56. NSURLProtectionSpace *protectionSpace;
  57. protectionSpace = [challenge protectionSpace];
  58. trust = [protectionSpace serverTrust];
  59. if(trust != nil) {
  60. [self saveCertificate:trust withName:@"tmp.der"];
  61. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  62. NSArray *listCertificateLocation = [[NCManageDatabase sharedInstance] getCertificatesLocation:[CCUtility getDirectoryCerificates]];
  63. for (int i = 0 ; i < [listCertificateLocation count] ; i++) {
  64. NSString *currentLocalCertLocation = [listCertificateLocation objectAtIndex:i];
  65. NSFileManager *fileManager = [ NSFileManager defaultManager];
  66. if([fileManager contentsEqualAtPath:[NSString stringWithFormat:@"%@/%@",localCertificatesFolder,@"tmp.der"] andPath:[NSString stringWithFormat:@"%@",currentLocalCertLocation]]) {
  67. NSLog(@"[LOG] Is the same certificate!!!");
  68. trusted = YES;
  69. }
  70. }
  71. } else
  72. trusted = NO;
  73. return trusted;
  74. }
  75. - (void)saveCertificate:(SecTrustRef)trust withName:(NSString *)certName
  76. {
  77. SecCertificateRef currentServerCert = SecTrustGetLeafCertificate(trust);
  78. CFDataRef data = SecCertificateCopyData(currentServerCert);
  79. X509 *x509cert = NULL;
  80. if (data) {
  81. BIO *mem = BIO_new_mem_buf((void *)CFDataGetBytePtr(data), (int)CFDataGetLength(data));
  82. x509cert = d2i_X509_bio(mem, NULL);
  83. BIO_free(mem);
  84. CFRelease(data);
  85. if (!x509cert) {
  86. NSLog(@"[LOG] OpenSSL couldn't parse X509 Certificate");
  87. } else {
  88. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  89. certName = [NSString stringWithFormat:@"%@/%@",localCertificatesFolder,certName];
  90. if ([[NSFileManager defaultManager] fileExistsAtPath:certName]) {
  91. NSError *error;
  92. [[NSFileManager defaultManager] removeItemAtPath:certName error:&error];
  93. }
  94. FILE *file;
  95. file = fopen([certName UTF8String], "w");
  96. if (file) {
  97. PEM_write_X509(file, x509cert);
  98. }
  99. fclose(file);
  100. }
  101. } else {
  102. NSLog(@"[LOG] Failed to retrieve DER data from Certificate Ref");
  103. }
  104. //Free
  105. X509_free(x509cert);
  106. }
  107. - (void)presentViewControllerCertificateWithAccount:(NSString *)account viewController:(UIViewController *)viewController delegate:(id)delegate
  108. {
  109. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  110. _delegate = delegate;
  111. if (![viewController isKindOfClass:[UIViewController class]])
  112. return;
  113. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  114. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  115. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  116. [[CCCertificate sharedManager] acceptCertificate];
  117. if (account != nil) [CCUtility setCertificateError:account error:NO];
  118. [appDelegate startTimerErrorNetworking];
  119. if([self.delegate respondsToSelector:@selector(trustedCerticateAccepted)])
  120. [self.delegate trustedCerticateAccepted];
  121. }]];
  122. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  123. [appDelegate startTimerErrorNetworking];
  124. if([self.delegate respondsToSelector:@selector(trustedCerticateDenied)])
  125. [self.delegate trustedCerticateDenied];
  126. }]];
  127. [viewController presentViewController:alertController animated:YES completion:^{
  128. // Stop timer error network
  129. [appDelegate.timerErrorNetworking invalidate];
  130. }];
  131. });
  132. }
  133. - (BOOL)acceptCertificate
  134. {
  135. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  136. NSError *error;
  137. NSFileManager *fm = [[NSFileManager alloc] init];
  138. NSTimeInterval dateCertificate = [[NSDate date] timeIntervalSince1970];
  139. NSString *currentCertLocation = [NSString stringWithFormat:@"%@/%f.der",localCertificatesFolder, dateCertificate];
  140. NSLog(@"[LOG] currentCertLocation: %@", currentCertLocation);
  141. if(![fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@",localCertificatesFolder, @"tmp.der"] toPath:currentCertLocation error:&error]) {
  142. NSLog(@"[LOG] Error: %@", [error localizedDescription]);
  143. return NO;
  144. } else {
  145. [[NCManageDatabase sharedInstance] addCertificates:[NSString stringWithFormat:@"%f.der", dateCertificate]];
  146. }
  147. return YES;
  148. }
  149. @end