You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
3 years ago
3 years ago
7 years ago
  1. //
  2. // pEpObjCAdapter.m
  3. // pEpObjCAdapter
  4. //
  5. // Created by Volker Birk on 28.04.15.
  6. // Copyright (c) 2015 pp. All rights reserved.
  7. //
  8. @import Foundation;
  9. #ifdef IS_IOS_BUILD
  10. #import <pEp4iosIntern/pEp4iosIntern.h>
  11. #endif
  12. #import "PEPObjCAdapter.h"
  13. #import "PEPObjCAdapter+Internal.h"
  14. #import "NSError+PEP.h"
  15. #import "NSString+NormalizePassphrase.h"
  16. #import "PEPInternalSession.h"
  17. #import "PEPPassphraseCache.h"
  18. #import "Logger.h"
  19. #import "keymanagement.h"
  20. #import "mime.h"
  21. #import "message.h"
  22. #import "message_api.h"
  23. const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
  24. /**
  25. The pEp part of the home directory (where pEp is supposed to store data).
  26. */
  27. static NSString * const s_pEpHomeComponent = @"pEp_home";
  28. const char* _Nullable perMachineDirectory = NULL;
  29. NSURL *s_homeURL;
  30. static BOOL s_unEncryptedSubjectEnabled = NO;
  31. static BOOL s_passiveModeEnabled = NO;
  32. static NSString *s_passphraseForNewKeys = nil;
  33. static id<PEPPassphraseProviderProtocol> s_passphraseProvider = nil;
  34. @implementation PEPObjCAdapter
  35. #pragma mark - SUBJECT PROTECTION
  36. + (BOOL)unEncryptedSubjectEnabled;
  37. {
  38. return s_unEncryptedSubjectEnabled;
  39. }
  40. + (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
  41. {
  42. s_unEncryptedSubjectEnabled = enabled;
  43. }
  44. #pragma mark - Passive Mode
  45. + (BOOL)passiveModeEnabled
  46. {
  47. return s_passiveModeEnabled;
  48. }
  49. + (void)setPassiveModeEnabled:(BOOL)enabled
  50. {
  51. s_passiveModeEnabled = enabled;
  52. }
  53. #pragma mark - Passphrase for own keys
  54. + (BOOL)configurePassphraseForNewKeys:(NSString * _Nullable)passphrase
  55. error:(NSError * _Nullable * _Nullable)error
  56. {
  57. if (passphrase == nil) {
  58. s_passphraseForNewKeys = nil;
  59. [[PEPPassphraseCache sharedInstance] setStoredPassphrase:passphrase];
  60. return YES;
  61. } else {
  62. NSString *normalizedPassphrase = [passphrase normalizedPassphraseWithError:error];
  63. if (normalizedPassphrase == nil) {
  64. return NO;
  65. }
  66. s_passphraseForNewKeys = normalizedPassphrase;
  67. [[PEPPassphraseCache sharedInstance] setStoredPassphrase:passphrase];
  68. return YES;
  69. }
  70. }
  71. + (NSString * _Nullable)passphraseForNewKeys
  72. {
  73. return s_passphraseForNewKeys;
  74. }
  75. #pragma mark - Passphrase Provider
  76. + (void)setPassphraseProvider:(id<PEPPassphraseProviderProtocol> _Nullable)passphraseProvider
  77. {
  78. s_passphraseProvider = passphraseProvider;
  79. }
  80. + (id<PEPPassphraseProviderProtocol> _Nullable)passphraseProvider
  81. {
  82. return s_passphraseProvider;
  83. }
  84. #pragma mark - DB PATHS
  85. + (void)initialize
  86. {
  87. [self setupPerUserDirectory];
  88. [self setupPerMachineDirectory];
  89. }
  90. + (NSURL *)homeURL
  91. {
  92. return s_homeURL;
  93. }
  94. + (void)setupPerUserDirectory {
  95. // The Engine uses the home env as per-user-directory. We hijack that on iOS.
  96. #if TARGET_OS_IPHONE
  97. s_homeURL = [self createApplicationDirectory];
  98. // The engine will put its per_user_directory under this directory.
  99. setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
  100. #else
  101. // For macOS there is nothing toDo. The defaults in Engine platform_unix.h should do.
  102. #endif
  103. }
  104. + (void)setupPerMachineDirectory {
  105. #if TARGET_OS_IPHONE
  106. [self setPerMachineDirectory:[self homeURL]];
  107. #else
  108. // For macOS there is nothing toDo. The defaults in Engine platform_unix.h should do.
  109. #endif
  110. }
  111. /**
  112. Looks up the shared directory for pEp apps under iOS and makes sure it exists.
  113. @return A URL pointing a pEp directory in the app container.
  114. */
  115. #ifdef IS_IOS_BUILD
  116. + (NSURL *)createApplicationDirectoryiOS
  117. {
  118. NSFileManager *fm = [NSFileManager defaultManager];
  119. NSURL *containerUrl = [fm containerURLForSecurityApplicationGroupIdentifier:kAppGroupIdentifier];
  120. LogInfo(@"containerUrl '%@'", containerUrl);
  121. if (containerUrl == nil) {
  122. // Will happen when running tests, so fall back.
  123. NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
  124. inDomains:NSUserDomainMask];
  125. containerUrl = [appSupportDir lastObject];
  126. }
  127. if (containerUrl == nil) {
  128. LogErrorAndCrash(@"No app container, no application support directory.");
  129. }
  130. NSURL *dirPath = [containerUrl URLByAppendingPathComponent:s_pEpHomeComponent];
  131. // If the directory does not exist, this method creates it.
  132. NSError *theError = nil;
  133. if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
  134. attributes:nil error:&theError]) {
  135. LogErrorAndCrash(@"Could not create pEp home directory, directly writing to app container instead.");
  136. }
  137. return dirPath;
  138. }
  139. #endif
  140. /**
  141. Looks up the shared directory for pEp apps under iOS and makes sure it exists.
  142. Derived settings:
  143. * $HOME (the engine uses that).
  144. * The engine's per_user_directory (which is placed under $HOME).
  145. * The engine's per_machine_directory (see [PEPObjCAdapter setPerMachineDirectory:]).
  146. @return A URL pointing to as app-specific directory under the OS defined
  147. application support directory for the current user.
  148. */
  149. #ifdef IS_IOS_BUILD
  150. + (NSURL *)createApplicationDirectory
  151. {
  152. return [self createApplicationDirectoryiOS];
  153. }
  154. #endif
  155. /**
  156. Sets the directory that will be fed into the engine's per_machine_directory.
  157. Does not handle macOS. For macOS, either PER_MACHINE_DIRECTORY has to be defined
  158. (if constant), or this method has to be extended to handle it.
  159. @param perMachineDir The url to use as the per_machine_directory directory.
  160. */
  161. + (void)setPerMachineDirectory:(NSURL *)perMachineDir
  162. {
  163. if (perMachineDirectory) {
  164. free((void *) perMachineDirectory); //BUFF: DIRK??
  165. }
  166. perMachineDirectory = strdup([perMachineDir path].UTF8String);
  167. }
  168. + (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)srcBundle
  169. fileName:(NSString *)fileName {
  170. #ifdef IS_IOS_BUILD
  171. NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
  172. if(!(srcBundle && systemDir && fileName)) {
  173. return;
  174. }
  175. // Check if the database file exists in the documents directory.
  176. NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
  177. if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
  178. // The file does not exist in the documents directory, so copy it from bundle now.
  179. NSString *sourcePath =[[srcBundle resourcePath] stringByAppendingPathComponent: fileName];
  180. NSError *error;
  181. [[NSFileManager defaultManager]
  182. copyItemAtPath:sourcePath toPath:destinationPath error:&error];
  183. // Check if any error occurred during copying and display it.
  184. if (error != nil) {
  185. LogInfo(@"%@", [error localizedDescription]);
  186. }
  187. }
  188. #endif
  189. }
  190. + (void)setupTrustWordsDB:(NSBundle *)rootBundle {
  191. #if TARGET_OS_IPHONE
  192. [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
  193. fileName:@"system.db"];
  194. #else
  195. // On macOS the installer must put that in place.
  196. #endif
  197. }
  198. + (void)setupTrustWordsDB
  199. {
  200. [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
  201. }
  202. + (NSString * _Nonnull)perUserDirectoryString
  203. {
  204. return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
  205. }
  206. + (NSString * _Nonnull)perMachineDirectoryString
  207. {
  208. return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
  209. }
  210. @end