pEpObjCAdapter/PEPObjCAdapter.m
author Dirk Zimmermann <dz@pep.security>
Fri, 06 Sep 2019 11:03:27 +0200
branchIOS-1784
changeset 1226 e808633de8ea
parent 1225 6d89f5f624db
child 1227 5abaa89ca842
permissions -rw-r--r--
IOS-1784 Docs, rename.
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@1225
    63
dz@1225
    64
    // The engine will put its per_user_directory under this directory.
dz@1225
    65
    setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
dz@1225
    66
dz@1225
    67
    // This informs the engine's per_machine_directory.
dz@1225
    68
    [self setPerMachineDirectory:s_homeURL];
dirk@187
    69
}
dirk@187
    70
dirk@187
    71
+ (NSURL *)homeURL
dirk@187
    72
{
dirk@187
    73
    return s_homeURL;
dirk@187
    74
}
dirk@187
    75
dz@1224
    76
/**
dz@1224
    77
 Looks up the application support directory and creates an app-specific subdirectory under it.
dz@1224
    78
dz@1224
    79
 Directories derived from it:
dz@1224
    80
dz@1224
    81
 * $HOME is set for the engine.
dz@1224
    82
 * The engine's per_user_directory (which is placed under $HOME).
dz@1224
    83
 * The engine's per_machine_directory.
dz@1224
    84
dz@1224
    85
 @return A URL pointing to as app-specific directory under the OS defined
dz@1224
    86
         application support directory for the current user.
dz@1224
    87
 */
dirk@187
    88
+ (NSURL *)createApplicationDirectory
dirk@187
    89
{
dirk@187
    90
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
    91
    if (!bundleID) {
dirk@187
    92
        // This can happen in unit tests
dirk@187
    93
        bundleID = @"test";
dirk@187
    94
    }
dirk@187
    95
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
    96
    NSURL *dirPath = nil;
andreas@268
    97
    
dirk@187
    98
    // Find the application support directory in the home directory.
dirk@187
    99
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
   100
                                        inDomains:NSUserDomainMask];
dirk@187
   101
    if ([appSupportDir count] > 0)
dirk@187
   102
    {
dirk@187
   103
        // Append the bundle ID to the URL for the
dirk@187
   104
        // Application Support directory.
dirk@187
   105
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
   106
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
   107
        
dirk@187
   108
        // If the directory does not exist, this method creates it.
dirk@187
   109
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
   110
        NSError *theError = nil;
dirk@187
   111
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
   112
                           attributes:nil error:&theError])
dirk@187
   113
        {
dirk@187
   114
            // Handle the error.
dirk@187
   115
            return nil;
dirk@187
   116
        }
dirk@187
   117
    }
andreas@268
   118
    
dirk@187
   119
    return dirPath;
dirk@187
   120
}
dirk@187
   121
dz@1226
   122
/**
dz@1226
   123
 Sets the directory that will be fed into the engine's per_machine_directory.
dz@1226
   124
dz@1226
   125
 @param perMachineDir The url to use as the per_machine_directory directory.
dz@1226
   126
 */
dz@1226
   127
+ (void)setPerMachineDirectory:(NSURL *)perMachineDir
dirk@187
   128
{
andreas@1173
   129
#if TARGET_OS_IPHONE
andreas@1175
   130
    if (perMachineDirectory) {
andreas@1175
   131
        free((void *) perMachineDirectory);
andreas@1173
   132
    }
dz@1226
   133
    perMachineDirectory = strdup([perMachineDir path].UTF8String);
andreas@1173
   134
#endif
dirk@187
   135
}
dirk@187
   136
andreas@276
   137
+ (NSString *)getBundlePathFor: (NSString *) filename
dirk@187
   138
{
dirk@187
   139
    return nil;
dirk@187
   140
}
dirk@187
   141
andreas@1175
   142
+ (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
dirk@442
   143
                                    bundleName:(NSString *)bundleName
dirk@442
   144
                                      fileName:(NSString *)fileName {
andreas@1175
   145
andreas@1175
   146
    NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
andreas@268
   147
    
andreas@1175
   148
    if(!(systemDir && bundleName && fileName))
andreas@1175
   149
        return;
dirk@187
   150
    
dirk@187
   151
    // Check if the database file exists in the documents directory.
andreas@1175
   152
    NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
dirk@187
   153
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   154
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   155
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   156
                               [[rootBundle resourcePath]
dirk@187
   157
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   158
        if (!bundleObj)
andreas@1175
   159
            return;
dirk@187
   160
        
dirk@187
   161
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   162
        
dirk@187
   163
        NSError *error;
dirk@187
   164
        [[NSFileManager defaultManager]
dirk@187
   165
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   166
        
dirk@187
   167
        // Check if any error occurred during copying and display it.
dirk@187
   168
        if (error != nil) {
dirk@187
   169
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   170
        }
dirk@187
   171
    }
dirk@187
   172
}
dirk@187
   173
andreas@1173
   174
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle {
andreas@1175
   175
// iOS to force us to think about macOS
andreas@1173
   176
#if TARGET_OS_IPHONE
andreas@1175
   177
    [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
andreas@1175
   178
                                          bundleName:@"pEpTrustWords.bundle"
andreas@1175
   179
                                            fileName:@"system.db"];
andreas@1175
   180
andreas@1173
   181
#endif
dirk@187
   182
}
dirk@187
   183
dirk@187
   184
+ (void)setupTrustWordsDB
dirk@187
   185
{
dirk@187
   186
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   187
}
dirk@187
   188
dz@1215
   189
dz@1216
   190
+ (NSString * _Nonnull)perUserDirectoryString
dz@1215
   191
{
dz@1215
   192
    return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
dz@1215
   193
}
dz@1215
   194
dz@1216
   195
+ (NSString * _Nonnull)perMachineDirectoryString
dz@1215
   196
{
dz@1215
   197
    return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
dz@1215
   198
}
dz@1215
   199
dirk@187
   200
@end