pEpObjCAdapter/PEPObjCAdapter.m
author buff <andreas@pep-project.org>
Sat, 31 Aug 2019 15:25:23 +0200
branchIOS-1784
changeset 1175 afeecbeb15bd
parent 1174 0700139e399c
child 1211 7bf9f33c3cf6
permissions -rw-r--r--
IOS-1784 renames method that does not set any more
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];
andreas@1175
    63
    [self setPerMachineDirectory:s_homeURL]; // Important, defines $HOME for the engine //???: how I understand the OS does define HOME
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
dirk@187
    71
+ (NSURL *)createApplicationDirectory
dirk@187
    72
{
dirk@187
    73
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
    74
    if (!bundleID) {
dirk@187
    75
        // This can happen in unit tests
dirk@187
    76
        bundleID = @"test";
dirk@187
    77
    }
dirk@187
    78
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
    79
    NSURL *dirPath = nil;
andreas@268
    80
    
dirk@187
    81
    // Find the application support directory in the home directory.
dirk@187
    82
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
    83
                                        inDomains:NSUserDomainMask];
dirk@187
    84
    if ([appSupportDir count] > 0)
dirk@187
    85
    {
dirk@187
    86
        // Append the bundle ID to the URL for the
dirk@187
    87
        // Application Support directory.
dirk@187
    88
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
    89
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
    90
        
dirk@187
    91
        // If the directory does not exist, this method creates it.
dirk@187
    92
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
    93
        NSError *theError = nil;
dirk@187
    94
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
    95
                           attributes:nil error:&theError])
dirk@187
    96
        {
dirk@187
    97
            // Handle the error.
dirk@187
    98
            return nil;
dirk@187
    99
        }
dirk@187
   100
    }
andreas@268
   101
    
dirk@187
   102
    return dirPath;
dirk@187
   103
}
dirk@187
   104
andreas@1175
   105
+ (void)setPerMachineDirectory:(NSURL *)homeDir
dirk@187
   106
{
andreas@1173
   107
#if TARGET_OS_IPHONE
andreas@1175
   108
    if (perMachineDirectory) {
andreas@1175
   109
        free((void *) perMachineDirectory);
andreas@1173
   110
    }
andreas@1175
   111
    perMachineDirectory = strdup([homeDir path].UTF8String);
andreas@1173
   112
#endif
dirk@187
   113
}
dirk@187
   114
andreas@276
   115
+ (NSString *)getBundlePathFor: (NSString *) filename
dirk@187
   116
{
dirk@187
   117
    return nil;
dirk@187
   118
}
dirk@187
   119
andreas@1175
   120
+ (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
dirk@442
   121
                                    bundleName:(NSString *)bundleName
dirk@442
   122
                                      fileName:(NSString *)fileName {
andreas@1175
   123
andreas@1175
   124
    NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
andreas@268
   125
    
andreas@1175
   126
    if(!(systemDir && bundleName && fileName))
andreas@1175
   127
        return;
dirk@187
   128
    
dirk@187
   129
    // Check if the database file exists in the documents directory.
andreas@1175
   130
    NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
dirk@187
   131
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   132
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   133
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   134
                               [[rootBundle resourcePath]
dirk@187
   135
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   136
        if (!bundleObj)
andreas@1175
   137
            return;
dirk@187
   138
        
dirk@187
   139
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   140
        
dirk@187
   141
        NSError *error;
dirk@187
   142
        [[NSFileManager defaultManager]
dirk@187
   143
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   144
        
dirk@187
   145
        // Check if any error occurred during copying and display it.
dirk@187
   146
        if (error != nil) {
dirk@187
   147
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   148
        }
dirk@187
   149
    }
dirk@187
   150
}
dirk@187
   151
andreas@1173
   152
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle {
andreas@1175
   153
// iOS to force us to think about macOS
andreas@1173
   154
#if TARGET_OS_IPHONE
andreas@1175
   155
    [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
andreas@1175
   156
                                          bundleName:@"pEpTrustWords.bundle"
andreas@1175
   157
                                            fileName:@"system.db"];
andreas@1175
   158
andreas@1173
   159
#endif
dirk@187
   160
}
dirk@187
   161
dirk@187
   162
+ (void)setupTrustWordsDB
dirk@187
   163
{
dirk@187
   164
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   165
}
dirk@187
   166
dirk@187
   167
@end