pEpObjCAdapter/PEPObjCAdapter.m
author Dirk Zimmermann <dz@pep.security>
Fri, 06 Sep 2019 11:03:27 +0200
branchIOS-1784
changeset 1224 dd09a3ede1d0
parent 1223 023b20652160
child 1225 6d89f5f624db
permissions -rw-r--r--
IOS-1784 Docs.
dirk@187
     1
//
edouard@191
     2
//  pEpObjCAdapter.m
edouard@191
     3
//  pEpObjCAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Volker Birk on 28.04.15.
dirk@187
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
@import Foundation;
dirk@187
    10
dirk@187
    11
#import "PEPObjCAdapter.h"
dirk@187
    12
#import "PEPObjCAdapter+Internal.h"
dirk@367
    13
#import "PEPMessageUtil.h"
dirk@484
    14
#import "NSError+PEP.h"
dirk@484
    15
dirk@484
    16
#import "keymanagement.h"
dirk@484
    17
#import "mime.h"
dirk@484
    18
#import "message.h"
dirk@187
    19
andreas@635
    20
const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
andreas@635
    21
andreas@1173
    22
#if TARGET_OS_IPHONE
andreas@1175
    23
// marked for iOS to think about what we want on macOS
andreas@1173
    24
const char* _Nullable perMachineDirectory = NULL;
andreas@1173
    25
#endif
andreas@1173
    26
dirk@187
    27
NSURL *s_homeURL;
dirk@625
    28
dirk@601
    29
static BOOL s_unEncryptedSubjectEnabled = NO;
dirk@625
    30
static BOOL s_passiveModeEnabled = NO;
dirk@187
    31
dirk@187
    32
@implementation PEPObjCAdapter
dirk@187
    33
andreas@416
    34
#pragma mark - SUBJECT PROTECTION
andreas@416
    35
dirk@601
    36
+ (BOOL)unEncryptedSubjectEnabled;
andreas@416
    37
{
dirk@601
    38
    return s_unEncryptedSubjectEnabled;
andreas@416
    39
}
andreas@416
    40
dirk@601
    41
+ (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
andreas@416
    42
{
dirk@601
    43
    s_unEncryptedSubjectEnabled = enabled;
andreas@416
    44
}
andreas@416
    45
dirk@625
    46
#pragma mark - Passive Mode
dirk@625
    47
dirk@625
    48
+ (BOOL)passiveModeEnabled
dirk@625
    49
{
dirk@625
    50
    return s_passiveModeEnabled;
dirk@625
    51
}
dirk@625
    52
dirk@625
    53
+ (void)setPassiveModeEnabled:(BOOL)enabled
dirk@625
    54
{
dirk@625
    55
    s_passiveModeEnabled = enabled;
dirk@625
    56
}
dirk@625
    57
andreas@416
    58
#pragma mark - DB PATHS
andreas@416
    59
dirk@187
    60
+ (void)initialize
dirk@187
    61
{
dirk@187
    62
    s_homeURL = [self createApplicationDirectory];
dz@1211
    63
    [self setPerMachineDirectory:s_homeURL]; // Important, defines $HOME for the engine
dirk@187
    64
}
dirk@187
    65
dirk@187
    66
+ (NSURL *)homeURL
dirk@187
    67
{
dirk@187
    68
    return s_homeURL;
dirk@187
    69
}
dirk@187
    70
dz@1224
    71
/**
dz@1224
    72
 Looks up the application support directory and creates an app-specific subdirectory under it.
dz@1224
    73
dz@1224
    74
 Directories derived from it:
dz@1224
    75
dz@1224
    76
 * $HOME is set for the engine.
dz@1224
    77
 * The engine's per_user_directory (which is placed under $HOME).
dz@1224
    78
 * The engine's per_machine_directory.
dz@1224
    79
dz@1224
    80
 @return A URL pointing to as app-specific directory under the OS defined
dz@1224
    81
         application support directory for the current user.
dz@1224
    82
 */
dirk@187
    83
+ (NSURL *)createApplicationDirectory
dirk@187
    84
{
dirk@187
    85
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
    86
    if (!bundleID) {
dirk@187
    87
        // This can happen in unit tests
dirk@187
    88
        bundleID = @"test";
dirk@187
    89
    }
dirk@187
    90
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
    91
    NSURL *dirPath = nil;
andreas@268
    92
    
dirk@187
    93
    // Find the application support directory in the home directory.
dirk@187
    94
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
    95
                                        inDomains:NSUserDomainMask];
dirk@187
    96
    if ([appSupportDir count] > 0)
dirk@187
    97
    {
dirk@187
    98
        // Append the bundle ID to the URL for the
dirk@187
    99
        // Application Support directory.
dirk@187
   100
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
   101
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
   102
        
dirk@187
   103
        // If the directory does not exist, this method creates it.
dirk@187
   104
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
   105
        NSError *theError = nil;
dirk@187
   106
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
   107
                           attributes:nil error:&theError])
