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.

298 lines
8.5 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
3 years ago
3 years ago
7 years ago
7 years ago
7 years ago
7 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. #import "PEPObjCAdapter.h"
  10. #import "PEPObjCAdapter+Internal.h"
  11. #import "PEPMessageUtil.h"
  12. #import "NSError+PEP.h"
  13. #import "NSString+NormalizePassphrase.h"
  14. #import "PEPInternalSession.h"
  15. #import "PEPPassphraseCache.h"
  16. #import "keymanagement.h"
  17. #import "mime.h"
  18. #import "message.h"
  19. const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
  20. /**
  21. The pEp part of the home directory (where pEp is supposed to store data).
  22. */
  23. static NSString * const s_pEpHomeComponent = @"pEp_home";
  24. #if TARGET_OS_IPHONE
  25. // marked for iOS to think about what we want on macOS
  26. const char* _Nullable perMachineDirectory = NULL;
  27. #endif
  28. NSURL *s_homeURL;
  29. static BOOL s_unEncryptedSubjectEnabled = NO;
  30. static BOOL s_passiveModeEnabled = NO;
  31. static NSString *s_passphraseForNewKeys = nil;
  32. static id<PEPPassphraseProviderProtocol> s_passphraseProvider = nil;
  33. @implementation PEPObjCAdapter
  34. #pragma mark - SUBJECT PROTECTION
  35. + (BOOL)unEncryptedSubjectEnabled;
  36. {
  37. return s_unEncryptedSubjectEnabled;
  38. }
  39. + (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
  40. {
  41. s_unEncryptedSubjectEnabled = enabled;
  42. }
  43. #pragma mark - Passive Mode
  44. + (BOOL)passiveModeEnabled
  45. {
  46. return s_passiveModeEnabled;
  47. }
  48. + (void)setPassiveModeEnabled:(BOOL)enabled
  49. {
  50. s_passiveModeEnabled = enabled;
  51. }
  52. #pragma mark - Passphrase for own keys
  53. + (BOOL)configurePassphraseForNewKeys:(NSString * _Nullable)passphrase
  54. error:(NSError * _Nullable * _Nullable)error
  55. {
  56. if (passphrase == nil) {
  57. s_passphraseForNewKeys = nil;
  58. [[PEPPassphraseCache sharedInstance] setStoredPassphrase:passphrase];
  59. return YES;
  60. } else {
  61. NSString *normalizedPassphrase = [passphrase normalizedPassphraseWithError:error];
  62. if (normalizedPassphrase == nil) {
  63. return NO;
  64. }
  65. s_passphraseForNewKeys = normalizedPassphrase;
  66. [[PEPPassphraseCache sharedInstance] setStoredPassphrase:passphrase];
  67. return YES;
  68. }
  69. }
  70. + (NSString * _Nullable)passphraseForNewKeys
  71. {
  72. return s_passphraseForNewKeys;
  73. }
  74. #pragma mark - Passphrase Provider
  75. + (void)setPassphraseProvider:(id<PEPPassphraseProviderProtocol> _Nullable)passphraseProvider
  76. {
  77. s_passphraseProvider = passphraseProvider;
  78. }
  79. + (id<PEPPassphraseProviderProtocol> _Nullable)passphraseProvider
  80. {
  81. return s_passphraseProvider;
  82. }
  83. #pragma mark - DB PATHS
  84. + (void)initialize
  85. {
  86. s_homeURL = [self createApplicationDirectory];
  87. // The engine will put its per_user_directory under this directory.
  88. setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
  89. // This sets the engine's per_machine_directory under iOS.
  90. [self setPerMachineDirectory:s_homeURL];
  91. }
  92. + (NSURL *)homeURL
  93. {
  94. return s_homeURL;
  95. }
  96. /**
  97. Looks up (and creates if necessary) a pEp directory under "Application Support".
  98. @return A URL pointing a pEp directory under "Application Support".
  99. */
  100. + (NSURL *)createApplicationDirectoryOSX
  101. {
  102. NSFileManager *fm = [NSFileManager defaultManager];
  103. NSURL *dirPath = nil;
  104. // Find the application support directory in the home directory.
  105. NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
  106. inDomains:NSUserDomainMask];
  107. if ([appSupportDir count] > 0)
  108. {
  109. // Append the bundle ID to the URL for the
  110. // Application Support directory.
  111. // Mainly needed for OS X, but doesn't do any harm on iOS
  112. dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:s_pEpHomeComponent];
  113. // If the directory does not exist, this method creates it.
  114. // This method is only available in OS X v10.7 and iOS 5.0 or later.
  115. NSError *theError = nil;
  116. if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
  117. attributes:nil error:&theError])
  118. {
  119. // Handle the error.
  120. return nil;
  121. }
  122. }
  123. return dirPath;
  124. }
  125. /**
  126. Looks up the shared directory for pEp apps under iOS and makes sure it exists.
  127. @return A URL pointing a pEp directory in the app container.
  128. */
  129. + (NSURL *)createApplicationDirectoryiOS
  130. {
  131. NSString *appGroupId = @"group.security.pep.pep4ios";
  132. NSFileManager *fm = [NSFileManager defaultManager];
  133. NSURL *containerUrl = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupId];
  134. NSLog(@"containerUrl '%@'", containerUrl);
  135. if (containerUrl == nil) {
  136. // Will happen when running tests, so fall back.
  137. NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
  138. inDomains:NSUserDomainMask];
  139. containerUrl = [appSupportDir lastObject];
  140. }
  141. if (containerUrl == nil) {
  142. NSLog(@"ERROR: No app container, no application support directory.");
  143. }
  144. NSURL *dirPath = [containerUrl URLByAppendingPathComponent:s_pEpHomeComponent];
  145. // If the directory does not exist, this method creates it.
  146. NSError *theError = nil;
  147. if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
  148. attributes:nil error:&theError]) {
  149. NSLog(@"ERROR: Could not create pEp home directory, directly writing to app container instead.");
  150. }
  151. return dirPath;
  152. }
  153. /**
  154. Looks up the shared directory for pEp apps under iOS and makes sure it exists.
  155. Derived settings:
  156. * $HOME (the engine uses that).
  157. * The engine's per_user_directory (which is placed under $HOME).
  158. * The engine's per_machine_directory (see [PEPObjCAdapter setPerMachineDirectory:]).
  159. @return A URL pointing to as app-specific directory under the OS defined
  160. application support directory for the current user.
  161. */
  162. + (NSURL *)createApplicationDirectory
  163. {
  164. #if TARGET_OS_IPHONE
  165. return [self createApplicationDirectoryiOS];
  166. #else
  167. return [self createApplicationDirectoryOSX];
  168. #endif
  169. }
  170. /**
  171. Sets the directory that will be fed into the engine's per_machine_directory.
  172. Does not handle macOS. For macOS, either PER_MACHINE_DIRECTORY has to be defined
  173. (if constant), or this method has to be extended to handle it.
  174. @param perMachineDir The url to use as the per_machine_directory directory.
  175. */
  176. + (void)setPerMachineDirectory:(NSURL *)perMachineDir
  177. {
  178. #if TARGET_OS_IPHONE
  179. if (perMachineDirectory) {
  180. free((void *) perMachineDirectory);
  181. }
  182. perMachineDirectory = strdup([perMachineDir path].UTF8String);
  183. #endif
  184. }
  185. + (NSString *)getBundlePathFor: (NSString *) filename
  186. {
  187. return nil;
  188. }
  189. + (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
  190. bundleName:(NSString *)bundleName
  191. fileName:(NSString *)fileName {
  192. NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
  193. if(!(systemDir && bundleName && fileName))
  194. return;
  195. // Check if the database file exists in the documents directory.
  196. NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
  197. if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
  198. // The file does not exist in the documents directory, so copy it from bundle now.
  199. NSBundle *bundleObj = [NSBundle bundleWithPath:
  200. [[rootBundle resourcePath]
  201. stringByAppendingPathComponent: bundleName]];
  202. if (!bundleObj)
  203. return;
  204. NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
  205. NSError *error;
  206. [[NSFileManager defaultManager]
  207. copyItemAtPath:sourcePath toPath:destinationPath error:&error];
  208. // Check if any error occurred during copying and display it.
  209. if (error != nil) {
  210. NSLog(@"%@", [error localizedDescription]);
  211. }
  212. }
  213. }
  214. + (void)setupTrustWordsDB:(NSBundle *)rootBundle {
  215. // iOS to force us to think about macOS
  216. #if TARGET_OS_IPHONE
  217. [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
  218. bundleName:@"pEpTrustWords.bundle"
  219. fileName:@"system.db"];
  220. #endif
  221. }
  222. + (void)setupTrustWordsDB
  223. {
  224. [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
  225. }
  226. + (NSString * _Nonnull)perUserDirectoryString
  227. {
  228. return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
  229. }
  230. + (NSString * _Nonnull)perMachineDirectoryString
  231. {
  232. return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
  233. }
  234. @end