dirk@187
   108
        {
dirk@187
   109
            // Handle the error.
dirk@187
   110
            return nil;
dirk@187
   111
        }
dirk@187
   112
    }
andreas@268
   113
    
dirk@187
   114
    return dirPath;
dirk@187
   115
}
dirk@187
   116
andreas@1175
   117
+ (void)setPerMachineDirectory:(NSURL *)homeDir
dirk@187
   118
{
andreas@1173
   119
#if TARGET_OS_IPHONE
dz@1211
   120
    // Set HOME, which is also important for the perUser directory later
dz@1211
   121
    setenv("HOME", [[homeDir path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
dz@1211
   122
andreas@1175
   123
    if (perMachineDirectory) {
andreas@1175
   124
        free((void *) perMachineDirectory);
andreas@1173
   125
    }
andreas@1175
   126
    perMachineDirectory = strdup([homeDir path].UTF8String);
andreas@1173
   127
#endif
dirk@187
   128
}
dirk@187
   129
andreas@276
   130
+ (NSString *)getBundlePathFor: (NSString *) filename
dirk@187
   131
{
dirk@187
   132
    return nil;
dirk@187
   133
}
dirk@187
   134
andreas@1175
   135
+ (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
dirk@442
   136
                                    bundleName:(NSString *)bundleName
dirk@442
   137
                                      fileName:(NSString *)fileName {
andreas@1175
   138
andreas@1175
   139
    NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
andreas@268
   140
    
andreas@1175
   141
    if(!(systemDir && bundleName && fileName))
andreas@1175
   142
        return;
dirk@187
   143
    
dirk@187
   144
    // Check if the database file exists in the documents directory.
andreas@1175
   145
    NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
dirk@187
   146
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   147
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   148
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   149
                               [[rootBundle resourcePath]
dirk@187
   150
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   151
        if (!bundleObj)
andreas@1175
   152
            return;
dirk@187
   153
        
dirk@187
   154
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   155
        
dirk@187
   156
        NSError *error;
dirk@187
   157
        [[NSFileManager defaultManager]
dirk@187
   158
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   159
        
dirk@187
   160
        // Check if any error occurred during copying and display it.
dirk@187
   161
        if (error != nil) {
dirk@187
   162
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   163
        }
dirk@187
   164
    }
dirk@187
   165
}
dirk@187
   166
andreas@1173
   167
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle {
andreas@1175
   168
// iOS to force us to think about macOS
andreas@1173
   169
#if TARGET_OS_IPHONE
andreas@1175
   170
    [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
andreas@1175
   171
                                          bundleName:@"pEpTrustWords.bundle"
andreas@1175
   172
                                            fileName:@"system.db"];
andreas@1175
   173
andreas@1173
   174
#endif
dirk@187
   175
}
dirk@187
   176
dirk@187
   177
+ (void)setupTrustWordsDB
dirk@187
   178
{
dirk@187
   179
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   180
}
dirk@187
   181
dz@1215
   182
dz@1216
   183
+ (NSString * _Nonnull)perUserDirectoryString
dz@1215
   184
{
dz@1215
   185
    return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
dz@1215
   186
}
dz@1215
   187
dz@1216
   188
+ (NSString * _Nonnull)perMachineDirectoryString
dz@1215
   189
{
dz@1215
   190
    return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
dz@1215
   191
}
dz@1215
   192
dirk@187
   193
@end